1 /*****************************************************************************
2 * Copyright (c) 2007, 2016 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.AbstractMap
.SimpleEntry
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Arrays
;
26 import java
.util
.Collections
;
27 import java
.util
.HashMap
;
28 import java
.util
.Iterator
;
29 import java
.util
.LinkedHashMap
;
30 import java
.util
.LinkedList
;
31 import java
.util
.List
;
33 import java
.util
.Map
.Entry
;
34 import java
.util
.Queue
;
36 import org
.eclipse
.jdt
.annotation
.NonNull
;
37 import org
.eclipse
.jface
.action
.IStatusLineManager
;
38 import org
.eclipse
.jface
.resource
.JFaceResources
;
39 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
40 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
41 import org
.eclipse
.jface
.viewers
.ISelection
;
42 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
43 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
44 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
45 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
46 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
47 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
48 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
49 import org
.eclipse
.osgi
.util
.NLS
;
50 import org
.eclipse
.swt
.SWT
;
51 import org
.eclipse
.swt
.events
.ControlEvent
;
52 import org
.eclipse
.swt
.events
.ControlListener
;
53 import org
.eclipse
.swt
.events
.FocusEvent
;
54 import org
.eclipse
.swt
.events
.FocusListener
;
55 import org
.eclipse
.swt
.events
.KeyEvent
;
56 import org
.eclipse
.swt
.events
.KeyListener
;
57 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
58 import org
.eclipse
.swt
.events
.MenuDetectListener
;
59 import org
.eclipse
.swt
.events
.MouseEvent
;
60 import org
.eclipse
.swt
.events
.MouseListener
;
61 import org
.eclipse
.swt
.events
.MouseMoveListener
;
62 import org
.eclipse
.swt
.events
.MouseTrackListener
;
63 import org
.eclipse
.swt
.events
.MouseWheelListener
;
64 import org
.eclipse
.swt
.events
.PaintEvent
;
65 import org
.eclipse
.swt
.events
.SelectionListener
;
66 import org
.eclipse
.swt
.events
.TraverseEvent
;
67 import org
.eclipse
.swt
.events
.TraverseListener
;
68 import org
.eclipse
.swt
.events
.TypedEvent
;
69 import org
.eclipse
.swt
.graphics
.Color
;
70 import org
.eclipse
.swt
.graphics
.Cursor
;
71 import org
.eclipse
.swt
.graphics
.Font
;
72 import org
.eclipse
.swt
.graphics
.FontData
;
73 import org
.eclipse
.swt
.graphics
.GC
;
74 import org
.eclipse
.swt
.graphics
.Image
;
75 import org
.eclipse
.swt
.graphics
.Point
;
76 import org
.eclipse
.swt
.graphics
.Rectangle
;
77 import org
.eclipse
.swt
.widgets
.Composite
;
78 import org
.eclipse
.swt
.widgets
.Display
;
79 import org
.eclipse
.swt
.widgets
.Event
;
80 import org
.eclipse
.swt
.widgets
.Listener
;
81 import org
.eclipse
.swt
.widgets
.Menu
;
82 import org
.eclipse
.swt
.widgets
.Tree
;
83 import org
.eclipse
.swt
.widgets
.TreeColumn
;
84 import org
.eclipse
.tracecompass
.common
.core
.math
.SaturatedArithmetic
;
85 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
95 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
96 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
97 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
98 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
99 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
100 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
101 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
102 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
104 import com
.google
.common
.collect
.Iterables
;
107 * Time graph control implementation
109 * @author Alvaro Sanchez-Leon
110 * @author Patrick Tasse
112 public class TimeGraphControl
extends TimeGraphBaseControl
113 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
114 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
115 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
117 /** Constant indicating that all levels of the time graph should be expanded */
118 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
120 private static final int DRAG_MARGIN
= 5;
122 private static final int DRAG_NONE
= 0;
123 private static final int DRAG_TRACE_ITEM
= 1;
124 private static final int DRAG_SPLIT_LINE
= 2;
125 private static final int DRAG_ZOOM
= 3;
126 private static final int DRAG_SELECTION
= 4;
128 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
130 private static final double ZOOM_FACTOR
= 1.5;
131 private static final double ZOOM_IN_FACTOR
= 0.8;
132 private static final double ZOOM_OUT_FACTOR
= 1.25;
134 private static final int SNAP_WIDTH
= 3;
135 private static final int ARROW_HOVER_MAX_DIST
= 5;
137 private static final double ARROW_RATIO
= Math
.sqrt(3) / 2; // base to height ratio
139 private static final int NO_STATUS
= -1;
140 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
142 private static final int MAX_LABEL_LENGTH
= 256;
144 private static final int PPI
= 72; // points per inch
145 private static final int DPI
= Display
.getDefault().getDPI().y
;
147 private static final int VERTICAL_ZOOM_DELAY
= 400;
149 private static final String PREFERRED_WIDTH
= "width"; //$NON-NLS-1$
151 /** Resource manager */
152 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
154 /** Color map for event types */
155 private Color
[] fEventColorMap
= null;
157 private ITimeDataProvider fTimeProvider
;
158 private ITableLabelProvider fLabelProvider
;
159 private IStatusLineManager fStatusLineManager
= null;
160 private Tree fTree
= null;
161 private TimeGraphScale fTimeGraphScale
= null;
163 private boolean fIsInFocus
= false;
164 private boolean fMouseOverSplitLine
= false;
165 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
166 private int fHeightAdjustment
= 0;
167 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
168 private boolean fBlendSubPixelEvents
= false;
169 private int fMinimumItemWidth
= 0;
170 private int fTopIndex
= 0;
171 private int fDragState
= DRAG_NONE
;
172 private boolean fDragBeginMarker
= false;
173 private int fDragButton
;
174 private int fDragX0
= 0;
175 private int fDragX
= 0;
176 private boolean fHasNamespaceFocus
= false;
177 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
178 private int fIdealNameSpace
= 0;
179 private boolean fAutoResizeColumns
= true;
180 private long fTime0bak
;
181 private long fTime1bak
;
182 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
183 private ItemData fItemData
= null;
184 private List
<IMarkerEvent
> fMarkers
= null;
185 private boolean fMarkersVisible
= true;
186 private List
<SelectionListener
> fSelectionListeners
;
187 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
188 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
189 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
190 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
191 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
192 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
193 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
194 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
195 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
196 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
197 private MenuDetectEvent fPendingMenuDetectEvent
= null;
198 private boolean fGridLinesVisible
= true;
199 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
200 private boolean fHideArrows
= false;
201 private int fAutoExpandLevel
= ALL_LEVELS
;
202 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
203 private long fVerticalZoomAlignTime
= 0;
204 private int fBorderWidth
= 0;
205 private int fHeaderHeight
= 0;
208 * Standard constructor
211 * The parent composite object
213 * The color scheme to use
215 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
217 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
219 fItemData
= new ItemData();
221 addFocusListener(this);
222 addMouseListener(this);
223 addMouseMoveListener(this);
224 addMouseTrackListener(this);
225 addMouseWheelListener(this);
226 addTraverseListener(this);
227 addKeyListener(this);
228 addMenuDetectListener(this);
229 addListener(SWT
.MouseWheel
, this);
230 addDisposeListener((e
) -> {
231 fResourceManager
.dispose();
232 for (Font font
: fFonts
.values()) {
239 * Sets the timegraph provider used by this timegraph viewer.
241 * @param timeGraphProvider the timegraph provider
243 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
244 fTimeGraphProvider
= timeGraphProvider
;
246 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
247 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
248 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
251 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
252 colorSettingsChanged(stateItems
);
256 * Gets the timegraph provider used by this timegraph viewer.
258 * @return the timegraph provider, or <code>null</code> if not set.
260 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
261 return fTimeGraphProvider
;
265 * Gets the time data provider used by this viewer.
267 * @return The time data provider, or <code>null</code> if not set
270 public ITimeDataProvider
getTimeDataProvider() {
271 return fTimeProvider
;
275 * Gets the color map used by this timegraph viewer.
277 * @return a color map, or <code>null</code> if not set.
279 public Color
[] getEventColorMap() {
280 return fEventColorMap
;
284 * Assign the given time provider
286 * @param timeProvider
289 public void setTimeProvider(ITimeDataProvider timeProvider
) {
290 fTimeProvider
= timeProvider
;
295 * Set the label provider for the name space
297 * @param labelProvider
301 public void setLabelProvider(ITableLabelProvider labelProvider
) {
302 fLabelProvider
= labelProvider
;
307 * Get the label provider for the name space
309 * @return The label provider
312 public ITableLabelProvider
getLabelProvider() {
313 return fLabelProvider
;
317 * Assign the status line manager
319 * @param statusLineManager
320 * The status line manager, or null to disable status line messages
322 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
323 if (fStatusLineManager
!= null && statusLineManager
== null) {
324 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
326 fStatusLineManager
= statusLineManager
;
330 * Assign the tree that represents the name space header
336 public void setTree(Tree tree
) {
341 * Returns the tree control associated with this time graph control. The
342 * tree is only used for column handling of the name space and contains no
345 * @return the tree control
348 public Tree
getTree() {
353 * Sets the columns for this time graph control's name space.
359 public void setColumns(String
[] columnNames
) {
360 for (TreeColumn column
: fTree
.getColumns()) {
363 ControlListener controlListener
= new ControlListener() {
365 public void controlResized(ControlEvent e
) {
366 if (fAutoResizeColumns
&& ((TreeColumn
) e
.widget
).getWidth() < (Integer
) e
.widget
.getData(PREFERRED_WIDTH
)) {
367 fAutoResizeColumns
= false;
372 public void controlMoved(ControlEvent e
) {
376 for (String columnName
: columnNames
) {
377 TreeColumn column
= new TreeColumn(fTree
, SWT
.LEFT
);
378 column
.setMoveable(true);
379 column
.setText(columnName
);
381 column
.setData(PREFERRED_WIDTH
, column
.getWidth());
382 column
.addControlListener(controlListener
);
387 * Assign the time graph scale
389 * @param timeGraphScale
390 * The time graph scale
392 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
393 fTimeGraphScale
= timeGraphScale
;
397 * Add a selection listener
400 * The listener to add
402 public void addSelectionListener(SelectionListener listener
) {
403 if (listener
== null) {
404 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
406 if (null == fSelectionListeners
) {
407 fSelectionListeners
= new ArrayList
<>();
409 fSelectionListeners
.add(listener
);
413 * Remove a selection listener
416 * The listener to remove
418 public void removeSelectionListener(SelectionListener listener
) {
419 if (null != fSelectionListeners
) {
420 fSelectionListeners
.remove(listener
);
425 * Selection changed callback
427 public void fireSelectionChanged() {
428 if (null != fSelectionListeners
) {
429 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
430 while (it
.hasNext()) {
431 SelectionListener listener
= it
.next();
432 listener
.widgetSelected(null);
436 if (null != fSelectionChangedListeners
) {
437 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
438 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
444 * Default selection callback
446 public void fireDefaultSelection() {
447 if (null != fSelectionListeners
) {
448 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
449 while (it
.hasNext()) {
450 SelectionListener listener
= it
.next();
451 listener
.widgetDefaultSelected(null);
457 * Add a drag selection listener
460 * The listener to add
462 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
463 if (listener
== null) {
464 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
466 if (null == fDragSelectionListeners
) {
467 fDragSelectionListeners
= new ArrayList
<>();
469 fDragSelectionListeners
.add(listener
);
473 * Remove a drag selection listener
476 * The listener to remove
478 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
479 if (null != fDragSelectionListeners
) {
480 fDragSelectionListeners
.remove(listener
);
485 * Drag Selection changed callback
488 * Time interval start
492 public void fireDragSelectionChanged(long start
, long end
) {
493 // check for backward intervals
494 long beginTime
, endTime
;
502 // call the listeners
503 if (null != fDragSelectionListeners
) {
504 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
505 while (it
.hasNext()) {
506 ITimeGraphTimeListener listener
= it
.next();
507 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
513 * Get the traces in the model
515 * @return The array of traces
517 public ITimeGraphEntry
[] getTraces() {
518 return fItemData
.getEntries();
522 * Refresh the data for the thing
524 public void refreshData() {
525 fItemData
.refreshData();
530 * Refresh data for the given traces
533 * The traces to refresh
535 public void refreshData(ITimeGraphEntry
[] traces
) {
536 fItemData
.refreshData(traces
);
541 * Refresh the links (arrows) of this widget
543 * @param events The link event list
545 public void refreshArrows(List
<ILinkEvent
> events
) {
546 fItemData
.refreshArrows(events
);
550 * Get the links (arrows) of this widget
552 * @return The unmodifiable link event list
556 public List
<ILinkEvent
> getArrows() {
557 return Collections
.unmodifiableList(fItemData
.fLinks
);
560 boolean ensureVisibleItem(int idx
, boolean redraw
) {
561 boolean changed
= false;
564 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
565 if (fItemData
.fExpandedItems
[index
].fSelected
) {
570 if (index
>= fItemData
.fExpandedItems
.length
) {
573 if (index
< fTopIndex
) {
580 int page
= countPerPage();
581 if (index
>= fTopIndex
+ page
) {
582 setTopIndex(index
- page
+ 1);
593 * Assign the given index as the top one
598 public void setTopIndex(int idx
) {
599 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
600 index
= Math
.max(0, index
);
606 * Set the top index so that the requested element is at the specified
610 * the time graph entry to be positioned
612 * the requested y-coordinate
615 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
616 Item item
= fItemData
.fItemMap
.get(entry
);
617 if (item
== null || item
.fExpandedIndex
== -1) {
620 int index
= item
.fExpandedIndex
;
621 Rectangle itemRect
= getItemRect(getClientArea(), index
);
622 int delta
= itemRect
.y
+ itemRect
.height
- y
;
623 int topIndex
= getItemIndexAtY(delta
);
624 if (topIndex
!= -1) {
625 setTopIndex(topIndex
);
630 setTopIndex(getExpandedElementCount());
636 * Sets the auto-expand level to be used for new entries discovered when
637 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
638 * . The value 0 means that there is no auto-expand; 1 means that top-level
639 * entries are expanded, but not their children; 2 means that top-level
640 * entries are expanded, and their children, but not grand-children; and so
643 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
647 * non-negative level, or <code>ALL_LEVELS</code> to expand all
650 public void setAutoExpandLevel(int level
) {
651 fAutoExpandLevel
= level
;
655 * Returns the auto-expand level.
657 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
658 * the tree are expanded automatically
659 * @see #setAutoExpandLevel
661 public int getAutoExpandLevel() {
662 return fAutoExpandLevel
;
666 * Get the expanded state of a given entry.
670 * @return true if the entry is expanded, false if collapsed
673 public boolean getExpandedState(ITimeGraphEntry entry
) {
674 Item item
= fItemData
.fItemMap
.get(entry
);
675 return (item
!= null ? item
.fExpanded
: false);
679 * Set the expanded state of a given entry
684 * True if expanded, false if collapsed
686 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
687 Item item
= fItemData
.findItem(entry
);
688 if (item
!= null && item
.fExpanded
!= expanded
) {
689 item
.fExpanded
= expanded
;
690 fItemData
.updateExpandedItems();
696 * Set the expanded state of a given entry to certain relative level.
697 * It will call fireTreeEvent() for each changed entry. At the end
698 * it will call redraw().
703 * level to expand to or negative for all levels
705 * True if expanded, false if collapsed
707 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
708 setExpandedStateInt(entry
, level
, expanded
);
713 * Set the expanded state of a given entry and its children to the first
714 * level that has one collapsed entry.
719 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
720 int level
= findExpandedLevel(entry
);
722 setExpandedStateInt(entry
, level
, true);
728 * Inner class for finding relative level with at least one
731 private class SearchNode
{
732 SearchNode(ITimeGraphEntry e
, int l
) {
736 ITimeGraphEntry entry
;
741 * Finds the relative level with at least one collapsed entry.
745 * @return the found level or -1 if all levels are already expanded.
747 private int findExpandedLevel(ITimeGraphEntry entry
) {
748 Queue
<SearchNode
> queue
= new LinkedList
<>();
749 SearchNode root
= new SearchNode(entry
, 0);
750 SearchNode node
= root
;
753 while (!queue
.isEmpty()) {
754 node
= queue
.remove();
755 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
758 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
759 if (e
.hasChildren()) {
760 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
769 * Set the expanded state of a given entry to certain relative level.
770 * It will call fireTreeEvent() for each changed entry. No redraw is done.
775 * level to expand to or negative for all levels
777 * True if expanded, false if collapsed
779 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
781 if ((level
> 0) || (level
< 0)) {
783 if (entry
.hasChildren()) {
784 for (ITimeGraphEntry e
: entry
.getChildren()) {
785 setExpandedStateInt(e
, level
, expanded
);
789 Item item
= fItemData
.findItem(entry
);
790 if (item
!= null && item
.fExpanded
!= expanded
) {
791 item
.fExpanded
= expanded
;
792 fItemData
.updateExpandedItems();
793 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
798 * Collapses all nodes of the viewer's tree, starting with the root.
800 public void collapseAll() {
801 for (Item item
: fItemData
.fItems
) {
802 item
.fExpanded
= false;
804 fItemData
.updateExpandedItems();
809 * Expands all nodes of the viewer's tree, starting with the root.
811 public void expandAll() {
812 for (Item item
: fItemData
.fItems
) {
813 item
.fExpanded
= true;
815 fItemData
.updateExpandedItems();
820 * Add a tree listener
823 * The listener to add
825 public void addTreeListener(ITimeGraphTreeListener listener
) {
826 if (!fTreeListeners
.contains(listener
)) {
827 fTreeListeners
.add(listener
);
832 * Remove a tree listener
835 * The listener to remove
837 public void removeTreeListener(ITimeGraphTreeListener listener
) {
838 if (fTreeListeners
.contains(listener
)) {
839 fTreeListeners
.remove(listener
);
844 * Tree event callback
849 * The expanded state (true for expanded, false for collapsed)
851 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
852 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
853 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
855 listener
.treeExpanded(event
);
857 listener
.treeCollapsed(event
);
863 * Add a menu listener on {@link ITimeGraphEntry}s
866 * The listener to add
868 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
869 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
870 fTimeGraphEntryMenuListeners
.add(listener
);
875 * Remove a menu listener on {@link ITimeGraphEntry}s
878 * The listener to remove
880 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
881 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
882 fTimeGraphEntryMenuListeners
.remove(listener
);
887 * Menu event callback on {@link ITimeGraphEntry}s
890 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
892 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
893 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
894 listener
.menuDetected(event
);
899 * Add a menu listener on {@link ITimeEvent}s
902 * The listener to add
904 public void addTimeEventMenuListener(MenuDetectListener listener
) {
905 if (!fTimeEventMenuListeners
.contains(listener
)) {
906 fTimeEventMenuListeners
.add(listener
);
911 * Remove a menu listener on {@link ITimeEvent}s
914 * The listener to remove
916 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
917 if (fTimeEventMenuListeners
.contains(listener
)) {
918 fTimeEventMenuListeners
.remove(listener
);
923 * Menu event callback on {@link ITimeEvent}s
926 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
928 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
929 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
930 listener
.menuDetected(event
);
935 public boolean setFocus() {
936 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
937 fHasNamespaceFocus
= true;
939 return super.setFocus();
943 * Returns the current selection for this time graph. If a time graph entry
944 * is selected, it will be the first element in the selection. If a time
945 * event is selected, it will be the second element in the selection.
947 * @return the current selection
950 public ISelection
getSelection() {
951 ITimeGraphEntry entry
= getSelectedTrace();
952 if (null != entry
&& null != fTimeProvider
) {
953 long selectedTime
= fTimeProvider
.getSelectionBegin();
954 ITimeEvent event
= Utils
.findEvent(entry
, selectedTime
, 0);
956 return new StructuredSelection(entry
);
958 return new StructuredSelection(new Object
[] { entry
, event
});
960 return StructuredSelection
.EMPTY
;
964 * Get the selection object
966 * @return The selection
968 public ISelection
getSelectionTrace() {
969 ITimeGraphEntry entry
= getSelectedTrace();
971 return new StructuredSelection(entry
);
973 return StructuredSelection
.EMPTY
;
977 * Enable/disable one of the traces in the model
980 * 1 to enable it, -1 to disable. The method returns immediately
981 * if another value is used.
983 public void selectTrace(int n
) {
984 if ((n
!= 1) && (n
!= -1)) {
988 boolean changed
= false;
989 int lastSelection
= -1;
990 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
991 Item item
= fItemData
.fExpandedItems
[i
];
992 if (item
.fSelected
) {
994 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
995 item
.fSelected
= false;
996 item
= fItemData
.fExpandedItems
[i
+ 1];
997 item
.fSelected
= true;
999 } else if ((-1 == n
) && (i
> 0)) {
1000 item
.fSelected
= false;
1001 item
= fItemData
.fExpandedItems
[i
- 1];
1002 item
.fSelected
= true;
1009 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
1010 Item item
= fItemData
.fExpandedItems
[0];
1011 item
.fSelected
= true;
1016 ensureVisibleItem(-1, false);
1018 fireSelectionChanged();
1026 * 1 for next event, -1 for previous event
1028 * true to extend selection range, false for single selection
1031 public void selectEvent(int n
, boolean extend
) {
1032 if (null == fTimeProvider
) {
1035 ITimeGraphEntry trace
= getSelectedTrace();
1036 if (trace
== null) {
1039 long selectedTime
= fTimeProvider
.getSelectionEnd();
1040 long endTime
= fTimeProvider
.getMaxTime();
1041 ITimeEvent nextEvent
;
1042 if (n
== -1 && selectedTime
> endTime
) {
1043 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
1045 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
1047 if (null == nextEvent
&& n
== -1) {
1048 nextEvent
= Utils
.getFirstEvent(trace
);
1050 if (null != nextEvent
) {
1051 long nextTime
= nextEvent
.getTime();
1052 // If last event detected e.g. going back or not moving to a next
1054 if (nextTime
<= selectedTime
&& n
== 1) {
1055 // Select to the end of this last event
1056 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
1057 // but not beyond the end of the trace
1058 if (nextTime
> endTime
) {
1061 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
1062 // for previous event go to its end time unless we were already there
1063 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
1066 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
1068 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
1070 fireSelectionChanged();
1071 } else if (n
== 1) {
1073 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
1075 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
1077 fireSelectionChanged();
1079 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1083 * Select the next event
1086 * true to extend selection range, false for single selection
1089 public void selectNextEvent(boolean extend
) {
1090 selectEvent(1, extend
);
1091 // Notify if visible time window has been adjusted
1092 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1096 * Select the previous event
1099 * true to extend selection range, false for single selection
1102 public void selectPrevEvent(boolean extend
) {
1103 selectEvent(-1, extend
);
1104 // Notify if visible time window has been adjusted
1105 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1109 * Select the next trace
1111 public void selectNextTrace() {
1116 * Select the previous trace
1118 public void selectPrevTrace() {
1123 * Scroll left or right by one half window size
1126 * true to scroll left, false to scroll right
1128 public void horizontalScroll(boolean left
) {
1129 long time0
= fTimeProvider
.getTime0();
1130 long time1
= fTimeProvider
.getTime1();
1131 long timeMin
= fTimeProvider
.getMinTime();
1132 long timeMax
= fTimeProvider
.getMaxTime();
1133 long range
= time1
- time0
;
1137 long increment
= Math
.max(1, range
/ 2);
1139 time0
= Math
.max(time0
- increment
, timeMin
);
1140 time1
= time0
+ range
;
1142 time1
= Math
.min(time1
+ increment
, timeMax
);
1143 time0
= time1
- range
;
1145 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1149 * Zoom based on mouse cursor location with mouse scrolling
1151 * @param zoomIn true to zoom in, false to zoom out
1153 public void zoom(boolean zoomIn
) {
1154 int globalX
= getDisplay().getCursorLocation().x
;
1155 Point p
= toControl(globalX
, 0);
1156 int nameSpace
= fTimeProvider
.getNameSpace();
1157 int timeSpace
= fTimeProvider
.getTimeSpace();
1158 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1159 long time0
= fTimeProvider
.getTime0();
1160 long time1
= fTimeProvider
.getTime1();
1161 long interval
= time1
- time0
;
1162 if (interval
== 0) {
1164 } // to allow getting out of single point interval
1167 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1169 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1171 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1172 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1173 long newTime1
= newTime0
+ newInterval
;
1174 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1178 * zoom in using single click
1180 public void zoomIn() {
1181 long prevTime0
= fTimeProvider
.getTime0();
1182 long prevTime1
= fTimeProvider
.getTime1();
1183 long prevRange
= prevTime1
- prevTime0
;
1184 if (prevRange
== 0) {
1187 ITimeDataProvider provider
= fTimeProvider
;
1188 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1189 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1190 selTime
= (prevTime0
+ prevTime1
) / 2;
1192 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1193 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1195 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1197 if (inaccuracy
> 0 && inaccuracy
< 100) {
1198 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1202 long min
= fTimeProvider
.getMinTimeInterval();
1203 if ((time1
- time0
) < min
) {
1204 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1205 time1
= time0
+ min
;
1208 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1212 * zoom out using single click
1214 public void zoomOut() {
1215 long prevTime0
= fTimeProvider
.getTime0();
1216 long prevTime1
= fTimeProvider
.getTime1();
1217 ITimeDataProvider provider
= fTimeProvider
;
1218 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1219 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
1220 selTime
= (prevTime0
+ prevTime1
) / 2;
1222 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
1223 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
1225 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
1226 if (inaccuracy
> 0 && inaccuracy
< 100) {
1227 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1231 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1238 * true to zoom in, false to zoom out
1241 public void verticalZoom(boolean zoomIn
) {
1243 fHeightAdjustment
++;
1245 fHeightAdjustment
--;
1247 fItemData
.refreshData();
1252 * Reset the vertical zoom to default.
1256 public void resetVerticalZoom() {
1257 fHeightAdjustment
= 0;
1258 fItemData
.refreshData();
1263 * Set the grid lines visibility. The default is true.
1266 * true to show the grid lines, false otherwise
1269 public void setGridLinesVisible(boolean visible
) {
1270 fGridLinesVisible
= visible
;
1274 * Get the grid lines visibility.
1276 * @return true if the grid lines are visible, false otherwise
1279 public boolean getGridLinesVisible() {
1280 return fGridLinesVisible
;
1284 * Set the grid line color. The default is SWT.COLOR_GRAY.
1287 * the grid line color
1290 public void setGridLineColor(Color color
) {
1291 fGridLineColor
= color
;
1295 * Get the grid line color.
1297 * @return the grid line color
1300 public Color
getGridLineColor() {
1301 return fGridLineColor
;
1305 * Set the markers list.
1308 * The markers list, or null
1311 public void setMarkers(List
<IMarkerEvent
> markers
) {
1316 * Get the markers list.
1318 * @return The markers list, or null
1321 public List
<IMarkerEvent
> getMarkers() {
1326 * Set the markers visibility. The default is true.
1329 * true to show the markers, false otherwise
1332 public void setMarkersVisible(boolean visible
) {
1333 fMarkersVisible
= visible
;
1337 * Get the markers visibility.
1339 * @return true if the markers are visible, false otherwise
1342 public boolean getMarkersVisible() {
1343 return fMarkersVisible
;
1349 * @param hideArrows true to hide arrows
1351 public void hideArrows(boolean hideArrows
) {
1352 fHideArrows
= hideArrows
;
1356 * Follow the arrow forward
1359 * true to extend selection range, false for single selection
1362 public void followArrowFwd(boolean extend
) {
1363 ITimeGraphEntry trace
= getSelectedTrace();
1364 if (trace
== null) {
1367 long selectedTime
= fTimeProvider
.getSelectionEnd();
1368 for (ILinkEvent link
: fItemData
.fLinks
) {
1369 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1370 selectItem(link
.getDestinationEntry(), false);
1371 if (link
.getDuration() != 0) {
1373 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1375 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1377 // Notify if visible time window has been adjusted
1378 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1380 fireSelectionChanged();
1381 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1385 selectNextEvent(extend
);
1389 * Follow the arrow backward
1392 * true to extend selection range, false for single selection
1395 public void followArrowBwd(boolean extend
) {
1396 ITimeGraphEntry trace
= getSelectedTrace();
1397 if (trace
== null) {
1400 long selectedTime
= fTimeProvider
.getSelectionEnd();
1401 for (ILinkEvent link
: fItemData
.fLinks
) {
1402 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1403 selectItem(link
.getEntry(), false);
1404 if (link
.getDuration() != 0) {
1406 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1408 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1410 // Notify if visible time window has been adjusted
1411 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1413 fireSelectionChanged();
1414 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1418 selectPrevEvent(extend
);
1422 * Return the currently selected trace
1424 * @return The entry matching the trace
1426 public ITimeGraphEntry
getSelectedTrace() {
1427 ITimeGraphEntry trace
= null;
1428 int idx
= getSelectedIndex();
1430 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1436 * Retrieve the index of the currently selected item
1440 public int getSelectedIndex() {
1442 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1443 Item item
= fItemData
.fExpandedItems
[i
];
1444 if (item
.fSelected
) {
1452 boolean toggle(int idx
) {
1453 boolean toggled
= false;
1454 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1455 Item item
= fItemData
.fExpandedItems
[idx
];
1456 if (item
.fHasChildren
) {
1457 item
.fExpanded
= !item
.fExpanded
;
1458 fItemData
.updateExpandedItems();
1461 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1468 * Gets the index of the item at the given location.
1472 * @return the index of the item at the given location, of -1 if none.
1474 protected int getItemIndexAtY(int y
) {
1477 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1478 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1484 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1485 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1494 boolean isOverSplitLine(int x
) {
1495 if (x
< 0 || null == fTimeProvider
) {
1498 int nameWidth
= fTimeProvider
.getNameSpace();
1499 return Math
.abs(x
- nameWidth
) <= SNAP_WIDTH
;
1502 boolean isOverTimeSpace(int x
, int y
) {
1503 Point size
= getSize();
1504 return x
>= fTimeProvider
.getNameSpace() && x
< size
.x
&& y
>= 0 && y
< size
.y
;
1507 * Gets the {@link ITimeGraphEntry} at the given location.
1510 * a point in the widget
1511 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1515 public ITimeGraphEntry
getEntry(Point pt
) {
1516 int idx
= getItemIndexAtY(pt
.y
);
1517 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1521 * Return the arrow event closest to the given point that is no further than
1522 * a maximum distance.
1525 * a point in the widget
1526 * @return The closest arrow event, or null if there is none close enough.
1528 protected ILinkEvent
getArrow(Point pt
) {
1532 ILinkEvent linkEvent
= null;
1533 double minDistance
= Double
.MAX_VALUE
;
1534 for (ILinkEvent event
: fItemData
.fLinks
) {
1535 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1539 int x2
= x1
+ rect
.width
;
1540 int y2
= y1
+ rect
.height
;
1541 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1542 if (minDistance
> d
) {
1548 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1555 public int getXForTime(long time
) {
1556 if (null == fTimeProvider
) {
1559 long time0
= fTimeProvider
.getTime0();
1560 long time1
= fTimeProvider
.getTime1();
1561 int width
= getSize().x
;
1562 int nameSpace
= fTimeProvider
.getNameSpace();
1563 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1564 int x
= SaturatedArithmetic
.add(getBounds().x
+ nameSpace
, (int) ((time
- time0
) * pixelsPerNanoSec
));
1569 public long getTimeAtX(int coord
) {
1570 if (null == fTimeProvider
) {
1574 Point size
= getSize();
1575 long time0
= fTimeProvider
.getTime0();
1576 long time1
= fTimeProvider
.getTime1();
1577 int nameWidth
= fTimeProvider
.getNameSpace();
1578 final int x
= coord
- nameWidth
;
1579 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1580 if (x
>= 0 && size
.x
>= nameWidth
) {
1581 if (time1
- time0
> timeWidth
) {
1582 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1583 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1585 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1586 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1592 void selectItem(int idx
, boolean addSelection
) {
1593 selectItem(idx
, addSelection
, true);
1596 void selectItem(int idx
, boolean addSelection
, boolean reveal
) {
1597 boolean changed
= false;
1599 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1600 Item item
= fItemData
.fExpandedItems
[idx
];
1601 changed
= !item
.fSelected
;
1602 item
.fSelected
= true;
1605 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1606 Item item
= fItemData
.fExpandedItems
[i
];
1607 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1610 item
.fSelected
= i
== idx
;
1614 changed
|= ensureVisibleItem(idx
, true);
1622 * Select an entry and make it visible
1625 * The entry to select
1626 * @param addSelection
1627 * <code>true</code> to add the entry to the current selection,
1628 * or <code>false</code> to set a new selection
1630 public void selectItem(ITimeGraphEntry entry
, boolean addSelection
) {
1631 int idx
= fItemData
.findItemIndex(entry
);
1632 selectItem(idx
, addSelection
);
1639 * The entry to select
1640 * @param addSelection
1641 * <code>true</code> to add the entry to the current selection,
1642 * or <code>false</code> to set a new selection
1644 * <code>true</code> if the selection is to be made visible, and
1645 * <code>false</code> otherwise
1648 public void selectItem(ITimeGraphEntry entry
, boolean addSelection
, boolean reveal
) {
1649 int idx
= fItemData
.findItemIndex(entry
);
1650 selectItem(idx
, addSelection
, reveal
);
1654 * Retrieve the number of entries shown per page.
1658 public int countPerPage() {
1659 int height
= getSize().y
;
1662 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1663 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1664 if (ySum
>= height
) {
1669 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1670 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1671 if (ySum
>= height
) {
1680 * Get the index of the top element
1684 public int getTopIndex() {
1689 * Get the number of expanded (visible) items
1691 * @return The count of expanded (visible) items
1693 public int getExpandedElementCount() {
1694 return fItemData
.fExpandedItems
.length
;
1698 * Get an array of all expanded (visible) elements
1700 * @return The expanded (visible) elements
1702 public ITimeGraphEntry
[] getExpandedElements() {
1703 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1704 for (Item item
: fItemData
.fExpandedItems
) {
1705 elements
.add(item
.fEntry
);
1707 return elements
.toArray(new ITimeGraphEntry
[0]);
1711 * Get the expanded (visible) element at the specified index.
1715 * @return The expanded (visible) element or null if out of range
1718 public ITimeGraphEntry
getExpandedElement(int index
) {
1719 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1722 return fItemData
.fExpandedItems
[index
].fEntry
;
1726 * Get the bounds of the specified entry
1728 * @param entry the time graph entry
1729 * @return the bounds of the entry, or null if the entry is not visible
1732 public Rectangle
getItemBounds(ITimeGraphEntry entry
) {
1733 int idx
= fItemData
.findItemIndex(entry
);
1735 return getItemRect(getBounds(), idx
);
1740 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1741 Rectangle rect
= getItemRect(bounds
, idx
);
1742 rect
.width
= nameWidth
;
1746 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1747 Rectangle rect
= getItemRect(bounds
, idx
);
1748 rect
.x
+= nameWidth
;
1749 rect
.width
-= nameWidth
;
1753 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1755 if (idx
>= fTopIndex
) {
1756 for (int i
= fTopIndex
; i
< idx
; i
++) {
1757 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1760 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1761 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1764 int y
= bounds
.y
+ ySum
;
1765 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1766 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1770 void paint(Rectangle bounds
, PaintEvent e
) {
1773 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1777 fIdealNameSpace
= 0;
1778 int nameSpace
= fTimeProvider
.getNameSpace();
1780 // draw the background layer
1781 drawBackground(bounds
, nameSpace
, gc
);
1783 // draw the grid lines
1784 drawGridLines(bounds
, gc
);
1786 // draw the background markers
1787 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1790 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1792 // draw the foreground markers
1793 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1795 // draw the links (arrows)
1796 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1798 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1800 int alpha
= gc
.getAlpha();
1803 long time0
= fTimeProvider
.getTime0();
1804 long time1
= fTimeProvider
.getTime1();
1805 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1806 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1807 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1808 int x0
= SaturatedArithmetic
.add(bounds
.x
+ nameSpace
, (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
));
1809 int x1
= SaturatedArithmetic
.add(bounds
.x
+ nameSpace
, (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
));
1811 // draw selection lines
1812 if (fDragState
!= DRAG_SELECTION
) {
1813 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1814 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1815 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1818 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1819 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1824 // draw selection background
1825 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1826 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1827 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1828 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1830 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1831 } else if (x0
- x1
> 1) {
1832 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1836 // draw drag selection background
1837 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1838 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1839 if (fDragX0
< fDragX
) {
1840 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1841 } else if (fDragX0
> fDragX
) {
1842 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1847 if (DRAG_SPLIT_LINE
== fDragState
||
1848 (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0)) {
1849 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.DARK_GRAY
));
1851 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.GRAY
));
1853 gc
.fillRectangle(bounds
.x
+ nameSpace
- SNAP_WIDTH
, bounds
.y
, SNAP_WIDTH
, bounds
.height
);
1855 if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1856 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1857 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1858 if (fDragX
!= fDragX0
) {
1859 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1861 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1862 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1863 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1864 if (fDragX
!= fDragX0
) {
1865 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1873 * Draw the background layer. Fills the background of the control's name
1874 * space and states space, updates the background of items if necessary,
1875 * and draws the item's name text and middle line.
1878 * The bounds of the control
1880 * The name space width
1885 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1886 // draw empty name space background
1887 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1888 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1890 // draw empty states space background
1891 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1892 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1894 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1895 Rectangle itemRect
= getItemRect(bounds
, i
);
1896 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1899 Item item
= fItemData
.fExpandedItems
[i
];
1900 // draw the background of selected item and items with no time events
1901 if (! item
.fEntry
.hasTimeEvents()) {
1902 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1903 gc
.fillRectangle(itemRect
);
1904 } else if (item
.fSelected
) {
1905 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1906 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1907 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1908 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1910 // draw the name space
1911 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1912 drawName(item
, nameRect
, gc
);
1913 if (item
.fEntry
.hasTimeEvents()) {
1914 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1915 drawMidLine(rect
, gc
);
1921 * Draw the grid lines
1924 * The bounds of the control
1929 public void drawGridLines(Rectangle bounds
, GC gc
) {
1930 if (!fGridLinesVisible
) {
1933 gc
.setForeground(fGridLineColor
);
1934 gc
.setAlpha(fGridLineColor
.getAlpha());
1935 for (int x
: fTimeGraphScale
.getTickList()) {
1936 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1945 * The rectangle of the area
1946 * @param timeProvider
1949 * The list of markers
1951 * true to draw the foreground markers, false otherwise
1953 * The width reserved for the names
1955 * Reference to the SWT GC object
1958 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1959 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1962 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1963 /* the list can grow concurrently but cannot shrink */
1964 for (int i
= 0; i
< markers
.size(); i
++) {
1965 IMarkerEvent marker
= markers
.get(i
);
1966 if (marker
.isForeground() == foreground
) {
1967 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1970 gc
.setClipping((Rectangle
) null);
1974 * Draw a single marker
1979 * The bounds of the control
1980 * @param timeProvider
1983 * The width reserved for the name
1985 * Reference to the SWT GC object
1988 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1989 Rectangle rect
= Utils
.clone(bounds
);
1990 if (marker
.getEntry() != null) {
1991 int index
= fItemData
.findItemIndex(marker
.getEntry());
1995 rect
= getStatesRect(bounds
, index
, nameSpace
);
1996 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
2000 int x0
= getXForTime(marker
.getTime());
2001 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
2002 if (x0
> bounds
.width
|| x1
< nameSpace
) {
2005 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
2006 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
2008 Color color
= getColorScheme().getColor(marker
.getColor());
2009 gc
.setBackground(color
);
2010 gc
.setAlpha(color
.getAlpha());
2011 gc
.fillRectangle(rect
);
2013 String label
= marker
.getLabel();
2014 if (label
!= null && marker
.getEntry() != null) {
2015 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
2016 gc
.setForeground(color
);
2017 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
2022 * Draw many items at once
2025 * The bounds of the control
2026 * @param timeProvider
2029 * The array items to draw
2031 * The index of the first element to draw
2033 * The name space width
2037 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
2038 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
2039 for (int i
= topIndex
; i
< items
.length
; i
++) {
2040 Item item
= items
[i
];
2041 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
2051 * The bounds of the control
2052 * @param timeProvider
2055 * The expanded item index
2057 * The name space width
2061 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
2062 Rectangle itemRect
= getItemRect(bounds
, i
);
2063 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
2067 ITimeGraphEntry entry
= item
.fEntry
;
2068 long time0
= timeProvider
.getTime0();
2069 long time1
= timeProvider
.getTime1();
2070 long selectedTime
= fTimeProvider
.getSelectionEnd();
2072 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
2073 if (rect
.isEmpty() || (time1
<= time0
)) {
2074 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
2078 boolean selected
= item
.fSelected
;
2079 // K pixels per second
2080 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
2082 if (item
.fEntry
.hasTimeEvents()) {
2083 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2084 fillSpace(rect
, gc
, selected
);
2086 int margins
= getMarginForHeight(rect
.height
);
2087 int height
= rect
.height
- margins
;
2088 int topMargin
= (margins
+ 1) / 2;
2089 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
2091 /* Set the font for this item */
2092 setFontForHeight(height
, gc
);
2094 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
2095 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
2098 while (iterator
.hasNext()) {
2099 ITimeEvent event
= iterator
.next();
2100 int x
= SaturatedArithmetic
.add(rect
.x
, (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
));
2101 int xEnd
= SaturatedArithmetic
.add(rect
.x
, (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
));
2102 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
2103 // event is out of bounds
2106 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
2107 stateRect
.x
= Math
.max(rect
.x
, x
);
2108 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
2109 if (stateRect
.x
== lastX
) {
2110 stateRect
.width
-= 1;
2111 if (stateRect
.width
> 0) {
2112 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
2113 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
2117 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
2118 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
2119 lastX
= stateRect
.x
;
2122 gc
.setClipping((Rectangle
) null);
2124 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
2131 * The bounds of the control
2132 * @param timeProvider
2135 * The list of link events
2137 * The name space width
2141 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
2142 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2146 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2147 /* the list can grow concurrently but cannot shrink */
2148 for (int i
= 0; i
< links
.size(); i
++) {
2149 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2151 gc
.setClipping((Rectangle
) null);
2155 * Draws a link type event
2158 * The link event to draw
2160 * The bounds of the control
2161 * @param timeProvider
2164 * The name space width
2168 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2169 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2172 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2173 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2174 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2176 if ((srcIndex
== -1) || (destIndex
== -1)) {
2180 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2181 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2183 int x0
= getXForTime(event
.getTime());
2184 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2186 // limit the x-coordinates to prevent integer overflow in calculations
2187 // and also GC.drawLine doesn't draw properly with large coordinates
2188 final int limit
= Integer
.MAX_VALUE
/ 1024;
2189 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2190 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2192 int y0
= src
.y
+ src
.height
/ 2;
2193 int y1
= dst
.y
+ dst
.height
/ 2;
2194 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2203 * Time event for which we're drawing the arrow
2205 * The arrow rectangle
2208 * @return true if the arrow was drawn
2210 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2211 Rectangle rect
, GC gc
) {
2216 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2220 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2223 Color stateColor
= null;
2224 if (colorIdx
< fEventColorMap
.length
) {
2225 stateColor
= fEventColorMap
[colorIdx
];
2227 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2230 gc
.setForeground(stateColor
);
2231 gc
.setBackground(stateColor
);
2233 /* Draw the arrow */
2234 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2235 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2238 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2243 * @author Francis Giraldeau
2246 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2248 * The algorithm was taken from this site, not the code itself
2250 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
2253 double cos
= 0.9510;
2254 double sin
= 0.3090;
2255 long lenx
= x1
- x0
;
2256 long leny
= y1
- y0
;
2257 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2259 double dx
= factor
* lenx
/ len
;
2260 double dy
= factor
* leny
/ len
;
2261 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2262 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2263 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2264 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2265 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2266 gc
.fillPolygon(arrow
);
2270 * Draw the name space of an item.
2275 * The bounds of the item's name space
2279 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2280 // No name space to be drawn
2281 if (fTimeProvider
.getNameSpace() == 0) {
2285 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2286 if (hasTimeEvents
) {
2287 gc
.setClipping(bounds
);
2290 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2291 setFontForHeight(height
, gc
);
2293 String name
= fLabelProvider
== null ? item
.fName
: fLabelProvider
.getColumnText(item
.fEntry
, 0);
2294 Rectangle rect
= Utils
.clone(bounds
);
2295 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2296 TreeColumn
[] columns
= fTree
.getColumns();
2297 int idealNameSpace
= 0;
2298 for (int i
= 0; i
< columns
.length
; i
++) {
2299 int columnIndex
= fTree
.getColumnOrder()[i
];
2300 TreeColumn column
= columns
[columnIndex
];
2301 rect
.width
= column
.getWidth();
2302 gc
.setClipping(rect
.x
, bounds
.y
, Math
.min(rect
.width
, bounds
.x
+ bounds
.width
- rect
.x
- SNAP_WIDTH
), bounds
.height
);
2305 // first visible column
2306 width
+= item
.fLevel
* EXPAND_SIZE
;
2307 if (item
.fHasChildren
) {
2308 // draw expand/collapse arrow
2309 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.DARK_GRAY
));
2310 int arrowHeightHint
= (height
< 4) ? height
: (height
< 6) ? height
- 1 : height
- 2;
2311 int arrowHalfHeight
= Math
.max(1, Math
.min(arrowHeightHint
, (int) Math
.round((EXPAND_SIZE
- 2) / ARROW_RATIO
))) / 2;
2312 int arrowHalfWidth
= (Math
.max(1, Math
.min(EXPAND_SIZE
- 2, (int) Math
.round(arrowHeightHint
* ARROW_RATIO
))) + 1) / 2;
2313 int x1
= bounds
.x
+ width
+ 1;
2314 int x2
= x1
+ 2 * arrowHalfWidth
;
2315 int midy
= bounds
.y
+ bounds
.height
/ 2;
2316 int y1
= midy
- arrowHalfHeight
;
2317 int y2
= midy
+ arrowHalfHeight
;
2318 if (!item
.fExpanded
) { // >
2319 gc
.fillPolygon(new int[] { x1
, y1
, x2
, midy
, x1
, y2
});
2321 int midx
= x1
+ arrowHalfWidth
;
2322 gc
.fillPolygon(new int[] { x1
, y1
, x2
, y1
, midx
, y2
});
2325 width
+= EXPAND_SIZE
+ MARGIN
;
2327 Image img
= fLabelProvider
!= null ? fLabelProvider
.getColumnImage(item
.fEntry
, columnIndex
)
2328 : columnIndex
== 0 ? fTimeGraphProvider
.getItemImage(item
.fEntry
) : null;
2331 int imgHeight
= img
.getImageData().height
;
2332 int imgWidth
= img
.getImageData().width
;
2333 int dstHeight
= Math
.min(bounds
.height
, imgHeight
);
2334 int dstWidth
= dstHeight
* imgWidth
/ imgHeight
;
2336 int y
= bounds
.y
+ (bounds
.height
- dstHeight
) / 2;
2337 gc
.drawImage(img
, 0, 0, imgWidth
, imgHeight
, x
, y
, dstWidth
, dstHeight
);
2338 width
+= imgWidth
+ MARGIN
;
2341 if (fLabelProvider
== null) {
2345 String label
= fLabelProvider
!= null ? fLabelProvider
.getColumnText(item
.fEntry
, columnIndex
)
2346 : columnIndex
== 0 ? item
.fName
: ""; //$NON-NLS-1$
2347 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2348 Rectangle textRect
= new Rectangle(rect
.x
+ width
, rect
.y
, rect
.width
- width
, rect
.height
);
2349 int textWidth
= Utils
.drawText(gc
, label
, textRect
, true);
2350 width
+= textWidth
+ MARGIN
;
2351 if (textWidth
> 0) {
2352 idealNameSpace
= rect
.x
+ width
;
2354 if (columns
.length
== 1) {
2355 drawMidLine(new Rectangle(bounds
.x
+ width
, bounds
.y
, bounds
.x
+ bounds
.width
, bounds
.height
), gc
);
2357 if (fAutoResizeColumns
&& width
> column
.getWidth()) {
2358 column
.setData(PREFERRED_WIDTH
, width
);
2359 column
.setWidth(width
);
2361 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2362 if (i
< columns
.length
- 1) {
2363 // not the last visible column: draw the vertical cell border
2364 int x
= rect
.x
+ rect
.width
- 1;
2365 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
2367 rect
.x
+= rect
.width
;
2369 fIdealNameSpace
= Math
.max(fIdealNameSpace
, idealNameSpace
);
2371 gc
.setClipping((Rectangle
) null);
2375 * Draw the state (color fill)
2380 * Time event for which we're drawing the state
2382 * The state rectangle
2386 * Is this time event currently selected (so it appears
2388 * @param timeSelected
2389 * Is the timestamp currently selected
2390 * @return true if the state was drawn
2392 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2393 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2395 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2396 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2399 boolean visible
= rect
.width
== 0 ?
false : true;
2400 rect
.width
= Math
.max(1, rect
.width
);
2401 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2402 gc
.setForeground(black
);
2404 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2406 // Only draw the top and bottom borders
2407 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2408 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2409 if (rect
.width
== 1) {
2410 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2413 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2416 Color stateColor
= null;
2417 if (colorIdx
< fEventColorMap
.length
) {
2418 stateColor
= fEventColorMap
[colorIdx
];
2423 boolean reallySelected
= timeSelected
&& selected
;
2424 // fill all rect area
2425 gc
.setBackground(stateColor
);
2427 gc
.fillRectangle(rect
);
2428 } else if (fBlendSubPixelEvents
) {
2430 gc
.fillRectangle(rect
);
2434 if (reallySelected
) {
2435 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2436 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2439 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2441 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2446 * Fill an item's states rectangle
2449 * The states rectangle
2453 * true if the item is selected
2455 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2456 /* Nothing to draw */
2460 * Draw a line at the middle height of a rectangle
2467 private void drawMidLine(Rectangle rect
, GC gc
) {
2468 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2469 int midy
= rect
.y
+ rect
.height
/ 2;
2470 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2473 private static int getMarginForHeight(int height
) {
2475 * State rectangle is smaller than the item bounds when height is > 4.
2476 * Don't use any margin if the height is below or equal that threshold.
2477 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2478 * pixels for the state height, but with a minimum margin of 1.
2480 final int MARGIN_THRESHOLD
= 4;
2481 final int PREFERRED_HEIGHT
= 13;
2482 final int MIN_MARGIN
= 1;
2483 final int MAX_MARGIN
= 6;
2484 return height
<= MARGIN_THRESHOLD ?
0 :
2485 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2488 private void setFontForHeight(int pixels
, GC gc
) {
2489 /* convert font height from pixels to points */
2490 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2491 Font font
= fFonts
.get(height
);
2493 FontData fontData
= gc
.getFont().getFontData()[0];
2494 fontData
.setHeight(height
);
2495 font
= new Font(gc
.getDevice(), fontData
);
2496 fFonts
.put(height
, font
);
2502 public void keyTraversed(TraverseEvent e
) {
2503 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2509 public void keyPressed(KeyEvent e
) {
2511 if (fItemData
.fExpandedItems
.length
== 0) {
2514 if (SWT
.HOME
== e
.keyCode
) {
2516 } else if (SWT
.END
== e
.keyCode
) {
2517 idx
= fItemData
.fExpandedItems
.length
- 1;
2518 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2519 idx
= getSelectedIndex();
2522 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2525 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2526 idx
= getSelectedIndex();
2529 } else if (idx
> 0) {
2532 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2533 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2534 selectPrevEvent(extend
);
2535 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2536 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2537 selectNextEvent(extend
);
2538 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2539 int page
= countPerPage();
2540 idx
= getSelectedIndex();
2545 if (idx
>= fItemData
.fExpandedItems
.length
) {
2546 idx
= fItemData
.fExpandedItems
.length
- 1;
2548 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2549 int page
= countPerPage();
2550 idx
= getSelectedIndex();
2558 } else if (SWT
.CR
== e
.keyCode
) {
2559 idx
= getSelectedIndex();
2561 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2564 fireDefaultSelection();
2568 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2569 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2571 if (fVerticalZoomAlignEntry
!= null) {
2572 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2574 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2575 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2576 verticalZoom(false);
2577 if (fVerticalZoomAlignEntry
!= null) {
2578 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2580 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2581 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2582 resetVerticalZoom();
2583 if (fVerticalZoomAlignEntry
!= null) {
2584 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2586 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2587 if (fHasNamespaceFocus
) {
2588 ITimeGraphEntry entry
= getSelectedTrace();
2589 setExpandedState(entry
, 0, true);
2593 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2594 if (fHasNamespaceFocus
) {
2595 ITimeGraphEntry entry
= getSelectedTrace();
2596 if ((entry
!= null) && entry
.hasChildren()) {
2597 setExpandedState(entry
, -1, false);
2602 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2603 if (fHasNamespaceFocus
) {
2604 ITimeGraphEntry entry
= getSelectedTrace();
2605 if ((entry
!= null) && entry
.hasChildren()) {
2606 setExpandedStateLevel(entry
);
2611 selectItem(idx
, false);
2612 fireSelectionChanged();
2614 int x
= toControl(e
.display
.getCursorLocation()).x
;
2615 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2619 public void keyReleased(KeyEvent e
) {
2620 int x
= toControl(e
.display
.getCursorLocation()).x
;
2621 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2625 public void focusGained(FocusEvent e
) {
2628 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2632 public void focusLost(FocusEvent e
) {
2634 if (DRAG_NONE
!= fDragState
) {
2636 fDragState
= DRAG_NONE
;
2639 updateStatusLine(NO_STATUS
);
2643 * @return If the current view is focused
2645 public boolean isInFocus() {
2650 * Provide the possibility to control the wait cursor externally e.g. data
2651 * requests in progress
2653 * @param waitInd Should we wait indefinitely?
2655 public void waitCursor(boolean waitInd
) {
2656 // Update cursor as indicated
2658 setCursor(fWaitCursor
);
2664 private void updateCursor(int x
, int stateMask
) {
2665 // if Wait cursor not active, check for the need to change the cursor
2666 if (getCursor() == fWaitCursor
) {
2669 Cursor cursor
= null;
2670 if (fDragState
== DRAG_SPLIT_LINE
) {
2671 } else if (fDragState
== DRAG_SELECTION
) {
2672 cursor
= fResizeCursor
;
2673 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2674 cursor
= fDragCursor
;
2675 } else if (fDragState
== DRAG_ZOOM
) {
2676 cursor
= fZoomCursor
;
2677 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2678 cursor
= fDragCursor
;
2679 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2680 cursor
= fResizeCursor
;
2681 } else if (!isOverSplitLine(x
)) {
2682 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2683 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2684 int xBegin
= getXForTime(selectionBegin
);
2685 int xEnd
= getXForTime(selectionEnd
);
2686 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2687 cursor
= fResizeCursor
;
2690 if (getCursor() != cursor
) {
2696 * Update the status line following a change of selection.
2700 public void updateStatusLine() {
2701 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2704 private void updateStatusLine(int x
) {
2705 // use the time provider of the time graph scale for the status line
2706 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2707 if (fStatusLineManager
== null || null == tdp
||
2708 tdp
.getTime0() == tdp
.getTime1()) {
2711 TimeFormat tf
= tdp
.getTimeFormat();
2712 Resolution res
= Resolution
.NANOSEC
;
2713 StringBuilder message
= new StringBuilder();
2714 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2715 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2716 long time
= getTimeAtX(x
);
2718 if (tdp
instanceof ITimeDataProviderConverter
) {
2719 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2721 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2723 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2724 Utils
.formatTime(time
, tf
, res
)
2728 long selectionBegin
= tdp
.getSelectionBegin();
2729 long selectionEnd
= tdp
.getSelectionEnd();
2730 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2732 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2733 Utils
.formatTime(selectionBegin
, tf
, res
)
2735 if (selectionBegin
!= selectionEnd
) {
2736 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2738 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2739 Utils
.formatTime(selectionEnd
, tf
, res
),
2740 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2743 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2744 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2745 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2746 if (tdp
instanceof ITimeDataProviderConverter
) {
2747 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2748 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2750 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2752 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2753 Utils
.formatTime(time0
, tf
, res
),
2754 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2755 Utils
.formatTime(time
, tf
, res
),
2756 Utils
.formatDelta(time
- time0
, tf
, res
)
2759 fStatusLineManager
.setMessage(message
.toString());
2763 public void mouseMove(MouseEvent e
) {
2764 if (null == fTimeProvider
) {
2767 Point size
= getSize();
2768 if (DRAG_TRACE_ITEM
== fDragState
) {
2769 int nameWidth
= fTimeProvider
.getNameSpace();
2770 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2772 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2773 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2774 long time1
= fTime1bak
- timeDelta
;
2775 long maxTime
= fTimeProvider
.getMaxTime();
2776 if (time1
> maxTime
) {
2779 long time0
= time1
- (fTime1bak
- fTime0bak
);
2780 if (time0
< fTimeProvider
.getMinTime()) {
2781 time0
= fTimeProvider
.getMinTime();
2782 time1
= time0
+ (fTime1bak
- fTime0bak
);
2784 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2786 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2788 fTimeProvider
.setNameSpace(e
.x
);
2789 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2790 } else if (DRAG_SELECTION
== fDragState
) {
2791 if (fDragBeginMarker
) {
2792 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2794 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2797 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2798 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2799 } else if (DRAG_ZOOM
== fDragState
) {
2800 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2802 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2803 } else if (DRAG_NONE
== fDragState
) {
2804 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2805 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2808 fMouseOverSplitLine
= mouseOverSplitLine
;
2811 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2812 fHasNamespaceFocus
= false;
2814 fHasNamespaceFocus
= true;
2816 updateCursor(e
.x
, e
.stateMask
);
2817 updateStatusLine(e
.x
);
2821 public void mouseDoubleClick(MouseEvent e
) {
2822 if (null == fTimeProvider
) {
2825 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2826 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2827 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2828 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2829 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2832 fMouseOverSplitLine
= mouseOverSplitLine
;
2833 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2836 int idx
= getItemIndexAtY(e
.y
);
2838 selectItem(idx
, false);
2839 fireDefaultSelection();
2845 public void mouseDown(MouseEvent e
) {
2846 if (fDragState
!= DRAG_NONE
) {
2849 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2850 int nameSpace
= fTimeProvider
.getNameSpace();
2851 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2852 fDragState
= DRAG_SPLIT_LINE
;
2853 fDragButton
= e
.button
;
2857 updateCursor(e
.x
, e
.stateMask
);
2861 if (fTimeProvider
== null ||
2862 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2863 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2867 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2868 int nameSpace
= fTimeProvider
.getNameSpace();
2869 idx
= getItemIndexAtY(e
.y
);
2871 Item item
= fItemData
.fExpandedItems
[idx
];
2872 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2876 selectItem(idx
, false);
2877 fireSelectionChanged();
2879 selectItem(idx
, false); // clear selection
2880 fireSelectionChanged();
2882 long hitTime
= getTimeAtX(e
.x
);
2886 fDragState
= DRAG_SELECTION
;
2887 fDragBeginMarker
= false;
2888 fDragButton
= e
.button
;
2891 fDragTime0
= getTimeAtX(fDragX0
);
2892 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2893 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2894 int xBegin
= getXForTime(selectionBegin
);
2895 int xEnd
= getXForTime(selectionEnd
);
2896 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2897 long time
= getTimeAtX(e
.x
);
2898 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2899 fDragBeginMarker
= true;
2902 fDragTime0
= selectionEnd
;
2905 fDragTime0
= selectionBegin
;
2908 long time
= getTimeAtX(e
.x
);
2909 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2910 fDragBeginMarker
= true;
2913 fDragTime0
= selectionEnd
;
2914 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2916 fDragTime0
= selectionBegin
;
2919 fTime0bak
= fTimeProvider
.getTime0();
2920 fTime1bak
= fTimeProvider
.getTime1();
2922 updateCursor(e
.x
, e
.stateMask
);
2923 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2925 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2926 long hitTime
= getTimeAtX(e
.x
);
2929 fDragState
= DRAG_TRACE_ITEM
;
2930 fDragButton
= e
.button
;
2933 fTime0bak
= fTimeProvider
.getTime0();
2934 fTime1bak
= fTimeProvider
.getTime1();
2935 updateCursor(e
.x
, e
.stateMask
);
2937 } else if (3 == e
.button
) {
2938 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2940 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2942 fDragTime0
= getTimeAtX(fDragX0
);
2943 fDragState
= DRAG_ZOOM
;
2944 fDragButton
= e
.button
;
2946 updateCursor(e
.x
, e
.stateMask
);
2947 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2949 idx
= getItemIndexAtY(e
.y
);
2950 selectItem(idx
, false);
2951 fireSelectionChanged();
2957 public void mouseUp(MouseEvent e
) {
2958 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2959 if ((fDragState
== DRAG_ZOOM
) && isInDragZoomMargin()) {
2960 // Select entry and time event for single click
2961 long time
= getTimeAtX(fDragX0
);
2962 fTimeProvider
.setSelectionRangeNotify(time
, time
, false);
2963 int idx
= getItemIndexAtY(e
.y
);
2964 selectItem(idx
, false);
2965 fireSelectionChanged();
2967 menuDetected(fPendingMenuDetectEvent
);
2969 if (DRAG_NONE
!= fDragState
) {
2971 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2972 fDragState
= DRAG_NONE
;
2973 if (fDragX
!= fDragX0
) {
2974 fTimeProvider
.notifyStartFinishTime();
2976 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2977 fDragState
= DRAG_NONE
;
2979 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2980 fDragState
= DRAG_NONE
;
2981 if (fDragX
== fDragX0
) { // click without selecting anything
2982 long time
= getTimeAtX(e
.x
);
2983 fTimeProvider
.setSelectedTimeNotify(time
, false);
2985 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2986 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2987 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2990 fTimeGraphScale
.setDragRange(-1, -1);
2991 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2992 fDragState
= DRAG_NONE
;
2993 int nameWidth
= fTimeProvider
.getNameSpace();
2994 if ((Math
.max(fDragX
, fDragX0
) > nameWidth
) && !isInDragZoomMargin()) {
2995 long time0
= getTimeAtX(fDragX0
);
2996 long time1
= getTimeAtX(fDragX
);
2997 if (time0
< time1
) {
2998 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
3000 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
3005 fTimeGraphScale
.setDragRange(-1, -1);
3008 updateCursor(e
.x
, e
.stateMask
);
3009 updateStatusLine(e
.x
);
3013 public void mouseEnter(MouseEvent e
) {
3017 public void mouseExit(MouseEvent e
) {
3018 if (fMouseOverSplitLine
) {
3019 fMouseOverSplitLine
= false;
3022 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
3026 public void mouseHover(MouseEvent e
) {
3030 public void mouseScrolled(MouseEvent e
) {
3031 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
3036 * On some platforms the mouse scroll event is sent to the
3037 * control that has focus even if it is not under the cursor.
3038 * Handle the event only if over the time graph control.
3040 Point size
= getSize();
3041 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
3042 if (!bounds
.contains(e
.x
, e
.y
)) {
3046 boolean horizontalZoom
= false;
3047 boolean horizontalScroll
= false;
3048 boolean verticalZoom
= false;
3049 boolean verticalScroll
= false;
3051 // over the time graph control
3052 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
3053 verticalZoom
= true;
3054 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
3055 // over the name space
3056 verticalScroll
= true;
3058 // over the state area
3059 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
3060 // over the state area, CTRL pressed
3061 horizontalZoom
= true;
3062 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
3063 // over the state area, SHIFT pressed
3064 horizontalScroll
= true;
3066 // over the state area, no modifier pressed
3067 verticalScroll
= true;
3071 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
3072 verticalZoom(e
.count
> 0);
3073 if (fVerticalZoomAlignEntry
!= null) {
3074 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
3076 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
3078 } else if (horizontalScroll
) {
3079 horizontalScroll(e
.count
> 0);
3080 } else if (verticalScroll
){
3081 setTopIndex(getTopIndex() - e
.count
);
3086 * Get the vertical zoom alignment entry and position based on the current
3087 * selection. If there is no selection or if the selection is not visible,
3088 * return an alignment entry with a null time graph entry.
3090 * @return a map entry where the key is the selection's time graph entry and
3091 * the value is the center y-coordinate of that entry, or null
3093 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
3094 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
3095 if (alignEntry
!= null) {
3098 int index
= getSelectedIndex();
3099 if (index
== -1 || index
>= getExpandedElementCount()) {
3100 return new SimpleEntry
<>(null, 0);
3102 Rectangle bounds
= getClientArea();
3103 Rectangle itemRect
= getItemRect(bounds
, index
);
3104 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
3105 /* selection is not visible */
3106 return new SimpleEntry
<>(null, 0);
3108 ITimeGraphEntry entry
= getExpandedElement(index
);
3109 int y
= itemRect
.y
+ itemRect
.height
/ 2;
3110 return new SimpleEntry
<>(entry
, y
);
3114 * Get the vertical zoom alignment entry and position at the specified
3118 * the cursor y-coordinate
3119 * @return a map entry where the key is the time graph entry under the
3120 * cursor and the value is the cursor y-coordinate
3122 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
3123 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
3124 if (alignEntry
!= null) {
3127 int index
= getItemIndexAtY(y
);
3129 index
= getExpandedElementCount() - 1;
3131 ITimeGraphEntry entry
= getExpandedElement(index
);
3132 return new SimpleEntry
<>(entry
, y
);
3136 * Get the vertical zoom alignment entry and position if there is an ongoing
3137 * one and we are within the vertical zoom delay, or otherwise return null.
3139 * @return a map entry where the key is a time graph entry and the value is
3140 * a y-coordinate, or null
3142 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
3143 long currentTimeMillis
= System
.currentTimeMillis();
3144 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
3146 * If the vertical zoom is triggered repeatedly in a short amount of
3147 * time, use the initial event's entry and position.
3149 fVerticalZoomAlignTime
= currentTimeMillis
;
3150 return fVerticalZoomAlignEntry
;
3152 fVerticalZoomAlignTime
= currentTimeMillis
;
3157 public void handleEvent(Event event
) {
3158 if (event
.type
== SWT
.MouseWheel
) {
3159 // prevent horizontal scrolling when the mouse wheel is used to
3160 // scroll vertically or zoom
3166 public int getBorderWidth() {
3167 return fBorderWidth
;
3171 * Set the border width
3173 * @param borderWidth
3176 public void setBorderWidth(int borderWidth
) {
3177 this.fBorderWidth
= borderWidth
;
3181 * @return The current height of the header row
3183 public int getHeaderHeight() {
3184 return fHeaderHeight
;
3188 * Set the height of the header row
3190 * @param headerHeight
3193 public void setHeaderHeight(int headerHeight
) {
3194 this.fHeaderHeight
= headerHeight
;
3198 * @return The default height of regular item rows
3200 public int getItemHeight() {
3201 return fGlobalItemHeight
;
3205 * Set the default height of regular item rows.
3210 public void setItemHeight(int rowHeight
) {
3211 this.fGlobalItemHeight
= rowHeight
;
3212 for (Item item
: fItemData
.fItems
) {
3213 item
.fItemHeight
= rowHeight
;
3218 * Set the height of a specific item. Overrides the default item height.
3221 * A time graph entry
3224 * @return true if the height is successfully stored, false otherwise
3226 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3227 Item item
= fItemData
.findItem(entry
);
3229 item
.fItemHeight
= rowHeight
;
3236 * Set the minimum item width
3238 * @param width The minimum width
3240 public void setMinimumItemWidth(int width
) {
3241 this.fMinimumItemWidth
= width
;
3245 * @return The minimum item width
3247 public int getMinimumItemWidth() {
3248 return fMinimumItemWidth
;
3252 * Set whether all time events with a duration shorter than one pixel should
3253 * be blended in. If false, only the first such time event will be drawn and
3254 * the subsequent time events in the same pixel will be discarded. The
3255 * default value is false.
3258 * true if sub-pixel events should be blended, false otherwise.
3261 public void setBlendSubPixelEvents(boolean blend
) {
3262 fBlendSubPixelEvents
= blend
;
3266 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3267 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3268 fSelectionChangedListeners
.add(listener
);
3273 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3274 if (listener
!= null) {
3275 fSelectionChangedListeners
.remove(listener
);
3280 public void setSelection(ISelection selection
) {
3281 if (selection
instanceof IStructuredSelection
) {
3282 Object ob
= ((IStructuredSelection
) selection
).getFirstElement();
3283 if (ob
instanceof ITimeGraphEntry
) {
3284 selectItem((ITimeGraphEntry
) ob
, false);
3291 * @param filter The filter object to be attached to the view
3293 public void addFilter(@NonNull ViewerFilter filter
) {
3294 if (!fFilters
.contains(filter
)) {
3295 fFilters
.add(filter
);
3300 * @param filter The filter object to be attached to the view
3302 public void removeFilter(@NonNull ViewerFilter filter
) {
3303 fFilters
.remove(filter
);
3307 * Returns this control's filters.
3309 * @return an array of viewer filters
3312 public @NonNull ViewerFilter
[] getFilters() {
3313 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3317 * Sets the filters, replacing any previous filters.
3320 * an array of viewer filters, or null
3323 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3325 if (filters
!= null) {
3326 fFilters
.addAll(Arrays
.asList(filters
));
3331 public void colorSettingsChanged(StateItem
[] stateItems
) {
3332 /* Destroy previous colors from the resource manager */
3333 if (fEventColorMap
!= null) {
3334 for (Color color
: fEventColorMap
) {
3335 fResourceManager
.destroyColor(color
.getRGB());
3338 if (stateItems
!= null) {
3339 fEventColorMap
= new Color
[stateItems
.length
];
3340 for (int i
= 0; i
< stateItems
.length
; i
++) {
3341 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3344 fEventColorMap
= new Color
[] { };
3349 private class ItemData
{
3350 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3351 private Item
[] fExpandedItems
= new Item
[0];
3352 private Item
[] fItems
= new Item
[0];
3353 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3354 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3359 public Item
findItem(ITimeGraphEntry entry
) {
3360 return fItemMap
.get(entry
);
3363 public int findItemIndex(ITimeGraphEntry entry
) {
3364 Item item
= fItemMap
.get(entry
);
3368 return item
.fExpandedIndex
;
3371 public void refreshData() {
3372 ITimeGraphEntry selection
= getSelectedTrace();
3373 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3374 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3375 ITimeGraphEntry entry
= fRootEntries
[i
];
3376 refreshData(itemMap
, null, 0, entry
);
3379 fItems
= fItemMap
.values().toArray(new Item
[0]);
3380 updateExpandedItems();
3381 if (selection
!= null) {
3382 for (Item item
: fExpandedItems
) {
3383 if (item
.fEntry
== selection
) {
3384 item
.fSelected
= true;
3391 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3392 Item item
= new Item(entry
, entry
.getName(), level
);
3393 if (parent
!= null) {
3394 parent
.fChildren
.add(item
);
3396 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3397 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3399 item
.fItemHeight
= fGlobalItemHeight
;
3401 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3402 itemMap
.put(entry
, item
);
3403 if (entry
.hasChildren()) {
3404 Item oldItem
= fItemMap
.get(entry
);
3405 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3406 /* existing items keep their old expanded state */
3407 item
.fExpanded
= oldItem
.fExpanded
;
3409 /* new items set the expanded state according to auto-expand level */
3410 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3412 item
.fHasChildren
= true;
3413 for (ITimeGraphEntry child
: entry
.getChildren()) {
3414 refreshData(itemMap
, item
, level
+ 1, child
);
3419 public void updateExpandedItems() {
3420 for (Item item
: fItems
) {
3421 item
.fExpandedIndex
= -1;
3423 List
<Item
> expandedItemList
= new ArrayList
<>();
3424 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3425 ITimeGraphEntry entry
= fRootEntries
[i
];
3426 Item item
= findItem(entry
);
3427 refreshExpanded(expandedItemList
, item
);
3429 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3430 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3433 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3434 // Check for filters
3435 boolean display
= true;
3436 for (ViewerFilter filter
: fFilters
) {
3437 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3443 item
.fExpandedIndex
= expandedItemList
.size();
3444 expandedItemList
.add(item
);
3445 if (item
.fHasChildren
&& item
.fExpanded
) {
3446 for (Item child
: item
.fChildren
) {
3447 refreshExpanded(expandedItemList
, child
);
3453 public void refreshData(ITimeGraphEntry
[] entries
) {
3454 if (entries
== null) {
3455 fRootEntries
= null;
3457 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3463 public void refreshArrows(List
<ILinkEvent
> events
) {
3464 /* If links are null, reset the list */
3465 if (events
!= null) {
3468 fLinks
= new ArrayList
<>();
3472 public ITimeGraphEntry
[] getEntries() {
3473 return fRootEntries
;
3477 private class Item
{
3478 private boolean fExpanded
;
3479 private int fExpandedIndex
;
3480 private boolean fSelected
;
3481 private boolean fHasChildren
;
3482 private int fItemHeight
;
3483 private final int fLevel
;
3484 private final List
<Item
> fChildren
;
3485 private final String fName
;
3486 private final ITimeGraphEntry fEntry
;
3488 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3489 this.fEntry
= entry
;
3491 this.fLevel
= level
;
3492 this.fChildren
= new ArrayList
<>();
3496 public String
toString() {
3502 public void menuDetected(MenuDetectEvent e
) {
3503 if (null == fTimeProvider
) {
3507 * This flag indicates if menu was prevented from being shown below and
3508 * therefore must be made visible on callback from mouseUp().
3510 boolean pendingEventCallback
= fPendingMenuDetectEvent
!= null;
3511 Point p
= toControl(e
.x
, e
.y
);
3512 if (e
.detail
== SWT
.MENU_MOUSE
&& isOverTimeSpace(p
.x
, p
.y
)) {
3513 if (fPendingMenuDetectEvent
== null) {
3514 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3515 * Store the event and trigger it later just before handling mouseUp.
3516 * This allows for the method to detect if mouse is used to drag zoom.
3518 fPendingMenuDetectEvent
= e
;
3520 * Prevent the platform to show the menu when returning. The
3521 * menu will be shown (see below) when this method is called
3522 * again during mouseUp().
3527 fPendingMenuDetectEvent
= null;
3528 if (fDragState
!= DRAG_ZOOM
|| !isInDragZoomMargin()) {
3530 * Don't show the menu on mouseUp() if a drag zoom is in
3531 * progress with a drag range outside of the drag zoom margin,
3532 * or if any other drag operation, or none, is in progress.
3538 if (fDragState
!= DRAG_NONE
) {
3540 * Don't show the menu on keyboard menu or mouse menu outside of
3541 * the time space if any drag operation is in progress.
3547 int idx
= getItemIndexAtY(p
.y
);
3548 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3549 Item item
= fItemData
.fExpandedItems
[idx
];
3550 ITimeGraphEntry entry
= item
.fEntry
;
3552 /* Send menu event for the time graph entry */
3555 fireMenuEventOnTimeGraphEntry(e
);
3556 Menu menu
= getMenu();
3557 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3558 menu
.setVisible(true);
3561 /* Send menu event for time event */
3562 if (entry
.hasTimeEvents()) {
3563 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3564 if (event
!= null) {
3567 fireMenuEventOnTimeEvent(e
);
3569 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3570 menu
.setVisible(true);
3578 * Perform the alignment operation.
3581 * the alignment offset
3583 * @see ITmfTimeAligned
3587 public void performAlign(int offset
) {
3588 fTimeProvider
.setNameSpace(offset
);
3592 * Return the time alignment information
3594 * @return the time alignment information
3596 * @see ITmfTimeAligned
3600 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3601 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());
3604 private boolean isInDragZoomMargin() {
3605 return (Math
.abs(fDragX
- fDragX0
) < DRAG_MARGIN
);