59ee111f75e5eebacf888b7aa5cf51c8165d5ae2
[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 final 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 dstHeight = Math.min(bounds.height, imgHeight);
2334 int dstWidth = dstHeight * imgWidth / imgHeight;
2335 int x = width;
2336 int y = bounds.y + (bounds.height - dstHeight) / 2;
2337 gc.drawImage(img, 0, 0, imgWidth, imgHeight, x, y, dstWidth, dstHeight);
2338 width += imgWidth + MARGIN;
2339 }
2340 } else {
2341 if (fLabelProvider == null) {
2342 break;
2343 }
2344 }
2345 String label = fLabelProvider != null ? fLabelProvider.getColumnText(item.fEntry, columnIndex)
2346 : columnIndex == 0 ? item.fName : ""; //$NON-NLS-1$
2347 gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus));
2348 Rectangle textRect = new Rectangle(rect.x + width, rect.y, rect.width - width, rect.height);
2349 int textWidth = Utils.drawText(gc, label, textRect, true);
2350 width += textWidth + MARGIN;
2351 if (textWidth > 0) {
2352 idealNameSpace = rect.x + width;
2353 }
2354 if (columns.length == 1) {
2355 drawMidLine(new Rectangle(bounds.x + width, bounds.y, bounds.x + bounds.width, bounds.height), gc);
2356 }
2357 if (fAutoResizeColumns && width > column.getWidth()) {
2358 column.setData(PREFERRED_WIDTH, width);
2359 column.setWidth(width);
2360 }
2361 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
2362 if (i < columns.length - 1) {
2363 // not the last visible column: draw the vertical cell border
2364 int x = rect.x + rect.width - 1;
2365 gc.drawLine(x, bounds.y, x, bounds.y + bounds.height);
2366 }
2367 rect.x += rect.width;
2368 }
2369 fIdealNameSpace = Math.max(fIdealNameSpace, idealNameSpace);
2370
2371 gc.setClipping((Rectangle) null);
2372 }
2373
2374 /**
2375 * Draw the state (color fill)
2376 *
2377 * @param colors
2378 * Color scheme
2379 * @param event
2380 * Time event for which we're drawing the state
2381 * @param rect
2382 * The state rectangle
2383 * @param gc
2384 * Graphics context
2385 * @param selected
2386 * Is this time event currently selected (so it appears
2387 * highlighted)
2388 * @param timeSelected
2389 * Is the timestamp currently selected
2390 * @return true if the state was drawn
2391 */
2392 protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event,
2393 Rectangle rect, GC gc, boolean selected, boolean timeSelected) {
2394
2395 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
2396 if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) {
2397 return false;
2398 }
2399 boolean visible = rect.width == 0 ? false : true;
2400 rect.width = Math.max(1, rect.width);
2401 Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
2402 gc.setForeground(black);
2403
2404 if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) {
2405 if (visible) {
2406 // Only draw the top and bottom borders
2407 gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y);
2408 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
2409 if (rect.width == 1) {
2410 gc.drawPoint(rect.x, rect.y - 2);
2411 }
2412 }
2413 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2414 return false;
2415 }
2416 Color stateColor = null;
2417 if (colorIdx < fEventColorMap.length) {
2418 stateColor = fEventColorMap[colorIdx];
2419 } else {
2420 stateColor = black;
2421 }
2422
2423 boolean reallySelected = timeSelected && selected;
2424 // fill all rect area
2425 gc.setBackground(stateColor);
2426 if (visible) {
2427 gc.fillRectangle(rect);
2428 } else if (fBlendSubPixelEvents) {
2429 gc.setAlpha(128);
2430 gc.fillRectangle(rect);
2431 gc.setAlpha(255);
2432 }
2433
2434 if (reallySelected) {
2435 gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1);
2436 gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height);
2437 }
2438 if (!visible) {
2439 gc.drawPoint(rect.x, rect.y - 2);
2440 }
2441 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2442 return visible;
2443 }
2444
2445 /**
2446 * Fill an item's states rectangle
2447 *
2448 * @param rect
2449 * The states rectangle
2450 * @param gc
2451 * Graphics context
2452 * @param selected
2453 * true if the item is selected
2454 */
2455 protected void fillSpace(Rectangle rect, GC gc, boolean selected) {
2456 /* Nothing to draw */
2457 }
2458
2459 /**
2460 * Draw a line at the middle height of a rectangle
2461 *
2462 * @param rect
2463 * The rectangle
2464 * @param gc
2465 * Graphics context
2466 */
2467 private void drawMidLine(Rectangle rect, GC gc) {
2468 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
2469 int midy = rect.y + rect.height / 2;
2470 gc.drawLine(rect.x, midy, rect.x + rect.width, midy);
2471 }
2472
2473 private static int getMarginForHeight(int height) {
2474 /*
2475 * State rectangle is smaller than the item bounds when height is > 4.
2476 * Don't use any margin if the height is below or equal that threshold.
2477 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2478 * pixels for the state height, but with a minimum margin of 1.
2479 */
2480 final int MARGIN_THRESHOLD = 4;
2481 final int PREFERRED_HEIGHT = 13;
2482 final int MIN_MARGIN = 1;
2483 final int MAX_MARGIN = 6;
2484 return height <= MARGIN_THRESHOLD ? 0 :
2485 Math.max(Math.min(height - PREFERRED_HEIGHT, MAX_MARGIN), MIN_MARGIN);
2486 }
2487
2488 private void setFontForHeight(int pixels, GC gc) {
2489 /* convert font height from pixels to points */
2490 int height = Math.max(pixels * PPI / DPI, 1);
2491 Font font = fFonts.get(height);
2492 if (font == null) {
2493 FontData fontData = gc.getFont().getFontData()[0];
2494 fontData.setHeight(height);
2495 font = new Font(gc.getDevice(), fontData);
2496 fFonts.put(height, font);
2497 }
2498 gc.setFont(font);
2499 }
2500
2501 @Override
2502 public void keyTraversed(TraverseEvent e) {
2503 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
2504 e.doit = true;
2505 }
2506 }
2507
2508 @Override
2509 public void keyPressed(KeyEvent e) {
2510 int idx = -1;
2511 if (fItemData.fExpandedItems.length == 0) {
2512 return;
2513 }
2514 if (SWT.HOME == e.keyCode) {
2515 idx = 0;
2516 } else if (SWT.END == e.keyCode) {
2517 idx = fItemData.fExpandedItems.length - 1;
2518 } else if (SWT.ARROW_DOWN == e.keyCode) {
2519 idx = getSelectedIndex();
2520 if (idx < 0) {
2521 idx = 0;
2522 } else if (idx < fItemData.fExpandedItems.length - 1) {
2523 idx++;
2524 }
2525 } else if (SWT.ARROW_UP == e.keyCode) {
2526 idx = getSelectedIndex();
2527 if (idx < 0) {
2528 idx = 0;
2529 } else if (idx > 0) {
2530 idx--;
2531 }
2532 } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) {
2533 boolean extend = (e.stateMask & SWT.SHIFT) != 0;
2534 selectPrevEvent(extend);
2535 } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) {
2536 boolean extend = (e.stateMask & SWT.SHIFT) != 0;
2537 selectNextEvent(extend);
2538 } else if (SWT.PAGE_DOWN == e.keyCode) {
2539 int page = countPerPage();
2540 idx = getSelectedIndex();
2541 if (idx < 0) {
2542 idx = 0;
2543 }
2544 idx += page;
2545 if (idx >= fItemData.fExpandedItems.length) {
2546 idx = fItemData.fExpandedItems.length - 1;
2547 }
2548 } else if (SWT.PAGE_UP == e.keyCode) {
2549 int page = countPerPage();
2550 idx = getSelectedIndex();
2551 if (idx < 0) {
2552 idx = 0;
2553 }
2554 idx -= page;
2555 if (idx < 0) {
2556 idx = 0;
2557 }
2558 } else if (SWT.CR == e.keyCode) {
2559 idx = getSelectedIndex();
2560 if (idx >= 0) {
2561 if (fItemData.fExpandedItems[idx].fHasChildren) {
2562 toggle(idx);
2563 } else {
2564 fireDefaultSelection();
2565 }
2566 }
2567 idx = -1;
2568 } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) != 0)) {
2569 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2570 verticalZoom(true);
2571 if (fVerticalZoomAlignEntry != null) {
2572 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2573 }
2574 } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) != 0)) {
2575 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2576 verticalZoom(false);
2577 if (fVerticalZoomAlignEntry != null) {
2578 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2579 }
2580 } else if (e.character == '0' && ((e.stateMask & SWT.CTRL) != 0)) {
2581 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2582 resetVerticalZoom();
2583 if (fVerticalZoomAlignEntry != null) {
2584 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2585 }
2586 } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) {
2587 if (fHasNamespaceFocus) {
2588 ITimeGraphEntry entry = getSelectedTrace();
2589 setExpandedState(entry, 0, true);
2590 } else {
2591 zoomIn();
2592 }
2593 } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) {
2594 if (fHasNamespaceFocus) {
2595 ITimeGraphEntry entry = getSelectedTrace();
2596 if ((entry != null) && entry.hasChildren()) {
2597 setExpandedState(entry, -1, false);
2598 }
2599 } else {
2600 zoomOut();
2601 }
2602 } else if ((e.character == '*') && ((e.stateMask & SWT.CTRL) == 0)) {
2603 if (fHasNamespaceFocus) {
2604 ITimeGraphEntry entry = getSelectedTrace();
2605 if ((entry != null) && entry.hasChildren()) {
2606 setExpandedStateLevel(entry);
2607 }
2608 }
2609 }
2610 if (idx >= 0) {
2611 selectItem(idx, false);
2612 fireSelectionChanged();
2613 }
2614 int x = toControl(e.display.getCursorLocation()).x;
2615 updateCursor(x, e.stateMask | e.keyCode);
2616 }
2617
2618 @Override
2619 public void keyReleased(KeyEvent e) {
2620 int x = toControl(e.display.getCursorLocation()).x;
2621 updateCursor(x, e.stateMask & ~e.keyCode);
2622 }
2623
2624 @Override
2625 public void focusGained(FocusEvent e) {
2626 fIsInFocus = true;
2627 redraw();
2628 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2629 }
2630
2631 @Override
2632 public void focusLost(FocusEvent e) {
2633 fIsInFocus = false;
2634 if (DRAG_NONE != fDragState) {
2635 setCapture(false);
2636 fDragState = DRAG_NONE;
2637 }
2638 redraw();
2639 updateStatusLine(NO_STATUS);
2640 }
2641
2642 /**
2643 * @return If the current view is focused
2644 */
2645 public boolean isInFocus() {
2646 return fIsInFocus;
2647 }
2648
2649 /**
2650 * Provide the possibility to control the wait cursor externally e.g. data
2651 * requests in progress
2652 *
2653 * @param waitInd Should we wait indefinitely?
2654 */
2655 public void waitCursor(boolean waitInd) {
2656 // Update cursor as indicated
2657 if (waitInd) {
2658 setCursor(fWaitCursor);
2659 } else {
2660 setCursor(null);
2661 }
2662 }
2663
2664 private void updateCursor(int x, int stateMask) {
2665 // if Wait cursor not active, check for the need to change the cursor
2666 if (getCursor() == fWaitCursor) {
2667 return;
2668 }
2669 Cursor cursor = null;
2670 if (fDragState == DRAG_SPLIT_LINE) {
2671 } else if (fDragState == DRAG_SELECTION) {
2672 cursor = fResizeCursor;
2673 } else if (fDragState == DRAG_TRACE_ITEM) {
2674 cursor = fDragCursor;
2675 } else if (fDragState == DRAG_ZOOM) {
2676 cursor = fZoomCursor;
2677 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
2678 cursor = fDragCursor;
2679 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2680 cursor = fResizeCursor;
2681 } else if (!isOverSplitLine(x)) {
2682 long selectionBegin = fTimeProvider.getSelectionBegin();
2683 long selectionEnd = fTimeProvider.getSelectionEnd();
2684 int xBegin = getXForTime(selectionBegin);
2685 int xEnd = getXForTime(selectionEnd);
2686 if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) {
2687 cursor = fResizeCursor;
2688 }
2689 }
2690 if (getCursor() != cursor) {
2691 setCursor(cursor);
2692 }
2693 }
2694
2695 /**
2696 * Update the status line following a change of selection.
2697 *
2698 * @since 2.0
2699 */
2700 public void updateStatusLine() {
2701 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2702 }
2703
2704 private void updateStatusLine(int x) {
2705 // use the time provider of the time graph scale for the status line
2706 ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider();
2707 if (fStatusLineManager == null || null == tdp ||
2708 tdp.getTime0() == tdp.getTime1()) {
2709 return;
2710 }
2711 TimeFormat tf = tdp.getTimeFormat();
2712 Resolution res = Resolution.NANOSEC;
2713 StringBuilder message = new StringBuilder();
2714 if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) {
2715 if (x != STATUS_WITHOUT_CURSOR_TIME) {
2716 long time = getTimeAtX(x);
2717 if (time >= 0) {
2718 if (tdp instanceof ITimeDataProviderConverter) {
2719 time = ((ITimeDataProviderConverter) tdp).convertTime(time);
2720 }
2721 message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$
2722 new Object[] {
2723 tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$
2724 Utils.formatTime(time, tf, res)
2725 }));
2726 }
2727 }
2728 long selectionBegin = tdp.getSelectionBegin();
2729 long selectionEnd = tdp.getSelectionEnd();
2730 message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$
2731 new Object[] {
2732 tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$
2733 Utils.formatTime(selectionBegin, tf, res)
2734 }));
2735 if (selectionBegin != selectionEnd) {
2736 message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2737 new Object[] {
2738 tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$
2739 Utils.formatTime(selectionEnd, tf, res),
2740 Utils.formatDelta(selectionEnd - selectionBegin, tf, res)
2741 }));
2742 }
2743 } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) {
2744 long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0;
2745 long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX);
2746 if (tdp instanceof ITimeDataProviderConverter) {
2747 time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0);
2748 time = ((ITimeDataProviderConverter) tdp).convertTime(time);
2749 }
2750 message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2751 new Object[] {
2752 tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$
2753 Utils.formatTime(time0, tf, res),
2754 tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$
2755 Utils.formatTime(time, tf, res),
2756 Utils.formatDelta(time - time0, tf, res)
2757 }));
2758 }
2759 fStatusLineManager.setMessage(message.toString());
2760 }
2761
2762 @Override
2763 public void mouseMove(MouseEvent e) {
2764 if (null == fTimeProvider) {
2765 return;
2766 }
2767 Point size = getSize();
2768 if (DRAG_TRACE_ITEM == fDragState) {
2769 int nameWidth = fTimeProvider.getNameSpace();
2770 if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) {
2771 fDragX = e.x;
2772 double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak);
2773 long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec));
2774 long time1 = fTime1bak - timeDelta;
2775 long maxTime = fTimeProvider.getMaxTime();
2776 if (time1 > maxTime) {
2777 time1 = maxTime;
2778 }
2779 long time0 = time1 - (fTime1bak - fTime0bak);
2780 if (time0 < fTimeProvider.getMinTime()) {
2781 time0 = fTimeProvider.getMinTime();
2782 time1 = time0 + (fTime1bak - fTime0bak);
2783 }
2784 fTimeProvider.setStartFinishTimeNotify(time0, time1);
2785 }
2786 } else if (DRAG_SPLIT_LINE == fDragState) {
2787 fDragX = e.x;
2788 fTimeProvider.setNameSpace(e.x);
2789 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2790 } else if (DRAG_SELECTION == fDragState) {
2791 if (fDragBeginMarker) {
2792 fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2793 } else {
2794 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2795 }
2796 redraw();
2797 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2798 fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX));
2799 } else if (DRAG_ZOOM == fDragState) {
2800 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2801 redraw();
2802 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2803 } else if (DRAG_NONE == fDragState) {
2804 boolean mouseOverSplitLine = isOverSplitLine(e.x);
2805 if (fMouseOverSplitLine != mouseOverSplitLine) {
2806 redraw();
2807 }
2808 fMouseOverSplitLine = mouseOverSplitLine;
2809 }
2810
2811 if (e.x >= fTimeProvider.getNameSpace()) {
2812 fHasNamespaceFocus = false;
2813 } else {
2814 fHasNamespaceFocus = true;
2815 }
2816 updateCursor(e.x, e.stateMask);
2817 updateStatusLine(e.x);
2818 }
2819
2820 @Override
2821 public void mouseDoubleClick(MouseEvent e) {
2822 if (null == fTimeProvider) {
2823 return;
2824 }
2825 if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) {
2826 if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) {
2827 fTimeProvider.setNameSpace(fIdealNameSpace);
2828 boolean mouseOverSplitLine = isOverSplitLine(e.x);
2829 if (fMouseOverSplitLine != mouseOverSplitLine) {
2830 redraw();
2831 }
2832 fMouseOverSplitLine = mouseOverSplitLine;
2833 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2834 return;
2835 }
2836 int idx = getItemIndexAtY(e.y);
2837 if (idx >= 0) {
2838 selectItem(idx, false);
2839 fireDefaultSelection();
2840 }
2841 }
2842 }
2843
2844 @Override
2845 public void mouseDown(MouseEvent e) {
2846 if (fDragState != DRAG_NONE) {
2847 return;
2848 }
2849 if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) {
2850 int nameSpace = fTimeProvider.getNameSpace();
2851 if (nameSpace != 0 && isOverSplitLine(e.x)) {
2852 fDragState = DRAG_SPLIT_LINE;
2853 fDragButton = e.button;
2854 fDragX = e.x;
2855 fDragX0 = fDragX;
2856 redraw();
2857 updateCursor(e.x, e.stateMask);
2858 return;
2859 }
2860 }
2861 if (fTimeProvider == null ||
2862 fTimeProvider.getTime0() == fTimeProvider.getTime1() ||
2863 getSize().x - fTimeProvider.getNameSpace() <= 0) {
2864 return;
2865 }
2866 int idx;
2867 if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) {
2868 int nameSpace = fTimeProvider.getNameSpace();
2869 idx = getItemIndexAtY(e.y);
2870 if (idx >= 0) {
2871 Item item = fItemData.fExpandedItems[idx];
2872 if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) {
2873 toggle(idx);
2874 return;
2875 }
2876 selectItem(idx, false);
2877 fireSelectionChanged();
2878 } else {
2879 selectItem(idx, false); // clear selection
2880 fireSelectionChanged();
2881 }
2882 long hitTime = getTimeAtX(e.x);
2883 if (hitTime >= 0) {
2884 setCapture(true);
2885
2886 fDragState = DRAG_SELECTION;
2887 fDragBeginMarker = false;
2888 fDragButton = e.button;
2889 fDragX = e.x;
2890 fDragX0 = fDragX;
2891 fDragTime0 = getTimeAtX(fDragX0);
2892 long selectionBegin = fTimeProvider.getSelectionBegin();
2893 long selectionEnd = fTimeProvider.getSelectionEnd();
2894 int xBegin = getXForTime(selectionBegin);
2895 int xEnd = getXForTime(selectionEnd);
2896 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2897 long time = getTimeAtX(e.x);
2898 if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) {
2899 fDragBeginMarker = true;
2900 fDragX = xEnd;
2901 fDragX0 = e.x;
2902 fDragTime0 = selectionEnd;
2903 } else {
2904 fDragX0 = xBegin;
2905 fDragTime0 = selectionBegin;
2906 }
2907 } else {
2908 long time = getTimeAtX(e.x);
2909 if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) {
2910 fDragBeginMarker = true;
2911 fDragX = xEnd;
2912 fDragX0 = e.x;
2913 fDragTime0 = selectionEnd;
2914 } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) {
2915 fDragX0 = xBegin;
2916 fDragTime0 = selectionBegin;
2917 }
2918 }
2919 fTime0bak = fTimeProvider.getTime0();
2920 fTime1bak = fTimeProvider.getTime1();
2921 redraw();
2922 updateCursor(e.x, e.stateMask);
2923 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2924 }
2925 } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) {
2926 long hitTime = getTimeAtX(e.x);
2927 if (hitTime > 0) {
2928 setCapture(true);
2929 fDragState = DRAG_TRACE_ITEM;
2930 fDragButton = e.button;
2931 fDragX = e.x;
2932 fDragX0 = fDragX;
2933 fTime0bak = fTimeProvider.getTime0();
2934 fTime1bak = fTimeProvider.getTime1();
2935 updateCursor(e.x, e.stateMask);
2936 }
2937 } else if (3 == e.button) {
2938 if (e.x >= fTimeProvider.getNameSpace()) {
2939 setCapture(true);
2940 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN);
2941 fDragX0 = fDragX;
2942 fDragTime0 = getTimeAtX(fDragX0);
2943 fDragState = DRAG_ZOOM;
2944 fDragButton = e.button;
2945 redraw();
2946 updateCursor(e.x, e.stateMask);
2947 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2948 } else {
2949 idx = getItemIndexAtY(e.y);
2950 selectItem(idx, false);
2951 fireSelectionChanged();
2952 }
2953 }
2954 }
2955
2956 @Override
2957 public void mouseUp(MouseEvent e) {
2958 if (fPendingMenuDetectEvent != null && e.button == 3) {
2959 if ((fDragState == DRAG_ZOOM) && isInDragZoomMargin()) {
2960 // Select entry and time event for single click
2961 long time = getTimeAtX(fDragX0);
2962 fTimeProvider.setSelectionRangeNotify(time, time, false);
2963 int idx = getItemIndexAtY(e.y);
2964 selectItem(idx, false);
2965 fireSelectionChanged();
2966 }
2967 menuDetected(fPendingMenuDetectEvent);
2968 }
2969 if (DRAG_NONE != fDragState) {
2970 setCapture(false);
2971 if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) {
2972 fDragState = DRAG_NONE;
2973 if (fDragX != fDragX0) {
2974 fTimeProvider.notifyStartFinishTime();
2975 }
2976 } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) {
2977 fDragState = DRAG_NONE;
2978 redraw();
2979 } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) {
2980 fDragState = DRAG_NONE;
2981 if (fDragX == fDragX0) { // click without selecting anything
2982 long time = getTimeAtX(e.x);
2983 fTimeProvider.setSelectedTimeNotify(time, false);
2984 } else {
2985 long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0;
2986 long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX);
2987 fTimeProvider.setSelectionRangeNotify(time0, time1, false);
2988 }
2989 redraw();
2990 fTimeGraphScale.setDragRange(-1, -1);
2991 } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) {
2992 fDragState = DRAG_NONE;
2993 int nameWidth = fTimeProvider.getNameSpace();
2994 if ((Math.max(fDragX, fDragX0) > nameWidth) && !isInDragZoomMargin()) {
2995 long time0 = getTimeAtX(fDragX0);
2996 long time1 = getTimeAtX(fDragX);
2997 if (time0 < time1) {
2998 fTimeProvider.setStartFinishTimeNotify(time0, time1);
2999 } else {
3000 fTimeProvider.setStartFinishTimeNotify(time1, time0);
3001 }
3002 } else {
3003 redraw();
3004 }
3005 fTimeGraphScale.setDragRange(-1, -1);
3006 }
3007 }
3008 updateCursor(e.x, e.stateMask);
3009 updateStatusLine(e.x);
3010 }
3011
3012 @Override
3013 public void mouseEnter(MouseEvent e) {
3014 }
3015
3016 @Override
3017 public void mouseExit(MouseEvent e) {
3018 if (fMouseOverSplitLine) {
3019 fMouseOverSplitLine = false;
3020 redraw();
3021 }
3022 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
3023 }
3024
3025 @Override
3026 public void mouseHover(MouseEvent e) {
3027 }
3028
3029 @Override
3030 public void mouseScrolled(MouseEvent e) {
3031 if (fDragState != DRAG_NONE || e.count == 0) {
3032 return;
3033 }
3034
3035 /*
3036 * On some platforms the mouse scroll event is sent to the
3037 * control that has focus even if it is not under the cursor.
3038 * Handle the event only if over the time graph control.
3039 */
3040 Point size = getSize();
3041 Rectangle bounds = new Rectangle(0, 0, size.x, size.y);
3042 if (!bounds.contains(e.x, e.y)) {
3043 return;
3044 }
3045
3046 boolean horizontalZoom = false;
3047 boolean horizontalScroll = false;
3048 boolean verticalZoom = false;
3049 boolean verticalScroll = false;
3050
3051 // over the time graph control
3052 if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) {
3053 verticalZoom = true;
3054 } else if (e.x < fTimeProvider.getNameSpace()) {
3055 // over the name space
3056 verticalScroll = true;
3057 } else {
3058 // over the state area
3059 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
3060 // over the state area, CTRL pressed
3061 horizontalZoom = true;
3062 } else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
3063 // over the state area, SHIFT pressed
3064 horizontalScroll = true;
3065 } else {
3066 // over the state area, no modifier pressed
3067 verticalScroll = true;
3068 }
3069 }
3070 if (verticalZoom) {
3071 fVerticalZoomAlignEntry = getVerticalZoomAlignCursor(e.y);
3072 verticalZoom(e.count > 0);
3073 if (fVerticalZoomAlignEntry != null) {
3074 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
3075 }
3076 } else if (horizontalZoom && fTimeProvider.getTime0() != fTimeProvider.getTime1()) {
3077 zoom(e.count > 0);
3078 } else if (horizontalScroll) {
3079 horizontalScroll(e.count > 0);
3080 } else if (verticalScroll){
3081 setTopIndex(getTopIndex() - e.count);
3082 }
3083 }
3084
3085 /**
3086 * Get the vertical zoom alignment entry and position based on the current
3087 * selection. If there is no selection or if the selection is not visible,
3088 * return an alignment entry with a null time graph entry.
3089 *
3090 * @return a map entry where the key is the selection's time graph entry and
3091 * the value is the center y-coordinate of that entry, or null
3092 */
3093 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignSelection() {
3094 Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing();
3095 if (alignEntry != null) {
3096 return alignEntry;
3097 }
3098 int index = getSelectedIndex();
3099 if (index == -1 || index >= getExpandedElementCount()) {
3100 return new SimpleEntry<>(null, 0);
3101 }
3102 Rectangle bounds = getClientArea();
3103 Rectangle itemRect = getItemRect(bounds, index);
3104 if (itemRect.y < bounds.y || itemRect.y > bounds.y + bounds.height) {
3105 /* selection is not visible */
3106 return new SimpleEntry<>(null, 0);
3107 }
3108 ITimeGraphEntry entry = getExpandedElement(index);
3109 int y = itemRect.y + itemRect.height / 2;
3110 return new SimpleEntry<>(entry, y);
3111 }
3112
3113 /**
3114 * Get the vertical zoom alignment entry and position at the specified
3115 * cursor position.
3116 *
3117 * @param y
3118 * the cursor y-coordinate
3119 * @return a map entry where the key is the time graph entry under the
3120 * cursor and the value is the cursor y-coordinate
3121 */
3122 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignCursor(int y) {
3123 Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing();
3124 if (alignEntry != null) {
3125 return alignEntry;
3126 }
3127 int index = getItemIndexAtY(y);
3128 if (index == -1) {
3129 index = getExpandedElementCount() - 1;
3130 }
3131 ITimeGraphEntry entry = getExpandedElement(index);
3132 return new SimpleEntry<>(entry, y);
3133 }
3134
3135 /**
3136 * Get the vertical zoom alignment entry and position if there is an ongoing
3137 * one and we are within the vertical zoom delay, or otherwise return null.
3138 *
3139 * @return a map entry where the key is a time graph entry and the value is
3140 * a y-coordinate, or null
3141 */
3142 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignOngoing() {
3143 long currentTimeMillis = System.currentTimeMillis();
3144 if (currentTimeMillis < fVerticalZoomAlignTime + VERTICAL_ZOOM_DELAY) {
3145 /*
3146 * If the vertical zoom is triggered repeatedly in a short amount of
3147 * time, use the initial event's entry and position.
3148 */
3149 fVerticalZoomAlignTime = currentTimeMillis;
3150 return fVerticalZoomAlignEntry;
3151 }
3152 fVerticalZoomAlignTime = currentTimeMillis;
3153 return null;
3154 }
3155
3156 @Override
3157 public void handleEvent(Event event) {
3158 if (event.type == SWT.MouseWheel) {
3159 // prevent horizontal scrolling when the mouse wheel is used to
3160 // scroll vertically or zoom
3161 event.doit = false;
3162 }
3163 }
3164
3165 @Override
3166 public int getBorderWidth() {
3167 return fBorderWidth;
3168 }
3169
3170 /**
3171 * Set the border width
3172 *
3173 * @param borderWidth
3174 * The width
3175 */
3176 public void setBorderWidth(int borderWidth) {
3177 this.fBorderWidth = borderWidth;
3178 }
3179
3180 /**
3181 * @return The current height of the header row
3182 */
3183 public int getHeaderHeight() {
3184 return fHeaderHeight;
3185 }
3186
3187 /**
3188 * Set the height of the header row
3189 *
3190 * @param headerHeight
3191 * The height
3192 */
3193 public void setHeaderHeight(int headerHeight) {
3194 this.fHeaderHeight = headerHeight;
3195 }
3196
3197 /**
3198 * @return The default height of regular item rows
3199 */
3200 public int getItemHeight() {
3201 return fGlobalItemHeight;
3202 }
3203
3204 /**
3205 * Set the default height of regular item rows.
3206 *
3207 * @param rowHeight
3208 * The height
3209 */
3210 public void setItemHeight(int rowHeight) {
3211 this.fGlobalItemHeight = rowHeight;
3212 for (Item item : fItemData.fItems) {
3213 item.fItemHeight = rowHeight;
3214 }
3215 }
3216
3217 /**
3218 * Set the height of a specific item. Overrides the default item height.
3219 *
3220 * @param entry
3221 * A time graph entry
3222 * @param rowHeight
3223 * The height
3224 * @return true if the height is successfully stored, false otherwise
3225 */
3226 public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) {
3227 Item item = fItemData.findItem(entry);
3228 if (item != null) {
3229 item.fItemHeight = rowHeight;
3230 return true;
3231 }
3232 return false;
3233 }
3234
3235 /**
3236 * Set the minimum item width
3237 *
3238 * @param width The minimum width
3239 */
3240 public void setMinimumItemWidth(int width) {
3241 this.fMinimumItemWidth = width;
3242 }
3243
3244 /**
3245 * @return The minimum item width
3246 */
3247 public int getMinimumItemWidth() {
3248 return fMinimumItemWidth;
3249 }
3250
3251 /**
3252 * Set whether all time events with a duration shorter than one pixel should
3253 * be blended in. If false, only the first such time event will be drawn and
3254 * the subsequent time events in the same pixel will be discarded. The
3255 * default value is false.
3256 *
3257 * @param blend
3258 * true if sub-pixel events should be blended, false otherwise.
3259 * @since 1.1
3260 */
3261 public void setBlendSubPixelEvents(boolean blend) {
3262 fBlendSubPixelEvents = blend;
3263 }
3264
3265 @Override
3266 public void addSelectionChangedListener(ISelectionChangedListener listener) {
3267 if (listener != null && !fSelectionChangedListeners.contains(listener)) {
3268 fSelectionChangedListeners.add(listener);
3269 }
3270 }
3271
3272 @Override
3273 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
3274 if (listener != null) {
3275 fSelectionChangedListeners.remove(listener);
3276 }
3277 }
3278
3279 @Override
3280 public void setSelection(ISelection selection) {
3281 if (selection instanceof IStructuredSelection) {
3282 Object ob = ((IStructuredSelection) selection).getFirstElement();
3283 if (ob instanceof ITimeGraphEntry) {
3284 selectItem((ITimeGraphEntry) ob, false);
3285 }
3286 }
3287
3288 }
3289
3290 /**
3291 * @param filter The filter object to be attached to the view
3292 */
3293 public void addFilter(@NonNull ViewerFilter filter) {
3294 if (!fFilters.contains(filter)) {
3295 fFilters.add(filter);
3296 }
3297 }
3298
3299 /**
3300 * @param filter The filter object to be attached to the view
3301 */
3302 public void removeFilter(@NonNull ViewerFilter filter) {
3303 fFilters.remove(filter);
3304 }
3305
3306 /**
3307 * Returns this control's filters.
3308 *
3309 * @return an array of viewer filters
3310 * @since 1.2
3311 */
3312 public @NonNull ViewerFilter[] getFilters() {
3313 return Iterables.toArray(fFilters, ViewerFilter.class);
3314 }
3315
3316 /**
3317 * Sets the filters, replacing any previous filters.
3318 *
3319 * @param filters
3320 * an array of viewer filters, or null
3321 * @since 1.2
3322 */
3323 public void setFilters(@NonNull ViewerFilter[] filters) {
3324 fFilters.clear();
3325 if (filters != null) {
3326 fFilters.addAll(Arrays.asList(filters));
3327 }
3328 }
3329
3330 @Override
3331 public void colorSettingsChanged(StateItem[] stateItems) {
3332 /* Destroy previous colors from the resource manager */
3333 if (fEventColorMap != null) {
3334 for (Color color : fEventColorMap) {
3335 fResourceManager.destroyColor(color.getRGB());
3336 }
3337 }
3338 if (stateItems != null) {
3339 fEventColorMap = new Color[stateItems.length];
3340 for (int i = 0; i < stateItems.length; i++) {
3341 fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor());
3342 }
3343 } else {
3344 fEventColorMap = new Color[] { };
3345 }
3346 redraw();
3347 }
3348
3349 private class ItemData {
3350 private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>();
3351 private Item[] fExpandedItems = new Item[0];
3352 private Item[] fItems = new Item[0];
3353 private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0];
3354 private List<ILinkEvent> fLinks = new ArrayList<>();
3355
3356 public ItemData() {
3357 }
3358
3359 public Item findItem(ITimeGraphEntry entry) {
3360 return fItemMap.get(entry);
3361 }
3362
3363 public int findItemIndex(ITimeGraphEntry entry) {
3364 Item item = fItemMap.get(entry);
3365 if (item == null) {
3366 return -1;
3367 }
3368 return item.fExpandedIndex;
3369 }
3370
3371 public void refreshData() {
3372 ITimeGraphEntry selection = getSelectedTrace();
3373 Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>();
3374 for (int i = 0; i < fRootEntries.length; i++) {
3375 ITimeGraphEntry entry = fRootEntries[i];
3376 refreshData(itemMap, null, 0, entry);
3377 }
3378 fItemMap = itemMap;
3379 fItems = fItemMap.values().toArray(new Item[0]);
3380 updateExpandedItems();
3381 if (selection != null) {
3382 for (Item item : fExpandedItems) {
3383 if (item.fEntry == selection) {
3384 item.fSelected = true;
3385 break;
3386 }
3387 }
3388 }
3389 }
3390
3391 private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) {
3392 Item item = new Item(entry, entry.getName(), level);
3393 if (parent != null) {
3394 parent.fChildren.add(item);
3395 }
3396 if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) {
3397 item.fItemHeight = fTimeGraphProvider.getItemHeight(entry);
3398 } else {
3399 item.fItemHeight = fGlobalItemHeight;
3400 }
3401 item.fItemHeight = Math.max(1, item.fItemHeight + fHeightAdjustment);
3402 itemMap.put(entry, item);
3403 if (entry.hasChildren()) {
3404 Item oldItem = fItemMap.get(entry);
3405 if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) {
3406 /* existing items keep their old expanded state */
3407 item.fExpanded = oldItem.fExpanded;
3408 } else {
3409 /* new items set the expanded state according to auto-expand level */
3410 item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel;
3411 }
3412 item.fHasChildren = true;
3413 for (ITimeGraphEntry child : entry.getChildren()) {
3414 refreshData(itemMap, item, level + 1, child);
3415 }
3416 }
3417 }
3418
3419 public void updateExpandedItems() {
3420 for (Item item : fItems) {
3421 item.fExpandedIndex = -1;
3422 }
3423 List<Item> expandedItemList = new ArrayList<>();
3424 for (int i = 0; i < fRootEntries.length; i++) {
3425 ITimeGraphEntry entry = fRootEntries[i];
3426 Item item = findItem(entry);
3427 refreshExpanded(expandedItemList, item);
3428 }
3429 fExpandedItems = expandedItemList.toArray(new Item[0]);
3430 fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1));
3431 }
3432
3433 private void refreshExpanded(List<Item> expandedItemList, Item item) {
3434 // Check for filters
3435 boolean display = true;
3436 for (ViewerFilter filter : fFilters) {
3437 if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) {
3438 display = false;
3439 break;
3440 }
3441 }
3442 if (display) {
3443 item.fExpandedIndex = expandedItemList.size();
3444 expandedItemList.add(item);
3445 if (item.fHasChildren && item.fExpanded) {
3446 for (Item child : item.fChildren) {
3447 refreshExpanded(expandedItemList, child);
3448 }
3449 }
3450 }
3451 }
3452
3453 public void refreshData(ITimeGraphEntry[] entries) {
3454 if (entries == null) {
3455 fRootEntries = null;
3456 } else {
3457 fRootEntries = Arrays.copyOf(entries, entries.length);
3458 }
3459
3460 refreshData();
3461 }
3462
3463 public void refreshArrows(List<ILinkEvent> events) {
3464 /* If links are null, reset the list */
3465 if (events != null) {
3466 fLinks = events;
3467 } else {
3468 fLinks = new ArrayList<>();
3469 }
3470 }
3471
3472 public ITimeGraphEntry[] getEntries() {
3473 return fRootEntries;
3474 }
3475 }
3476
3477 private class Item {
3478 private boolean fExpanded;
3479 private int fExpandedIndex;
3480 private boolean fSelected;
3481 private boolean fHasChildren;
3482 private int fItemHeight;
3483 private final int fLevel;
3484 private final List<Item> fChildren;
3485 private final String fName;
3486 private final ITimeGraphEntry fEntry;
3487
3488 public Item(ITimeGraphEntry entry, String name, int level) {
3489 this.fEntry = entry;
3490 this.fName = name;
3491 this.fLevel = level;
3492 this.fChildren = new ArrayList<>();
3493 }
3494
3495 @Override
3496 public String toString() {
3497 return fName;
3498 }
3499 }
3500
3501 @Override
3502 public void menuDetected(MenuDetectEvent e) {
3503 if (null == fTimeProvider) {
3504 return;
3505 }
3506 /*
3507 * This flag indicates if menu was prevented from being shown below and
3508 * therefore must be made visible on callback from mouseUp().
3509 */
3510 boolean pendingEventCallback = fPendingMenuDetectEvent != null;
3511 Point p = toControl(e.x, e.y);
3512 if (e.detail == SWT.MENU_MOUSE && isOverTimeSpace(p.x, p.y)) {
3513 if (fPendingMenuDetectEvent == null) {
3514 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3515 * Store the event and trigger it later just before handling mouseUp.
3516 * This allows for the method to detect if mouse is used to drag zoom.
3517 */
3518 fPendingMenuDetectEvent = e;
3519 /*
3520 * Prevent the platform to show the menu when returning. The
3521 * menu will be shown (see below) when this method is called
3522 * again during mouseUp().
3523 */
3524 e.doit = false;
3525 return;
3526 }
3527 fPendingMenuDetectEvent = null;
3528 if (fDragState != DRAG_ZOOM || !isInDragZoomMargin()) {
3529 /*
3530 * Don't show the menu on mouseUp() if a drag zoom is in
3531 * progress with a drag range outside of the drag zoom margin,
3532 * or if any other drag operation, or none, is in progress.
3533 */
3534 e.doit = false;
3535 return;
3536 }
3537 } else {
3538 if (fDragState != DRAG_NONE) {
3539 /*
3540 * Don't show the menu on keyboard menu or mouse menu outside of
3541 * the time space if any drag operation is in progress.
3542 */
3543 e.doit = false;
3544 return;
3545 }
3546 }
3547 int idx = getItemIndexAtY(p.y);
3548 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
3549 Item item = fItemData.fExpandedItems[idx];
3550 ITimeGraphEntry entry = item.fEntry;
3551
3552 /* Send menu event for the time graph entry */
3553 e.doit = true;
3554 e.data = entry;
3555 fireMenuEventOnTimeGraphEntry(e);
3556 Menu menu = getMenu();
3557 if (pendingEventCallback && e.doit && (menu != null)) {
3558 menu.setVisible(true);
3559 }
3560
3561 /* Send menu event for time event */
3562 if (entry.hasTimeEvents()) {
3563 ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2);
3564 if (event != null) {
3565 e.doit = true;
3566 e.data = event;
3567 fireMenuEventOnTimeEvent(e);
3568 menu = getMenu();
3569 if (pendingEventCallback && e.doit && (menu != null)) {
3570 menu.setVisible(true);
3571 }
3572 }
3573 }
3574 }
3575 }
3576
3577 /**
3578 * Perform the alignment operation.
3579 *
3580 * @param offset
3581 * the alignment offset
3582 *
3583 * @see ITmfTimeAligned
3584 *
3585 * @since 1.0
3586 */
3587 public void performAlign(int offset) {
3588 fTimeProvider.setNameSpace(offset);
3589 }
3590
3591 /**
3592 * Return the time alignment information
3593 *
3594 * @return the time alignment information
3595 *
3596 * @see ITmfTimeAligned
3597 *
3598 * @since 1.0
3599 */
3600 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
3601 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace());
3602 }
3603
3604 private boolean isInDragZoomMargin() {
3605 return (Math.abs(fDragX - fDragX0) < DRAG_MARGIN);
3606 }
3607 }
This page took 0.107549 seconds and 4 git commands to generate.