tmf: Deprecate TimeGraphCombo and support columns in TimeGraphViewer
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / widgets / timegraph / widgets / TimeGraphControl.java
1 /*****************************************************************************
2 * Copyright (c) 2007, 2016 Intel Corporation and others
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
20
21 package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets;
22
23 import java.util.AbstractMap.SimpleEntry;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.LinkedHashMap;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Queue;
35
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.eclipse.jface.action.IStatusLineManager;
38 import org.eclipse.jface.resource.JFaceResources;
39 import org.eclipse.jface.resource.LocalResourceManager;
40 import org.eclipse.jface.viewers.AbstractTreeViewer;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.ISelectionChangedListener;
43 import org.eclipse.jface.viewers.ISelectionProvider;
44 import org.eclipse.jface.viewers.IStructuredSelection;
45 import org.eclipse.jface.viewers.ITableLabelProvider;
46 import org.eclipse.jface.viewers.SelectionChangedEvent;
47 import org.eclipse.jface.viewers.StructuredSelection;
48 import org.eclipse.jface.viewers.ViewerFilter;
49 import org.eclipse.osgi.util.NLS;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.events.ControlEvent;
52 import org.eclipse.swt.events.ControlListener;
53 import org.eclipse.swt.events.FocusEvent;
54 import org.eclipse.swt.events.FocusListener;
55 import org.eclipse.swt.events.KeyEvent;
56 import org.eclipse.swt.events.KeyListener;
57 import org.eclipse.swt.events.MenuDetectEvent;
58 import org.eclipse.swt.events.MenuDetectListener;
59 import org.eclipse.swt.events.MouseEvent;
60 import org.eclipse.swt.events.MouseListener;
61 import org.eclipse.swt.events.MouseMoveListener;
62 import org.eclipse.swt.events.MouseTrackListener;
63 import org.eclipse.swt.events.MouseWheelListener;
64 import org.eclipse.swt.events.PaintEvent;
65 import org.eclipse.swt.events.SelectionListener;
66 import org.eclipse.swt.events.TraverseEvent;
67 import org.eclipse.swt.events.TraverseListener;
68 import org.eclipse.swt.events.TypedEvent;
69 import org.eclipse.swt.graphics.Color;
70 import org.eclipse.swt.graphics.Cursor;
71 import org.eclipse.swt.graphics.Font;
72 import org.eclipse.swt.graphics.FontData;
73 import org.eclipse.swt.graphics.GC;
74 import org.eclipse.swt.graphics.Image;
75 import org.eclipse.swt.graphics.Point;
76 import org.eclipse.swt.graphics.Rectangle;
77 import org.eclipse.swt.widgets.Composite;
78 import org.eclipse.swt.widgets.Display;
79 import org.eclipse.swt.widgets.Event;
80 import org.eclipse.swt.widgets.Listener;
81 import org.eclipse.swt.widgets.Menu;
82 import org.eclipse.swt.widgets.Tree;
83 import org.eclipse.swt.widgets.TreeColumn;
84 import org.eclipse.tracecompass.common.core.math.SaturatedArithmetic;
85 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
86 import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo;
87 import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal;
88 import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned;
89 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphColorListener;
90 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;
91 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
92 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener;
93 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTreeListener;
94 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem;
95 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent;
96 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent;
97 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
98 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent;
99 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
100 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
101 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
102 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
103
104 import com.google.common.collect.Iterables;
105
106 /**
107 * Time graph control implementation
108 *
109 * @author Alvaro Sanchez-Leon
110 * @author Patrick Tasse
111 */
112 public class TimeGraphControl extends TimeGraphBaseControl
113 implements FocusListener, KeyListener, MouseMoveListener, MouseListener,
114 MouseWheelListener, MouseTrackListener, TraverseListener, ISelectionProvider,
115 MenuDetectListener, ITmfTimeGraphDrawingHelper, ITimeGraphColorListener, Listener {
116
117 /** Constant indicating that all levels of the time graph should be expanded */
118 public static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS;
119
120 private static final int DRAG_MARGIN = 5;
121
122 private static final int DRAG_NONE = 0;
123 private static final int DRAG_TRACE_ITEM = 1;
124 private static final int DRAG_SPLIT_LINE = 2;
125 private static final int DRAG_ZOOM = 3;
126 private static final int DRAG_SELECTION = 4;
127
128 private static final int CUSTOM_ITEM_HEIGHT = -1; // get item height from provider
129
130 private static final double ZOOM_FACTOR = 1.5;
131 private static final double ZOOM_IN_FACTOR = 0.8;
132 private static final double ZOOM_OUT_FACTOR = 1.25;
133
134 private static final int SNAP_WIDTH = 3;
135 private static final int ARROW_HOVER_MAX_DIST = 5;
136
137 private static double ARROW_RATIO = Math.sqrt(3) / 2; // base to height ratio
138
139 private static final int NO_STATUS = -1;
140 private static final int STATUS_WITHOUT_CURSOR_TIME = -2;
141
142 private static final int MAX_LABEL_LENGTH = 256;
143
144 private static final int PPI = 72; // points per inch
145 private static final int DPI = Display.getDefault().getDPI().y;
146
147 private static final int VERTICAL_ZOOM_DELAY = 400;
148
149 private static final String PREFERRED_WIDTH = "width"; //$NON-NLS-1$
150
151 /** Resource manager */
152 private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources());
153
154 /** Color map for event types */
155 private Color[] fEventColorMap = null;
156
157 private ITimeDataProvider fTimeProvider;
158 private ITableLabelProvider fLabelProvider;
159 private IStatusLineManager fStatusLineManager = null;
160 private Tree fTree = null;
161 private TimeGraphScale fTimeGraphScale = null;
162
163 private boolean fIsInFocus = false;
164 private boolean fMouseOverSplitLine = false;
165 private int fGlobalItemHeight = CUSTOM_ITEM_HEIGHT;
166 private int fHeightAdjustment = 0;
167 private Map<Integer, Font> fFonts = new HashMap<>();
168 private boolean fBlendSubPixelEvents = false;
169 private int fMinimumItemWidth = 0;
170 private int fTopIndex = 0;
171 private int fDragState = DRAG_NONE;
172 private boolean fDragBeginMarker = false;
173 private int fDragButton;
174 private int fDragX0 = 0;
175 private int fDragX = 0;
176 private boolean fHasNamespaceFocus = false;
177 private long fDragTime0 = 0; // used to preserve accuracy of modified selection
178 private int fIdealNameSpace = 0;
179 private boolean fAutoResizeColumns = true;
180 private long fTime0bak;
181 private long fTime1bak;
182 private ITimeGraphPresentationProvider fTimeGraphProvider = null;
183 private ItemData fItemData = null;
184 private List<IMarkerEvent> fMarkers = null;
185 private boolean fMarkersVisible = true;
186 private List<SelectionListener> fSelectionListeners;
187 private List<ITimeGraphTimeListener> fDragSelectionListeners;
188 private final List<ISelectionChangedListener> fSelectionChangedListeners = new ArrayList<>();
189 private final List<ITimeGraphTreeListener> fTreeListeners = new ArrayList<>();
190 private final List<MenuDetectListener> fTimeGraphEntryMenuListeners = new ArrayList<>();
191 private final List<MenuDetectListener> fTimeEventMenuListeners = new ArrayList<>();
192 private final Cursor fDragCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND);
193 private final Cursor fResizeCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_IBEAM);
194 private final Cursor fWaitCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT);
195 private final Cursor fZoomCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEWE);
196 private final List<@NonNull ViewerFilter> fFilters = new ArrayList<>();
197 private MenuDetectEvent fPendingMenuDetectEvent = null;
198 private boolean fGridLinesVisible = true;
199 private Color fGridLineColor = Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
200 private boolean fHideArrows = false;
201 private int fAutoExpandLevel = ALL_LEVELS;
202 private Entry<ITimeGraphEntry, Integer> fVerticalZoomAlignEntry = null;
203 private long fVerticalZoomAlignTime = 0;
204 private int fBorderWidth = 0;
205 private int fHeaderHeight = 0;
206
207 /**
208 * Standard constructor
209 *
210 * @param parent
211 * The parent composite object
212 * @param colors
213 * The color scheme to use
214 */
215 public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) {
216
217 super(parent, colors, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
218
219 fItemData = new ItemData();
220
221 addFocusListener(this);
222 addMouseListener(this);
223 addMouseMoveListener(this);
224 addMouseTrackListener(this);
225 addMouseWheelListener(this);
226 addTraverseListener(this);
227 addKeyListener(this);
228 addMenuDetectListener(this);
229 addListener(SWT.MouseWheel, this);
230 addDisposeListener((e) -> {
231 fResourceManager.dispose();
232 for (Font font : fFonts.values()) {
233 font.dispose();
234 }
235 });
236 }
237
238 /**
239 * Sets the timegraph provider used by this timegraph viewer.
240 *
241 * @param timeGraphProvider the timegraph provider
242 */
243 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) {
244 fTimeGraphProvider = timeGraphProvider;
245
246 if (timeGraphProvider instanceof ITimeGraphPresentationProvider2) {
247 ((ITimeGraphPresentationProvider2) timeGraphProvider).setDrawingHelper(this);
248 ((ITimeGraphPresentationProvider2) timeGraphProvider).addColorListener(this);
249 }
250
251 StateItem[] stateItems = fTimeGraphProvider.getStateTable();
252 colorSettingsChanged(stateItems);
253 }
254
255 /**
256 * Gets the timegraph provider used by this timegraph viewer.
257 *
258 * @return the timegraph provider, or <code>null</code> if not set.
259 */
260 public ITimeGraphPresentationProvider getTimeGraphProvider() {
261 return fTimeGraphProvider;
262 }
263
264 /**
265 * Gets the time data provider used by this viewer.
266 *
267 * @return The time data provider, or <code>null</code> if not set
268 * @since 2.1
269 */
270 public ITimeDataProvider getTimeDataProvider() {
271 return fTimeProvider;
272 }
273
274 /**
275 * Gets the color map used by this timegraph viewer.
276 *
277 * @return a color map, or <code>null</code> if not set.
278 */
279 public Color[] getEventColorMap() {
280 return fEventColorMap;
281 }
282
283 /**
284 * Assign the given time provider
285 *
286 * @param timeProvider
287 * The time provider
288 */
289 public void setTimeProvider(ITimeDataProvider timeProvider) {
290 fTimeProvider = timeProvider;
291 redraw();
292 }
293
294 /**
295 * Set the label provider for the name space
296 *
297 * @param labelProvider
298 * The label provider
299 * @since 2.3
300 */
301 public void setLabelProvider(ITableLabelProvider labelProvider) {
302 fLabelProvider = labelProvider;
303 redraw();
304 }
305
306 /**
307 * Get the label provider for the name space
308 *
309 * @return The label provider
310 * @since 2.3
311 */
312 public ITableLabelProvider getLabelProvider() {
313 return fLabelProvider;
314 }
315
316 /**
317 * Assign the status line manager
318 *
319 * @param statusLineManager
320 * The status line manager, or null to disable status line messages
321 */
322 public void setStatusLineManager(IStatusLineManager statusLineManager) {
323 if (fStatusLineManager != null && statusLineManager == null) {
324 fStatusLineManager.setMessage(""); //$NON-NLS-1$
325 }
326 fStatusLineManager = statusLineManager;
327 }
328
329 /**
330 * Assign the tree that represents the name space header
331 *
332 * @param tree
333 * The tree
334 * @since 2.3
335 */
336 public void setTree(Tree tree) {
337 fTree = tree;
338 }
339
340 /**
341 * Returns the tree control associated with this time graph control. The
342 * tree is only used for column handling of the name space and contains no
343 * tree items.
344 *
345 * @return the tree control
346 * @since 2.3
347 */
348 public Tree getTree() {
349 return fTree;
350 }
351
352 /**
353 * Sets the columns for this time graph control's name space.
354 *
355 * @param columnNames
356 * the column names
357 * @since 2.3
358 */
359 public void setColumns(String[] columnNames) {
360 for (TreeColumn column : fTree.getColumns()) {
361 column.dispose();
362 }
363 ControlListener controlListener = new ControlListener() {
364 @Override
365 public void controlResized(ControlEvent e) {
366 if (fAutoResizeColumns && ((TreeColumn) e.widget).getWidth() < (Integer) e.widget.getData(PREFERRED_WIDTH)) {
367 fAutoResizeColumns = false;
368 }
369 redraw();
370 }
371 @Override
372 public void controlMoved(ControlEvent e) {
373 redraw();
374 }
375 };
376 for (String columnName : columnNames) {
377 TreeColumn column = new TreeColumn(fTree, SWT.LEFT);
378 column.setMoveable(true);
379 column.setText(columnName);
380 column.pack();
381 column.setData(PREFERRED_WIDTH, column.getWidth());
382 column.addControlListener(controlListener);
383 }
384 }
385
386 /**
387 * Assign the time graph scale
388 *
389 * @param timeGraphScale
390 * The time graph scale
391 */
392 public void setTimeGraphScale(TimeGraphScale timeGraphScale) {
393 fTimeGraphScale = timeGraphScale;
394 }
395
396 /**
397 * Add a selection listener
398 *
399 * @param listener
400 * The listener to add
401 */
402 public void addSelectionListener(SelectionListener listener) {
403 if (listener == null) {
404 SWT.error(SWT.ERROR_NULL_ARGUMENT);
405 }
406 if (null == fSelectionListeners) {
407 fSelectionListeners = new ArrayList<>();
408 }
409 fSelectionListeners.add(listener);
410 }
411
412 /**
413 * Remove a selection listener
414 *
415 * @param listener
416 * The listener to remove
417 */
418 public void removeSelectionListener(SelectionListener listener) {
419 if (null != fSelectionListeners) {
420 fSelectionListeners.remove(listener);
421 }
422 }
423
424 /**
425 * Selection changed callback
426 */
427 public void fireSelectionChanged() {
428 if (null != fSelectionListeners) {
429 Iterator<SelectionListener> it = fSelectionListeners.iterator();
430 while (it.hasNext()) {
431 SelectionListener listener = it.next();
432 listener.widgetSelected(null);
433 }
434 }
435
436 if (null != fSelectionChangedListeners) {
437 for (ISelectionChangedListener listener : fSelectionChangedListeners) {
438 listener.selectionChanged(new SelectionChangedEvent(this, getSelection()));
439 }
440 }
441 }
442
443 /**
444 * Default selection callback
445 */
446 public void fireDefaultSelection() {
447 if (null != fSelectionListeners) {
448 Iterator<SelectionListener> it = fSelectionListeners.iterator();
449 while (it.hasNext()) {
450 SelectionListener listener = it.next();
451 listener.widgetDefaultSelected(null);
452 }
453 }
454 }
455
456 /**
457 * Add a drag selection listener
458 *
459 * @param listener
460 * The listener to add
461 */
462 public void addDragSelectionListener(ITimeGraphTimeListener listener) {
463 if (listener == null) {
464 SWT.error(SWT.ERROR_NULL_ARGUMENT);
465 }
466 if (null == fDragSelectionListeners) {
467 fDragSelectionListeners = new ArrayList<>();
468 }
469 fDragSelectionListeners.add(listener);
470 }
471
472 /**
473 * Remove a drag selection listener
474 *
475 * @param listener
476 * The listener to remove
477 */
478 public void removeDragSelectionListener(ITimeGraphTimeListener listener) {
479 if (null != fDragSelectionListeners) {
480 fDragSelectionListeners.remove(listener);
481 }
482 }
483
484 /**
485 * Drag Selection changed callback
486 *
487 * @param start
488 * Time interval start
489 * @param end
490 * Time interval end
491 */
492 public void fireDragSelectionChanged(long start, long end) {
493 // check for backward intervals
494 long beginTime, endTime;
495 if (start > end) {
496 beginTime = end;
497 endTime = start;
498 } else {
499 beginTime = start;
500 endTime = end;
501 }
502 // call the listeners
503 if (null != fDragSelectionListeners) {
504 Iterator<ITimeGraphTimeListener> it = fDragSelectionListeners.iterator();
505 while (it.hasNext()) {
506 ITimeGraphTimeListener listener = it.next();
507 listener.timeSelected(new TimeGraphTimeEvent(this, beginTime, endTime));
508 }
509 }
510 }
511
512 /**
513 * Get the traces in the model
514 *
515 * @return The array of traces
516 */
517 public ITimeGraphEntry[] getTraces() {
518 return fItemData.getEntries();
519 }
520
521 /**
522 * Refresh the data for the thing
523 */
524 public void refreshData() {
525 fItemData.refreshData();
526 redraw();
527 }
528
529 /**
530 * Refresh data for the given traces
531 *
532 * @param traces
533 * The traces to refresh
534 */
535 public void refreshData(ITimeGraphEntry[] traces) {
536 fItemData.refreshData(traces);
537 redraw();
538 }
539
540 /**
541 * Refresh the links (arrows) of this widget
542 *
543 * @param events The link event list
544 */
545 public void refreshArrows(List<ILinkEvent> events) {
546 fItemData.refreshArrows(events);
547 }
548
549 /**
550 * Get the links (arrows) of this widget
551 *
552 * @return The unmodifiable link event list
553 *
554 * @since 1.1
555 */
556 public List<ILinkEvent> getArrows() {
557 return Collections.unmodifiableList(fItemData.fLinks);
558 }
559
560 boolean ensureVisibleItem(int idx, boolean redraw) {
561 boolean changed = false;
562 int index = idx;
563 if (index < 0) {
564 for (index = 0; index < fItemData.fExpandedItems.length; index++) {
565 if (fItemData.fExpandedItems[index].fSelected) {
566 break;
567 }
568 }
569 }
570 if (index >= fItemData.fExpandedItems.length) {
571 return changed;
572 }
573 if (index < fTopIndex) {
574 setTopIndex(index);
575 if (redraw) {
576 redraw();
577 }
578 changed = true;
579 } else {
580 int page = countPerPage();
581 if (index >= fTopIndex + page) {
582 setTopIndex(index - page + 1);
583 if (redraw) {
584 redraw();
585 }
586 changed = true;
587 }
588 }
589 return changed;
590 }
591
592 /**
593 * Assign the given index as the top one
594 *
595 * @param idx
596 * The index
597 */
598 public void setTopIndex(int idx) {
599 int index = Math.min(idx, fItemData.fExpandedItems.length - countPerPage());
600 index = Math.max(0, index);
601 fTopIndex = index;
602 redraw();
603 }
604
605 /**
606 * Set the top index so that the requested element is at the specified
607 * position.
608 *
609 * @param entry
610 * the time graph entry to be positioned
611 * @param y
612 * the requested y-coordinate
613 * @since 2.0
614 */
615 public void setElementPosition(ITimeGraphEntry entry, int y) {
616 Item item = fItemData.fItemMap.get(entry);
617 if (item == null || item.fExpandedIndex == -1) {
618 return;
619 }
620 int index = item.fExpandedIndex;
621 Rectangle itemRect = getItemRect(getClientArea(), index);
622 int delta = itemRect.y + itemRect.height - y;
623 int topIndex = getItemIndexAtY(delta);
624 if (topIndex != -1) {
625 setTopIndex(topIndex);
626 } else {
627 if (delta < 0) {
628 setTopIndex(0);
629 } else {
630 setTopIndex(getExpandedElementCount());
631 }
632 }
633 }
634
635 /**
636 * Sets the auto-expand level to be used for new entries discovered when
637 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
638 * . The value 0 means that there is no auto-expand; 1 means that top-level
639 * entries are expanded, but not their children; 2 means that top-level
640 * entries are expanded, and their children, but not grand-children; and so
641 * on.
642 * <p>
643 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
644 * </p>
645 *
646 * @param level
647 * non-negative level, or <code>ALL_LEVELS</code> to expand all
648 * levels of the tree
649 */
650 public void setAutoExpandLevel(int level) {
651 fAutoExpandLevel = level;
652 }
653
654 /**
655 * Returns the auto-expand level.
656 *
657 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
658 * the tree are expanded automatically
659 * @see #setAutoExpandLevel
660 */
661 public int getAutoExpandLevel() {
662 return fAutoExpandLevel;
663 }
664
665 /**
666 * Get the expanded state of a given entry.
667 *
668 * @param entry
669 * The entry
670 * @return true if the entry is expanded, false if collapsed
671 * @since 1.1
672 */
673 public boolean getExpandedState(ITimeGraphEntry entry) {
674 Item item = fItemData.fItemMap.get(entry);
675 return (item != null ? item.fExpanded : false);
676 }
677
678 /**
679 * Set the expanded state of a given entry
680 *
681 * @param entry
682 * The entry
683 * @param expanded
684 * True if expanded, false if collapsed
685 */
686 public void setExpandedState(ITimeGraphEntry entry, boolean expanded) {
687 Item item = fItemData.findItem(entry);
688 if (item != null && item.fExpanded != expanded) {
689 item.fExpanded = expanded;
690 fItemData.updateExpandedItems();
691 redraw();
692 }
693 }
694
695 /**
696 * Set the expanded state of a given entry to certain relative level.
697 * It will call fireTreeEvent() for each changed entry. At the end
698 * it will call redraw().
699 *
700 * @param entry
701 * The entry
702 * @param level
703 * level to expand to or negative for all levels
704 * @param expanded
705 * True if expanded, false if collapsed
706 */
707 private void setExpandedState(ITimeGraphEntry entry, int level, boolean expanded) {
708 setExpandedStateInt(entry, level, expanded);
709 redraw();
710 }
711
712 /**
713 * Set the expanded state of a given entry and its children to the first
714 * level that has one collapsed entry.
715 *
716 * @param entry
717 * The entry
718 */
719 private void setExpandedStateLevel(ITimeGraphEntry entry) {
720 int level = findExpandedLevel(entry);
721 if (level >= 0) {
722 setExpandedStateInt(entry, level, true);
723 redraw();
724 }
725 }
726
727 /*
728 * Inner class for finding relative level with at least one
729 * collapsed entry.
730 */
731 private class SearchNode {
732 SearchNode(ITimeGraphEntry e, int l) {
733 entry = e;
734 level = l;
735 }
736 ITimeGraphEntry entry;
737 int level;
738 }
739
740 /**
741 * Finds the relative level with at least one collapsed entry.
742 *
743 * @param entry
744 * the start entry
745 * @return the found level or -1 if all levels are already expanded.
746 */
747 private int findExpandedLevel(ITimeGraphEntry entry) {
748 Queue<SearchNode> queue = new LinkedList<>();
749 SearchNode root = new SearchNode(entry, 0);
750 SearchNode node = root;
751 queue.add(root);
752
753 while (!queue.isEmpty()) {
754 node = queue.remove();
755 if (node.entry.hasChildren() && !getExpandedState(node.entry)) {
756 return node.level;
757 }
758 for (ITimeGraphEntry e : node.entry.getChildren()) {
759 if (e.hasChildren()) {
760 SearchNode n = new SearchNode(e, node.level + 1);
761 queue.add(n);
762 }
763 }
764 }
765 return -1;
766 }
767
768 /**
769 * Set the expanded state of a given entry to certain relative level.
770 * It will call fireTreeEvent() for each changed entry. No redraw is done.
771 *
772 * @param entry
773 * The entry
774 * @param level
775 * level to expand to or negative for all levels
776 * @param expanded
777 * True if expanded, false if collapsed
778 */
779 private void setExpandedStateInt(ITimeGraphEntry entry, int aLevel, boolean expanded) {
780 int level = aLevel;
781 if ((level > 0) || (level < 0)) {
782 level--;
783 if (entry.hasChildren()) {
784 for (ITimeGraphEntry e : entry.getChildren()) {
785 setExpandedStateInt(e, level, expanded);
786 }
787 }
788 }
789 Item item = fItemData.findItem(entry);
790 if (item != null && item.fExpanded != expanded) {
791 item.fExpanded = expanded;
792 fItemData.updateExpandedItems();
793 fireTreeEvent(item.fEntry, item.fExpanded);
794 }
795 }
796
797 /**
798 * Collapses all nodes of the viewer's tree, starting with the root.
799 */
800 public void collapseAll() {
801 for (Item item : fItemData.fItems) {
802 item.fExpanded = false;
803 }
804 fItemData.updateExpandedItems();
805 redraw();
806 }
807
808 /**
809 * Expands all nodes of the viewer's tree, starting with the root.
810 */
811 public void expandAll() {
812 for (Item item : fItemData.fItems) {
813 item.fExpanded = true;
814 }
815 fItemData.updateExpandedItems();
816 redraw();
817 }
818
819 /**
820 * Add a tree listener
821 *
822 * @param listener
823 * The listener to add
824 */
825 public void addTreeListener(ITimeGraphTreeListener listener) {
826 if (!fTreeListeners.contains(listener)) {
827 fTreeListeners.add(listener);
828 }
829 }
830
831 /**
832 * Remove a tree listener
833 *
834 * @param listener
835 * The listener to remove
836 */
837 public void removeTreeListener(ITimeGraphTreeListener listener) {
838 if (fTreeListeners.contains(listener)) {
839 fTreeListeners.remove(listener);
840 }
841 }
842
843 /**
844 * Tree event callback
845 *
846 * @param entry
847 * The affected entry
848 * @param expanded
849 * The expanded state (true for expanded, false for collapsed)
850 */
851 public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) {
852 TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry);
853 for (ITimeGraphTreeListener listener : fTreeListeners) {
854 if (expanded) {
855 listener.treeExpanded(event);
856 } else {
857 listener.treeCollapsed(event);
858 }
859 }
860 }
861
862 /**
863 * Add a menu listener on {@link ITimeGraphEntry}s
864 *
865 * @param listener
866 * The listener to add
867 */
868 public void addTimeGraphEntryMenuListener(MenuDetectListener listener) {
869 if (!fTimeGraphEntryMenuListeners.contains(listener)) {
870 fTimeGraphEntryMenuListeners.add(listener);
871 }
872 }
873
874 /**
875 * Remove a menu listener on {@link ITimeGraphEntry}s
876 *
877 * @param listener
878 * The listener to remove
879 */
880 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) {
881 if (fTimeGraphEntryMenuListeners.contains(listener)) {
882 fTimeGraphEntryMenuListeners.remove(listener);
883 }
884 }
885
886 /**
887 * Menu event callback on {@link ITimeGraphEntry}s
888 *
889 * @param event
890 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
891 */
892 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) {
893 for (MenuDetectListener listener : fTimeGraphEntryMenuListeners) {
894 listener.menuDetected(event);
895 }
896 }
897
898 /**
899 * Add a menu listener on {@link ITimeEvent}s
900 *
901 * @param listener
902 * The listener to add
903 */
904 public void addTimeEventMenuListener(MenuDetectListener listener) {
905 if (!fTimeEventMenuListeners.contains(listener)) {
906 fTimeEventMenuListeners.add(listener);
907 }
908 }
909
910 /**
911 * Remove a menu listener on {@link ITimeEvent}s
912 *
913 * @param listener
914 * The listener to remove
915 */
916 public void removeTimeEventMenuListener(MenuDetectListener listener) {
917 if (fTimeEventMenuListeners.contains(listener)) {
918 fTimeEventMenuListeners.remove(listener);
919 }
920 }
921
922 /**
923 * Menu event callback on {@link ITimeEvent}s
924 *
925 * @param event
926 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
927 */
928 private void fireMenuEventOnTimeEvent(MenuDetectEvent event) {
929 for (MenuDetectListener listener : fTimeEventMenuListeners) {
930 listener.menuDetected(event);
931 }
932 }
933
934 @Override
935 public boolean setFocus() {
936 if ((fTimeProvider != null) && fTimeProvider.getNameSpace() > 0) {
937 fHasNamespaceFocus = true;
938 }
939 return super.setFocus();
940 }
941
942 /**
943 * Returns the current selection for this time graph. If a time graph entry
944 * is selected, it will be the first element in the selection. If a time
945 * event is selected, it will be the second element in the selection.
946 *
947 * @return the current selection
948 */
949 @Override
950 public ISelection getSelection() {
951 ITimeGraphEntry entry = getSelectedTrace();
952 if (null != entry && null != fTimeProvider) {
953 long selectedTime = fTimeProvider.getSelectionBegin();
954 ITimeEvent event = Utils.findEvent(entry, selectedTime, 0);
955 if (event == null) {
956 return new StructuredSelection(entry);
957 }
958 return new StructuredSelection(new Object[] { entry, event });
959 }
960 return StructuredSelection.EMPTY;
961 }
962
963 /**
964 * Get the selection object
965 *
966 * @return The selection
967 */
968 public ISelection getSelectionTrace() {
969 ITimeGraphEntry entry = getSelectedTrace();
970 if (null != entry) {
971 return new StructuredSelection(entry);
972 }
973 return StructuredSelection.EMPTY;
974 }
975
976 /**
977 * Enable/disable one of the traces in the model
978 *
979 * @param n
980 * 1 to enable it, -1 to disable. The method returns immediately
981 * if another value is used.
982 */
983 public void selectTrace(int n) {
984 if ((n != 1) && (n != -1)) {
985 return;
986 }
987
988 boolean changed = false;
989 int lastSelection = -1;
990 for (int i = 0; i < fItemData.fExpandedItems.length; i++) {
991 Item item = fItemData.fExpandedItems[i];
992 if (item.fSelected) {
993 lastSelection = i;
994 if ((1 == n) && (i < fItemData.fExpandedItems.length - 1)) {
995 item.fSelected = false;
996 item = fItemData.fExpandedItems[i + 1];
997 item.fSelected = true;
998 changed = true;
999 } else if ((-1 == n) && (i > 0)) {
1000 item.fSelected = false;
1001 item = fItemData.fExpandedItems[i - 1];
1002 item.fSelected = true;
1003 changed = true;
1004 }
1005 break;
1006 }
1007 }
1008
1009 if (lastSelection < 0 && fItemData.fExpandedItems.length > 0) {
1010 Item item = fItemData.fExpandedItems[0];
1011 item.fSelected = true;
1012 changed = true;
1013 }
1014
1015 if (changed) {
1016 ensureVisibleItem(-1, false);
1017 redraw();
1018 fireSelectionChanged();
1019 }
1020 }
1021
1022 /**
1023 * Select an event
1024 *
1025 * @param n
1026 * 1 for next event, -1 for previous event
1027 * @param extend
1028 * true to extend selection range, false for single selection
1029 * @since 1.0
1030 */
1031 public void selectEvent(int n, boolean extend) {
1032 if (null == fTimeProvider) {
1033 return;
1034 }
1035 ITimeGraphEntry trace = getSelectedTrace();
1036 if (trace == null) {
1037 return;
1038 }
1039 long selectedTime = fTimeProvider.getSelectionEnd();
1040 long endTime = fTimeProvider.getMaxTime();
1041 ITimeEvent nextEvent;
1042 if (n == -1 && selectedTime > endTime) {
1043 nextEvent = Utils.findEvent(trace, selectedTime, 0);
1044 } else {
1045 nextEvent = Utils.findEvent(trace, selectedTime, n);
1046 }
1047 if (null == nextEvent && n == -1) {
1048 nextEvent = Utils.getFirstEvent(trace);
1049 }
1050 if (null != nextEvent) {
1051 long nextTime = nextEvent.getTime();
1052 // If last event detected e.g. going back or not moving to a next
1053 // event
1054 if (nextTime <= selectedTime && n == 1) {
1055 // Select to the end of this last event
1056 nextTime = nextEvent.getTime() + nextEvent.getDuration();
1057 // but not beyond the end of the trace
1058 if (nextTime > endTime) {
1059 nextTime = endTime;
1060 }
1061 } else if (n == -1 && nextEvent.getTime() + nextEvent.getDuration() < selectedTime) {
1062 // for previous event go to its end time unless we were already there
1063 nextTime = nextEvent.getTime() + nextEvent.getDuration();
1064 }
1065 if (extend) {
1066 fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), nextTime, true);
1067 } else {
1068 fTimeProvider.setSelectedTimeNotify(nextTime, true);
1069 }
1070 fireSelectionChanged();
1071 } else if (n == 1) {
1072 if (extend) {
1073 fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), endTime, true);
1074 } else {
1075 fTimeProvider.setSelectedTimeNotify(endTime, true);
1076 }
1077 fireSelectionChanged();
1078 }
1079 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
1080 }
1081
1082 /**
1083 * Select the next event
1084 *
1085 * @param extend
1086 * true to extend selection range, false for single selection
1087 * @since 1.0
1088 */
1089 public void selectNextEvent(boolean extend) {
1090 selectEvent(1, extend);
1091 // Notify if visible time window has been adjusted
1092 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1());
1093 }
1094
1095 /**
1096 * Select the previous event
1097 *
1098 * @param extend
1099 * true to extend selection range, false for single selection
1100 * @since 1.0
1101 */
1102 public void selectPrevEvent(boolean extend) {
1103 selectEvent(-1, extend);
1104 // Notify if visible time window has been adjusted
1105 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1());
1106 }
1107
1108 /**
1109 * Select the next trace
1110 */
1111 public void selectNextTrace() {
1112 selectTrace(1);
1113 }
1114
1115 /**
1116 * Select the previous trace
1117 */
1118 public void selectPrevTrace() {
1119 selectTrace(-1);
1120 }
1121
1122 /**
1123 * Scroll left or right by one half window size
1124 *
1125 * @param left
1126 * true to scroll left, false to scroll right
1127 */
1128 public void horizontalScroll(boolean left) {
1129 long time0 = fTimeProvider.getTime0();
1130 long time1 = fTimeProvider.getTime1();
1131 long timeMin = fTimeProvider.getMinTime();
1132 long timeMax = fTimeProvider.getMaxTime();
1133 long range = time1 - time0;
1134 if (range <= 0) {
1135 return;
1136 }
1137 long increment = Math.max(1, range / 2);
1138 if (left) {
1139 time0 = Math.max(time0 - increment, timeMin);
1140 time1 = time0 + range;
1141 } else {
1142 time1 = Math.min(time1 + increment, timeMax);
1143 time0 = time1 - range;
1144 }
1145 fTimeProvider.setStartFinishTimeNotify(time0, time1);
1146 }
1147
1148 /**
1149 * Zoom based on mouse cursor location with mouse scrolling
1150 *
1151 * @param zoomIn true to zoom in, false to zoom out
1152 */
1153 public void zoom(boolean zoomIn) {
1154 int globalX = getDisplay().getCursorLocation().x;
1155 Point p = toControl(globalX, 0);
1156 int nameSpace = fTimeProvider.getNameSpace();
1157 int timeSpace = fTimeProvider.getTimeSpace();
1158 int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x));
1159 long time0 = fTimeProvider.getTime0();
1160 long time1 = fTimeProvider.getTime1();
1161 long interval = time1 - time0;
1162 if (interval == 0) {
1163 interval = 1;
1164 } // to allow getting out of single point interval
1165 long newInterval;
1166 if (zoomIn) {
1167 newInterval = Math.max(Math.round(interval * ZOOM_IN_FACTOR), fTimeProvider.getMinTimeInterval());
1168 } else {
1169 newInterval = (long) Math.ceil(interval * ZOOM_OUT_FACTOR);
1170 }
1171 long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval));
1172 long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval);
1173 long newTime1 = newTime0 + newInterval;
1174 fTimeProvider.setStartFinishTimeNotify(newTime0, newTime1);
1175 }
1176
1177 /**
1178 * zoom in using single click
1179 */
1180 public void zoomIn() {
1181 long prevTime0 = fTimeProvider.getTime0();
1182 long prevTime1 = fTimeProvider.getTime1();
1183 long prevRange = prevTime1 - prevTime0;
1184 if (prevRange == 0) {
1185 return;
1186 }
1187 ITimeDataProvider provider = fTimeProvider;
1188 long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2;
1189 if (selTime <= prevTime0 || selTime >= prevTime1) {
1190 selTime = (prevTime0 + prevTime1) / 2;
1191 }
1192 long time0 = selTime - (long) ((selTime - prevTime0) / ZOOM_FACTOR);
1193 long time1 = selTime + (long) ((prevTime1 - selTime) / ZOOM_FACTOR);
1194
1195 long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0);
1196
1197 if (inaccuracy > 0 && inaccuracy < 100) {
1198 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime());
1199 return;
1200 }
1201
1202 long min = fTimeProvider.getMinTimeInterval();
1203 if ((time1 - time0) < min) {
1204 time0 = selTime - (selTime - prevTime0) * min / prevRange;
1205 time1 = time0 + min;
1206 }
1207
1208 fTimeProvider.setStartFinishTimeNotify(time0, time1);
1209 }
1210
1211 /**
1212 * zoom out using single click
1213 */
1214 public void zoomOut() {
1215 long prevTime0 = fTimeProvider.getTime0();
1216 long prevTime1 = fTimeProvider.getTime1();
1217 ITimeDataProvider provider = fTimeProvider;
1218 long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2;
1219 if (selTime <= prevTime0 || selTime >= prevTime1) {
1220 selTime = (prevTime0 + prevTime1) / 2;
1221 }
1222 long time0 = (long) (selTime - (selTime - prevTime0) * ZOOM_FACTOR);
1223 long time1 = (long) (selTime + (prevTime1 - selTime) * ZOOM_FACTOR);
1224
1225 long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0);
1226 if (inaccuracy > 0 && inaccuracy < 100) {
1227 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime());
1228 return;
1229 }
1230
1231 fTimeProvider.setStartFinishTimeNotify(time0, time1);
1232 }
1233
1234 /**
1235 * Zoom vertically.
1236 *
1237 * @param zoomIn
1238 * true to zoom in, false to zoom out
1239 * @since 2.0
1240 */
1241 public void verticalZoom(boolean zoomIn) {
1242 if (zoomIn) {
1243 fHeightAdjustment++;
1244 } else {
1245 fHeightAdjustment--;
1246 }
1247 fItemData.refreshData();
1248 redraw();
1249 }
1250
1251 /**
1252 * Reset the vertical zoom to default.
1253 *
1254 * @since 2.0
1255 */
1256 public void resetVerticalZoom() {
1257 fHeightAdjustment = 0;
1258 fItemData.refreshData();
1259 redraw();
1260 }
1261
1262 /**
1263 * Set the grid lines visibility. The default is true.
1264 *
1265 * @param visible
1266 * true to show the grid lines, false otherwise
1267 * @since 2.0
1268 */
1269 public void setGridLinesVisible(boolean visible) {
1270 fGridLinesVisible = visible;
1271 }
1272
1273 /**
1274 * Get the grid lines visibility.
1275 *
1276 * @return true if the grid lines are visible, false otherwise
1277 * @since 2.0
1278 */
1279 public boolean getGridLinesVisible() {
1280 return fGridLinesVisible;
1281 }
1282
1283 /**
1284 * Set the grid line color. The default is SWT.COLOR_GRAY.
1285 *
1286 * @param color
1287 * the grid line color
1288 * @since 2.0
1289 */
1290 public void setGridLineColor(Color color) {
1291 fGridLineColor = color;
1292 }
1293
1294 /**
1295 * Get the grid line color.
1296 *
1297 * @return the grid line color
1298 * @since 2.0
1299 */
1300 public Color getGridLineColor() {
1301 return fGridLineColor;
1302 }
1303
1304 /**
1305 * Set the markers list.
1306 *
1307 * @param markers
1308 * The markers list, or null
1309 * @since 2.0
1310 */
1311 public void setMarkers(List<IMarkerEvent> markers) {
1312 fMarkers = markers;
1313 }
1314
1315 /**
1316 * Get the markers list.
1317 *
1318 * @return The markers list, or null
1319 * @since 2.0
1320 */
1321 public List<IMarkerEvent> getMarkers() {
1322 return fMarkers;
1323 }
1324
1325 /**
1326 * Set the markers visibility. The default is true.
1327 *
1328 * @param visible
1329 * true to show the markers, false otherwise
1330 * @since 2.0
1331 */
1332 public void setMarkersVisible(boolean visible) {
1333 fMarkersVisible = visible;
1334 }
1335
1336 /**
1337 * Get the markers visibility.
1338 *
1339 * @return true if the markers are visible, false otherwise
1340 * @since 2.0
1341 */
1342 public boolean getMarkersVisible() {
1343 return fMarkersVisible;
1344 }
1345
1346 /**
1347 * Hide arrows
1348 *
1349 * @param hideArrows true to hide arrows
1350 */
1351 public void hideArrows(boolean hideArrows) {
1352 fHideArrows = hideArrows;
1353 }
1354
1355 /**
1356 * Follow the arrow forward
1357 *
1358 * @param extend
1359 * true to extend selection range, false for single selection
1360 * @since 1.0
1361 */
1362 public void followArrowFwd(boolean extend) {
1363 ITimeGraphEntry trace = getSelectedTrace();
1364 if (trace == null) {
1365 return;
1366 }
1367 long selectedTime = fTimeProvider.getSelectionEnd();
1368 for (ILinkEvent link : fItemData.fLinks) {
1369 if (link.getEntry() == trace && link.getTime() == selectedTime) {
1370 selectItem(link.getDestinationEntry(), false);
1371 if (link.getDuration() != 0) {
1372 if (extend) {
1373 fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime() + link.getDuration(), true);
1374 } else {
1375 fTimeProvider.setSelectedTimeNotify(link.getTime() + link.getDuration(), true);
1376 }
1377 // Notify if visible time window has been adjusted
1378 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1());
1379 }
1380 fireSelectionChanged();
1381 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
1382 return;
1383 }
1384 }
1385 selectNextEvent(extend);
1386 }
1387
1388 /**
1389 * Follow the arrow backward
1390 *
1391 * @param extend
1392 * true to extend selection range, false for single selection
1393 * @since 1.0
1394 */
1395 public void followArrowBwd(boolean extend) {
1396 ITimeGraphEntry trace = getSelectedTrace();
1397 if (trace == null) {
1398 return;
1399 }
1400 long selectedTime = fTimeProvider.getSelectionEnd();
1401 for (ILinkEvent link : fItemData.fLinks) {
1402 if (link.getDestinationEntry() == trace && link.getTime() + link.getDuration() == selectedTime) {
1403 selectItem(link.getEntry(), false);
1404 if (link.getDuration() != 0) {
1405 if (extend) {
1406 fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime(), true);
1407 } else {
1408 fTimeProvider.setSelectedTimeNotify(link.getTime(), true);
1409 }
1410 // Notify if visible time window has been adjusted
1411 fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1());
1412 }
1413 fireSelectionChanged();
1414 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
1415 return;
1416 }
1417 }
1418 selectPrevEvent(extend);
1419 }
1420
1421 /**
1422 * Return the currently selected trace
1423 *
1424 * @return The entry matching the trace
1425 */
1426 public ITimeGraphEntry getSelectedTrace() {
1427 ITimeGraphEntry trace = null;
1428 int idx = getSelectedIndex();
1429 if (idx >= 0) {
1430 trace = fItemData.fExpandedItems[idx].fEntry;
1431 }
1432 return trace;
1433 }
1434
1435 /**
1436 * Retrieve the index of the currently selected item
1437 *
1438 * @return The index
1439 */
1440 public int getSelectedIndex() {
1441 int idx = -1;
1442 for (int i = 0; i < fItemData.fExpandedItems.length; i++) {
1443 Item item = fItemData.fExpandedItems[i];
1444 if (item.fSelected) {
1445 idx = i;
1446 break;
1447 }
1448 }
1449 return idx;
1450 }
1451
1452 boolean toggle(int idx) {
1453 boolean toggled = false;
1454 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
1455 Item item = fItemData.fExpandedItems[idx];
1456 if (item.fHasChildren) {
1457 item.fExpanded = !item.fExpanded;
1458 fItemData.updateExpandedItems();
1459 redraw();
1460 toggled = true;
1461 fireTreeEvent(item.fEntry, item.fExpanded);
1462 }
1463 }
1464 return toggled;
1465 }
1466
1467 /**
1468 * Gets the index of the item at the given location.
1469 *
1470 * @param y
1471 * the y coordinate
1472 * @return the index of the item at the given location, of -1 if none.
1473 */
1474 protected int getItemIndexAtY(int y) {
1475 int ySum = 0;
1476 if (y < 0) {
1477 for (int idx = fTopIndex - 1; idx >= 0; idx--) {
1478 ySum -= fItemData.fExpandedItems[idx].fItemHeight;
1479 if (y >= ySum) {
1480 return idx;
1481 }
1482 }
1483 } else {
1484 for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) {
1485 ySum += fItemData.fExpandedItems[idx].fItemHeight;
1486 if (y < ySum) {
1487 return idx;
1488 }
1489 }
1490 }
1491 return -1;
1492 }
1493
1494 boolean isOverSplitLine(int x) {
1495 if (x < 0 || null == fTimeProvider) {
1496 return false;
1497 }
1498 int nameWidth = fTimeProvider.getNameSpace();
1499 return Math.abs(x - nameWidth) <= SNAP_WIDTH;
1500 }
1501
1502 boolean isOverTimeSpace(int x, int y) {
1503 Point size = getSize();
1504 return x >= fTimeProvider.getNameSpace() && x < size.x && y >= 0 && y < size.y;
1505 }
1506 /**
1507 * Gets the {@link ITimeGraphEntry} at the given location.
1508 *
1509 * @param pt
1510 * a point in the widget
1511 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1512 * if none.
1513 * @since 2.0
1514 */
1515 public ITimeGraphEntry getEntry(Point pt) {
1516 int idx = getItemIndexAtY(pt.y);
1517 return idx >= 0 ? fItemData.fExpandedItems[idx].fEntry : null;
1518 }
1519
1520 /**
1521 * Return the arrow event closest to the given point that is no further than
1522 * a maximum distance.
1523 *
1524 * @param pt
1525 * a point in the widget
1526 * @return The closest arrow event, or null if there is none close enough.
1527 */
1528 protected ILinkEvent getArrow(Point pt) {
1529 if (fHideArrows) {
1530 return null;
1531 }
1532 ILinkEvent linkEvent = null;
1533 double minDistance = Double.MAX_VALUE;
1534 for (ILinkEvent event : fItemData.fLinks) {
1535 Rectangle rect = getArrowRectangle(new Rectangle(0, 0, 0, 0), event);
1536 if (rect != null) {
1537 int x1 = rect.x;
1538 int y1 = rect.y;
1539 int x2 = x1 + rect.width;
1540 int y2 = y1 + rect.height;
1541 double d = Utils.distance(pt.x, pt.y, x1, y1, x2, y2);
1542 if (minDistance > d) {
1543 minDistance = d;
1544 linkEvent = event;
1545 }
1546 }
1547 }
1548 if (minDistance <= ARROW_HOVER_MAX_DIST) {
1549 return linkEvent;
1550 }
1551 return null;
1552 }
1553
1554 @Override
1555 public int getXForTime(long time) {
1556 if (null == fTimeProvider) {
1557 return -1;
1558 }
1559 long time0 = fTimeProvider.getTime0();
1560 long time1 = fTimeProvider.getTime1();
1561 int width = getSize().x;
1562 int nameSpace = fTimeProvider.getNameSpace();
1563 double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
1564 int x = SaturatedArithmetic.add(getBounds().x + nameSpace, (int) ((time - time0) * pixelsPerNanoSec));
1565 return x;
1566 }
1567
1568 @Override
1569 public long getTimeAtX(int coord) {
1570 if (null == fTimeProvider) {
1571 return -1;
1572 }
1573 long hitTime = -1;
1574 Point size = getSize();
1575 long time0 = fTimeProvider.getTime0();
1576 long time1 = fTimeProvider.getTime1();
1577 int nameWidth = fTimeProvider.getNameSpace();
1578 final int x = coord - nameWidth;
1579 int timeWidth = size.x - nameWidth - RIGHT_MARGIN;
1580 if (x >= 0 && size.x >= nameWidth) {
1581 if (time1 - time0 > timeWidth) {
1582 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1583 hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth));
1584 } else {
1585 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1586 hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth));
1587 }
1588 }
1589 return hitTime;
1590 }
1591
1592 void selectItem(int idx, boolean addSelection) {
1593 selectItem(idx, addSelection, true);
1594 }
1595
1596 void selectItem(int idx, boolean addSelection, boolean reveal) {
1597 boolean changed = false;
1598 if (addSelection) {
1599 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
1600 Item item = fItemData.fExpandedItems[idx];
1601 changed = !item.fSelected;
1602 item.fSelected = true;
1603 }
1604 } else {
1605 for (int i = 0; i < fItemData.fExpandedItems.length; i++) {
1606 Item item = fItemData.fExpandedItems[i];
1607 if ((i == idx && !item.fSelected) || (idx == -1 && item.fSelected)) {
1608 changed = true;
1609 }
1610 item.fSelected = i == idx;
1611 }
1612 }
1613 if (reveal) {
1614 changed |= ensureVisibleItem(idx, true);
1615 }
1616 if (changed) {
1617 redraw();
1618 }
1619 }
1620
1621 /**
1622 * Select an entry and make it visible
1623 *
1624 * @param entry
1625 * The entry to select
1626 * @param addSelection
1627 * <code>true</code> to add the entry to the current selection,
1628 * or <code>false</code> to set a new selection
1629 */
1630 public void selectItem(ITimeGraphEntry entry, boolean addSelection) {
1631 int idx = fItemData.findItemIndex(entry);
1632 selectItem(idx, addSelection);
1633 }
1634
1635 /**
1636 * Select an entry
1637 *
1638 * @param entry
1639 * The entry to select
1640 * @param addSelection
1641 * <code>true</code> to add the entry to the current selection,
1642 * or <code>false</code> to set a new selection
1643 * @param reveal
1644 * <code>true</code> if the selection is to be made visible, and
1645 * <code>false</code> otherwise
1646 * @since 2.3
1647 */
1648 public void selectItem(ITimeGraphEntry entry, boolean addSelection, boolean reveal) {
1649 int idx = fItemData.findItemIndex(entry);
1650 selectItem(idx, addSelection, reveal);
1651 }
1652
1653 /**
1654 * Retrieve the number of entries shown per page.
1655 *
1656 * @return The count
1657 */
1658 public int countPerPage() {
1659 int height = getSize().y;
1660 int count = 0;
1661 int ySum = 0;
1662 for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) {
1663 ySum += fItemData.fExpandedItems[idx].fItemHeight;
1664 if (ySum >= height) {
1665 return count;
1666 }
1667 count++;
1668 }
1669 for (int idx = fTopIndex - 1; idx >= 0; idx--) {
1670 ySum += fItemData.fExpandedItems[idx].fItemHeight;
1671 if (ySum >= height) {
1672 return count;
1673 }
1674 count++;
1675 }
1676 return count;
1677 }
1678
1679 /**
1680 * Get the index of the top element
1681 *
1682 * @return The index
1683 */
1684 public int getTopIndex() {
1685 return fTopIndex;
1686 }
1687
1688 /**
1689 * Get the number of expanded (visible) items
1690 *
1691 * @return The count of expanded (visible) items
1692 */
1693 public int getExpandedElementCount() {
1694 return fItemData.fExpandedItems.length;
1695 }
1696
1697 /**
1698 * Get an array of all expanded (visible) elements
1699 *
1700 * @return The expanded (visible) elements
1701 */
1702 public ITimeGraphEntry[] getExpandedElements() {
1703 ArrayList<ITimeGraphEntry> elements = new ArrayList<>();
1704 for (Item item : fItemData.fExpandedItems) {
1705 elements.add(item.fEntry);
1706 }
1707 return elements.toArray(new ITimeGraphEntry[0]);
1708 }
1709
1710 /**
1711 * Get the expanded (visible) element at the specified index.
1712 *
1713 * @param index
1714 * the element index
1715 * @return The expanded (visible) element or null if out of range
1716 * @since 2.0
1717 */
1718 public ITimeGraphEntry getExpandedElement(int index) {
1719 if (index < 0 || index >= fItemData.fExpandedItems.length) {
1720 return null;
1721 }
1722 return fItemData.fExpandedItems[index].fEntry;
1723 }
1724
1725 /**
1726 * Get the bounds of the specified entry
1727 *
1728 * @param entry the time graph entry
1729 * @return the bounds of the entry, or null if the entry is not visible
1730 * @since 2.3
1731 */
1732 public Rectangle getItemBounds(ITimeGraphEntry entry) {
1733 int idx = fItemData.findItemIndex(entry);
1734 if (idx >= 0) {
1735 return getItemRect(getBounds(), idx);
1736 }
1737 return null;
1738 }
1739
1740 Rectangle getNameRect(Rectangle bounds, int idx, int nameWidth) {
1741 Rectangle rect = getItemRect(bounds, idx);
1742 rect.width = nameWidth;
1743 return rect;
1744 }
1745
1746 Rectangle getStatesRect(Rectangle bounds, int idx, int nameWidth) {
1747 Rectangle rect = getItemRect(bounds, idx);
1748 rect.x += nameWidth;
1749 rect.width -= nameWidth;
1750 return rect;
1751 }
1752
1753 Rectangle getItemRect(Rectangle bounds, int idx) {
1754 int ySum = 0;
1755 if (idx >= fTopIndex) {
1756 for (int i = fTopIndex; i < idx; i++) {
1757 ySum += fItemData.fExpandedItems[i].fItemHeight;
1758 }
1759 } else {
1760 for (int i = fTopIndex - 1; i >= idx; i--) {
1761 ySum -= fItemData.fExpandedItems[i].fItemHeight;
1762 }
1763 }
1764 int y = bounds.y + ySum;
1765 int height = fItemData.fExpandedItems[idx].fItemHeight;
1766 return new Rectangle(bounds.x, y, bounds.width, height);
1767 }
1768
1769 @Override
1770 void paint(Rectangle bounds, PaintEvent e) {
1771 GC gc = e.gc;
1772
1773 if (bounds.width < 2 || bounds.height < 2 || null == fTimeProvider) {
1774 return;
1775 }
1776
1777 fIdealNameSpace = 0;
1778 int nameSpace = fTimeProvider.getNameSpace();
1779
1780 // draw the background layer
1781 drawBackground(bounds, nameSpace, gc);
1782
1783 // draw the grid lines
1784 drawGridLines(bounds, gc);
1785
1786 // draw the background markers
1787 drawMarkers(bounds, fTimeProvider, fMarkers, false, nameSpace, gc);
1788
1789 // draw the items
1790 drawItems(bounds, fTimeProvider, fItemData.fExpandedItems, fTopIndex, nameSpace, gc);
1791
1792 // draw the foreground markers
1793 drawMarkers(bounds, fTimeProvider, fMarkers, true, nameSpace, gc);
1794
1795 // draw the links (arrows)
1796 drawLinks(bounds, fTimeProvider, fItemData.fLinks, nameSpace, gc);
1797
1798 fTimeGraphProvider.postDrawControl(bounds, gc);
1799
1800 int alpha = gc.getAlpha();
1801 gc.setAlpha(100);
1802
1803 long time0 = fTimeProvider.getTime0();
1804 long time1 = fTimeProvider.getTime1();
1805 long selectionBegin = fTimeProvider.getSelectionBegin();
1806 long selectionEnd = fTimeProvider.getSelectionEnd();
1807 double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
1808 int x0 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionBegin - time0) * pixelsPerNanoSec));
1809 int x1 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionEnd - time0) * pixelsPerNanoSec));
1810
1811 // draw selection lines
1812 if (fDragState != DRAG_SELECTION) {
1813 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME));
1814 if (x0 >= nameSpace && x0 < bounds.x + bounds.width) {
1815 gc.drawLine(x0, bounds.y, x0, bounds.y + bounds.height);
1816 }
1817 if (x1 != x0) {
1818 if (x1 >= nameSpace && x1 < bounds.x + bounds.width) {
1819 gc.drawLine(x1, bounds.y, x1, bounds.y + bounds.height);
1820 }
1821 }
1822 }
1823
1824 // draw selection background
1825 if (selectionBegin != 0 && selectionEnd != 0 && fDragState != DRAG_SELECTION) {
1826 x0 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x0));
1827 x1 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x1));
1828 gc.setBackground(getColorScheme().getBkColor(false, false, true));
1829 if (x1 - x0 > 1) {
1830 gc.fillRectangle(new Rectangle(x0 + 1, bounds.y, x1 - x0 - 1, bounds.height));
1831 } else if (x0 - x1 > 1) {
1832 gc.fillRectangle(new Rectangle(x1 + 1, bounds.y, x0 - x1 - 1, bounds.height));
1833 }
1834 }
1835
1836 // draw drag selection background
1837 if (fDragState == DRAG_ZOOM || fDragState == DRAG_SELECTION) {
1838 gc.setBackground(getColorScheme().getBkColor(false, false, true));
1839 if (fDragX0 < fDragX) {
1840 gc.fillRectangle(new Rectangle(fDragX0, bounds.y, fDragX - fDragX0, bounds.height));
1841 } else if (fDragX0 > fDragX) {
1842 gc.fillRectangle(new Rectangle(fDragX, bounds.y, fDragX0 - fDragX, bounds.height));
1843 }
1844 }
1845
1846 // draw split line
1847 if (DRAG_SPLIT_LINE == fDragState ||
1848 (DRAG_NONE == fDragState && fMouseOverSplitLine && fTimeProvider.getNameSpace() > 0)) {
1849 gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.DARK_GRAY));
1850 } else {
1851 gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.GRAY));
1852 }
1853 gc.fillRectangle(bounds.x + nameSpace - SNAP_WIDTH, bounds.y, SNAP_WIDTH, bounds.height);
1854
1855 if (DRAG_ZOOM == fDragState && Math.max(fDragX, fDragX0) > nameSpace) {
1856 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND));
1857 gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1);
1858 if (fDragX != fDragX0) {
1859 gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1);
1860 }
1861 } else if (DRAG_SELECTION == fDragState && Math.max(fDragX, fDragX0) > nameSpace) {
1862 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME));
1863 gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1);
1864 if (fDragX != fDragX0) {
1865 gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1);
1866 }
1867 }
1868
1869 gc.setAlpha(alpha);
1870 }
1871
1872 /**
1873 * Draw the background layer. Fills the background of the control's name
1874 * space and states space, updates the background of items if necessary,
1875 * and draws the item's name text and middle line.
1876 *
1877 * @param bounds
1878 * The bounds of the control
1879 * @param nameSpace
1880 * The name space width
1881 * @param gc
1882 * Graphics context
1883 * @since 2.0
1884 */
1885 protected void drawBackground(Rectangle bounds, int nameSpace, GC gc) {
1886 // draw empty name space background
1887 gc.setBackground(getColorScheme().getBkColor(false, false, true));
1888 drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height);
1889
1890 // draw empty states space background
1891 gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.BACKGROUND));
1892 drawBackground(gc, bounds.x + nameSpace, bounds.y, bounds.width - nameSpace, bounds.height);
1893
1894 for (int i = fTopIndex; i < fItemData.fExpandedItems.length; i++) {
1895 Rectangle itemRect = getItemRect(bounds, i);
1896 if (itemRect.y >= bounds.y + bounds.height) {
1897 break;
1898 }
1899 Item item = fItemData.fExpandedItems[i];
1900 // draw the background of selected item and items with no time events
1901 if (! item.fEntry.hasTimeEvents()) {
1902 gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus));
1903 gc.fillRectangle(itemRect);
1904 } else if (item.fSelected) {
1905 gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, true));
1906 gc.fillRectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height);
1907 gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, false));
1908 gc.fillRectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height);
1909 }
1910 // draw the name space
1911 Rectangle nameRect = new Rectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height);
1912 drawName(item, nameRect, gc);
1913 if (item.fEntry.hasTimeEvents()) {
1914 Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height);
1915 drawMidLine(rect, gc);
1916 }
1917 }
1918 }
1919
1920 /**
1921 * Draw the grid lines
1922 *
1923 * @param bounds
1924 * The bounds of the control
1925 * @param gc
1926 * Graphics context
1927 * @since 2.0
1928 */
1929 public void drawGridLines(Rectangle bounds, GC gc) {
1930 if (!fGridLinesVisible) {
1931 return;
1932 }
1933 gc.setForeground(fGridLineColor);
1934 gc.setAlpha(fGridLineColor.getAlpha());
1935 for (int x : fTimeGraphScale.getTickList()) {
1936 gc.drawLine(x, bounds.y, x, bounds.y + bounds.height);
1937 }
1938 gc.setAlpha(255);
1939 }
1940
1941 /**
1942 * Draw the markers
1943 *
1944 * @param bounds
1945 * The rectangle of the area
1946 * @param timeProvider
1947 * The time provider
1948 * @param markers
1949 * The list of markers
1950 * @param foreground
1951 * true to draw the foreground markers, false otherwise
1952 * @param nameSpace
1953 * The width reserved for the names
1954 * @param gc
1955 * Reference to the SWT GC object
1956 * @since 2.0
1957 */
1958 protected void drawMarkers(Rectangle bounds, ITimeDataProvider timeProvider, List<IMarkerEvent> markers, boolean foreground, int nameSpace, GC gc) {
1959 if (!fMarkersVisible || markers == null || markers.isEmpty()) {
1960 return;
1961 }
1962 gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height));
1963 /* the list can grow concurrently but cannot shrink */
1964 for (int i = 0; i < markers.size(); i++) {
1965 IMarkerEvent marker = markers.get(i);
1966 if (marker.isForeground() == foreground) {
1967 drawMarker(marker, bounds, timeProvider, nameSpace, gc);
1968 }
1969 }
1970 gc.setClipping((Rectangle) null);
1971 }
1972
1973 /**
1974 * Draw a single marker
1975 *
1976 * @param marker
1977 * The marker event
1978 * @param bounds
1979 * The bounds of the control
1980 * @param timeProvider
1981 * The time provider
1982 * @param nameSpace
1983 * The width reserved for the name
1984 * @param gc
1985 * Reference to the SWT GC object
1986 * @since 2.0
1987 */
1988 protected void drawMarker(IMarkerEvent marker, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) {
1989 Rectangle rect = Utils.clone(bounds);
1990 if (marker.getEntry() != null) {
1991 int index = fItemData.findItemIndex(marker.getEntry());
1992 if (index == -1) {
1993 return;
1994 }
1995 rect = getStatesRect(bounds, index, nameSpace);
1996 if (rect.y < 0 || rect.y > bounds.height) {
1997 return;
1998 }
1999 }
2000 int x0 = getXForTime(marker.getTime());
2001 int x1 = getXForTime(marker.getTime() + marker.getDuration());
2002 if (x0 > bounds.width || x1 < nameSpace) {
2003 return;
2004 }
2005 rect.x = Math.max(nameSpace, Math.min(bounds.width, x0));
2006 rect.width = Math.max(1, Math.min(bounds.width, x1) - rect.x);
2007
2008 Color color = getColorScheme().getColor(marker.getColor());
2009 gc.setBackground(color);
2010 gc.setAlpha(color.getAlpha());
2011 gc.fillRectangle(rect);
2012 gc.setAlpha(255);
2013 String label = marker.getLabel();
2014 if (label != null && marker.getEntry() != null) {
2015 label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH));
2016 gc.setForeground(color);
2017 Utils.drawText(gc, label, rect.x - gc.textExtent(label).x, rect.y, true);
2018 }
2019 }
2020
2021 /**
2022 * Draw many items at once
2023 *
2024 * @param bounds
2025 * The bounds of the control
2026 * @param timeProvider
2027 * The time provider
2028 * @param items
2029 * The array items to draw
2030 * @param topIndex
2031 * The index of the first element to draw
2032 * @param nameSpace
2033 * The name space width
2034 * @param gc
2035 * Graphics context
2036 */
2037 public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider,
2038 Item[] items, int topIndex, int nameSpace, GC gc) {
2039 for (int i = topIndex; i < items.length; i++) {
2040 Item item = items[i];
2041 drawItem(item, bounds, timeProvider, i, nameSpace, gc);
2042 }
2043 }
2044
2045 /**
2046 * Draws the item
2047 *
2048 * @param item
2049 * The item to draw
2050 * @param bounds
2051 * The bounds of the control
2052 * @param timeProvider
2053 * The time provider
2054 * @param i
2055 * The expanded item index
2056 * @param nameSpace
2057 * The name space width
2058 * @param gc
2059 * Graphics context
2060 */
2061 protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) {
2062 Rectangle itemRect = getItemRect(bounds, i);
2063 if (itemRect.y >= bounds.y + bounds.height) {
2064 return;
2065 }
2066
2067 ITimeGraphEntry entry = item.fEntry;
2068 long time0 = timeProvider.getTime0();
2069 long time1 = timeProvider.getTime1();
2070 long selectedTime = fTimeProvider.getSelectionEnd();
2071
2072 Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height);
2073 if (rect.isEmpty() || (time1 <= time0)) {
2074 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
2075 return;
2076 }
2077
2078 boolean selected = item.fSelected;
2079 // K pixels per second
2080 double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0);
2081
2082 if (item.fEntry.hasTimeEvents()) {
2083 gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height));
2084 fillSpace(rect, gc, selected);
2085
2086 int margins = getMarginForHeight(rect.height);
2087 int height = rect.height - margins;
2088 int topMargin = (margins + 1) / 2;
2089 Rectangle stateRect = new Rectangle(rect.x, rect.y + topMargin, rect.width, height);
2090
2091 /* Set the font for this item */
2092 setFontForHeight(height, gc);
2093
2094 long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace();
2095 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration);
2096
2097 int lastX = -1;
2098 while (iterator.hasNext()) {
2099 ITimeEvent event = iterator.next();
2100 int x = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() - time0) * pixelsPerNanoSec));
2101 int xEnd = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec));
2102 if (x >= rect.x + rect.width || xEnd < rect.x) {
2103 // event is out of bounds
2104 continue;
2105 }
2106 xEnd = Math.min(rect.x + rect.width, xEnd);
2107 stateRect.x = Math.max(rect.x, x);
2108 stateRect.width = Math.max(0, xEnd - stateRect.x + 1);
2109 if (stateRect.x == lastX) {
2110 stateRect.width -= 1;
2111 if (stateRect.width > 0) {
2112 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
2113 gc.drawPoint(stateRect.x, stateRect.y - 2);
2114 stateRect.x += 1;
2115 }
2116 }
2117 boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration();
2118 if (drawState(getColorScheme(), event, stateRect, gc, selected, timeSelected)) {
2119 lastX = stateRect.x;
2120 }
2121 }
2122 gc.setClipping((Rectangle) null);
2123 }
2124 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
2125 }
2126
2127 /**
2128 * Draw the links
2129 *
2130 * @param bounds
2131 * The bounds of the control
2132 * @param timeProvider
2133 * The time provider
2134 * @param links
2135 * The list of link events
2136 * @param nameSpace
2137 * The name space width
2138 * @param gc
2139 * Graphics context
2140 */
2141 public void drawLinks(Rectangle bounds, ITimeDataProvider timeProvider,
2142 List<ILinkEvent> links, int nameSpace, GC gc) {
2143 if (fHideArrows) {
2144 return;
2145 }
2146 gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height));
2147 /* the list can grow concurrently but cannot shrink */
2148 for (int i = 0; i < links.size(); i++) {
2149 drawLink(links.get(i), bounds, timeProvider, nameSpace, gc);
2150 }
2151 gc.setClipping((Rectangle) null);
2152 }
2153
2154 /**
2155 * Draws a link type event
2156 *
2157 * @param event
2158 * The link event to draw
2159 * @param bounds
2160 * The bounds of the control
2161 * @param timeProvider
2162 * The time provider
2163 * @param nameSpace
2164 * The name space width
2165 * @param gc
2166 * Graphics context
2167 */
2168 protected void drawLink(ILinkEvent event, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) {
2169 drawArrow(getColorScheme(), event, getArrowRectangle(bounds, event), gc);
2170 }
2171
2172 private Rectangle getArrowRectangle(Rectangle bounds, ILinkEvent event) {
2173 int srcIndex = fItemData.findItemIndex(event.getEntry());
2174 int destIndex = fItemData.findItemIndex(event.getDestinationEntry());
2175
2176 if ((srcIndex == -1) || (destIndex == -1)) {
2177 return null;
2178 }
2179
2180 Rectangle src = getStatesRect(bounds, srcIndex, fTimeProvider.getNameSpace());
2181 Rectangle dst = getStatesRect(bounds, destIndex, fTimeProvider.getNameSpace());
2182
2183 int x0 = getXForTime(event.getTime());
2184 int x1 = getXForTime(event.getTime() + event.getDuration());
2185
2186 // limit the x-coordinates to prevent integer overflow in calculations
2187 // and also GC.drawLine doesn't draw properly with large coordinates
2188 final int limit = Integer.MAX_VALUE / 1024;
2189 x0 = Math.max(-limit, Math.min(x0, limit));
2190 x1 = Math.max(-limit, Math.min(x1, limit));
2191
2192 int y0 = src.y + src.height / 2;
2193 int y1 = dst.y + dst.height / 2;
2194 return new Rectangle(x0, y0, x1 - x0, y1 - y0);
2195 }
2196
2197 /**
2198 * Draw an arrow
2199 *
2200 * @param colors
2201 * Color scheme
2202 * @param event
2203 * Time event for which we're drawing the arrow
2204 * @param rect
2205 * The arrow rectangle
2206 * @param gc
2207 * Graphics context
2208 * @return true if the arrow was drawn
2209 */
2210 protected boolean drawArrow(TimeGraphColorScheme colors, ITimeEvent event,
2211 Rectangle rect, GC gc) {
2212
2213 if (rect == null) {
2214 return false;
2215 }
2216 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
2217 if (colorIdx < 0) {
2218 return false;
2219 }
2220 boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true;
2221
2222 if (visible) {
2223 Color stateColor = null;
2224 if (colorIdx < fEventColorMap.length) {
2225 stateColor = fEventColorMap[colorIdx];
2226 } else {
2227 stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
2228 }
2229
2230 gc.setForeground(stateColor);
2231 gc.setBackground(stateColor);
2232
2233 /* Draw the arrow */
2234 gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
2235 drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc);
2236
2237 }
2238 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2239 return visible;
2240 }
2241
2242 /*
2243 * @author Francis Giraldeau
2244 *
2245 * Inspiration:
2246 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2247 *
2248 * The algorithm was taken from this site, not the code itself
2249 */
2250 private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc)
2251 {
2252 int factor = 10;
2253 double cos = 0.9510;
2254 double sin = 0.3090;
2255 long lenx = x1 - x0;
2256 long leny = y1 - y0;
2257 double len = Math.sqrt(lenx * lenx + leny * leny);
2258
2259 double dx = factor * lenx / len;
2260 double dy = factor * leny / len;
2261 int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin)));
2262 int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos)));
2263 int end2X = (int) Math.round((x1 - (dx * cos + dy * sin)));
2264 int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos)));
2265 int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 };
2266 gc.fillPolygon(arrow);
2267 }
2268
2269 /**
2270 * Draw the name space of an item.
2271 *
2272 * @param item
2273 * Item object
2274 * @param bounds
2275 * The bounds of the item's name space
2276 * @param gc
2277 * Graphics context
2278 */
2279 protected void drawName(Item item, Rectangle bounds, GC gc) {
2280 // No name space to be drawn
2281 if (fTimeProvider.getNameSpace() == 0) {
2282 return;
2283 }
2284
2285 boolean hasTimeEvents = item.fEntry.hasTimeEvents();
2286 if (hasTimeEvents) {
2287 gc.setClipping(bounds);
2288 }
2289
2290 int height = bounds.height - getMarginForHeight(bounds.height);
2291 setFontForHeight(height, gc);
2292
2293 String name = fLabelProvider == null ? item.fName : fLabelProvider.getColumnText(item.fEntry, 0);
2294 Rectangle rect = Utils.clone(bounds);
2295 rect.y += (bounds.height - gc.stringExtent(name).y) / 2;
2296 TreeColumn[] columns = fTree.getColumns();
2297 int idealNameSpace = 0;
2298 for (int i = 0; i < columns.length; i++) {
2299 int columnIndex = fTree.getColumnOrder()[i];
2300 TreeColumn column = columns[columnIndex];
2301 rect.width = column.getWidth();
2302 gc.setClipping(rect.x, bounds.y, Math.min(rect.width, bounds.x + bounds.width - rect.x - SNAP_WIDTH), bounds.height);
2303 int width = MARGIN;
2304 if (i == 0) {
2305 // first visible column
2306 width += item.fLevel * EXPAND_SIZE;
2307 if (item.fHasChildren) {
2308 // draw expand/collapse arrow
2309 gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.DARK_GRAY));
2310 int arrowHeightHint = (height < 4) ? height : (height < 6) ? height - 1 : height - 2;
2311 int arrowHalfHeight = Math.max(1, Math.min(arrowHeightHint, (int) Math.round((EXPAND_SIZE - 2) / ARROW_RATIO))) / 2;
2312 int arrowHalfWidth = (Math.max(1, Math.min(EXPAND_SIZE - 2, (int) Math.round(arrowHeightHint * ARROW_RATIO))) + 1) / 2;
2313 int x1 = bounds.x + width + 1;
2314 int x2 = x1 + 2 * arrowHalfWidth;
2315 int midy = bounds.y + bounds.height / 2;
2316 int y1 = midy - arrowHalfHeight;
2317 int y2 = midy + arrowHalfHeight;
2318 if (!item.fExpanded) { // >
2319 gc.fillPolygon(new int[] { x1, y1, x2, midy, x1, y2 });
2320 } else { // v
2321 int midx = x1 + arrowHalfWidth;
2322 gc.fillPolygon(new int[] { x1, y1, x2, y1, midx, y2 });
2323 }
2324 }
2325 width += EXPAND_SIZE + MARGIN;
2326
2327 Image img = fLabelProvider != null ? fLabelProvider.getColumnImage(item.fEntry, columnIndex)
2328 : columnIndex == 0 ? fTimeGraphProvider.getItemImage(item.fEntry) : null;
2329 if (img != null) {
2330 // draw icon
2331 int imgHeight = img.getImageData().height;
2332 int imgWidth = img.getImageData().width;
2333 int x = width;
2334 int y = bounds.y + (bounds.height - imgHeight) / 2;
2335 gc.drawImage(img, x, y);
2336 width += imgWidth + MARGIN;
2337 }
2338 } else {
2339 if (fLabelProvider == null) {
2340 break;
2341 }
2342 }
2343 String label = fLabelProvider != null ? fLabelProvider.getColumnText(item.fEntry, columnIndex)
2344 : columnIndex == 0 ? item.fName : ""; //$NON-NLS-1$
2345 gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus));
2346 Rectangle textRect = new Rectangle(rect.x + width, rect.y, rect.width - width, rect.height);
2347 int textWidth = Utils.drawText(gc, label, textRect, true);
2348 width += textWidth + MARGIN;
2349 if (textWidth > 0) {
2350 idealNameSpace = rect.x + width;
2351 }
2352 if (columns.length == 1) {
2353 drawMidLine(new Rectangle(bounds.x + width, bounds.y, bounds.x + bounds.width, bounds.height), gc);
2354 }
2355 if (fAutoResizeColumns && width > column.getWidth()) {
2356 column.setData(PREFERRED_WIDTH, width);
2357 column.setWidth(width);
2358 }
2359 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
2360 if (i < columns.length - 1) {
2361 // not the last visible column: draw the vertical cell border
2362 int x = rect.x + rect.width - 1;
2363 gc.drawLine(x, bounds.y, x, bounds.y + bounds.height);
2364 }
2365 rect.x += rect.width;
2366 }
2367 fIdealNameSpace = Math.max(fIdealNameSpace, idealNameSpace);
2368
2369 gc.setClipping((Rectangle) null);
2370 }
2371
2372 /**
2373 * Draw the state (color fill)
2374 *
2375 * @param colors
2376 * Color scheme
2377 * @param event
2378 * Time event for which we're drawing the state
2379 * @param rect
2380 * The state rectangle
2381 * @param gc
2382 * Graphics context
2383 * @param selected
2384 * Is this time event currently selected (so it appears
2385 * highlighted)
2386 * @param timeSelected
2387 * Is the timestamp currently selected
2388 * @return true if the state was drawn
2389 */
2390 protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event,
2391 Rectangle rect, GC gc, boolean selected, boolean timeSelected) {
2392
2393 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
2394 if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) {
2395 return false;
2396 }
2397 boolean visible = rect.width == 0 ? false : true;
2398 rect.width = Math.max(1, rect.width);
2399 Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
2400 gc.setForeground(black);
2401
2402 if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) {
2403 if (visible) {
2404 // Only draw the top and bottom borders
2405 gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y);
2406 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
2407 if (rect.width == 1) {
2408 gc.drawPoint(rect.x, rect.y - 2);
2409 }
2410 }
2411 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2412 return false;
2413 }
2414 Color stateColor = null;
2415 if (colorIdx < fEventColorMap.length) {
2416 stateColor = fEventColorMap[colorIdx];
2417 } else {
2418 stateColor = black;
2419 }
2420
2421 boolean reallySelected = timeSelected && selected;
2422 // fill all rect area
2423 gc.setBackground(stateColor);
2424 if (visible) {
2425 gc.fillRectangle(rect);
2426 } else if (fBlendSubPixelEvents) {
2427 gc.setAlpha(128);
2428 gc.fillRectangle(rect);
2429 gc.setAlpha(255);
2430 }
2431
2432 if (reallySelected) {
2433 gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1);
2434 gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height);
2435 }
2436 if (!visible) {
2437 gc.drawPoint(rect.x, rect.y - 2);
2438 }
2439 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2440 return visible;
2441 }
2442
2443 /**
2444 * Fill an item's states rectangle
2445 *
2446 * @param rect
2447 * The states rectangle
2448 * @param gc
2449 * Graphics context
2450 * @param selected
2451 * true if the item is selected
2452 */
2453 protected void fillSpace(Rectangle rect, GC gc, boolean selected) {
2454 /* Nothing to draw */
2455 }
2456
2457 /**
2458 * Draw a line at the middle height of a rectangle
2459 *
2460 * @param rect
2461 * The rectangle
2462 * @param gc
2463 * Graphics context
2464 */
2465 private void drawMidLine(Rectangle rect, GC gc) {
2466 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
2467 int midy = rect.y + rect.height / 2;
2468 gc.drawLine(rect.x, midy, rect.x + rect.width, midy);
2469 }
2470
2471 private static int getMarginForHeight(int height) {
2472 /*
2473 * State rectangle is smaller than the item bounds when height is > 4.
2474 * Don't use any margin if the height is below or equal that threshold.
2475 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2476 * pixels for the state height, but with a minimum margin of 1.
2477 */
2478 final int MARGIN_THRESHOLD = 4;
2479 final int PREFERRED_HEIGHT = 13;
2480 final int MIN_MARGIN = 1;
2481 final int MAX_MARGIN = 6;
2482 return height <= MARGIN_THRESHOLD ? 0 :
2483 Math.max(Math.min(height - PREFERRED_HEIGHT, MAX_MARGIN), MIN_MARGIN);
2484 }
2485
2486 private void setFontForHeight(int pixels, GC gc) {
2487 /* convert font height from pixels to points */
2488 int height = Math.max(pixels * PPI / DPI, 1);
2489 Font font = fFonts.get(height);
2490 if (font == null) {
2491 FontData fontData = gc.getFont().getFontData()[0];
2492 fontData.setHeight(height);
2493 font = new Font(gc.getDevice(), fontData);
2494 fFonts.put(height, font);
2495 }
2496 gc.setFont(font);
2497 }
2498
2499 @Override
2500 public void keyTraversed(TraverseEvent e) {
2501 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
2502 e.doit = true;
2503 }
2504 }
2505
2506 @Override
2507 public void keyPressed(KeyEvent e) {
2508 int idx = -1;
2509 if (fItemData.fExpandedItems.length == 0) {
2510 return;
2511 }
2512 if (SWT.HOME == e.keyCode) {
2513 idx = 0;
2514 } else if (SWT.END == e.keyCode) {
2515 idx = fItemData.fExpandedItems.length - 1;
2516 } else if (SWT.ARROW_DOWN == e.keyCode) {
2517 idx = getSelectedIndex();
2518 if (idx < 0) {
2519 idx = 0;
2520 } else if (idx < fItemData.fExpandedItems.length - 1) {
2521 idx++;
2522 }
2523 } else if (SWT.ARROW_UP == e.keyCode) {
2524 idx = getSelectedIndex();
2525 if (idx < 0) {
2526 idx = 0;
2527 } else if (idx > 0) {
2528 idx--;
2529 }
2530 } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) {
2531 boolean extend = (e.stateMask & SWT.SHIFT) != 0;
2532 selectPrevEvent(extend);
2533 } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) {
2534 boolean extend = (e.stateMask & SWT.SHIFT) != 0;
2535 selectNextEvent(extend);
2536 } else if (SWT.PAGE_DOWN == e.keyCode) {
2537 int page = countPerPage();
2538 idx = getSelectedIndex();
2539 if (idx < 0) {
2540 idx = 0;
2541 }
2542 idx += page;
2543 if (idx >= fItemData.fExpandedItems.length) {
2544 idx = fItemData.fExpandedItems.length - 1;
2545 }
2546 } else if (SWT.PAGE_UP == e.keyCode) {
2547 int page = countPerPage();
2548 idx = getSelectedIndex();
2549 if (idx < 0) {
2550 idx = 0;
2551 }
2552 idx -= page;
2553 if (idx < 0) {
2554 idx = 0;
2555 }
2556 } else if (SWT.CR == e.keyCode) {
2557 idx = getSelectedIndex();
2558 if (idx >= 0) {
2559 if (fItemData.fExpandedItems[idx].fHasChildren) {
2560 toggle(idx);
2561 } else {
2562 fireDefaultSelection();
2563 }
2564 }
2565 idx = -1;
2566 } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) != 0)) {
2567 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2568 verticalZoom(true);
2569 if (fVerticalZoomAlignEntry != null) {
2570 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2571 }
2572 } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) != 0)) {
2573 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2574 verticalZoom(false);
2575 if (fVerticalZoomAlignEntry != null) {
2576 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2577 }
2578 } else if (e.character == '0' && ((e.stateMask & SWT.CTRL) != 0)) {
2579 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2580 resetVerticalZoom();
2581 if (fVerticalZoomAlignEntry != null) {
2582 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2583 }
2584 } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) {
2585 if (fHasNamespaceFocus) {
2586 ITimeGraphEntry entry = getSelectedTrace();
2587 setExpandedState(entry, 0, true);
2588 } else {
2589 zoomIn();
2590 }
2591 } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) {
2592 if (fHasNamespaceFocus) {
2593 ITimeGraphEntry entry = getSelectedTrace();
2594 if ((entry != null) && entry.hasChildren()) {
2595 setExpandedState(entry, -1, false);
2596 }
2597 } else {
2598 zoomOut();
2599 }
2600 } else if ((e.character == '*') && ((e.stateMask & SWT.CTRL) == 0)) {
2601 if (fHasNamespaceFocus) {
2602 ITimeGraphEntry entry = getSelectedTrace();
2603 if ((entry != null) && entry.hasChildren()) {
2604 setExpandedStateLevel(entry);
2605 }
2606 }
2607 }
2608 if (idx >= 0) {
2609 selectItem(idx, false);
2610 fireSelectionChanged();
2611 }
2612 int x = toControl(e.display.getCursorLocation()).x;
2613 updateCursor(x, e.stateMask | e.keyCode);
2614 }
2615
2616 @Override
2617 public void keyReleased(KeyEvent e) {
2618 int x = toControl(e.display.getCursorLocation()).x;
2619 updateCursor(x, e.stateMask & ~e.keyCode);
2620 }
2621
2622 @Override
2623 public void focusGained(FocusEvent e) {
2624 fIsInFocus = true;
2625 redraw();
2626 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2627 }
2628
2629 @Override
2630 public void focusLost(FocusEvent e) {
2631 fIsInFocus = false;
2632 if (DRAG_NONE != fDragState) {
2633 setCapture(false);
2634 fDragState = DRAG_NONE;
2635 }
2636 redraw();
2637 updateStatusLine(NO_STATUS);
2638 }
2639
2640 /**
2641 * @return If the current view is focused
2642 */
2643 public boolean isInFocus() {
2644 return fIsInFocus;
2645 }
2646
2647 /**
2648 * Provide the possibility to control the wait cursor externally e.g. data
2649 * requests in progress
2650 *
2651 * @param waitInd Should we wait indefinitely?
2652 */
2653 public void waitCursor(boolean waitInd) {
2654 // Update cursor as indicated
2655 if (waitInd) {
2656 setCursor(fWaitCursor);
2657 } else {
2658 setCursor(null);
2659 }
2660 }
2661
2662 private void updateCursor(int x, int stateMask) {
2663 // if Wait cursor not active, check for the need to change the cursor
2664 if (getCursor() == fWaitCursor) {
2665 return;
2666 }
2667 Cursor cursor = null;
2668 if (fDragState == DRAG_SPLIT_LINE) {
2669 } else if (fDragState == DRAG_SELECTION) {
2670 cursor = fResizeCursor;
2671 } else if (fDragState == DRAG_TRACE_ITEM) {
2672 cursor = fDragCursor;
2673 } else if (fDragState == DRAG_ZOOM) {
2674 cursor = fZoomCursor;
2675 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
2676 cursor = fDragCursor;
2677 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2678 cursor = fResizeCursor;
2679 } else if (!isOverSplitLine(x)) {
2680 long selectionBegin = fTimeProvider.getSelectionBegin();
2681 long selectionEnd = fTimeProvider.getSelectionEnd();
2682 int xBegin = getXForTime(selectionBegin);
2683 int xEnd = getXForTime(selectionEnd);
2684 if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) {
2685 cursor = fResizeCursor;
2686 }
2687 }
2688 if (getCursor() != cursor) {
2689 setCursor(cursor);
2690 }
2691 }
2692
2693 /**
2694 * Update the status line following a change of selection.
2695 *
2696 * @since 2.0
2697 */
2698 public void updateStatusLine() {
2699 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2700 }
2701
2702 private void updateStatusLine(int x) {
2703 // use the time provider of the time graph scale for the status line
2704 ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider();
2705 if (fStatusLineManager == null || null == tdp ||
2706 tdp.getTime0() == tdp.getTime1()) {
2707 return;
2708 }
2709 TimeFormat tf = tdp.getTimeFormat();
2710 Resolution res = Resolution.NANOSEC;
2711 StringBuilder message = new StringBuilder();
2712 if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) {
2713 if (x != STATUS_WITHOUT_CURSOR_TIME) {
2714 long time = getTimeAtX(x);
2715 if (time >= 0) {
2716 if (tdp instanceof ITimeDataProviderConverter) {
2717 time = ((ITimeDataProviderConverter) tdp).convertTime(time);
2718 }
2719 message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$
2720 new Object[] {
2721 tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$
2722 Utils.formatTime(time, tf, res)
2723 }));
2724 }
2725 }
2726 long selectionBegin = tdp.getSelectionBegin();
2727 long selectionEnd = tdp.getSelectionEnd();
2728 message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$
2729 new Object[] {
2730 tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$
2731 Utils.formatTime(selectionBegin, tf, res)
2732 }));
2733 if (selectionBegin != selectionEnd) {
2734 message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2735 new Object[] {
2736 tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$
2737 Utils.formatTime(selectionEnd, tf, res),
2738 Utils.formatDelta(selectionEnd - selectionBegin, tf, res)
2739 }));
2740 }
2741 } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) {
2742 long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0;
2743 long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX);
2744 if (tdp instanceof ITimeDataProviderConverter) {
2745 time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0);
2746 time = ((ITimeDataProviderConverter) tdp).convertTime(time);
2747 }
2748 message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2749 new Object[] {
2750 tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$
2751 Utils.formatTime(time0, tf, res),
2752 tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$
2753 Utils.formatTime(time, tf, res),
2754 Utils.formatDelta(time - time0, tf, res)
2755 }));
2756 }
2757 fStatusLineManager.setMessage(message.toString());
2758 }
2759
2760 @Override
2761 public void mouseMove(MouseEvent e) {
2762 if (null == fTimeProvider) {
2763 return;
2764 }
2765 Point size = getSize();
2766 if (DRAG_TRACE_ITEM == fDragState) {
2767 int nameWidth = fTimeProvider.getNameSpace();
2768 if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) {
2769 fDragX = e.x;
2770 double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak);
2771 long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec));
2772 long time1 = fTime1bak - timeDelta;
2773 long maxTime = fTimeProvider.getMaxTime();
2774 if (time1 > maxTime) {
2775 time1 = maxTime;
2776 }
2777 long time0 = time1 - (fTime1bak - fTime0bak);
2778 if (time0 < fTimeProvider.getMinTime()) {
2779 time0 = fTimeProvider.getMinTime();
2780 time1 = time0 + (fTime1bak - fTime0bak);
2781 }
2782 fTimeProvider.setStartFinishTimeNotify(time0, time1);
2783 }
2784 } else if (DRAG_SPLIT_LINE == fDragState) {
2785 fDragX = e.x;
2786 fTimeProvider.setNameSpace(e.x);
2787 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2788 } else if (DRAG_SELECTION == fDragState) {
2789 if (fDragBeginMarker) {
2790 fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2791 } else {
2792 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2793 }
2794 redraw();
2795 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2796 fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX));
2797 } else if (DRAG_ZOOM == fDragState) {
2798 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2799 redraw();
2800 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2801 } else if (DRAG_NONE == fDragState) {
2802 boolean mouseOverSplitLine = isOverSplitLine(e.x);
2803 if (fMouseOverSplitLine != mouseOverSplitLine) {
2804 redraw();
2805 }
2806 fMouseOverSplitLine = mouseOverSplitLine;
2807 }
2808
2809 if (e.x >= fTimeProvider.getNameSpace()) {
2810 fHasNamespaceFocus = false;
2811 } else {
2812 fHasNamespaceFocus = true;
2813 }
2814 updateCursor(e.x, e.stateMask);
2815 updateStatusLine(e.x);
2816 }
2817
2818 @Override
2819 public void mouseDoubleClick(MouseEvent e) {
2820 if (null == fTimeProvider) {
2821 return;
2822 }
2823 if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) {
2824 if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) {
2825 fTimeProvider.setNameSpace(fIdealNameSpace);
2826 boolean mouseOverSplitLine = isOverSplitLine(e.x);
2827 if (fMouseOverSplitLine != mouseOverSplitLine) {
2828 redraw();
2829 }
2830 fMouseOverSplitLine = mouseOverSplitLine;
2831 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2832 return;
2833 }
2834 int idx = getItemIndexAtY(e.y);
2835 if (idx >= 0) {
2836 selectItem(idx, false);
2837 fireDefaultSelection();
2838 }
2839 }
2840 }
2841
2842 @Override
2843 public void mouseDown(MouseEvent e) {
2844 if (fDragState != DRAG_NONE) {
2845 return;
2846 }
2847 if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) {
2848 int nameSpace = fTimeProvider.getNameSpace();
2849 if (nameSpace != 0 && isOverSplitLine(e.x)) {
2850 fDragState = DRAG_SPLIT_LINE;
2851 fDragButton = e.button;
2852 fDragX = e.x;
2853 fDragX0 = fDragX;
2854 redraw();
2855 updateCursor(e.x, e.stateMask);
2856 return;
2857 }
2858 }
2859 if (fTimeProvider == null ||
2860 fTimeProvider.getTime0() == fTimeProvider.getTime1() ||
2861 getSize().x - fTimeProvider.getNameSpace() <= 0) {
2862 return;
2863 }
2864 int idx;
2865 if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) {
2866 int nameSpace = fTimeProvider.getNameSpace();
2867 idx = getItemIndexAtY(e.y);
2868 if (idx >= 0) {
2869 Item item = fItemData.fExpandedItems[idx];
2870 if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) {
2871 toggle(idx);
2872 return;
2873 }
2874 selectItem(idx, false);
2875 fireSelectionChanged();
2876 } else {
2877 selectItem(idx, false); // clear selection
2878 fireSelectionChanged();
2879 }
2880 long hitTime = getTimeAtX(e.x);
2881 if (hitTime >= 0) {
2882 setCapture(true);
2883
2884 fDragState = DRAG_SELECTION;
2885 fDragBeginMarker = false;
2886 fDragButton = e.button;
2887 fDragX = e.x;
2888 fDragX0 = fDragX;
2889 fDragTime0 = getTimeAtX(fDragX0);
2890 long selectionBegin = fTimeProvider.getSelectionBegin();
2891 long selectionEnd = fTimeProvider.getSelectionEnd();
2892 int xBegin = getXForTime(selectionBegin);
2893 int xEnd = getXForTime(selectionEnd);
2894 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2895 long time = getTimeAtX(e.x);
2896 if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) {
2897 fDragBeginMarker = true;
2898 fDragX = xEnd;
2899 fDragX0 = e.x;
2900 fDragTime0 = selectionEnd;
2901 } else {
2902 fDragX0 = xBegin;
2903 fDragTime0 = selectionBegin;
2904 }
2905 } else {
2906 long time = getTimeAtX(e.x);
2907 if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) {
2908 fDragBeginMarker = true;
2909 fDragX = xEnd;
2910 fDragX0 = e.x;
2911 fDragTime0 = selectionEnd;
2912 } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) {
2913 fDragX0 = xBegin;
2914 fDragTime0 = selectionBegin;
2915 }
2916 }
2917 fTime0bak = fTimeProvider.getTime0();
2918 fTime1bak = fTimeProvider.getTime1();
2919 redraw();
2920 updateCursor(e.x, e.stateMask);
2921 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2922 }
2923 } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) {
2924 long hitTime = getTimeAtX(e.x);
2925 if (hitTime > 0) {
2926 setCapture(true);
2927 fDragState = DRAG_TRACE_ITEM;
2928 fDragButton = e.button;
2929 fDragX = e.x;
2930 fDragX0 = fDragX;
2931 fTime0bak = fTimeProvider.getTime0();
2932 fTime1bak = fTimeProvider.getTime1();
2933 updateCursor(e.x, e.stateMask);
2934 }
2935 } else if (3 == e.button) {
2936 if (e.x >= fTimeProvider.getNameSpace()) {
2937 setCapture(true);
2938 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN);
2939 fDragX0 = fDragX;
2940 fDragTime0 = getTimeAtX(fDragX0);
2941 fDragState = DRAG_ZOOM;
2942 fDragButton = e.button;
2943 redraw();
2944 updateCursor(e.x, e.stateMask);
2945 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2946 } else {
2947 idx = getItemIndexAtY(e.y);
2948 selectItem(idx, false);
2949 fireSelectionChanged();
2950 }
2951 }
2952 }
2953
2954 @Override
2955 public void mouseUp(MouseEvent e) {
2956 if (fPendingMenuDetectEvent != null && e.button == 3) {
2957 if ((fDragState == DRAG_ZOOM) && isInDragZoomMargin()) {
2958 // Select entry and time event for single click
2959 long time = getTimeAtX(fDragX0);
2960 fTimeProvider.setSelectionRangeNotify(time, time, false);
2961 int idx = getItemIndexAtY(e.y);
2962 selectItem(idx, false);
2963 fireSelectionChanged();
2964 }
2965 menuDetected(fPendingMenuDetectEvent);
2966 }
2967 if (DRAG_NONE != fDragState) {
2968 setCapture(false);
2969 if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) {
2970 fDragState = DRAG_NONE;
2971 if (fDragX != fDragX0) {
2972 fTimeProvider.notifyStartFinishTime();
2973 }
2974 } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) {
2975 fDragState = DRAG_NONE;
2976 redraw();
2977 } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) {
2978 fDragState = DRAG_NONE;
2979 if (fDragX == fDragX0) { // click without selecting anything
2980 long time = getTimeAtX(e.x);
2981 fTimeProvider.setSelectedTimeNotify(time, false);
2982 } else {
2983 long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0;
2984 long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX);
2985 fTimeProvider.setSelectionRangeNotify(time0, time1, false);
2986 }
2987 redraw();
2988 fTimeGraphScale.setDragRange(-1, -1);
2989 } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) {
2990 fDragState = DRAG_NONE;
2991 int nameWidth = fTimeProvider.getNameSpace();
2992 if ((Math.max(fDragX, fDragX0) > nameWidth) && !isInDragZoomMargin()) {
2993 long time0 = getTimeAtX(fDragX0);
2994 long time1 = getTimeAtX(fDragX);
2995 if (time0 < time1) {
2996 fTimeProvider.setStartFinishTimeNotify(time0, time1);
2997 } else {
2998 fTimeProvider.setStartFinishTimeNotify(time1, time0);
2999 }
3000 } else {
3001 redraw();
3002 }
3003 fTimeGraphScale.setDragRange(-1, -1);
3004 }
3005 }
3006 updateCursor(e.x, e.stateMask);
3007 updateStatusLine(e.x);
3008 }
3009
3010 @Override
3011 public void mouseEnter(MouseEvent e) {
3012 }
3013
3014 @Override
3015 public void mouseExit(MouseEvent e) {
3016 if (fMouseOverSplitLine) {
3017 fMouseOverSplitLine = false;
3018 redraw();
3019 }
3020 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
3021 }
3022
3023 @Override
3024 public void mouseHover(MouseEvent e) {
3025 }
3026
3027 @Override
3028 public void mouseScrolled(MouseEvent e) {
3029 if (fDragState != DRAG_NONE || e.count == 0) {
3030 return;
3031 }
3032
3033 /*
3034 * On some platforms the mouse scroll event is sent to the
3035 * control that has focus even if it is not under the cursor.
3036 * Handle the event only if over the time graph control.
3037 */
3038 Point size = getSize();
3039 Rectangle bounds = new Rectangle(0, 0, size.x, size.y);
3040 if (!bounds.contains(e.x, e.y)) {
3041 return;
3042 }
3043
3044 boolean horizontalZoom = false;
3045 boolean horizontalScroll = false;
3046 boolean verticalZoom = false;
3047 boolean verticalScroll = false;
3048
3049 // over the time graph control
3050 if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) {
3051 verticalZoom = true;
3052 } else if (e.x < fTimeProvider.getNameSpace()) {
3053 // over the name space
3054 verticalScroll = true;
3055 } else {
3056 // over the state area
3057 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
3058 // over the state area, CTRL pressed
3059 horizontalZoom = true;
3060 } else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
3061 // over the state area, SHIFT pressed
3062 horizontalScroll = true;
3063 } else {
3064 // over the state area, no modifier pressed
3065 verticalScroll = true;
3066 }
3067 }
3068 if (verticalZoom) {
3069 fVerticalZoomAlignEntry = getVerticalZoomAlignCursor(e.y);
3070 verticalZoom(e.count > 0);
3071 if (fVerticalZoomAlignEntry != null) {
3072 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
3073 }
3074 } else if (horizontalZoom && fTimeProvider.getTime0() != fTimeProvider.getTime1()) {
3075 zoom(e.count > 0);
3076 } else if (horizontalScroll) {
3077 horizontalScroll(e.count > 0);
3078 } else if (verticalScroll){
3079 setTopIndex(getTopIndex() - e.count);
3080 }
3081 }
3082
3083 /**
3084 * Get the vertical zoom alignment entry and position based on the current
3085 * selection. If there is no selection or if the selection is not visible,
3086 * return an alignment entry with a null time graph entry.
3087 *
3088 * @return a map entry where the key is the selection's time graph entry and
3089 * the value is the center y-coordinate of that entry, or null
3090 */
3091 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignSelection() {
3092 Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing();
3093 if (alignEntry != null) {
3094 return alignEntry;
3095 }
3096 int index = getSelectedIndex();
3097 if (index == -1 || index >= getExpandedElementCount()) {
3098 return new SimpleEntry<>(null, 0);
3099 }
3100 Rectangle bounds = getClientArea();
3101 Rectangle itemRect = getItemRect(bounds, index);
3102 if (itemRect.y < bounds.y || itemRect.y > bounds.y + bounds.height) {
3103 /* selection is not visible */
3104 return new SimpleEntry<>(null, 0);
3105 }
3106 ITimeGraphEntry entry = getExpandedElement(index);
3107 int y = itemRect.y + itemRect.height / 2;
3108 return new SimpleEntry<>(entry, y);
3109 }
3110
3111 /**
3112 * Get the vertical zoom alignment entry and position at the specified
3113 * cursor position.
3114 *
3115 * @param y
3116 * the cursor y-coordinate
3117 * @return a map entry where the key is the time graph entry under the
3118 * cursor and the value is the cursor y-coordinate
3119 */
3120 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignCursor(int y) {
3121 Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing();
3122 if (alignEntry != null) {
3123 return alignEntry;
3124 }
3125 int index = getItemIndexAtY(y);
3126 if (index == -1) {
3127 index = getExpandedElementCount() - 1;
3128 }
3129 ITimeGraphEntry entry = getExpandedElement(index);
3130 return new SimpleEntry<>(entry, y);
3131 }
3132
3133 /**
3134 * Get the vertical zoom alignment entry and position if there is an ongoing
3135 * one and we are within the vertical zoom delay, or otherwise return null.
3136 *
3137 * @return a map entry where the key is a time graph entry and the value is
3138 * a y-coordinate, or null
3139 */
3140 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignOngoing() {
3141 long currentTimeMillis = System.currentTimeMillis();
3142 if (currentTimeMillis < fVerticalZoomAlignTime + VERTICAL_ZOOM_DELAY) {
3143 /*
3144 * If the vertical zoom is triggered repeatedly in a short amount of
3145 * time, use the initial event's entry and position.
3146 */
3147 fVerticalZoomAlignTime = currentTimeMillis;
3148 return fVerticalZoomAlignEntry;
3149 }
3150 fVerticalZoomAlignTime = currentTimeMillis;
3151 return null;
3152 }
3153
3154 @Override
3155 public void handleEvent(Event event) {
3156 if (event.type == SWT.MouseWheel) {
3157 // prevent horizontal scrolling when the mouse wheel is used to
3158 // scroll vertically or zoom
3159 event.doit = false;
3160 }
3161 }
3162
3163 @Override
3164 public int getBorderWidth() {
3165 return fBorderWidth;
3166 }
3167
3168 /**
3169 * Set the border width
3170 *
3171 * @param borderWidth
3172 * The width
3173 */
3174 public void setBorderWidth(int borderWidth) {
3175 this.fBorderWidth = borderWidth;
3176 }
3177
3178 /**
3179 * @return The current height of the header row
3180 */
3181 public int getHeaderHeight() {
3182 return fHeaderHeight;
3183 }
3184
3185 /**
3186 * Set the height of the header row
3187 *
3188 * @param headerHeight
3189 * The height
3190 */
3191 public void setHeaderHeight(int headerHeight) {
3192 this.fHeaderHeight = headerHeight;
3193 }
3194
3195 /**
3196 * @return The default height of regular item rows
3197 */
3198 public int getItemHeight() {
3199 return fGlobalItemHeight;
3200 }
3201
3202 /**
3203 * Set the default height of regular item rows.
3204 *
3205 * @param rowHeight
3206 * The height
3207 */
3208 public void setItemHeight(int rowHeight) {
3209 this.fGlobalItemHeight = rowHeight;
3210 for (Item item : fItemData.fItems) {
3211 item.fItemHeight = rowHeight;
3212 }
3213 }
3214
3215 /**
3216 * Set the height of a specific item. Overrides the default item height.
3217 *
3218 * @param entry
3219 * A time graph entry
3220 * @param rowHeight
3221 * The height
3222 * @return true if the height is successfully stored, false otherwise
3223 */
3224 public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) {
3225 Item item = fItemData.findItem(entry);
3226 if (item != null) {
3227 item.fItemHeight = rowHeight;
3228 return true;
3229 }
3230 return false;
3231 }
3232
3233 /**
3234 * Set the minimum item width
3235 *
3236 * @param width The minimum width
3237 */
3238 public void setMinimumItemWidth(int width) {
3239 this.fMinimumItemWidth = width;
3240 }
3241
3242 /**
3243 * @return The minimum item width
3244 */
3245 public int getMinimumItemWidth() {
3246 return fMinimumItemWidth;
3247 }
3248
3249 /**
3250 * Set whether all time events with a duration shorter than one pixel should
3251 * be blended in. If false, only the first such time event will be drawn and
3252 * the subsequent time events in the same pixel will be discarded. The
3253 * default value is false.
3254 *
3255 * @param blend
3256 * true if sub-pixel events should be blended, false otherwise.
3257 * @since 1.1
3258 */
3259 public void setBlendSubPixelEvents(boolean blend) {
3260 fBlendSubPixelEvents = blend;
3261 }
3262
3263 @Override
3264 public void addSelectionChangedListener(ISelectionChangedListener listener) {
3265 if (listener != null && !fSelectionChangedListeners.contains(listener)) {
3266 fSelectionChangedListeners.add(listener);
3267 }
3268 }
3269
3270 @Override
3271 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
3272 if (listener != null) {
3273 fSelectionChangedListeners.remove(listener);
3274 }
3275 }
3276
3277 @Override
3278 public void setSelection(ISelection selection) {
3279 if (selection instanceof IStructuredSelection) {
3280 Object ob = ((IStructuredSelection) selection).getFirstElement();
3281 if (ob instanceof ITimeGraphEntry) {
3282 selectItem((ITimeGraphEntry) ob, false);
3283 }
3284 }
3285
3286 }
3287
3288 /**
3289 * @param filter The filter object to be attached to the view
3290 */
3291 public void addFilter(@NonNull ViewerFilter filter) {
3292 if (!fFilters.contains(filter)) {
3293 fFilters.add(filter);
3294 }
3295 }
3296
3297 /**
3298 * @param filter The filter object to be attached to the view
3299 */
3300 public void removeFilter(@NonNull ViewerFilter filter) {
3301 fFilters.remove(filter);
3302 }
3303
3304 /**
3305 * Returns this control's filters.
3306 *
3307 * @return an array of viewer filters
3308 * @since 1.2
3309 */
3310 public @NonNull ViewerFilter[] getFilters() {
3311 return Iterables.toArray(fFilters, ViewerFilter.class);
3312 }
3313
3314 /**
3315 * Sets the filters, replacing any previous filters.
3316 *
3317 * @param filters
3318 * an array of viewer filters, or null
3319 * @since 1.2
3320 */
3321 public void setFilters(@NonNull ViewerFilter[] filters) {
3322 fFilters.clear();
3323 if (filters != null) {
3324 fFilters.addAll(Arrays.asList(filters));
3325 }
3326 }
3327
3328 @Override
3329 public void colorSettingsChanged(StateItem[] stateItems) {
3330 /* Destroy previous colors from the resource manager */
3331 if (fEventColorMap != null) {
3332 for (Color color : fEventColorMap) {
3333 fResourceManager.destroyColor(color.getRGB());
3334 }
3335 }
3336 if (stateItems != null) {
3337 fEventColorMap = new Color[stateItems.length];
3338 for (int i = 0; i < stateItems.length; i++) {
3339 fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor());
3340 }
3341 } else {
3342 fEventColorMap = new Color[] { };
3343 }
3344 redraw();
3345 }
3346
3347 private class ItemData {
3348 private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>();
3349 private Item[] fExpandedItems = new Item[0];
3350 private Item[] fItems = new Item[0];
3351 private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0];
3352 private List<ILinkEvent> fLinks = new ArrayList<>();
3353
3354 public ItemData() {
3355 }
3356
3357 public Item findItem(ITimeGraphEntry entry) {
3358 return fItemMap.get(entry);
3359 }
3360
3361 public int findItemIndex(ITimeGraphEntry entry) {
3362 Item item = fItemMap.get(entry);
3363 if (item == null) {
3364 return -1;
3365 }
3366 return item.fExpandedIndex;
3367 }
3368
3369 public void refreshData() {
3370 ITimeGraphEntry selection = getSelectedTrace();
3371 Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>();
3372 for (int i = 0; i < fRootEntries.length; i++) {
3373 ITimeGraphEntry entry = fRootEntries[i];
3374 refreshData(itemMap, null, 0, entry);
3375 }
3376 fItemMap = itemMap;
3377 fItems = fItemMap.values().toArray(new Item[0]);
3378 updateExpandedItems();
3379 if (selection != null) {
3380 for (Item item : fExpandedItems) {
3381 if (item.fEntry == selection) {
3382 item.fSelected = true;
3383 break;
3384 }
3385 }
3386 }
3387 }
3388
3389 private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) {
3390 Item item = new Item(entry, entry.getName(), level);
3391 if (parent != null) {
3392 parent.fChildren.add(item);
3393 }
3394 if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) {
3395 item.fItemHeight = fTimeGraphProvider.getItemHeight(entry);
3396 } else {
3397 item.fItemHeight = fGlobalItemHeight;
3398 }
3399 item.fItemHeight = Math.max(1, item.fItemHeight + fHeightAdjustment);
3400 itemMap.put(entry, item);
3401 if (entry.hasChildren()) {
3402 Item oldItem = fItemMap.get(entry);
3403 if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) {
3404 /* existing items keep their old expanded state */
3405 item.fExpanded = oldItem.fExpanded;
3406 } else {
3407 /* new items set the expanded state according to auto-expand level */
3408 item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel;
3409 }
3410 item.fHasChildren = true;
3411 for (ITimeGraphEntry child : entry.getChildren()) {
3412 refreshData(itemMap, item, level + 1, child);
3413 }
3414 }
3415 }
3416
3417 public void updateExpandedItems() {
3418 for (Item item : fItems) {
3419 item.fExpandedIndex = -1;
3420 }
3421 List<Item> expandedItemList = new ArrayList<>();
3422 for (int i = 0; i < fRootEntries.length; i++) {
3423 ITimeGraphEntry entry = fRootEntries[i];
3424 Item item = findItem(entry);
3425 refreshExpanded(expandedItemList, item);
3426 }
3427 fExpandedItems = expandedItemList.toArray(new Item[0]);
3428 fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1));
3429 }
3430
3431 private void refreshExpanded(List<Item> expandedItemList, Item item) {
3432 // Check for filters
3433 boolean display = true;
3434 for (ViewerFilter filter : fFilters) {
3435 if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) {
3436 display = false;
3437 break;
3438 }
3439 }
3440 if (display) {
3441 item.fExpandedIndex = expandedItemList.size();
3442 expandedItemList.add(item);
3443 if (item.fHasChildren && item.fExpanded) {
3444 for (Item child : item.fChildren) {
3445 refreshExpanded(expandedItemList, child);
3446 }
3447 }
3448 }
3449 }
3450
3451 public void refreshData(ITimeGraphEntry[] entries) {
3452 if (entries == null) {
3453 fRootEntries = null;
3454 } else {
3455 fRootEntries = Arrays.copyOf(entries, entries.length);
3456 }
3457
3458 refreshData();
3459 }
3460
3461 public void refreshArrows(List<ILinkEvent> events) {
3462 /* If links are null, reset the list */
3463 if (events != null) {
3464 fLinks = events;
3465 } else {
3466 fLinks = new ArrayList<>();
3467 }
3468 }
3469
3470 public ITimeGraphEntry[] getEntries() {
3471 return fRootEntries;
3472 }
3473 }
3474
3475 private class Item {
3476 private boolean fExpanded;
3477 private int fExpandedIndex;
3478 private boolean fSelected;
3479 private boolean fHasChildren;
3480 private int fItemHeight;
3481 private final int fLevel;
3482 private final List<Item> fChildren;
3483 private final String fName;
3484 private final ITimeGraphEntry fEntry;
3485
3486 public Item(ITimeGraphEntry entry, String name, int level) {
3487 this.fEntry = entry;
3488 this.fName = name;
3489 this.fLevel = level;
3490 this.fChildren = new ArrayList<>();
3491 }
3492
3493 @Override
3494 public String toString() {
3495 return fName;
3496 }
3497 }
3498
3499 @Override
3500 public void menuDetected(MenuDetectEvent e) {
3501 if (null == fTimeProvider) {
3502 return;
3503 }
3504 /*
3505 * This flag indicates if menu was prevented from being shown below and
3506 * therefore must be made visible on callback from mouseUp().
3507 */
3508 boolean pendingEventCallback = fPendingMenuDetectEvent != null;
3509 Point p = toControl(e.x, e.y);
3510 if (e.detail == SWT.MENU_MOUSE && isOverTimeSpace(p.x, p.y)) {
3511 if (fPendingMenuDetectEvent == null) {
3512 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3513 * Store the event and trigger it later just before handling mouseUp.
3514 * This allows for the method to detect if mouse is used to drag zoom.
3515 */
3516 fPendingMenuDetectEvent = e;
3517 /*
3518 * Prevent the platform to show the menu when returning. The
3519 * menu will be shown (see below) when this method is called
3520 * again during mouseUp().
3521 */
3522 e.doit = false;
3523 return;
3524 }
3525 fPendingMenuDetectEvent = null;
3526 if (fDragState != DRAG_ZOOM || !isInDragZoomMargin()) {
3527 /*
3528 * Don't show the menu on mouseUp() if a drag zoom is in
3529 * progress with a drag range outside of the drag zoom margin,
3530 * or if any other drag operation, or none, is in progress.
3531 */
3532 e.doit = false;
3533 return;
3534 }
3535 } else {
3536 if (fDragState != DRAG_NONE) {
3537 /*
3538 * Don't show the menu on keyboard menu or mouse menu outside of
3539 * the time space if any drag operation is in progress.
3540 */
3541 e.doit = false;
3542 return;
3543 }
3544 }
3545 int idx = getItemIndexAtY(p.y);
3546 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
3547 Item item = fItemData.fExpandedItems[idx];
3548 ITimeGraphEntry entry = item.fEntry;
3549
3550 /* Send menu event for the time graph entry */
3551 e.doit = true;
3552 e.data = entry;
3553 fireMenuEventOnTimeGraphEntry(e);
3554 Menu menu = getMenu();
3555 if (pendingEventCallback && e.doit && (menu != null)) {
3556 menu.setVisible(true);
3557 }
3558
3559 /* Send menu event for time event */
3560 if (entry.hasTimeEvents()) {
3561 ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2);
3562 if (event != null) {
3563 e.doit = true;
3564 e.data = event;
3565 fireMenuEventOnTimeEvent(e);
3566 menu = getMenu();
3567 if (pendingEventCallback && e.doit && (menu != null)) {
3568 menu.setVisible(true);
3569 }
3570 }
3571 }
3572 }
3573 }
3574
3575 /**
3576 * Perform the alignment operation.
3577 *
3578 * @param offset
3579 * the alignment offset
3580 *
3581 * @see ITmfTimeAligned
3582 *
3583 * @since 1.0
3584 */
3585 public void performAlign(int offset) {
3586 fTimeProvider.setNameSpace(offset);
3587 }
3588
3589 /**
3590 * Return the time alignment information
3591 *
3592 * @return the time alignment information
3593 *
3594 * @see ITmfTimeAligned
3595 *
3596 * @since 1.0
3597 */
3598 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
3599 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace());
3600 }
3601
3602 private boolean isInDragZoomMargin() {
3603 return (Math.abs(fDragX - fDragX0) < DRAG_MARGIN);
3604 }
3605 }
This page took 0.113494 seconds and 5 git commands to generate.