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
.IStructuredSelection
;
45 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
46 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
47 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
48 import org
.eclipse
.osgi
.util
.NLS
;
49 import org
.eclipse
.swt
.SWT
;
50 import org
.eclipse
.swt
.events
.FocusEvent
;
51 import org
.eclipse
.swt
.events
.FocusListener
;
52 import org
.eclipse
.swt
.events
.KeyEvent
;
53 import org
.eclipse
.swt
.events
.KeyListener
;
54 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
55 import org
.eclipse
.swt
.events
.MenuDetectListener
;
56 import org
.eclipse
.swt
.events
.MouseEvent
;
57 import org
.eclipse
.swt
.events
.MouseListener
;
58 import org
.eclipse
.swt
.events
.MouseMoveListener
;
59 import org
.eclipse
.swt
.events
.MouseTrackListener
;
60 import org
.eclipse
.swt
.events
.MouseWheelListener
;
61 import org
.eclipse
.swt
.events
.PaintEvent
;
62 import org
.eclipse
.swt
.events
.SelectionListener
;
63 import org
.eclipse
.swt
.events
.TraverseEvent
;
64 import org
.eclipse
.swt
.events
.TraverseListener
;
65 import org
.eclipse
.swt
.events
.TypedEvent
;
66 import org
.eclipse
.swt
.graphics
.Color
;
67 import org
.eclipse
.swt
.graphics
.Cursor
;
68 import org
.eclipse
.swt
.graphics
.Font
;
69 import org
.eclipse
.swt
.graphics
.FontData
;
70 import org
.eclipse
.swt
.graphics
.GC
;
71 import org
.eclipse
.swt
.graphics
.Image
;
72 import org
.eclipse
.swt
.graphics
.Point
;
73 import org
.eclipse
.swt
.graphics
.Rectangle
;
74 import org
.eclipse
.swt
.widgets
.Composite
;
75 import org
.eclipse
.swt
.widgets
.Display
;
76 import org
.eclipse
.swt
.widgets
.Event
;
77 import org
.eclipse
.swt
.widgets
.Listener
;
78 import org
.eclipse
.swt
.widgets
.Menu
;
79 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
95 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
96 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
98 import com
.google
.common
.collect
.Iterables
;
101 * Time graph control implementation
103 * @author Alvaro Sanchez-Leon
104 * @author Patrick Tasse
106 public class TimeGraphControl
extends TimeGraphBaseControl
107 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
108 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
109 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
111 /** Constant indicating that all levels of the time graph should be expanded */
112 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
114 private static final int DRAG_MARGIN
= 5;
116 private static final int DRAG_NONE
= 0;
117 private static final int DRAG_TRACE_ITEM
= 1;
118 private static final int DRAG_SPLIT_LINE
= 2;
119 private static final int DRAG_ZOOM
= 3;
120 private static final int DRAG_SELECTION
= 4;
122 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
124 private static final double ZOOM_FACTOR
= 1.5;
125 private static final double ZOOM_IN_FACTOR
= 0.8;
126 private static final double ZOOM_OUT_FACTOR
= 1.25;
128 private static final int SNAP_WIDTH
= 2;
129 private static final int ARROW_HOVER_MAX_DIST
= 5;
131 private static final int NO_STATUS
= -1;
132 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
134 private static final int MAX_LABEL_LENGTH
= 256;
136 private static final int PPI
= 72; // points per inch
137 private static final int DPI
= Display
.getDefault().getDPI().y
;
139 private static final int VERTICAL_ZOOM_DELAY
= 400;
141 /** Resource manager */
142 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
144 /** Color map for event types */
145 private Color
[] fEventColorMap
= null;
147 private ITimeDataProvider fTimeProvider
;
148 private IStatusLineManager fStatusLineManager
= null;
149 private TimeGraphScale fTimeGraphScale
= null;
151 private boolean fIsInFocus
= false;
152 private boolean fMouseOverSplitLine
= false;
153 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
154 private int fHeightAdjustment
= 0;
155 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
156 private boolean fBlendSubPixelEvents
= false;
157 private int fMinimumItemWidth
= 0;
158 private int fTopIndex
= 0;
159 private int fDragState
= DRAG_NONE
;
160 private boolean fDragBeginMarker
= false;
161 private int fDragButton
;
162 private int fDragX0
= 0;
163 private int fDragX
= 0;
164 private boolean fHasNamespaceFocus
= false;
165 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
166 private int fIdealNameSpace
= 0;
167 private long fTime0bak
;
168 private long fTime1bak
;
169 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
170 private ItemData fItemData
= null;
171 private List
<IMarkerEvent
> fMarkers
= null;
172 private boolean fMarkersVisible
= true;
173 private List
<SelectionListener
> fSelectionListeners
;
174 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
175 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
176 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
177 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
178 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
179 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
180 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
181 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
182 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
183 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
184 private MenuDetectEvent fPendingMenuDetectEvent
= null;
185 private boolean fGridLinesVisible
= true;
186 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
187 private boolean fHideArrows
= false;
188 private int fAutoExpandLevel
= ALL_LEVELS
;
189 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
190 private long fVerticalZoomAlignTime
= 0;
191 private int fBorderWidth
= 0;
192 private int fHeaderHeight
= 0;
195 * Standard constructor
198 * The parent composite object
200 * The color scheme to use
202 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
204 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
206 fItemData
= new ItemData();
208 addFocusListener(this);
209 addMouseListener(this);
210 addMouseMoveListener(this);
211 addMouseTrackListener(this);
212 addMouseWheelListener(this);
213 addTraverseListener(this);
214 addKeyListener(this);
215 addMenuDetectListener(this);
216 addListener(SWT
.MouseWheel
, this);
217 addDisposeListener((e
) -> {
218 fResourceManager
.dispose();
219 for (Font font
: fFonts
.values()) {
226 * Sets the timegraph provider used by this timegraph viewer.
228 * @param timeGraphProvider the timegraph provider
230 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
231 fTimeGraphProvider
= timeGraphProvider
;
233 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
234 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
235 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
238 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
239 colorSettingsChanged(stateItems
);
243 * Gets the timegraph provider used by this timegraph viewer.
245 * @return the timegraph provider, or <code>null</code> if not set.
247 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
248 return fTimeGraphProvider
;
252 * Gets the time data provider used by this viewer.
254 * @return The time data provider, or <code>null</code> if not set
257 public ITimeDataProvider
getTimeDataProvider() {
258 return fTimeProvider
;
262 * Gets the color map used by this timegraph viewer.
264 * @return a color map, or <code>null</code> if not set.
266 public Color
[] getEventColorMap() {
267 return fEventColorMap
;
271 * Assign the given time provider
273 * @param timeProvider
276 public void setTimeProvider(ITimeDataProvider timeProvider
) {
277 fTimeProvider
= timeProvider
;
282 * Assign the status line manager
284 * @param statusLineManager
285 * The status line manager, or null to disable status line messages
287 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
288 if (fStatusLineManager
!= null && statusLineManager
== null) {
289 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
291 fStatusLineManager
= statusLineManager
;
295 * Assign the time graph scale
297 * @param timeGraphScale
298 * The time graph scale
300 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
301 fTimeGraphScale
= timeGraphScale
;
305 * Add a selection listener
308 * The listener to add
310 public void addSelectionListener(SelectionListener listener
) {
311 if (listener
== null) {
312 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
314 if (null == fSelectionListeners
) {
315 fSelectionListeners
= new ArrayList
<>();
317 fSelectionListeners
.add(listener
);
321 * Remove a selection listener
324 * The listener to remove
326 public void removeSelectionListener(SelectionListener listener
) {
327 if (null != fSelectionListeners
) {
328 fSelectionListeners
.remove(listener
);
333 * Selection changed callback
335 public void fireSelectionChanged() {
336 if (null != fSelectionListeners
) {
337 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
338 while (it
.hasNext()) {
339 SelectionListener listener
= it
.next();
340 listener
.widgetSelected(null);
344 if (null != fSelectionChangedListeners
) {
345 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
346 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
352 * Default selection callback
354 public void fireDefaultSelection() {
355 if (null != fSelectionListeners
) {
356 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
357 while (it
.hasNext()) {
358 SelectionListener listener
= it
.next();
359 listener
.widgetDefaultSelected(null);
365 * Add a drag selection listener
368 * The listener to add
370 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
371 if (listener
== null) {
372 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
374 if (null == fDragSelectionListeners
) {
375 fDragSelectionListeners
= new ArrayList
<>();
377 fDragSelectionListeners
.add(listener
);
381 * Remove a drag selection listener
384 * The listener to remove
386 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
387 if (null != fDragSelectionListeners
) {
388 fDragSelectionListeners
.remove(listener
);
393 * Drag Selection changed callback
396 * Time interval start
400 public void fireDragSelectionChanged(long start
, long end
) {
401 // check for backward intervals
402 long beginTime
, endTime
;
410 // call the listeners
411 if (null != fDragSelectionListeners
) {
412 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
413 while (it
.hasNext()) {
414 ITimeGraphTimeListener listener
= it
.next();
415 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
421 * Get the traces in the model
423 * @return The array of traces
425 public ITimeGraphEntry
[] getTraces() {
426 return fItemData
.getEntries();
430 * Refresh the data for the thing
432 public void refreshData() {
433 fItemData
.refreshData();
438 * Refresh data for the given traces
441 * The traces to refresh
443 public void refreshData(ITimeGraphEntry
[] traces
) {
444 fItemData
.refreshData(traces
);
449 * Refresh the links (arrows) of this widget
451 * @param events The link event list
453 public void refreshArrows(List
<ILinkEvent
> events
) {
454 fItemData
.refreshArrows(events
);
458 * Get the links (arrows) of this widget
460 * @return The unmodifiable link event list
464 public List
<ILinkEvent
> getArrows() {
465 return Collections
.unmodifiableList(fItemData
.fLinks
);
468 boolean ensureVisibleItem(int idx
, boolean redraw
) {
469 boolean changed
= false;
472 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
473 if (fItemData
.fExpandedItems
[index
].fSelected
) {
478 if (index
>= fItemData
.fExpandedItems
.length
) {
481 if (index
< fTopIndex
) {
488 int page
= countPerPage();
489 if (index
>= fTopIndex
+ page
) {
490 setTopIndex(index
- page
+ 1);
501 * Assign the given index as the top one
506 public void setTopIndex(int idx
) {
507 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
508 index
= Math
.max(0, index
);
514 * Set the top index so that the requested element is at the specified
518 * the time graph entry to be positioned
520 * the requested y-coordinate
523 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
524 Item item
= fItemData
.fItemMap
.get(entry
);
525 if (item
== null || item
.fExpandedIndex
== -1) {
528 int index
= item
.fExpandedIndex
;
529 Rectangle itemRect
= getItemRect(getClientArea(), index
);
530 int delta
= itemRect
.y
+ itemRect
.height
- y
;
531 int topIndex
= getItemIndexAtY(delta
);
532 if (topIndex
!= -1) {
533 setTopIndex(topIndex
);
538 setTopIndex(getExpandedElementCount());
544 * Sets the auto-expand level to be used for new entries discovered when
545 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
546 * . The value 0 means that there is no auto-expand; 1 means that top-level
547 * entries are expanded, but not their children; 2 means that top-level
548 * entries are expanded, and their children, but not grand-children; and so
551 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
555 * non-negative level, or <code>ALL_LEVELS</code> to expand all
558 public void setAutoExpandLevel(int level
) {
559 fAutoExpandLevel
= level
;
563 * Returns the auto-expand level.
565 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
566 * the tree are expanded automatically
567 * @see #setAutoExpandLevel
569 public int getAutoExpandLevel() {
570 return fAutoExpandLevel
;
574 * Get the expanded state of a given entry.
578 * @return true if the entry is expanded, false if collapsed
581 public boolean getExpandedState(ITimeGraphEntry entry
) {
582 Item item
= fItemData
.fItemMap
.get(entry
);
583 return (item
!= null ? item
.fExpanded
: false);
587 * Set the expanded state of a given entry
592 * True if expanded, false if collapsed
594 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
595 Item item
= fItemData
.findItem(entry
);
596 if (item
!= null && item
.fExpanded
!= expanded
) {
597 item
.fExpanded
= expanded
;
598 fItemData
.updateExpandedItems();
604 * Set the expanded state of a given entry to certain relative level.
605 * It will call fireTreeEvent() for each changed entry. At the end
606 * it will call redraw().
611 * level to expand to or negative for all levels
613 * True if expanded, false if collapsed
615 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
616 setExpandedStateInt(entry
, level
, expanded
);
621 * Set the expanded state of a given entry and its children to the first
622 * level that has one collapsed entry.
627 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
628 int level
= findExpandedLevel(entry
);
630 setExpandedStateInt(entry
, level
, true);
636 * Inner class for finding relative level with at least one
639 private class SearchNode
{
640 SearchNode(ITimeGraphEntry e
, int l
) {
644 ITimeGraphEntry entry
;
649 * Finds the relative level with at least one collapsed entry.
653 * @return the found level or -1 if all levels are already expanded.
655 private int findExpandedLevel(ITimeGraphEntry entry
) {
656 Queue
<SearchNode
> queue
= new LinkedList
<>();
657 SearchNode root
= new SearchNode(entry
, 0);
658 SearchNode node
= root
;
661 while (!queue
.isEmpty()) {
662 node
= queue
.remove();
663 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
666 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
667 if (e
.hasChildren()) {
668 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
677 * Set the expanded state of a given entry to certain relative level.
678 * It will call fireTreeEvent() for each changed entry. No redraw is done.
683 * level to expand to or negative for all levels
685 * True if expanded, false if collapsed
687 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
689 if ((level
> 0) || (level
< 0)) {
691 if (entry
.hasChildren()) {
692 for (ITimeGraphEntry e
: entry
.getChildren()) {
693 setExpandedStateInt(e
, level
, expanded
);
697 Item item
= fItemData
.findItem(entry
);
698 if (item
!= null && item
.fExpanded
!= expanded
) {
699 item
.fExpanded
= expanded
;
700 fItemData
.updateExpandedItems();
701 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
706 * Collapses all nodes of the viewer's tree, starting with the root.
708 public void collapseAll() {
709 for (Item item
: fItemData
.fItems
) {
710 item
.fExpanded
= false;
712 fItemData
.updateExpandedItems();
717 * Expands all nodes of the viewer's tree, starting with the root.
719 public void expandAll() {
720 for (Item item
: fItemData
.fItems
) {
721 item
.fExpanded
= true;
723 fItemData
.updateExpandedItems();
728 * Add a tree listener
731 * The listener to add
733 public void addTreeListener(ITimeGraphTreeListener listener
) {
734 if (!fTreeListeners
.contains(listener
)) {
735 fTreeListeners
.add(listener
);
740 * Remove a tree listener
743 * The listener to remove
745 public void removeTreeListener(ITimeGraphTreeListener listener
) {
746 if (fTreeListeners
.contains(listener
)) {
747 fTreeListeners
.remove(listener
);
752 * Tree event callback
757 * The expanded state (true for expanded, false for collapsed)
759 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
760 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
761 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
763 listener
.treeExpanded(event
);
765 listener
.treeCollapsed(event
);
771 * Add a menu listener on {@link ITimeGraphEntry}s
774 * The listener to add
776 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
777 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
778 fTimeGraphEntryMenuListeners
.add(listener
);
783 * Remove a menu listener on {@link ITimeGraphEntry}s
786 * The listener to remove
788 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
789 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
790 fTimeGraphEntryMenuListeners
.remove(listener
);
795 * Menu event callback on {@link ITimeGraphEntry}s
798 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
800 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
801 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
802 listener
.menuDetected(event
);
807 * Add a menu listener on {@link ITimeEvent}s
810 * The listener to add
812 public void addTimeEventMenuListener(MenuDetectListener listener
) {
813 if (!fTimeEventMenuListeners
.contains(listener
)) {
814 fTimeEventMenuListeners
.add(listener
);
819 * Remove a menu listener on {@link ITimeEvent}s
822 * The listener to remove
824 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
825 if (fTimeEventMenuListeners
.contains(listener
)) {
826 fTimeEventMenuListeners
.remove(listener
);
831 * Menu event callback on {@link ITimeEvent}s
834 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
836 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
837 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
838 listener
.menuDetected(event
);
843 public boolean setFocus() {
844 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
845 fHasNamespaceFocus
= true;
847 return super.setFocus();
851 * Returns the current selection for this time graph. If a time graph entry
852 * is selected, it will be the first element in the selection. If a time
853 * event is selected, it will be the second element in the selection.
855 * @return the current selection
858 public ISelection
getSelection() {
859 ITimeGraphEntry entry
= getSelectedTrace();
860 if (null != entry
&& null != fTimeProvider
) {
861 long selectedTime
= fTimeProvider
.getSelectionBegin();
862 ITimeEvent event
= Utils
.findEvent(entry
, selectedTime
, 0);
864 return new StructuredSelection(entry
);
866 return new StructuredSelection(new Object
[] { entry
, event
});
868 return StructuredSelection
.EMPTY
;
872 * Get the selection object
874 * @return The selection
876 public ISelection
getSelectionTrace() {
877 ITimeGraphEntry entry
= getSelectedTrace();
879 return new StructuredSelection(entry
);
881 return StructuredSelection
.EMPTY
;
885 * Enable/disable one of the traces in the model
888 * 1 to enable it, -1 to disable. The method returns immediately
889 * if another value is used.
891 public void selectTrace(int n
) {
892 if ((n
!= 1) && (n
!= -1)) {
896 boolean changed
= false;
897 int lastSelection
= -1;
898 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
899 Item item
= fItemData
.fExpandedItems
[i
];
900 if (item
.fSelected
) {
902 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
903 item
.fSelected
= false;
904 item
= fItemData
.fExpandedItems
[i
+ 1];
905 item
.fSelected
= true;
907 } else if ((-1 == n
) && (i
> 0)) {
908 item
.fSelected
= false;
909 item
= fItemData
.fExpandedItems
[i
- 1];
910 item
.fSelected
= true;
917 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
918 Item item
= fItemData
.fExpandedItems
[0];
919 item
.fSelected
= true;
924 ensureVisibleItem(-1, false);
926 fireSelectionChanged();
934 * 1 for next event, -1 for previous event
936 * true to extend selection range, false for single selection
939 public void selectEvent(int n
, boolean extend
) {
940 if (null == fTimeProvider
) {
943 ITimeGraphEntry trace
= getSelectedTrace();
947 long selectedTime
= fTimeProvider
.getSelectionEnd();
948 long endTime
= fTimeProvider
.getMaxTime();
949 ITimeEvent nextEvent
;
950 if (n
== -1 && selectedTime
> endTime
) {
951 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
953 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
955 if (null == nextEvent
&& n
== -1) {
956 nextEvent
= Utils
.getFirstEvent(trace
);
958 if (null != nextEvent
) {
959 long nextTime
= nextEvent
.getTime();
960 // If last event detected e.g. going back or not moving to a next
962 if (nextTime
<= selectedTime
&& n
== 1) {
963 // Select to the end of this last event
964 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
965 // but not beyond the end of the trace
966 if (nextTime
> endTime
) {
969 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
970 // for previous event go to its end time unless we were already there
971 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
974 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
976 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
978 fireSelectionChanged();
981 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
983 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
985 fireSelectionChanged();
987 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
991 * Select the next event
994 * true to extend selection range, false for single selection
997 public void selectNextEvent(boolean extend
) {
998 selectEvent(1, extend
);
999 // Notify if visible time window has been adjusted
1000 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1004 * Select the previous event
1007 * true to extend selection range, false for single selection
1010 public void selectPrevEvent(boolean extend
) {
1011 selectEvent(-1, extend
);
1012 // Notify if visible time window has been adjusted
1013 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1017 * Select the next trace
1019 public void selectNextTrace() {
1024 * Select the previous trace
1026 public void selectPrevTrace() {
1031 * Scroll left or right by one half window size
1034 * true to scroll left, false to scroll right
1036 public void horizontalScroll(boolean left
) {
1037 long time0
= fTimeProvider
.getTime0();
1038 long time1
= fTimeProvider
.getTime1();
1039 long timeMin
= fTimeProvider
.getMinTime();
1040 long timeMax
= fTimeProvider
.getMaxTime();
1041 long range
= time1
- time0
;
1045 long increment
= Math
.max(1, range
/ 2);
1047 time0
= Math
.max(time0
- increment
, timeMin
);
1048 time1
= time0
+ range
;
1050 time1
= Math
.min(time1
+ increment
, timeMax
);
1051 time0
= time1
- range
;
1053 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1057 * Zoom based on mouse cursor location with mouse scrolling
1059 * @param zoomIn true to zoom in, false to zoom out
1061 public void zoom(boolean zoomIn
) {
1062 int globalX
= getDisplay().getCursorLocation().x
;
1063 Point p
= toControl(globalX
, 0);
1064 int nameSpace
= fTimeProvider
.getNameSpace();
1065 int timeSpace
= fTimeProvider
.getTimeSpace();
1066 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1067 long time0
= fTimeProvider
.getTime0();
1068 long time1
= fTimeProvider
.getTime1();
1069 long interval
= time1
- time0
;
1070 if (interval
== 0) {
1072 } // to allow getting out of single point interval
1075 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1077 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1079 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1080 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1081 long newTime1
= newTime0
+ newInterval
;
1082 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1086 * zoom in using single click
1088 public void zoomIn() {
1089 long prevTime0
= fTimeProvider
.getTime0();
1090 long prevTime1
= fTimeProvider
.getTime1();
1091 long prevRange
= prevTime1
- prevTime0
;
1092 if (prevRange
== 0) {
1095 ITimeDataProvider provider
= fTimeProvider
;
1096 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1097 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1098 selTime
= (prevTime0
+ prevTime1
) / 2;
1100 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1101 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1103 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1105 if (inaccuracy
> 0 && inaccuracy
< 100) {
1106 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1110 long min
= fTimeProvider
.getMinTimeInterval();
1111 if ((time1
- time0
) < min
) {
1112 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1113 time1
= time0
+ min
;
1116 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1120 * zoom out using single click
1122 public void zoomOut() {
1123 long prevTime0
= fTimeProvider
.getTime0();
1124 long prevTime1
= fTimeProvider
.getTime1();
1125 ITimeDataProvider provider
= fTimeProvider
;
1126 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1127 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1128 selTime
= (prevTime0
+ prevTime1
) / 2;
1130 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1131 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1133 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1134 if (inaccuracy
> 0 && inaccuracy
< 100) {
1135 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1139 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1146 * true to zoom in, false to zoom out
1149 public void verticalZoom(boolean zoomIn
) {
1151 fHeightAdjustment
++;
1153 fHeightAdjustment
--;
1155 fItemData
.refreshData();
1160 * Reset the vertical zoom to default.
1164 public void resetVerticalZoom() {
1165 fHeightAdjustment
= 0;
1166 fItemData
.refreshData();
1171 * Set the grid lines visibility. The default is true.
1174 * true to show the grid lines, false otherwise
1177 public void setGridLinesVisible(boolean visible
) {
1178 fGridLinesVisible
= visible
;
1182 * Get the grid lines visibility.
1184 * @return true if the grid lines are visible, false otherwise
1187 public boolean getGridLinesVisible() {
1188 return fGridLinesVisible
;
1192 * Set the grid line color. The default is SWT.COLOR_GRAY.
1195 * the grid line color
1198 public void setGridLineColor(Color color
) {
1199 fGridLineColor
= color
;
1203 * Get the grid line color.
1205 * @return the grid line color
1208 public Color
getGridLineColor() {
1209 return fGridLineColor
;
1213 * Set the markers list.
1216 * The markers list, or null
1219 public void setMarkers(List
<IMarkerEvent
> markers
) {
1224 * Get the markers list.
1226 * @return The markers list, or null
1229 public List
<IMarkerEvent
> getMarkers() {
1234 * Set the markers visibility. The default is true.
1237 * true to show the markers, false otherwise
1240 public void setMarkersVisible(boolean visible
) {
1241 fMarkersVisible
= visible
;
1245 * Get the markers visibility.
1247 * @return true if the markers are visible, false otherwise
1250 public boolean getMarkersVisible() {
1251 return fMarkersVisible
;
1257 * @param hideArrows true to hide arrows
1259 public void hideArrows(boolean hideArrows
) {
1260 fHideArrows
= hideArrows
;
1264 * Follow the arrow forward
1267 * true to extend selection range, false for single selection
1270 public void followArrowFwd(boolean extend
) {
1271 ITimeGraphEntry trace
= getSelectedTrace();
1272 if (trace
== null) {
1275 long selectedTime
= fTimeProvider
.getSelectionEnd();
1276 for (ILinkEvent link
: fItemData
.fLinks
) {
1277 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1278 selectItem(link
.getDestinationEntry(), false);
1279 if (link
.getDuration() != 0) {
1281 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1283 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1285 // Notify if visible time window has been adjusted
1286 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1288 fireSelectionChanged();
1289 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1293 selectNextEvent(extend
);
1297 * Follow the arrow backward
1300 * true to extend selection range, false for single selection
1303 public void followArrowBwd(boolean extend
) {
1304 ITimeGraphEntry trace
= getSelectedTrace();
1305 if (trace
== null) {
1308 long selectedTime
= fTimeProvider
.getSelectionEnd();
1309 for (ILinkEvent link
: fItemData
.fLinks
) {
1310 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1311 selectItem(link
.getEntry(), false);
1312 if (link
.getDuration() != 0) {
1314 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1316 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1318 // Notify if visible time window has been adjusted
1319 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1321 fireSelectionChanged();
1322 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1326 selectPrevEvent(extend
);
1330 * Return the currently selected trace
1332 * @return The entry matching the trace
1334 public ITimeGraphEntry
getSelectedTrace() {
1335 ITimeGraphEntry trace
= null;
1336 int idx
= getSelectedIndex();
1338 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1344 * Retrieve the index of the currently selected item
1348 public int getSelectedIndex() {
1350 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1351 Item item
= fItemData
.fExpandedItems
[i
];
1352 if (item
.fSelected
) {
1360 boolean toggle(int idx
) {
1361 boolean toggled
= false;
1362 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1363 Item item
= fItemData
.fExpandedItems
[idx
];
1364 if (item
.fHasChildren
) {
1365 item
.fExpanded
= !item
.fExpanded
;
1366 fItemData
.updateExpandedItems();
1369 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1376 * Gets the index of the item at the given location.
1380 * @return the index of the item at the given location, of -1 if none.
1382 protected int getItemIndexAtY(int y
) {
1385 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1386 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1392 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1393 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1402 boolean isOverSplitLine(int x
) {
1403 if (x
< 0 || null == fTimeProvider
) {
1406 int nameWidth
= fTimeProvider
.getNameSpace();
1407 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1411 * Gets the {@link ITimeGraphEntry} at the given location.
1414 * a point in the widget
1415 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1419 public ITimeGraphEntry
getEntry(Point pt
) {
1420 int idx
= getItemIndexAtY(pt
.y
);
1421 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1425 * Return the arrow event closest to the given point that is no further than
1426 * a maximum distance.
1429 * a point in the widget
1430 * @return The closest arrow event, or null if there is none close enough.
1432 protected ILinkEvent
getArrow(Point pt
) {
1436 ILinkEvent linkEvent
= null;
1437 double minDistance
= Double
.MAX_VALUE
;
1438 for (ILinkEvent event
: fItemData
.fLinks
) {
1439 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1443 int x2
= x1
+ rect
.width
;
1444 int y2
= y1
+ rect
.height
;
1445 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1446 if (minDistance
> d
) {
1452 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1459 public int getXForTime(long time
) {
1460 if (null == fTimeProvider
) {
1463 long time0
= fTimeProvider
.getTime0();
1464 long time1
= fTimeProvider
.getTime1();
1465 int width
= getSize().x
;
1466 int nameSpace
= fTimeProvider
.getNameSpace();
1467 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1468 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1473 public long getTimeAtX(int coord
) {
1474 if (null == fTimeProvider
) {
1478 Point size
= getSize();
1479 long time0
= fTimeProvider
.getTime0();
1480 long time1
= fTimeProvider
.getTime1();
1481 int nameWidth
= fTimeProvider
.getNameSpace();
1482 final int x
= coord
- nameWidth
;
1483 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1484 if (x
>= 0 && size
.x
>= nameWidth
) {
1485 if (time1
- time0
> timeWidth
) {
1486 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1487 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1489 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1490 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1496 void selectItem(int idx
, boolean addSelection
) {
1497 boolean changed
= false;
1499 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1500 Item item
= fItemData
.fExpandedItems
[idx
];
1501 changed
= !item
.fSelected
;
1502 item
.fSelected
= true;
1505 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1506 Item item
= fItemData
.fExpandedItems
[i
];
1507 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1510 item
.fSelected
= i
== idx
;
1513 changed
|= ensureVisibleItem(idx
, true);
1520 * Callback for item selection
1523 * The entry matching the trace
1524 * @param addSelection
1525 * If the selection is added or removed
1527 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1528 int idx
= fItemData
.findItemIndex(trace
);
1529 selectItem(idx
, addSelection
);
1533 * Retrieve the number of entries shown per page.
1537 public int countPerPage() {
1538 int height
= getSize().y
;
1541 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1542 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1543 if (ySum
>= height
) {
1548 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1549 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1550 if (ySum
>= height
) {
1559 * Get the index of the top element
1563 public int getTopIndex() {
1568 * Get the number of expanded (visible) items
1570 * @return The count of expanded (visible) items
1572 public int getExpandedElementCount() {
1573 return fItemData
.fExpandedItems
.length
;
1577 * Get an array of all expanded (visible) elements
1579 * @return The expanded (visible) elements
1581 public ITimeGraphEntry
[] getExpandedElements() {
1582 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1583 for (Item item
: fItemData
.fExpandedItems
) {
1584 elements
.add(item
.fEntry
);
1586 return elements
.toArray(new ITimeGraphEntry
[0]);
1590 * Get the expanded (visible) element at the specified index.
1594 * @return The expanded (visible) element or null if out of range
1597 public ITimeGraphEntry
getExpandedElement(int index
) {
1598 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1601 return fItemData
.fExpandedItems
[index
].fEntry
;
1604 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1605 Rectangle rect
= getItemRect(bounds
, idx
);
1606 rect
.width
= nameWidth
;
1610 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1611 Rectangle rect
= getItemRect(bounds
, idx
);
1612 rect
.x
+= nameWidth
;
1613 rect
.width
-= nameWidth
;
1617 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1619 if (idx
>= fTopIndex
) {
1620 for (int i
= fTopIndex
; i
< idx
; i
++) {
1621 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1624 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1625 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1628 int y
= bounds
.y
+ ySum
;
1629 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1630 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1634 void paint(Rectangle bounds
, PaintEvent e
) {
1637 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1641 fIdealNameSpace
= 0;
1642 int nameSpace
= fTimeProvider
.getNameSpace();
1644 // draw the background layer
1645 drawBackground(bounds
, nameSpace
, gc
);
1647 // draw the grid lines
1648 drawGridLines(bounds
, gc
);
1650 // draw the background markers
1651 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1654 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1656 // draw the foreground markers
1657 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1659 // draw the links (arrows)
1660 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1662 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1664 int alpha
= gc
.getAlpha();
1667 long time0
= fTimeProvider
.getTime0();
1668 long time1
= fTimeProvider
.getTime1();
1669 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1670 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1671 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1672 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1673 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1675 // draw selection lines
1676 if (fDragState
!= DRAG_SELECTION
) {
1677 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1678 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1679 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1682 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1683 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1688 // draw selection background
1689 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1690 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1691 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1692 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1694 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1695 } else if (x0
- x1
> 1) {
1696 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1700 // draw drag selection background
1701 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1702 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1703 if (fDragX0
< fDragX
) {
1704 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1705 } else if (fDragX0
> fDragX
) {
1706 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1711 if (DRAG_SPLIT_LINE
== fDragState
) {
1712 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1713 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1714 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1715 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1716 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1717 if (fDragX
!= fDragX0
) {
1718 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1720 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1721 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1722 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1723 if (fDragX
!= fDragX0
) {
1724 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1726 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1727 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1728 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1735 * Draw the background layer. Fills the background of the control's name
1736 * space and states space, updates the background of items if necessary,
1737 * and draws the item's name text and middle line.
1740 * The bounds of the control
1742 * The name space width
1747 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1748 // draw empty name space background
1749 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1750 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1752 // draw empty states space background
1753 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1754 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1756 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1757 Rectangle itemRect
= getItemRect(bounds
, i
);
1758 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1761 Item item
= fItemData
.fExpandedItems
[i
];
1762 // draw the background of selected item and items with no time events
1763 if (! item
.fEntry
.hasTimeEvents()) {
1764 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1765 gc
.fillRectangle(itemRect
);
1766 } else if (item
.fSelected
) {
1767 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1768 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1769 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1770 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1772 // draw the name and middle line
1773 if (! item
.fEntry
.hasTimeEvents()) {
1774 drawName(item
, itemRect
, gc
);
1776 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1777 drawName(item
, nameRect
, gc
);
1778 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1779 drawMidLine(rect
, gc
);
1785 * Draw the grid lines
1788 * The bounds of the control
1793 public void drawGridLines(Rectangle bounds
, GC gc
) {
1794 if (!fGridLinesVisible
) {
1797 gc
.setForeground(fGridLineColor
);
1798 gc
.setAlpha(fGridLineColor
.getAlpha());
1799 for (int x
: fTimeGraphScale
.getTickList()) {
1800 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1809 * The rectangle of the area
1810 * @param timeProvider
1813 * The list of markers
1815 * true to draw the foreground markers, false otherwise
1817 * The width reserved for the names
1819 * Reference to the SWT GC object
1822 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1823 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1826 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1827 /* the list can grow concurrently but cannot shrink */
1828 for (int i
= 0; i
< markers
.size(); i
++) {
1829 IMarkerEvent marker
= markers
.get(i
);
1830 if (marker
.isForeground() == foreground
) {
1831 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1834 gc
.setClipping((Rectangle
) null);
1838 * Draw a single marker
1843 * The bounds of the control
1844 * @param timeProvider
1847 * The width reserved for the name
1849 * Reference to the SWT GC object
1852 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1853 Rectangle rect
= Utils
.clone(bounds
);
1854 if (marker
.getEntry() != null) {
1855 int index
= fItemData
.findItemIndex(marker
.getEntry());
1859 rect
= getStatesRect(bounds
, index
, nameSpace
);
1860 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1864 int x0
= getXForTime(marker
.getTime());
1865 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1866 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1869 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1870 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1872 Color color
= getColorScheme().getColor(marker
.getColor());
1873 gc
.setBackground(color
);
1874 gc
.setAlpha(color
.getAlpha());
1875 gc
.fillRectangle(rect
);
1877 String label
= marker
.getLabel();
1878 if (label
!= null && marker
.getEntry() != null) {
1879 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1880 gc
.setForeground(color
);
1881 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1886 * Draw many items at once
1889 * The bounds of the control
1890 * @param timeProvider
1893 * The array items to draw
1895 * The index of the first element to draw
1897 * The name space width
1901 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1902 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1903 for (int i
= topIndex
; i
< items
.length
; i
++) {
1904 Item item
= items
[i
];
1905 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1915 * The bounds of the control
1916 * @param timeProvider
1919 * The expanded item index
1921 * The name space width
1925 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1926 Rectangle itemRect
= getItemRect(bounds
, i
);
1927 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1931 ITimeGraphEntry entry
= item
.fEntry
;
1932 long time0
= timeProvider
.getTime0();
1933 long time1
= timeProvider
.getTime1();
1934 long selectedTime
= fTimeProvider
.getSelectionEnd();
1936 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1937 if (rect
.isEmpty() || (time1
<= time0
)) {
1938 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1942 boolean selected
= item
.fSelected
;
1943 // K pixels per second
1944 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1946 if (item
.fEntry
.hasTimeEvents()) {
1947 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1948 fillSpace(rect
, gc
, selected
);
1950 int margins
= getMarginForHeight(rect
.height
);
1951 int height
= rect
.height
- margins
;
1952 int topMargin
= (margins
+ 1) / 2;
1953 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1955 /* Set the font for this item */
1956 setFontForHeight(height
, gc
);
1958 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1959 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1962 while (iterator
.hasNext()) {
1963 ITimeEvent event
= iterator
.next();
1964 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1965 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1966 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1967 // event is out of bounds
1970 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1971 stateRect
.x
= Math
.max(rect
.x
, x
);
1972 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1973 if (stateRect
.x
== lastX
) {
1974 stateRect
.width
-= 1;
1975 if (stateRect
.width
> 0) {
1976 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1977 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1981 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1982 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1983 lastX
= stateRect
.x
;
1986 gc
.setClipping((Rectangle
) null);
1988 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1995 * The bounds of the control
1996 * @param timeProvider
1999 * The list of link events
2001 * The name space width
2005 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
2006 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2010 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2011 /* the list can grow concurrently but cannot shrink */
2012 for (int i
= 0; i
< links
.size(); i
++) {
2013 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2015 gc
.setClipping((Rectangle
) null);
2019 * Draws a link type event
2022 * The link event to draw
2024 * The bounds of the control
2025 * @param timeProvider
2028 * The name space width
2032 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2033 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2036 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2037 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2038 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2040 if ((srcIndex
== -1) || (destIndex
== -1)) {
2044 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2045 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2047 int x0
= getXForTime(event
.getTime());
2048 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2050 // limit the x-coordinates to prevent integer overflow in calculations
2051 // and also GC.drawLine doesn't draw properly with large coordinates
2052 final int limit
= Integer
.MAX_VALUE
/ 1024;
2053 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2054 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2056 int y0
= src
.y
+ src
.height
/ 2;
2057 int y1
= dst
.y
+ dst
.height
/ 2;
2058 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2067 * Time event for which we're drawing the arrow
2069 * The arrow rectangle
2072 * @return true if the arrow was drawn
2074 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2075 Rectangle rect
, GC gc
) {
2080 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2084 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2087 Color stateColor
= null;
2088 if (colorIdx
< fEventColorMap
.length
) {
2089 stateColor
= fEventColorMap
[colorIdx
];
2091 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2094 gc
.setForeground(stateColor
);
2095 gc
.setBackground(stateColor
);
2097 /* Draw the arrow */
2098 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2099 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2102 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2107 * @author Francis Giraldeau
2110 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2112 * The algorithm was taken from this site, not the code itself
2114 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2117 double cos
= 0.9510;
2118 double sin
= 0.3090;
2119 long lenx
= x1
- x0
;
2120 long leny
= y1
- y0
;
2121 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2123 double dx
= factor
* lenx
/ len
;
2124 double dy
= factor
* leny
/ len
;
2125 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2126 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2127 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2128 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2129 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2130 gc
.fillPolygon(arrow
);
2134 * Draw the name of an item.
2139 * The bounds of the item's name space
2143 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2144 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2146 // No name to be drawn
2147 if (fTimeProvider
.getNameSpace() == 0) {
2151 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2152 setFontForHeight(height
, gc
);
2154 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2155 if (item
.fHasChildren
) {
2156 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2157 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2158 Rectangle rect
= Utils
.clone(bounds
);
2159 rect
.x
+= leftMargin
;
2160 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2161 rect
.width
= EXPAND_SIZE
;
2162 rect
.height
= EXPAND_SIZE
;
2163 gc
.fillRectangle(rect
);
2164 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2165 int midy
= rect
.y
+ rect
.height
/ 2;
2166 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2167 if (!item
.fExpanded
) {
2168 int midx
= rect
.x
+ rect
.width
/ 2;
2169 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2172 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2174 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2177 int imgHeight
= img
.getImageData().height
;
2178 int imgWidth
= img
.getImageData().width
;
2180 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2181 gc
.drawImage(img
, x
, y
);
2182 leftMargin
+= imgWidth
+ MARGIN
;
2184 String name
= item
.fName
;
2185 Point size
= gc
.stringExtent(name
);
2186 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2187 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2189 if (hasTimeEvents
) {
2190 // cut long string with "..."
2191 int width
= bounds
.width
- leftMargin
;
2193 while (size
.x
> width
&& name
.length() > 1) {
2195 name
= name
.substring(0, name
.length() - 1);
2196 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2199 name
+= "..."; //$NON-NLS-1$
2202 Rectangle rect
= Utils
.clone(bounds
);
2203 rect
.x
+= leftMargin
;
2204 rect
.width
-= leftMargin
;
2206 if (rect
.width
> 0) {
2207 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2208 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2209 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2210 leftMargin
+= textWidth
+ MARGIN
;
2212 if (hasTimeEvents
) {
2214 rect
.x
= bounds
.x
+ leftMargin
;
2216 rect
.width
= bounds
.width
- rect
.x
;
2217 drawMidLine(rect
, gc
);
2223 * Draw the state (color fill)
2228 * Time event for which we're drawing the state
2230 * The state rectangle
2234 * Is this time event currently selected (so it appears
2236 * @param timeSelected
2237 * Is the timestamp currently selected
2238 * @return true if the state was drawn
2240 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2241 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2243 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2244 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2247 boolean visible
= rect
.width
== 0 ?
false : true;
2248 rect
.width
= Math
.max(1, rect
.width
);
2249 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2250 gc
.setForeground(black
);
2252 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2254 // Only draw the top and bottom borders
2255 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2256 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2257 if (rect
.width
== 1) {
2258 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2261 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2264 Color stateColor
= null;
2265 if (colorIdx
< fEventColorMap
.length
) {
2266 stateColor
= fEventColorMap
[colorIdx
];
2271 boolean reallySelected
= timeSelected
&& selected
;
2272 // fill all rect area
2273 gc
.setBackground(stateColor
);
2275 gc
.fillRectangle(rect
);
2276 } else if (fBlendSubPixelEvents
) {
2278 gc
.fillRectangle(rect
);
2282 if (reallySelected
) {
2283 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2284 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2287 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2289 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2294 * Fill an item's states rectangle
2297 * The states rectangle
2301 * true if the item is selected
2303 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2304 /* Nothing to draw */
2308 * Draw a line at the middle height of a rectangle
2315 private void drawMidLine(Rectangle rect
, GC gc
) {
2316 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2317 int midy
= rect
.y
+ rect
.height
/ 2;
2318 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2321 private static int getMarginForHeight(int height
) {
2323 * State rectangle is smaller than the item bounds when height is > 4.
2324 * Don't use any margin if the height is below or equal that threshold.
2325 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2326 * pixels for the state height, but with a minimum margin of 1.
2328 final int MARGIN_THRESHOLD
= 4;
2329 final int PREFERRED_HEIGHT
= 13;
2330 final int MIN_MARGIN
= 1;
2331 final int MAX_MARGIN
= 6;
2332 return height
<= MARGIN_THRESHOLD ?
0 :
2333 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2336 private void setFontForHeight(int pixels
, GC gc
) {
2337 /* convert font height from pixels to points */
2338 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2339 Font font
= fFonts
.get(height
);
2341 FontData fontData
= gc
.getFont().getFontData()[0];
2342 fontData
.setHeight(height
);
2343 font
= new Font(gc
.getDevice(), fontData
);
2344 fFonts
.put(height
, font
);
2350 public void keyTraversed(TraverseEvent e
) {
2351 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2357 public void keyPressed(KeyEvent e
) {
2359 if (fItemData
.fExpandedItems
.length
== 0) {
2362 if (SWT
.HOME
== e
.keyCode
) {
2364 } else if (SWT
.END
== e
.keyCode
) {
2365 idx
= fItemData
.fExpandedItems
.length
- 1;
2366 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2367 idx
= getSelectedIndex();
2370 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2373 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2374 idx
= getSelectedIndex();
2377 } else if (idx
> 0) {
2380 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2381 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2382 selectPrevEvent(extend
);
2383 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2384 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2385 selectNextEvent(extend
);
2386 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2387 int page
= countPerPage();
2388 idx
= getSelectedIndex();
2393 if (idx
>= fItemData
.fExpandedItems
.length
) {
2394 idx
= fItemData
.fExpandedItems
.length
- 1;
2396 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2397 int page
= countPerPage();
2398 idx
= getSelectedIndex();
2406 } else if (SWT
.CR
== e
.keyCode
) {
2407 idx
= getSelectedIndex();
2409 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2412 fireDefaultSelection();
2416 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2417 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2419 if (fVerticalZoomAlignEntry
!= null) {
2420 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2422 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2423 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2424 verticalZoom(false);
2425 if (fVerticalZoomAlignEntry
!= null) {
2426 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2428 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2429 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2430 resetVerticalZoom();
2431 if (fVerticalZoomAlignEntry
!= null) {
2432 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2434 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2435 if (fHasNamespaceFocus
) {
2436 ITimeGraphEntry entry
= getSelectedTrace();
2437 setExpandedState(entry
, 0, true);
2441 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2442 if (fHasNamespaceFocus
) {
2443 ITimeGraphEntry entry
= getSelectedTrace();
2444 if ((entry
!= null) && entry
.hasChildren()) {
2445 setExpandedState(entry
, -1, false);
2450 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2451 if (fHasNamespaceFocus
) {
2452 ITimeGraphEntry entry
= getSelectedTrace();
2453 if ((entry
!= null) && entry
.hasChildren()) {
2454 setExpandedStateLevel(entry
);
2459 selectItem(idx
, false);
2460 fireSelectionChanged();
2462 int x
= toControl(e
.display
.getCursorLocation()).x
;
2463 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2467 public void keyReleased(KeyEvent e
) {
2468 int x
= toControl(e
.display
.getCursorLocation()).x
;
2469 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2473 public void focusGained(FocusEvent e
) {
2476 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2480 public void focusLost(FocusEvent e
) {
2482 if (DRAG_NONE
!= fDragState
) {
2484 fDragState
= DRAG_NONE
;
2487 updateStatusLine(NO_STATUS
);
2491 * @return If the current view is focused
2493 public boolean isInFocus() {
2498 * Provide the possibility to control the wait cursor externally e.g. data
2499 * requests in progress
2501 * @param waitInd Should we wait indefinitely?
2503 public void waitCursor(boolean waitInd
) {
2504 // Update cursor as indicated
2506 setCursor(fWaitCursor
);
2512 private void updateCursor(int x
, int stateMask
) {
2513 // if Wait cursor not active, check for the need to change the cursor
2514 if (getCursor() == fWaitCursor
) {
2517 Cursor cursor
= null;
2518 if (fDragState
== DRAG_SPLIT_LINE
) {
2519 } else if (fDragState
== DRAG_SELECTION
) {
2520 cursor
= fResizeCursor
;
2521 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2522 cursor
= fDragCursor
;
2523 } else if (fDragState
== DRAG_ZOOM
) {
2524 cursor
= fZoomCursor
;
2525 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2526 cursor
= fDragCursor
;
2527 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2528 cursor
= fResizeCursor
;
2529 } else if (!isOverSplitLine(x
)) {
2530 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2531 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2532 int xBegin
= getXForTime(selectionBegin
);
2533 int xEnd
= getXForTime(selectionEnd
);
2534 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2535 cursor
= fResizeCursor
;
2538 if (getCursor() != cursor
) {
2544 * Update the status line following a change of selection.
2548 public void updateStatusLine() {
2549 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2552 private void updateStatusLine(int x
) {
2553 // use the time provider of the time graph scale for the status line
2554 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2555 if (fStatusLineManager
== null || null == tdp
||
2556 tdp
.getTime0() == tdp
.getTime1()) {
2559 TimeFormat tf
= tdp
.getTimeFormat();
2560 Resolution res
= Resolution
.NANOSEC
;
2561 StringBuilder message
= new StringBuilder();
2562 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2563 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2564 long time
= getTimeAtX(x
);
2566 if (tdp
instanceof ITimeDataProviderConverter
) {
2567 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2569 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2571 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2572 Utils
.formatTime(time
, tf
, res
)
2576 long selectionBegin
= tdp
.getSelectionBegin();
2577 long selectionEnd
= tdp
.getSelectionEnd();
2578 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2580 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2581 Utils
.formatTime(selectionBegin
, tf
, res
)
2583 if (selectionBegin
!= selectionEnd
) {
2584 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2586 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2587 Utils
.formatTime(selectionEnd
, tf
, res
),
2588 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2591 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2592 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2593 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2594 if (tdp
instanceof ITimeDataProviderConverter
) {
2595 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2596 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2598 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2600 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2601 Utils
.formatTime(time0
, tf
, res
),
2602 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2603 Utils
.formatTime(time
, tf
, res
),
2604 Utils
.formatDelta(time
- time0
, tf
, res
)
2607 fStatusLineManager
.setMessage(message
.toString());
2611 public void mouseMove(MouseEvent e
) {
2612 if (null == fTimeProvider
) {
2615 Point size
= getSize();
2616 if (DRAG_TRACE_ITEM
== fDragState
) {
2617 int nameWidth
= fTimeProvider
.getNameSpace();
2618 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2620 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2621 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2622 long time1
= fTime1bak
- timeDelta
;
2623 long maxTime
= fTimeProvider
.getMaxTime();
2624 if (time1
> maxTime
) {
2627 long time0
= time1
- (fTime1bak
- fTime0bak
);
2628 if (time0
< fTimeProvider
.getMinTime()) {
2629 time0
= fTimeProvider
.getMinTime();
2630 time1
= time0
+ (fTime1bak
- fTime0bak
);
2632 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2634 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2636 fTimeProvider
.setNameSpace(e
.x
);
2637 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2638 } else if (DRAG_SELECTION
== fDragState
) {
2639 if (fDragBeginMarker
) {
2640 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2642 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2645 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2646 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2647 } else if (DRAG_ZOOM
== fDragState
) {
2648 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2650 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2651 } else if (DRAG_NONE
== fDragState
) {
2652 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2653 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2656 fMouseOverSplitLine
= mouseOverSplitLine
;
2659 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2660 fHasNamespaceFocus
= false;
2662 fHasNamespaceFocus
= true;
2664 updateCursor(e
.x
, e
.stateMask
);
2665 updateStatusLine(e
.x
);
2669 public void mouseDoubleClick(MouseEvent e
) {
2670 if (null == fTimeProvider
) {
2673 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2674 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2675 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2676 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2677 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2680 fMouseOverSplitLine
= mouseOverSplitLine
;
2681 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2684 int idx
= getItemIndexAtY(e
.y
);
2686 selectItem(idx
, false);
2687 fireDefaultSelection();
2693 public void mouseDown(MouseEvent e
) {
2694 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2695 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2696 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2700 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2701 int nameSpace
= fTimeProvider
.getNameSpace();
2702 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2703 fDragState
= DRAG_SPLIT_LINE
;
2704 fDragButton
= e
.button
;
2707 fTime0bak
= fTimeProvider
.getTime0();
2708 fTime1bak
= fTimeProvider
.getTime1();
2710 updateCursor(e
.x
, e
.stateMask
);
2714 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2715 int nameSpace
= fTimeProvider
.getNameSpace();
2716 idx
= getItemIndexAtY(e
.y
);
2718 Item item
= fItemData
.fExpandedItems
[idx
];
2719 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2723 selectItem(idx
, false);
2724 fireSelectionChanged();
2726 selectItem(idx
, false); // clear selection
2727 fireSelectionChanged();
2729 long hitTime
= getTimeAtX(e
.x
);
2733 fDragState
= DRAG_SELECTION
;
2734 fDragBeginMarker
= false;
2735 fDragButton
= e
.button
;
2738 fDragTime0
= getTimeAtX(fDragX0
);
2739 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2740 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2741 int xBegin
= getXForTime(selectionBegin
);
2742 int xEnd
= getXForTime(selectionEnd
);
2743 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2744 long time
= getTimeAtX(e
.x
);
2745 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2746 fDragBeginMarker
= true;
2749 fDragTime0
= selectionEnd
;
2752 fDragTime0
= selectionBegin
;
2755 long time
= getTimeAtX(e
.x
);
2756 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2757 fDragBeginMarker
= true;
2760 fDragTime0
= selectionEnd
;
2761 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2763 fDragTime0
= selectionBegin
;
2766 fTime0bak
= fTimeProvider
.getTime0();
2767 fTime1bak
= fTimeProvider
.getTime1();
2769 updateCursor(e
.x
, e
.stateMask
);
2770 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2772 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2773 long hitTime
= getTimeAtX(e
.x
);
2776 fDragState
= DRAG_TRACE_ITEM
;
2777 fDragButton
= e
.button
;
2780 fTime0bak
= fTimeProvider
.getTime0();
2781 fTime1bak
= fTimeProvider
.getTime1();
2782 updateCursor(e
.x
, e
.stateMask
);
2784 } else if (3 == e
.button
) {
2785 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2787 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2789 fDragTime0
= getTimeAtX(fDragX0
);
2790 fDragState
= DRAG_ZOOM
;
2791 fDragButton
= e
.button
;
2793 updateCursor(e
.x
, e
.stateMask
);
2794 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2796 idx
= getItemIndexAtY(e
.y
);
2797 selectItem(idx
, false);
2798 fireSelectionChanged();
2804 public void mouseUp(MouseEvent e
) {
2805 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2806 if ((fDragState
== DRAG_ZOOM
) && isInDragZoomMargin()) {
2807 // Select entry and time event for single click
2808 long time
= getTimeAtX(fDragX0
);
2809 fTimeProvider
.setSelectionRangeNotify(time
, time
, false);
2810 int idx
= getItemIndexAtY(e
.y
);
2811 selectItem(idx
, false);
2812 fireSelectionChanged();
2814 menuDetected(fPendingMenuDetectEvent
);
2816 if (DRAG_NONE
!= fDragState
) {
2818 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2819 fDragState
= DRAG_NONE
;
2820 if (fDragX
!= fDragX0
) {
2821 fTimeProvider
.notifyStartFinishTime();
2823 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2824 fDragState
= DRAG_NONE
;
2826 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2827 fDragState
= DRAG_NONE
;
2828 if (fDragX
== fDragX0
) { // click without selecting anything
2829 long time
= getTimeAtX(e
.x
);
2830 fTimeProvider
.setSelectedTimeNotify(time
, false);
2832 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2833 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2834 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2837 fTimeGraphScale
.setDragRange(-1, -1);
2838 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2839 fDragState
= DRAG_NONE
;
2840 int nameWidth
= fTimeProvider
.getNameSpace();
2841 if ((Math
.max(fDragX
, fDragX0
) > nameWidth
) && !isInDragZoomMargin()) {
2842 long time0
= getTimeAtX(fDragX0
);
2843 long time1
= getTimeAtX(fDragX
);
2844 if (time0
< time1
) {
2845 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2847 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2852 fTimeGraphScale
.setDragRange(-1, -1);
2855 updateCursor(e
.x
, e
.stateMask
);
2856 updateStatusLine(e
.x
);
2860 public void mouseEnter(MouseEvent e
) {
2864 public void mouseExit(MouseEvent e
) {
2865 if (fMouseOverSplitLine
) {
2866 fMouseOverSplitLine
= false;
2869 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2873 public void mouseHover(MouseEvent e
) {
2877 public void mouseScrolled(MouseEvent e
) {
2878 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2883 * On some platforms the mouse scroll event is sent to the
2884 * control that has focus even if it is not under the cursor.
2885 * Handle the event only if over the time graph control.
2887 Point size
= getSize();
2888 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2889 if (!bounds
.contains(e
.x
, e
.y
)) {
2893 boolean horizontalZoom
= false;
2894 boolean horizontalScroll
= false;
2895 boolean verticalZoom
= false;
2896 boolean verticalScroll
= false;
2898 // over the time graph control
2899 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2900 verticalZoom
= true;
2901 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2902 // over the name space
2903 verticalScroll
= true;
2905 // over the state area
2906 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2907 // over the state area, CTRL pressed
2908 horizontalZoom
= true;
2909 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2910 // over the state area, SHIFT pressed
2911 horizontalScroll
= true;
2913 // over the state area, no modifier pressed
2914 verticalScroll
= true;
2918 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2919 verticalZoom(e
.count
> 0);
2920 if (fVerticalZoomAlignEntry
!= null) {
2921 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2923 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2925 } else if (horizontalScroll
) {
2926 horizontalScroll(e
.count
> 0);
2927 } else if (verticalScroll
){
2928 setTopIndex(getTopIndex() - e
.count
);
2933 * Get the vertical zoom alignment entry and position based on the current
2934 * selection. If there is no selection or if the selection is not visible,
2935 * return an alignment entry with a null time graph entry.
2937 * @return a map entry where the key is the selection's time graph entry and
2938 * the value is the center y-coordinate of that entry, or null
2940 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2941 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2942 if (alignEntry
!= null) {
2945 int index
= getSelectedIndex();
2946 if (index
== -1 || index
>= getExpandedElementCount()) {
2947 return new SimpleEntry
<>(null, 0);
2949 Rectangle bounds
= getClientArea();
2950 Rectangle itemRect
= getItemRect(bounds
, index
);
2951 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2952 /* selection is not visible */
2953 return new SimpleEntry
<>(null, 0);
2955 ITimeGraphEntry entry
= getExpandedElement(index
);
2956 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2957 return new SimpleEntry
<>(entry
, y
);
2961 * Get the vertical zoom alignment entry and position at the specified
2965 * the cursor y-coordinate
2966 * @return a map entry where the key is the time graph entry under the
2967 * cursor and the value is the cursor y-coordinate
2969 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2970 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2971 if (alignEntry
!= null) {
2974 int index
= getItemIndexAtY(y
);
2976 index
= getExpandedElementCount() - 1;
2978 ITimeGraphEntry entry
= getExpandedElement(index
);
2979 return new SimpleEntry
<>(entry
, y
);
2983 * Get the vertical zoom alignment entry and position if there is an ongoing
2984 * one and we are within the vertical zoom delay, or otherwise return null.
2986 * @return a map entry where the key is a time graph entry and the value is
2987 * a y-coordinate, or null
2989 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2990 long currentTimeMillis
= System
.currentTimeMillis();
2991 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2993 * If the vertical zoom is triggered repeatedly in a short amount of
2994 * time, use the initial event's entry and position.
2996 fVerticalZoomAlignTime
= currentTimeMillis
;
2997 return fVerticalZoomAlignEntry
;
2999 fVerticalZoomAlignTime
= currentTimeMillis
;
3004 public void handleEvent(Event event
) {
3005 if (event
.type
== SWT
.MouseWheel
) {
3006 // prevent horizontal scrolling when the mouse wheel is used to
3007 // scroll vertically or zoom
3013 public int getBorderWidth() {
3014 return fBorderWidth
;
3018 * Set the border width
3020 * @param borderWidth
3023 public void setBorderWidth(int borderWidth
) {
3024 this.fBorderWidth
= borderWidth
;
3028 * @return The current height of the header row
3030 public int getHeaderHeight() {
3031 return fHeaderHeight
;
3035 * Set the height of the header row
3037 * @param headerHeight
3040 public void setHeaderHeight(int headerHeight
) {
3041 this.fHeaderHeight
= headerHeight
;
3045 * @return The default height of regular item rows
3047 public int getItemHeight() {
3048 return fGlobalItemHeight
;
3052 * Set the default height of regular item rows.
3057 public void setItemHeight(int rowHeight
) {
3058 this.fGlobalItemHeight
= rowHeight
;
3059 for (Item item
: fItemData
.fItems
) {
3060 item
.fItemHeight
= rowHeight
;
3065 * Set the height of a specific item. Overrides the default item height.
3068 * A time graph entry
3071 * @return true if the height is successfully stored, false otherwise
3073 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3074 Item item
= fItemData
.findItem(entry
);
3076 item
.fItemHeight
= rowHeight
;
3083 * Set the minimum item width
3085 * @param width The minimum width
3087 public void setMinimumItemWidth(int width
) {
3088 this.fMinimumItemWidth
= width
;
3092 * @return The minimum item width
3094 public int getMinimumItemWidth() {
3095 return fMinimumItemWidth
;
3099 * Set whether all time events with a duration shorter than one pixel should
3100 * be blended in. If false, only the first such time event will be drawn and
3101 * the subsequent time events in the same pixel will be discarded. The
3102 * default value is false.
3105 * true if sub-pixel events should be blended, false otherwise.
3108 public void setBlendSubPixelEvents(boolean blend
) {
3109 fBlendSubPixelEvents
= blend
;
3113 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3114 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3115 fSelectionChangedListeners
.add(listener
);
3120 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3121 if (listener
!= null) {
3122 fSelectionChangedListeners
.remove(listener
);
3127 public void setSelection(ISelection selection
) {
3128 if (selection
instanceof IStructuredSelection
) {
3129 Object ob
= ((IStructuredSelection
) selection
).getFirstElement();
3130 if (ob
instanceof ITimeGraphEntry
) {
3131 selectItem((ITimeGraphEntry
) ob
, false);
3138 * @param filter The filter object to be attached to the view
3140 public void addFilter(@NonNull ViewerFilter filter
) {
3141 if (!fFilters
.contains(filter
)) {
3142 fFilters
.add(filter
);
3147 * @param filter The filter object to be attached to the view
3149 public void removeFilter(@NonNull ViewerFilter filter
) {
3150 fFilters
.remove(filter
);
3154 * Returns this control's filters.
3156 * @return an array of viewer filters
3159 public @NonNull ViewerFilter
[] getFilters() {
3160 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3164 * Sets the filters, replacing any previous filters.
3167 * an array of viewer filters, or null
3170 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3172 if (filters
!= null) {
3173 fFilters
.addAll(Arrays
.asList(filters
));
3178 public void colorSettingsChanged(StateItem
[] stateItems
) {
3179 /* Destroy previous colors from the resource manager */
3180 if (fEventColorMap
!= null) {
3181 for (Color color
: fEventColorMap
) {
3182 fResourceManager
.destroyColor(color
.getRGB());
3185 if (stateItems
!= null) {
3186 fEventColorMap
= new Color
[stateItems
.length
];
3187 for (int i
= 0; i
< stateItems
.length
; i
++) {
3188 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3191 fEventColorMap
= new Color
[] { };
3196 private class ItemData
{
3197 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3198 private Item
[] fExpandedItems
= new Item
[0];
3199 private Item
[] fItems
= new Item
[0];
3200 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3201 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3206 public Item
findItem(ITimeGraphEntry entry
) {
3207 return fItemMap
.get(entry
);
3210 public int findItemIndex(ITimeGraphEntry entry
) {
3211 Item item
= fItemMap
.get(entry
);
3215 return item
.fExpandedIndex
;
3218 public void refreshData() {
3219 ITimeGraphEntry selection
= getSelectedTrace();
3220 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3221 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3222 ITimeGraphEntry entry
= fRootEntries
[i
];
3223 refreshData(itemMap
, null, 0, entry
);
3226 fItems
= fItemMap
.values().toArray(new Item
[0]);
3227 updateExpandedItems();
3228 if (selection
!= null) {
3229 for (Item item
: fExpandedItems
) {
3230 if (item
.fEntry
== selection
) {
3231 item
.fSelected
= true;
3238 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3239 Item item
= new Item(entry
, entry
.getName(), level
);
3240 if (parent
!= null) {
3241 parent
.fChildren
.add(item
);
3243 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3244 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3246 item
.fItemHeight
= fGlobalItemHeight
;
3248 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3249 itemMap
.put(entry
, item
);
3250 if (entry
.hasChildren()) {
3251 Item oldItem
= fItemMap
.get(entry
);
3252 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3253 /* existing items keep their old expanded state */
3254 item
.fExpanded
= oldItem
.fExpanded
;
3256 /* new items set the expanded state according to auto-expand level */
3257 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3259 item
.fHasChildren
= true;
3260 for (ITimeGraphEntry child
: entry
.getChildren()) {
3261 refreshData(itemMap
, item
, level
+ 1, child
);
3266 public void updateExpandedItems() {
3267 for (Item item
: fItems
) {
3268 item
.fExpandedIndex
= -1;
3270 List
<Item
> expandedItemList
= new ArrayList
<>();
3271 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3272 ITimeGraphEntry entry
= fRootEntries
[i
];
3273 Item item
= findItem(entry
);
3274 refreshExpanded(expandedItemList
, item
);
3276 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3277 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3280 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3281 // Check for filters
3282 boolean display
= true;
3283 for (ViewerFilter filter
: fFilters
) {
3284 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3290 item
.fExpandedIndex
= expandedItemList
.size();
3291 expandedItemList
.add(item
);
3292 if (item
.fHasChildren
&& item
.fExpanded
) {
3293 for (Item child
: item
.fChildren
) {
3294 refreshExpanded(expandedItemList
, child
);
3300 public void refreshData(ITimeGraphEntry
[] entries
) {
3301 if (entries
== null) {
3302 fRootEntries
= null;
3304 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3310 public void refreshArrows(List
<ILinkEvent
> events
) {
3311 /* If links are null, reset the list */
3312 if (events
!= null) {
3315 fLinks
= new ArrayList
<>();
3319 public ITimeGraphEntry
[] getEntries() {
3320 return fRootEntries
;
3324 private class Item
{
3325 private boolean fExpanded
;
3326 private int fExpandedIndex
;
3327 private boolean fSelected
;
3328 private boolean fHasChildren
;
3329 private int fItemHeight
;
3330 private final int fLevel
;
3331 private final List
<Item
> fChildren
;
3332 private final String fName
;
3333 private final ITimeGraphEntry fEntry
;
3335 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3336 this.fEntry
= entry
;
3338 this.fLevel
= level
;
3339 this.fChildren
= new ArrayList
<>();
3343 public String
toString() {
3349 public void menuDetected(MenuDetectEvent e
) {
3350 if (null == fTimeProvider
) {
3353 Point p
= toControl(e
.x
, e
.y
);
3354 if (e
.detail
== SWT
.MENU_MOUSE
) {
3355 if (fPendingMenuDetectEvent
== null) {
3356 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3357 * Store the event and trigger it later just before handling mouseUp.
3358 * This allows for the method to detect if mouse is used to drag zoom.
3360 fPendingMenuDetectEvent
= e
;
3362 * Prevent the platform to show the menu when returning. The
3363 * menu will be shown (see below) when this method is called
3364 * again during mouseup().
3369 fPendingMenuDetectEvent
= null;
3370 if ((p
.x
>= fTimeProvider
.getNameSpace()) && (fDragState
!= DRAG_ZOOM
|| !isInDragZoomMargin())) {
3374 if (fDragState
!= DRAG_NONE
) {
3378 int idx
= getItemIndexAtY(p
.y
);
3379 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3381 Item item
= fItemData
.fExpandedItems
[idx
];
3382 ITimeGraphEntry entry
= item
.fEntry
;
3384 /* Send menu event for the time graph entry */
3387 fireMenuEventOnTimeGraphEntry(e
);
3388 Menu menu
= getMenu();
3389 if (e
.doit
&& (menu
!= null)) {
3390 menu
.setVisible(true);
3393 /* Send menu event for time event */
3394 if (entry
.hasTimeEvents()) {
3395 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3396 if (event
!= null) {
3399 fireMenuEventOnTimeEvent(e
);
3401 if (e
.doit
&& (menu
!= null)) {
3402 menu
.setVisible(true);
3410 * Perform the alignment operation.
3413 * the alignment offset
3415 * @see ITmfTimeAligned
3419 public void performAlign(int offset
) {
3420 fTimeProvider
.setNameSpace(offset
);
3424 * Return the time alignment information
3426 * @return the time alignment information
3428 * @see ITmfTimeAligned
3432 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3433 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());
3436 private boolean isInDragZoomMargin() {
3437 return (Math
.abs(fDragX
- fDragX0
) < DRAG_MARGIN
);