1 /*****************************************************************************
2 * Copyright (c) 2007, 2016 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.AbstractMap
.SimpleEntry
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Arrays
;
26 import java
.util
.Collections
;
27 import java
.util
.HashMap
;
28 import java
.util
.Iterator
;
29 import java
.util
.LinkedHashMap
;
30 import java
.util
.LinkedList
;
31 import java
.util
.List
;
33 import java
.util
.Map
.Entry
;
34 import java
.util
.Queue
;
36 import org
.eclipse
.jdt
.annotation
.NonNull
;
37 import org
.eclipse
.jface
.action
.IStatusLineManager
;
38 import org
.eclipse
.jface
.resource
.JFaceResources
;
39 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
40 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
41 import org
.eclipse
.jface
.viewers
.ISelection
;
42 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
43 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
44 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
45 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
46 import org
.eclipse
.osgi
.util
.NLS
;
47 import org
.eclipse
.swt
.SWT
;
48 import org
.eclipse
.swt
.events
.FocusEvent
;
49 import org
.eclipse
.swt
.events
.FocusListener
;
50 import org
.eclipse
.swt
.events
.KeyEvent
;
51 import org
.eclipse
.swt
.events
.KeyListener
;
52 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
53 import org
.eclipse
.swt
.events
.MenuDetectListener
;
54 import org
.eclipse
.swt
.events
.MouseEvent
;
55 import org
.eclipse
.swt
.events
.MouseListener
;
56 import org
.eclipse
.swt
.events
.MouseMoveListener
;
57 import org
.eclipse
.swt
.events
.MouseTrackListener
;
58 import org
.eclipse
.swt
.events
.MouseWheelListener
;
59 import org
.eclipse
.swt
.events
.PaintEvent
;
60 import org
.eclipse
.swt
.events
.SelectionListener
;
61 import org
.eclipse
.swt
.events
.TraverseEvent
;
62 import org
.eclipse
.swt
.events
.TraverseListener
;
63 import org
.eclipse
.swt
.events
.TypedEvent
;
64 import org
.eclipse
.swt
.graphics
.Color
;
65 import org
.eclipse
.swt
.graphics
.Cursor
;
66 import org
.eclipse
.swt
.graphics
.Font
;
67 import org
.eclipse
.swt
.graphics
.FontData
;
68 import org
.eclipse
.swt
.graphics
.GC
;
69 import org
.eclipse
.swt
.graphics
.Image
;
70 import org
.eclipse
.swt
.graphics
.Point
;
71 import org
.eclipse
.swt
.graphics
.Rectangle
;
72 import org
.eclipse
.swt
.widgets
.Composite
;
73 import org
.eclipse
.swt
.widgets
.Display
;
74 import org
.eclipse
.swt
.widgets
.Event
;
75 import org
.eclipse
.swt
.widgets
.Listener
;
76 import org
.eclipse
.swt
.widgets
.Menu
;
77 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
96 import com
.google
.common
.collect
.Iterables
;
99 * Time graph control implementation
101 * @author Alvaro Sanchez-Leon
102 * @author Patrick Tasse
104 public class TimeGraphControl
extends TimeGraphBaseControl
105 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
106 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
107 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
109 /** Constant indicating that all levels of the time graph should be expanded */
110 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
112 private static final int DRAG_NONE
= 0;
113 private static final int DRAG_TRACE_ITEM
= 1;
114 private static final int DRAG_SPLIT_LINE
= 2;
115 private static final int DRAG_ZOOM
= 3;
116 private static final int DRAG_SELECTION
= 4;
118 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
120 private static final double ZOOM_FACTOR
= 1.5;
121 private static final double ZOOM_IN_FACTOR
= 0.8;
122 private static final double ZOOM_OUT_FACTOR
= 1.25;
124 private static final int SNAP_WIDTH
= 2;
125 private static final int ARROW_HOVER_MAX_DIST
= 5;
127 private static final int NO_STATUS
= -1;
128 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
130 private static final int MAX_LABEL_LENGTH
= 256;
132 private static final int PPI
= 72; // points per inch
133 private static final int DPI
= Display
.getDefault().getDPI().y
;
135 private static final int VERTICAL_ZOOM_DELAY
= 400;
137 /** Resource manager */
138 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
140 /** Color map for event types */
141 private Color
[] fEventColorMap
= null;
143 private ITimeDataProvider fTimeProvider
;
144 private IStatusLineManager fStatusLineManager
= null;
145 private TimeGraphScale fTimeGraphScale
= null;
147 private boolean fIsInFocus
= false;
148 private boolean fMouseOverSplitLine
= false;
149 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
150 private int fHeightAdjustment
= 0;
151 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
152 private boolean fBlendSubPixelEvents
= false;
153 private int fMinimumItemWidth
= 0;
154 private int fTopIndex
= 0;
155 private int fDragState
= DRAG_NONE
;
156 private boolean fDragBeginMarker
= false;
157 private int fDragButton
;
158 private int fDragX0
= 0;
159 private int fDragX
= 0;
160 private boolean fHasNamespaceFocus
= false;
161 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
162 private int fIdealNameSpace
= 0;
163 private long fTime0bak
;
164 private long fTime1bak
;
165 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
166 private ItemData fItemData
= null;
167 private List
<IMarkerEvent
> fMarkers
= null;
168 private boolean fMarkersVisible
= true;
169 private List
<SelectionListener
> fSelectionListeners
;
170 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
171 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
172 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
173 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
174 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
175 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
176 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
177 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
178 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
179 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
180 private MenuDetectEvent fPendingMenuDetectEvent
= null;
181 private boolean fGridLinesVisible
= true;
182 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
183 private boolean fHideArrows
= false;
184 private int fAutoExpandLevel
= ALL_LEVELS
;
185 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
186 private long fVerticalZoomAlignTime
= 0;
187 private int fBorderWidth
= 0;
188 private int fHeaderHeight
= 0;
191 * Standard constructor
194 * The parent composite object
196 * The color scheme to use
198 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
200 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
202 fItemData
= new ItemData();
204 addFocusListener(this);
205 addMouseListener(this);
206 addMouseMoveListener(this);
207 addMouseTrackListener(this);
208 addMouseWheelListener(this);
209 addTraverseListener(this);
210 addKeyListener(this);
211 addMenuDetectListener(this);
212 addListener(SWT
.MouseWheel
, this);
213 addDisposeListener((e
) -> {
214 fResourceManager
.dispose();
215 for (Font font
: fFonts
.values()) {
222 * Sets the timegraph provider used by this timegraph viewer.
224 * @param timeGraphProvider the timegraph provider
226 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
227 fTimeGraphProvider
= timeGraphProvider
;
229 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
230 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
231 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
234 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
235 colorSettingsChanged(stateItems
);
239 * Gets the timegraph provider used by this timegraph viewer.
241 * @return the timegraph provider, or <code>null</code> if not set.
243 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
244 return fTimeGraphProvider
;
248 * Gets the time data provider used by this viewer.
250 * @return The time data provider, or <code>null</code> if not set
253 public ITimeDataProvider
getTimeDataProvider() {
254 return fTimeProvider
;
258 * Gets the color map used by this timegraph viewer.
260 * @return a color map, or <code>null</code> if not set.
262 public Color
[] getEventColorMap() {
263 return fEventColorMap
;
267 * Assign the given time provider
269 * @param timeProvider
272 public void setTimeProvider(ITimeDataProvider timeProvider
) {
273 fTimeProvider
= timeProvider
;
278 * Assign the status line manager
280 * @param statusLineManager
281 * The status line manager, or null to disable status line messages
283 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
284 if (fStatusLineManager
!= null && statusLineManager
== null) {
285 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
287 fStatusLineManager
= statusLineManager
;
291 * Assign the time graph scale
293 * @param timeGraphScale
294 * The time graph scale
296 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
297 fTimeGraphScale
= timeGraphScale
;
301 * Add a selection listener
304 * The listener to add
306 public void addSelectionListener(SelectionListener listener
) {
307 if (listener
== null) {
308 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
310 if (null == fSelectionListeners
) {
311 fSelectionListeners
= new ArrayList
<>();
313 fSelectionListeners
.add(listener
);
317 * Remove a selection listener
320 * The listener to remove
322 public void removeSelectionListener(SelectionListener listener
) {
323 if (null != fSelectionListeners
) {
324 fSelectionListeners
.remove(listener
);
329 * Selection changed callback
331 public void fireSelectionChanged() {
332 if (null != fSelectionListeners
) {
333 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
334 while (it
.hasNext()) {
335 SelectionListener listener
= it
.next();
336 listener
.widgetSelected(null);
340 if (null != fSelectionChangedListeners
) {
341 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
342 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
348 * Default selection callback
350 public void fireDefaultSelection() {
351 if (null != fSelectionListeners
) {
352 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
353 while (it
.hasNext()) {
354 SelectionListener listener
= it
.next();
355 listener
.widgetDefaultSelected(null);
361 * Add a drag selection listener
364 * The listener to add
366 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
367 if (listener
== null) {
368 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
370 if (null == fDragSelectionListeners
) {
371 fDragSelectionListeners
= new ArrayList
<>();
373 fDragSelectionListeners
.add(listener
);
377 * Remove a drag selection listener
380 * The listener to remove
382 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
383 if (null != fDragSelectionListeners
) {
384 fDragSelectionListeners
.remove(listener
);
389 * Drag Selection changed callback
392 * Time interval start
396 public void fireDragSelectionChanged(long start
, long end
) {
397 // check for backward intervals
398 long beginTime
, endTime
;
406 // call the listeners
407 if (null != fDragSelectionListeners
) {
408 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
409 while (it
.hasNext()) {
410 ITimeGraphTimeListener listener
= it
.next();
411 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
417 * Get the traces in the model
419 * @return The array of traces
421 public ITimeGraphEntry
[] getTraces() {
422 return fItemData
.getEntries();
426 * Refresh the data for the thing
428 public void refreshData() {
429 fItemData
.refreshData();
434 * Refresh data for the given traces
437 * The traces to refresh
439 public void refreshData(ITimeGraphEntry
[] traces
) {
440 fItemData
.refreshData(traces
);
445 * Refresh the links (arrows) of this widget
447 * @param events The link event list
449 public void refreshArrows(List
<ILinkEvent
> events
) {
450 fItemData
.refreshArrows(events
);
454 * Get the links (arrows) of this widget
456 * @return The unmodifiable link event list
460 public List
<ILinkEvent
> getArrows() {
461 return Collections
.unmodifiableList(fItemData
.fLinks
);
464 boolean ensureVisibleItem(int idx
, boolean redraw
) {
465 boolean changed
= false;
468 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
469 if (fItemData
.fExpandedItems
[index
].fSelected
) {
474 if (index
>= fItemData
.fExpandedItems
.length
) {
477 if (index
< fTopIndex
) {
484 int page
= countPerPage();
485 if (index
>= fTopIndex
+ page
) {
486 setTopIndex(index
- page
+ 1);
497 * Assign the given index as the top one
502 public void setTopIndex(int idx
) {
503 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
504 index
= Math
.max(0, index
);
510 * Set the top index so that the requested element is at the specified
514 * the time graph entry to be positioned
516 * the requested y-coordinate
519 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
520 Item item
= fItemData
.fItemMap
.get(entry
);
521 if (item
== null || item
.fExpandedIndex
== -1) {
524 int index
= item
.fExpandedIndex
;
525 Rectangle itemRect
= getItemRect(getClientArea(), index
);
526 int delta
= itemRect
.y
+ itemRect
.height
- y
;
527 int topIndex
= getItemIndexAtY(delta
);
528 if (topIndex
!= -1) {
529 setTopIndex(topIndex
);
534 setTopIndex(getExpandedElementCount());
540 * Sets the auto-expand level to be used for new entries discovered when
541 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
542 * . The value 0 means that there is no auto-expand; 1 means that top-level
543 * entries are expanded, but not their children; 2 means that top-level
544 * entries are expanded, and their children, but not grand-children; and so
547 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
551 * non-negative level, or <code>ALL_LEVELS</code> to expand all
554 public void setAutoExpandLevel(int level
) {
555 fAutoExpandLevel
= level
;
559 * Returns the auto-expand level.
561 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
562 * the tree are expanded automatically
563 * @see #setAutoExpandLevel
565 public int getAutoExpandLevel() {
566 return fAutoExpandLevel
;
570 * Get the expanded state of a given entry.
574 * @return true if the entry is expanded, false if collapsed
577 public boolean getExpandedState(ITimeGraphEntry entry
) {
578 Item item
= fItemData
.fItemMap
.get(entry
);
579 return (item
!= null ? item
.fExpanded
: false);
583 * Set the expanded state of a given entry
588 * True if expanded, false if collapsed
590 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
591 Item item
= fItemData
.findItem(entry
);
592 if (item
!= null && item
.fExpanded
!= expanded
) {
593 item
.fExpanded
= expanded
;
594 fItemData
.updateExpandedItems();
600 * Set the expanded state of a given entry to certain relative level.
601 * It will call fireTreeEvent() for each changed entry. At the end
602 * it will call redraw().
607 * level to expand to or negative for all levels
609 * True if expanded, false if collapsed
611 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
612 setExpandedStateInt(entry
, level
, expanded
);
617 * Set the expanded state of a given entry and its children to the first
618 * level that has one collapsed entry.
623 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
624 int level
= findExpandedLevel(entry
);
626 setExpandedStateInt(entry
, level
, true);
632 * Inner class for finding relative level with at least one
635 private class SearchNode
{
636 SearchNode(ITimeGraphEntry e
, int l
) {
640 ITimeGraphEntry entry
;
645 * Finds the relative level with at least one collapsed entry.
649 * @return the found level or -1 if all levels are already expanded.
651 private int findExpandedLevel(ITimeGraphEntry entry
) {
652 Queue
<SearchNode
> queue
= new LinkedList
<>();
653 SearchNode root
= new SearchNode(entry
, 0);
654 SearchNode node
= root
;
657 while (!queue
.isEmpty()) {
658 node
= queue
.remove();
659 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
662 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
663 if (e
.hasChildren()) {
664 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
673 * Set the expanded state of a given entry to certain relative level.
674 * It will call fireTreeEvent() for each changed entry. No redraw is done.
679 * level to expand to or negative for all levels
681 * True if expanded, false if collapsed
683 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
685 if ((level
> 0) || (level
< 0)) {
687 if (entry
.hasChildren()) {
688 for (ITimeGraphEntry e
: entry
.getChildren()) {
689 setExpandedStateInt(e
, level
, expanded
);
693 Item item
= fItemData
.findItem(entry
);
694 if (item
!= null && item
.fExpanded
!= expanded
) {
695 item
.fExpanded
= expanded
;
696 fItemData
.updateExpandedItems();
697 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
702 * Collapses all nodes of the viewer's tree, starting with the root.
704 public void collapseAll() {
705 for (Item item
: fItemData
.fItems
) {
706 item
.fExpanded
= false;
708 fItemData
.updateExpandedItems();
713 * Expands all nodes of the viewer's tree, starting with the root.
715 public void expandAll() {
716 for (Item item
: fItemData
.fItems
) {
717 item
.fExpanded
= true;
719 fItemData
.updateExpandedItems();
724 * Add a tree listener
727 * The listener to add
729 public void addTreeListener(ITimeGraphTreeListener listener
) {
730 if (!fTreeListeners
.contains(listener
)) {
731 fTreeListeners
.add(listener
);
736 * Remove a tree listener
739 * The listener to remove
741 public void removeTreeListener(ITimeGraphTreeListener listener
) {
742 if (fTreeListeners
.contains(listener
)) {
743 fTreeListeners
.remove(listener
);
748 * Tree event callback
753 * The expanded state (true for expanded, false for collapsed)
755 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
756 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
757 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
759 listener
.treeExpanded(event
);
761 listener
.treeCollapsed(event
);
767 * Add a menu listener on {@link ITimeGraphEntry}s
770 * The listener to add
772 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
773 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
774 fTimeGraphEntryMenuListeners
.add(listener
);
779 * Remove a menu listener on {@link ITimeGraphEntry}s
782 * The listener to remove
784 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
785 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
786 fTimeGraphEntryMenuListeners
.remove(listener
);
791 * Menu event callback on {@link ITimeGraphEntry}s
794 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
796 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
797 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
798 listener
.menuDetected(event
);
803 * Add a menu listener on {@link ITimeEvent}s
806 * The listener to add
808 public void addTimeEventMenuListener(MenuDetectListener listener
) {
809 if (!fTimeEventMenuListeners
.contains(listener
)) {
810 fTimeEventMenuListeners
.add(listener
);
815 * Remove a menu listener on {@link ITimeEvent}s
818 * The listener to remove
820 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
821 if (fTimeEventMenuListeners
.contains(listener
)) {
822 fTimeEventMenuListeners
.remove(listener
);
827 * Menu event callback on {@link ITimeEvent}s
830 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
832 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
833 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
834 listener
.menuDetected(event
);
839 public boolean setFocus() {
840 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
841 fHasNamespaceFocus
= true;
843 return super.setFocus();
847 public ISelection
getSelection() {
848 TimeGraphSelection sel
= new TimeGraphSelection();
849 ITimeGraphEntry trace
= getSelectedTrace();
850 if (null != trace
&& null != fTimeProvider
) {
851 long selectedTime
= fTimeProvider
.getSelectionBegin();
852 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
862 * Get the selection object
864 * @return The selection
866 public ISelection
getSelectionTrace() {
867 TimeGraphSelection sel
= new TimeGraphSelection();
868 ITimeGraphEntry trace
= getSelectedTrace();
876 * Enable/disable one of the traces in the model
879 * 1 to enable it, -1 to disable. The method returns immediately
880 * if another value is used.
882 public void selectTrace(int n
) {
883 if ((n
!= 1) && (n
!= -1)) {
887 boolean changed
= false;
888 int lastSelection
= -1;
889 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
890 Item item
= fItemData
.fExpandedItems
[i
];
891 if (item
.fSelected
) {
893 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
894 item
.fSelected
= false;
895 item
= fItemData
.fExpandedItems
[i
+ 1];
896 item
.fSelected
= true;
898 } else if ((-1 == n
) && (i
> 0)) {
899 item
.fSelected
= false;
900 item
= fItemData
.fExpandedItems
[i
- 1];
901 item
.fSelected
= true;
908 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
909 Item item
= fItemData
.fExpandedItems
[0];
910 item
.fSelected
= true;
915 ensureVisibleItem(-1, false);
917 fireSelectionChanged();
925 * 1 for next event, -1 for previous event
927 * true to extend selection range, false for single selection
930 public void selectEvent(int n
, boolean extend
) {
931 if (null == fTimeProvider
) {
934 ITimeGraphEntry trace
= getSelectedTrace();
938 long selectedTime
= fTimeProvider
.getSelectionEnd();
939 long endTime
= fTimeProvider
.getMaxTime();
940 ITimeEvent nextEvent
;
941 if (n
== -1 && selectedTime
> endTime
) {
942 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
944 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
946 if (null == nextEvent
&& n
== -1) {
947 nextEvent
= Utils
.getFirstEvent(trace
);
949 if (null != nextEvent
) {
950 long nextTime
= nextEvent
.getTime();
951 // If last event detected e.g. going back or not moving to a next
953 if (nextTime
<= selectedTime
&& n
== 1) {
954 // Select to the end of this last event
955 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
956 // but not beyond the end of the trace
957 if (nextTime
> endTime
) {
960 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
961 // for previous event go to its end time unless we were already there
962 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
965 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
967 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
969 fireSelectionChanged();
972 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
974 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
976 fireSelectionChanged();
978 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
982 * Select the next event
985 * true to extend selection range, false for single selection
988 public void selectNextEvent(boolean extend
) {
989 selectEvent(1, extend
);
990 // Notify if visible time window has been adjusted
991 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
995 * Select the previous event
998 * true to extend selection range, false for single selection
1001 public void selectPrevEvent(boolean extend
) {
1002 selectEvent(-1, extend
);
1003 // Notify if visible time window has been adjusted
1004 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1008 * Select the next trace
1010 public void selectNextTrace() {
1015 * Select the previous trace
1017 public void selectPrevTrace() {
1022 * Scroll left or right by one half window size
1025 * true to scroll left, false to scroll right
1027 public void horizontalScroll(boolean left
) {
1028 long time0
= fTimeProvider
.getTime0();
1029 long time1
= fTimeProvider
.getTime1();
1030 long timeMin
= fTimeProvider
.getMinTime();
1031 long timeMax
= fTimeProvider
.getMaxTime();
1032 long range
= time1
- time0
;
1036 long increment
= Math
.max(1, range
/ 2);
1038 time0
= Math
.max(time0
- increment
, timeMin
);
1039 time1
= time0
+ range
;
1041 time1
= Math
.min(time1
+ increment
, timeMax
);
1042 time0
= time1
- range
;
1044 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1048 * Zoom based on mouse cursor location with mouse scrolling
1050 * @param zoomIn true to zoom in, false to zoom out
1052 public void zoom(boolean zoomIn
) {
1053 int globalX
= getDisplay().getCursorLocation().x
;
1054 Point p
= toControl(globalX
, 0);
1055 int nameSpace
= fTimeProvider
.getNameSpace();
1056 int timeSpace
= fTimeProvider
.getTimeSpace();
1057 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1058 long time0
= fTimeProvider
.getTime0();
1059 long time1
= fTimeProvider
.getTime1();
1060 long interval
= time1
- time0
;
1061 if (interval
== 0) {
1063 } // to allow getting out of single point interval
1066 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1068 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1070 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1071 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1072 long newTime1
= newTime0
+ newInterval
;
1073 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1077 * zoom in using single click
1079 public void zoomIn() {
1080 long prevTime0
= fTimeProvider
.getTime0();
1081 long prevTime1
= fTimeProvider
.getTime1();
1082 long prevRange
= prevTime1
- prevTime0
;
1083 if (prevRange
== 0) {
1086 ITimeDataProvider provider
= fTimeProvider
;
1087 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1088 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1089 selTime
= (prevTime0
+ prevTime1
) / 2;
1091 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1092 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1094 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1096 if (inaccuracy
> 0 && inaccuracy
< 100) {
1097 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1101 long min
= fTimeProvider
.getMinTimeInterval();
1102 if ((time1
- time0
) < min
) {
1103 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1104 time1
= time0
+ min
;
1107 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1111 * zoom out using single click
1113 public void zoomOut() {
1114 long prevTime0
= fTimeProvider
.getTime0();
1115 long prevTime1
= fTimeProvider
.getTime1();
1116 ITimeDataProvider provider
= fTimeProvider
;
1117 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1118 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1119 selTime
= (prevTime0
+ prevTime1
) / 2;
1121 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1122 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1124 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1125 if (inaccuracy
> 0 && inaccuracy
< 100) {
1126 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1130 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1137 * true to zoom in, false to zoom out
1140 public void verticalZoom(boolean zoomIn
) {
1142 fHeightAdjustment
++;
1144 fHeightAdjustment
--;
1146 fItemData
.refreshData();
1151 * Reset the vertical zoom to default.
1155 public void resetVerticalZoom() {
1156 fHeightAdjustment
= 0;
1157 fItemData
.refreshData();
1162 * Set the grid lines visibility. The default is true.
1165 * true to show the grid lines, false otherwise
1168 public void setGridLinesVisible(boolean visible
) {
1169 fGridLinesVisible
= visible
;
1173 * Get the grid lines visibility.
1175 * @return true if the grid lines are visible, false otherwise
1178 public boolean getGridLinesVisible() {
1179 return fGridLinesVisible
;
1183 * Set the grid line color. The default is SWT.COLOR_GRAY.
1186 * the grid line color
1189 public void setGridLineColor(Color color
) {
1190 fGridLineColor
= color
;
1194 * Get the grid line color.
1196 * @return the grid line color
1199 public Color
getGridLineColor() {
1200 return fGridLineColor
;
1204 * Set the markers list.
1207 * The markers list, or null
1210 public void setMarkers(List
<IMarkerEvent
> markers
) {
1215 * Get the markers list.
1217 * @return The markers list, or null
1220 public List
<IMarkerEvent
> getMarkers() {
1225 * Set the markers visibility. The default is true.
1228 * true to show the markers, false otherwise
1231 public void setMarkersVisible(boolean visible
) {
1232 fMarkersVisible
= visible
;
1236 * Get the markers visibility.
1238 * @return true if the markers are visible, false otherwise
1241 public boolean getMarkersVisible() {
1242 return fMarkersVisible
;
1248 * @param hideArrows true to hide arrows
1250 public void hideArrows(boolean hideArrows
) {
1251 fHideArrows
= hideArrows
;
1255 * Follow the arrow forward
1258 * true to extend selection range, false for single selection
1261 public void followArrowFwd(boolean extend
) {
1262 ITimeGraphEntry trace
= getSelectedTrace();
1263 if (trace
== null) {
1266 long selectedTime
= fTimeProvider
.getSelectionEnd();
1267 for (ILinkEvent link
: fItemData
.fLinks
) {
1268 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1269 selectItem(link
.getDestinationEntry(), false);
1270 if (link
.getDuration() != 0) {
1272 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1274 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1276 // Notify if visible time window has been adjusted
1277 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1279 fireSelectionChanged();
1280 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1284 selectNextEvent(extend
);
1288 * Follow the arrow backward
1291 * true to extend selection range, false for single selection
1294 public void followArrowBwd(boolean extend
) {
1295 ITimeGraphEntry trace
= getSelectedTrace();
1296 if (trace
== null) {
1299 long selectedTime
= fTimeProvider
.getSelectionEnd();
1300 for (ILinkEvent link
: fItemData
.fLinks
) {
1301 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1302 selectItem(link
.getEntry(), false);
1303 if (link
.getDuration() != 0) {
1305 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1307 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1309 // Notify if visible time window has been adjusted
1310 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1312 fireSelectionChanged();
1313 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1317 selectPrevEvent(extend
);
1321 * Return the currently selected trace
1323 * @return The entry matching the trace
1325 public ITimeGraphEntry
getSelectedTrace() {
1326 ITimeGraphEntry trace
= null;
1327 int idx
= getSelectedIndex();
1329 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1335 * Retrieve the index of the currently selected item
1339 public int getSelectedIndex() {
1341 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1342 Item item
= fItemData
.fExpandedItems
[i
];
1343 if (item
.fSelected
) {
1351 boolean toggle(int idx
) {
1352 boolean toggled
= false;
1353 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1354 Item item
= fItemData
.fExpandedItems
[idx
];
1355 if (item
.fHasChildren
) {
1356 item
.fExpanded
= !item
.fExpanded
;
1357 fItemData
.updateExpandedItems();
1360 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1367 * Gets the index of the item at the given location.
1371 * @return the index of the item at the given location, of -1 if none.
1373 protected int getItemIndexAtY(int y
) {
1376 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1377 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1383 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1384 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1393 boolean isOverSplitLine(int x
) {
1394 if (x
< 0 || null == fTimeProvider
) {
1397 int nameWidth
= fTimeProvider
.getNameSpace();
1398 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1402 * Gets the {@link ITimeGraphEntry} at the given location.
1405 * a point in the widget
1406 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1410 public ITimeGraphEntry
getEntry(Point pt
) {
1411 int idx
= getItemIndexAtY(pt
.y
);
1412 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1416 * Return the arrow event closest to the given point that is no further than
1417 * a maximum distance.
1420 * a point in the widget
1421 * @return The closest arrow event, or null if there is none close enough.
1423 protected ILinkEvent
getArrow(Point pt
) {
1427 ILinkEvent linkEvent
= null;
1428 double minDistance
= Double
.MAX_VALUE
;
1429 for (ILinkEvent event
: fItemData
.fLinks
) {
1430 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1434 int x2
= x1
+ rect
.width
;
1435 int y2
= y1
+ rect
.height
;
1436 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1437 if (minDistance
> d
) {
1443 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1450 public int getXForTime(long time
) {
1451 if (null == fTimeProvider
) {
1454 long time0
= fTimeProvider
.getTime0();
1455 long time1
= fTimeProvider
.getTime1();
1456 int width
= getSize().x
;
1457 int nameSpace
= fTimeProvider
.getNameSpace();
1458 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1459 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1464 public long getTimeAtX(int coord
) {
1465 if (null == fTimeProvider
) {
1469 Point size
= getSize();
1470 long time0
= fTimeProvider
.getTime0();
1471 long time1
= fTimeProvider
.getTime1();
1472 int nameWidth
= fTimeProvider
.getNameSpace();
1473 final int x
= coord
- nameWidth
;
1474 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1475 if (x
>= 0 && size
.x
>= nameWidth
) {
1476 if (time1
- time0
> timeWidth
) {
1477 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1478 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1480 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1481 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1487 void selectItem(int idx
, boolean addSelection
) {
1488 boolean changed
= false;
1490 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1491 Item item
= fItemData
.fExpandedItems
[idx
];
1492 changed
= !item
.fSelected
;
1493 item
.fSelected
= true;
1496 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1497 Item item
= fItemData
.fExpandedItems
[i
];
1498 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1501 item
.fSelected
= i
== idx
;
1504 changed
|= ensureVisibleItem(idx
, true);
1511 * Callback for item selection
1514 * The entry matching the trace
1515 * @param addSelection
1516 * If the selection is added or removed
1518 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1519 int idx
= fItemData
.findItemIndex(trace
);
1520 selectItem(idx
, addSelection
);
1524 * Retrieve the number of entries shown per page.
1528 public int countPerPage() {
1529 int height
= getSize().y
;
1532 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1533 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1534 if (ySum
>= height
) {
1539 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1540 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1541 if (ySum
>= height
) {
1550 * Get the index of the top element
1554 public int getTopIndex() {
1559 * Get the number of expanded (visible) items
1561 * @return The count of expanded (visible) items
1563 public int getExpandedElementCount() {
1564 return fItemData
.fExpandedItems
.length
;
1568 * Get an array of all expanded (visible) elements
1570 * @return The expanded (visible) elements
1572 public ITimeGraphEntry
[] getExpandedElements() {
1573 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1574 for (Item item
: fItemData
.fExpandedItems
) {
1575 elements
.add(item
.fEntry
);
1577 return elements
.toArray(new ITimeGraphEntry
[0]);
1581 * Get the expanded (visible) element at the specified index.
1585 * @return The expanded (visible) element or null if out of range
1588 public ITimeGraphEntry
getExpandedElement(int index
) {
1589 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1592 return fItemData
.fExpandedItems
[index
].fEntry
;
1595 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1596 Rectangle rect
= getItemRect(bounds
, idx
);
1597 rect
.width
= nameWidth
;
1601 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1602 Rectangle rect
= getItemRect(bounds
, idx
);
1603 rect
.x
+= nameWidth
;
1604 rect
.width
-= nameWidth
;
1608 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1610 if (idx
>= fTopIndex
) {
1611 for (int i
= fTopIndex
; i
< idx
; i
++) {
1612 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1615 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1616 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1619 int y
= bounds
.y
+ ySum
;
1620 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1621 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1625 void paint(Rectangle bounds
, PaintEvent e
) {
1628 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1632 fIdealNameSpace
= 0;
1633 int nameSpace
= fTimeProvider
.getNameSpace();
1635 // draw the background layer
1636 drawBackground(bounds
, nameSpace
, gc
);
1638 // draw the grid lines
1639 drawGridLines(bounds
, gc
);
1641 // draw the background markers
1642 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1645 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1647 // draw the foreground markers
1648 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1650 // draw the links (arrows)
1651 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1653 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1655 int alpha
= gc
.getAlpha();
1658 long time0
= fTimeProvider
.getTime0();
1659 long time1
= fTimeProvider
.getTime1();
1660 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1661 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1662 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1663 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1664 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1666 // draw selection lines
1667 if (fDragState
!= DRAG_SELECTION
) {
1668 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1669 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1670 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1673 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1674 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1679 // draw selection background
1680 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1681 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1682 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1683 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1685 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1686 } else if (x0
- x1
> 1) {
1687 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1691 // draw drag selection background
1692 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1693 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1694 if (fDragX0
< fDragX
) {
1695 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1696 } else if (fDragX0
> fDragX
) {
1697 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1702 if (DRAG_SPLIT_LINE
== fDragState
) {
1703 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1704 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1705 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1706 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1707 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1708 if (fDragX
!= fDragX0
) {
1709 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1711 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1712 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1713 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1714 if (fDragX
!= fDragX0
) {
1715 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1717 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1718 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1719 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1726 * Draw the background layer. Fills the background of the control's name
1727 * space and states space, updates the background of items if necessary,
1728 * and draws the item's name text and middle line.
1731 * The bounds of the control
1733 * The name space width
1738 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1739 // draw empty name space background
1740 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1741 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1743 // draw empty states space background
1744 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1745 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1747 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1748 Rectangle itemRect
= getItemRect(bounds
, i
);
1749 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1752 Item item
= fItemData
.fExpandedItems
[i
];
1753 // draw the background of selected item and items with no time events
1754 if (! item
.fEntry
.hasTimeEvents()) {
1755 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1756 gc
.fillRectangle(itemRect
);
1757 } else if (item
.fSelected
) {
1758 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1759 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1760 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1761 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1763 // draw the name and middle line
1764 if (! item
.fEntry
.hasTimeEvents()) {
1765 drawName(item
, itemRect
, gc
);
1767 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1768 drawName(item
, nameRect
, gc
);
1769 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1770 drawMidLine(rect
, gc
);
1776 * Draw the grid lines
1779 * The bounds of the control
1784 public void drawGridLines(Rectangle bounds
, GC gc
) {
1785 if (!fGridLinesVisible
) {
1788 gc
.setForeground(fGridLineColor
);
1789 gc
.setAlpha(fGridLineColor
.getAlpha());
1790 for (int x
: fTimeGraphScale
.getTickList()) {
1791 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1800 * The rectangle of the area
1801 * @param timeProvider
1804 * The list of markers
1806 * true to draw the foreground markers, false otherwise
1808 * The width reserved for the names
1810 * Reference to the SWT GC object
1813 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1814 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1817 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1818 /* the list can grow concurrently but cannot shrink */
1819 for (int i
= 0; i
< markers
.size(); i
++) {
1820 IMarkerEvent marker
= markers
.get(i
);
1821 if (marker
.isForeground() == foreground
) {
1822 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1825 gc
.setClipping((Rectangle
) null);
1829 * Draw a single marker
1834 * The bounds of the control
1835 * @param timeProvider
1838 * The width reserved for the name
1840 * Reference to the SWT GC object
1843 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1844 Rectangle rect
= Utils
.clone(bounds
);
1845 if (marker
.getEntry() != null) {
1846 int index
= fItemData
.findItemIndex(marker
.getEntry());
1850 rect
= getStatesRect(bounds
, index
, nameSpace
);
1851 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1855 int x0
= getXForTime(marker
.getTime());
1856 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1857 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1860 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1861 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1863 Color color
= getColorScheme().getColor(marker
.getColor());
1864 gc
.setBackground(color
);
1865 gc
.setAlpha(color
.getAlpha());
1866 gc
.fillRectangle(rect
);
1868 String label
= marker
.getLabel();
1869 if (label
!= null && marker
.getEntry() != null) {
1870 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1871 gc
.setForeground(color
);
1872 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1877 * Draw many items at once
1880 * The bounds of the control
1881 * @param timeProvider
1884 * The array items to draw
1886 * The index of the first element to draw
1888 * The name space width
1892 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1893 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1894 for (int i
= topIndex
; i
< items
.length
; i
++) {
1895 Item item
= items
[i
];
1896 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1906 * The bounds of the control
1907 * @param timeProvider
1910 * The expanded item index
1912 * The name space width
1916 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1917 Rectangle itemRect
= getItemRect(bounds
, i
);
1918 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1922 ITimeGraphEntry entry
= item
.fEntry
;
1923 long time0
= timeProvider
.getTime0();
1924 long time1
= timeProvider
.getTime1();
1925 long selectedTime
= fTimeProvider
.getSelectionEnd();
1927 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1928 if (rect
.isEmpty() || (time1
<= time0
)) {
1929 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1933 boolean selected
= item
.fSelected
;
1934 // K pixels per second
1935 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1937 if (item
.fEntry
.hasTimeEvents()) {
1938 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1939 fillSpace(rect
, gc
, selected
);
1941 int margins
= getMarginForHeight(rect
.height
);
1942 int height
= rect
.height
- margins
;
1943 int topMargin
= (margins
+ 1) / 2;
1944 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1946 /* Set the font for this item */
1947 setFontForHeight(height
, gc
);
1949 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1950 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1953 while (iterator
.hasNext()) {
1954 ITimeEvent event
= iterator
.next();
1955 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1956 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1957 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1958 // event is out of bounds
1961 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1962 stateRect
.x
= Math
.max(rect
.x
, x
);
1963 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1964 if (stateRect
.x
== lastX
) {
1965 stateRect
.width
-= 1;
1966 if (stateRect
.width
> 0) {
1967 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1968 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1972 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1973 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1974 lastX
= stateRect
.x
;
1977 gc
.setClipping((Rectangle
) null);
1979 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1986 * The bounds of the control
1987 * @param timeProvider
1990 * The list of link events
1992 * The name space width
1996 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1997 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2001 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2002 /* the list can grow concurrently but cannot shrink */
2003 for (int i
= 0; i
< links
.size(); i
++) {
2004 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2006 gc
.setClipping((Rectangle
) null);
2010 * Draws a link type event
2013 * The link event to draw
2015 * The bounds of the control
2016 * @param timeProvider
2019 * The name space width
2023 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2024 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2027 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2028 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2029 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2031 if ((srcIndex
== -1) || (destIndex
== -1)) {
2035 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2036 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2038 int x0
= getXForTime(event
.getTime());
2039 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2041 // limit the x-coordinates to prevent integer overflow in calculations
2042 // and also GC.drawLine doesn't draw properly with large coordinates
2043 final int limit
= Integer
.MAX_VALUE
/ 1024;
2044 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2045 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2047 int y0
= src
.y
+ src
.height
/ 2;
2048 int y1
= dst
.y
+ dst
.height
/ 2;
2049 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2058 * Time event for which we're drawing the arrow
2060 * The arrow rectangle
2063 * @return true if the arrow was drawn
2065 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2066 Rectangle rect
, GC gc
) {
2071 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2075 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2078 Color stateColor
= null;
2079 if (colorIdx
< fEventColorMap
.length
) {
2080 stateColor
= fEventColorMap
[colorIdx
];
2082 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2085 gc
.setForeground(stateColor
);
2086 gc
.setBackground(stateColor
);
2088 /* Draw the arrow */
2089 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2090 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2093 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2098 * @author Francis Giraldeau
2101 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2103 * The algorithm was taken from this site, not the code itself
2105 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2108 double cos
= 0.9510;
2109 double sin
= 0.3090;
2110 long lenx
= x1
- x0
;
2111 long leny
= y1
- y0
;
2112 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2114 double dx
= factor
* lenx
/ len
;
2115 double dy
= factor
* leny
/ len
;
2116 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2117 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2118 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2119 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2120 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2121 gc
.fillPolygon(arrow
);
2125 * Draw the name of an item.
2130 * The bounds of the item's name space
2134 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2135 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2137 // No name to be drawn
2138 if (fTimeProvider
.getNameSpace() == 0) {
2142 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2143 setFontForHeight(height
, gc
);
2145 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2146 if (item
.fHasChildren
) {
2147 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2148 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2149 Rectangle rect
= Utils
.clone(bounds
);
2150 rect
.x
+= leftMargin
;
2151 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2152 rect
.width
= EXPAND_SIZE
;
2153 rect
.height
= EXPAND_SIZE
;
2154 gc
.fillRectangle(rect
);
2155 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2156 int midy
= rect
.y
+ rect
.height
/ 2;
2157 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2158 if (!item
.fExpanded
) {
2159 int midx
= rect
.x
+ rect
.width
/ 2;
2160 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2163 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2165 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2168 int imgHeight
= img
.getImageData().height
;
2169 int imgWidth
= img
.getImageData().width
;
2171 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2172 gc
.drawImage(img
, x
, y
);
2173 leftMargin
+= imgWidth
+ MARGIN
;
2175 String name
= item
.fName
;
2176 Point size
= gc
.stringExtent(name
);
2177 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2178 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2180 if (hasTimeEvents
) {
2181 // cut long string with "..."
2182 int width
= bounds
.width
- leftMargin
;
2184 while (size
.x
> width
&& name
.length() > 1) {
2186 name
= name
.substring(0, name
.length() - 1);
2187 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2190 name
+= "..."; //$NON-NLS-1$
2193 Rectangle rect
= Utils
.clone(bounds
);
2194 rect
.x
+= leftMargin
;
2195 rect
.width
-= leftMargin
;
2197 if (rect
.width
> 0) {
2198 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2199 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2200 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2201 leftMargin
+= textWidth
+ MARGIN
;
2203 if (hasTimeEvents
) {
2205 rect
.x
= bounds
.x
+ leftMargin
;
2207 rect
.width
= bounds
.width
- rect
.x
;
2208 drawMidLine(rect
, gc
);
2214 * Draw the state (color fill)
2219 * Time event for which we're drawing the state
2221 * The state rectangle
2225 * Is this time event currently selected (so it appears
2227 * @param timeSelected
2228 * Is the timestamp currently selected
2229 * @return true if the state was drawn
2231 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2232 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2234 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2235 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2238 boolean visible
= rect
.width
== 0 ?
false : true;
2239 rect
.width
= Math
.max(1, rect
.width
);
2240 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2241 gc
.setForeground(black
);
2243 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2245 // Only draw the top and bottom borders
2246 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2247 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2248 if (rect
.width
== 1) {
2249 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2252 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2255 Color stateColor
= null;
2256 if (colorIdx
< fEventColorMap
.length
) {
2257 stateColor
= fEventColorMap
[colorIdx
];
2262 boolean reallySelected
= timeSelected
&& selected
;
2263 // fill all rect area
2264 gc
.setBackground(stateColor
);
2266 gc
.fillRectangle(rect
);
2267 } else if (fBlendSubPixelEvents
) {
2269 gc
.fillRectangle(rect
);
2273 if (reallySelected
) {
2274 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2275 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2278 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2280 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2285 * Fill an item's states rectangle
2288 * The states rectangle
2292 * true if the item is selected
2294 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2295 /* Nothing to draw */
2299 * Draw a line at the middle height of a rectangle
2306 private void drawMidLine(Rectangle rect
, GC gc
) {
2307 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2308 int midy
= rect
.y
+ rect
.height
/ 2;
2309 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2312 private static int getMarginForHeight(int height
) {
2314 * State rectangle is smaller than the item bounds when height is > 4.
2315 * Don't use any margin if the height is below or equal that threshold.
2316 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2317 * pixels for the state height, but with a minimum margin of 1.
2319 final int MARGIN_THRESHOLD
= 4;
2320 final int PREFERRED_HEIGHT
= 13;
2321 final int MIN_MARGIN
= 1;
2322 final int MAX_MARGIN
= 6;
2323 return height
<= MARGIN_THRESHOLD ?
0 :
2324 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2327 private void setFontForHeight(int pixels
, GC gc
) {
2328 /* convert font height from pixels to points */
2329 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2330 Font font
= fFonts
.get(height
);
2332 FontData fontData
= gc
.getFont().getFontData()[0];
2333 fontData
.setHeight(height
);
2334 font
= new Font(gc
.getDevice(), fontData
);
2335 fFonts
.put(height
, font
);
2341 public void keyTraversed(TraverseEvent e
) {
2342 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2348 public void keyPressed(KeyEvent e
) {
2350 if (fItemData
.fExpandedItems
.length
== 0) {
2353 if (SWT
.HOME
== e
.keyCode
) {
2355 } else if (SWT
.END
== e
.keyCode
) {
2356 idx
= fItemData
.fExpandedItems
.length
- 1;
2357 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2358 idx
= getSelectedIndex();
2361 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2364 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2365 idx
= getSelectedIndex();
2368 } else if (idx
> 0) {
2371 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2372 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2373 selectPrevEvent(extend
);
2374 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2375 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2376 selectNextEvent(extend
);
2377 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2378 int page
= countPerPage();
2379 idx
= getSelectedIndex();
2384 if (idx
>= fItemData
.fExpandedItems
.length
) {
2385 idx
= fItemData
.fExpandedItems
.length
- 1;
2387 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2388 int page
= countPerPage();
2389 idx
= getSelectedIndex();
2397 } else if (SWT
.CR
== e
.keyCode
) {
2398 idx
= getSelectedIndex();
2400 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2403 fireDefaultSelection();
2407 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2408 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2410 if (fVerticalZoomAlignEntry
!= null) {
2411 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2413 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2414 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2415 verticalZoom(false);
2416 if (fVerticalZoomAlignEntry
!= null) {
2417 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2419 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2420 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2421 resetVerticalZoom();
2422 if (fVerticalZoomAlignEntry
!= null) {
2423 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2425 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2426 if (fHasNamespaceFocus
) {
2427 ITimeGraphEntry entry
= getSelectedTrace();
2428 setExpandedState(entry
, 0, true);
2432 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2433 if (fHasNamespaceFocus
) {
2434 ITimeGraphEntry entry
= getSelectedTrace();
2435 if ((entry
!= null) && entry
.hasChildren()) {
2436 setExpandedState(entry
, -1, false);
2441 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2442 if (fHasNamespaceFocus
) {
2443 ITimeGraphEntry entry
= getSelectedTrace();
2444 if ((entry
!= null) && entry
.hasChildren()) {
2445 setExpandedStateLevel(entry
);
2450 selectItem(idx
, false);
2451 fireSelectionChanged();
2453 int x
= toControl(e
.display
.getCursorLocation()).x
;
2454 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2458 public void keyReleased(KeyEvent e
) {
2459 int x
= toControl(e
.display
.getCursorLocation()).x
;
2460 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2464 public void focusGained(FocusEvent e
) {
2467 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2471 public void focusLost(FocusEvent e
) {
2473 if (DRAG_NONE
!= fDragState
) {
2475 fDragState
= DRAG_NONE
;
2478 updateStatusLine(NO_STATUS
);
2482 * @return If the current view is focused
2484 public boolean isInFocus() {
2489 * Provide the possibility to control the wait cursor externally e.g. data
2490 * requests in progress
2492 * @param waitInd Should we wait indefinitely?
2494 public void waitCursor(boolean waitInd
) {
2495 // Update cursor as indicated
2497 setCursor(fWaitCursor
);
2503 private void updateCursor(int x
, int stateMask
) {
2504 // if Wait cursor not active, check for the need to change the cursor
2505 if (getCursor() == fWaitCursor
) {
2508 Cursor cursor
= null;
2509 if (fDragState
== DRAG_SPLIT_LINE
) {
2510 } else if (fDragState
== DRAG_SELECTION
) {
2511 cursor
= fResizeCursor
;
2512 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2513 cursor
= fDragCursor
;
2514 } else if (fDragState
== DRAG_ZOOM
) {
2515 cursor
= fZoomCursor
;
2516 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2517 cursor
= fDragCursor
;
2518 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2519 cursor
= fResizeCursor
;
2520 } else if (!isOverSplitLine(x
)) {
2521 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2522 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2523 int xBegin
= getXForTime(selectionBegin
);
2524 int xEnd
= getXForTime(selectionEnd
);
2525 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2526 cursor
= fResizeCursor
;
2529 if (getCursor() != cursor
) {
2535 * Update the status line following a change of selection.
2539 public void updateStatusLine() {
2540 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2543 private void updateStatusLine(int x
) {
2544 // use the time provider of the time graph scale for the status line
2545 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2546 if (fStatusLineManager
== null || null == tdp
||
2547 tdp
.getTime0() == tdp
.getTime1()) {
2550 TimeFormat tf
= tdp
.getTimeFormat();
2551 Resolution res
= Resolution
.NANOSEC
;
2552 StringBuilder message
= new StringBuilder();
2553 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2554 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2555 long time
= getTimeAtX(x
);
2557 if (tdp
instanceof ITimeDataProviderConverter
) {
2558 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2560 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2562 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2563 Utils
.formatTime(time
, tf
, res
)
2567 long selectionBegin
= tdp
.getSelectionBegin();
2568 long selectionEnd
= tdp
.getSelectionEnd();
2569 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2571 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2572 Utils
.formatTime(selectionBegin
, tf
, res
)
2574 if (selectionBegin
!= selectionEnd
) {
2575 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2577 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2578 Utils
.formatTime(selectionEnd
, tf
, res
),
2579 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2582 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2583 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2584 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2585 if (tdp
instanceof ITimeDataProviderConverter
) {
2586 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2587 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2589 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2591 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2592 Utils
.formatTime(time0
, tf
, res
),
2593 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2594 Utils
.formatTime(time
, tf
, res
),
2595 Utils
.formatDelta(time
- time0
, tf
, res
)
2598 fStatusLineManager
.setMessage(message
.toString());
2602 public void mouseMove(MouseEvent e
) {
2603 if (null == fTimeProvider
) {
2606 Point size
= getSize();
2607 if (DRAG_TRACE_ITEM
== fDragState
) {
2608 int nameWidth
= fTimeProvider
.getNameSpace();
2609 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2611 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2612 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2613 long time1
= fTime1bak
- timeDelta
;
2614 long maxTime
= fTimeProvider
.getMaxTime();
2615 if (time1
> maxTime
) {
2618 long time0
= time1
- (fTime1bak
- fTime0bak
);
2619 if (time0
< fTimeProvider
.getMinTime()) {
2620 time0
= fTimeProvider
.getMinTime();
2621 time1
= time0
+ (fTime1bak
- fTime0bak
);
2623 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2625 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2627 fTimeProvider
.setNameSpace(e
.x
);
2628 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2629 } else if (DRAG_SELECTION
== fDragState
) {
2630 if (fDragBeginMarker
) {
2631 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2633 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2636 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2637 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2638 } else if (DRAG_ZOOM
== fDragState
) {
2639 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2641 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2642 } else if (DRAG_NONE
== fDragState
) {
2643 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2644 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2647 fMouseOverSplitLine
= mouseOverSplitLine
;
2650 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2651 fHasNamespaceFocus
= false;
2653 fHasNamespaceFocus
= true;
2655 updateCursor(e
.x
, e
.stateMask
);
2656 updateStatusLine(e
.x
);
2660 public void mouseDoubleClick(MouseEvent e
) {
2661 if (null == fTimeProvider
) {
2664 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2665 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2666 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2667 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2668 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2671 fMouseOverSplitLine
= mouseOverSplitLine
;
2672 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2675 int idx
= getItemIndexAtY(e
.y
);
2677 selectItem(idx
, false);
2678 fireDefaultSelection();
2684 public void mouseDown(MouseEvent e
) {
2685 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2686 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2687 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2691 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2692 int nameSpace
= fTimeProvider
.getNameSpace();
2693 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2694 fDragState
= DRAG_SPLIT_LINE
;
2695 fDragButton
= e
.button
;
2698 fTime0bak
= fTimeProvider
.getTime0();
2699 fTime1bak
= fTimeProvider
.getTime1();
2701 updateCursor(e
.x
, e
.stateMask
);
2705 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2706 int nameSpace
= fTimeProvider
.getNameSpace();
2707 idx
= getItemIndexAtY(e
.y
);
2709 Item item
= fItemData
.fExpandedItems
[idx
];
2710 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2714 selectItem(idx
, false);
2715 fireSelectionChanged();
2717 selectItem(idx
, false); // clear selection
2718 fireSelectionChanged();
2720 long hitTime
= getTimeAtX(e
.x
);
2724 fDragState
= DRAG_SELECTION
;
2725 fDragBeginMarker
= false;
2726 fDragButton
= e
.button
;
2729 fDragTime0
= getTimeAtX(fDragX0
);
2730 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2731 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2732 int xBegin
= getXForTime(selectionBegin
);
2733 int xEnd
= getXForTime(selectionEnd
);
2734 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2735 long time
= getTimeAtX(e
.x
);
2736 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2737 fDragBeginMarker
= true;
2740 fDragTime0
= selectionEnd
;
2743 fDragTime0
= selectionBegin
;
2746 long time
= getTimeAtX(e
.x
);
2747 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2748 fDragBeginMarker
= true;
2751 fDragTime0
= selectionEnd
;
2752 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2754 fDragTime0
= selectionBegin
;
2757 fTime0bak
= fTimeProvider
.getTime0();
2758 fTime1bak
= fTimeProvider
.getTime1();
2760 updateCursor(e
.x
, e
.stateMask
);
2761 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2763 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2764 long hitTime
= getTimeAtX(e
.x
);
2767 fDragState
= DRAG_TRACE_ITEM
;
2768 fDragButton
= e
.button
;
2771 fTime0bak
= fTimeProvider
.getTime0();
2772 fTime1bak
= fTimeProvider
.getTime1();
2773 updateCursor(e
.x
, e
.stateMask
);
2775 } else if (3 == e
.button
) {
2776 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2778 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2780 fDragTime0
= getTimeAtX(fDragX0
);
2781 fDragState
= DRAG_ZOOM
;
2782 fDragButton
= e
.button
;
2784 updateCursor(e
.x
, e
.stateMask
);
2785 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2787 idx
= getItemIndexAtY(e
.y
);
2788 selectItem(idx
, false);
2789 fireSelectionChanged();
2795 public void mouseUp(MouseEvent e
) {
2796 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2797 menuDetected(fPendingMenuDetectEvent
);
2799 if (DRAG_NONE
!= fDragState
) {
2801 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2802 if (fDragX
!= fDragX0
) {
2803 fTimeProvider
.notifyStartFinishTime();
2805 fDragState
= DRAG_NONE
;
2806 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2807 fDragState
= DRAG_NONE
;
2809 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2810 if (fDragX
== fDragX0
) { // click without selecting anything
2811 long time
= getTimeAtX(e
.x
);
2812 fTimeProvider
.setSelectedTimeNotify(time
, false);
2814 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2815 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2816 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2818 fDragState
= DRAG_NONE
;
2820 fTimeGraphScale
.setDragRange(-1, -1);
2821 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2822 int nameWidth
= fTimeProvider
.getNameSpace();
2823 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2824 long time0
= getTimeAtX(fDragX0
);
2825 long time1
= getTimeAtX(fDragX
);
2826 if (time0
< time1
) {
2827 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2829 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2834 fDragState
= DRAG_NONE
;
2835 fTimeGraphScale
.setDragRange(-1, -1);
2838 updateCursor(e
.x
, e
.stateMask
);
2839 updateStatusLine(e
.x
);
2843 public void mouseEnter(MouseEvent e
) {
2847 public void mouseExit(MouseEvent e
) {
2848 if (fMouseOverSplitLine
) {
2849 fMouseOverSplitLine
= false;
2852 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2856 public void mouseHover(MouseEvent e
) {
2860 public void mouseScrolled(MouseEvent e
) {
2861 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2866 * On some platforms the mouse scroll event is sent to the
2867 * control that has focus even if it is not under the cursor.
2868 * Handle the event only if over the time graph control.
2870 Point size
= getSize();
2871 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2872 if (!bounds
.contains(e
.x
, e
.y
)) {
2876 boolean horizontalZoom
= false;
2877 boolean horizontalScroll
= false;
2878 boolean verticalZoom
= false;
2879 boolean verticalScroll
= false;
2881 // over the time graph control
2882 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2883 verticalZoom
= true;
2884 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2885 // over the name space
2886 verticalScroll
= true;
2888 // over the state area
2889 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2890 // over the state area, CTRL pressed
2891 horizontalZoom
= true;
2892 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2893 // over the state area, SHIFT pressed
2894 horizontalScroll
= true;
2896 // over the state area, no modifier pressed
2897 verticalScroll
= true;
2901 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2902 verticalZoom(e
.count
> 0);
2903 if (fVerticalZoomAlignEntry
!= null) {
2904 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2906 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2908 } else if (horizontalScroll
) {
2909 horizontalScroll(e
.count
> 0);
2910 } else if (verticalScroll
){
2911 setTopIndex(getTopIndex() - e
.count
);
2916 * Get the vertical zoom alignment entry and position based on the current
2917 * selection. If there is no selection or if the selection is not visible,
2918 * return an alignment entry with a null time graph entry.
2920 * @return a map entry where the key is the selection's time graph entry and
2921 * the value is the center y-coordinate of that entry, or null
2923 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2924 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2925 if (alignEntry
!= null) {
2928 int index
= getSelectedIndex();
2929 if (index
== -1 || index
>= getExpandedElementCount()) {
2930 return new SimpleEntry
<>(null, 0);
2932 Rectangle bounds
= getClientArea();
2933 Rectangle itemRect
= getItemRect(bounds
, index
);
2934 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2935 /* selection is not visible */
2936 return new SimpleEntry
<>(null, 0);
2938 ITimeGraphEntry entry
= getExpandedElement(index
);
2939 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2940 return new SimpleEntry
<>(entry
, y
);
2944 * Get the vertical zoom alignment entry and position at the specified
2948 * the cursor y-coordinate
2949 * @return a map entry where the key is the time graph entry under the
2950 * cursor and the value is the cursor y-coordinate
2952 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2953 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2954 if (alignEntry
!= null) {
2957 int index
= getItemIndexAtY(y
);
2959 index
= getExpandedElementCount() - 1;
2961 ITimeGraphEntry entry
= getExpandedElement(index
);
2962 return new SimpleEntry
<>(entry
, y
);
2966 * Get the vertical zoom alignment entry and position if there is an ongoing
2967 * one and we are within the vertical zoom delay, or otherwise return null.
2969 * @return a map entry where the key is a time graph entry and the value is
2970 * a y-coordinate, or null
2972 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2973 long currentTimeMillis
= System
.currentTimeMillis();
2974 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2976 * If the vertical zoom is triggered repeatedly in a short amount of
2977 * time, use the initial event's entry and position.
2979 fVerticalZoomAlignTime
= currentTimeMillis
;
2980 return fVerticalZoomAlignEntry
;
2982 fVerticalZoomAlignTime
= currentTimeMillis
;
2987 public void handleEvent(Event event
) {
2988 if (event
.type
== SWT
.MouseWheel
) {
2989 // prevent horizontal scrolling when the mouse wheel is used to
2990 // scroll vertically or zoom
2996 public int getBorderWidth() {
2997 return fBorderWidth
;
3001 * Set the border width
3003 * @param borderWidth
3006 public void setBorderWidth(int borderWidth
) {
3007 this.fBorderWidth
= borderWidth
;
3011 * @return The current height of the header row
3013 public int getHeaderHeight() {
3014 return fHeaderHeight
;
3018 * Set the height of the header row
3020 * @param headerHeight
3023 public void setHeaderHeight(int headerHeight
) {
3024 this.fHeaderHeight
= headerHeight
;
3028 * @return The default height of regular item rows
3030 public int getItemHeight() {
3031 return fGlobalItemHeight
;
3035 * Set the default height of regular item rows.
3040 public void setItemHeight(int rowHeight
) {
3041 this.fGlobalItemHeight
= rowHeight
;
3042 for (Item item
: fItemData
.fItems
) {
3043 item
.fItemHeight
= rowHeight
;
3048 * Set the height of a specific item. Overrides the default item height.
3051 * A time graph entry
3054 * @return true if the height is successfully stored, false otherwise
3056 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3057 Item item
= fItemData
.findItem(entry
);
3059 item
.fItemHeight
= rowHeight
;
3066 * Set the minimum item width
3068 * @param width The minimum width
3070 public void setMinimumItemWidth(int width
) {
3071 this.fMinimumItemWidth
= width
;
3075 * @return The minimum item width
3077 public int getMinimumItemWidth() {
3078 return fMinimumItemWidth
;
3082 * Set whether all time events with a duration shorter than one pixel should
3083 * be blended in. If false, only the first such time event will be drawn and
3084 * the subsequent time events in the same pixel will be discarded. The
3085 * default value is false.
3088 * true if sub-pixel events should be blended, false otherwise.
3091 public void setBlendSubPixelEvents(boolean blend
) {
3092 fBlendSubPixelEvents
= blend
;
3096 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3097 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3098 fSelectionChangedListeners
.add(listener
);
3103 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3104 if (listener
!= null) {
3105 fSelectionChangedListeners
.remove(listener
);
3110 public void setSelection(ISelection selection
) {
3111 if (selection
instanceof TimeGraphSelection
) {
3112 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
3113 Object ob
= sel
.getFirstElement();
3114 if (ob
instanceof ITimeGraphEntry
) {
3115 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
3116 selectItem(trace
, false);
3123 * @param filter The filter object to be attached to the view
3125 public void addFilter(@NonNull ViewerFilter filter
) {
3126 if (!fFilters
.contains(filter
)) {
3127 fFilters
.add(filter
);
3132 * @param filter The filter object to be attached to the view
3134 public void removeFilter(@NonNull ViewerFilter filter
) {
3135 fFilters
.remove(filter
);
3139 * Returns this control's filters.
3141 * @return an array of viewer filters
3144 public @NonNull ViewerFilter
[] getFilters() {
3145 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3149 * Sets the filters, replacing any previous filters.
3152 * an array of viewer filters, or null
3155 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3157 if (filters
!= null) {
3158 fFilters
.addAll(Arrays
.asList(filters
));
3163 public void colorSettingsChanged(StateItem
[] stateItems
) {
3164 /* Destroy previous colors from the resource manager */
3165 if (fEventColorMap
!= null) {
3166 for (Color color
: fEventColorMap
) {
3167 fResourceManager
.destroyColor(color
.getRGB());
3170 if (stateItems
!= null) {
3171 fEventColorMap
= new Color
[stateItems
.length
];
3172 for (int i
= 0; i
< stateItems
.length
; i
++) {
3173 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3176 fEventColorMap
= new Color
[] { };
3181 private class ItemData
{
3182 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3183 private Item
[] fExpandedItems
= new Item
[0];
3184 private Item
[] fItems
= new Item
[0];
3185 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3186 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3191 public Item
findItem(ITimeGraphEntry entry
) {
3192 return fItemMap
.get(entry
);
3195 public int findItemIndex(ITimeGraphEntry entry
) {
3196 Item item
= fItemMap
.get(entry
);
3200 return item
.fExpandedIndex
;
3203 public void refreshData() {
3204 ITimeGraphEntry selection
= getSelectedTrace();
3205 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3206 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3207 ITimeGraphEntry entry
= fRootEntries
[i
];
3208 refreshData(itemMap
, null, 0, entry
);
3211 fItems
= fItemMap
.values().toArray(new Item
[0]);
3212 updateExpandedItems();
3213 if (selection
!= null) {
3214 for (Item item
: fExpandedItems
) {
3215 if (item
.fEntry
== selection
) {
3216 item
.fSelected
= true;
3223 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3224 Item item
= new Item(entry
, entry
.getName(), level
);
3225 if (parent
!= null) {
3226 parent
.fChildren
.add(item
);
3228 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3229 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3231 item
.fItemHeight
= fGlobalItemHeight
;
3233 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3234 itemMap
.put(entry
, item
);
3235 if (entry
.hasChildren()) {
3236 Item oldItem
= fItemMap
.get(entry
);
3237 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3238 /* existing items keep their old expanded state */
3239 item
.fExpanded
= oldItem
.fExpanded
;
3241 /* new items set the expanded state according to auto-expand level */
3242 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3244 item
.fHasChildren
= true;
3245 for (ITimeGraphEntry child
: entry
.getChildren()) {
3246 refreshData(itemMap
, item
, level
+ 1, child
);
3251 public void updateExpandedItems() {
3252 for (Item item
: fItems
) {
3253 item
.fExpandedIndex
= -1;
3255 List
<Item
> expandedItemList
= new ArrayList
<>();
3256 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3257 ITimeGraphEntry entry
= fRootEntries
[i
];
3258 Item item
= findItem(entry
);
3259 refreshExpanded(expandedItemList
, item
);
3261 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3262 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3265 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3266 // Check for filters
3267 boolean display
= true;
3268 for (ViewerFilter filter
: fFilters
) {
3269 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3275 item
.fExpandedIndex
= expandedItemList
.size();
3276 expandedItemList
.add(item
);
3277 if (item
.fHasChildren
&& item
.fExpanded
) {
3278 for (Item child
: item
.fChildren
) {
3279 refreshExpanded(expandedItemList
, child
);
3285 public void refreshData(ITimeGraphEntry
[] entries
) {
3286 if (entries
== null) {
3287 fRootEntries
= null;
3289 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3295 public void refreshArrows(List
<ILinkEvent
> events
) {
3296 /* If links are null, reset the list */
3297 if (events
!= null) {
3300 fLinks
= new ArrayList
<>();
3304 public ITimeGraphEntry
[] getEntries() {
3305 return fRootEntries
;
3309 private class Item
{
3310 private boolean fExpanded
;
3311 private int fExpandedIndex
;
3312 private boolean fSelected
;
3313 private boolean fHasChildren
;
3314 private int fItemHeight
;
3315 private final int fLevel
;
3316 private final List
<Item
> fChildren
;
3317 private final String fName
;
3318 private final ITimeGraphEntry fEntry
;
3320 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3321 this.fEntry
= entry
;
3323 this.fLevel
= level
;
3324 this.fChildren
= new ArrayList
<>();
3328 public String
toString() {
3334 public void menuDetected(MenuDetectEvent e
) {
3335 if (null == fTimeProvider
) {
3338 Point p
= toControl(e
.x
, e
.y
);
3339 if (e
.detail
== SWT
.MENU_MOUSE
) {
3340 if (fPendingMenuDetectEvent
== null) {
3341 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3342 * Store the event and trigger it later just before handling mouseUp.
3343 * This allows for the method to detect if mouse is used to drag zoom.
3345 fPendingMenuDetectEvent
= e
;
3347 * Prevent the platform to show the menu when returning. The
3348 * menu will be shown (see below) when this method is called
3349 * again during mouseup().
3354 fPendingMenuDetectEvent
= null;
3355 if ((p
.x
>= fTimeProvider
.getNameSpace()) && (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
)) {
3359 if (fDragState
!= DRAG_NONE
) {
3363 int idx
= getItemIndexAtY(p
.y
);
3364 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3366 Item item
= fItemData
.fExpandedItems
[idx
];
3367 ITimeGraphEntry entry
= item
.fEntry
;
3369 /* Send menu event for the time graph entry */
3372 fireMenuEventOnTimeGraphEntry(e
);
3373 Menu menu
= getMenu();
3374 if (e
.doit
&& (menu
!= null)) {
3375 menu
.setVisible(true);
3378 /* Send menu event for time event */
3379 if (entry
.hasTimeEvents()) {
3380 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3381 if (event
!= null) {
3384 fireMenuEventOnTimeEvent(e
);
3386 if (e
.doit
&& (menu
!= null)) {
3387 menu
.setVisible(true);
3395 * Perform the alignment operation.
3398 * the alignment offset
3400 * @see ITmfTimeAligned
3404 public void performAlign(int offset
) {
3405 fTimeProvider
.setNameSpace(offset
);
3409 * Return the time alignment information
3411 * @return the time alignment information
3413 * @see ITmfTimeAligned
3417 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3418 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());