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 color map used by this timegraph viewer.
250 * @return a color map, or <code>null</code> if not set.
252 public Color
[] getEventColorMap() {
253 return fEventColorMap
;
257 * Assign the given time provider
259 * @param timeProvider
262 public void setTimeProvider(ITimeDataProvider timeProvider
) {
263 fTimeProvider
= timeProvider
;
268 * Assign the status line manager
270 * @param statusLineManager
271 * The status line manager, or null to disable status line messages
273 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
274 if (fStatusLineManager
!= null && statusLineManager
== null) {
275 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
277 fStatusLineManager
= statusLineManager
;
281 * Assign the time graph scale
283 * @param timeGraphScale
284 * The time graph scale
286 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
287 fTimeGraphScale
= timeGraphScale
;
291 * Add a selection listener
294 * The listener to add
296 public void addSelectionListener(SelectionListener listener
) {
297 if (listener
== null) {
298 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
300 if (null == fSelectionListeners
) {
301 fSelectionListeners
= new ArrayList
<>();
303 fSelectionListeners
.add(listener
);
307 * Remove a selection listener
310 * The listener to remove
312 public void removeSelectionListener(SelectionListener listener
) {
313 if (null != fSelectionListeners
) {
314 fSelectionListeners
.remove(listener
);
319 * Selection changed callback
321 public void fireSelectionChanged() {
322 if (null != fSelectionListeners
) {
323 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
324 while (it
.hasNext()) {
325 SelectionListener listener
= it
.next();
326 listener
.widgetSelected(null);
330 if (null != fSelectionChangedListeners
) {
331 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
332 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
338 * Default selection callback
340 public void fireDefaultSelection() {
341 if (null != fSelectionListeners
) {
342 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
343 while (it
.hasNext()) {
344 SelectionListener listener
= it
.next();
345 listener
.widgetDefaultSelected(null);
351 * Add a drag selection listener
354 * The listener to add
356 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
357 if (listener
== null) {
358 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
360 if (null == fDragSelectionListeners
) {
361 fDragSelectionListeners
= new ArrayList
<>();
363 fDragSelectionListeners
.add(listener
);
367 * Remove a drag selection listener
370 * The listener to remove
372 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
373 if (null != fDragSelectionListeners
) {
374 fDragSelectionListeners
.remove(listener
);
379 * Drag Selection changed callback
382 * Time interval start
386 public void fireDragSelectionChanged(long start
, long end
) {
387 // check for backward intervals
388 long beginTime
, endTime
;
396 // call the listeners
397 if (null != fDragSelectionListeners
) {
398 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
399 while (it
.hasNext()) {
400 ITimeGraphTimeListener listener
= it
.next();
401 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
407 * Get the traces in the model
409 * @return The array of traces
411 public ITimeGraphEntry
[] getTraces() {
412 return fItemData
.getEntries();
416 * Refresh the data for the thing
418 public void refreshData() {
419 fItemData
.refreshData();
424 * Refresh data for the given traces
427 * The traces to refresh
429 public void refreshData(ITimeGraphEntry
[] traces
) {
430 fItemData
.refreshData(traces
);
435 * Refresh the links (arrows) of this widget
437 * @param events The link event list
439 public void refreshArrows(List
<ILinkEvent
> events
) {
440 fItemData
.refreshArrows(events
);
444 * Get the links (arrows) of this widget
446 * @return The unmodifiable link event list
450 public List
<ILinkEvent
> getArrows() {
451 return Collections
.unmodifiableList(fItemData
.fLinks
);
454 boolean ensureVisibleItem(int idx
, boolean redraw
) {
455 boolean changed
= false;
458 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
459 if (fItemData
.fExpandedItems
[index
].fSelected
) {
464 if (index
>= fItemData
.fExpandedItems
.length
) {
467 if (index
< fTopIndex
) {
474 int page
= countPerPage();
475 if (index
>= fTopIndex
+ page
) {
476 setTopIndex(index
- page
+ 1);
487 * Assign the given index as the top one
492 public void setTopIndex(int idx
) {
493 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
494 index
= Math
.max(0, index
);
500 * Set the top index so that the requested element is at the specified
504 * the time graph entry to be positioned
506 * the requested y-coordinate
509 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
510 Item item
= fItemData
.fItemMap
.get(entry
);
511 if (item
== null || item
.fExpandedIndex
== -1) {
514 int index
= item
.fExpandedIndex
;
515 Rectangle itemRect
= getItemRect(getClientArea(), index
);
516 int delta
= itemRect
.y
+ itemRect
.height
- y
;
517 int topIndex
= getItemIndexAtY(delta
);
518 if (topIndex
!= -1) {
519 setTopIndex(topIndex
);
524 setTopIndex(getExpandedElementCount());
530 * Sets the auto-expand level to be used for new entries discovered when
531 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
532 * . The value 0 means that there is no auto-expand; 1 means that top-level
533 * entries are expanded, but not their children; 2 means that top-level
534 * entries are expanded, and their children, but not grand-children; and so
537 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
541 * non-negative level, or <code>ALL_LEVELS</code> to expand all
544 public void setAutoExpandLevel(int level
) {
545 fAutoExpandLevel
= level
;
549 * Returns the auto-expand level.
551 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
552 * the tree are expanded automatically
553 * @see #setAutoExpandLevel
555 public int getAutoExpandLevel() {
556 return fAutoExpandLevel
;
560 * Get the expanded state of a given entry.
564 * @return true if the entry is expanded, false if collapsed
567 public boolean getExpandedState(ITimeGraphEntry entry
) {
568 Item item
= fItemData
.fItemMap
.get(entry
);
569 return (item
!= null ? item
.fExpanded
: false);
573 * Set the expanded state of a given entry
578 * True if expanded, false if collapsed
580 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
581 Item item
= fItemData
.findItem(entry
);
582 if (item
!= null && item
.fExpanded
!= expanded
) {
583 item
.fExpanded
= expanded
;
584 fItemData
.updateExpandedItems();
590 * Set the expanded state of a given entry to certain relative level.
591 * It will call fireTreeEvent() for each changed entry. At the end
592 * it will call redraw().
597 * level to expand to or negative for all levels
599 * True if expanded, false if collapsed
601 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
602 setExpandedStateInt(entry
, level
, expanded
);
607 * Set the expanded state of a given entry and its children to the first
608 * level that has one collapsed entry.
613 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
614 int level
= findExpandedLevel(entry
);
616 setExpandedStateInt(entry
, level
, true);
622 * Inner class for finding relative level with at least one
625 private class SearchNode
{
626 SearchNode(ITimeGraphEntry e
, int l
) {
630 ITimeGraphEntry entry
;
635 * Finds the relative level with at least one collapsed entry.
639 * @return the found level or -1 if all levels are already expanded.
641 private int findExpandedLevel(ITimeGraphEntry entry
) {
642 Queue
<SearchNode
> queue
= new LinkedList
<>();
643 SearchNode root
= new SearchNode(entry
, 0);
644 SearchNode node
= root
;
647 while (!queue
.isEmpty()) {
648 node
= queue
.remove();
649 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
652 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
653 if (e
.hasChildren()) {
654 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
663 * Set the expanded state of a given entry to certain relative level.
664 * It will call fireTreeEvent() for each changed entry. No redraw is done.
669 * level to expand to or negative for all levels
671 * True if expanded, false if collapsed
673 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
675 if ((level
> 0) || (level
< 0)) {
677 if (entry
.hasChildren()) {
678 for (ITimeGraphEntry e
: entry
.getChildren()) {
679 setExpandedStateInt(e
, level
, expanded
);
683 Item item
= fItemData
.findItem(entry
);
684 if (item
!= null && item
.fExpanded
!= expanded
) {
685 item
.fExpanded
= expanded
;
686 fItemData
.updateExpandedItems();
687 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
692 * Collapses all nodes of the viewer's tree, starting with the root.
694 public void collapseAll() {
695 for (Item item
: fItemData
.fItems
) {
696 item
.fExpanded
= false;
698 fItemData
.updateExpandedItems();
703 * Expands all nodes of the viewer's tree, starting with the root.
705 public void expandAll() {
706 for (Item item
: fItemData
.fItems
) {
707 item
.fExpanded
= true;
709 fItemData
.updateExpandedItems();
714 * Add a tree listener
717 * The listener to add
719 public void addTreeListener(ITimeGraphTreeListener listener
) {
720 if (!fTreeListeners
.contains(listener
)) {
721 fTreeListeners
.add(listener
);
726 * Remove a tree listener
729 * The listener to remove
731 public void removeTreeListener(ITimeGraphTreeListener listener
) {
732 if (fTreeListeners
.contains(listener
)) {
733 fTreeListeners
.remove(listener
);
738 * Tree event callback
743 * The expanded state (true for expanded, false for collapsed)
745 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
746 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
747 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
749 listener
.treeExpanded(event
);
751 listener
.treeCollapsed(event
);
757 * Add a menu listener on {@link ITimeGraphEntry}s
760 * The listener to add
762 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
763 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
764 fTimeGraphEntryMenuListeners
.add(listener
);
769 * Remove a menu listener on {@link ITimeGraphEntry}s
772 * The listener to remove
774 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
775 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
776 fTimeGraphEntryMenuListeners
.remove(listener
);
781 * Menu event callback on {@link ITimeGraphEntry}s
784 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
786 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
787 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
788 listener
.menuDetected(event
);
793 * Add a menu listener on {@link ITimeEvent}s
796 * The listener to add
798 public void addTimeEventMenuListener(MenuDetectListener listener
) {
799 if (!fTimeEventMenuListeners
.contains(listener
)) {
800 fTimeEventMenuListeners
.add(listener
);
805 * Remove a menu listener on {@link ITimeEvent}s
808 * The listener to remove
810 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
811 if (fTimeEventMenuListeners
.contains(listener
)) {
812 fTimeEventMenuListeners
.remove(listener
);
817 * Menu event callback on {@link ITimeEvent}s
820 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
822 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
823 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
824 listener
.menuDetected(event
);
829 public boolean setFocus() {
830 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
831 fHasNamespaceFocus
= true;
833 return super.setFocus();
837 public ISelection
getSelection() {
838 TimeGraphSelection sel
= new TimeGraphSelection();
839 ITimeGraphEntry trace
= getSelectedTrace();
840 if (null != trace
&& null != fTimeProvider
) {
841 long selectedTime
= fTimeProvider
.getSelectionBegin();
842 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
852 * Get the selection object
854 * @return The selection
856 public ISelection
getSelectionTrace() {
857 TimeGraphSelection sel
= new TimeGraphSelection();
858 ITimeGraphEntry trace
= getSelectedTrace();
866 * Enable/disable one of the traces in the model
869 * 1 to enable it, -1 to disable. The method returns immediately
870 * if another value is used.
872 public void selectTrace(int n
) {
873 if ((n
!= 1) && (n
!= -1)) {
877 boolean changed
= false;
878 int lastSelection
= -1;
879 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
880 Item item
= fItemData
.fExpandedItems
[i
];
881 if (item
.fSelected
) {
883 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
884 item
.fSelected
= false;
885 item
= fItemData
.fExpandedItems
[i
+ 1];
886 item
.fSelected
= true;
888 } else if ((-1 == n
) && (i
> 0)) {
889 item
.fSelected
= false;
890 item
= fItemData
.fExpandedItems
[i
- 1];
891 item
.fSelected
= true;
898 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
899 Item item
= fItemData
.fExpandedItems
[0];
900 item
.fSelected
= true;
905 ensureVisibleItem(-1, false);
907 fireSelectionChanged();
915 * 1 for next event, -1 for previous event
917 * true to extend selection range, false for single selection
920 public void selectEvent(int n
, boolean extend
) {
921 if (null == fTimeProvider
) {
924 ITimeGraphEntry trace
= getSelectedTrace();
928 long selectedTime
= fTimeProvider
.getSelectionEnd();
929 long endTime
= fTimeProvider
.getMaxTime();
930 ITimeEvent nextEvent
;
931 if (n
== -1 && selectedTime
> endTime
) {
932 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
934 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
936 if (null == nextEvent
&& n
== -1) {
937 nextEvent
= Utils
.getFirstEvent(trace
);
939 if (null != nextEvent
) {
940 long nextTime
= nextEvent
.getTime();
941 // If last event detected e.g. going back or not moving to a next
943 if (nextTime
<= selectedTime
&& n
== 1) {
944 // Select to the end of this last event
945 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
946 // but not beyond the end of the trace
947 if (nextTime
> endTime
) {
950 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
951 // for previous event go to its end time unless we were already there
952 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
955 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
957 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
959 fireSelectionChanged();
962 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
964 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
966 fireSelectionChanged();
968 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
972 * Select the next event
975 * true to extend selection range, false for single selection
978 public void selectNextEvent(boolean extend
) {
979 selectEvent(1, extend
);
980 // Notify if visible time window has been adjusted
981 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
985 * Select the previous event
988 * true to extend selection range, false for single selection
991 public void selectPrevEvent(boolean extend
) {
992 selectEvent(-1, extend
);
993 // Notify if visible time window has been adjusted
994 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
998 * Select the next trace
1000 public void selectNextTrace() {
1005 * Select the previous trace
1007 public void selectPrevTrace() {
1012 * Scroll left or right by one half window size
1015 * true to scroll left, false to scroll right
1017 public void horizontalScroll(boolean left
) {
1018 long time0
= fTimeProvider
.getTime0();
1019 long time1
= fTimeProvider
.getTime1();
1020 long timeMin
= fTimeProvider
.getMinTime();
1021 long timeMax
= fTimeProvider
.getMaxTime();
1022 long range
= time1
- time0
;
1026 long increment
= Math
.max(1, range
/ 2);
1028 time0
= Math
.max(time0
- increment
, timeMin
);
1029 time1
= time0
+ range
;
1031 time1
= Math
.min(time1
+ increment
, timeMax
);
1032 time0
= time1
- range
;
1034 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1038 * Zoom based on mouse cursor location with mouse scrolling
1040 * @param zoomIn true to zoom in, false to zoom out
1042 public void zoom(boolean zoomIn
) {
1043 int globalX
= getDisplay().getCursorLocation().x
;
1044 Point p
= toControl(globalX
, 0);
1045 int nameSpace
= fTimeProvider
.getNameSpace();
1046 int timeSpace
= fTimeProvider
.getTimeSpace();
1047 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1048 long time0
= fTimeProvider
.getTime0();
1049 long time1
= fTimeProvider
.getTime1();
1050 long interval
= time1
- time0
;
1051 if (interval
== 0) {
1053 } // to allow getting out of single point interval
1056 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1058 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1060 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1061 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1062 long newTime1
= newTime0
+ newInterval
;
1063 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1067 * zoom in using single click
1069 public void zoomIn() {
1070 long prevTime0
= fTimeProvider
.getTime0();
1071 long prevTime1
= fTimeProvider
.getTime1();
1072 long prevRange
= prevTime1
- prevTime0
;
1073 if (prevRange
== 0) {
1076 ITimeDataProvider provider
= fTimeProvider
;
1077 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1078 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1079 selTime
= (prevTime0
+ prevTime1
) / 2;
1081 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1082 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1084 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1086 if (inaccuracy
> 0 && inaccuracy
< 100) {
1087 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1091 long min
= fTimeProvider
.getMinTimeInterval();
1092 if ((time1
- time0
) < min
) {
1093 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1094 time1
= time0
+ min
;
1097 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1101 * zoom out using single click
1103 public void zoomOut() {
1104 long prevTime0
= fTimeProvider
.getTime0();
1105 long prevTime1
= fTimeProvider
.getTime1();
1106 ITimeDataProvider provider
= fTimeProvider
;
1107 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1108 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1109 selTime
= (prevTime0
+ prevTime1
) / 2;
1111 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1112 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1114 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1115 if (inaccuracy
> 0 && inaccuracy
< 100) {
1116 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1120 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1127 * true to zoom in, false to zoom out
1130 public void verticalZoom(boolean zoomIn
) {
1132 fHeightAdjustment
++;
1134 fHeightAdjustment
--;
1136 fItemData
.refreshData();
1141 * Reset the vertical zoom to default.
1145 public void resetVerticalZoom() {
1146 fHeightAdjustment
= 0;
1147 fItemData
.refreshData();
1152 * Set the grid lines visibility. The default is true.
1155 * true to show the grid lines, false otherwise
1158 public void setGridLinesVisible(boolean visible
) {
1159 fGridLinesVisible
= visible
;
1163 * Get the grid lines visibility.
1165 * @return true if the grid lines are visible, false otherwise
1168 public boolean getGridLinesVisible() {
1169 return fGridLinesVisible
;
1173 * Set the grid line color. The default is SWT.COLOR_GRAY.
1176 * the grid line color
1179 public void setGridLineColor(Color color
) {
1180 fGridLineColor
= color
;
1184 * Get the grid line color.
1186 * @return the grid line color
1189 public Color
getGridLineColor() {
1190 return fGridLineColor
;
1194 * Set the markers list.
1197 * The markers list, or null
1200 public void setMarkers(List
<IMarkerEvent
> markers
) {
1205 * Get the markers list.
1207 * @return The markers list, or null
1210 public List
<IMarkerEvent
> getMarkers() {
1215 * Set the markers visibility. The default is true.
1218 * true to show the markers, false otherwise
1221 public void setMarkersVisible(boolean visible
) {
1222 fMarkersVisible
= visible
;
1226 * Get the markers visibility.
1228 * @return true if the markers are visible, false otherwise
1231 public boolean getMarkersVisible() {
1232 return fMarkersVisible
;
1238 * @param hideArrows true to hide arrows
1240 public void hideArrows(boolean hideArrows
) {
1241 fHideArrows
= hideArrows
;
1245 * Follow the arrow forward
1248 * true to extend selection range, false for single selection
1251 public void followArrowFwd(boolean extend
) {
1252 ITimeGraphEntry trace
= getSelectedTrace();
1253 if (trace
== null) {
1256 long selectedTime
= fTimeProvider
.getSelectionEnd();
1257 for (ILinkEvent link
: fItemData
.fLinks
) {
1258 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1259 selectItem(link
.getDestinationEntry(), false);
1260 if (link
.getDuration() != 0) {
1262 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1264 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1266 // Notify if visible time window has been adjusted
1267 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1269 fireSelectionChanged();
1270 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1274 selectNextEvent(extend
);
1278 * Follow the arrow backward
1281 * true to extend selection range, false for single selection
1284 public void followArrowBwd(boolean extend
) {
1285 ITimeGraphEntry trace
= getSelectedTrace();
1286 if (trace
== null) {
1289 long selectedTime
= fTimeProvider
.getSelectionEnd();
1290 for (ILinkEvent link
: fItemData
.fLinks
) {
1291 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1292 selectItem(link
.getEntry(), false);
1293 if (link
.getDuration() != 0) {
1295 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1297 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1299 // Notify if visible time window has been adjusted
1300 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1302 fireSelectionChanged();
1303 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1307 selectPrevEvent(extend
);
1311 * Return the currently selected trace
1313 * @return The entry matching the trace
1315 public ITimeGraphEntry
getSelectedTrace() {
1316 ITimeGraphEntry trace
= null;
1317 int idx
= getSelectedIndex();
1319 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1325 * Retrieve the index of the currently selected item
1329 public int getSelectedIndex() {
1331 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1332 Item item
= fItemData
.fExpandedItems
[i
];
1333 if (item
.fSelected
) {
1341 boolean toggle(int idx
) {
1342 boolean toggled
= false;
1343 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1344 Item item
= fItemData
.fExpandedItems
[idx
];
1345 if (item
.fHasChildren
) {
1346 item
.fExpanded
= !item
.fExpanded
;
1347 fItemData
.updateExpandedItems();
1350 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1357 * Gets the index of the item at the given location.
1361 * @return the index of the item at the given location, of -1 if none.
1363 protected int getItemIndexAtY(int y
) {
1366 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1367 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1373 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1374 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1383 boolean isOverSplitLine(int x
) {
1384 if (x
< 0 || null == fTimeProvider
) {
1387 int nameWidth
= fTimeProvider
.getNameSpace();
1388 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1392 * Gets the {@link ITimeGraphEntry} at the given location.
1395 * a point in the widget
1396 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1400 public ITimeGraphEntry
getEntry(Point pt
) {
1401 int idx
= getItemIndexAtY(pt
.y
);
1402 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1406 * Return the arrow event closest to the given point that is no further than
1407 * a maximum distance.
1410 * a point in the widget
1411 * @return The closest arrow event, or null if there is none close enough.
1413 protected ILinkEvent
getArrow(Point pt
) {
1417 ILinkEvent linkEvent
= null;
1418 double minDistance
= Double
.MAX_VALUE
;
1419 for (ILinkEvent event
: fItemData
.fLinks
) {
1420 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1424 int x2
= x1
+ rect
.width
;
1425 int y2
= y1
+ rect
.height
;
1426 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1427 if (minDistance
> d
) {
1433 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1440 public int getXForTime(long time
) {
1441 if (null == fTimeProvider
) {
1444 long time0
= fTimeProvider
.getTime0();
1445 long time1
= fTimeProvider
.getTime1();
1446 int width
= getSize().x
;
1447 int nameSpace
= fTimeProvider
.getNameSpace();
1448 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1449 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1454 public long getTimeAtX(int coord
) {
1455 if (null == fTimeProvider
) {
1459 Point size
= getSize();
1460 long time0
= fTimeProvider
.getTime0();
1461 long time1
= fTimeProvider
.getTime1();
1462 int nameWidth
= fTimeProvider
.getNameSpace();
1463 final int x
= coord
- nameWidth
;
1464 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1465 if (x
>= 0 && size
.x
>= nameWidth
) {
1466 if (time1
- time0
> timeWidth
) {
1467 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1468 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1470 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1471 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1477 void selectItem(int idx
, boolean addSelection
) {
1478 boolean changed
= false;
1480 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1481 Item item
= fItemData
.fExpandedItems
[idx
];
1482 changed
= !item
.fSelected
;
1483 item
.fSelected
= true;
1486 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1487 Item item
= fItemData
.fExpandedItems
[i
];
1488 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1491 item
.fSelected
= i
== idx
;
1494 changed
|= ensureVisibleItem(idx
, true);
1501 * Callback for item selection
1504 * The entry matching the trace
1505 * @param addSelection
1506 * If the selection is added or removed
1508 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1509 int idx
= fItemData
.findItemIndex(trace
);
1510 selectItem(idx
, addSelection
);
1514 * Retrieve the number of entries shown per page.
1518 public int countPerPage() {
1519 int height
= getSize().y
;
1522 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1523 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1524 if (ySum
>= height
) {
1529 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1530 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1531 if (ySum
>= height
) {
1540 * Get the index of the top element
1544 public int getTopIndex() {
1549 * Get the number of expanded (visible) items
1551 * @return The count of expanded (visible) items
1553 public int getExpandedElementCount() {
1554 return fItemData
.fExpandedItems
.length
;
1558 * Get an array of all expanded (visible) elements
1560 * @return The expanded (visible) elements
1562 public ITimeGraphEntry
[] getExpandedElements() {
1563 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1564 for (Item item
: fItemData
.fExpandedItems
) {
1565 elements
.add(item
.fEntry
);
1567 return elements
.toArray(new ITimeGraphEntry
[0]);
1571 * Get the expanded (visible) element at the specified index.
1575 * @return The expanded (visible) element or null if out of range
1578 public ITimeGraphEntry
getExpandedElement(int index
) {
1579 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1582 return fItemData
.fExpandedItems
[index
].fEntry
;
1585 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1586 Rectangle rect
= getItemRect(bounds
, idx
);
1587 rect
.width
= nameWidth
;
1591 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1592 Rectangle rect
= getItemRect(bounds
, idx
);
1593 rect
.x
+= nameWidth
;
1594 rect
.width
-= nameWidth
;
1598 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1600 if (idx
>= fTopIndex
) {
1601 for (int i
= fTopIndex
; i
< idx
; i
++) {
1602 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1605 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1606 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1609 int y
= bounds
.y
+ ySum
;
1610 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1611 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1615 void paint(Rectangle bounds
, PaintEvent e
) {
1618 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1622 fIdealNameSpace
= 0;
1623 int nameSpace
= fTimeProvider
.getNameSpace();
1625 // draw the background layer
1626 drawBackground(bounds
, nameSpace
, gc
);
1628 // draw the grid lines
1629 drawGridLines(bounds
, gc
);
1631 // draw the background markers
1632 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1635 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1637 // draw the foreground markers
1638 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1640 // draw the links (arrows)
1641 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1643 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1645 int alpha
= gc
.getAlpha();
1648 long time0
= fTimeProvider
.getTime0();
1649 long time1
= fTimeProvider
.getTime1();
1650 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1651 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1652 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1653 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1654 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1656 // draw selection lines
1657 if (fDragState
!= DRAG_SELECTION
) {
1658 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1659 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1660 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1663 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1664 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1669 // draw selection background
1670 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1671 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1672 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1673 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1675 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1676 } else if (x0
- x1
> 1) {
1677 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1681 // draw drag selection background
1682 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1683 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1684 if (fDragX0
< fDragX
) {
1685 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1686 } else if (fDragX0
> fDragX
) {
1687 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1692 if (DRAG_SPLIT_LINE
== fDragState
) {
1693 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1694 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1695 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1696 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1697 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1698 if (fDragX
!= fDragX0
) {
1699 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1701 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1702 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1703 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1704 if (fDragX
!= fDragX0
) {
1705 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1707 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1708 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1709 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1716 * Draw the background layer. Fills the background of the control's name
1717 * space and states space, updates the background of items if necessary,
1718 * and draws the item's name text and middle line.
1721 * The bounds of the control
1723 * The name space width
1728 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1729 // draw empty name space background
1730 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1731 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1733 // draw empty states space background
1734 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1735 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1737 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1738 Rectangle itemRect
= getItemRect(bounds
, i
);
1739 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1742 Item item
= fItemData
.fExpandedItems
[i
];
1743 // draw the background of selected item and items with no time events
1744 if (! item
.fEntry
.hasTimeEvents()) {
1745 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1746 gc
.fillRectangle(itemRect
);
1747 } else if (item
.fSelected
) {
1748 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1749 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1750 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1751 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1753 // draw the name and middle line
1754 if (! item
.fEntry
.hasTimeEvents()) {
1755 drawName(item
, itemRect
, gc
);
1757 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1758 drawName(item
, nameRect
, gc
);
1759 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1760 drawMidLine(rect
, gc
);
1766 * Draw the grid lines
1769 * The bounds of the control
1774 public void drawGridLines(Rectangle bounds
, GC gc
) {
1775 if (!fGridLinesVisible
) {
1778 gc
.setForeground(fGridLineColor
);
1779 gc
.setAlpha(fGridLineColor
.getAlpha());
1780 for (int x
: fTimeGraphScale
.getTickList()) {
1781 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1790 * The rectangle of the area
1791 * @param timeProvider
1794 * The list of markers
1796 * true to draw the foreground markers, false otherwise
1798 * The width reserved for the names
1800 * Reference to the SWT GC object
1803 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1804 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1807 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1808 /* the list can grow concurrently but cannot shrink */
1809 for (int i
= 0; i
< markers
.size(); i
++) {
1810 IMarkerEvent marker
= markers
.get(i
);
1811 if (marker
.isForeground() == foreground
) {
1812 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1815 gc
.setClipping((Rectangle
) null);
1819 * Draw a single marker
1824 * The bounds of the control
1825 * @param timeProvider
1828 * The width reserved for the name
1830 * Reference to the SWT GC object
1833 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1834 Rectangle rect
= Utils
.clone(bounds
);
1835 if (marker
.getEntry() != null) {
1836 int index
= fItemData
.findItemIndex(marker
.getEntry());
1840 rect
= getStatesRect(bounds
, index
, nameSpace
);
1841 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1845 int x0
= getXForTime(marker
.getTime());
1846 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1847 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1850 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1851 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1853 Color color
= getColorScheme().getColor(marker
.getColor());
1854 gc
.setBackground(color
);
1855 gc
.setAlpha(color
.getAlpha());
1856 gc
.fillRectangle(rect
);
1858 String label
= marker
.getLabel();
1859 if (label
!= null && marker
.getEntry() != null) {
1860 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1861 gc
.setForeground(color
);
1862 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1867 * Draw many items at once
1870 * The bounds of the control
1871 * @param timeProvider
1874 * The array items to draw
1876 * The index of the first element to draw
1878 * The name space width
1882 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1883 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1884 for (int i
= topIndex
; i
< items
.length
; i
++) {
1885 Item item
= items
[i
];
1886 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1896 * The bounds of the control
1897 * @param timeProvider
1900 * The expanded item index
1902 * The name space width
1906 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1907 Rectangle itemRect
= getItemRect(bounds
, i
);
1908 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1912 ITimeGraphEntry entry
= item
.fEntry
;
1913 long time0
= timeProvider
.getTime0();
1914 long time1
= timeProvider
.getTime1();
1915 long selectedTime
= fTimeProvider
.getSelectionEnd();
1917 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1918 if (rect
.isEmpty() || (time1
<= time0
)) {
1919 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1923 boolean selected
= item
.fSelected
;
1924 // K pixels per second
1925 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1927 if (item
.fEntry
.hasTimeEvents()) {
1928 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1929 fillSpace(rect
, gc
, selected
);
1931 int margins
= getMarginForHeight(rect
.height
);
1932 int height
= rect
.height
- margins
;
1933 int topMargin
= (margins
+ 1) / 2;
1934 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1936 /* Set the font for this item */
1937 setFontForHeight(height
, gc
);
1939 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1940 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1943 while (iterator
.hasNext()) {
1944 ITimeEvent event
= iterator
.next();
1945 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1946 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1947 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1948 // event is out of bounds
1951 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1952 stateRect
.x
= Math
.max(rect
.x
, x
);
1953 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1954 if (stateRect
.x
== lastX
) {
1955 stateRect
.width
-= 1;
1956 if (stateRect
.width
> 0) {
1957 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1958 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1962 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1963 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1964 lastX
= stateRect
.x
;
1967 gc
.setClipping((Rectangle
) null);
1969 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1976 * The bounds of the control
1977 * @param timeProvider
1980 * The list of link events
1982 * The name space width
1986 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1987 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1991 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1992 /* the list can grow concurrently but cannot shrink */
1993 for (int i
= 0; i
< links
.size(); i
++) {
1994 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1996 gc
.setClipping((Rectangle
) null);
2000 * Draws a link type event
2003 * The link event to draw
2005 * The bounds of the control
2006 * @param timeProvider
2009 * The name space width
2013 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2014 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2017 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2018 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2019 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2021 if ((srcIndex
== -1) || (destIndex
== -1)) {
2025 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2026 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2028 int x0
= getXForTime(event
.getTime());
2029 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2031 // limit the x-coordinates to prevent integer overflow in calculations
2032 // and also GC.drawLine doesn't draw properly with large coordinates
2033 final int limit
= Integer
.MAX_VALUE
/ 1024;
2034 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2035 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2037 int y0
= src
.y
+ src
.height
/ 2;
2038 int y1
= dst
.y
+ dst
.height
/ 2;
2039 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2048 * Time event for which we're drawing the arrow
2050 * The arrow rectangle
2053 * @return true if the arrow was drawn
2055 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2056 Rectangle rect
, GC gc
) {
2061 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2065 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2068 Color stateColor
= null;
2069 if (colorIdx
< fEventColorMap
.length
) {
2070 stateColor
= fEventColorMap
[colorIdx
];
2072 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2075 gc
.setForeground(stateColor
);
2076 gc
.setBackground(stateColor
);
2078 /* Draw the arrow */
2079 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2080 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2083 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2088 * @author Francis Giraldeau
2091 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2093 * The algorithm was taken from this site, not the code itself
2095 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2098 double cos
= 0.9510;
2099 double sin
= 0.3090;
2100 long lenx
= x1
- x0
;
2101 long leny
= y1
- y0
;
2102 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2104 double dx
= factor
* lenx
/ len
;
2105 double dy
= factor
* leny
/ len
;
2106 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2107 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2108 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2109 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2110 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2111 gc
.fillPolygon(arrow
);
2115 * Draw the name of an item.
2120 * The bounds of the item's name space
2124 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2125 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2127 // No name to be drawn
2128 if (fTimeProvider
.getNameSpace() == 0) {
2132 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2133 setFontForHeight(height
, gc
);
2135 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2136 if (item
.fHasChildren
) {
2137 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2138 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2139 Rectangle rect
= Utils
.clone(bounds
);
2140 rect
.x
+= leftMargin
;
2141 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2142 rect
.width
= EXPAND_SIZE
;
2143 rect
.height
= EXPAND_SIZE
;
2144 gc
.fillRectangle(rect
);
2145 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2146 int midy
= rect
.y
+ rect
.height
/ 2;
2147 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2148 if (!item
.fExpanded
) {
2149 int midx
= rect
.x
+ rect
.width
/ 2;
2150 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2153 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2155 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2158 int imgHeight
= img
.getImageData().height
;
2159 int imgWidth
= img
.getImageData().width
;
2161 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2162 gc
.drawImage(img
, x
, y
);
2163 leftMargin
+= imgWidth
+ MARGIN
;
2165 String name
= item
.fName
;
2166 Point size
= gc
.stringExtent(name
);
2167 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2168 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2170 if (hasTimeEvents
) {
2171 // cut long string with "..."
2172 int width
= bounds
.width
- leftMargin
;
2174 while (size
.x
> width
&& name
.length() > 1) {
2176 name
= name
.substring(0, name
.length() - 1);
2177 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2180 name
+= "..."; //$NON-NLS-1$
2183 Rectangle rect
= Utils
.clone(bounds
);
2184 rect
.x
+= leftMargin
;
2185 rect
.width
-= leftMargin
;
2187 if (rect
.width
> 0) {
2188 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2189 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2190 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2191 leftMargin
+= textWidth
+ MARGIN
;
2193 if (hasTimeEvents
) {
2195 rect
.x
= bounds
.x
+ leftMargin
;
2197 rect
.width
= bounds
.width
- rect
.x
;
2198 drawMidLine(rect
, gc
);
2204 * Draw the state (color fill)
2209 * Time event for which we're drawing the state
2211 * The state rectangle
2215 * Is this time event currently selected (so it appears
2217 * @param timeSelected
2218 * Is the timestamp currently selected
2219 * @return true if the state was drawn
2221 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2222 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2224 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2225 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2228 boolean visible
= rect
.width
== 0 ?
false : true;
2229 rect
.width
= Math
.max(1, rect
.width
);
2230 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2231 gc
.setForeground(black
);
2233 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2235 // Only draw the top and bottom borders
2236 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2237 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2238 if (rect
.width
== 1) {
2239 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2242 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2245 Color stateColor
= null;
2246 if (colorIdx
< fEventColorMap
.length
) {
2247 stateColor
= fEventColorMap
[colorIdx
];
2252 boolean reallySelected
= timeSelected
&& selected
;
2253 // fill all rect area
2254 gc
.setBackground(stateColor
);
2256 gc
.fillRectangle(rect
);
2257 } else if (fBlendSubPixelEvents
) {
2259 gc
.fillRectangle(rect
);
2263 if (reallySelected
) {
2264 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2265 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2268 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2270 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2275 * Fill an item's states rectangle
2278 * The states rectangle
2282 * true if the item is selected
2284 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2285 /* Nothing to draw */
2289 * Draw a line at the middle height of a rectangle
2296 private void drawMidLine(Rectangle rect
, GC gc
) {
2297 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2298 int midy
= rect
.y
+ rect
.height
/ 2;
2299 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2302 private static int getMarginForHeight(int height
) {
2304 * State rectangle is smaller than the item bounds when height is > 4.
2305 * Don't use any margin if the height is below or equal that threshold.
2306 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2307 * pixels for the state height, but with a minimum margin of 1.
2309 final int MARGIN_THRESHOLD
= 4;
2310 final int PREFERRED_HEIGHT
= 13;
2311 final int MIN_MARGIN
= 1;
2312 final int MAX_MARGIN
= 6;
2313 return height
<= MARGIN_THRESHOLD ?
0 :
2314 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2317 private void setFontForHeight(int pixels
, GC gc
) {
2318 /* convert font height from pixels to points */
2319 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2320 Font font
= fFonts
.get(height
);
2322 FontData fontData
= gc
.getFont().getFontData()[0];
2323 fontData
.setHeight(height
);
2324 font
= new Font(gc
.getDevice(), fontData
);
2325 fFonts
.put(height
, font
);
2331 public void keyTraversed(TraverseEvent e
) {
2332 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2338 public void keyPressed(KeyEvent e
) {
2340 if (fItemData
.fExpandedItems
.length
== 0) {
2343 if (SWT
.HOME
== e
.keyCode
) {
2345 } else if (SWT
.END
== e
.keyCode
) {
2346 idx
= fItemData
.fExpandedItems
.length
- 1;
2347 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2348 idx
= getSelectedIndex();
2351 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2354 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2355 idx
= getSelectedIndex();
2358 } else if (idx
> 0) {
2361 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2362 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2363 selectPrevEvent(extend
);
2364 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2365 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2366 selectNextEvent(extend
);
2367 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2368 int page
= countPerPage();
2369 idx
= getSelectedIndex();
2374 if (idx
>= fItemData
.fExpandedItems
.length
) {
2375 idx
= fItemData
.fExpandedItems
.length
- 1;
2377 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2378 int page
= countPerPage();
2379 idx
= getSelectedIndex();
2387 } else if (SWT
.CR
== e
.keyCode
) {
2388 idx
= getSelectedIndex();
2390 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2393 fireDefaultSelection();
2397 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2398 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2400 if (fVerticalZoomAlignEntry
!= null) {
2401 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2403 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2404 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2405 verticalZoom(false);
2406 if (fVerticalZoomAlignEntry
!= null) {
2407 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2409 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2410 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2411 resetVerticalZoom();
2412 if (fVerticalZoomAlignEntry
!= null) {
2413 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2415 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2416 if (fHasNamespaceFocus
) {
2417 ITimeGraphEntry entry
= getSelectedTrace();
2418 setExpandedState(entry
, 0, true);
2422 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2423 if (fHasNamespaceFocus
) {
2424 ITimeGraphEntry entry
= getSelectedTrace();
2425 if ((entry
!= null) && entry
.hasChildren()) {
2426 setExpandedState(entry
, -1, false);
2431 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2432 if (fHasNamespaceFocus
) {
2433 ITimeGraphEntry entry
= getSelectedTrace();
2434 if ((entry
!= null) && entry
.hasChildren()) {
2435 setExpandedStateLevel(entry
);
2440 selectItem(idx
, false);
2441 fireSelectionChanged();
2443 int x
= toControl(e
.display
.getCursorLocation()).x
;
2444 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2448 public void keyReleased(KeyEvent e
) {
2449 int x
= toControl(e
.display
.getCursorLocation()).x
;
2450 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2454 public void focusGained(FocusEvent e
) {
2457 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2461 public void focusLost(FocusEvent e
) {
2463 if (DRAG_NONE
!= fDragState
) {
2465 fDragState
= DRAG_NONE
;
2468 updateStatusLine(NO_STATUS
);
2472 * @return If the current view is focused
2474 public boolean isInFocus() {
2479 * Provide the possibility to control the wait cursor externally e.g. data
2480 * requests in progress
2482 * @param waitInd Should we wait indefinitely?
2484 public void waitCursor(boolean waitInd
) {
2485 // Update cursor as indicated
2487 setCursor(fWaitCursor
);
2493 private void updateCursor(int x
, int stateMask
) {
2494 // if Wait cursor not active, check for the need to change the cursor
2495 if (getCursor() == fWaitCursor
) {
2498 Cursor cursor
= null;
2499 if (fDragState
== DRAG_SPLIT_LINE
) {
2500 } else if (fDragState
== DRAG_SELECTION
) {
2501 cursor
= fResizeCursor
;
2502 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2503 cursor
= fDragCursor
;
2504 } else if (fDragState
== DRAG_ZOOM
) {
2505 cursor
= fZoomCursor
;
2506 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2507 cursor
= fDragCursor
;
2508 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2509 cursor
= fResizeCursor
;
2510 } else if (!isOverSplitLine(x
)) {
2511 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2512 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2513 int xBegin
= getXForTime(selectionBegin
);
2514 int xEnd
= getXForTime(selectionEnd
);
2515 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2516 cursor
= fResizeCursor
;
2519 if (getCursor() != cursor
) {
2525 * Update the status line following a change of selection.
2529 public void updateStatusLine() {
2530 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2533 private void updateStatusLine(int x
) {
2534 // use the time provider of the time graph scale for the status line
2535 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2536 if (fStatusLineManager
== null || null == tdp
||
2537 tdp
.getTime0() == tdp
.getTime1()) {
2540 TimeFormat tf
= tdp
.getTimeFormat();
2541 Resolution res
= Resolution
.NANOSEC
;
2542 StringBuilder message
= new StringBuilder();
2543 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2544 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2545 long time
= getTimeAtX(x
);
2547 if (tdp
instanceof ITimeDataProviderConverter
) {
2548 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2550 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2552 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2553 Utils
.formatTime(time
, tf
, res
)
2557 long selectionBegin
= tdp
.getSelectionBegin();
2558 long selectionEnd
= tdp
.getSelectionEnd();
2559 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2561 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2562 Utils
.formatTime(selectionBegin
, tf
, res
)
2564 if (selectionBegin
!= selectionEnd
) {
2565 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2567 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2568 Utils
.formatTime(selectionEnd
, tf
, res
),
2569 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2572 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2573 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2574 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2575 if (tdp
instanceof ITimeDataProviderConverter
) {
2576 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2577 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2579 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2581 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2582 Utils
.formatTime(time0
, tf
, res
),
2583 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2584 Utils
.formatTime(time
, tf
, res
),
2585 Utils
.formatDelta(time
- time0
, tf
, res
)
2588 fStatusLineManager
.setMessage(message
.toString());
2592 public void mouseMove(MouseEvent e
) {
2593 if (null == fTimeProvider
) {
2596 Point size
= getSize();
2597 if (DRAG_TRACE_ITEM
== fDragState
) {
2598 int nameWidth
= fTimeProvider
.getNameSpace();
2599 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2601 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2602 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2603 long time1
= fTime1bak
- timeDelta
;
2604 long maxTime
= fTimeProvider
.getMaxTime();
2605 if (time1
> maxTime
) {
2608 long time0
= time1
- (fTime1bak
- fTime0bak
);
2609 if (time0
< fTimeProvider
.getMinTime()) {
2610 time0
= fTimeProvider
.getMinTime();
2611 time1
= time0
+ (fTime1bak
- fTime0bak
);
2613 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2615 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2617 fTimeProvider
.setNameSpace(e
.x
);
2618 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2619 } else if (DRAG_SELECTION
== fDragState
) {
2620 if (fDragBeginMarker
) {
2621 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2623 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2626 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2627 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2628 } else if (DRAG_ZOOM
== fDragState
) {
2629 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2631 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2632 } else if (DRAG_NONE
== fDragState
) {
2633 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2634 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2637 fMouseOverSplitLine
= mouseOverSplitLine
;
2640 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2641 fHasNamespaceFocus
= false;
2643 fHasNamespaceFocus
= true;
2645 updateCursor(e
.x
, e
.stateMask
);
2646 updateStatusLine(e
.x
);
2650 public void mouseDoubleClick(MouseEvent e
) {
2651 if (null == fTimeProvider
) {
2654 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2655 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2656 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2657 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2658 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2661 fMouseOverSplitLine
= mouseOverSplitLine
;
2662 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2665 int idx
= getItemIndexAtY(e
.y
);
2667 selectItem(idx
, false);
2668 fireDefaultSelection();
2674 public void mouseDown(MouseEvent e
) {
2675 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2676 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2677 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2681 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2682 int nameSpace
= fTimeProvider
.getNameSpace();
2683 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2684 fDragState
= DRAG_SPLIT_LINE
;
2685 fDragButton
= e
.button
;
2688 fTime0bak
= fTimeProvider
.getTime0();
2689 fTime1bak
= fTimeProvider
.getTime1();
2691 updateCursor(e
.x
, e
.stateMask
);
2695 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2696 int nameSpace
= fTimeProvider
.getNameSpace();
2697 idx
= getItemIndexAtY(e
.y
);
2699 Item item
= fItemData
.fExpandedItems
[idx
];
2700 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2704 selectItem(idx
, false);
2705 fireSelectionChanged();
2707 selectItem(idx
, false); // clear selection
2708 fireSelectionChanged();
2710 long hitTime
= getTimeAtX(e
.x
);
2714 fDragState
= DRAG_SELECTION
;
2715 fDragBeginMarker
= false;
2716 fDragButton
= e
.button
;
2719 fDragTime0
= getTimeAtX(fDragX0
);
2720 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2721 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2722 int xBegin
= getXForTime(selectionBegin
);
2723 int xEnd
= getXForTime(selectionEnd
);
2724 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2725 long time
= getTimeAtX(e
.x
);
2726 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2727 fDragBeginMarker
= true;
2730 fDragTime0
= selectionEnd
;
2733 fDragTime0
= selectionBegin
;
2736 long time
= getTimeAtX(e
.x
);
2737 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2738 fDragBeginMarker
= true;
2741 fDragTime0
= selectionEnd
;
2742 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2744 fDragTime0
= selectionBegin
;
2747 fTime0bak
= fTimeProvider
.getTime0();
2748 fTime1bak
= fTimeProvider
.getTime1();
2750 updateCursor(e
.x
, e
.stateMask
);
2751 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2753 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2754 long hitTime
= getTimeAtX(e
.x
);
2757 fDragState
= DRAG_TRACE_ITEM
;
2758 fDragButton
= e
.button
;
2761 fTime0bak
= fTimeProvider
.getTime0();
2762 fTime1bak
= fTimeProvider
.getTime1();
2763 updateCursor(e
.x
, e
.stateMask
);
2765 } else if (3 == e
.button
) {
2766 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2768 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2770 fDragTime0
= getTimeAtX(fDragX0
);
2771 fDragState
= DRAG_ZOOM
;
2772 fDragButton
= e
.button
;
2774 updateCursor(e
.x
, e
.stateMask
);
2775 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2777 idx
= getItemIndexAtY(e
.y
);
2778 selectItem(idx
, false);
2779 fireSelectionChanged();
2785 public void mouseUp(MouseEvent e
) {
2786 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2787 menuDetected(fPendingMenuDetectEvent
);
2789 if (DRAG_NONE
!= fDragState
) {
2791 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2792 if (fDragX
!= fDragX0
) {
2793 fTimeProvider
.notifyStartFinishTime();
2795 fDragState
= DRAG_NONE
;
2796 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2797 fDragState
= DRAG_NONE
;
2799 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2800 if (fDragX
== fDragX0
) { // click without selecting anything
2801 long time
= getTimeAtX(e
.x
);
2802 fTimeProvider
.setSelectedTimeNotify(time
, false);
2804 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2805 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2806 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2808 fDragState
= DRAG_NONE
;
2810 fTimeGraphScale
.setDragRange(-1, -1);
2811 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2812 int nameWidth
= fTimeProvider
.getNameSpace();
2813 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2814 long time0
= getTimeAtX(fDragX0
);
2815 long time1
= getTimeAtX(fDragX
);
2816 if (time0
< time1
) {
2817 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2819 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2824 fDragState
= DRAG_NONE
;
2825 fTimeGraphScale
.setDragRange(-1, -1);
2828 updateCursor(e
.x
, e
.stateMask
);
2829 updateStatusLine(e
.x
);
2833 public void mouseEnter(MouseEvent e
) {
2837 public void mouseExit(MouseEvent e
) {
2838 if (fMouseOverSplitLine
) {
2839 fMouseOverSplitLine
= false;
2842 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2846 public void mouseHover(MouseEvent e
) {
2850 public void mouseScrolled(MouseEvent e
) {
2851 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2856 * On some platforms the mouse scroll event is sent to the
2857 * control that has focus even if it is not under the cursor.
2858 * Handle the event only if over the time graph control.
2860 Point size
= getSize();
2861 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2862 if (!bounds
.contains(e
.x
, e
.y
)) {
2866 boolean horizontalZoom
= false;
2867 boolean horizontalScroll
= false;
2868 boolean verticalZoom
= false;
2869 boolean verticalScroll
= false;
2871 // over the time graph control
2872 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2873 verticalZoom
= true;
2874 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2875 // over the name space
2876 verticalScroll
= true;
2878 // over the state area
2879 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2880 // over the state area, CTRL pressed
2881 horizontalZoom
= true;
2882 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2883 // over the state area, SHIFT pressed
2884 horizontalScroll
= true;
2886 // over the state area, no modifier pressed
2887 verticalScroll
= true;
2891 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2892 verticalZoom(e
.count
> 0);
2893 if (fVerticalZoomAlignEntry
!= null) {
2894 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2896 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2898 } else if (horizontalScroll
) {
2899 horizontalScroll(e
.count
> 0);
2900 } else if (verticalScroll
){
2901 setTopIndex(getTopIndex() - e
.count
);
2906 * Get the vertical zoom alignment entry and position based on the current
2907 * selection. If there is no selection or if the selection is not visible,
2908 * return an alignment entry with a null time graph entry.
2910 * @return a map entry where the key is the selection's time graph entry and
2911 * the value is the center y-coordinate of that entry, or null
2913 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2914 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2915 if (alignEntry
!= null) {
2918 int index
= getSelectedIndex();
2919 if (index
== -1 || index
>= getExpandedElementCount()) {
2920 return new SimpleEntry
<>(null, 0);
2922 Rectangle bounds
= getClientArea();
2923 Rectangle itemRect
= getItemRect(bounds
, index
);
2924 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2925 /* selection is not visible */
2926 return new SimpleEntry
<>(null, 0);
2928 ITimeGraphEntry entry
= getExpandedElement(index
);
2929 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2930 return new SimpleEntry
<>(entry
, y
);
2934 * Get the vertical zoom alignment entry and position at the specified
2938 * the cursor y-coordinate
2939 * @return a map entry where the key is the time graph entry under the
2940 * cursor and the value is the cursor y-coordinate
2942 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2943 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2944 if (alignEntry
!= null) {
2947 int index
= getItemIndexAtY(y
);
2949 index
= getExpandedElementCount() - 1;
2951 ITimeGraphEntry entry
= getExpandedElement(index
);
2952 return new SimpleEntry
<>(entry
, y
);
2956 * Get the vertical zoom alignment entry and position if there is an ongoing
2957 * one and we are within the vertical zoom delay, or otherwise return null.
2959 * @return a map entry where the key is a time graph entry and the value is
2960 * a y-coordinate, or null
2962 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2963 long currentTimeMillis
= System
.currentTimeMillis();
2964 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2966 * If the vertical zoom is triggered repeatedly in a short amount of
2967 * time, use the initial event's entry and position.
2969 fVerticalZoomAlignTime
= currentTimeMillis
;
2970 return fVerticalZoomAlignEntry
;
2972 fVerticalZoomAlignTime
= currentTimeMillis
;
2977 public void handleEvent(Event event
) {
2978 if (event
.type
== SWT
.MouseWheel
) {
2979 // prevent horizontal scrolling when the mouse wheel is used to
2980 // scroll vertically or zoom
2986 public int getBorderWidth() {
2987 return fBorderWidth
;
2991 * Set the border width
2993 * @param borderWidth
2996 public void setBorderWidth(int borderWidth
) {
2997 this.fBorderWidth
= borderWidth
;
3001 * @return The current height of the header row
3003 public int getHeaderHeight() {
3004 return fHeaderHeight
;
3008 * Set the height of the header row
3010 * @param headerHeight
3013 public void setHeaderHeight(int headerHeight
) {
3014 this.fHeaderHeight
= headerHeight
;
3018 * @return The default height of regular item rows
3020 public int getItemHeight() {
3021 return fGlobalItemHeight
;
3025 * Set the default height of regular item rows.
3030 public void setItemHeight(int rowHeight
) {
3031 this.fGlobalItemHeight
= rowHeight
;
3032 for (Item item
: fItemData
.fItems
) {
3033 item
.fItemHeight
= rowHeight
;
3038 * Set the height of a specific item. Overrides the default item height.
3041 * A time graph entry
3044 * @return true if the height is successfully stored, false otherwise
3046 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3047 Item item
= fItemData
.findItem(entry
);
3049 item
.fItemHeight
= rowHeight
;
3056 * Set the minimum item width
3058 * @param width The minimum width
3060 public void setMinimumItemWidth(int width
) {
3061 this.fMinimumItemWidth
= width
;
3065 * @return The minimum item width
3067 public int getMinimumItemWidth() {
3068 return fMinimumItemWidth
;
3072 * Set whether all time events with a duration shorter than one pixel should
3073 * be blended in. If false, only the first such time event will be drawn and
3074 * the subsequent time events in the same pixel will be discarded. The
3075 * default value is false.
3078 * true if sub-pixel events should be blended, false otherwise.
3081 public void setBlendSubPixelEvents(boolean blend
) {
3082 fBlendSubPixelEvents
= blend
;
3086 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3087 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3088 fSelectionChangedListeners
.add(listener
);
3093 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3094 if (listener
!= null) {
3095 fSelectionChangedListeners
.remove(listener
);
3100 public void setSelection(ISelection selection
) {
3101 if (selection
instanceof TimeGraphSelection
) {
3102 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
3103 Object ob
= sel
.getFirstElement();
3104 if (ob
instanceof ITimeGraphEntry
) {
3105 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
3106 selectItem(trace
, false);
3113 * @param filter The filter object to be attached to the view
3115 public void addFilter(@NonNull ViewerFilter filter
) {
3116 if (!fFilters
.contains(filter
)) {
3117 fFilters
.add(filter
);
3122 * @param filter The filter object to be attached to the view
3124 public void removeFilter(@NonNull ViewerFilter filter
) {
3125 fFilters
.remove(filter
);
3129 * Returns this control's filters.
3131 * @return an array of viewer filters
3134 public @NonNull ViewerFilter
[] getFilters() {
3135 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3139 * Sets the filters, replacing any previous filters.
3142 * an array of viewer filters, or null
3145 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3147 if (filters
!= null) {
3148 fFilters
.addAll(Arrays
.asList(filters
));
3153 public void colorSettingsChanged(StateItem
[] stateItems
) {
3154 /* Destroy previous colors from the resource manager */
3155 if (fEventColorMap
!= null) {
3156 for (Color color
: fEventColorMap
) {
3157 fResourceManager
.destroyColor(color
.getRGB());
3160 if (stateItems
!= null) {
3161 fEventColorMap
= new Color
[stateItems
.length
];
3162 for (int i
= 0; i
< stateItems
.length
; i
++) {
3163 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3166 fEventColorMap
= new Color
[] { };
3171 private class ItemData
{
3172 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3173 private Item
[] fExpandedItems
= new Item
[0];
3174 private Item
[] fItems
= new Item
[0];
3175 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3176 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3181 public Item
findItem(ITimeGraphEntry entry
) {
3182 return fItemMap
.get(entry
);
3185 public int findItemIndex(ITimeGraphEntry entry
) {
3186 Item item
= fItemMap
.get(entry
);
3190 return item
.fExpandedIndex
;
3193 public void refreshData() {
3194 ITimeGraphEntry selection
= getSelectedTrace();
3195 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3196 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3197 ITimeGraphEntry entry
= fRootEntries
[i
];
3198 refreshData(itemMap
, null, 0, entry
);
3201 fItems
= fItemMap
.values().toArray(new Item
[0]);
3202 updateExpandedItems();
3203 if (selection
!= null) {
3204 for (Item item
: fExpandedItems
) {
3205 if (item
.fEntry
== selection
) {
3206 item
.fSelected
= true;
3213 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3214 Item item
= new Item(entry
, entry
.getName(), level
);
3215 if (parent
!= null) {
3216 parent
.fChildren
.add(item
);
3218 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3219 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3221 item
.fItemHeight
= fGlobalItemHeight
;
3223 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3224 itemMap
.put(entry
, item
);
3225 if (entry
.hasChildren()) {
3226 Item oldItem
= fItemMap
.get(entry
);
3227 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3228 /* existing items keep their old expanded state */
3229 item
.fExpanded
= oldItem
.fExpanded
;
3231 /* new items set the expanded state according to auto-expand level */
3232 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3234 item
.fHasChildren
= true;
3235 for (ITimeGraphEntry child
: entry
.getChildren()) {
3236 refreshData(itemMap
, item
, level
+ 1, child
);
3241 public void updateExpandedItems() {
3242 for (Item item
: fItems
) {
3243 item
.fExpandedIndex
= -1;
3245 List
<Item
> expandedItemList
= new ArrayList
<>();
3246 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3247 ITimeGraphEntry entry
= fRootEntries
[i
];
3248 Item item
= findItem(entry
);
3249 refreshExpanded(expandedItemList
, item
);
3251 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3252 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3255 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3256 // Check for filters
3257 boolean display
= true;
3258 for (ViewerFilter filter
: fFilters
) {
3259 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3265 item
.fExpandedIndex
= expandedItemList
.size();
3266 expandedItemList
.add(item
);
3267 if (item
.fHasChildren
&& item
.fExpanded
) {
3268 for (Item child
: item
.fChildren
) {
3269 refreshExpanded(expandedItemList
, child
);
3275 public void refreshData(ITimeGraphEntry
[] entries
) {
3276 if (entries
== null) {
3277 fRootEntries
= null;
3279 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3285 public void refreshArrows(List
<ILinkEvent
> events
) {
3286 /* If links are null, reset the list */
3287 if (events
!= null) {
3290 fLinks
= new ArrayList
<>();
3294 public ITimeGraphEntry
[] getEntries() {
3295 return fRootEntries
;
3299 private class Item
{
3300 private boolean fExpanded
;
3301 private int fExpandedIndex
;
3302 private boolean fSelected
;
3303 private boolean fHasChildren
;
3304 private int fItemHeight
;
3305 private final int fLevel
;
3306 private final List
<Item
> fChildren
;
3307 private final String fName
;
3308 private final ITimeGraphEntry fEntry
;
3310 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3311 this.fEntry
= entry
;
3313 this.fLevel
= level
;
3314 this.fChildren
= new ArrayList
<>();
3318 public String
toString() {
3324 public void menuDetected(MenuDetectEvent e
) {
3325 if (null == fTimeProvider
) {
3328 Point p
= toControl(e
.x
, e
.y
);
3329 if (e
.detail
== SWT
.MENU_MOUSE
) {
3330 if (fPendingMenuDetectEvent
== null) {
3331 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3332 * Store the event and trigger it later just before handling mouseUp.
3333 * This allows for the method to detect if mouse is used to drag zoom.
3335 fPendingMenuDetectEvent
= e
;
3337 * Prevent the platform to show the menu when returning. The
3338 * menu will be shown (see below) when this method is called
3339 * again during mouseup().
3344 fPendingMenuDetectEvent
= null;
3345 if ((p
.x
>= fTimeProvider
.getNameSpace()) && (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
)) {
3349 if (fDragState
!= DRAG_NONE
) {
3353 int idx
= getItemIndexAtY(p
.y
);
3354 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3356 Item item
= fItemData
.fExpandedItems
[idx
];
3357 ITimeGraphEntry entry
= item
.fEntry
;
3359 /* Send menu event for the time graph entry */
3362 fireMenuEventOnTimeGraphEntry(e
);
3363 Menu menu
= getMenu();
3364 if (e
.doit
&& (menu
!= null)) {
3365 menu
.setVisible(true);
3368 /* Send menu event for time event */
3369 if (entry
.hasTimeEvents()) {
3370 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3371 if (event
!= null) {
3374 fireMenuEventOnTimeEvent(e
);
3376 if (e
.doit
&& (menu
!= null)) {
3377 menu
.setVisible(true);
3385 * Perform the alignment operation.
3388 * the alignment offset
3390 * @see ITmfTimeAligned
3394 public void performAlign(int offset
) {
3395 fTimeProvider
.setNameSpace(offset
);
3399 * Return the time alignment information
3401 * @return the time alignment information
3403 * @see ITmfTimeAligned
3407 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3408 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());