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
.common
.core
.math
.SaturatedArithmetic
;
80 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
95 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
96 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
97 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
99 import com
.google
.common
.collect
.Iterables
;
102 * Time graph control implementation
104 * @author Alvaro Sanchez-Leon
105 * @author Patrick Tasse
107 public class TimeGraphControl
extends TimeGraphBaseControl
108 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
109 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
110 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
112 /** Constant indicating that all levels of the time graph should be expanded */
113 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
115 private static final int DRAG_MARGIN
= 5;
117 private static final int DRAG_NONE
= 0;
118 private static final int DRAG_TRACE_ITEM
= 1;
119 private static final int DRAG_SPLIT_LINE
= 2;
120 private static final int DRAG_ZOOM
= 3;
121 private static final int DRAG_SELECTION
= 4;
123 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
125 private static final double ZOOM_FACTOR
= 1.5;
126 private static final double ZOOM_IN_FACTOR
= 0.8;
127 private static final double ZOOM_OUT_FACTOR
= 1.25;
129 private static final int SNAP_WIDTH
= 2;
130 private static final int ARROW_HOVER_MAX_DIST
= 5;
132 private static final int NO_STATUS
= -1;
133 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
135 private static final int MAX_LABEL_LENGTH
= 256;
137 private static final int PPI
= 72; // points per inch
138 private static final int DPI
= Display
.getDefault().getDPI().y
;
140 private static final int VERTICAL_ZOOM_DELAY
= 400;
142 /** Resource manager */
143 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
145 /** Color map for event types */
146 private Color
[] fEventColorMap
= null;
148 private ITimeDataProvider fTimeProvider
;
149 private IStatusLineManager fStatusLineManager
= null;
150 private TimeGraphScale fTimeGraphScale
= null;
152 private boolean fIsInFocus
= false;
153 private boolean fMouseOverSplitLine
= false;
154 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
155 private int fHeightAdjustment
= 0;
156 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
157 private boolean fBlendSubPixelEvents
= false;
158 private int fMinimumItemWidth
= 0;
159 private int fTopIndex
= 0;
160 private int fDragState
= DRAG_NONE
;
161 private boolean fDragBeginMarker
= false;
162 private int fDragButton
;
163 private int fDragX0
= 0;
164 private int fDragX
= 0;
165 private boolean fHasNamespaceFocus
= false;
166 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
167 private int fIdealNameSpace
= 0;
168 private long fTime0bak
;
169 private long fTime1bak
;
170 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
171 private ItemData fItemData
= null;
172 private List
<IMarkerEvent
> fMarkers
= null;
173 private boolean fMarkersVisible
= true;
174 private List
<SelectionListener
> fSelectionListeners
;
175 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
176 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
177 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
178 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
179 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
180 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
181 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
182 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
183 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
184 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
185 private MenuDetectEvent fPendingMenuDetectEvent
= null;
186 private boolean fGridLinesVisible
= true;
187 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
188 private boolean fHideArrows
= false;
189 private int fAutoExpandLevel
= ALL_LEVELS
;
190 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
191 private long fVerticalZoomAlignTime
= 0;
192 private int fBorderWidth
= 0;
193 private int fHeaderHeight
= 0;
196 * Standard constructor
199 * The parent composite object
201 * The color scheme to use
203 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
205 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
207 fItemData
= new ItemData();
209 addFocusListener(this);
210 addMouseListener(this);
211 addMouseMoveListener(this);
212 addMouseTrackListener(this);
213 addMouseWheelListener(this);
214 addTraverseListener(this);
215 addKeyListener(this);
216 addMenuDetectListener(this);
217 addListener(SWT
.MouseWheel
, this);
218 addDisposeListener((e
) -> {
219 fResourceManager
.dispose();
220 for (Font font
: fFonts
.values()) {
227 * Sets the timegraph provider used by this timegraph viewer.
229 * @param timeGraphProvider the timegraph provider
231 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
232 fTimeGraphProvider
= timeGraphProvider
;
234 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
235 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
236 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
239 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
240 colorSettingsChanged(stateItems
);
244 * Gets the timegraph provider used by this timegraph viewer.
246 * @return the timegraph provider, or <code>null</code> if not set.
248 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
249 return fTimeGraphProvider
;
253 * Gets the time data provider used by this viewer.
255 * @return The time data provider, or <code>null</code> if not set
258 public ITimeDataProvider
getTimeDataProvider() {
259 return fTimeProvider
;
263 * Gets the color map used by this timegraph viewer.
265 * @return a color map, or <code>null</code> if not set.
267 public Color
[] getEventColorMap() {
268 return fEventColorMap
;
272 * Assign the given time provider
274 * @param timeProvider
277 public void setTimeProvider(ITimeDataProvider timeProvider
) {
278 fTimeProvider
= timeProvider
;
283 * Assign the status line manager
285 * @param statusLineManager
286 * The status line manager, or null to disable status line messages
288 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
289 if (fStatusLineManager
!= null && statusLineManager
== null) {
290 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
292 fStatusLineManager
= statusLineManager
;
296 * Assign the time graph scale
298 * @param timeGraphScale
299 * The time graph scale
301 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
302 fTimeGraphScale
= timeGraphScale
;
306 * Add a selection listener
309 * The listener to add
311 public void addSelectionListener(SelectionListener listener
) {
312 if (listener
== null) {
313 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
315 if (null == fSelectionListeners
) {
316 fSelectionListeners
= new ArrayList
<>();
318 fSelectionListeners
.add(listener
);
322 * Remove a selection listener
325 * The listener to remove
327 public void removeSelectionListener(SelectionListener listener
) {
328 if (null != fSelectionListeners
) {
329 fSelectionListeners
.remove(listener
);
334 * Selection changed callback
336 public void fireSelectionChanged() {
337 if (null != fSelectionListeners
) {
338 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
339 while (it
.hasNext()) {
340 SelectionListener listener
= it
.next();
341 listener
.widgetSelected(null);
345 if (null != fSelectionChangedListeners
) {
346 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
347 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
353 * Default selection callback
355 public void fireDefaultSelection() {
356 if (null != fSelectionListeners
) {
357 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
358 while (it
.hasNext()) {
359 SelectionListener listener
= it
.next();
360 listener
.widgetDefaultSelected(null);
366 * Add a drag selection listener
369 * The listener to add
371 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
372 if (listener
== null) {
373 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
375 if (null == fDragSelectionListeners
) {
376 fDragSelectionListeners
= new ArrayList
<>();
378 fDragSelectionListeners
.add(listener
);
382 * Remove a drag selection listener
385 * The listener to remove
387 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
388 if (null != fDragSelectionListeners
) {
389 fDragSelectionListeners
.remove(listener
);
394 * Drag Selection changed callback
397 * Time interval start
401 public void fireDragSelectionChanged(long start
, long end
) {
402 // check for backward intervals
403 long beginTime
, endTime
;
411 // call the listeners
412 if (null != fDragSelectionListeners
) {
413 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
414 while (it
.hasNext()) {
415 ITimeGraphTimeListener listener
= it
.next();
416 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
422 * Get the traces in the model
424 * @return The array of traces
426 public ITimeGraphEntry
[] getTraces() {
427 return fItemData
.getEntries();
431 * Refresh the data for the thing
433 public void refreshData() {
434 fItemData
.refreshData();
439 * Refresh data for the given traces
442 * The traces to refresh
444 public void refreshData(ITimeGraphEntry
[] traces
) {
445 fItemData
.refreshData(traces
);
450 * Refresh the links (arrows) of this widget
452 * @param events The link event list
454 public void refreshArrows(List
<ILinkEvent
> events
) {
455 fItemData
.refreshArrows(events
);
459 * Get the links (arrows) of this widget
461 * @return The unmodifiable link event list
465 public List
<ILinkEvent
> getArrows() {
466 return Collections
.unmodifiableList(fItemData
.fLinks
);
469 boolean ensureVisibleItem(int idx
, boolean redraw
) {
470 boolean changed
= false;
473 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
474 if (fItemData
.fExpandedItems
[index
].fSelected
) {
479 if (index
>= fItemData
.fExpandedItems
.length
) {
482 if (index
< fTopIndex
) {
489 int page
= countPerPage();
490 if (index
>= fTopIndex
+ page
) {
491 setTopIndex(index
- page
+ 1);
502 * Assign the given index as the top one
507 public void setTopIndex(int idx
) {
508 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
509 index
= Math
.max(0, index
);
515 * Set the top index so that the requested element is at the specified
519 * the time graph entry to be positioned
521 * the requested y-coordinate
524 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
525 Item item
= fItemData
.fItemMap
.get(entry
);
526 if (item
== null || item
.fExpandedIndex
== -1) {
529 int index
= item
.fExpandedIndex
;
530 Rectangle itemRect
= getItemRect(getClientArea(), index
);
531 int delta
= itemRect
.y
+ itemRect
.height
- y
;
532 int topIndex
= getItemIndexAtY(delta
);
533 if (topIndex
!= -1) {
534 setTopIndex(topIndex
);
539 setTopIndex(getExpandedElementCount());
545 * Sets the auto-expand level to be used for new entries discovered when
546 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
547 * . The value 0 means that there is no auto-expand; 1 means that top-level
548 * entries are expanded, but not their children; 2 means that top-level
549 * entries are expanded, and their children, but not grand-children; and so
552 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
556 * non-negative level, or <code>ALL_LEVELS</code> to expand all
559 public void setAutoExpandLevel(int level
) {
560 fAutoExpandLevel
= level
;
564 * Returns the auto-expand level.
566 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
567 * the tree are expanded automatically
568 * @see #setAutoExpandLevel
570 public int getAutoExpandLevel() {
571 return fAutoExpandLevel
;
575 * Get the expanded state of a given entry.
579 * @return true if the entry is expanded, false if collapsed
582 public boolean getExpandedState(ITimeGraphEntry entry
) {
583 Item item
= fItemData
.fItemMap
.get(entry
);
584 return (item
!= null ? item
.fExpanded
: false);
588 * Set the expanded state of a given entry
593 * True if expanded, false if collapsed
595 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
596 Item item
= fItemData
.findItem(entry
);
597 if (item
!= null && item
.fExpanded
!= expanded
) {
598 item
.fExpanded
= expanded
;
599 fItemData
.updateExpandedItems();
605 * Set the expanded state of a given entry to certain relative level.
606 * It will call fireTreeEvent() for each changed entry. At the end
607 * it will call redraw().
612 * level to expand to or negative for all levels
614 * True if expanded, false if collapsed
616 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
617 setExpandedStateInt(entry
, level
, expanded
);
622 * Set the expanded state of a given entry and its children to the first
623 * level that has one collapsed entry.
628 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
629 int level
= findExpandedLevel(entry
);
631 setExpandedStateInt(entry
, level
, true);
637 * Inner class for finding relative level with at least one
640 private class SearchNode
{
641 SearchNode(ITimeGraphEntry e
, int l
) {
645 ITimeGraphEntry entry
;
650 * Finds the relative level with at least one collapsed entry.
654 * @return the found level or -1 if all levels are already expanded.
656 private int findExpandedLevel(ITimeGraphEntry entry
) {
657 Queue
<SearchNode
> queue
= new LinkedList
<>();
658 SearchNode root
= new SearchNode(entry
, 0);
659 SearchNode node
= root
;
662 while (!queue
.isEmpty()) {
663 node
= queue
.remove();
664 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
667 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
668 if (e
.hasChildren()) {
669 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
678 * Set the expanded state of a given entry to certain relative level.
679 * It will call fireTreeEvent() for each changed entry. No redraw is done.
684 * level to expand to or negative for all levels
686 * True if expanded, false if collapsed
688 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
690 if ((level
> 0) || (level
< 0)) {
692 if (entry
.hasChildren()) {
693 for (ITimeGraphEntry e
: entry
.getChildren()) {
694 setExpandedStateInt(e
, level
, expanded
);
698 Item item
= fItemData
.findItem(entry
);
699 if (item
!= null && item
.fExpanded
!= expanded
) {
700 item
.fExpanded
= expanded
;
701 fItemData
.updateExpandedItems();
702 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
707 * Collapses all nodes of the viewer's tree, starting with the root.
709 public void collapseAll() {
710 for (Item item
: fItemData
.fItems
) {
711 item
.fExpanded
= false;
713 fItemData
.updateExpandedItems();
718 * Expands all nodes of the viewer's tree, starting with the root.
720 public void expandAll() {
721 for (Item item
: fItemData
.fItems
) {
722 item
.fExpanded
= true;
724 fItemData
.updateExpandedItems();
729 * Add a tree listener
732 * The listener to add
734 public void addTreeListener(ITimeGraphTreeListener listener
) {
735 if (!fTreeListeners
.contains(listener
)) {
736 fTreeListeners
.add(listener
);
741 * Remove a tree listener
744 * The listener to remove
746 public void removeTreeListener(ITimeGraphTreeListener listener
) {
747 if (fTreeListeners
.contains(listener
)) {
748 fTreeListeners
.remove(listener
);
753 * Tree event callback
758 * The expanded state (true for expanded, false for collapsed)
760 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
761 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
762 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
764 listener
.treeExpanded(event
);
766 listener
.treeCollapsed(event
);
772 * Add a menu listener on {@link ITimeGraphEntry}s
775 * The listener to add
777 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
778 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
779 fTimeGraphEntryMenuListeners
.add(listener
);
784 * Remove a menu listener on {@link ITimeGraphEntry}s
787 * The listener to remove
789 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
790 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
791 fTimeGraphEntryMenuListeners
.remove(listener
);
796 * Menu event callback on {@link ITimeGraphEntry}s
799 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
801 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
802 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
803 listener
.menuDetected(event
);
808 * Add a menu listener on {@link ITimeEvent}s
811 * The listener to add
813 public void addTimeEventMenuListener(MenuDetectListener listener
) {
814 if (!fTimeEventMenuListeners
.contains(listener
)) {
815 fTimeEventMenuListeners
.add(listener
);
820 * Remove a menu listener on {@link ITimeEvent}s
823 * The listener to remove
825 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
826 if (fTimeEventMenuListeners
.contains(listener
)) {
827 fTimeEventMenuListeners
.remove(listener
);
832 * Menu event callback on {@link ITimeEvent}s
835 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
837 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
838 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
839 listener
.menuDetected(event
);
844 public boolean setFocus() {
845 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
846 fHasNamespaceFocus
= true;
848 return super.setFocus();
852 * Returns the current selection for this time graph. If a time graph entry
853 * is selected, it will be the first element in the selection. If a time
854 * event is selected, it will be the second element in the selection.
856 * @return the current selection
859 public ISelection
getSelection() {
860 ITimeGraphEntry entry
= getSelectedTrace();
861 if (null != entry
&& null != fTimeProvider
) {
862 long selectedTime
= fTimeProvider
.getSelectionBegin();
863 ITimeEvent event
= Utils
.findEvent(entry
, selectedTime
, 0);
865 return new StructuredSelection(entry
);
867 return new StructuredSelection(new Object
[] { entry
, event
});
869 return StructuredSelection
.EMPTY
;
873 * Get the selection object
875 * @return The selection
877 public ISelection
getSelectionTrace() {
878 ITimeGraphEntry entry
= getSelectedTrace();
880 return new StructuredSelection(entry
);
882 return StructuredSelection
.EMPTY
;
886 * Enable/disable one of the traces in the model
889 * 1 to enable it, -1 to disable. The method returns immediately
890 * if another value is used.
892 public void selectTrace(int n
) {
893 if ((n
!= 1) && (n
!= -1)) {
897 boolean changed
= false;
898 int lastSelection
= -1;
899 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
900 Item item
= fItemData
.fExpandedItems
[i
];
901 if (item
.fSelected
) {
903 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
904 item
.fSelected
= false;
905 item
= fItemData
.fExpandedItems
[i
+ 1];
906 item
.fSelected
= true;
908 } else if ((-1 == n
) && (i
> 0)) {
909 item
.fSelected
= false;
910 item
= fItemData
.fExpandedItems
[i
- 1];
911 item
.fSelected
= true;
918 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
919 Item item
= fItemData
.fExpandedItems
[0];
920 item
.fSelected
= true;
925 ensureVisibleItem(-1, false);
927 fireSelectionChanged();
935 * 1 for next event, -1 for previous event
937 * true to extend selection range, false for single selection
940 public void selectEvent(int n
, boolean extend
) {
941 if (null == fTimeProvider
) {
944 ITimeGraphEntry trace
= getSelectedTrace();
948 long selectedTime
= fTimeProvider
.getSelectionEnd();
949 long endTime
= fTimeProvider
.getMaxTime();
950 ITimeEvent nextEvent
;
951 if (n
== -1 && selectedTime
> endTime
) {
952 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
954 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
956 if (null == nextEvent
&& n
== -1) {
957 nextEvent
= Utils
.getFirstEvent(trace
);
959 if (null != nextEvent
) {
960 long nextTime
= nextEvent
.getTime();
961 // If last event detected e.g. going back or not moving to a next
963 if (nextTime
<= selectedTime
&& n
== 1) {
964 // Select to the end of this last event
965 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
966 // but not beyond the end of the trace
967 if (nextTime
> endTime
) {
970 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
971 // for previous event go to its end time unless we were already there
972 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
975 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
977 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
979 fireSelectionChanged();
982 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
984 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
986 fireSelectionChanged();
988 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
992 * Select the next event
995 * true to extend selection range, false for single selection
998 public void selectNextEvent(boolean extend
) {
999 selectEvent(1, extend
);
1000 // Notify if visible time window has been adjusted
1001 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1005 * Select the previous event
1008 * true to extend selection range, false for single selection
1011 public void selectPrevEvent(boolean extend
) {
1012 selectEvent(-1, extend
);
1013 // Notify if visible time window has been adjusted
1014 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1018 * Select the next trace
1020 public void selectNextTrace() {
1025 * Select the previous trace
1027 public void selectPrevTrace() {
1032 * Scroll left or right by one half window size
1035 * true to scroll left, false to scroll right
1037 public void horizontalScroll(boolean left
) {
1038 long time0
= fTimeProvider
.getTime0();
1039 long time1
= fTimeProvider
.getTime1();
1040 long timeMin
= fTimeProvider
.getMinTime();
1041 long timeMax
= fTimeProvider
.getMaxTime();
1042 long range
= time1
- time0
;
1046 long increment
= Math
.max(1, range
/ 2);
1048 time0
= Math
.max(time0
- increment
, timeMin
);
1049 time1
= time0
+ range
;
1051 time1
= Math
.min(time1
+ increment
, timeMax
);
1052 time0
= time1
- range
;
1054 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1058 * Zoom based on mouse cursor location with mouse scrolling
1060 * @param zoomIn true to zoom in, false to zoom out
1062 public void zoom(boolean zoomIn
) {
1063 int globalX
= getDisplay().getCursorLocation().x
;
1064 Point p
= toControl(globalX
, 0);
1065 int nameSpace
= fTimeProvider
.getNameSpace();
1066 int timeSpace
= fTimeProvider
.getTimeSpace();
1067 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1068 long time0
= fTimeProvider
.getTime0();
1069 long time1
= fTimeProvider
.getTime1();
1070 long interval
= time1
- time0
;
1071 if (interval
== 0) {
1073 } // to allow getting out of single point interval
1076 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1078 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1080 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1081 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1082 long newTime1
= newTime0
+ newInterval
;
1083 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1087 * zoom in using single click
1089 public void zoomIn() {
1090 long prevTime0
= fTimeProvider
.getTime0();
1091 long prevTime1
= fTimeProvider
.getTime1();
1092 long prevRange
= prevTime1
- prevTime0
;
1093 if (prevRange
== 0) {
1096 ITimeDataProvider provider
= fTimeProvider
;
1097 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1098 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1099 selTime
= (prevTime0
+ prevTime1
) / 2;
1101 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1102 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1104 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1106 if (inaccuracy
> 0 && inaccuracy
< 100) {
1107 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1111 long min
= fTimeProvider
.getMinTimeInterval();
1112 if ((time1
- time0
) < min
) {
1113 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1114 time1
= time0
+ min
;
1117 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1121 * zoom out using single click
1123 public void zoomOut() {
1124 long prevTime0
= fTimeProvider
.getTime0();
1125 long prevTime1
= fTimeProvider
.getTime1();
1126 ITimeDataProvider provider
= fTimeProvider
;
1127 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1128 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1129 selTime
= (prevTime0
+ prevTime1
) / 2;
1131 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1132 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1134 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1135 if (inaccuracy
> 0 && inaccuracy
< 100) {
1136 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1140 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1147 * true to zoom in, false to zoom out
1150 public void verticalZoom(boolean zoomIn
) {
1152 fHeightAdjustment
++;
1154 fHeightAdjustment
--;
1156 fItemData
.refreshData();
1161 * Reset the vertical zoom to default.
1165 public void resetVerticalZoom() {
1166 fHeightAdjustment
= 0;
1167 fItemData
.refreshData();
1172 * Set the grid lines visibility. The default is true.
1175 * true to show the grid lines, false otherwise
1178 public void setGridLinesVisible(boolean visible
) {
1179 fGridLinesVisible
= visible
;
1183 * Get the grid lines visibility.
1185 * @return true if the grid lines are visible, false otherwise
1188 public boolean getGridLinesVisible() {
1189 return fGridLinesVisible
;
1193 * Set the grid line color. The default is SWT.COLOR_GRAY.
1196 * the grid line color
1199 public void setGridLineColor(Color color
) {
1200 fGridLineColor
= color
;
1204 * Get the grid line color.
1206 * @return the grid line color
1209 public Color
getGridLineColor() {
1210 return fGridLineColor
;
1214 * Set the markers list.
1217 * The markers list, or null
1220 public void setMarkers(List
<IMarkerEvent
> markers
) {
1225 * Get the markers list.
1227 * @return The markers list, or null
1230 public List
<IMarkerEvent
> getMarkers() {
1235 * Set the markers visibility. The default is true.
1238 * true to show the markers, false otherwise
1241 public void setMarkersVisible(boolean visible
) {
1242 fMarkersVisible
= visible
;
1246 * Get the markers visibility.
1248 * @return true if the markers are visible, false otherwise
1251 public boolean getMarkersVisible() {
1252 return fMarkersVisible
;
1258 * @param hideArrows true to hide arrows
1260 public void hideArrows(boolean hideArrows
) {
1261 fHideArrows
= hideArrows
;
1265 * Follow the arrow forward
1268 * true to extend selection range, false for single selection
1271 public void followArrowFwd(boolean extend
) {
1272 ITimeGraphEntry trace
= getSelectedTrace();
1273 if (trace
== null) {
1276 long selectedTime
= fTimeProvider
.getSelectionEnd();
1277 for (ILinkEvent link
: fItemData
.fLinks
) {
1278 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1279 selectItem(link
.getDestinationEntry(), false);
1280 if (link
.getDuration() != 0) {
1282 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1284 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1286 // Notify if visible time window has been adjusted
1287 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1289 fireSelectionChanged();
1290 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1294 selectNextEvent(extend
);
1298 * Follow the arrow backward
1301 * true to extend selection range, false for single selection
1304 public void followArrowBwd(boolean extend
) {
1305 ITimeGraphEntry trace
= getSelectedTrace();
1306 if (trace
== null) {
1309 long selectedTime
= fTimeProvider
.getSelectionEnd();
1310 for (ILinkEvent link
: fItemData
.fLinks
) {
1311 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1312 selectItem(link
.getEntry(), false);
1313 if (link
.getDuration() != 0) {
1315 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1317 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1319 // Notify if visible time window has been adjusted
1320 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1322 fireSelectionChanged();
1323 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1327 selectPrevEvent(extend
);
1331 * Return the currently selected trace
1333 * @return The entry matching the trace
1335 public ITimeGraphEntry
getSelectedTrace() {
1336 ITimeGraphEntry trace
= null;
1337 int idx
= getSelectedIndex();
1339 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1345 * Retrieve the index of the currently selected item
1349 public int getSelectedIndex() {
1351 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1352 Item item
= fItemData
.fExpandedItems
[i
];
1353 if (item
.fSelected
) {
1361 boolean toggle(int idx
) {
1362 boolean toggled
= false;
1363 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1364 Item item
= fItemData
.fExpandedItems
[idx
];
1365 if (item
.fHasChildren
) {
1366 item
.fExpanded
= !item
.fExpanded
;
1367 fItemData
.updateExpandedItems();
1370 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1377 * Gets the index of the item at the given location.
1381 * @return the index of the item at the given location, of -1 if none.
1383 protected int getItemIndexAtY(int y
) {
1386 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1387 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1393 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1394 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1403 boolean isOverSplitLine(int x
) {
1404 if (x
< 0 || null == fTimeProvider
) {
1407 int nameWidth
= fTimeProvider
.getNameSpace();
1408 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1411 boolean isOverTimeSpace(int x
, int y
) {
1412 Point size
= getSize();
1413 return x
>= fTimeProvider
.getNameSpace() && x
< size
.x
&& y
>= 0 && y
< size
.y
;
1416 * Gets the {@link ITimeGraphEntry} at the given location.
1419 * a point in the widget
1420 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1424 public ITimeGraphEntry
getEntry(Point pt
) {
1425 int idx
= getItemIndexAtY(pt
.y
);
1426 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1430 * Return the arrow event closest to the given point that is no further than
1431 * a maximum distance.
1434 * a point in the widget
1435 * @return The closest arrow event, or null if there is none close enough.
1437 protected ILinkEvent
getArrow(Point pt
) {
1441 ILinkEvent linkEvent
= null;
1442 double minDistance
= Double
.MAX_VALUE
;
1443 for (ILinkEvent event
: fItemData
.fLinks
) {
1444 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1448 int x2
= x1
+ rect
.width
;
1449 int y2
= y1
+ rect
.height
;
1450 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1451 if (minDistance
> d
) {
1457 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1464 public int getXForTime(long time
) {
1465 if (null == fTimeProvider
) {
1468 long time0
= fTimeProvider
.getTime0();
1469 long time1
= fTimeProvider
.getTime1();
1470 int width
= getSize().x
;
1471 int nameSpace
= fTimeProvider
.getNameSpace();
1472 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1473 int x
= SaturatedArithmetic
.add(getBounds().x
+ nameSpace
, (int) ((time
- time0
) * pixelsPerNanoSec
));
1478 public long getTimeAtX(int coord
) {
1479 if (null == fTimeProvider
) {
1483 Point size
= getSize();
1484 long time0
= fTimeProvider
.getTime0();
1485 long time1
= fTimeProvider
.getTime1();
1486 int nameWidth
= fTimeProvider
.getNameSpace();
1487 final int x
= coord
- nameWidth
;
1488 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1489 if (x
>= 0 && size
.x
>= nameWidth
) {
1490 if (time1
- time0
> timeWidth
) {
1491 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1492 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1494 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1495 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1501 void selectItem(int idx
, boolean addSelection
) {
1502 boolean changed
= false;
1504 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1505 Item item
= fItemData
.fExpandedItems
[idx
];
1506 changed
= !item
.fSelected
;
1507 item
.fSelected
= true;
1510 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1511 Item item
= fItemData
.fExpandedItems
[i
];
1512 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1515 item
.fSelected
= i
== idx
;
1518 changed
|= ensureVisibleItem(idx
, true);
1525 * Callback for item selection
1528 * The entry matching the trace
1529 * @param addSelection
1530 * If the selection is added or removed
1532 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1533 int idx
= fItemData
.findItemIndex(trace
);
1534 selectItem(idx
, addSelection
);
1538 * Retrieve the number of entries shown per page.
1542 public int countPerPage() {
1543 int height
= getSize().y
;
1546 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1547 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1548 if (ySum
>= height
) {
1553 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1554 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1555 if (ySum
>= height
) {
1564 * Get the index of the top element
1568 public int getTopIndex() {
1573 * Get the number of expanded (visible) items
1575 * @return The count of expanded (visible) items
1577 public int getExpandedElementCount() {
1578 return fItemData
.fExpandedItems
.length
;
1582 * Get an array of all expanded (visible) elements
1584 * @return The expanded (visible) elements
1586 public ITimeGraphEntry
[] getExpandedElements() {
1587 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1588 for (Item item
: fItemData
.fExpandedItems
) {
1589 elements
.add(item
.fEntry
);
1591 return elements
.toArray(new ITimeGraphEntry
[0]);
1595 * Get the expanded (visible) element at the specified index.
1599 * @return The expanded (visible) element or null if out of range
1602 public ITimeGraphEntry
getExpandedElement(int index
) {
1603 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1606 return fItemData
.fExpandedItems
[index
].fEntry
;
1610 * Get the bounds of the specified entry
1612 * @param entry the time graph entry
1613 * @return the bounds of the entry, or null if the entry is not visible
1616 public Rectangle
getItemBounds(ITimeGraphEntry entry
) {
1617 int idx
= fItemData
.findItemIndex(entry
);
1619 return getItemRect(getBounds(), idx
);
1624 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1625 Rectangle rect
= getItemRect(bounds
, idx
);
1626 rect
.width
= nameWidth
;
1630 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1631 Rectangle rect
= getItemRect(bounds
, idx
);
1632 rect
.x
+= nameWidth
;
1633 rect
.width
-= nameWidth
;
1637 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1639 if (idx
>= fTopIndex
) {
1640 for (int i
= fTopIndex
; i
< idx
; i
++) {
1641 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1644 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1645 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1648 int y
= bounds
.y
+ ySum
;
1649 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1650 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1654 void paint(Rectangle bounds
, PaintEvent e
) {
1657 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1661 fIdealNameSpace
= 0;
1662 int nameSpace
= fTimeProvider
.getNameSpace();
1664 // draw the background layer
1665 drawBackground(bounds
, nameSpace
, gc
);
1667 // draw the grid lines
1668 drawGridLines(bounds
, gc
);
1670 // draw the background markers
1671 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1674 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1676 // draw the foreground markers
1677 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1679 // draw the links (arrows)
1680 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1682 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1684 int alpha
= gc
.getAlpha();
1687 long time0
= fTimeProvider
.getTime0();
1688 long time1
= fTimeProvider
.getTime1();
1689 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1690 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1691 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1692 int x0
= SaturatedArithmetic
.add(bounds
.x
+ nameSpace
, (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
));
1693 int x1
= SaturatedArithmetic
.add(bounds
.x
+ nameSpace
, (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
));
1695 // draw selection lines
1696 if (fDragState
!= DRAG_SELECTION
) {
1697 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1698 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1699 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1702 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1703 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1708 // draw selection background
1709 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1710 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1711 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1712 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1714 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1715 } else if (x0
- x1
> 1) {
1716 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1720 // draw drag selection background
1721 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1722 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1723 if (fDragX0
< fDragX
) {
1724 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1725 } else if (fDragX0
> fDragX
) {
1726 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1731 if (DRAG_SPLIT_LINE
== fDragState
) {
1732 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1733 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1734 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1735 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1736 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1737 if (fDragX
!= fDragX0
) {
1738 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1740 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1741 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1742 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1743 if (fDragX
!= fDragX0
) {
1744 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1746 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1747 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1748 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1755 * Draw the background layer. Fills the background of the control's name
1756 * space and states space, updates the background of items if necessary,
1757 * and draws the item's name text and middle line.
1760 * The bounds of the control
1762 * The name space width
1767 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1768 // draw empty name space background
1769 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1770 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1772 // draw empty states space background
1773 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1774 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1776 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1777 Rectangle itemRect
= getItemRect(bounds
, i
);
1778 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1781 Item item
= fItemData
.fExpandedItems
[i
];
1782 // draw the background of selected item and items with no time events
1783 if (! item
.fEntry
.hasTimeEvents()) {
1784 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1785 gc
.fillRectangle(itemRect
);
1786 } else if (item
.fSelected
) {
1787 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1788 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1789 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1790 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1792 // draw the name and middle line
1793 if (! item
.fEntry
.hasTimeEvents()) {
1794 drawName(item
, itemRect
, gc
);
1796 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1797 drawName(item
, nameRect
, gc
);
1798 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1799 drawMidLine(rect
, gc
);
1805 * Draw the grid lines
1808 * The bounds of the control
1813 public void drawGridLines(Rectangle bounds
, GC gc
) {
1814 if (!fGridLinesVisible
) {
1817 gc
.setForeground(fGridLineColor
);
1818 gc
.setAlpha(fGridLineColor
.getAlpha());
1819 for (int x
: fTimeGraphScale
.getTickList()) {
1820 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1829 * The rectangle of the area
1830 * @param timeProvider
1833 * The list of markers
1835 * true to draw the foreground markers, false otherwise
1837 * The width reserved for the names
1839 * Reference to the SWT GC object
1842 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1843 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1846 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1847 /* the list can grow concurrently but cannot shrink */
1848 for (int i
= 0; i
< markers
.size(); i
++) {
1849 IMarkerEvent marker
= markers
.get(i
);
1850 if (marker
.isForeground() == foreground
) {
1851 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1854 gc
.setClipping((Rectangle
) null);
1858 * Draw a single marker
1863 * The bounds of the control
1864 * @param timeProvider
1867 * The width reserved for the name
1869 * Reference to the SWT GC object
1872 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1873 Rectangle rect
= Utils
.clone(bounds
);
1874 if (marker
.getEntry() != null) {
1875 int index
= fItemData
.findItemIndex(marker
.getEntry());
1879 rect
= getStatesRect(bounds
, index
, nameSpace
);
1880 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1884 int x0
= getXForTime(marker
.getTime());
1885 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1886 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1889 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1890 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1892 Color color
= getColorScheme().getColor(marker
.getColor());
1893 gc
.setBackground(color
);
1894 gc
.setAlpha(color
.getAlpha());
1895 gc
.fillRectangle(rect
);
1897 String label
= marker
.getLabel();
1898 if (label
!= null && marker
.getEntry() != null) {
1899 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1900 gc
.setForeground(color
);
1901 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1906 * Draw many items at once
1909 * The bounds of the control
1910 * @param timeProvider
1913 * The array items to draw
1915 * The index of the first element to draw
1917 * The name space width
1921 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1922 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1923 for (int i
= topIndex
; i
< items
.length
; i
++) {
1924 Item item
= items
[i
];
1925 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1935 * The bounds of the control
1936 * @param timeProvider
1939 * The expanded item index
1941 * The name space width
1945 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1946 Rectangle itemRect
= getItemRect(bounds
, i
);
1947 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1951 ITimeGraphEntry entry
= item
.fEntry
;
1952 long time0
= timeProvider
.getTime0();
1953 long time1
= timeProvider
.getTime1();
1954 long selectedTime
= fTimeProvider
.getSelectionEnd();
1956 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1957 if (rect
.isEmpty() || (time1
<= time0
)) {
1958 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1962 boolean selected
= item
.fSelected
;
1963 // K pixels per second
1964 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1966 if (item
.fEntry
.hasTimeEvents()) {
1967 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1968 fillSpace(rect
, gc
, selected
);
1970 int margins
= getMarginForHeight(rect
.height
);
1971 int height
= rect
.height
- margins
;
1972 int topMargin
= (margins
+ 1) / 2;
1973 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1975 /* Set the font for this item */
1976 setFontForHeight(height
, gc
);
1978 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1979 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1982 while (iterator
.hasNext()) {
1983 ITimeEvent event
= iterator
.next();
1984 int x
= SaturatedArithmetic
.add(rect
.x
, (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
));
1985 int xEnd
= SaturatedArithmetic
.add(rect
.x
, (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
));
1986 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1987 // event is out of bounds
1990 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1991 stateRect
.x
= Math
.max(rect
.x
, x
);
1992 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1993 if (stateRect
.x
== lastX
) {
1994 stateRect
.width
-= 1;
1995 if (stateRect
.width
> 0) {
1996 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1997 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
2001 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
2002 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
2003 lastX
= stateRect
.x
;
2006 gc
.setClipping((Rectangle
) null);
2008 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
2015 * The bounds of the control
2016 * @param timeProvider
2019 * The list of link events
2021 * The name space width
2025 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
2026 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2030 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2031 /* the list can grow concurrently but cannot shrink */
2032 for (int i
= 0; i
< links
.size(); i
++) {
2033 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2035 gc
.setClipping((Rectangle
) null);
2039 * Draws a link type event
2042 * The link event to draw
2044 * The bounds of the control
2045 * @param timeProvider
2048 * The name space width
2052 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2053 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2056 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2057 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2058 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2060 if ((srcIndex
== -1) || (destIndex
== -1)) {
2064 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2065 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2067 int x0
= getXForTime(event
.getTime());
2068 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2070 // limit the x-coordinates to prevent integer overflow in calculations
2071 // and also GC.drawLine doesn't draw properly with large coordinates
2072 final int limit
= Integer
.MAX_VALUE
/ 1024;
2073 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2074 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2076 int y0
= src
.y
+ src
.height
/ 2;
2077 int y1
= dst
.y
+ dst
.height
/ 2;
2078 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2087 * Time event for which we're drawing the arrow
2089 * The arrow rectangle
2092 * @return true if the arrow was drawn
2094 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2095 Rectangle rect
, GC gc
) {
2100 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2104 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2107 Color stateColor
= null;
2108 if (colorIdx
< fEventColorMap
.length
) {
2109 stateColor
= fEventColorMap
[colorIdx
];
2111 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2114 gc
.setForeground(stateColor
);
2115 gc
.setBackground(stateColor
);
2117 /* Draw the arrow */
2118 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2119 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2122 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2127 * @author Francis Giraldeau
2130 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2132 * The algorithm was taken from this site, not the code itself
2134 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2137 double cos
= 0.9510;
2138 double sin
= 0.3090;
2139 long lenx
= x1
- x0
;
2140 long leny
= y1
- y0
;
2141 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2143 double dx
= factor
* lenx
/ len
;
2144 double dy
= factor
* leny
/ len
;
2145 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2146 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2147 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2148 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2149 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2150 gc
.fillPolygon(arrow
);
2154 * Draw the name of an item.
2159 * The bounds of the item's name space
2163 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2164 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2166 // No name to be drawn
2167 if (fTimeProvider
.getNameSpace() == 0) {
2171 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2172 setFontForHeight(height
, gc
);
2174 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2175 if (item
.fHasChildren
) {
2176 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2177 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2178 Rectangle rect
= Utils
.clone(bounds
);
2179 rect
.x
+= leftMargin
;
2180 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2181 rect
.width
= EXPAND_SIZE
;
2182 rect
.height
= EXPAND_SIZE
;
2183 gc
.fillRectangle(rect
);
2184 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2185 int midy
= rect
.y
+ rect
.height
/ 2;
2186 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2187 if (!item
.fExpanded
) {
2188 int midx
= rect
.x
+ rect
.width
/ 2;
2189 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2192 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2194 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2197 int imgHeight
= img
.getImageData().height
;
2198 int imgWidth
= img
.getImageData().width
;
2200 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2201 gc
.drawImage(img
, x
, y
);
2202 leftMargin
+= imgWidth
+ MARGIN
;
2204 String name
= item
.fName
;
2205 Point size
= gc
.stringExtent(name
);
2206 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2207 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2209 if (hasTimeEvents
) {
2210 // cut long string with "..."
2211 int width
= bounds
.width
- leftMargin
;
2213 while (size
.x
> width
&& name
.length() > 1) {
2215 name
= name
.substring(0, name
.length() - 1);
2216 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2219 name
+= "..."; //$NON-NLS-1$
2222 Rectangle rect
= Utils
.clone(bounds
);
2223 rect
.x
+= leftMargin
;
2224 rect
.width
-= leftMargin
;
2226 if (rect
.width
> 0) {
2227 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2228 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2229 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2230 leftMargin
+= textWidth
+ MARGIN
;
2232 if (hasTimeEvents
) {
2234 rect
.x
= bounds
.x
+ leftMargin
;
2236 rect
.width
= bounds
.width
- rect
.x
;
2237 drawMidLine(rect
, gc
);
2243 * Draw the state (color fill)
2248 * Time event for which we're drawing the state
2250 * The state rectangle
2254 * Is this time event currently selected (so it appears
2256 * @param timeSelected
2257 * Is the timestamp currently selected
2258 * @return true if the state was drawn
2260 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2261 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2263 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2264 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2267 boolean visible
= rect
.width
== 0 ?
false : true;
2268 rect
.width
= Math
.max(1, rect
.width
);
2269 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2270 gc
.setForeground(black
);
2272 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2274 // Only draw the top and bottom borders
2275 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2276 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2277 if (rect
.width
== 1) {
2278 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2281 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2284 Color stateColor
= null;
2285 if (colorIdx
< fEventColorMap
.length
) {
2286 stateColor
= fEventColorMap
[colorIdx
];
2291 boolean reallySelected
= timeSelected
&& selected
;
2292 // fill all rect area
2293 gc
.setBackground(stateColor
);
2295 gc
.fillRectangle(rect
);
2296 } else if (fBlendSubPixelEvents
) {
2298 gc
.fillRectangle(rect
);
2302 if (reallySelected
) {
2303 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2304 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2307 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2309 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2314 * Fill an item's states rectangle
2317 * The states rectangle
2321 * true if the item is selected
2323 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2324 /* Nothing to draw */
2328 * Draw a line at the middle height of a rectangle
2335 private void drawMidLine(Rectangle rect
, GC gc
) {
2336 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2337 int midy
= rect
.y
+ rect
.height
/ 2;
2338 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2341 private static int getMarginForHeight(int height
) {
2343 * State rectangle is smaller than the item bounds when height is > 4.
2344 * Don't use any margin if the height is below or equal that threshold.
2345 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2346 * pixels for the state height, but with a minimum margin of 1.
2348 final int MARGIN_THRESHOLD
= 4;
2349 final int PREFERRED_HEIGHT
= 13;
2350 final int MIN_MARGIN
= 1;
2351 final int MAX_MARGIN
= 6;
2352 return height
<= MARGIN_THRESHOLD ?
0 :
2353 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2356 private void setFontForHeight(int pixels
, GC gc
) {
2357 /* convert font height from pixels to points */
2358 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2359 Font font
= fFonts
.get(height
);
2361 FontData fontData
= gc
.getFont().getFontData()[0];
2362 fontData
.setHeight(height
);
2363 font
= new Font(gc
.getDevice(), fontData
);
2364 fFonts
.put(height
, font
);
2370 public void keyTraversed(TraverseEvent e
) {
2371 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2377 public void keyPressed(KeyEvent e
) {
2379 if (fItemData
.fExpandedItems
.length
== 0) {
2382 if (SWT
.HOME
== e
.keyCode
) {
2384 } else if (SWT
.END
== e
.keyCode
) {
2385 idx
= fItemData
.fExpandedItems
.length
- 1;
2386 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2387 idx
= getSelectedIndex();
2390 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2393 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2394 idx
= getSelectedIndex();
2397 } else if (idx
> 0) {
2400 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2401 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2402 selectPrevEvent(extend
);
2403 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2404 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2405 selectNextEvent(extend
);
2406 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2407 int page
= countPerPage();
2408 idx
= getSelectedIndex();
2413 if (idx
>= fItemData
.fExpandedItems
.length
) {
2414 idx
= fItemData
.fExpandedItems
.length
- 1;
2416 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2417 int page
= countPerPage();
2418 idx
= getSelectedIndex();
2426 } else if (SWT
.CR
== e
.keyCode
) {
2427 idx
= getSelectedIndex();
2429 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2432 fireDefaultSelection();
2436 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2437 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2439 if (fVerticalZoomAlignEntry
!= null) {
2440 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2442 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2443 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2444 verticalZoom(false);
2445 if (fVerticalZoomAlignEntry
!= null) {
2446 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2448 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2449 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2450 resetVerticalZoom();
2451 if (fVerticalZoomAlignEntry
!= null) {
2452 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2454 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2455 if (fHasNamespaceFocus
) {
2456 ITimeGraphEntry entry
= getSelectedTrace();
2457 setExpandedState(entry
, 0, true);
2461 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2462 if (fHasNamespaceFocus
) {
2463 ITimeGraphEntry entry
= getSelectedTrace();
2464 if ((entry
!= null) && entry
.hasChildren()) {
2465 setExpandedState(entry
, -1, false);
2470 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2471 if (fHasNamespaceFocus
) {
2472 ITimeGraphEntry entry
= getSelectedTrace();
2473 if ((entry
!= null) && entry
.hasChildren()) {
2474 setExpandedStateLevel(entry
);
2479 selectItem(idx
, false);
2480 fireSelectionChanged();
2482 int x
= toControl(e
.display
.getCursorLocation()).x
;
2483 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2487 public void keyReleased(KeyEvent e
) {
2488 int x
= toControl(e
.display
.getCursorLocation()).x
;
2489 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2493 public void focusGained(FocusEvent e
) {
2496 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2500 public void focusLost(FocusEvent e
) {
2502 if (DRAG_NONE
!= fDragState
) {
2504 fDragState
= DRAG_NONE
;
2507 updateStatusLine(NO_STATUS
);
2511 * @return If the current view is focused
2513 public boolean isInFocus() {
2518 * Provide the possibility to control the wait cursor externally e.g. data
2519 * requests in progress
2521 * @param waitInd Should we wait indefinitely?
2523 public void waitCursor(boolean waitInd
) {
2524 // Update cursor as indicated
2526 setCursor(fWaitCursor
);
2532 private void updateCursor(int x
, int stateMask
) {
2533 // if Wait cursor not active, check for the need to change the cursor
2534 if (getCursor() == fWaitCursor
) {
2537 Cursor cursor
= null;
2538 if (fDragState
== DRAG_SPLIT_LINE
) {
2539 } else if (fDragState
== DRAG_SELECTION
) {
2540 cursor
= fResizeCursor
;
2541 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2542 cursor
= fDragCursor
;
2543 } else if (fDragState
== DRAG_ZOOM
) {
2544 cursor
= fZoomCursor
;
2545 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2546 cursor
= fDragCursor
;
2547 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2548 cursor
= fResizeCursor
;
2549 } else if (!isOverSplitLine(x
)) {
2550 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2551 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2552 int xBegin
= getXForTime(selectionBegin
);
2553 int xEnd
= getXForTime(selectionEnd
);
2554 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2555 cursor
= fResizeCursor
;
2558 if (getCursor() != cursor
) {
2564 * Update the status line following a change of selection.
2568 public void updateStatusLine() {
2569 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2572 private void updateStatusLine(int x
) {
2573 // use the time provider of the time graph scale for the status line
2574 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2575 if (fStatusLineManager
== null || null == tdp
||
2576 tdp
.getTime0() == tdp
.getTime1()) {
2579 TimeFormat tf
= tdp
.getTimeFormat();
2580 Resolution res
= Resolution
.NANOSEC
;
2581 StringBuilder message
= new StringBuilder();
2582 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2583 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2584 long time
= getTimeAtX(x
);
2586 if (tdp
instanceof ITimeDataProviderConverter
) {
2587 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2589 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2591 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2592 Utils
.formatTime(time
, tf
, res
)
2596 long selectionBegin
= tdp
.getSelectionBegin();
2597 long selectionEnd
= tdp
.getSelectionEnd();
2598 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2600 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2601 Utils
.formatTime(selectionBegin
, tf
, res
)
2603 if (selectionBegin
!= selectionEnd
) {
2604 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2606 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2607 Utils
.formatTime(selectionEnd
, tf
, res
),
2608 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2611 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2612 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2613 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2614 if (tdp
instanceof ITimeDataProviderConverter
) {
2615 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2616 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2618 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2620 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2621 Utils
.formatTime(time0
, tf
, res
),
2622 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2623 Utils
.formatTime(time
, tf
, res
),
2624 Utils
.formatDelta(time
- time0
, tf
, res
)
2627 fStatusLineManager
.setMessage(message
.toString());
2631 public void mouseMove(MouseEvent e
) {
2632 if (null == fTimeProvider
) {
2635 Point size
= getSize();
2636 if (DRAG_TRACE_ITEM
== fDragState
) {
2637 int nameWidth
= fTimeProvider
.getNameSpace();
2638 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2640 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2641 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2642 long time1
= fTime1bak
- timeDelta
;
2643 long maxTime
= fTimeProvider
.getMaxTime();
2644 if (time1
> maxTime
) {
2647 long time0
= time1
- (fTime1bak
- fTime0bak
);
2648 if (time0
< fTimeProvider
.getMinTime()) {
2649 time0
= fTimeProvider
.getMinTime();
2650 time1
= time0
+ (fTime1bak
- fTime0bak
);
2652 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2654 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2656 fTimeProvider
.setNameSpace(e
.x
);
2657 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2658 } else if (DRAG_SELECTION
== fDragState
) {
2659 if (fDragBeginMarker
) {
2660 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2662 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2665 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2666 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2667 } else if (DRAG_ZOOM
== fDragState
) {
2668 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2670 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2671 } else if (DRAG_NONE
== fDragState
) {
2672 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2673 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2676 fMouseOverSplitLine
= mouseOverSplitLine
;
2679 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2680 fHasNamespaceFocus
= false;
2682 fHasNamespaceFocus
= true;
2684 updateCursor(e
.x
, e
.stateMask
);
2685 updateStatusLine(e
.x
);
2689 public void mouseDoubleClick(MouseEvent e
) {
2690 if (null == fTimeProvider
) {
2693 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2694 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2695 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2696 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2697 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2700 fMouseOverSplitLine
= mouseOverSplitLine
;
2701 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2704 int idx
= getItemIndexAtY(e
.y
);
2706 selectItem(idx
, false);
2707 fireDefaultSelection();
2713 public void mouseDown(MouseEvent e
) {
2714 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2715 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2716 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2720 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2721 int nameSpace
= fTimeProvider
.getNameSpace();
2722 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2723 fDragState
= DRAG_SPLIT_LINE
;
2724 fDragButton
= e
.button
;
2727 fTime0bak
= fTimeProvider
.getTime0();
2728 fTime1bak
= fTimeProvider
.getTime1();
2730 updateCursor(e
.x
, e
.stateMask
);
2734 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2735 int nameSpace
= fTimeProvider
.getNameSpace();
2736 idx
= getItemIndexAtY(e
.y
);
2738 Item item
= fItemData
.fExpandedItems
[idx
];
2739 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2743 selectItem(idx
, false);
2744 fireSelectionChanged();
2746 selectItem(idx
, false); // clear selection
2747 fireSelectionChanged();
2749 long hitTime
= getTimeAtX(e
.x
);
2753 fDragState
= DRAG_SELECTION
;
2754 fDragBeginMarker
= false;
2755 fDragButton
= e
.button
;
2758 fDragTime0
= getTimeAtX(fDragX0
);
2759 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2760 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2761 int xBegin
= getXForTime(selectionBegin
);
2762 int xEnd
= getXForTime(selectionEnd
);
2763 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2764 long time
= getTimeAtX(e
.x
);
2765 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2766 fDragBeginMarker
= true;
2769 fDragTime0
= selectionEnd
;
2772 fDragTime0
= selectionBegin
;
2775 long time
= getTimeAtX(e
.x
);
2776 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2777 fDragBeginMarker
= true;
2780 fDragTime0
= selectionEnd
;
2781 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2783 fDragTime0
= selectionBegin
;
2786 fTime0bak
= fTimeProvider
.getTime0();
2787 fTime1bak
= fTimeProvider
.getTime1();
2789 updateCursor(e
.x
, e
.stateMask
);
2790 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2792 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2793 long hitTime
= getTimeAtX(e
.x
);
2796 fDragState
= DRAG_TRACE_ITEM
;
2797 fDragButton
= e
.button
;
2800 fTime0bak
= fTimeProvider
.getTime0();
2801 fTime1bak
= fTimeProvider
.getTime1();
2802 updateCursor(e
.x
, e
.stateMask
);
2804 } else if (3 == e
.button
) {
2805 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2807 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2809 fDragTime0
= getTimeAtX(fDragX0
);
2810 fDragState
= DRAG_ZOOM
;
2811 fDragButton
= e
.button
;
2813 updateCursor(e
.x
, e
.stateMask
);
2814 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2816 idx
= getItemIndexAtY(e
.y
);
2817 selectItem(idx
, false);
2818 fireSelectionChanged();
2824 public void mouseUp(MouseEvent e
) {
2825 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2826 if ((fDragState
== DRAG_ZOOM
) && isInDragZoomMargin()) {
2827 // Select entry and time event for single click
2828 long time
= getTimeAtX(fDragX0
);
2829 fTimeProvider
.setSelectionRangeNotify(time
, time
, false);
2830 int idx
= getItemIndexAtY(e
.y
);
2831 selectItem(idx
, false);
2832 fireSelectionChanged();
2834 menuDetected(fPendingMenuDetectEvent
);
2836 if (DRAG_NONE
!= fDragState
) {
2838 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2839 fDragState
= DRAG_NONE
;
2840 if (fDragX
!= fDragX0
) {
2841 fTimeProvider
.notifyStartFinishTime();
2843 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2844 fDragState
= DRAG_NONE
;
2846 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2847 fDragState
= DRAG_NONE
;
2848 if (fDragX
== fDragX0
) { // click without selecting anything
2849 long time
= getTimeAtX(e
.x
);
2850 fTimeProvider
.setSelectedTimeNotify(time
, false);
2852 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2853 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2854 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2857 fTimeGraphScale
.setDragRange(-1, -1);
2858 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2859 fDragState
= DRAG_NONE
;
2860 int nameWidth
= fTimeProvider
.getNameSpace();
2861 if ((Math
.max(fDragX
, fDragX0
) > nameWidth
) && !isInDragZoomMargin()) {
2862 long time0
= getTimeAtX(fDragX0
);
2863 long time1
= getTimeAtX(fDragX
);
2864 if (time0
< time1
) {
2865 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2867 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2872 fTimeGraphScale
.setDragRange(-1, -1);
2875 updateCursor(e
.x
, e
.stateMask
);
2876 updateStatusLine(e
.x
);
2880 public void mouseEnter(MouseEvent e
) {
2884 public void mouseExit(MouseEvent e
) {
2885 if (fMouseOverSplitLine
) {
2886 fMouseOverSplitLine
= false;
2889 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2893 public void mouseHover(MouseEvent e
) {
2897 public void mouseScrolled(MouseEvent e
) {
2898 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2903 * On some platforms the mouse scroll event is sent to the
2904 * control that has focus even if it is not under the cursor.
2905 * Handle the event only if over the time graph control.
2907 Point size
= getSize();
2908 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2909 if (!bounds
.contains(e
.x
, e
.y
)) {
2913 boolean horizontalZoom
= false;
2914 boolean horizontalScroll
= false;
2915 boolean verticalZoom
= false;
2916 boolean verticalScroll
= false;
2918 // over the time graph control
2919 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2920 verticalZoom
= true;
2921 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2922 // over the name space
2923 verticalScroll
= true;
2925 // over the state area
2926 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2927 // over the state area, CTRL pressed
2928 horizontalZoom
= true;
2929 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2930 // over the state area, SHIFT pressed
2931 horizontalScroll
= true;
2933 // over the state area, no modifier pressed
2934 verticalScroll
= true;
2938 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2939 verticalZoom(e
.count
> 0);
2940 if (fVerticalZoomAlignEntry
!= null) {
2941 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2943 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2945 } else if (horizontalScroll
) {
2946 horizontalScroll(e
.count
> 0);
2947 } else if (verticalScroll
){
2948 setTopIndex(getTopIndex() - e
.count
);
2953 * Get the vertical zoom alignment entry and position based on the current
2954 * selection. If there is no selection or if the selection is not visible,
2955 * return an alignment entry with a null time graph entry.
2957 * @return a map entry where the key is the selection's time graph entry and
2958 * the value is the center y-coordinate of that entry, or null
2960 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2961 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2962 if (alignEntry
!= null) {
2965 int index
= getSelectedIndex();
2966 if (index
== -1 || index
>= getExpandedElementCount()) {
2967 return new SimpleEntry
<>(null, 0);
2969 Rectangle bounds
= getClientArea();
2970 Rectangle itemRect
= getItemRect(bounds
, index
);
2971 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2972 /* selection is not visible */
2973 return new SimpleEntry
<>(null, 0);
2975 ITimeGraphEntry entry
= getExpandedElement(index
);
2976 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2977 return new SimpleEntry
<>(entry
, y
);
2981 * Get the vertical zoom alignment entry and position at the specified
2985 * the cursor y-coordinate
2986 * @return a map entry where the key is the time graph entry under the
2987 * cursor and the value is the cursor y-coordinate
2989 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2990 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2991 if (alignEntry
!= null) {
2994 int index
= getItemIndexAtY(y
);
2996 index
= getExpandedElementCount() - 1;
2998 ITimeGraphEntry entry
= getExpandedElement(index
);
2999 return new SimpleEntry
<>(entry
, y
);
3003 * Get the vertical zoom alignment entry and position if there is an ongoing
3004 * one and we are within the vertical zoom delay, or otherwise return null.
3006 * @return a map entry where the key is a time graph entry and the value is
3007 * a y-coordinate, or null
3009 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
3010 long currentTimeMillis
= System
.currentTimeMillis();
3011 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
3013 * If the vertical zoom is triggered repeatedly in a short amount of
3014 * time, use the initial event's entry and position.
3016 fVerticalZoomAlignTime
= currentTimeMillis
;
3017 return fVerticalZoomAlignEntry
;
3019 fVerticalZoomAlignTime
= currentTimeMillis
;
3024 public void handleEvent(Event event
) {
3025 if (event
.type
== SWT
.MouseWheel
) {
3026 // prevent horizontal scrolling when the mouse wheel is used to
3027 // scroll vertically or zoom
3033 public int getBorderWidth() {
3034 return fBorderWidth
;
3038 * Set the border width
3040 * @param borderWidth
3043 public void setBorderWidth(int borderWidth
) {
3044 this.fBorderWidth
= borderWidth
;
3048 * @return The current height of the header row
3050 public int getHeaderHeight() {
3051 return fHeaderHeight
;
3055 * Set the height of the header row
3057 * @param headerHeight
3060 public void setHeaderHeight(int headerHeight
) {
3061 this.fHeaderHeight
= headerHeight
;
3065 * @return The default height of regular item rows
3067 public int getItemHeight() {
3068 return fGlobalItemHeight
;
3072 * Set the default height of regular item rows.
3077 public void setItemHeight(int rowHeight
) {
3078 this.fGlobalItemHeight
= rowHeight
;
3079 for (Item item
: fItemData
.fItems
) {
3080 item
.fItemHeight
= rowHeight
;
3085 * Set the height of a specific item. Overrides the default item height.
3088 * A time graph entry
3091 * @return true if the height is successfully stored, false otherwise
3093 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3094 Item item
= fItemData
.findItem(entry
);
3096 item
.fItemHeight
= rowHeight
;
3103 * Set the minimum item width
3105 * @param width The minimum width
3107 public void setMinimumItemWidth(int width
) {
3108 this.fMinimumItemWidth
= width
;
3112 * @return The minimum item width
3114 public int getMinimumItemWidth() {
3115 return fMinimumItemWidth
;
3119 * Set whether all time events with a duration shorter than one pixel should
3120 * be blended in. If false, only the first such time event will be drawn and
3121 * the subsequent time events in the same pixel will be discarded. The
3122 * default value is false.
3125 * true if sub-pixel events should be blended, false otherwise.
3128 public void setBlendSubPixelEvents(boolean blend
) {
3129 fBlendSubPixelEvents
= blend
;
3133 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3134 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3135 fSelectionChangedListeners
.add(listener
);
3140 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3141 if (listener
!= null) {
3142 fSelectionChangedListeners
.remove(listener
);
3147 public void setSelection(ISelection selection
) {
3148 if (selection
instanceof IStructuredSelection
) {
3149 Object ob
= ((IStructuredSelection
) selection
).getFirstElement();
3150 if (ob
instanceof ITimeGraphEntry
) {
3151 selectItem((ITimeGraphEntry
) ob
, false);
3158 * @param filter The filter object to be attached to the view
3160 public void addFilter(@NonNull ViewerFilter filter
) {
3161 if (!fFilters
.contains(filter
)) {
3162 fFilters
.add(filter
);
3167 * @param filter The filter object to be attached to the view
3169 public void removeFilter(@NonNull ViewerFilter filter
) {
3170 fFilters
.remove(filter
);
3174 * Returns this control's filters.
3176 * @return an array of viewer filters
3179 public @NonNull ViewerFilter
[] getFilters() {
3180 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3184 * Sets the filters, replacing any previous filters.
3187 * an array of viewer filters, or null
3190 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3192 if (filters
!= null) {
3193 fFilters
.addAll(Arrays
.asList(filters
));
3198 public void colorSettingsChanged(StateItem
[] stateItems
) {
3199 /* Destroy previous colors from the resource manager */
3200 if (fEventColorMap
!= null) {
3201 for (Color color
: fEventColorMap
) {
3202 fResourceManager
.destroyColor(color
.getRGB());
3205 if (stateItems
!= null) {
3206 fEventColorMap
= new Color
[stateItems
.length
];
3207 for (int i
= 0; i
< stateItems
.length
; i
++) {
3208 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3211 fEventColorMap
= new Color
[] { };
3216 private class ItemData
{
3217 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3218 private Item
[] fExpandedItems
= new Item
[0];
3219 private Item
[] fItems
= new Item
[0];
3220 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3221 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3226 public Item
findItem(ITimeGraphEntry entry
) {
3227 return fItemMap
.get(entry
);
3230 public int findItemIndex(ITimeGraphEntry entry
) {
3231 Item item
= fItemMap
.get(entry
);
3235 return item
.fExpandedIndex
;
3238 public void refreshData() {
3239 ITimeGraphEntry selection
= getSelectedTrace();
3240 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3241 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3242 ITimeGraphEntry entry
= fRootEntries
[i
];
3243 refreshData(itemMap
, null, 0, entry
);
3246 fItems
= fItemMap
.values().toArray(new Item
[0]);
3247 updateExpandedItems();
3248 if (selection
!= null) {
3249 for (Item item
: fExpandedItems
) {
3250 if (item
.fEntry
== selection
) {
3251 item
.fSelected
= true;
3258 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3259 Item item
= new Item(entry
, entry
.getName(), level
);
3260 if (parent
!= null) {
3261 parent
.fChildren
.add(item
);
3263 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3264 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3266 item
.fItemHeight
= fGlobalItemHeight
;
3268 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3269 itemMap
.put(entry
, item
);
3270 if (entry
.hasChildren()) {
3271 Item oldItem
= fItemMap
.get(entry
);
3272 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3273 /* existing items keep their old expanded state */
3274 item
.fExpanded
= oldItem
.fExpanded
;
3276 /* new items set the expanded state according to auto-expand level */
3277 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3279 item
.fHasChildren
= true;
3280 for (ITimeGraphEntry child
: entry
.getChildren()) {
3281 refreshData(itemMap
, item
, level
+ 1, child
);
3286 public void updateExpandedItems() {
3287 for (Item item
: fItems
) {
3288 item
.fExpandedIndex
= -1;
3290 List
<Item
> expandedItemList
= new ArrayList
<>();
3291 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3292 ITimeGraphEntry entry
= fRootEntries
[i
];
3293 Item item
= findItem(entry
);
3294 refreshExpanded(expandedItemList
, item
);
3296 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3297 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3300 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3301 // Check for filters
3302 boolean display
= true;
3303 for (ViewerFilter filter
: fFilters
) {
3304 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3310 item
.fExpandedIndex
= expandedItemList
.size();
3311 expandedItemList
.add(item
);
3312 if (item
.fHasChildren
&& item
.fExpanded
) {
3313 for (Item child
: item
.fChildren
) {
3314 refreshExpanded(expandedItemList
, child
);
3320 public void refreshData(ITimeGraphEntry
[] entries
) {
3321 if (entries
== null) {
3322 fRootEntries
= null;
3324 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3330 public void refreshArrows(List
<ILinkEvent
> events
) {
3331 /* If links are null, reset the list */
3332 if (events
!= null) {
3335 fLinks
= new ArrayList
<>();
3339 public ITimeGraphEntry
[] getEntries() {
3340 return fRootEntries
;
3344 private class Item
{
3345 private boolean fExpanded
;
3346 private int fExpandedIndex
;
3347 private boolean fSelected
;
3348 private boolean fHasChildren
;
3349 private int fItemHeight
;
3350 private final int fLevel
;
3351 private final List
<Item
> fChildren
;
3352 private final String fName
;
3353 private final ITimeGraphEntry fEntry
;
3355 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3356 this.fEntry
= entry
;
3358 this.fLevel
= level
;
3359 this.fChildren
= new ArrayList
<>();
3363 public String
toString() {
3369 public void menuDetected(MenuDetectEvent e
) {
3370 if (null == fTimeProvider
) {
3374 * This flag indicates if menu was prevented from being shown below and
3375 * therefore must be made visible on callback from mouseUp().
3377 boolean pendingEventCallback
= fPendingMenuDetectEvent
!= null;
3378 Point p
= toControl(e
.x
, e
.y
);
3379 if (e
.detail
== SWT
.MENU_MOUSE
&& isOverTimeSpace(p
.x
, p
.y
)) {
3380 if (fPendingMenuDetectEvent
== null) {
3381 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3382 * Store the event and trigger it later just before handling mouseUp.
3383 * This allows for the method to detect if mouse is used to drag zoom.
3385 fPendingMenuDetectEvent
= e
;
3387 * Prevent the platform to show the menu when returning. The
3388 * menu will be shown (see below) when this method is called
3389 * again during mouseUp().
3394 fPendingMenuDetectEvent
= null;
3395 if (fDragState
!= DRAG_ZOOM
|| !isInDragZoomMargin()) {
3397 * Don't show the menu on mouseUp() if a drag zoom is in
3398 * progress with a drag range outside of the drag zoom margin,
3399 * or if any other drag operation, or none, is in progress.
3405 if (fDragState
!= DRAG_NONE
) {
3407 * Don't show the menu on keyboard menu or mouse menu outside of
3408 * the time space if any drag operation is in progress.
3414 int idx
= getItemIndexAtY(p
.y
);
3415 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3416 Item item
= fItemData
.fExpandedItems
[idx
];
3417 ITimeGraphEntry entry
= item
.fEntry
;
3419 /* Send menu event for the time graph entry */
3422 fireMenuEventOnTimeGraphEntry(e
);
3423 Menu menu
= getMenu();
3424 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3425 menu
.setVisible(true);
3428 /* Send menu event for time event */
3429 if (entry
.hasTimeEvents()) {
3430 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3431 if (event
!= null) {
3434 fireMenuEventOnTimeEvent(e
);
3436 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3437 menu
.setVisible(true);
3445 * Perform the alignment operation.
3448 * the alignment offset
3450 * @see ITmfTimeAligned
3454 public void performAlign(int offset
) {
3455 fTimeProvider
.setNameSpace(offset
);
3459 * Return the time alignment information
3461 * @return the time alignment information
3463 * @see ITmfTimeAligned
3467 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3468 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());
3471 private boolean isInDragZoomMargin() {
3472 return (Math
.abs(fDragX
- fDragX0
) < DRAG_MARGIN
);