1 /*****************************************************************************
2 * Copyright (c) 2007, 2016 Intel Corporation and others
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
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 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
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
;
33 import java
.util
.Map
.Entry
;
34 import java
.util
.Queue
;
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
.SelectionChangedEvent
;
45 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
46 import org
.eclipse
.osgi
.util
.NLS
;
47 import org
.eclipse
.swt
.SWT
;
48 import org
.eclipse
.swt
.events
.FocusEvent
;
49 import org
.eclipse
.swt
.events
.FocusListener
;
50 import org
.eclipse
.swt
.events
.KeyEvent
;
51 import org
.eclipse
.swt
.events
.KeyListener
;
52 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
53 import org
.eclipse
.swt
.events
.MenuDetectListener
;
54 import org
.eclipse
.swt
.events
.MouseEvent
;
55 import org
.eclipse
.swt
.events
.MouseListener
;
56 import org
.eclipse
.swt
.events
.MouseMoveListener
;
57 import org
.eclipse
.swt
.events
.MouseTrackListener
;
58 import org
.eclipse
.swt
.events
.MouseWheelListener
;
59 import org
.eclipse
.swt
.events
.PaintEvent
;
60 import org
.eclipse
.swt
.events
.SelectionListener
;
61 import org
.eclipse
.swt
.events
.TraverseEvent
;
62 import org
.eclipse
.swt
.events
.TraverseListener
;
63 import org
.eclipse
.swt
.events
.TypedEvent
;
64 import org
.eclipse
.swt
.graphics
.Color
;
65 import org
.eclipse
.swt
.graphics
.Cursor
;
66 import org
.eclipse
.swt
.graphics
.Font
;
67 import org
.eclipse
.swt
.graphics
.FontData
;
68 import org
.eclipse
.swt
.graphics
.GC
;
69 import org
.eclipse
.swt
.graphics
.Image
;
70 import org
.eclipse
.swt
.graphics
.Point
;
71 import org
.eclipse
.swt
.graphics
.Rectangle
;
72 import org
.eclipse
.swt
.widgets
.Composite
;
73 import org
.eclipse
.swt
.widgets
.Display
;
74 import org
.eclipse
.swt
.widgets
.Event
;
75 import org
.eclipse
.swt
.widgets
.Listener
;
76 import org
.eclipse
.swt
.widgets
.Menu
;
77 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
96 import com
.google
.common
.collect
.Iterables
;
99 * Time graph control implementation
101 * @author Alvaro Sanchez-Leon
102 * @author Patrick Tasse
104 public class TimeGraphControl
extends TimeGraphBaseControl
105 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
106 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
107 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
109 /** Constant indicating that all levels of the time graph should be expanded */
110 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
112 private static final int DRAG_NONE
= 0;
113 private static final int DRAG_TRACE_ITEM
= 1;
114 private static final int DRAG_SPLIT_LINE
= 2;
115 private static final int DRAG_ZOOM
= 3;
116 private static final int DRAG_SELECTION
= 4;
118 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
120 private static final double ZOOM_FACTOR
= 1.5;
121 private static final double ZOOM_IN_FACTOR
= 0.8;
122 private static final double ZOOM_OUT_FACTOR
= 1.25;
124 private static final int SNAP_WIDTH
= 2;
125 private static final int ARROW_HOVER_MAX_DIST
= 5;
127 private static final int NO_STATUS
= -1;
128 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
130 private static final int MAX_LABEL_LENGTH
= 256;
132 private static final int PPI
= 72; // points per inch
133 private static final int DPI
= Display
.getDefault().getDPI().y
;
135 private static final int VERTICAL_ZOOM_DELAY
= 400;
137 /** Resource manager */
138 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
140 /** Color map for event types */
141 private Color
[] fEventColorMap
= null;
143 private ITimeDataProvider fTimeProvider
;
144 private IStatusLineManager fStatusLineManager
= null;
145 private TimeGraphScale fTimeGraphScale
= null;
147 private boolean fIsInFocus
= false;
148 private boolean fMouseOverSplitLine
= false;
149 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
150 private int fHeightAdjustment
= 0;
151 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
152 private boolean fBlendSubPixelEvents
= false;
153 private int fMinimumItemWidth
= 0;
154 private int fTopIndex
= 0;
155 private int fDragState
= DRAG_NONE
;
156 private boolean fDragBeginMarker
= false;
157 private int fDragButton
;
158 private int fDragX0
= 0;
159 private int fDragX
= 0;
160 private boolean fHasNamespaceFocus
= false;
161 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
162 private int fIdealNameSpace
= 0;
163 private long fTime0bak
;
164 private long fTime1bak
;
165 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
166 private ItemData fItemData
= null;
167 private List
<IMarkerEvent
> fMarkers
= null;
168 private boolean fMarkersVisible
= true;
169 private List
<SelectionListener
> fSelectionListeners
;
170 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
171 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
172 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
173 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
174 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
175 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
176 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
177 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
178 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
179 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
180 private MenuDetectEvent fPendingMenuDetectEvent
= null;
181 private boolean fGridLinesVisible
= true;
182 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
183 private boolean fHideArrows
= false;
184 private int fAutoExpandLevel
= ALL_LEVELS
;
185 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
186 private long fVerticalZoomAlignTime
= 0;
187 private int fBorderWidth
= 0;
188 private int fHeaderHeight
= 0;
191 * Standard constructor
194 * The parent composite object
196 * The color scheme to use
198 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
200 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
202 fItemData
= new ItemData();
204 addFocusListener(this);
205 addMouseListener(this);
206 addMouseMoveListener(this);
207 addMouseTrackListener(this);
208 addMouseWheelListener(this);
209 addTraverseListener(this);
210 addKeyListener(this);
211 addMenuDetectListener(this);
212 addListener(SWT
.MouseWheel
, this);
213 addDisposeListener((e
) -> {
214 fResourceManager
.dispose();
215 for (Font font
: fFonts
.values()) {
222 * Sets the timegraph provider used by this timegraph viewer.
224 * @param timeGraphProvider the timegraph provider
226 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
227 fTimeGraphProvider
= timeGraphProvider
;
229 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
230 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
231 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
234 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
235 colorSettingsChanged(stateItems
);
239 * Gets the timegraph provider used by this timegraph viewer.
241 * @return the timegraph provider, or <code>null</code> if not set.
243 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
244 return fTimeGraphProvider
;
248 * Gets the time data provider used by this viewer.
250 * @return The time data provider, or <code>null</code> if not set
252 public ITimeDataProvider
getTimeDataProvider() {
253 return fTimeProvider
;
257 * Gets the color map used by this timegraph viewer.
259 * @return a color map, or <code>null</code> if not set.
261 public Color
[] getEventColorMap() {
262 return fEventColorMap
;
266 * Assign the given time provider
268 * @param timeProvider
271 public void setTimeProvider(ITimeDataProvider timeProvider
) {
272 fTimeProvider
= timeProvider
;
277 * Assign the status line manager
279 * @param statusLineManager
280 * The status line manager, or null to disable status line messages
282 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
283 if (fStatusLineManager
!= null && statusLineManager
== null) {
284 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
286 fStatusLineManager
= statusLineManager
;
290 * Assign the time graph scale
292 * @param timeGraphScale
293 * The time graph scale
295 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
296 fTimeGraphScale
= timeGraphScale
;
300 * Add a selection listener
303 * The listener to add
305 public void addSelectionListener(SelectionListener listener
) {
306 if (listener
== null) {
307 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
309 if (null == fSelectionListeners
) {
310 fSelectionListeners
= new ArrayList
<>();
312 fSelectionListeners
.add(listener
);
316 * Remove a selection listener
319 * The listener to remove
321 public void removeSelectionListener(SelectionListener listener
) {
322 if (null != fSelectionListeners
) {
323 fSelectionListeners
.remove(listener
);
328 * Selection changed callback
330 public void fireSelectionChanged() {
331 if (null != fSelectionListeners
) {
332 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
333 while (it
.hasNext()) {
334 SelectionListener listener
= it
.next();
335 listener
.widgetSelected(null);
339 if (null != fSelectionChangedListeners
) {
340 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
341 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
347 * Default selection callback
349 public void fireDefaultSelection() {
350 if (null != fSelectionListeners
) {
351 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
352 while (it
.hasNext()) {
353 SelectionListener listener
= it
.next();
354 listener
.widgetDefaultSelected(null);
360 * Add a drag selection listener
363 * The listener to add
365 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
366 if (listener
== null) {
367 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
369 if (null == fDragSelectionListeners
) {
370 fDragSelectionListeners
= new ArrayList
<>();
372 fDragSelectionListeners
.add(listener
);
376 * Remove a drag selection listener
379 * The listener to remove
381 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
382 if (null != fDragSelectionListeners
) {
383 fDragSelectionListeners
.remove(listener
);
388 * Drag Selection changed callback
391 * Time interval start
395 public void fireDragSelectionChanged(long start
, long end
) {
396 // check for backward intervals
397 long beginTime
, endTime
;
405 // call the listeners
406 if (null != fDragSelectionListeners
) {
407 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
408 while (it
.hasNext()) {
409 ITimeGraphTimeListener listener
= it
.next();
410 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
416 * Get the traces in the model
418 * @return The array of traces
420 public ITimeGraphEntry
[] getTraces() {
421 return fItemData
.getEntries();
425 * Refresh the data for the thing
427 public void refreshData() {
428 fItemData
.refreshData();
433 * Refresh data for the given traces
436 * The traces to refresh
438 public void refreshData(ITimeGraphEntry
[] traces
) {
439 fItemData
.refreshData(traces
);
444 * Refresh the links (arrows) of this widget
446 * @param events The link event list
448 public void refreshArrows(List
<ILinkEvent
> events
) {
449 fItemData
.refreshArrows(events
);
453 * Get the links (arrows) of this widget
455 * @return The unmodifiable link event list
459 public List
<ILinkEvent
> getArrows() {
460 return Collections
.unmodifiableList(fItemData
.fLinks
);
463 boolean ensureVisibleItem(int idx
, boolean redraw
) {
464 boolean changed
= false;
467 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
468 if (fItemData
.fExpandedItems
[index
].fSelected
) {
473 if (index
>= fItemData
.fExpandedItems
.length
) {
476 if (index
< fTopIndex
) {
483 int page
= countPerPage();
484 if (index
>= fTopIndex
+ page
) {
485 setTopIndex(index
- page
+ 1);
496 * Assign the given index as the top one
501 public void setTopIndex(int idx
) {
502 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
503 index
= Math
.max(0, index
);
509 * Set the top index so that the requested element is at the specified
513 * the time graph entry to be positioned
515 * the requested y-coordinate
518 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
519 Item item
= fItemData
.fItemMap
.get(entry
);
520 if (item
== null || item
.fExpandedIndex
== -1) {
523 int index
= item
.fExpandedIndex
;
524 Rectangle itemRect
= getItemRect(getClientArea(), index
);
525 int delta
= itemRect
.y
+ itemRect
.height
- y
;
526 int topIndex
= getItemIndexAtY(delta
);
527 if (topIndex
!= -1) {
528 setTopIndex(topIndex
);
533 setTopIndex(getExpandedElementCount());
539 * Sets the auto-expand level to be used for new entries discovered when
540 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
541 * . The value 0 means that there is no auto-expand; 1 means that top-level
542 * entries are expanded, but not their children; 2 means that top-level
543 * entries are expanded, and their children, but not grand-children; and so
546 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
550 * non-negative level, or <code>ALL_LEVELS</code> to expand all
553 public void setAutoExpandLevel(int level
) {
554 fAutoExpandLevel
= level
;
558 * Returns the auto-expand level.
560 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
561 * the tree are expanded automatically
562 * @see #setAutoExpandLevel
564 public int getAutoExpandLevel() {
565 return fAutoExpandLevel
;
569 * Get the expanded state of a given entry.
573 * @return true if the entry is expanded, false if collapsed
576 public boolean getExpandedState(ITimeGraphEntry entry
) {
577 Item item
= fItemData
.fItemMap
.get(entry
);
578 return (item
!= null ? item
.fExpanded
: false);
582 * Set the expanded state of a given entry
587 * True if expanded, false if collapsed
589 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
590 Item item
= fItemData
.findItem(entry
);
591 if (item
!= null && item
.fExpanded
!= expanded
) {
592 item
.fExpanded
= expanded
;
593 fItemData
.updateExpandedItems();
599 * Set the expanded state of a given entry to certain relative level.
600 * It will call fireTreeEvent() for each changed entry. At the end
601 * it will call redraw().
606 * level to expand to or negative for all levels
608 * True if expanded, false if collapsed
610 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
611 setExpandedStateInt(entry
, level
, expanded
);
616 * Set the expanded state of a given entry and its children to the first
617 * level that has one collapsed entry.
622 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
623 int level
= findExpandedLevel(entry
);
625 setExpandedStateInt(entry
, level
, true);
631 * Inner class for finding relative level with at least one
634 private class SearchNode
{
635 SearchNode(ITimeGraphEntry e
, int l
) {
639 ITimeGraphEntry entry
;
644 * Finds the relative level with at least one collapsed entry.
648 * @return the found level or -1 if all levels are already expanded.
650 private int findExpandedLevel(ITimeGraphEntry entry
) {
651 Queue
<SearchNode
> queue
= new LinkedList
<>();
652 SearchNode root
= new SearchNode(entry
, 0);
653 SearchNode node
= root
;
656 while (!queue
.isEmpty()) {
657 node
= queue
.remove();
658 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
661 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
662 if (e
.hasChildren()) {
663 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
672 * Set the expanded state of a given entry to certain relative level.
673 * It will call fireTreeEvent() for each changed entry. No redraw is done.
678 * level to expand to or negative for all levels
680 * True if expanded, false if collapsed
682 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
684 if ((level
> 0) || (level
< 0)) {
686 if (entry
.hasChildren()) {
687 for (ITimeGraphEntry e
: entry
.getChildren()) {
688 setExpandedStateInt(e
, level
, expanded
);
692 Item item
= fItemData
.findItem(entry
);
693 if (item
!= null && item
.fExpanded
!= expanded
) {
694 item
.fExpanded
= expanded
;
695 fItemData
.updateExpandedItems();
696 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
701 * Collapses all nodes of the viewer's tree, starting with the root.
703 public void collapseAll() {
704 for (Item item
: fItemData
.fItems
) {
705 item
.fExpanded
= false;
707 fItemData
.updateExpandedItems();
712 * Expands all nodes of the viewer's tree, starting with the root.
714 public void expandAll() {
715 for (Item item
: fItemData
.fItems
) {
716 item
.fExpanded
= true;
718 fItemData
.updateExpandedItems();
723 * Add a tree listener
726 * The listener to add
728 public void addTreeListener(ITimeGraphTreeListener listener
) {
729 if (!fTreeListeners
.contains(listener
)) {
730 fTreeListeners
.add(listener
);
735 * Remove a tree listener
738 * The listener to remove
740 public void removeTreeListener(ITimeGraphTreeListener listener
) {
741 if (fTreeListeners
.contains(listener
)) {
742 fTreeListeners
.remove(listener
);
747 * Tree event callback
752 * The expanded state (true for expanded, false for collapsed)
754 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
755 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
756 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
758 listener
.treeExpanded(event
);
760 listener
.treeCollapsed(event
);
766 * Add a menu listener on {@link ITimeGraphEntry}s
769 * The listener to add
771 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
772 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
773 fTimeGraphEntryMenuListeners
.add(listener
);
778 * Remove a menu listener on {@link ITimeGraphEntry}s
781 * The listener to remove
783 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
784 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
785 fTimeGraphEntryMenuListeners
.remove(listener
);
790 * Menu event callback on {@link ITimeGraphEntry}s
793 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
795 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
796 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
797 listener
.menuDetected(event
);
802 * Add a menu listener on {@link ITimeEvent}s
805 * The listener to add
807 public void addTimeEventMenuListener(MenuDetectListener listener
) {
808 if (!fTimeEventMenuListeners
.contains(listener
)) {
809 fTimeEventMenuListeners
.add(listener
);
814 * Remove a menu listener on {@link ITimeEvent}s
817 * The listener to remove
819 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
820 if (fTimeEventMenuListeners
.contains(listener
)) {
821 fTimeEventMenuListeners
.remove(listener
);
826 * Menu event callback on {@link ITimeEvent}s
829 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
831 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
832 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
833 listener
.menuDetected(event
);
838 public boolean setFocus() {
839 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
840 fHasNamespaceFocus
= true;
842 return super.setFocus();
846 public ISelection
getSelection() {
847 TimeGraphSelection sel
= new TimeGraphSelection();
848 ITimeGraphEntry trace
= getSelectedTrace();
849 if (null != trace
&& null != fTimeProvider
) {
850 long selectedTime
= fTimeProvider
.getSelectionBegin();
851 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
861 * Get the selection object
863 * @return The selection
865 public ISelection
getSelectionTrace() {
866 TimeGraphSelection sel
= new TimeGraphSelection();
867 ITimeGraphEntry trace
= getSelectedTrace();
875 * Enable/disable one of the traces in the model
878 * 1 to enable it, -1 to disable. The method returns immediately
879 * if another value is used.
881 public void selectTrace(int n
) {
882 if ((n
!= 1) && (n
!= -1)) {
886 boolean changed
= false;
887 int lastSelection
= -1;
888 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
889 Item item
= fItemData
.fExpandedItems
[i
];
890 if (item
.fSelected
) {
892 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
893 item
.fSelected
= false;
894 item
= fItemData
.fExpandedItems
[i
+ 1];
895 item
.fSelected
= true;
897 } else if ((-1 == n
) && (i
> 0)) {
898 item
.fSelected
= false;
899 item
= fItemData
.fExpandedItems
[i
- 1];
900 item
.fSelected
= true;
907 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
908 Item item
= fItemData
.fExpandedItems
[0];
909 item
.fSelected
= true;
914 ensureVisibleItem(-1, false);
916 fireSelectionChanged();
924 * 1 for next event, -1 for previous event
926 * true to extend selection range, false for single selection
929 public void selectEvent(int n
, boolean extend
) {
930 if (null == fTimeProvider
) {
933 ITimeGraphEntry trace
= getSelectedTrace();
937 long selectedTime
= fTimeProvider
.getSelectionEnd();
938 long endTime
= fTimeProvider
.getMaxTime();
939 ITimeEvent nextEvent
;
940 if (n
== -1 && selectedTime
> endTime
) {
941 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
943 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
945 if (null == nextEvent
&& n
== -1) {
946 nextEvent
= Utils
.getFirstEvent(trace
);
948 if (null != nextEvent
) {
949 long nextTime
= nextEvent
.getTime();
950 // If last event detected e.g. going back or not moving to a next
952 if (nextTime
<= selectedTime
&& n
== 1) {
953 // Select to the end of this last event
954 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
955 // but not beyond the end of the trace
956 if (nextTime
> endTime
) {
959 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
960 // for previous event go to its end time unless we were already there
961 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
964 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
966 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
968 fireSelectionChanged();
971 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
973 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
975 fireSelectionChanged();
977 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
981 * Select the next event
984 * true to extend selection range, false for single selection
987 public void selectNextEvent(boolean extend
) {
988 selectEvent(1, extend
);
989 // Notify if visible time window has been adjusted
990 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
994 * Select the previous event
997 * true to extend selection range, false for single selection
1000 public void selectPrevEvent(boolean extend
) {
1001 selectEvent(-1, extend
);
1002 // Notify if visible time window has been adjusted
1003 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1007 * Select the next trace
1009 public void selectNextTrace() {
1014 * Select the previous trace
1016 public void selectPrevTrace() {
1021 * Scroll left or right by one half window size
1024 * true to scroll left, false to scroll right
1026 public void horizontalScroll(boolean left
) {
1027 long time0
= fTimeProvider
.getTime0();
1028 long time1
= fTimeProvider
.getTime1();
1029 long timeMin
= fTimeProvider
.getMinTime();
1030 long timeMax
= fTimeProvider
.getMaxTime();
1031 long range
= time1
- time0
;
1035 long increment
= Math
.max(1, range
/ 2);
1037 time0
= Math
.max(time0
- increment
, timeMin
);
1038 time1
= time0
+ range
;
1040 time1
= Math
.min(time1
+ increment
, timeMax
);
1041 time0
= time1
- range
;
1043 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1047 * Zoom based on mouse cursor location with mouse scrolling
1049 * @param zoomIn true to zoom in, false to zoom out
1051 public void zoom(boolean zoomIn
) {
1052 int globalX
= getDisplay().getCursorLocation().x
;
1053 Point p
= toControl(globalX
, 0);
1054 int nameSpace
= fTimeProvider
.getNameSpace();
1055 int timeSpace
= fTimeProvider
.getTimeSpace();
1056 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1057 long time0
= fTimeProvider
.getTime0();
1058 long time1
= fTimeProvider
.getTime1();
1059 long interval
= time1
- time0
;
1060 if (interval
== 0) {
1062 } // to allow getting out of single point interval
1065 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1067 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1069 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1070 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1071 long newTime1
= newTime0
+ newInterval
;
1072 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1076 * zoom in using single click
1078 public void zoomIn() {
1079 long prevTime0
= fTimeProvider
.getTime0();
1080 long prevTime1
= fTimeProvider
.getTime1();
1081 long prevRange
= prevTime1
- prevTime0
;
1082 if (prevRange
== 0) {
1085 ITimeDataProvider provider
= fTimeProvider
;
1086 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1087 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1088 selTime
= (prevTime0
+ prevTime1
) / 2;
1090 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1091 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1093 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1095 if (inaccuracy
> 0 && inaccuracy
< 100) {
1096 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1100 long min
= fTimeProvider
.getMinTimeInterval();
1101 if ((time1
- time0
) < min
) {
1102 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1103 time1
= time0
+ min
;
1106 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1110 * zoom out using single click
1112 public void zoomOut() {
1113 long prevTime0
= fTimeProvider
.getTime0();
1114 long prevTime1
= fTimeProvider
.getTime1();
1115 ITimeDataProvider provider
= fTimeProvider
;
1116 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1117 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1118 selTime
= (prevTime0
+ prevTime1
) / 2;
1120 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1121 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1123 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1124 if (inaccuracy
> 0 && inaccuracy
< 100) {
1125 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1129 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1136 * true to zoom in, false to zoom out
1139 public void verticalZoom(boolean zoomIn
) {
1141 fHeightAdjustment
++;
1143 fHeightAdjustment
--;
1145 fItemData
.refreshData();
1150 * Reset the vertical zoom to default.
1154 public void resetVerticalZoom() {
1155 fHeightAdjustment
= 0;
1156 fItemData
.refreshData();
1161 * Set the grid lines visibility. The default is true.
1164 * true to show the grid lines, false otherwise
1167 public void setGridLinesVisible(boolean visible
) {
1168 fGridLinesVisible
= visible
;
1172 * Get the grid lines visibility.
1174 * @return true if the grid lines are visible, false otherwise
1177 public boolean getGridLinesVisible() {
1178 return fGridLinesVisible
;
1182 * Set the grid line color. The default is SWT.COLOR_GRAY.
1185 * the grid line color
1188 public void setGridLineColor(Color color
) {
1189 fGridLineColor
= color
;
1193 * Get the grid line color.
1195 * @return the grid line color
1198 public Color
getGridLineColor() {
1199 return fGridLineColor
;
1203 * Set the markers list.
1206 * The markers list, or null
1209 public void setMarkers(List
<IMarkerEvent
> markers
) {
1214 * Get the markers list.
1216 * @return The markers list, or null
1219 public List
<IMarkerEvent
> getMarkers() {
1224 * Set the markers visibility. The default is true.
1227 * true to show the markers, false otherwise
1230 public void setMarkersVisible(boolean visible
) {
1231 fMarkersVisible
= visible
;
1235 * Get the markers visibility.
1237 * @return true if the markers are visible, false otherwise
1240 public boolean getMarkersVisible() {
1241 return fMarkersVisible
;
1247 * @param hideArrows true to hide arrows
1249 public void hideArrows(boolean hideArrows
) {
1250 fHideArrows
= hideArrows
;
1254 * Follow the arrow forward
1257 * true to extend selection range, false for single selection
1260 public void followArrowFwd(boolean extend
) {
1261 ITimeGraphEntry trace
= getSelectedTrace();
1262 if (trace
== null) {
1265 long selectedTime
= fTimeProvider
.getSelectionEnd();
1266 for (ILinkEvent link
: fItemData
.fLinks
) {
1267 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1268 selectItem(link
.getDestinationEntry(), false);
1269 if (link
.getDuration() != 0) {
1271 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1273 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1275 // Notify if visible time window has been adjusted
1276 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1278 fireSelectionChanged();
1279 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1283 selectNextEvent(extend
);
1287 * Follow the arrow backward
1290 * true to extend selection range, false for single selection
1293 public void followArrowBwd(boolean extend
) {
1294 ITimeGraphEntry trace
= getSelectedTrace();
1295 if (trace
== null) {
1298 long selectedTime
= fTimeProvider
.getSelectionEnd();
1299 for (ILinkEvent link
: fItemData
.fLinks
) {
1300 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1301 selectItem(link
.getEntry(), false);
1302 if (link
.getDuration() != 0) {
1304 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1306 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1308 // Notify if visible time window has been adjusted
1309 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1311 fireSelectionChanged();
1312 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1316 selectPrevEvent(extend
);
1320 * Return the currently selected trace
1322 * @return The entry matching the trace
1324 public ITimeGraphEntry
getSelectedTrace() {
1325 ITimeGraphEntry trace
= null;
1326 int idx
= getSelectedIndex();
1328 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1334 * Retrieve the index of the currently selected item
1338 public int getSelectedIndex() {
1340 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1341 Item item
= fItemData
.fExpandedItems
[i
];
1342 if (item
.fSelected
) {
1350 boolean toggle(int idx
) {
1351 boolean toggled
= false;
1352 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1353 Item item
= fItemData
.fExpandedItems
[idx
];
1354 if (item
.fHasChildren
) {
1355 item
.fExpanded
= !item
.fExpanded
;
1356 fItemData
.updateExpandedItems();
1359 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1366 * Gets the index of the item at the given location.
1370 * @return the index of the item at the given location, of -1 if none.
1372 protected int getItemIndexAtY(int y
) {
1375 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1376 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1382 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1383 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1392 boolean isOverSplitLine(int x
) {
1393 if (x
< 0 || null == fTimeProvider
) {
1396 int nameWidth
= fTimeProvider
.getNameSpace();
1397 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1401 * Gets the {@link ITimeGraphEntry} at the given location.
1404 * a point in the widget
1405 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1409 public ITimeGraphEntry
getEntry(Point pt
) {
1410 int idx
= getItemIndexAtY(pt
.y
);
1411 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1415 * Return the arrow event closest to the given point that is no further than
1416 * a maximum distance.
1419 * a point in the widget
1420 * @return The closest arrow event, or null if there is none close enough.
1422 protected ILinkEvent
getArrow(Point pt
) {
1426 ILinkEvent linkEvent
= null;
1427 double minDistance
= Double
.MAX_VALUE
;
1428 for (ILinkEvent event
: fItemData
.fLinks
) {
1429 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1433 int x2
= x1
+ rect
.width
;
1434 int y2
= y1
+ rect
.height
;
1435 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1436 if (minDistance
> d
) {
1442 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1449 public int getXForTime(long time
) {
1450 if (null == fTimeProvider
) {
1453 long time0
= fTimeProvider
.getTime0();
1454 long time1
= fTimeProvider
.getTime1();
1455 int width
= getSize().x
;
1456 int nameSpace
= fTimeProvider
.getNameSpace();
1457 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1458 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1463 public long getTimeAtX(int coord
) {
1464 if (null == fTimeProvider
) {
1468 Point size
= getSize();
1469 long time0
= fTimeProvider
.getTime0();
1470 long time1
= fTimeProvider
.getTime1();
1471 int nameWidth
= fTimeProvider
.getNameSpace();
1472 final int x
= coord
- nameWidth
;
1473 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1474 if (x
>= 0 && size
.x
>= nameWidth
) {
1475 if (time1
- time0
> timeWidth
) {
1476 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1477 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1479 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1480 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1486 void selectItem(int idx
, boolean addSelection
) {
1487 boolean changed
= false;
1489 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1490 Item item
= fItemData
.fExpandedItems
[idx
];
1491 changed
= !item
.fSelected
;
1492 item
.fSelected
= true;
1495 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1496 Item item
= fItemData
.fExpandedItems
[i
];
1497 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1500 item
.fSelected
= i
== idx
;
1503 changed
|= ensureVisibleItem(idx
, true);
1510 * Callback for item selection
1513 * The entry matching the trace
1514 * @param addSelection
1515 * If the selection is added or removed
1517 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1518 int idx
= fItemData
.findItemIndex(trace
);
1519 selectItem(idx
, addSelection
);
1523 * Retrieve the number of entries shown per page.
1527 public int countPerPage() {
1528 int height
= getSize().y
;
1531 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1532 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1533 if (ySum
>= height
) {
1538 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1539 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1540 if (ySum
>= height
) {
1549 * Get the index of the top element
1553 public int getTopIndex() {
1558 * Get the number of expanded (visible) items
1560 * @return The count of expanded (visible) items
1562 public int getExpandedElementCount() {
1563 return fItemData
.fExpandedItems
.length
;
1567 * Get an array of all expanded (visible) elements
1569 * @return The expanded (visible) elements
1571 public ITimeGraphEntry
[] getExpandedElements() {
1572 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1573 for (Item item
: fItemData
.fExpandedItems
) {
1574 elements
.add(item
.fEntry
);
1576 return elements
.toArray(new ITimeGraphEntry
[0]);
1580 * Get the expanded (visible) element at the specified index.
1584 * @return The expanded (visible) element or null if out of range
1587 public ITimeGraphEntry
getExpandedElement(int index
) {
1588 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1591 return fItemData
.fExpandedItems
[index
].fEntry
;
1594 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1595 Rectangle rect
= getItemRect(bounds
, idx
);
1596 rect
.width
= nameWidth
;
1600 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1601 Rectangle rect
= getItemRect(bounds
, idx
);
1602 rect
.x
+= nameWidth
;
1603 rect
.width
-= nameWidth
;
1607 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1609 if (idx
>= fTopIndex
) {
1610 for (int i
= fTopIndex
; i
< idx
; i
++) {
1611 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1614 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1615 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1618 int y
= bounds
.y
+ ySum
;
1619 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1620 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1624 void paint(Rectangle bounds
, PaintEvent e
) {
1627 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1631 fIdealNameSpace
= 0;
1632 int nameSpace
= fTimeProvider
.getNameSpace();
1634 // draw the background layer
1635 drawBackground(bounds
, nameSpace
, gc
);
1637 // draw the grid lines
1638 drawGridLines(bounds
, gc
);
1640 // draw the background markers
1641 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1644 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1646 // draw the foreground markers
1647 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1649 // draw the links (arrows)
1650 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1652 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1654 int alpha
= gc
.getAlpha();
1657 long time0
= fTimeProvider
.getTime0();
1658 long time1
= fTimeProvider
.getTime1();
1659 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1660 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1661 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1662 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1663 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1665 // draw selection lines
1666 if (fDragState
!= DRAG_SELECTION
) {
1667 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1668 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1669 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1672 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1673 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1678 // draw selection background
1679 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1680 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1681 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1682 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1684 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1685 } else if (x0
- x1
> 1) {
1686 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1690 // draw drag selection background
1691 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1692 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1693 if (fDragX0
< fDragX
) {
1694 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1695 } else if (fDragX0
> fDragX
) {
1696 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1701 if (DRAG_SPLIT_LINE
== fDragState
) {
1702 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1703 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1704 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1705 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1706 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1707 if (fDragX
!= fDragX0
) {
1708 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1710 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1711 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1712 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1713 if (fDragX
!= fDragX0
) {
1714 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1716 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1717 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1718 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1725 * Draw the background layer. Fills the background of the control's name
1726 * space and states space, updates the background of items if necessary,
1727 * and draws the item's name text and middle line.
1730 * The bounds of the control
1732 * The name space width
1737 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1738 // draw empty name space background
1739 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1740 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1742 // draw empty states space background
1743 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1744 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1746 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1747 Rectangle itemRect
= getItemRect(bounds
, i
);
1748 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1751 Item item
= fItemData
.fExpandedItems
[i
];
1752 // draw the background of selected item and items with no time events
1753 if (! item
.fEntry
.hasTimeEvents()) {
1754 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1755 gc
.fillRectangle(itemRect
);
1756 } else if (item
.fSelected
) {
1757 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1758 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1759 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1760 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1762 // draw the name and middle line
1763 if (! item
.fEntry
.hasTimeEvents()) {
1764 drawName(item
, itemRect
, gc
);
1766 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1767 drawName(item
, nameRect
, gc
);
1768 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1769 drawMidLine(rect
, gc
);
1775 * Draw the grid lines
1778 * The bounds of the control
1783 public void drawGridLines(Rectangle bounds
, GC gc
) {
1784 if (!fGridLinesVisible
) {
1787 gc
.setForeground(fGridLineColor
);
1788 gc
.setAlpha(fGridLineColor
.getAlpha());
1789 for (int x
: fTimeGraphScale
.getTickList()) {
1790 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1799 * The rectangle of the area
1800 * @param timeProvider
1803 * The list of markers
1805 * true to draw the foreground markers, false otherwise
1807 * The width reserved for the names
1809 * Reference to the SWT GC object
1812 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1813 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1816 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1817 /* the list can grow concurrently but cannot shrink */
1818 for (int i
= 0; i
< markers
.size(); i
++) {
1819 IMarkerEvent marker
= markers
.get(i
);
1820 if (marker
.isForeground() == foreground
) {
1821 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1824 gc
.setClipping((Rectangle
) null);
1828 * Draw a single marker
1833 * The bounds of the control
1834 * @param timeProvider
1837 * The width reserved for the name
1839 * Reference to the SWT GC object
1842 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1843 Rectangle rect
= Utils
.clone(bounds
);
1844 if (marker
.getEntry() != null) {
1845 int index
= fItemData
.findItemIndex(marker
.getEntry());
1849 rect
= getStatesRect(bounds
, index
, nameSpace
);
1850 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1854 int x0
= getXForTime(marker
.getTime());
1855 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1856 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1859 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1860 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1862 Color color
= getColorScheme().getColor(marker
.getColor());
1863 gc
.setBackground(color
);
1864 gc
.setAlpha(color
.getAlpha());
1865 gc
.fillRectangle(rect
);
1867 String label
= marker
.getLabel();
1868 if (label
!= null && marker
.getEntry() != null) {
1869 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1870 gc
.setForeground(color
);
1871 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1876 * Draw many items at once
1879 * The bounds of the control
1880 * @param timeProvider
1883 * The array items to draw
1885 * The index of the first element to draw
1887 * The name space width
1891 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1892 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1893 for (int i
= topIndex
; i
< items
.length
; i
++) {
1894 Item item
= items
[i
];
1895 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1905 * The bounds of the control
1906 * @param timeProvider
1909 * The expanded item index
1911 * The name space width
1915 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1916 Rectangle itemRect
= getItemRect(bounds
, i
);
1917 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1921 ITimeGraphEntry entry
= item
.fEntry
;
1922 long time0
= timeProvider
.getTime0();
1923 long time1
= timeProvider
.getTime1();
1924 long selectedTime
= fTimeProvider
.getSelectionEnd();
1926 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1927 if (rect
.isEmpty() || (time1
<= time0
)) {
1928 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1932 boolean selected
= item
.fSelected
;
1933 // K pixels per second
1934 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1936 if (item
.fEntry
.hasTimeEvents()) {
1937 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1938 fillSpace(rect
, gc
, selected
);
1940 int margins
= getMarginForHeight(rect
.height
);
1941 int height
= rect
.height
- margins
;
1942 int topMargin
= (margins
+ 1) / 2;
1943 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1945 /* Set the font for this item */
1946 setFontForHeight(height
, gc
);
1948 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1949 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1952 while (iterator
.hasNext()) {
1953 ITimeEvent event
= iterator
.next();
1954 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1955 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1956 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1957 // event is out of bounds
1960 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1961 stateRect
.x
= Math
.max(rect
.x
, x
);
1962 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1963 if (stateRect
.x
== lastX
) {
1964 stateRect
.width
-= 1;
1965 if (stateRect
.width
> 0) {
1966 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1967 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1971 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1972 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1973 lastX
= stateRect
.x
;
1976 gc
.setClipping((Rectangle
) null);
1978 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1985 * The bounds of the control
1986 * @param timeProvider
1989 * The list of link events
1991 * The name space width
1995 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1996 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2000 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2001 /* the list can grow concurrently but cannot shrink */
2002 for (int i
= 0; i
< links
.size(); i
++) {
2003 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2005 gc
.setClipping((Rectangle
) null);
2009 * Draws a link type event
2012 * The link event to draw
2014 * The bounds of the control
2015 * @param timeProvider
2018 * The name space width
2022 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2023 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2026 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2027 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2028 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2030 if ((srcIndex
== -1) || (destIndex
== -1)) {
2034 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2035 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2037 int x0
= getXForTime(event
.getTime());
2038 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2040 // limit the x-coordinates to prevent integer overflow in calculations
2041 // and also GC.drawLine doesn't draw properly with large coordinates
2042 final int limit
= Integer
.MAX_VALUE
/ 1024;
2043 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2044 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2046 int y0
= src
.y
+ src
.height
/ 2;
2047 int y1
= dst
.y
+ dst
.height
/ 2;
2048 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2057 * Time event for which we're drawing the arrow
2059 * The arrow rectangle
2062 * @return true if the arrow was drawn
2064 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2065 Rectangle rect
, GC gc
) {
2070 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2074 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2077 Color stateColor
= null;
2078 if (colorIdx
< fEventColorMap
.length
) {
2079 stateColor
= fEventColorMap
[colorIdx
];
2081 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2084 gc
.setForeground(stateColor
);
2085 gc
.setBackground(stateColor
);
2087 /* Draw the arrow */
2088 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2089 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2092 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2097 * @author Francis Giraldeau
2100 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2102 * The algorithm was taken from this site, not the code itself
2104 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2107 double cos
= 0.9510;
2108 double sin
= 0.3090;
2109 long lenx
= x1
- x0
;
2110 long leny
= y1
- y0
;
2111 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2113 double dx
= factor
* lenx
/ len
;
2114 double dy
= factor
* leny
/ len
;
2115 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2116 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2117 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2118 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2119 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2120 gc
.fillPolygon(arrow
);
2124 * Draw the name of an item.
2129 * The bounds of the item's name space
2133 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2134 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2136 // No name to be drawn
2137 if (fTimeProvider
.getNameSpace() == 0) {
2141 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2142 setFontForHeight(height
, gc
);
2144 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2145 if (item
.fHasChildren
) {
2146 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2147 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2148 Rectangle rect
= Utils
.clone(bounds
);
2149 rect
.x
+= leftMargin
;
2150 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2151 rect
.width
= EXPAND_SIZE
;
2152 rect
.height
= EXPAND_SIZE
;
2153 gc
.fillRectangle(rect
);
2154 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2155 int midy
= rect
.y
+ rect
.height
/ 2;
2156 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2157 if (!item
.fExpanded
) {
2158 int midx
= rect
.x
+ rect
.width
/ 2;
2159 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2162 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2164 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2167 int imgHeight
= img
.getImageData().height
;
2168 int imgWidth
= img
.getImageData().width
;
2170 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2171 gc
.drawImage(img
, x
, y
);
2172 leftMargin
+= imgWidth
+ MARGIN
;
2174 String name
= item
.fName
;
2175 Point size
= gc
.stringExtent(name
);
2176 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2177 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2179 if (hasTimeEvents
) {
2180 // cut long string with "..."
2181 int width
= bounds
.width
- leftMargin
;
2183 while (size
.x
> width
&& name
.length() > 1) {
2185 name
= name
.substring(0, name
.length() - 1);
2186 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2189 name
+= "..."; //$NON-NLS-1$
2192 Rectangle rect
= Utils
.clone(bounds
);
2193 rect
.x
+= leftMargin
;
2194 rect
.width
-= leftMargin
;
2196 if (rect
.width
> 0) {
2197 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2198 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2199 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2200 leftMargin
+= textWidth
+ MARGIN
;
2202 if (hasTimeEvents
) {
2204 rect
.x
= bounds
.x
+ leftMargin
;
2206 rect
.width
= bounds
.width
- rect
.x
;
2207 drawMidLine(rect
, gc
);
2213 * Draw the state (color fill)
2218 * Time event for which we're drawing the state
2220 * The state rectangle
2224 * Is this time event currently selected (so it appears
2226 * @param timeSelected
2227 * Is the timestamp currently selected
2228 * @return true if the state was drawn
2230 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2231 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2233 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2234 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2237 boolean visible
= rect
.width
== 0 ?
false : true;
2238 rect
.width
= Math
.max(1, rect
.width
);
2239 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2240 gc
.setForeground(black
);
2242 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2244 // Only draw the top and bottom borders
2245 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2246 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2247 if (rect
.width
== 1) {
2248 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2251 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2254 Color stateColor
= null;
2255 if (colorIdx
< fEventColorMap
.length
) {
2256 stateColor
= fEventColorMap
[colorIdx
];
2261 boolean reallySelected
= timeSelected
&& selected
;
2262 // fill all rect area
2263 gc
.setBackground(stateColor
);
2265 gc
.fillRectangle(rect
);
2266 } else if (fBlendSubPixelEvents
) {
2268 gc
.fillRectangle(rect
);
2272 if (reallySelected
) {
2273 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2274 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2277 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2279 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2284 * Fill an item's states rectangle
2287 * The states rectangle
2291 * true if the item is selected
2293 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2294 /* Nothing to draw */
2298 * Draw a line at the middle height of a rectangle
2305 private void drawMidLine(Rectangle rect
, GC gc
) {
2306 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2307 int midy
= rect
.y
+ rect
.height
/ 2;
2308 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2311 private static int getMarginForHeight(int height
) {
2313 * State rectangle is smaller than the item bounds when height is > 4.
2314 * Don't use any margin if the height is below or equal that threshold.
2315 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2316 * pixels for the state height, but with a minimum margin of 1.
2318 final int MARGIN_THRESHOLD
= 4;
2319 final int PREFERRED_HEIGHT
= 13;
2320 final int MIN_MARGIN
= 1;
2321 final int MAX_MARGIN
= 6;
2322 return height
<= MARGIN_THRESHOLD ?
0 :
2323 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2326 private void setFontForHeight(int pixels
, GC gc
) {
2327 /* convert font height from pixels to points */
2328 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2329 Font font
= fFonts
.get(height
);
2331 FontData fontData
= gc
.getFont().getFontData()[0];
2332 fontData
.setHeight(height
);
2333 font
= new Font(gc
.getDevice(), fontData
);
2334 fFonts
.put(height
, font
);
2340 public void keyTraversed(TraverseEvent e
) {
2341 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2347 public void keyPressed(KeyEvent e
) {
2349 if (fItemData
.fExpandedItems
.length
== 0) {
2352 if (SWT
.HOME
== e
.keyCode
) {
2354 } else if (SWT
.END
== e
.keyCode
) {
2355 idx
= fItemData
.fExpandedItems
.length
- 1;
2356 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2357 idx
= getSelectedIndex();
2360 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2363 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2364 idx
= getSelectedIndex();
2367 } else if (idx
> 0) {
2370 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2371 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2372 selectPrevEvent(extend
);
2373 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2374 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2375 selectNextEvent(extend
);
2376 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2377 int page
= countPerPage();
2378 idx
= getSelectedIndex();
2383 if (idx
>= fItemData
.fExpandedItems
.length
) {
2384 idx
= fItemData
.fExpandedItems
.length
- 1;
2386 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2387 int page
= countPerPage();
2388 idx
= getSelectedIndex();
2396 } else if (SWT
.CR
== e
.keyCode
) {
2397 idx
= getSelectedIndex();
2399 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2402 fireDefaultSelection();
2406 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2407 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2409 if (fVerticalZoomAlignEntry
!= null) {
2410 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2412 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2413 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2414 verticalZoom(false);
2415 if (fVerticalZoomAlignEntry
!= null) {
2416 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2418 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2419 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2420 resetVerticalZoom();
2421 if (fVerticalZoomAlignEntry
!= null) {
2422 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2424 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2425 if (fHasNamespaceFocus
) {
2426 ITimeGraphEntry entry
= getSelectedTrace();
2427 setExpandedState(entry
, 0, true);
2431 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2432 if (fHasNamespaceFocus
) {
2433 ITimeGraphEntry entry
= getSelectedTrace();
2434 if ((entry
!= null) && entry
.hasChildren()) {
2435 setExpandedState(entry
, -1, false);
2440 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2441 if (fHasNamespaceFocus
) {
2442 ITimeGraphEntry entry
= getSelectedTrace();
2443 if ((entry
!= null) && entry
.hasChildren()) {
2444 setExpandedStateLevel(entry
);
2449 selectItem(idx
, false);
2450 fireSelectionChanged();
2452 int x
= toControl(e
.display
.getCursorLocation()).x
;
2453 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2457 public void keyReleased(KeyEvent e
) {
2458 int x
= toControl(e
.display
.getCursorLocation()).x
;
2459 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2463 public void focusGained(FocusEvent e
) {
2466 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2470 public void focusLost(FocusEvent e
) {
2472 if (DRAG_NONE
!= fDragState
) {
2474 fDragState
= DRAG_NONE
;
2477 updateStatusLine(NO_STATUS
);
2481 * @return If the current view is focused
2483 public boolean isInFocus() {
2488 * Provide the possibility to control the wait cursor externally e.g. data
2489 * requests in progress
2491 * @param waitInd Should we wait indefinitely?
2493 public void waitCursor(boolean waitInd
) {
2494 // Update cursor as indicated
2496 setCursor(fWaitCursor
);
2502 private void updateCursor(int x
, int stateMask
) {
2503 // if Wait cursor not active, check for the need to change the cursor
2504 if (getCursor() == fWaitCursor
) {
2507 Cursor cursor
= null;
2508 if (fDragState
== DRAG_SPLIT_LINE
) {
2509 } else if (fDragState
== DRAG_SELECTION
) {
2510 cursor
= fResizeCursor
;
2511 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2512 cursor
= fDragCursor
;
2513 } else if (fDragState
== DRAG_ZOOM
) {
2514 cursor
= fZoomCursor
;
2515 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2516 cursor
= fDragCursor
;
2517 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2518 cursor
= fResizeCursor
;
2519 } else if (!isOverSplitLine(x
)) {
2520 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2521 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2522 int xBegin
= getXForTime(selectionBegin
);
2523 int xEnd
= getXForTime(selectionEnd
);
2524 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2525 cursor
= fResizeCursor
;
2528 if (getCursor() != cursor
) {
2534 * Update the status line following a change of selection.
2538 public void updateStatusLine() {
2539 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2542 private void updateStatusLine(int x
) {
2543 // use the time provider of the time graph scale for the status line
2544 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2545 if (fStatusLineManager
== null || null == tdp
||
2546 tdp
.getTime0() == tdp
.getTime1()) {
2549 TimeFormat tf
= tdp
.getTimeFormat();
2550 Resolution res
= Resolution
.NANOSEC
;
2551 StringBuilder message
= new StringBuilder();
2552 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2553 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2554 long time
= getTimeAtX(x
);
2556 if (tdp
instanceof ITimeDataProviderConverter
) {
2557 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2559 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2561 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2562 Utils
.formatTime(time
, tf
, res
)
2566 long selectionBegin
= tdp
.getSelectionBegin();
2567 long selectionEnd
= tdp
.getSelectionEnd();
2568 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2570 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2571 Utils
.formatTime(selectionBegin
, tf
, res
)
2573 if (selectionBegin
!= selectionEnd
) {
2574 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2576 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2577 Utils
.formatTime(selectionEnd
, tf
, res
),
2578 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2581 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2582 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2583 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2584 if (tdp
instanceof ITimeDataProviderConverter
) {
2585 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2586 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2588 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2590 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2591 Utils
.formatTime(time0
, tf
, res
),
2592 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2593 Utils
.formatTime(time
, tf
, res
),
2594 Utils
.formatDelta(time
- time0
, tf
, res
)
2597 fStatusLineManager
.setMessage(message
.toString());
2601 public void mouseMove(MouseEvent e
) {
2602 if (null == fTimeProvider
) {
2605 Point size
= getSize();
2606 if (DRAG_TRACE_ITEM
== fDragState
) {
2607 int nameWidth
= fTimeProvider
.getNameSpace();
2608 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2610 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2611 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2612 long time1
= fTime1bak
- timeDelta
;
2613 long maxTime
= fTimeProvider
.getMaxTime();
2614 if (time1
> maxTime
) {
2617 long time0
= time1
- (fTime1bak
- fTime0bak
);
2618 if (time0
< fTimeProvider
.getMinTime()) {
2619 time0
= fTimeProvider
.getMinTime();
2620 time1
= time0
+ (fTime1bak
- fTime0bak
);
2622 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2624 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2626 fTimeProvider
.setNameSpace(e
.x
);
2627 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2628 } else if (DRAG_SELECTION
== fDragState
) {
2629 if (fDragBeginMarker
) {
2630 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2632 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2635 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2636 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2637 } else if (DRAG_ZOOM
== fDragState
) {
2638 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2640 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2641 } else if (DRAG_NONE
== fDragState
) {
2642 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2643 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2646 fMouseOverSplitLine
= mouseOverSplitLine
;
2649 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2650 fHasNamespaceFocus
= false;
2652 fHasNamespaceFocus
= true;
2654 updateCursor(e
.x
, e
.stateMask
);
2655 updateStatusLine(e
.x
);
2659 public void mouseDoubleClick(MouseEvent e
) {
2660 if (null == fTimeProvider
) {
2663 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2664 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2665 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2666 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2667 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2670 fMouseOverSplitLine
= mouseOverSplitLine
;
2671 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2674 int idx
= getItemIndexAtY(e
.y
);
2676 selectItem(idx
, false);
2677 fireDefaultSelection();
2683 public void mouseDown(MouseEvent e
) {
2684 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2685 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2686 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2690 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2691 int nameSpace
= fTimeProvider
.getNameSpace();
2692 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2693 fDragState
= DRAG_SPLIT_LINE
;
2694 fDragButton
= e
.button
;
2697 fTime0bak
= fTimeProvider
.getTime0();
2698 fTime1bak
= fTimeProvider
.getTime1();
2700 updateCursor(e
.x
, e
.stateMask
);
2704 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2705 int nameSpace
= fTimeProvider
.getNameSpace();
2706 idx
= getItemIndexAtY(e
.y
);
2708 Item item
= fItemData
.fExpandedItems
[idx
];
2709 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2713 selectItem(idx
, false);
2714 fireSelectionChanged();
2716 selectItem(idx
, false); // clear selection
2717 fireSelectionChanged();
2719 long hitTime
= getTimeAtX(e
.x
);
2723 fDragState
= DRAG_SELECTION
;
2724 fDragBeginMarker
= false;
2725 fDragButton
= e
.button
;
2728 fDragTime0
= getTimeAtX(fDragX0
);
2729 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2730 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2731 int xBegin
= getXForTime(selectionBegin
);
2732 int xEnd
= getXForTime(selectionEnd
);
2733 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2734 long time
= getTimeAtX(e
.x
);
2735 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2736 fDragBeginMarker
= true;
2739 fDragTime0
= selectionEnd
;
2742 fDragTime0
= selectionBegin
;
2745 long time
= getTimeAtX(e
.x
);
2746 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2747 fDragBeginMarker
= true;
2750 fDragTime0
= selectionEnd
;
2751 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2753 fDragTime0
= selectionBegin
;
2756 fTime0bak
= fTimeProvider
.getTime0();
2757 fTime1bak
= fTimeProvider
.getTime1();
2759 updateCursor(e
.x
, e
.stateMask
);
2760 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2762 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2763 long hitTime
= getTimeAtX(e
.x
);
2766 fDragState
= DRAG_TRACE_ITEM
;
2767 fDragButton
= e
.button
;
2770 fTime0bak
= fTimeProvider
.getTime0();
2771 fTime1bak
= fTimeProvider
.getTime1();
2772 updateCursor(e
.x
, e
.stateMask
);
2774 } else if (3 == e
.button
) {
2775 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2777 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2779 fDragTime0
= getTimeAtX(fDragX0
);
2780 fDragState
= DRAG_ZOOM
;
2781 fDragButton
= e
.button
;
2783 updateCursor(e
.x
, e
.stateMask
);
2784 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2786 idx
= getItemIndexAtY(e
.y
);
2787 selectItem(idx
, false);
2788 fireSelectionChanged();
2794 public void mouseUp(MouseEvent e
) {
2795 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2796 menuDetected(fPendingMenuDetectEvent
);
2798 if (DRAG_NONE
!= fDragState
) {
2800 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2801 if (fDragX
!= fDragX0
) {
2802 fTimeProvider
.notifyStartFinishTime();
2804 fDragState
= DRAG_NONE
;
2805 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2806 fDragState
= DRAG_NONE
;
2808 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2809 if (fDragX
== fDragX0
) { // click without selecting anything
2810 long time
= getTimeAtX(e
.x
);
2811 fTimeProvider
.setSelectedTimeNotify(time
, false);
2813 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2814 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2815 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2817 fDragState
= DRAG_NONE
;
2819 fTimeGraphScale
.setDragRange(-1, -1);
2820 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2821 int nameWidth
= fTimeProvider
.getNameSpace();
2822 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2823 long time0
= getTimeAtX(fDragX0
);
2824 long time1
= getTimeAtX(fDragX
);
2825 if (time0
< time1
) {
2826 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2828 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2833 fDragState
= DRAG_NONE
;
2834 fTimeGraphScale
.setDragRange(-1, -1);
2837 updateCursor(e
.x
, e
.stateMask
);
2838 updateStatusLine(e
.x
);
2842 public void mouseEnter(MouseEvent e
) {
2846 public void mouseExit(MouseEvent e
) {
2847 if (fMouseOverSplitLine
) {
2848 fMouseOverSplitLine
= false;
2851 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2855 public void mouseHover(MouseEvent e
) {
2859 public void mouseScrolled(MouseEvent e
) {
2860 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2865 * On some platforms the mouse scroll event is sent to the
2866 * control that has focus even if it is not under the cursor.
2867 * Handle the event only if over the time graph control.
2869 Point size
= getSize();
2870 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2871 if (!bounds
.contains(e
.x
, e
.y
)) {
2875 boolean horizontalZoom
= false;
2876 boolean horizontalScroll
= false;
2877 boolean verticalZoom
= false;
2878 boolean verticalScroll
= false;
2880 // over the time graph control
2881 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2882 verticalZoom
= true;
2883 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2884 // over the name space
2885 verticalScroll
= true;
2887 // over the state area
2888 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2889 // over the state area, CTRL pressed
2890 horizontalZoom
= true;
2891 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2892 // over the state area, SHIFT pressed
2893 horizontalScroll
= true;
2895 // over the state area, no modifier pressed
2896 verticalScroll
= true;
2900 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2901 verticalZoom(e
.count
> 0);
2902 if (fVerticalZoomAlignEntry
!= null) {
2903 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2905 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2907 } else if (horizontalScroll
) {
2908 horizontalScroll(e
.count
> 0);
2909 } else if (verticalScroll
){
2910 setTopIndex(getTopIndex() - e
.count
);
2915 * Get the vertical zoom alignment entry and position based on the current
2916 * selection. If there is no selection or if the selection is not visible,
2917 * return an alignment entry with a null time graph entry.
2919 * @return a map entry where the key is the selection's time graph entry and
2920 * the value is the center y-coordinate of that entry, or null
2922 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2923 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2924 if (alignEntry
!= null) {
2927 int index
= getSelectedIndex();
2928 if (index
== -1 || index
>= getExpandedElementCount()) {
2929 return new SimpleEntry
<>(null, 0);
2931 Rectangle bounds
= getClientArea();
2932 Rectangle itemRect
= getItemRect(bounds
, index
);
2933 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2934 /* selection is not visible */
2935 return new SimpleEntry
<>(null, 0);
2937 ITimeGraphEntry entry
= getExpandedElement(index
);
2938 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2939 return new SimpleEntry
<>(entry
, y
);
2943 * Get the vertical zoom alignment entry and position at the specified
2947 * the cursor y-coordinate
2948 * @return a map entry where the key is the time graph entry under the
2949 * cursor and the value is the cursor y-coordinate
2951 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2952 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2953 if (alignEntry
!= null) {
2956 int index
= getItemIndexAtY(y
);
2958 index
= getExpandedElementCount() - 1;
2960 ITimeGraphEntry entry
= getExpandedElement(index
);
2961 return new SimpleEntry
<>(entry
, y
);
2965 * Get the vertical zoom alignment entry and position if there is an ongoing
2966 * one and we are within the vertical zoom delay, or otherwise return null.
2968 * @return a map entry where the key is a time graph entry and the value is
2969 * a y-coordinate, or null
2971 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2972 long currentTimeMillis
= System
.currentTimeMillis();
2973 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2975 * If the vertical zoom is triggered repeatedly in a short amount of
2976 * time, use the initial event's entry and position.
2978 fVerticalZoomAlignTime
= currentTimeMillis
;
2979 return fVerticalZoomAlignEntry
;
2981 fVerticalZoomAlignTime
= currentTimeMillis
;
2986 public void handleEvent(Event event
) {
2987 if (event
.type
== SWT
.MouseWheel
) {
2988 // prevent horizontal scrolling when the mouse wheel is used to
2989 // scroll vertically or zoom
2995 public int getBorderWidth() {
2996 return fBorderWidth
;
3000 * Set the border width
3002 * @param borderWidth
3005 public void setBorderWidth(int borderWidth
) {
3006 this.fBorderWidth
= borderWidth
;
3010 * @return The current height of the header row
3012 public int getHeaderHeight() {
3013 return fHeaderHeight
;
3017 * Set the height of the header row
3019 * @param headerHeight
3022 public void setHeaderHeight(int headerHeight
) {
3023 this.fHeaderHeight
= headerHeight
;
3027 * @return The default height of regular item rows
3029 public int getItemHeight() {
3030 return fGlobalItemHeight
;
3034 * Set the default height of regular item rows.
3039 public void setItemHeight(int rowHeight
) {
3040 this.fGlobalItemHeight
= rowHeight
;
3041 for (Item item
: fItemData
.fItems
) {
3042 item
.fItemHeight
= rowHeight
;
3047 * Set the height of a specific item. Overrides the default item height.
3050 * A time graph entry
3053 * @return true if the height is successfully stored, false otherwise
3055 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3056 Item item
= fItemData
.findItem(entry
);
3058 item
.fItemHeight
= rowHeight
;
3065 * Set the minimum item width
3067 * @param width The minimum width
3069 public void setMinimumItemWidth(int width
) {
3070 this.fMinimumItemWidth
= width
;
3074 * @return The minimum item width
3076 public int getMinimumItemWidth() {
3077 return fMinimumItemWidth
;
3081 * Set whether all time events with a duration shorter than one pixel should
3082 * be blended in. If false, only the first such time event will be drawn and
3083 * the subsequent time events in the same pixel will be discarded. The
3084 * default value is false.
3087 * true if sub-pixel events should be blended, false otherwise.
3090 public void setBlendSubPixelEvents(boolean blend
) {
3091 fBlendSubPixelEvents
= blend
;
3095 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3096 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3097 fSelectionChangedListeners
.add(listener
);
3102 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3103 if (listener
!= null) {
3104 fSelectionChangedListeners
.remove(listener
);
3109 public void setSelection(ISelection selection
) {
3110 if (selection
instanceof TimeGraphSelection
) {
3111 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
3112 Object ob
= sel
.getFirstElement();
3113 if (ob
instanceof ITimeGraphEntry
) {
3114 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
3115 selectItem(trace
, false);
3122 * @param filter The filter object to be attached to the view
3124 public void addFilter(@NonNull ViewerFilter filter
) {
3125 if (!fFilters
.contains(filter
)) {
3126 fFilters
.add(filter
);
3131 * @param filter The filter object to be attached to the view
3133 public void removeFilter(@NonNull ViewerFilter filter
) {
3134 fFilters
.remove(filter
);
3138 * Returns this control's filters.
3140 * @return an array of viewer filters
3143 public @NonNull ViewerFilter
[] getFilters() {
3144 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3148 * Sets the filters, replacing any previous filters.
3151 * an array of viewer filters, or null
3154 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3156 if (filters
!= null) {
3157 fFilters
.addAll(Arrays
.asList(filters
));
3162 public void colorSettingsChanged(StateItem
[] stateItems
) {
3163 /* Destroy previous colors from the resource manager */
3164 if (fEventColorMap
!= null) {
3165 for (Color color
: fEventColorMap
) {
3166 fResourceManager
.destroyColor(color
.getRGB());
3169 if (stateItems
!= null) {
3170 fEventColorMap
= new Color
[stateItems
.length
];
3171 for (int i
= 0; i
< stateItems
.length
; i
++) {
3172 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3175 fEventColorMap
= new Color
[] { };
3180 private class ItemData
{
3181 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3182 private Item
[] fExpandedItems
= new Item
[0];
3183 private Item
[] fItems
= new Item
[0];
3184 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3185 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3190 public Item
findItem(ITimeGraphEntry entry
) {
3191 return fItemMap
.get(entry
);
3194 public int findItemIndex(ITimeGraphEntry entry
) {
3195 Item item
= fItemMap
.get(entry
);
3199 return item
.fExpandedIndex
;
3202 public void refreshData() {
3203 ITimeGraphEntry selection
= getSelectedTrace();
3204 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3205 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3206 ITimeGraphEntry entry
= fRootEntries
[i
];
3207 refreshData(itemMap
, null, 0, entry
);
3210 fItems
= fItemMap
.values().toArray(new Item
[0]);
3211 updateExpandedItems();
3212 if (selection
!= null) {
3213 for (Item item
: fExpandedItems
) {
3214 if (item
.fEntry
== selection
) {
3215 item
.fSelected
= true;
3222 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3223 Item item
= new Item(entry
, entry
.getName(), level
);
3224 if (parent
!= null) {
3225 parent
.fChildren
.add(item
);
3227 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3228 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3230 item
.fItemHeight
= fGlobalItemHeight
;
3232 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3233 itemMap
.put(entry
, item
);
3234 if (entry
.hasChildren()) {
3235 Item oldItem
= fItemMap
.get(entry
);
3236 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3237 /* existing items keep their old expanded state */
3238 item
.fExpanded
= oldItem
.fExpanded
;
3240 /* new items set the expanded state according to auto-expand level */
3241 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3243 item
.fHasChildren
= true;
3244 for (ITimeGraphEntry child
: entry
.getChildren()) {
3245 refreshData(itemMap
, item
, level
+ 1, child
);
3250 public void updateExpandedItems() {
3251 for (Item item
: fItems
) {
3252 item
.fExpandedIndex
= -1;
3254 List
<Item
> expandedItemList
= new ArrayList
<>();
3255 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3256 ITimeGraphEntry entry
= fRootEntries
[i
];
3257 Item item
= findItem(entry
);
3258 refreshExpanded(expandedItemList
, item
);
3260 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3261 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3264 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3265 // Check for filters
3266 boolean display
= true;
3267 for (ViewerFilter filter
: fFilters
) {
3268 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3274 item
.fExpandedIndex
= expandedItemList
.size();
3275 expandedItemList
.add(item
);
3276 if (item
.fHasChildren
&& item
.fExpanded
) {
3277 for (Item child
: item
.fChildren
) {
3278 refreshExpanded(expandedItemList
, child
);
3284 public void refreshData(ITimeGraphEntry
[] entries
) {
3285 if (entries
== null) {
3286 fRootEntries
= null;
3288 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3294 public void refreshArrows(List
<ILinkEvent
> events
) {
3295 /* If links are null, reset the list */
3296 if (events
!= null) {
3299 fLinks
= new ArrayList
<>();
3303 public ITimeGraphEntry
[] getEntries() {
3304 return fRootEntries
;
3308 private class Item
{
3309 private boolean fExpanded
;
3310 private int fExpandedIndex
;
3311 private boolean fSelected
;
3312 private boolean fHasChildren
;
3313 private int fItemHeight
;
3314 private final int fLevel
;
3315 private final List
<Item
> fChildren
;
3316 private final String fName
;
3317 private final ITimeGraphEntry fEntry
;
3319 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3320 this.fEntry
= entry
;
3322 this.fLevel
= level
;
3323 this.fChildren
= new ArrayList
<>();
3327 public String
toString() {
3333 public void menuDetected(MenuDetectEvent e
) {
3334 if (null == fTimeProvider
) {
3337 Point p
= toControl(e
.x
, e
.y
);
3338 if (e
.detail
== SWT
.MENU_MOUSE
) {
3339 if (fPendingMenuDetectEvent
== null) {
3340 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3341 * Store the event and trigger it later just before handling mouseUp.
3342 * This allows for the method to detect if mouse is used to drag zoom.
3344 fPendingMenuDetectEvent
= e
;
3346 * Prevent the platform to show the menu when returning. The
3347 * menu will be shown (see below) when this method is called
3348 * again during mouseup().
3353 fPendingMenuDetectEvent
= null;
3354 if ((p
.x
>= fTimeProvider
.getNameSpace()) && (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
)) {
3358 if (fDragState
!= DRAG_NONE
) {
3362 int idx
= getItemIndexAtY(p
.y
);
3363 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3365 Item item
= fItemData
.fExpandedItems
[idx
];
3366 ITimeGraphEntry entry
= item
.fEntry
;
3368 /* Send menu event for the time graph entry */
3371 fireMenuEventOnTimeGraphEntry(e
);
3372 Menu menu
= getMenu();
3373 if (e
.doit
&& (menu
!= null)) {
3374 menu
.setVisible(true);
3377 /* Send menu event for time event */
3378 if (entry
.hasTimeEvents()) {
3379 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3380 if (event
!= null) {
3383 fireMenuEventOnTimeEvent(e
);
3385 if (e
.doit
&& (menu
!= null)) {
3386 menu
.setVisible(true);
3394 * Perform the alignment operation.
3397 * the alignment offset
3399 * @see ITmfTimeAligned
3403 public void performAlign(int offset
) {
3404 fTimeProvider
.setNameSpace(offset
);
3408 * Return the time alignment information
3410 * @return the time alignment information
3412 * @see ITmfTimeAligned
3416 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3417 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());