1 /*****************************************************************************
2 * Copyright (c) 2007, 2016 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.AbstractMap
.SimpleEntry
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Arrays
;
26 import java
.util
.Collections
;
27 import java
.util
.HashMap
;
28 import java
.util
.Iterator
;
29 import java
.util
.LinkedHashMap
;
30 import java
.util
.LinkedList
;
31 import java
.util
.List
;
33 import java
.util
.Map
.Entry
;
34 import java
.util
.Queue
;
36 import org
.eclipse
.jdt
.annotation
.NonNull
;
37 import org
.eclipse
.jface
.action
.IStatusLineManager
;
38 import org
.eclipse
.jface
.resource
.JFaceResources
;
39 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
40 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
41 import org
.eclipse
.jface
.viewers
.ISelection
;
42 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
43 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
44 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
45 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
46 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
47 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
48 import org
.eclipse
.osgi
.util
.NLS
;
49 import org
.eclipse
.swt
.SWT
;
50 import org
.eclipse
.swt
.events
.FocusEvent
;
51 import org
.eclipse
.swt
.events
.FocusListener
;
52 import org
.eclipse
.swt
.events
.KeyEvent
;
53 import org
.eclipse
.swt
.events
.KeyListener
;
54 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
55 import org
.eclipse
.swt
.events
.MenuDetectListener
;
56 import org
.eclipse
.swt
.events
.MouseEvent
;
57 import org
.eclipse
.swt
.events
.MouseListener
;
58 import org
.eclipse
.swt
.events
.MouseMoveListener
;
59 import org
.eclipse
.swt
.events
.MouseTrackListener
;
60 import org
.eclipse
.swt
.events
.MouseWheelListener
;
61 import org
.eclipse
.swt
.events
.PaintEvent
;
62 import org
.eclipse
.swt
.events
.SelectionListener
;
63 import org
.eclipse
.swt
.events
.TraverseEvent
;
64 import org
.eclipse
.swt
.events
.TraverseListener
;
65 import org
.eclipse
.swt
.events
.TypedEvent
;
66 import org
.eclipse
.swt
.graphics
.Color
;
67 import org
.eclipse
.swt
.graphics
.Cursor
;
68 import org
.eclipse
.swt
.graphics
.Font
;
69 import org
.eclipse
.swt
.graphics
.FontData
;
70 import org
.eclipse
.swt
.graphics
.GC
;
71 import org
.eclipse
.swt
.graphics
.Image
;
72 import org
.eclipse
.swt
.graphics
.Point
;
73 import org
.eclipse
.swt
.graphics
.Rectangle
;
74 import org
.eclipse
.swt
.widgets
.Composite
;
75 import org
.eclipse
.swt
.widgets
.Display
;
76 import org
.eclipse
.swt
.widgets
.Event
;
77 import org
.eclipse
.swt
.widgets
.Listener
;
78 import org
.eclipse
.swt
.widgets
.Menu
;
79 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
95 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
96 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
98 import com
.google
.common
.collect
.Iterables
;
101 * Time graph control implementation
103 * @author Alvaro Sanchez-Leon
104 * @author Patrick Tasse
106 public class TimeGraphControl
extends TimeGraphBaseControl
107 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
108 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
109 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
111 /** Constant indicating that all levels of the time graph should be expanded */
112 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
114 private static final int DRAG_NONE
= 0;
115 private static final int DRAG_TRACE_ITEM
= 1;
116 private static final int DRAG_SPLIT_LINE
= 2;
117 private static final int DRAG_ZOOM
= 3;
118 private static final int DRAG_SELECTION
= 4;
120 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
122 private static final double ZOOM_FACTOR
= 1.5;
123 private static final double ZOOM_IN_FACTOR
= 0.8;
124 private static final double ZOOM_OUT_FACTOR
= 1.25;
126 private static final int SNAP_WIDTH
= 2;
127 private static final int ARROW_HOVER_MAX_DIST
= 5;
129 private static final int NO_STATUS
= -1;
130 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
132 private static final int MAX_LABEL_LENGTH
= 256;
134 private static final int PPI
= 72; // points per inch
135 private static final int DPI
= Display
.getDefault().getDPI().y
;
137 private static final int VERTICAL_ZOOM_DELAY
= 400;
139 /** Resource manager */
140 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
142 /** Color map for event types */
143 private Color
[] fEventColorMap
= null;
145 private ITimeDataProvider fTimeProvider
;
146 private IStatusLineManager fStatusLineManager
= null;
147 private TimeGraphScale fTimeGraphScale
= null;
149 private boolean fIsInFocus
= false;
150 private boolean fMouseOverSplitLine
= false;
151 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
152 private int fHeightAdjustment
= 0;
153 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
154 private boolean fBlendSubPixelEvents
= false;
155 private int fMinimumItemWidth
= 0;
156 private int fTopIndex
= 0;
157 private int fDragState
= DRAG_NONE
;
158 private boolean fDragBeginMarker
= false;
159 private int fDragButton
;
160 private int fDragX0
= 0;
161 private int fDragX
= 0;
162 private boolean fHasNamespaceFocus
= false;
163 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
164 private int fIdealNameSpace
= 0;
165 private long fTime0bak
;
166 private long fTime1bak
;
167 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
168 private ItemData fItemData
= null;
169 private List
<IMarkerEvent
> fMarkers
= null;
170 private boolean fMarkersVisible
= true;
171 private List
<SelectionListener
> fSelectionListeners
;
172 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
173 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
174 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
175 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
176 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
177 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
178 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
179 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
180 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
181 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
182 private MenuDetectEvent fPendingMenuDetectEvent
= null;
183 private boolean fGridLinesVisible
= true;
184 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
185 private boolean fHideArrows
= false;
186 private int fAutoExpandLevel
= ALL_LEVELS
;
187 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
188 private long fVerticalZoomAlignTime
= 0;
189 private int fBorderWidth
= 0;
190 private int fHeaderHeight
= 0;
193 * Standard constructor
196 * The parent composite object
198 * The color scheme to use
200 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
202 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
204 fItemData
= new ItemData();
206 addFocusListener(this);
207 addMouseListener(this);
208 addMouseMoveListener(this);
209 addMouseTrackListener(this);
210 addMouseWheelListener(this);
211 addTraverseListener(this);
212 addKeyListener(this);
213 addMenuDetectListener(this);
214 addListener(SWT
.MouseWheel
, this);
215 addDisposeListener((e
) -> {
216 fResourceManager
.dispose();
217 for (Font font
: fFonts
.values()) {
224 * Sets the timegraph provider used by this timegraph viewer.
226 * @param timeGraphProvider the timegraph provider
228 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
229 fTimeGraphProvider
= timeGraphProvider
;
231 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
232 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
233 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
236 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
237 colorSettingsChanged(stateItems
);
241 * Gets the timegraph provider used by this timegraph viewer.
243 * @return the timegraph provider, or <code>null</code> if not set.
245 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
246 return fTimeGraphProvider
;
250 * Gets the time data provider used by this viewer.
252 * @return The time data provider, or <code>null</code> if not set
255 public ITimeDataProvider
getTimeDataProvider() {
256 return fTimeProvider
;
260 * Gets the color map used by this timegraph viewer.
262 * @return a color map, or <code>null</code> if not set.
264 public Color
[] getEventColorMap() {
265 return fEventColorMap
;
269 * Assign the given time provider
271 * @param timeProvider
274 public void setTimeProvider(ITimeDataProvider timeProvider
) {
275 fTimeProvider
= timeProvider
;
280 * Assign the status line manager
282 * @param statusLineManager
283 * The status line manager, or null to disable status line messages
285 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
286 if (fStatusLineManager
!= null && statusLineManager
== null) {
287 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
289 fStatusLineManager
= statusLineManager
;
293 * Assign the time graph scale
295 * @param timeGraphScale
296 * The time graph scale
298 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
299 fTimeGraphScale
= timeGraphScale
;
303 * Add a selection listener
306 * The listener to add
308 public void addSelectionListener(SelectionListener listener
) {
309 if (listener
== null) {
310 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
312 if (null == fSelectionListeners
) {
313 fSelectionListeners
= new ArrayList
<>();
315 fSelectionListeners
.add(listener
);
319 * Remove a selection listener
322 * The listener to remove
324 public void removeSelectionListener(SelectionListener listener
) {
325 if (null != fSelectionListeners
) {
326 fSelectionListeners
.remove(listener
);
331 * Selection changed callback
333 public void fireSelectionChanged() {
334 if (null != fSelectionListeners
) {
335 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
336 while (it
.hasNext()) {
337 SelectionListener listener
= it
.next();
338 listener
.widgetSelected(null);
342 if (null != fSelectionChangedListeners
) {
343 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
344 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
350 * Default selection callback
352 public void fireDefaultSelection() {
353 if (null != fSelectionListeners
) {
354 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
355 while (it
.hasNext()) {
356 SelectionListener listener
= it
.next();
357 listener
.widgetDefaultSelected(null);
363 * Add a drag selection listener
366 * The listener to add
368 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
369 if (listener
== null) {
370 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
372 if (null == fDragSelectionListeners
) {
373 fDragSelectionListeners
= new ArrayList
<>();
375 fDragSelectionListeners
.add(listener
);
379 * Remove a drag selection listener
382 * The listener to remove
384 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
385 if (null != fDragSelectionListeners
) {
386 fDragSelectionListeners
.remove(listener
);
391 * Drag Selection changed callback
394 * Time interval start
398 public void fireDragSelectionChanged(long start
, long end
) {
399 // check for backward intervals
400 long beginTime
, endTime
;
408 // call the listeners
409 if (null != fDragSelectionListeners
) {
410 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
411 while (it
.hasNext()) {
412 ITimeGraphTimeListener listener
= it
.next();
413 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
419 * Get the traces in the model
421 * @return The array of traces
423 public ITimeGraphEntry
[] getTraces() {
424 return fItemData
.getEntries();
428 * Refresh the data for the thing
430 public void refreshData() {
431 fItemData
.refreshData();
436 * Refresh data for the given traces
439 * The traces to refresh
441 public void refreshData(ITimeGraphEntry
[] traces
) {
442 fItemData
.refreshData(traces
);
447 * Refresh the links (arrows) of this widget
449 * @param events The link event list
451 public void refreshArrows(List
<ILinkEvent
> events
) {
452 fItemData
.refreshArrows(events
);
456 * Get the links (arrows) of this widget
458 * @return The unmodifiable link event list
462 public List
<ILinkEvent
> getArrows() {
463 return Collections
.unmodifiableList(fItemData
.fLinks
);
466 boolean ensureVisibleItem(int idx
, boolean redraw
) {
467 boolean changed
= false;
470 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
471 if (fItemData
.fExpandedItems
[index
].fSelected
) {
476 if (index
>= fItemData
.fExpandedItems
.length
) {
479 if (index
< fTopIndex
) {
486 int page
= countPerPage();
487 if (index
>= fTopIndex
+ page
) {
488 setTopIndex(index
- page
+ 1);
499 * Assign the given index as the top one
504 public void setTopIndex(int idx
) {
505 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
506 index
= Math
.max(0, index
);
512 * Set the top index so that the requested element is at the specified
516 * the time graph entry to be positioned
518 * the requested y-coordinate
521 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
522 Item item
= fItemData
.fItemMap
.get(entry
);
523 if (item
== null || item
.fExpandedIndex
== -1) {
526 int index
= item
.fExpandedIndex
;
527 Rectangle itemRect
= getItemRect(getClientArea(), index
);
528 int delta
= itemRect
.y
+ itemRect
.height
- y
;
529 int topIndex
= getItemIndexAtY(delta
);
530 if (topIndex
!= -1) {
531 setTopIndex(topIndex
);
536 setTopIndex(getExpandedElementCount());
542 * Sets the auto-expand level to be used for new entries discovered when
543 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
544 * . The value 0 means that there is no auto-expand; 1 means that top-level
545 * entries are expanded, but not their children; 2 means that top-level
546 * entries are expanded, and their children, but not grand-children; and so
549 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
553 * non-negative level, or <code>ALL_LEVELS</code> to expand all
556 public void setAutoExpandLevel(int level
) {
557 fAutoExpandLevel
= level
;
561 * Returns the auto-expand level.
563 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
564 * the tree are expanded automatically
565 * @see #setAutoExpandLevel
567 public int getAutoExpandLevel() {
568 return fAutoExpandLevel
;
572 * Get the expanded state of a given entry.
576 * @return true if the entry is expanded, false if collapsed
579 public boolean getExpandedState(ITimeGraphEntry entry
) {
580 Item item
= fItemData
.fItemMap
.get(entry
);
581 return (item
!= null ? item
.fExpanded
: false);
585 * Set the expanded state of a given entry
590 * True if expanded, false if collapsed
592 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
593 Item item
= fItemData
.findItem(entry
);
594 if (item
!= null && item
.fExpanded
!= expanded
) {
595 item
.fExpanded
= expanded
;
596 fItemData
.updateExpandedItems();
602 * Set the expanded state of a given entry to certain relative level.
603 * It will call fireTreeEvent() for each changed entry. At the end
604 * it will call redraw().
609 * level to expand to or negative for all levels
611 * True if expanded, false if collapsed
613 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
614 setExpandedStateInt(entry
, level
, expanded
);
619 * Set the expanded state of a given entry and its children to the first
620 * level that has one collapsed entry.
625 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
626 int level
= findExpandedLevel(entry
);
628 setExpandedStateInt(entry
, level
, true);
634 * Inner class for finding relative level with at least one
637 private class SearchNode
{
638 SearchNode(ITimeGraphEntry e
, int l
) {
642 ITimeGraphEntry entry
;
647 * Finds the relative level with at least one collapsed entry.
651 * @return the found level or -1 if all levels are already expanded.
653 private int findExpandedLevel(ITimeGraphEntry entry
) {
654 Queue
<SearchNode
> queue
= new LinkedList
<>();
655 SearchNode root
= new SearchNode(entry
, 0);
656 SearchNode node
= root
;
659 while (!queue
.isEmpty()) {
660 node
= queue
.remove();
661 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
664 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
665 if (e
.hasChildren()) {
666 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
675 * Set the expanded state of a given entry to certain relative level.
676 * It will call fireTreeEvent() for each changed entry. No redraw is done.
681 * level to expand to or negative for all levels
683 * True if expanded, false if collapsed
685 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
687 if ((level
> 0) || (level
< 0)) {
689 if (entry
.hasChildren()) {
690 for (ITimeGraphEntry e
: entry
.getChildren()) {
691 setExpandedStateInt(e
, level
, expanded
);
695 Item item
= fItemData
.findItem(entry
);
696 if (item
!= null && item
.fExpanded
!= expanded
) {
697 item
.fExpanded
= expanded
;
698 fItemData
.updateExpandedItems();
699 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
704 * Collapses all nodes of the viewer's tree, starting with the root.
706 public void collapseAll() {
707 for (Item item
: fItemData
.fItems
) {
708 item
.fExpanded
= false;
710 fItemData
.updateExpandedItems();
715 * Expands all nodes of the viewer's tree, starting with the root.
717 public void expandAll() {
718 for (Item item
: fItemData
.fItems
) {
719 item
.fExpanded
= true;
721 fItemData
.updateExpandedItems();
726 * Add a tree listener
729 * The listener to add
731 public void addTreeListener(ITimeGraphTreeListener listener
) {
732 if (!fTreeListeners
.contains(listener
)) {
733 fTreeListeners
.add(listener
);
738 * Remove a tree listener
741 * The listener to remove
743 public void removeTreeListener(ITimeGraphTreeListener listener
) {
744 if (fTreeListeners
.contains(listener
)) {
745 fTreeListeners
.remove(listener
);
750 * Tree event callback
755 * The expanded state (true for expanded, false for collapsed)
757 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
758 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
759 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
761 listener
.treeExpanded(event
);
763 listener
.treeCollapsed(event
);
769 * Add a menu listener on {@link ITimeGraphEntry}s
772 * The listener to add
774 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
775 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
776 fTimeGraphEntryMenuListeners
.add(listener
);
781 * Remove a menu listener on {@link ITimeGraphEntry}s
784 * The listener to remove
786 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
787 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
788 fTimeGraphEntryMenuListeners
.remove(listener
);
793 * Menu event callback on {@link ITimeGraphEntry}s
796 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
798 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
799 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
800 listener
.menuDetected(event
);
805 * Add a menu listener on {@link ITimeEvent}s
808 * The listener to add
810 public void addTimeEventMenuListener(MenuDetectListener listener
) {
811 if (!fTimeEventMenuListeners
.contains(listener
)) {
812 fTimeEventMenuListeners
.add(listener
);
817 * Remove a menu listener on {@link ITimeEvent}s
820 * The listener to remove
822 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
823 if (fTimeEventMenuListeners
.contains(listener
)) {
824 fTimeEventMenuListeners
.remove(listener
);
829 * Menu event callback on {@link ITimeEvent}s
832 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
834 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
835 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
836 listener
.menuDetected(event
);
841 public boolean setFocus() {
842 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
843 fHasNamespaceFocus
= true;
845 return super.setFocus();
849 * Returns the current selection for this time graph. If a time graph entry
850 * is selected, it will be the first element in the selection. If a time
851 * event is selected, it will be the second element in the selection.
853 * @return the current selection
856 public ISelection
getSelection() {
857 ITimeGraphEntry entry
= getSelectedTrace();
858 if (null != entry
&& null != fTimeProvider
) {
859 long selectedTime
= fTimeProvider
.getSelectionBegin();
860 ITimeEvent event
= Utils
.findEvent(entry
, selectedTime
, 0);
862 return new StructuredSelection(entry
);
864 return new StructuredSelection(new Object
[] { entry
, event
});
866 return StructuredSelection
.EMPTY
;
870 * Get the selection object
872 * @return The selection
874 public ISelection
getSelectionTrace() {
875 ITimeGraphEntry entry
= getSelectedTrace();
877 return new StructuredSelection(entry
);
879 return StructuredSelection
.EMPTY
;
883 * Enable/disable one of the traces in the model
886 * 1 to enable it, -1 to disable. The method returns immediately
887 * if another value is used.
889 public void selectTrace(int n
) {
890 if ((n
!= 1) && (n
!= -1)) {
894 boolean changed
= false;
895 int lastSelection
= -1;
896 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
897 Item item
= fItemData
.fExpandedItems
[i
];
898 if (item
.fSelected
) {
900 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
901 item
.fSelected
= false;
902 item
= fItemData
.fExpandedItems
[i
+ 1];
903 item
.fSelected
= true;
905 } else if ((-1 == n
) && (i
> 0)) {
906 item
.fSelected
= false;
907 item
= fItemData
.fExpandedItems
[i
- 1];
908 item
.fSelected
= true;
915 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
916 Item item
= fItemData
.fExpandedItems
[0];
917 item
.fSelected
= true;
922 ensureVisibleItem(-1, false);
924 fireSelectionChanged();
932 * 1 for next event, -1 for previous event
934 * true to extend selection range, false for single selection
937 public void selectEvent(int n
, boolean extend
) {
938 if (null == fTimeProvider
) {
941 ITimeGraphEntry trace
= getSelectedTrace();
945 long selectedTime
= fTimeProvider
.getSelectionEnd();
946 long endTime
= fTimeProvider
.getMaxTime();
947 ITimeEvent nextEvent
;
948 if (n
== -1 && selectedTime
> endTime
) {
949 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
951 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
953 if (null == nextEvent
&& n
== -1) {
954 nextEvent
= Utils
.getFirstEvent(trace
);
956 if (null != nextEvent
) {
957 long nextTime
= nextEvent
.getTime();
958 // If last event detected e.g. going back or not moving to a next
960 if (nextTime
<= selectedTime
&& n
== 1) {
961 // Select to the end of this last event
962 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
963 // but not beyond the end of the trace
964 if (nextTime
> endTime
) {
967 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
968 // for previous event go to its end time unless we were already there
969 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
972 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
974 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
976 fireSelectionChanged();
979 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
981 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
983 fireSelectionChanged();
985 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
989 * Select the next event
992 * true to extend selection range, false for single selection
995 public void selectNextEvent(boolean extend
) {
996 selectEvent(1, extend
);
997 // Notify if visible time window has been adjusted
998 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1002 * Select the previous event
1005 * true to extend selection range, false for single selection
1008 public void selectPrevEvent(boolean extend
) {
1009 selectEvent(-1, extend
);
1010 // Notify if visible time window has been adjusted
1011 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1015 * Select the next trace
1017 public void selectNextTrace() {
1022 * Select the previous trace
1024 public void selectPrevTrace() {
1029 * Scroll left or right by one half window size
1032 * true to scroll left, false to scroll right
1034 public void horizontalScroll(boolean left
) {
1035 long time0
= fTimeProvider
.getTime0();
1036 long time1
= fTimeProvider
.getTime1();
1037 long timeMin
= fTimeProvider
.getMinTime();
1038 long timeMax
= fTimeProvider
.getMaxTime();
1039 long range
= time1
- time0
;
1043 long increment
= Math
.max(1, range
/ 2);
1045 time0
= Math
.max(time0
- increment
, timeMin
);
1046 time1
= time0
+ range
;
1048 time1
= Math
.min(time1
+ increment
, timeMax
);
1049 time0
= time1
- range
;
1051 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1055 * Zoom based on mouse cursor location with mouse scrolling
1057 * @param zoomIn true to zoom in, false to zoom out
1059 public void zoom(boolean zoomIn
) {
1060 int globalX
= getDisplay().getCursorLocation().x
;
1061 Point p
= toControl(globalX
, 0);
1062 int nameSpace
= fTimeProvider
.getNameSpace();
1063 int timeSpace
= fTimeProvider
.getTimeSpace();
1064 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1065 long time0
= fTimeProvider
.getTime0();
1066 long time1
= fTimeProvider
.getTime1();
1067 long interval
= time1
- time0
;
1068 if (interval
== 0) {
1070 } // to allow getting out of single point interval
1073 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1075 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1077 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1078 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1079 long newTime1
= newTime0
+ newInterval
;
1080 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1084 * zoom in using single click
1086 public void zoomIn() {
1087 long prevTime0
= fTimeProvider
.getTime0();
1088 long prevTime1
= fTimeProvider
.getTime1();
1089 long prevRange
= prevTime1
- prevTime0
;
1090 if (prevRange
== 0) {
1093 ITimeDataProvider provider
= fTimeProvider
;
1094 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1095 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1096 selTime
= (prevTime0
+ prevTime1
) / 2;
1098 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1099 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1101 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1103 if (inaccuracy
> 0 && inaccuracy
< 100) {
1104 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1108 long min
= fTimeProvider
.getMinTimeInterval();
1109 if ((time1
- time0
) < min
) {
1110 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1111 time1
= time0
+ min
;
1114 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1118 * zoom out using single click
1120 public void zoomOut() {
1121 long prevTime0
= fTimeProvider
.getTime0();
1122 long prevTime1
= fTimeProvider
.getTime1();
1123 ITimeDataProvider provider
= fTimeProvider
;
1124 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1125 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1126 selTime
= (prevTime0
+ prevTime1
) / 2;
1128 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1129 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1131 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1132 if (inaccuracy
> 0 && inaccuracy
< 100) {
1133 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1137 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1144 * true to zoom in, false to zoom out
1147 public void verticalZoom(boolean zoomIn
) {
1149 fHeightAdjustment
++;
1151 fHeightAdjustment
--;
1153 fItemData
.refreshData();
1158 * Reset the vertical zoom to default.
1162 public void resetVerticalZoom() {
1163 fHeightAdjustment
= 0;
1164 fItemData
.refreshData();
1169 * Set the grid lines visibility. The default is true.
1172 * true to show the grid lines, false otherwise
1175 public void setGridLinesVisible(boolean visible
) {
1176 fGridLinesVisible
= visible
;
1180 * Get the grid lines visibility.
1182 * @return true if the grid lines are visible, false otherwise
1185 public boolean getGridLinesVisible() {
1186 return fGridLinesVisible
;
1190 * Set the grid line color. The default is SWT.COLOR_GRAY.
1193 * the grid line color
1196 public void setGridLineColor(Color color
) {
1197 fGridLineColor
= color
;
1201 * Get the grid line color.
1203 * @return the grid line color
1206 public Color
getGridLineColor() {
1207 return fGridLineColor
;
1211 * Set the markers list.
1214 * The markers list, or null
1217 public void setMarkers(List
<IMarkerEvent
> markers
) {
1222 * Get the markers list.
1224 * @return The markers list, or null
1227 public List
<IMarkerEvent
> getMarkers() {
1232 * Set the markers visibility. The default is true.
1235 * true to show the markers, false otherwise
1238 public void setMarkersVisible(boolean visible
) {
1239 fMarkersVisible
= visible
;
1243 * Get the markers visibility.
1245 * @return true if the markers are visible, false otherwise
1248 public boolean getMarkersVisible() {
1249 return fMarkersVisible
;
1255 * @param hideArrows true to hide arrows
1257 public void hideArrows(boolean hideArrows
) {
1258 fHideArrows
= hideArrows
;
1262 * Follow the arrow forward
1265 * true to extend selection range, false for single selection
1268 public void followArrowFwd(boolean extend
) {
1269 ITimeGraphEntry trace
= getSelectedTrace();
1270 if (trace
== null) {
1273 long selectedTime
= fTimeProvider
.getSelectionEnd();
1274 for (ILinkEvent link
: fItemData
.fLinks
) {
1275 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1276 selectItem(link
.getDestinationEntry(), false);
1277 if (link
.getDuration() != 0) {
1279 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1281 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1283 // Notify if visible time window has been adjusted
1284 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1286 fireSelectionChanged();
1287 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1291 selectNextEvent(extend
);
1295 * Follow the arrow backward
1298 * true to extend selection range, false for single selection
1301 public void followArrowBwd(boolean extend
) {
1302 ITimeGraphEntry trace
= getSelectedTrace();
1303 if (trace
== null) {
1306 long selectedTime
= fTimeProvider
.getSelectionEnd();
1307 for (ILinkEvent link
: fItemData
.fLinks
) {
1308 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1309 selectItem(link
.getEntry(), false);
1310 if (link
.getDuration() != 0) {
1312 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1314 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1316 // Notify if visible time window has been adjusted
1317 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1319 fireSelectionChanged();
1320 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1324 selectPrevEvent(extend
);
1328 * Return the currently selected trace
1330 * @return The entry matching the trace
1332 public ITimeGraphEntry
getSelectedTrace() {
1333 ITimeGraphEntry trace
= null;
1334 int idx
= getSelectedIndex();
1336 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1342 * Retrieve the index of the currently selected item
1346 public int getSelectedIndex() {
1348 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1349 Item item
= fItemData
.fExpandedItems
[i
];
1350 if (item
.fSelected
) {
1358 boolean toggle(int idx
) {
1359 boolean toggled
= false;
1360 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1361 Item item
= fItemData
.fExpandedItems
[idx
];
1362 if (item
.fHasChildren
) {
1363 item
.fExpanded
= !item
.fExpanded
;
1364 fItemData
.updateExpandedItems();
1367 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1374 * Gets the index of the item at the given location.
1378 * @return the index of the item at the given location, of -1 if none.
1380 protected int getItemIndexAtY(int y
) {
1383 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1384 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1390 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1391 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1400 boolean isOverSplitLine(int x
) {
1401 if (x
< 0 || null == fTimeProvider
) {
1404 int nameWidth
= fTimeProvider
.getNameSpace();
1405 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1409 * Gets the {@link ITimeGraphEntry} at the given location.
1412 * a point in the widget
1413 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1417 public ITimeGraphEntry
getEntry(Point pt
) {
1418 int idx
= getItemIndexAtY(pt
.y
);
1419 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1423 * Return the arrow event closest to the given point that is no further than
1424 * a maximum distance.
1427 * a point in the widget
1428 * @return The closest arrow event, or null if there is none close enough.
1430 protected ILinkEvent
getArrow(Point pt
) {
1434 ILinkEvent linkEvent
= null;
1435 double minDistance
= Double
.MAX_VALUE
;
1436 for (ILinkEvent event
: fItemData
.fLinks
) {
1437 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1441 int x2
= x1
+ rect
.width
;
1442 int y2
= y1
+ rect
.height
;
1443 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1444 if (minDistance
> d
) {
1450 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1457 public int getXForTime(long time
) {
1458 if (null == fTimeProvider
) {
1461 long time0
= fTimeProvider
.getTime0();
1462 long time1
= fTimeProvider
.getTime1();
1463 int width
= getSize().x
;
1464 int nameSpace
= fTimeProvider
.getNameSpace();
1465 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1466 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1471 public long getTimeAtX(int coord
) {
1472 if (null == fTimeProvider
) {
1476 Point size
= getSize();
1477 long time0
= fTimeProvider
.getTime0();
1478 long time1
= fTimeProvider
.getTime1();
1479 int nameWidth
= fTimeProvider
.getNameSpace();
1480 final int x
= coord
- nameWidth
;
1481 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1482 if (x
>= 0 && size
.x
>= nameWidth
) {
1483 if (time1
- time0
> timeWidth
) {
1484 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1485 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1487 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1488 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1494 void selectItem(int idx
, boolean addSelection
) {
1495 boolean changed
= false;
1497 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1498 Item item
= fItemData
.fExpandedItems
[idx
];
1499 changed
= !item
.fSelected
;
1500 item
.fSelected
= true;
1503 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1504 Item item
= fItemData
.fExpandedItems
[i
];
1505 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1508 item
.fSelected
= i
== idx
;
1511 changed
|= ensureVisibleItem(idx
, true);
1518 * Callback for item selection
1521 * The entry matching the trace
1522 * @param addSelection
1523 * If the selection is added or removed
1525 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1526 int idx
= fItemData
.findItemIndex(trace
);
1527 selectItem(idx
, addSelection
);
1531 * Retrieve the number of entries shown per page.
1535 public int countPerPage() {
1536 int height
= getSize().y
;
1539 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1540 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1541 if (ySum
>= height
) {
1546 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1547 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1548 if (ySum
>= height
) {
1557 * Get the index of the top element
1561 public int getTopIndex() {
1566 * Get the number of expanded (visible) items
1568 * @return The count of expanded (visible) items
1570 public int getExpandedElementCount() {
1571 return fItemData
.fExpandedItems
.length
;
1575 * Get an array of all expanded (visible) elements
1577 * @return The expanded (visible) elements
1579 public ITimeGraphEntry
[] getExpandedElements() {
1580 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1581 for (Item item
: fItemData
.fExpandedItems
) {
1582 elements
.add(item
.fEntry
);
1584 return elements
.toArray(new ITimeGraphEntry
[0]);
1588 * Get the expanded (visible) element at the specified index.
1592 * @return The expanded (visible) element or null if out of range
1595 public ITimeGraphEntry
getExpandedElement(int index
) {
1596 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1599 return fItemData
.fExpandedItems
[index
].fEntry
;
1602 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1603 Rectangle rect
= getItemRect(bounds
, idx
);
1604 rect
.width
= nameWidth
;
1608 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1609 Rectangle rect
= getItemRect(bounds
, idx
);
1610 rect
.x
+= nameWidth
;
1611 rect
.width
-= nameWidth
;
1615 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1617 if (idx
>= fTopIndex
) {
1618 for (int i
= fTopIndex
; i
< idx
; i
++) {
1619 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1622 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1623 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1626 int y
= bounds
.y
+ ySum
;
1627 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1628 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1632 void paint(Rectangle bounds
, PaintEvent e
) {
1635 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1639 fIdealNameSpace
= 0;
1640 int nameSpace
= fTimeProvider
.getNameSpace();
1642 // draw the background layer
1643 drawBackground(bounds
, nameSpace
, gc
);
1645 // draw the grid lines
1646 drawGridLines(bounds
, gc
);
1648 // draw the background markers
1649 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1652 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1654 // draw the foreground markers
1655 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1657 // draw the links (arrows)
1658 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1660 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1662 int alpha
= gc
.getAlpha();
1665 long time0
= fTimeProvider
.getTime0();
1666 long time1
= fTimeProvider
.getTime1();
1667 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1668 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1669 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1670 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1671 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1673 // draw selection lines
1674 if (fDragState
!= DRAG_SELECTION
) {
1675 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1676 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1677 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1680 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1681 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1686 // draw selection background
1687 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1688 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1689 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1690 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1692 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1693 } else if (x0
- x1
> 1) {
1694 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1698 // draw drag selection background
1699 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1700 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1701 if (fDragX0
< fDragX
) {
1702 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1703 } else if (fDragX0
> fDragX
) {
1704 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1709 if (DRAG_SPLIT_LINE
== fDragState
) {
1710 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1711 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1712 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1713 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1714 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1715 if (fDragX
!= fDragX0
) {
1716 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1718 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1719 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1720 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1721 if (fDragX
!= fDragX0
) {
1722 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1724 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1725 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1726 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1733 * Draw the background layer. Fills the background of the control's name
1734 * space and states space, updates the background of items if necessary,
1735 * and draws the item's name text and middle line.
1738 * The bounds of the control
1740 * The name space width
1745 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1746 // draw empty name space background
1747 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1748 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1750 // draw empty states space background
1751 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1752 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1754 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1755 Rectangle itemRect
= getItemRect(bounds
, i
);
1756 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1759 Item item
= fItemData
.fExpandedItems
[i
];
1760 // draw the background of selected item and items with no time events
1761 if (! item
.fEntry
.hasTimeEvents()) {
1762 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1763 gc
.fillRectangle(itemRect
);
1764 } else if (item
.fSelected
) {
1765 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1766 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1767 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1768 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1770 // draw the name and middle line
1771 if (! item
.fEntry
.hasTimeEvents()) {
1772 drawName(item
, itemRect
, gc
);
1774 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1775 drawName(item
, nameRect
, gc
);
1776 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1777 drawMidLine(rect
, gc
);
1783 * Draw the grid lines
1786 * The bounds of the control
1791 public void drawGridLines(Rectangle bounds
, GC gc
) {
1792 if (!fGridLinesVisible
) {
1795 gc
.setForeground(fGridLineColor
);
1796 gc
.setAlpha(fGridLineColor
.getAlpha());
1797 for (int x
: fTimeGraphScale
.getTickList()) {
1798 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1807 * The rectangle of the area
1808 * @param timeProvider
1811 * The list of markers
1813 * true to draw the foreground markers, false otherwise
1815 * The width reserved for the names
1817 * Reference to the SWT GC object
1820 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1821 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1824 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1825 /* the list can grow concurrently but cannot shrink */
1826 for (int i
= 0; i
< markers
.size(); i
++) {
1827 IMarkerEvent marker
= markers
.get(i
);
1828 if (marker
.isForeground() == foreground
) {
1829 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1832 gc
.setClipping((Rectangle
) null);
1836 * Draw a single marker
1841 * The bounds of the control
1842 * @param timeProvider
1845 * The width reserved for the name
1847 * Reference to the SWT GC object
1850 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1851 Rectangle rect
= Utils
.clone(bounds
);
1852 if (marker
.getEntry() != null) {
1853 int index
= fItemData
.findItemIndex(marker
.getEntry());
1857 rect
= getStatesRect(bounds
, index
, nameSpace
);
1858 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1862 int x0
= getXForTime(marker
.getTime());
1863 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1864 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1867 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1868 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1870 Color color
= getColorScheme().getColor(marker
.getColor());
1871 gc
.setBackground(color
);
1872 gc
.setAlpha(color
.getAlpha());
1873 gc
.fillRectangle(rect
);
1875 String label
= marker
.getLabel();
1876 if (label
!= null && marker
.getEntry() != null) {
1877 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1878 gc
.setForeground(color
);
1879 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1884 * Draw many items at once
1887 * The bounds of the control
1888 * @param timeProvider
1891 * The array items to draw
1893 * The index of the first element to draw
1895 * The name space width
1899 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1900 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1901 for (int i
= topIndex
; i
< items
.length
; i
++) {
1902 Item item
= items
[i
];
1903 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1913 * The bounds of the control
1914 * @param timeProvider
1917 * The expanded item index
1919 * The name space width
1923 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1924 Rectangle itemRect
= getItemRect(bounds
, i
);
1925 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1929 ITimeGraphEntry entry
= item
.fEntry
;
1930 long time0
= timeProvider
.getTime0();
1931 long time1
= timeProvider
.getTime1();
1932 long selectedTime
= fTimeProvider
.getSelectionEnd();
1934 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1935 if (rect
.isEmpty() || (time1
<= time0
)) {
1936 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1940 boolean selected
= item
.fSelected
;
1941 // K pixels per second
1942 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1944 if (item
.fEntry
.hasTimeEvents()) {
1945 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1946 fillSpace(rect
, gc
, selected
);
1948 int margins
= getMarginForHeight(rect
.height
);
1949 int height
= rect
.height
- margins
;
1950 int topMargin
= (margins
+ 1) / 2;
1951 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1953 /* Set the font for this item */
1954 setFontForHeight(height
, gc
);
1956 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1957 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1960 while (iterator
.hasNext()) {
1961 ITimeEvent event
= iterator
.next();
1962 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1963 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1964 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1965 // event is out of bounds
1968 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1969 stateRect
.x
= Math
.max(rect
.x
, x
);
1970 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1971 if (stateRect
.x
== lastX
) {
1972 stateRect
.width
-= 1;
1973 if (stateRect
.width
> 0) {
1974 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1975 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1979 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1980 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1981 lastX
= stateRect
.x
;
1984 gc
.setClipping((Rectangle
) null);
1986 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1993 * The bounds of the control
1994 * @param timeProvider
1997 * The list of link events
1999 * The name space width
2003 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
2004 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2008 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2009 /* the list can grow concurrently but cannot shrink */
2010 for (int i
= 0; i
< links
.size(); i
++) {
2011 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2013 gc
.setClipping((Rectangle
) null);
2017 * Draws a link type event
2020 * The link event to draw
2022 * The bounds of the control
2023 * @param timeProvider
2026 * The name space width
2030 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2031 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2034 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2035 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2036 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2038 if ((srcIndex
== -1) || (destIndex
== -1)) {
2042 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2043 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2045 int x0
= getXForTime(event
.getTime());
2046 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2048 // limit the x-coordinates to prevent integer overflow in calculations
2049 // and also GC.drawLine doesn't draw properly with large coordinates
2050 final int limit
= Integer
.MAX_VALUE
/ 1024;
2051 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2052 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2054 int y0
= src
.y
+ src
.height
/ 2;
2055 int y1
= dst
.y
+ dst
.height
/ 2;
2056 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2065 * Time event for which we're drawing the arrow
2067 * The arrow rectangle
2070 * @return true if the arrow was drawn
2072 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2073 Rectangle rect
, GC gc
) {
2078 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2082 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2085 Color stateColor
= null;
2086 if (colorIdx
< fEventColorMap
.length
) {
2087 stateColor
= fEventColorMap
[colorIdx
];
2089 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2092 gc
.setForeground(stateColor
);
2093 gc
.setBackground(stateColor
);
2095 /* Draw the arrow */
2096 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2097 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2100 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2105 * @author Francis Giraldeau
2108 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2110 * The algorithm was taken from this site, not the code itself
2112 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2115 double cos
= 0.9510;
2116 double sin
= 0.3090;
2117 long lenx
= x1
- x0
;
2118 long leny
= y1
- y0
;
2119 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2121 double dx
= factor
* lenx
/ len
;
2122 double dy
= factor
* leny
/ len
;
2123 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2124 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2125 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2126 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2127 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2128 gc
.fillPolygon(arrow
);
2132 * Draw the name of an item.
2137 * The bounds of the item's name space
2141 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2142 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2144 // No name to be drawn
2145 if (fTimeProvider
.getNameSpace() == 0) {
2149 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2150 setFontForHeight(height
, gc
);
2152 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2153 if (item
.fHasChildren
) {
2154 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2155 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2156 Rectangle rect
= Utils
.clone(bounds
);
2157 rect
.x
+= leftMargin
;
2158 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2159 rect
.width
= EXPAND_SIZE
;
2160 rect
.height
= EXPAND_SIZE
;
2161 gc
.fillRectangle(rect
);
2162 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2163 int midy
= rect
.y
+ rect
.height
/ 2;
2164 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2165 if (!item
.fExpanded
) {
2166 int midx
= rect
.x
+ rect
.width
/ 2;
2167 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2170 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2172 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2175 int imgHeight
= img
.getImageData().height
;
2176 int imgWidth
= img
.getImageData().width
;
2178 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2179 gc
.drawImage(img
, x
, y
);
2180 leftMargin
+= imgWidth
+ MARGIN
;
2182 String name
= item
.fName
;
2183 Point size
= gc
.stringExtent(name
);
2184 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2185 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2187 if (hasTimeEvents
) {
2188 // cut long string with "..."
2189 int width
= bounds
.width
- leftMargin
;
2191 while (size
.x
> width
&& name
.length() > 1) {
2193 name
= name
.substring(0, name
.length() - 1);
2194 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2197 name
+= "..."; //$NON-NLS-1$
2200 Rectangle rect
= Utils
.clone(bounds
);
2201 rect
.x
+= leftMargin
;
2202 rect
.width
-= leftMargin
;
2204 if (rect
.width
> 0) {
2205 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2206 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2207 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2208 leftMargin
+= textWidth
+ MARGIN
;
2210 if (hasTimeEvents
) {
2212 rect
.x
= bounds
.x
+ leftMargin
;
2214 rect
.width
= bounds
.width
- rect
.x
;
2215 drawMidLine(rect
, gc
);
2221 * Draw the state (color fill)
2226 * Time event for which we're drawing the state
2228 * The state rectangle
2232 * Is this time event currently selected (so it appears
2234 * @param timeSelected
2235 * Is the timestamp currently selected
2236 * @return true if the state was drawn
2238 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2239 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2241 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2242 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2245 boolean visible
= rect
.width
== 0 ?
false : true;
2246 rect
.width
= Math
.max(1, rect
.width
);
2247 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2248 gc
.setForeground(black
);
2250 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2252 // Only draw the top and bottom borders
2253 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2254 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2255 if (rect
.width
== 1) {
2256 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2259 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2262 Color stateColor
= null;
2263 if (colorIdx
< fEventColorMap
.length
) {
2264 stateColor
= fEventColorMap
[colorIdx
];
2269 boolean reallySelected
= timeSelected
&& selected
;
2270 // fill all rect area
2271 gc
.setBackground(stateColor
);
2273 gc
.fillRectangle(rect
);
2274 } else if (fBlendSubPixelEvents
) {
2276 gc
.fillRectangle(rect
);
2280 if (reallySelected
) {
2281 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2282 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2285 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2287 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2292 * Fill an item's states rectangle
2295 * The states rectangle
2299 * true if the item is selected
2301 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2302 /* Nothing to draw */
2306 * Draw a line at the middle height of a rectangle
2313 private void drawMidLine(Rectangle rect
, GC gc
) {
2314 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2315 int midy
= rect
.y
+ rect
.height
/ 2;
2316 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2319 private static int getMarginForHeight(int height
) {
2321 * State rectangle is smaller than the item bounds when height is > 4.
2322 * Don't use any margin if the height is below or equal that threshold.
2323 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2324 * pixels for the state height, but with a minimum margin of 1.
2326 final int MARGIN_THRESHOLD
= 4;
2327 final int PREFERRED_HEIGHT
= 13;
2328 final int MIN_MARGIN
= 1;
2329 final int MAX_MARGIN
= 6;
2330 return height
<= MARGIN_THRESHOLD ?
0 :
2331 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2334 private void setFontForHeight(int pixels
, GC gc
) {
2335 /* convert font height from pixels to points */
2336 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2337 Font font
= fFonts
.get(height
);
2339 FontData fontData
= gc
.getFont().getFontData()[0];
2340 fontData
.setHeight(height
);
2341 font
= new Font(gc
.getDevice(), fontData
);
2342 fFonts
.put(height
, font
);
2348 public void keyTraversed(TraverseEvent e
) {
2349 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2355 public void keyPressed(KeyEvent e
) {
2357 if (fItemData
.fExpandedItems
.length
== 0) {
2360 if (SWT
.HOME
== e
.keyCode
) {
2362 } else if (SWT
.END
== e
.keyCode
) {
2363 idx
= fItemData
.fExpandedItems
.length
- 1;
2364 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2365 idx
= getSelectedIndex();
2368 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2371 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2372 idx
= getSelectedIndex();
2375 } else if (idx
> 0) {
2378 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2379 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2380 selectPrevEvent(extend
);
2381 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2382 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2383 selectNextEvent(extend
);
2384 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2385 int page
= countPerPage();
2386 idx
= getSelectedIndex();
2391 if (idx
>= fItemData
.fExpandedItems
.length
) {
2392 idx
= fItemData
.fExpandedItems
.length
- 1;
2394 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2395 int page
= countPerPage();
2396 idx
= getSelectedIndex();
2404 } else if (SWT
.CR
== e
.keyCode
) {
2405 idx
= getSelectedIndex();
2407 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2410 fireDefaultSelection();
2414 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2415 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2417 if (fVerticalZoomAlignEntry
!= null) {
2418 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2420 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2421 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2422 verticalZoom(false);
2423 if (fVerticalZoomAlignEntry
!= null) {
2424 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2426 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2427 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2428 resetVerticalZoom();
2429 if (fVerticalZoomAlignEntry
!= null) {
2430 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2432 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2433 if (fHasNamespaceFocus
) {
2434 ITimeGraphEntry entry
= getSelectedTrace();
2435 setExpandedState(entry
, 0, true);
2439 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2440 if (fHasNamespaceFocus
) {
2441 ITimeGraphEntry entry
= getSelectedTrace();
2442 if ((entry
!= null) && entry
.hasChildren()) {
2443 setExpandedState(entry
, -1, false);
2448 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2449 if (fHasNamespaceFocus
) {
2450 ITimeGraphEntry entry
= getSelectedTrace();
2451 if ((entry
!= null) && entry
.hasChildren()) {
2452 setExpandedStateLevel(entry
);
2457 selectItem(idx
, false);
2458 fireSelectionChanged();
2460 int x
= toControl(e
.display
.getCursorLocation()).x
;
2461 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2465 public void keyReleased(KeyEvent e
) {
2466 int x
= toControl(e
.display
.getCursorLocation()).x
;
2467 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2471 public void focusGained(FocusEvent e
) {
2474 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2478 public void focusLost(FocusEvent e
) {
2480 if (DRAG_NONE
!= fDragState
) {
2482 fDragState
= DRAG_NONE
;
2485 updateStatusLine(NO_STATUS
);
2489 * @return If the current view is focused
2491 public boolean isInFocus() {
2496 * Provide the possibility to control the wait cursor externally e.g. data
2497 * requests in progress
2499 * @param waitInd Should we wait indefinitely?
2501 public void waitCursor(boolean waitInd
) {
2502 // Update cursor as indicated
2504 setCursor(fWaitCursor
);
2510 private void updateCursor(int x
, int stateMask
) {
2511 // if Wait cursor not active, check for the need to change the cursor
2512 if (getCursor() == fWaitCursor
) {
2515 Cursor cursor
= null;
2516 if (fDragState
== DRAG_SPLIT_LINE
) {
2517 } else if (fDragState
== DRAG_SELECTION
) {
2518 cursor
= fResizeCursor
;
2519 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2520 cursor
= fDragCursor
;
2521 } else if (fDragState
== DRAG_ZOOM
) {
2522 cursor
= fZoomCursor
;
2523 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2524 cursor
= fDragCursor
;
2525 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2526 cursor
= fResizeCursor
;
2527 } else if (!isOverSplitLine(x
)) {
2528 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2529 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2530 int xBegin
= getXForTime(selectionBegin
);
2531 int xEnd
= getXForTime(selectionEnd
);
2532 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2533 cursor
= fResizeCursor
;
2536 if (getCursor() != cursor
) {
2542 * Update the status line following a change of selection.
2546 public void updateStatusLine() {
2547 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2550 private void updateStatusLine(int x
) {
2551 // use the time provider of the time graph scale for the status line
2552 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2553 if (fStatusLineManager
== null || null == tdp
||
2554 tdp
.getTime0() == tdp
.getTime1()) {
2557 TimeFormat tf
= tdp
.getTimeFormat();
2558 Resolution res
= Resolution
.NANOSEC
;
2559 StringBuilder message
= new StringBuilder();
2560 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2561 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2562 long time
= getTimeAtX(x
);
2564 if (tdp
instanceof ITimeDataProviderConverter
) {
2565 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2567 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2569 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2570 Utils
.formatTime(time
, tf
, res
)
2574 long selectionBegin
= tdp
.getSelectionBegin();
2575 long selectionEnd
= tdp
.getSelectionEnd();
2576 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2578 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2579 Utils
.formatTime(selectionBegin
, tf
, res
)
2581 if (selectionBegin
!= selectionEnd
) {
2582 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2584 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2585 Utils
.formatTime(selectionEnd
, tf
, res
),
2586 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2589 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2590 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2591 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2592 if (tdp
instanceof ITimeDataProviderConverter
) {
2593 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2594 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2596 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2598 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2599 Utils
.formatTime(time0
, tf
, res
),
2600 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2601 Utils
.formatTime(time
, tf
, res
),
2602 Utils
.formatDelta(time
- time0
, tf
, res
)
2605 fStatusLineManager
.setMessage(message
.toString());
2609 public void mouseMove(MouseEvent e
) {
2610 if (null == fTimeProvider
) {
2613 Point size
= getSize();
2614 if (DRAG_TRACE_ITEM
== fDragState
) {
2615 int nameWidth
= fTimeProvider
.getNameSpace();
2616 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2618 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2619 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2620 long time1
= fTime1bak
- timeDelta
;
2621 long maxTime
= fTimeProvider
.getMaxTime();
2622 if (time1
> maxTime
) {
2625 long time0
= time1
- (fTime1bak
- fTime0bak
);
2626 if (time0
< fTimeProvider
.getMinTime()) {
2627 time0
= fTimeProvider
.getMinTime();
2628 time1
= time0
+ (fTime1bak
- fTime0bak
);
2630 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2632 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2634 fTimeProvider
.setNameSpace(e
.x
);
2635 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2636 } else if (DRAG_SELECTION
== fDragState
) {
2637 if (fDragBeginMarker
) {
2638 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2640 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2643 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2644 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2645 } else if (DRAG_ZOOM
== fDragState
) {
2646 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2648 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2649 } else if (DRAG_NONE
== fDragState
) {
2650 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2651 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2654 fMouseOverSplitLine
= mouseOverSplitLine
;
2657 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2658 fHasNamespaceFocus
= false;
2660 fHasNamespaceFocus
= true;
2662 updateCursor(e
.x
, e
.stateMask
);
2663 updateStatusLine(e
.x
);
2667 public void mouseDoubleClick(MouseEvent e
) {
2668 if (null == fTimeProvider
) {
2671 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2672 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2673 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2674 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2675 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2678 fMouseOverSplitLine
= mouseOverSplitLine
;
2679 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2682 int idx
= getItemIndexAtY(e
.y
);
2684 selectItem(idx
, false);
2685 fireDefaultSelection();
2691 public void mouseDown(MouseEvent e
) {
2692 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2693 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2694 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2698 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2699 int nameSpace
= fTimeProvider
.getNameSpace();
2700 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2701 fDragState
= DRAG_SPLIT_LINE
;
2702 fDragButton
= e
.button
;
2705 fTime0bak
= fTimeProvider
.getTime0();
2706 fTime1bak
= fTimeProvider
.getTime1();
2708 updateCursor(e
.x
, e
.stateMask
);
2712 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2713 int nameSpace
= fTimeProvider
.getNameSpace();
2714 idx
= getItemIndexAtY(e
.y
);
2716 Item item
= fItemData
.fExpandedItems
[idx
];
2717 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2721 selectItem(idx
, false);
2722 fireSelectionChanged();
2724 selectItem(idx
, false); // clear selection
2725 fireSelectionChanged();
2727 long hitTime
= getTimeAtX(e
.x
);
2731 fDragState
= DRAG_SELECTION
;
2732 fDragBeginMarker
= false;
2733 fDragButton
= e
.button
;
2736 fDragTime0
= getTimeAtX(fDragX0
);
2737 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2738 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2739 int xBegin
= getXForTime(selectionBegin
);
2740 int xEnd
= getXForTime(selectionEnd
);
2741 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2742 long time
= getTimeAtX(e
.x
);
2743 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2744 fDragBeginMarker
= true;
2747 fDragTime0
= selectionEnd
;
2750 fDragTime0
= selectionBegin
;
2753 long time
= getTimeAtX(e
.x
);
2754 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2755 fDragBeginMarker
= true;
2758 fDragTime0
= selectionEnd
;
2759 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2761 fDragTime0
= selectionBegin
;
2764 fTime0bak
= fTimeProvider
.getTime0();
2765 fTime1bak
= fTimeProvider
.getTime1();
2767 updateCursor(e
.x
, e
.stateMask
);
2768 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2770 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2771 long hitTime
= getTimeAtX(e
.x
);
2774 fDragState
= DRAG_TRACE_ITEM
;
2775 fDragButton
= e
.button
;
2778 fTime0bak
= fTimeProvider
.getTime0();
2779 fTime1bak
= fTimeProvider
.getTime1();
2780 updateCursor(e
.x
, e
.stateMask
);
2782 } else if (3 == e
.button
) {
2783 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2785 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2787 fDragTime0
= getTimeAtX(fDragX0
);
2788 fDragState
= DRAG_ZOOM
;
2789 fDragButton
= e
.button
;
2791 updateCursor(e
.x
, e
.stateMask
);
2792 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2794 idx
= getItemIndexAtY(e
.y
);
2795 selectItem(idx
, false);
2796 fireSelectionChanged();
2802 public void mouseUp(MouseEvent e
) {
2803 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2804 if ((fDragState
== DRAG_ZOOM
) && (fDragX0
== fDragX
)) {
2805 // Select entry and time event for single click
2806 long time
= getTimeAtX(e
.x
);
2807 fTimeProvider
.setSelectionRangeNotify(time
, time
, false);
2808 int idx
= getItemIndexAtY(e
.y
);
2809 selectItem(idx
, false);
2810 fireSelectionChanged();
2812 menuDetected(fPendingMenuDetectEvent
);
2814 if (DRAG_NONE
!= fDragState
) {
2816 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2817 if (fDragX
!= fDragX0
) {
2818 fTimeProvider
.notifyStartFinishTime();
2820 fDragState
= DRAG_NONE
;
2821 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2822 fDragState
= DRAG_NONE
;
2824 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2825 if (fDragX
== fDragX0
) { // click without selecting anything
2826 long time
= getTimeAtX(e
.x
);
2827 fTimeProvider
.setSelectedTimeNotify(time
, false);
2829 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2830 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2831 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2833 fDragState
= DRAG_NONE
;
2835 fTimeGraphScale
.setDragRange(-1, -1);
2836 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2837 int nameWidth
= fTimeProvider
.getNameSpace();
2838 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2839 long time0
= getTimeAtX(fDragX0
);
2840 long time1
= getTimeAtX(fDragX
);
2841 if (time0
< time1
) {
2842 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2844 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2849 fDragState
= DRAG_NONE
;
2850 fTimeGraphScale
.setDragRange(-1, -1);
2853 updateCursor(e
.x
, e
.stateMask
);
2854 updateStatusLine(e
.x
);
2858 public void mouseEnter(MouseEvent e
) {
2862 public void mouseExit(MouseEvent e
) {
2863 if (fMouseOverSplitLine
) {
2864 fMouseOverSplitLine
= false;
2867 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2871 public void mouseHover(MouseEvent e
) {
2875 public void mouseScrolled(MouseEvent e
) {
2876 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2881 * On some platforms the mouse scroll event is sent to the
2882 * control that has focus even if it is not under the cursor.
2883 * Handle the event only if over the time graph control.
2885 Point size
= getSize();
2886 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2887 if (!bounds
.contains(e
.x
, e
.y
)) {
2891 boolean horizontalZoom
= false;
2892 boolean horizontalScroll
= false;
2893 boolean verticalZoom
= false;
2894 boolean verticalScroll
= false;
2896 // over the time graph control
2897 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2898 verticalZoom
= true;
2899 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2900 // over the name space
2901 verticalScroll
= true;
2903 // over the state area
2904 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2905 // over the state area, CTRL pressed
2906 horizontalZoom
= true;
2907 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2908 // over the state area, SHIFT pressed
2909 horizontalScroll
= true;
2911 // over the state area, no modifier pressed
2912 verticalScroll
= true;
2916 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2917 verticalZoom(e
.count
> 0);
2918 if (fVerticalZoomAlignEntry
!= null) {
2919 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2921 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2923 } else if (horizontalScroll
) {
2924 horizontalScroll(e
.count
> 0);
2925 } else if (verticalScroll
){
2926 setTopIndex(getTopIndex() - e
.count
);
2931 * Get the vertical zoom alignment entry and position based on the current
2932 * selection. If there is no selection or if the selection is not visible,
2933 * return an alignment entry with a null time graph entry.
2935 * @return a map entry where the key is the selection's time graph entry and
2936 * the value is the center y-coordinate of that entry, or null
2938 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2939 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2940 if (alignEntry
!= null) {
2943 int index
= getSelectedIndex();
2944 if (index
== -1 || index
>= getExpandedElementCount()) {
2945 return new SimpleEntry
<>(null, 0);
2947 Rectangle bounds
= getClientArea();
2948 Rectangle itemRect
= getItemRect(bounds
, index
);
2949 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2950 /* selection is not visible */
2951 return new SimpleEntry
<>(null, 0);
2953 ITimeGraphEntry entry
= getExpandedElement(index
);
2954 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2955 return new SimpleEntry
<>(entry
, y
);
2959 * Get the vertical zoom alignment entry and position at the specified
2963 * the cursor y-coordinate
2964 * @return a map entry where the key is the time graph entry under the
2965 * cursor and the value is the cursor y-coordinate
2967 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2968 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2969 if (alignEntry
!= null) {
2972 int index
= getItemIndexAtY(y
);
2974 index
= getExpandedElementCount() - 1;
2976 ITimeGraphEntry entry
= getExpandedElement(index
);
2977 return new SimpleEntry
<>(entry
, y
);
2981 * Get the vertical zoom alignment entry and position if there is an ongoing
2982 * one and we are within the vertical zoom delay, or otherwise return null.
2984 * @return a map entry where the key is a time graph entry and the value is
2985 * a y-coordinate, or null
2987 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2988 long currentTimeMillis
= System
.currentTimeMillis();
2989 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2991 * If the vertical zoom is triggered repeatedly in a short amount of
2992 * time, use the initial event's entry and position.
2994 fVerticalZoomAlignTime
= currentTimeMillis
;
2995 return fVerticalZoomAlignEntry
;
2997 fVerticalZoomAlignTime
= currentTimeMillis
;
3002 public void handleEvent(Event event
) {
3003 if (event
.type
== SWT
.MouseWheel
) {
3004 // prevent horizontal scrolling when the mouse wheel is used to
3005 // scroll vertically or zoom
3011 public int getBorderWidth() {
3012 return fBorderWidth
;
3016 * Set the border width
3018 * @param borderWidth
3021 public void setBorderWidth(int borderWidth
) {
3022 this.fBorderWidth
= borderWidth
;
3026 * @return The current height of the header row
3028 public int getHeaderHeight() {
3029 return fHeaderHeight
;
3033 * Set the height of the header row
3035 * @param headerHeight
3038 public void setHeaderHeight(int headerHeight
) {
3039 this.fHeaderHeight
= headerHeight
;
3043 * @return The default height of regular item rows
3045 public int getItemHeight() {
3046 return fGlobalItemHeight
;
3050 * Set the default height of regular item rows.
3055 public void setItemHeight(int rowHeight
) {
3056 this.fGlobalItemHeight
= rowHeight
;
3057 for (Item item
: fItemData
.fItems
) {
3058 item
.fItemHeight
= rowHeight
;
3063 * Set the height of a specific item. Overrides the default item height.
3066 * A time graph entry
3069 * @return true if the height is successfully stored, false otherwise
3071 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3072 Item item
= fItemData
.findItem(entry
);
3074 item
.fItemHeight
= rowHeight
;
3081 * Set the minimum item width
3083 * @param width The minimum width
3085 public void setMinimumItemWidth(int width
) {
3086 this.fMinimumItemWidth
= width
;
3090 * @return The minimum item width
3092 public int getMinimumItemWidth() {
3093 return fMinimumItemWidth
;
3097 * Set whether all time events with a duration shorter than one pixel should
3098 * be blended in. If false, only the first such time event will be drawn and
3099 * the subsequent time events in the same pixel will be discarded. The
3100 * default value is false.
3103 * true if sub-pixel events should be blended, false otherwise.
3106 public void setBlendSubPixelEvents(boolean blend
) {
3107 fBlendSubPixelEvents
= blend
;
3111 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3112 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3113 fSelectionChangedListeners
.add(listener
);
3118 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3119 if (listener
!= null) {
3120 fSelectionChangedListeners
.remove(listener
);
3125 public void setSelection(ISelection selection
) {
3126 if (selection
instanceof IStructuredSelection
) {
3127 Object ob
= ((IStructuredSelection
) selection
).getFirstElement();
3128 if (ob
instanceof ITimeGraphEntry
) {
3129 selectItem((ITimeGraphEntry
) ob
, false);
3136 * @param filter The filter object to be attached to the view
3138 public void addFilter(@NonNull ViewerFilter filter
) {
3139 if (!fFilters
.contains(filter
)) {
3140 fFilters
.add(filter
);
3145 * @param filter The filter object to be attached to the view
3147 public void removeFilter(@NonNull ViewerFilter filter
) {
3148 fFilters
.remove(filter
);
3152 * Returns this control's filters.
3154 * @return an array of viewer filters
3157 public @NonNull ViewerFilter
[] getFilters() {
3158 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3162 * Sets the filters, replacing any previous filters.
3165 * an array of viewer filters, or null
3168 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3170 if (filters
!= null) {
3171 fFilters
.addAll(Arrays
.asList(filters
));
3176 public void colorSettingsChanged(StateItem
[] stateItems
) {
3177 /* Destroy previous colors from the resource manager */
3178 if (fEventColorMap
!= null) {
3179 for (Color color
: fEventColorMap
) {
3180 fResourceManager
.destroyColor(color
.getRGB());
3183 if (stateItems
!= null) {
3184 fEventColorMap
= new Color
[stateItems
.length
];
3185 for (int i
= 0; i
< stateItems
.length
; i
++) {
3186 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3189 fEventColorMap
= new Color
[] { };
3194 private class ItemData
{
3195 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3196 private Item
[] fExpandedItems
= new Item
[0];
3197 private Item
[] fItems
= new Item
[0];
3198 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3199 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3204 public Item
findItem(ITimeGraphEntry entry
) {
3205 return fItemMap
.get(entry
);
3208 public int findItemIndex(ITimeGraphEntry entry
) {
3209 Item item
= fItemMap
.get(entry
);
3213 return item
.fExpandedIndex
;
3216 public void refreshData() {
3217 ITimeGraphEntry selection
= getSelectedTrace();
3218 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3219 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3220 ITimeGraphEntry entry
= fRootEntries
[i
];
3221 refreshData(itemMap
, null, 0, entry
);
3224 fItems
= fItemMap
.values().toArray(new Item
[0]);
3225 updateExpandedItems();
3226 if (selection
!= null) {
3227 for (Item item
: fExpandedItems
) {
3228 if (item
.fEntry
== selection
) {
3229 item
.fSelected
= true;
3236 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3237 Item item
= new Item(entry
, entry
.getName(), level
);
3238 if (parent
!= null) {
3239 parent
.fChildren
.add(item
);
3241 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3242 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3244 item
.fItemHeight
= fGlobalItemHeight
;
3246 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3247 itemMap
.put(entry
, item
);
3248 if (entry
.hasChildren()) {
3249 Item oldItem
= fItemMap
.get(entry
);
3250 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3251 /* existing items keep their old expanded state */
3252 item
.fExpanded
= oldItem
.fExpanded
;
3254 /* new items set the expanded state according to auto-expand level */
3255 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3257 item
.fHasChildren
= true;
3258 for (ITimeGraphEntry child
: entry
.getChildren()) {
3259 refreshData(itemMap
, item
, level
+ 1, child
);
3264 public void updateExpandedItems() {
3265 for (Item item
: fItems
) {
3266 item
.fExpandedIndex
= -1;
3268 List
<Item
> expandedItemList
= new ArrayList
<>();
3269 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3270 ITimeGraphEntry entry
= fRootEntries
[i
];
3271 Item item
= findItem(entry
);
3272 refreshExpanded(expandedItemList
, item
);
3274 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3275 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3278 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3279 // Check for filters
3280 boolean display
= true;
3281 for (ViewerFilter filter
: fFilters
) {
3282 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3288 item
.fExpandedIndex
= expandedItemList
.size();
3289 expandedItemList
.add(item
);
3290 if (item
.fHasChildren
&& item
.fExpanded
) {
3291 for (Item child
: item
.fChildren
) {
3292 refreshExpanded(expandedItemList
, child
);
3298 public void refreshData(ITimeGraphEntry
[] entries
) {
3299 if (entries
== null) {
3300 fRootEntries
= null;
3302 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3308 public void refreshArrows(List
<ILinkEvent
> events
) {
3309 /* If links are null, reset the list */
3310 if (events
!= null) {
3313 fLinks
= new ArrayList
<>();
3317 public ITimeGraphEntry
[] getEntries() {
3318 return fRootEntries
;
3322 private class Item
{
3323 private boolean fExpanded
;
3324 private int fExpandedIndex
;
3325 private boolean fSelected
;
3326 private boolean fHasChildren
;
3327 private int fItemHeight
;
3328 private final int fLevel
;
3329 private final List
<Item
> fChildren
;
3330 private final String fName
;
3331 private final ITimeGraphEntry fEntry
;
3333 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3334 this.fEntry
= entry
;
3336 this.fLevel
= level
;
3337 this.fChildren
= new ArrayList
<>();
3341 public String
toString() {
3347 public void menuDetected(MenuDetectEvent e
) {
3348 if (null == fTimeProvider
) {
3351 Point p
= toControl(e
.x
, e
.y
);
3352 if (e
.detail
== SWT
.MENU_MOUSE
) {
3353 if (fPendingMenuDetectEvent
== null) {
3354 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3355 * Store the event and trigger it later just before handling mouseUp.
3356 * This allows for the method to detect if mouse is used to drag zoom.
3358 fPendingMenuDetectEvent
= e
;
3360 * Prevent the platform to show the menu when returning. The
3361 * menu will be shown (see below) when this method is called
3362 * again during mouseup().
3367 fPendingMenuDetectEvent
= null;
3368 if ((p
.x
>= fTimeProvider
.getNameSpace()) && (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
)) {
3372 if (fDragState
!= DRAG_NONE
) {
3376 int idx
= getItemIndexAtY(p
.y
);
3377 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3379 Item item
= fItemData
.fExpandedItems
[idx
];
3380 ITimeGraphEntry entry
= item
.fEntry
;
3382 /* Send menu event for the time graph entry */
3385 fireMenuEventOnTimeGraphEntry(e
);
3386 Menu menu
= getMenu();
3387 if (e
.doit
&& (menu
!= null)) {
3388 menu
.setVisible(true);
3391 /* Send menu event for time event */
3392 if (entry
.hasTimeEvents()) {
3393 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3394 if (event
!= null) {
3397 fireMenuEventOnTimeEvent(e
);
3399 if (e
.doit
&& (menu
!= null)) {
3400 menu
.setVisible(true);
3408 * Perform the alignment operation.
3411 * the alignment offset
3413 * @see ITmfTimeAligned
3417 public void performAlign(int offset
) {
3418 fTimeProvider
.setNameSpace(offset
);
3422 * Return the time alignment information
3424 * @return the time alignment information
3426 * @see ITmfTimeAligned
3430 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3431 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());