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 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
;
2334 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2335 gc
.drawImage(img
, x
, y
);
2336 width
+= imgWidth
+ MARGIN
;
2339 if (fLabelProvider
== null) {
2343 String label
= fLabelProvider
!= null ? fLabelProvider
.getColumnText(item
.fEntry
, columnIndex
)
2344 : columnIndex
== 0 ? item
.fName
: ""; //$NON-NLS-1$
2345 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2346 Rectangle textRect
= new Rectangle(rect
.x
+ width
, rect
.y
, rect
.width
- width
, rect
.height
);
2347 int textWidth
= Utils
.drawText(gc
, label
, textRect
, true);
2348 width
+= textWidth
+ MARGIN
;
2349 if (textWidth
> 0) {
2350 idealNameSpace
= rect
.x
+ width
;
2352 if (columns
.length
== 1) {
2353 drawMidLine(new Rectangle(bounds
.x
+ width
, bounds
.y
, bounds
.x
+ bounds
.width
, bounds
.height
), gc
);
2355 if (fAutoResizeColumns
&& width
> column
.getWidth()) {
2356 column
.setData(PREFERRED_WIDTH
, width
);
2357 column
.setWidth(width
);
2359 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2360 if (i
< columns
.length
- 1) {
2361 // not the last visible column: draw the vertical cell border
2362 int x
= rect
.x
+ rect
.width
- 1;
2363 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
2365 rect
.x
+= rect
.width
;
2367 fIdealNameSpace
= Math
.max(fIdealNameSpace
, idealNameSpace
);
2369 gc
.setClipping((Rectangle
) null);
2373 * Draw the state (color fill)
2378 * Time event for which we're drawing the state
2380 * The state rectangle
2384 * Is this time event currently selected (so it appears
2386 * @param timeSelected
2387 * Is the timestamp currently selected
2388 * @return true if the state was drawn
2390 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2391 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2393 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2394 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2397 boolean visible
= rect
.width
== 0 ?
false : true;
2398 rect
.width
= Math
.max(1, rect
.width
);
2399 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2400 gc
.setForeground(black
);
2402 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2404 // Only draw the top and bottom borders
2405 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2406 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2407 if (rect
.width
== 1) {
2408 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2411 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2414 Color stateColor
= null;
2415 if (colorIdx
< fEventColorMap
.length
) {
2416 stateColor
= fEventColorMap
[colorIdx
];
2421 boolean reallySelected
= timeSelected
&& selected
;
2422 // fill all rect area
2423 gc
.setBackground(stateColor
);
2425 gc
.fillRectangle(rect
);
2426 } else if (fBlendSubPixelEvents
) {
2428 gc
.fillRectangle(rect
);
2432 if (reallySelected
) {
2433 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2434 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2437 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2439 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2444 * Fill an item's states rectangle
2447 * The states rectangle
2451 * true if the item is selected
2453 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2454 /* Nothing to draw */
2458 * Draw a line at the middle height of a rectangle
2465 private void drawMidLine(Rectangle rect
, GC gc
) {
2466 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2467 int midy
= rect
.y
+ rect
.height
/ 2;
2468 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2471 private static int getMarginForHeight(int height
) {
2473 * State rectangle is smaller than the item bounds when height is > 4.
2474 * Don't use any margin if the height is below or equal that threshold.
2475 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2476 * pixels for the state height, but with a minimum margin of 1.
2478 final int MARGIN_THRESHOLD
= 4;
2479 final int PREFERRED_HEIGHT
= 13;
2480 final int MIN_MARGIN
= 1;
2481 final int MAX_MARGIN
= 6;
2482 return height
<= MARGIN_THRESHOLD ?
0 :
2483 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2486 private void setFontForHeight(int pixels
, GC gc
) {
2487 /* convert font height from pixels to points */
2488 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2489 Font font
= fFonts
.get(height
);
2491 FontData fontData
= gc
.getFont().getFontData()[0];
2492 fontData
.setHeight(height
);
2493 font
= new Font(gc
.getDevice(), fontData
);
2494 fFonts
.put(height
, font
);
2500 public void keyTraversed(TraverseEvent e
) {
2501 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2507 public void keyPressed(KeyEvent e
) {
2509 if (fItemData
.fExpandedItems
.length
== 0) {
2512 if (SWT
.HOME
== e
.keyCode
) {
2514 } else if (SWT
.END
== e
.keyCode
) {
2515 idx
= fItemData
.fExpandedItems
.length
- 1;
2516 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2517 idx
= getSelectedIndex();
2520 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2523 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2524 idx
= getSelectedIndex();
2527 } else if (idx
> 0) {
2530 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2531 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2532 selectPrevEvent(extend
);
2533 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2534 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2535 selectNextEvent(extend
);
2536 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2537 int page
= countPerPage();
2538 idx
= getSelectedIndex();
2543 if (idx
>= fItemData
.fExpandedItems
.length
) {
2544 idx
= fItemData
.fExpandedItems
.length
- 1;
2546 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2547 int page
= countPerPage();
2548 idx
= getSelectedIndex();
2556 } else if (SWT
.CR
== e
.keyCode
) {
2557 idx
= getSelectedIndex();
2559 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2562 fireDefaultSelection();
2566 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2567 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2569 if (fVerticalZoomAlignEntry
!= null) {
2570 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2572 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2573 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2574 verticalZoom(false);
2575 if (fVerticalZoomAlignEntry
!= null) {
2576 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2578 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2579 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2580 resetVerticalZoom();
2581 if (fVerticalZoomAlignEntry
!= null) {
2582 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2584 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2585 if (fHasNamespaceFocus
) {
2586 ITimeGraphEntry entry
= getSelectedTrace();
2587 setExpandedState(entry
, 0, true);
2591 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2592 if (fHasNamespaceFocus
) {
2593 ITimeGraphEntry entry
= getSelectedTrace();
2594 if ((entry
!= null) && entry
.hasChildren()) {
2595 setExpandedState(entry
, -1, false);
2600 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2601 if (fHasNamespaceFocus
) {
2602 ITimeGraphEntry entry
= getSelectedTrace();
2603 if ((entry
!= null) && entry
.hasChildren()) {
2604 setExpandedStateLevel(entry
);
2609 selectItem(idx
, false);
2610 fireSelectionChanged();
2612 int x
= toControl(e
.display
.getCursorLocation()).x
;
2613 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2617 public void keyReleased(KeyEvent e
) {
2618 int x
= toControl(e
.display
.getCursorLocation()).x
;
2619 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2623 public void focusGained(FocusEvent e
) {
2626 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2630 public void focusLost(FocusEvent e
) {
2632 if (DRAG_NONE
!= fDragState
) {
2634 fDragState
= DRAG_NONE
;
2637 updateStatusLine(NO_STATUS
);
2641 * @return If the current view is focused
2643 public boolean isInFocus() {
2648 * Provide the possibility to control the wait cursor externally e.g. data
2649 * requests in progress
2651 * @param waitInd Should we wait indefinitely?
2653 public void waitCursor(boolean waitInd
) {
2654 // Update cursor as indicated
2656 setCursor(fWaitCursor
);
2662 private void updateCursor(int x
, int stateMask
) {
2663 // if Wait cursor not active, check for the need to change the cursor
2664 if (getCursor() == fWaitCursor
) {
2667 Cursor cursor
= null;
2668 if (fDragState
== DRAG_SPLIT_LINE
) {
2669 } else if (fDragState
== DRAG_SELECTION
) {
2670 cursor
= fResizeCursor
;
2671 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2672 cursor
= fDragCursor
;
2673 } else if (fDragState
== DRAG_ZOOM
) {
2674 cursor
= fZoomCursor
;
2675 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2676 cursor
= fDragCursor
;
2677 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2678 cursor
= fResizeCursor
;
2679 } else if (!isOverSplitLine(x
)) {
2680 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2681 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2682 int xBegin
= getXForTime(selectionBegin
);
2683 int xEnd
= getXForTime(selectionEnd
);
2684 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2685 cursor
= fResizeCursor
;
2688 if (getCursor() != cursor
) {
2694 * Update the status line following a change of selection.
2698 public void updateStatusLine() {
2699 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2702 private void updateStatusLine(int x
) {
2703 // use the time provider of the time graph scale for the status line
2704 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2705 if (fStatusLineManager
== null || null == tdp
||
2706 tdp
.getTime0() == tdp
.getTime1()) {
2709 TimeFormat tf
= tdp
.getTimeFormat();
2710 Resolution res
= Resolution
.NANOSEC
;
2711 StringBuilder message
= new StringBuilder();
2712 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2713 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2714 long time
= getTimeAtX(x
);
2716 if (tdp
instanceof ITimeDataProviderConverter
) {
2717 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2719 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2721 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2722 Utils
.formatTime(time
, tf
, res
)
2726 long selectionBegin
= tdp
.getSelectionBegin();
2727 long selectionEnd
= tdp
.getSelectionEnd();
2728 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2730 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2731 Utils
.formatTime(selectionBegin
, tf
, res
)
2733 if (selectionBegin
!= selectionEnd
) {
2734 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2736 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2737 Utils
.formatTime(selectionEnd
, tf
, res
),
2738 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2741 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2742 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2743 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2744 if (tdp
instanceof ITimeDataProviderConverter
) {
2745 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2746 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2748 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2750 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2751 Utils
.formatTime(time0
, tf
, res
),
2752 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2753 Utils
.formatTime(time
, tf
, res
),
2754 Utils
.formatDelta(time
- time0
, tf
, res
)
2757 fStatusLineManager
.setMessage(message
.toString());
2761 public void mouseMove(MouseEvent e
) {
2762 if (null == fTimeProvider
) {
2765 Point size
= getSize();
2766 if (DRAG_TRACE_ITEM
== fDragState
) {
2767 int nameWidth
= fTimeProvider
.getNameSpace();
2768 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2770 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2771 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2772 long time1
= fTime1bak
- timeDelta
;
2773 long maxTime
= fTimeProvider
.getMaxTime();
2774 if (time1
> maxTime
) {
2777 long time0
= time1
- (fTime1bak
- fTime0bak
);
2778 if (time0
< fTimeProvider
.getMinTime()) {
2779 time0
= fTimeProvider
.getMinTime();
2780 time1
= time0
+ (fTime1bak
- fTime0bak
);
2782 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2784 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2786 fTimeProvider
.setNameSpace(e
.x
);
2787 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2788 } else if (DRAG_SELECTION
== fDragState
) {
2789 if (fDragBeginMarker
) {
2790 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2792 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2795 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2796 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2797 } else if (DRAG_ZOOM
== fDragState
) {
2798 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2800 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2801 } else if (DRAG_NONE
== fDragState
) {
2802 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2803 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2806 fMouseOverSplitLine
= mouseOverSplitLine
;
2809 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2810 fHasNamespaceFocus
= false;
2812 fHasNamespaceFocus
= true;
2814 updateCursor(e
.x
, e
.stateMask
);
2815 updateStatusLine(e
.x
);
2819 public void mouseDoubleClick(MouseEvent e
) {
2820 if (null == fTimeProvider
) {
2823 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2824 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2825 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2826 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2827 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2830 fMouseOverSplitLine
= mouseOverSplitLine
;
2831 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2834 int idx
= getItemIndexAtY(e
.y
);
2836 selectItem(idx
, false);
2837 fireDefaultSelection();
2843 public void mouseDown(MouseEvent e
) {
2844 if (fDragState
!= DRAG_NONE
) {
2847 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2848 int nameSpace
= fTimeProvider
.getNameSpace();
2849 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2850 fDragState
= DRAG_SPLIT_LINE
;
2851 fDragButton
= e
.button
;
2855 updateCursor(e
.x
, e
.stateMask
);
2859 if (fTimeProvider
== null ||
2860 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2861 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2865 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2866 int nameSpace
= fTimeProvider
.getNameSpace();
2867 idx
= getItemIndexAtY(e
.y
);
2869 Item item
= fItemData
.fExpandedItems
[idx
];
2870 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2874 selectItem(idx
, false);
2875 fireSelectionChanged();
2877 selectItem(idx
, false); // clear selection
2878 fireSelectionChanged();
2880 long hitTime
= getTimeAtX(e
.x
);
2884 fDragState
= DRAG_SELECTION
;
2885 fDragBeginMarker
= false;
2886 fDragButton
= e
.button
;
2889 fDragTime0
= getTimeAtX(fDragX0
);
2890 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2891 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2892 int xBegin
= getXForTime(selectionBegin
);
2893 int xEnd
= getXForTime(selectionEnd
);
2894 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2895 long time
= getTimeAtX(e
.x
);
2896 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2897 fDragBeginMarker
= true;
2900 fDragTime0
= selectionEnd
;
2903 fDragTime0
= selectionBegin
;
2906 long time
= getTimeAtX(e
.x
);
2907 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2908 fDragBeginMarker
= true;
2911 fDragTime0
= selectionEnd
;
2912 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2914 fDragTime0
= selectionBegin
;
2917 fTime0bak
= fTimeProvider
.getTime0();
2918 fTime1bak
= fTimeProvider
.getTime1();
2920 updateCursor(e
.x
, e
.stateMask
);
2921 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2923 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2924 long hitTime
= getTimeAtX(e
.x
);
2927 fDragState
= DRAG_TRACE_ITEM
;
2928 fDragButton
= e
.button
;
2931 fTime0bak
= fTimeProvider
.getTime0();
2932 fTime1bak
= fTimeProvider
.getTime1();
2933 updateCursor(e
.x
, e
.stateMask
);
2935 } else if (3 == e
.button
) {
2936 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2938 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2940 fDragTime0
= getTimeAtX(fDragX0
);
2941 fDragState
= DRAG_ZOOM
;
2942 fDragButton
= e
.button
;
2944 updateCursor(e
.x
, e
.stateMask
);
2945 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2947 idx
= getItemIndexAtY(e
.y
);
2948 selectItem(idx
, false);
2949 fireSelectionChanged();
2955 public void mouseUp(MouseEvent e
) {
2956 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2957 if ((fDragState
== DRAG_ZOOM
) && isInDragZoomMargin()) {
2958 // Select entry and time event for single click
2959 long time
= getTimeAtX(fDragX0
);
2960 fTimeProvider
.setSelectionRangeNotify(time
, time
, false);
2961 int idx
= getItemIndexAtY(e
.y
);
2962 selectItem(idx
, false);
2963 fireSelectionChanged();
2965 menuDetected(fPendingMenuDetectEvent
);
2967 if (DRAG_NONE
!= fDragState
) {
2969 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2970 fDragState
= DRAG_NONE
;
2971 if (fDragX
!= fDragX0
) {
2972 fTimeProvider
.notifyStartFinishTime();
2974 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2975 fDragState
= DRAG_NONE
;
2977 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2978 fDragState
= DRAG_NONE
;
2979 if (fDragX
== fDragX0
) { // click without selecting anything
2980 long time
= getTimeAtX(e
.x
);
2981 fTimeProvider
.setSelectedTimeNotify(time
, false);
2983 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2984 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2985 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2988 fTimeGraphScale
.setDragRange(-1, -1);
2989 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2990 fDragState
= DRAG_NONE
;
2991 int nameWidth
= fTimeProvider
.getNameSpace();
2992 if ((Math
.max(fDragX
, fDragX0
) > nameWidth
) && !isInDragZoomMargin()) {
2993 long time0
= getTimeAtX(fDragX0
);
2994 long time1
= getTimeAtX(fDragX
);
2995 if (time0
< time1
) {
2996 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2998 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
3003 fTimeGraphScale
.setDragRange(-1, -1);
3006 updateCursor(e
.x
, e
.stateMask
);
3007 updateStatusLine(e
.x
);
3011 public void mouseEnter(MouseEvent e
) {
3015 public void mouseExit(MouseEvent e
) {
3016 if (fMouseOverSplitLine
) {
3017 fMouseOverSplitLine
= false;
3020 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
3024 public void mouseHover(MouseEvent e
) {
3028 public void mouseScrolled(MouseEvent e
) {
3029 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
3034 * On some platforms the mouse scroll event is sent to the
3035 * control that has focus even if it is not under the cursor.
3036 * Handle the event only if over the time graph control.
3038 Point size
= getSize();
3039 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
3040 if (!bounds
.contains(e
.x
, e
.y
)) {
3044 boolean horizontalZoom
= false;
3045 boolean horizontalScroll
= false;
3046 boolean verticalZoom
= false;
3047 boolean verticalScroll
= false;
3049 // over the time graph control
3050 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
3051 verticalZoom
= true;
3052 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
3053 // over the name space
3054 verticalScroll
= true;
3056 // over the state area
3057 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
3058 // over the state area, CTRL pressed
3059 horizontalZoom
= true;
3060 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
3061 // over the state area, SHIFT pressed
3062 horizontalScroll
= true;
3064 // over the state area, no modifier pressed
3065 verticalScroll
= true;
3069 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
3070 verticalZoom(e
.count
> 0);
3071 if (fVerticalZoomAlignEntry
!= null) {
3072 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
3074 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
3076 } else if (horizontalScroll
) {
3077 horizontalScroll(e
.count
> 0);
3078 } else if (verticalScroll
){
3079 setTopIndex(getTopIndex() - e
.count
);
3084 * Get the vertical zoom alignment entry and position based on the current
3085 * selection. If there is no selection or if the selection is not visible,
3086 * return an alignment entry with a null time graph entry.
3088 * @return a map entry where the key is the selection's time graph entry and
3089 * the value is the center y-coordinate of that entry, or null
3091 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
3092 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
3093 if (alignEntry
!= null) {
3096 int index
= getSelectedIndex();
3097 if (index
== -1 || index
>= getExpandedElementCount()) {
3098 return new SimpleEntry
<>(null, 0);
3100 Rectangle bounds
= getClientArea();
3101 Rectangle itemRect
= getItemRect(bounds
, index
);
3102 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
3103 /* selection is not visible */
3104 return new SimpleEntry
<>(null, 0);
3106 ITimeGraphEntry entry
= getExpandedElement(index
);
3107 int y
= itemRect
.y
+ itemRect
.height
/ 2;
3108 return new SimpleEntry
<>(entry
, y
);
3112 * Get the vertical zoom alignment entry and position at the specified
3116 * the cursor y-coordinate
3117 * @return a map entry where the key is the time graph entry under the
3118 * cursor and the value is the cursor y-coordinate
3120 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
3121 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
3122 if (alignEntry
!= null) {
3125 int index
= getItemIndexAtY(y
);
3127 index
= getExpandedElementCount() - 1;
3129 ITimeGraphEntry entry
= getExpandedElement(index
);
3130 return new SimpleEntry
<>(entry
, y
);
3134 * Get the vertical zoom alignment entry and position if there is an ongoing
3135 * one and we are within the vertical zoom delay, or otherwise return null.
3137 * @return a map entry where the key is a time graph entry and the value is
3138 * a y-coordinate, or null
3140 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
3141 long currentTimeMillis
= System
.currentTimeMillis();
3142 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
3144 * If the vertical zoom is triggered repeatedly in a short amount of
3145 * time, use the initial event's entry and position.
3147 fVerticalZoomAlignTime
= currentTimeMillis
;
3148 return fVerticalZoomAlignEntry
;
3150 fVerticalZoomAlignTime
= currentTimeMillis
;
3155 public void handleEvent(Event event
) {
3156 if (event
.type
== SWT
.MouseWheel
) {
3157 // prevent horizontal scrolling when the mouse wheel is used to
3158 // scroll vertically or zoom
3164 public int getBorderWidth() {
3165 return fBorderWidth
;
3169 * Set the border width
3171 * @param borderWidth
3174 public void setBorderWidth(int borderWidth
) {
3175 this.fBorderWidth
= borderWidth
;
3179 * @return The current height of the header row
3181 public int getHeaderHeight() {
3182 return fHeaderHeight
;
3186 * Set the height of the header row
3188 * @param headerHeight
3191 public void setHeaderHeight(int headerHeight
) {
3192 this.fHeaderHeight
= headerHeight
;
3196 * @return The default height of regular item rows
3198 public int getItemHeight() {
3199 return fGlobalItemHeight
;
3203 * Set the default height of regular item rows.
3208 public void setItemHeight(int rowHeight
) {
3209 this.fGlobalItemHeight
= rowHeight
;
3210 for (Item item
: fItemData
.fItems
) {
3211 item
.fItemHeight
= rowHeight
;
3216 * Set the height of a specific item. Overrides the default item height.
3219 * A time graph entry
3222 * @return true if the height is successfully stored, false otherwise
3224 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3225 Item item
= fItemData
.findItem(entry
);
3227 item
.fItemHeight
= rowHeight
;
3234 * Set the minimum item width
3236 * @param width The minimum width
3238 public void setMinimumItemWidth(int width
) {
3239 this.fMinimumItemWidth
= width
;
3243 * @return The minimum item width
3245 public int getMinimumItemWidth() {
3246 return fMinimumItemWidth
;
3250 * Set whether all time events with a duration shorter than one pixel should
3251 * be blended in. If false, only the first such time event will be drawn and
3252 * the subsequent time events in the same pixel will be discarded. The
3253 * default value is false.
3256 * true if sub-pixel events should be blended, false otherwise.
3259 public void setBlendSubPixelEvents(boolean blend
) {
3260 fBlendSubPixelEvents
= blend
;
3264 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3265 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3266 fSelectionChangedListeners
.add(listener
);
3271 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3272 if (listener
!= null) {
3273 fSelectionChangedListeners
.remove(listener
);
3278 public void setSelection(ISelection selection
) {
3279 if (selection
instanceof IStructuredSelection
) {
3280 Object ob
= ((IStructuredSelection
) selection
).getFirstElement();
3281 if (ob
instanceof ITimeGraphEntry
) {
3282 selectItem((ITimeGraphEntry
) ob
, false);
3289 * @param filter The filter object to be attached to the view
3291 public void addFilter(@NonNull ViewerFilter filter
) {
3292 if (!fFilters
.contains(filter
)) {
3293 fFilters
.add(filter
);
3298 * @param filter The filter object to be attached to the view
3300 public void removeFilter(@NonNull ViewerFilter filter
) {
3301 fFilters
.remove(filter
);
3305 * Returns this control's filters.
3307 * @return an array of viewer filters
3310 public @NonNull ViewerFilter
[] getFilters() {
3311 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3315 * Sets the filters, replacing any previous filters.
3318 * an array of viewer filters, or null
3321 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3323 if (filters
!= null) {
3324 fFilters
.addAll(Arrays
.asList(filters
));
3329 public void colorSettingsChanged(StateItem
[] stateItems
) {
3330 /* Destroy previous colors from the resource manager */
3331 if (fEventColorMap
!= null) {
3332 for (Color color
: fEventColorMap
) {
3333 fResourceManager
.destroyColor(color
.getRGB());
3336 if (stateItems
!= null) {
3337 fEventColorMap
= new Color
[stateItems
.length
];
3338 for (int i
= 0; i
< stateItems
.length
; i
++) {
3339 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3342 fEventColorMap
= new Color
[] { };
3347 private class ItemData
{
3348 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3349 private Item
[] fExpandedItems
= new Item
[0];
3350 private Item
[] fItems
= new Item
[0];
3351 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3352 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3357 public Item
findItem(ITimeGraphEntry entry
) {
3358 return fItemMap
.get(entry
);
3361 public int findItemIndex(ITimeGraphEntry entry
) {
3362 Item item
= fItemMap
.get(entry
);
3366 return item
.fExpandedIndex
;
3369 public void refreshData() {
3370 ITimeGraphEntry selection
= getSelectedTrace();
3371 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3372 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3373 ITimeGraphEntry entry
= fRootEntries
[i
];
3374 refreshData(itemMap
, null, 0, entry
);
3377 fItems
= fItemMap
.values().toArray(new Item
[0]);
3378 updateExpandedItems();
3379 if (selection
!= null) {
3380 for (Item item
: fExpandedItems
) {
3381 if (item
.fEntry
== selection
) {
3382 item
.fSelected
= true;
3389 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3390 Item item
= new Item(entry
, entry
.getName(), level
);
3391 if (parent
!= null) {
3392 parent
.fChildren
.add(item
);
3394 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3395 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3397 item
.fItemHeight
= fGlobalItemHeight
;
3399 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3400 itemMap
.put(entry
, item
);
3401 if (entry
.hasChildren()) {
3402 Item oldItem
= fItemMap
.get(entry
);
3403 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3404 /* existing items keep their old expanded state */
3405 item
.fExpanded
= oldItem
.fExpanded
;
3407 /* new items set the expanded state according to auto-expand level */
3408 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3410 item
.fHasChildren
= true;
3411 for (ITimeGraphEntry child
: entry
.getChildren()) {
3412 refreshData(itemMap
, item
, level
+ 1, child
);
3417 public void updateExpandedItems() {
3418 for (Item item
: fItems
) {
3419 item
.fExpandedIndex
= -1;
3421 List
<Item
> expandedItemList
= new ArrayList
<>();
3422 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3423 ITimeGraphEntry entry
= fRootEntries
[i
];
3424 Item item
= findItem(entry
);
3425 refreshExpanded(expandedItemList
, item
);
3427 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3428 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3431 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3432 // Check for filters
3433 boolean display
= true;
3434 for (ViewerFilter filter
: fFilters
) {
3435 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3441 item
.fExpandedIndex
= expandedItemList
.size();
3442 expandedItemList
.add(item
);
3443 if (item
.fHasChildren
&& item
.fExpanded
) {
3444 for (Item child
: item
.fChildren
) {
3445 refreshExpanded(expandedItemList
, child
);
3451 public void refreshData(ITimeGraphEntry
[] entries
) {
3452 if (entries
== null) {
3453 fRootEntries
= null;
3455 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3461 public void refreshArrows(List
<ILinkEvent
> events
) {
3462 /* If links are null, reset the list */
3463 if (events
!= null) {
3466 fLinks
= new ArrayList
<>();
3470 public ITimeGraphEntry
[] getEntries() {
3471 return fRootEntries
;
3475 private class Item
{
3476 private boolean fExpanded
;
3477 private int fExpandedIndex
;
3478 private boolean fSelected
;
3479 private boolean fHasChildren
;
3480 private int fItemHeight
;
3481 private final int fLevel
;
3482 private final List
<Item
> fChildren
;
3483 private final String fName
;
3484 private final ITimeGraphEntry fEntry
;
3486 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3487 this.fEntry
= entry
;
3489 this.fLevel
= level
;
3490 this.fChildren
= new ArrayList
<>();
3494 public String
toString() {
3500 public void menuDetected(MenuDetectEvent e
) {
3501 if (null == fTimeProvider
) {
3505 * This flag indicates if menu was prevented from being shown below and
3506 * therefore must be made visible on callback from mouseUp().
3508 boolean pendingEventCallback
= fPendingMenuDetectEvent
!= null;
3509 Point p
= toControl(e
.x
, e
.y
);
3510 if (e
.detail
== SWT
.MENU_MOUSE
&& isOverTimeSpace(p
.x
, p
.y
)) {
3511 if (fPendingMenuDetectEvent
== null) {
3512 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3513 * Store the event and trigger it later just before handling mouseUp.
3514 * This allows for the method to detect if mouse is used to drag zoom.
3516 fPendingMenuDetectEvent
= e
;
3518 * Prevent the platform to show the menu when returning. The
3519 * menu will be shown (see below) when this method is called
3520 * again during mouseUp().
3525 fPendingMenuDetectEvent
= null;
3526 if (fDragState
!= DRAG_ZOOM
|| !isInDragZoomMargin()) {
3528 * Don't show the menu on mouseUp() if a drag zoom is in
3529 * progress with a drag range outside of the drag zoom margin,
3530 * or if any other drag operation, or none, is in progress.
3536 if (fDragState
!= DRAG_NONE
) {
3538 * Don't show the menu on keyboard menu or mouse menu outside of
3539 * the time space if any drag operation is in progress.
3545 int idx
= getItemIndexAtY(p
.y
);
3546 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3547 Item item
= fItemData
.fExpandedItems
[idx
];
3548 ITimeGraphEntry entry
= item
.fEntry
;
3550 /* Send menu event for the time graph entry */
3553 fireMenuEventOnTimeGraphEntry(e
);
3554 Menu menu
= getMenu();
3555 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3556 menu
.setVisible(true);
3559 /* Send menu event for time event */
3560 if (entry
.hasTimeEvents()) {
3561 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3562 if (event
!= null) {
3565 fireMenuEventOnTimeEvent(e
);
3567 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3568 menu
.setVisible(true);
3576 * Perform the alignment operation.
3579 * the alignment offset
3581 * @see ITmfTimeAligned
3585 public void performAlign(int offset
) {
3586 fTimeProvider
.setNameSpace(offset
);
3590 * Return the time alignment information
3592 * @return the time alignment information
3594 * @see ITmfTimeAligned
3598 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3599 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());
3602 private boolean isInDragZoomMargin() {
3603 return (Math
.abs(fDragX
- fDragX0
) < DRAG_MARGIN
);