1 /*****************************************************************************
2 * Copyright (c) 2007, 2017 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
.log
.TraceCompassLog
;
85 import org
.eclipse
.tracecompass
.common
.core
.math
.SaturatedArithmetic
;
86 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
91 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
92 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
93 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
94 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
95 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
96 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
97 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
98 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
99 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
100 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
101 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEventStyleStrings
;
102 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
103 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
104 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
106 import com
.google
.common
.collect
.Iterables
;
109 * Time graph control implementation
111 * @author Alvaro Sanchez-Leon
112 * @author Patrick Tasse
114 public class TimeGraphControl
extends TimeGraphBaseControl
115 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
116 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
117 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
120 * Constant indicating that all levels of the time graph should be expanded
122 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
124 private static final int DRAG_MARGIN
= 5;
126 private static final int DRAG_NONE
= 0;
127 private static final int DRAG_TRACE_ITEM
= 1;
128 private static final int DRAG_SPLIT_LINE
= 2;
129 private static final int DRAG_ZOOM
= 3;
130 private static final int DRAG_SELECTION
= 4;
133 * Get item height from provider
135 private static final int CUSTOM_ITEM_HEIGHT
= -1;
137 private static final double ZOOM_FACTOR
= 1.5;
138 private static final double ZOOM_IN_FACTOR
= 0.8;
139 private static final double ZOOM_OUT_FACTOR
= 1.25;
141 private static final int SNAP_WIDTH
= 3;
142 private static final int ARROW_HOVER_MAX_DIST
= 5;
145 * base to height ratio
147 private static final double ARROW_RATIO
= Math
.sqrt(3) / 2;
148 private static final int NO_STATUS
= -1;
149 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
151 private static final int MAX_LABEL_LENGTH
= 256;
153 /** points per inch */
154 private static final int PPI
= 72;
155 private static final int DPI
= Display
.getDefault().getDPI().y
;
157 private static final int VERTICAL_ZOOM_DELAY
= 400;
159 private static final String PREFERRED_WIDTH
= "width"; //$NON-NLS-1$
161 /** Resource manager */
162 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
164 /** Color map for event types */
165 private Color
[] fEventColorMap
= null;
167 private ITimeDataProvider fTimeProvider
;
168 private ITableLabelProvider fLabelProvider
;
169 private IStatusLineManager fStatusLineManager
= null;
170 private Tree fTree
= null;
171 private TimeGraphScale fTimeGraphScale
= null;
173 private boolean fIsInFocus
= false;
174 private boolean fMouseOverSplitLine
= false;
175 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
176 private int fHeightAdjustment
= 0;
177 private int fMaxItemHeight
= 0;
178 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
179 private boolean fBlendSubPixelEvents
= false;
180 private int fMinimumItemWidth
= 0;
181 private int fTopIndex
= 0;
182 private int fDragState
= DRAG_NONE
;
183 private boolean fDragBeginMarker
= false;
184 private int fDragButton
;
185 private int fDragX0
= 0;
186 private int fDragX
= 0;
187 private boolean fHasNamespaceFocus
= false;
189 * Used to preserve accuracy of modified selection
191 private long fDragTime0
= 0;
192 private int fIdealNameSpace
= 0;
193 private boolean fAutoResizeColumns
= true;
194 private long fTime0bak
;
195 private long fTime1bak
;
196 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
197 private ItemData fItemData
= null;
198 private List
<IMarkerEvent
> fMarkers
= null;
199 private boolean fMarkersVisible
= true;
200 private List
<SelectionListener
> fSelectionListeners
;
201 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
202 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
203 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
204 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
205 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
206 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
207 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
208 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
209 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
210 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
211 private MenuDetectEvent fPendingMenuDetectEvent
= null;
212 private boolean fGridLinesVisible
= true;
213 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
214 private boolean fHideArrows
= false;
215 private int fAutoExpandLevel
= ALL_LEVELS
;
216 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
217 private long fVerticalZoomAlignTime
= 0;
218 private int fBorderWidth
= 0;
219 private int fHeaderHeight
= 0;
221 private boolean fFirstHeightWarning
= true;
224 * Standard constructor
227 * The parent composite object
229 * The color scheme to use
231 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
233 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
235 fItemData
= new ItemData();
237 addFocusListener(this);
238 addMouseListener(this);
239 addMouseMoveListener(this);
240 addMouseTrackListener(this);
241 addMouseWheelListener(this);
242 addTraverseListener(this);
243 addKeyListener(this);
244 addMenuDetectListener(this);
245 addListener(SWT
.MouseWheel
, this);
246 addDisposeListener((e
) -> {
247 fResourceManager
.dispose();
248 for (Font font
: fFonts
.values()) {
255 * Sets the timegraph provider used by this timegraph viewer.
257 * @param timeGraphProvider
258 * the timegraph provider
260 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
261 fTimeGraphProvider
= timeGraphProvider
;
263 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
264 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
265 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
268 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
269 colorSettingsChanged(stateItems
);
273 * Gets the timegraph provider used by this timegraph viewer.
275 * @return the timegraph provider, or <code>null</code> if not set.
277 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
278 return fTimeGraphProvider
;
282 * Gets the time data provider used by this viewer.
284 * @return The time data provider, or <code>null</code> if not set
287 public ITimeDataProvider
getTimeDataProvider() {
288 return fTimeProvider
;
292 * Gets the color map used by this timegraph viewer.
294 * @return a color map, or <code>null</code> if not set.
296 public Color
[] getEventColorMap() {
297 return fEventColorMap
;
301 * Assign the given time provider
303 * @param timeProvider
306 public void setTimeProvider(ITimeDataProvider timeProvider
) {
307 fTimeProvider
= timeProvider
;
312 * Set the label provider for the name space
314 * @param labelProvider
318 public void setLabelProvider(ITableLabelProvider labelProvider
) {
319 fLabelProvider
= labelProvider
;
324 * Get the label provider for the name space
326 * @return The label provider
329 public ITableLabelProvider
getLabelProvider() {
330 return fLabelProvider
;
334 * Assign the status line manager
336 * @param statusLineManager
337 * The status line manager, or null to disable status line
340 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
341 if (fStatusLineManager
!= null && statusLineManager
== null) {
342 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
344 fStatusLineManager
= statusLineManager
;
348 * Assign the tree that represents the name space header
354 public void setTree(Tree tree
) {
359 * Returns the tree control associated with this time graph control. The
360 * tree is only used for column handling of the name space and contains no
363 * @return the tree control
366 public Tree
getTree() {
371 * Sets the columns for this time graph control's name space.
377 public void setColumns(String
[] columnNames
) {
378 for (TreeColumn column
: fTree
.getColumns()) {
381 ControlListener controlListener
= new ControlListener() {
383 public void controlResized(ControlEvent e
) {
384 if (fAutoResizeColumns
&& ((TreeColumn
) e
.widget
).getWidth() < (Integer
) e
.widget
.getData(PREFERRED_WIDTH
)) {
385 fAutoResizeColumns
= false;
391 public void controlMoved(ControlEvent e
) {
395 for (String columnName
: columnNames
) {
396 TreeColumn column
= new TreeColumn(fTree
, SWT
.LEFT
);
397 column
.setMoveable(true);
398 column
.setText(columnName
);
400 column
.setData(PREFERRED_WIDTH
, column
.getWidth());
401 column
.addControlListener(controlListener
);
406 * Assign the time graph scale
408 * @param timeGraphScale
409 * The time graph scale
411 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
412 fTimeGraphScale
= timeGraphScale
;
416 * Add a selection listener
419 * The listener to add
421 public void addSelectionListener(SelectionListener listener
) {
422 if (listener
== null) {
423 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
425 if (null == fSelectionListeners
) {
426 fSelectionListeners
= new ArrayList
<>();
428 fSelectionListeners
.add(listener
);
432 * Remove a selection listener
435 * The listener to remove
437 public void removeSelectionListener(SelectionListener listener
) {
438 if (null != fSelectionListeners
) {
439 fSelectionListeners
.remove(listener
);
444 * Selection changed callback
446 public void fireSelectionChanged() {
447 if (null != fSelectionListeners
) {
448 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
449 while (it
.hasNext()) {
450 SelectionListener listener
= it
.next();
451 listener
.widgetSelected(null);
455 if (null != fSelectionChangedListeners
) {
456 for (ISelectionChangedListener listener
: fSelectionChangedListeners
) {
457 listener
.selectionChanged(new SelectionChangedEvent(this, getSelection()));
463 * Default selection callback
465 public void fireDefaultSelection() {
466 if (null != fSelectionListeners
) {
467 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
468 while (it
.hasNext()) {
469 SelectionListener listener
= it
.next();
470 listener
.widgetDefaultSelected(null);
476 * Add a drag selection listener
479 * The listener to add
481 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
482 if (listener
== null) {
483 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
485 if (null == fDragSelectionListeners
) {
486 fDragSelectionListeners
= new ArrayList
<>();
488 fDragSelectionListeners
.add(listener
);
492 * Remove a drag selection listener
495 * The listener to remove
497 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
498 if (null != fDragSelectionListeners
) {
499 fDragSelectionListeners
.remove(listener
);
504 * Drag Selection changed callback
507 * Time interval start
511 public void fireDragSelectionChanged(long start
, long end
) {
512 // check for backward intervals
513 long beginTime
, endTime
;
521 // call the listeners
522 if (null != fDragSelectionListeners
) {
523 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
524 while (it
.hasNext()) {
525 ITimeGraphTimeListener listener
= it
.next();
526 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
532 * Get the traces in the model
534 * @return The array of traces
536 public ITimeGraphEntry
[] getTraces() {
537 return fItemData
.getEntries();
541 * Refresh the data for the thing
543 public void refreshData() {
544 fItemData
.refreshData();
549 * Refresh data for the given traces
552 * The traces to refresh
554 public void refreshData(ITimeGraphEntry
[] traces
) {
555 fItemData
.refreshData(traces
);
560 * Refresh the links (arrows) of this widget
563 * The link event list
565 public void refreshArrows(List
<ILinkEvent
> events
) {
566 fItemData
.refreshArrows(events
);
570 * Get the links (arrows) of this widget
572 * @return The unmodifiable link event list
576 public List
<ILinkEvent
> getArrows() {
577 return Collections
.unmodifiableList(fItemData
.fLinks
);
580 boolean ensureVisibleItem(int idx
, boolean redraw
) {
581 boolean changed
= false;
584 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
585 if (fItemData
.fExpandedItems
[index
].fSelected
) {
590 if (index
>= fItemData
.fExpandedItems
.length
) {
593 if (index
< fTopIndex
) {
600 int page
= countPerPage();
601 if (index
>= fTopIndex
+ page
) {
602 setTopIndex(index
- page
+ 1);
613 * Assign the given index as the top one
618 public void setTopIndex(int idx
) {
619 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
620 index
= Math
.max(0, index
);
626 * Set the top index so that the requested element is at the specified
630 * the time graph entry to be positioned
632 * the requested y-coordinate
635 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
636 Item item
= fItemData
.fItemMap
.get(entry
);
637 if (item
== null || item
.fExpandedIndex
== -1) {
640 int index
= item
.fExpandedIndex
;
641 Rectangle itemRect
= getItemRect(getClientArea(), index
);
642 int delta
= itemRect
.y
+ itemRect
.height
- y
;
643 int topIndex
= getItemIndexAtY(delta
);
644 if (topIndex
!= -1) {
645 setTopIndex(topIndex
);
650 setTopIndex(getExpandedElementCount());
656 * Sets the auto-expand level to be used for new entries discovered when
657 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
658 * . The value 0 means that there is no auto-expand; 1 means that top-level
659 * entries are expanded, but not their children; 2 means that top-level
660 * entries are expanded, and their children, but not grand-children; and so
663 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
667 * non-negative level, or <code>ALL_LEVELS</code> to expand all
670 public void setAutoExpandLevel(int level
) {
671 fAutoExpandLevel
= level
;
675 * Returns the auto-expand level.
677 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
678 * the tree are expanded automatically
679 * @see #setAutoExpandLevel
681 public int getAutoExpandLevel() {
682 return fAutoExpandLevel
;
686 * Get the expanded state of a given entry.
690 * @return true if the entry is expanded, false if collapsed
693 public boolean getExpandedState(ITimeGraphEntry entry
) {
694 Item item
= fItemData
.fItemMap
.get(entry
);
695 return (item
!= null ? item
.fExpanded
: false);
699 * Set the expanded state of a given entry
704 * True if expanded, false if collapsed
706 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
707 Item item
= fItemData
.findItem(entry
);
708 if (item
!= null && item
.fExpanded
!= expanded
) {
709 item
.fExpanded
= expanded
;
710 fItemData
.updateExpandedItems();
716 * Set the expanded state of a given entry to certain relative level. It
717 * will call fireTreeEvent() for each changed entry. At the end it will call
723 * level to expand to or negative for all levels
725 * True if expanded, false if collapsed
727 private void setExpandedState(ITimeGraphEntry entry
, int level
, boolean expanded
) {
728 setExpandedStateInt(entry
, level
, expanded
);
733 * Set the expanded state of a given entry and its children to the first
734 * level that has one collapsed entry.
739 private void setExpandedStateLevel(ITimeGraphEntry entry
) {
740 int level
= findExpandedLevel(entry
);
742 setExpandedStateInt(entry
, level
, true);
748 * Inner class for finding relative level with at least one collapsed entry.
750 private class SearchNode
{
751 SearchNode(ITimeGraphEntry e
, int l
) {
756 ITimeGraphEntry entry
;
761 * Finds the relative level with at least one collapsed entry.
765 * @return the found level or -1 if all levels are already expanded.
767 private int findExpandedLevel(ITimeGraphEntry entry
) {
768 Queue
<SearchNode
> queue
= new LinkedList
<>();
769 SearchNode root
= new SearchNode(entry
, 0);
770 SearchNode node
= root
;
773 while (!queue
.isEmpty()) {
774 node
= queue
.remove();
775 if (node
.entry
.hasChildren() && !getExpandedState(node
.entry
)) {
778 for (ITimeGraphEntry e
: node
.entry
.getChildren()) {
779 if (e
.hasChildren()) {
780 SearchNode n
= new SearchNode(e
, node
.level
+ 1);
789 * Set the expanded state of a given entry to certain relative level. It
790 * will call fireTreeEvent() for each changed entry. No redraw is done.
795 * level to expand to or negative for all levels
797 * True if expanded, false if collapsed
799 private void setExpandedStateInt(ITimeGraphEntry entry
, int aLevel
, boolean expanded
) {
801 if ((level
> 0) || (level
< 0)) {
803 if (entry
.hasChildren()) {
804 for (ITimeGraphEntry e
: entry
.getChildren()) {
805 setExpandedStateInt(e
, level
, expanded
);
809 Item item
= fItemData
.findItem(entry
);
810 if (item
!= null && item
.fExpanded
!= expanded
) {
811 item
.fExpanded
= expanded
;
812 fItemData
.updateExpandedItems();
813 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
818 * Collapses all nodes of the viewer's tree, starting with the root.
820 public void collapseAll() {
821 for (Item item
: fItemData
.fItems
) {
822 item
.fExpanded
= false;
824 fItemData
.updateExpandedItems();
829 * Expands all nodes of the viewer's tree, starting with the root.
831 public void expandAll() {
832 for (Item item
: fItemData
.fItems
) {
833 item
.fExpanded
= true;
835 fItemData
.updateExpandedItems();
840 * Add a tree listener
843 * The listener to add
845 public void addTreeListener(ITimeGraphTreeListener listener
) {
846 if (!fTreeListeners
.contains(listener
)) {
847 fTreeListeners
.add(listener
);
852 * Remove a tree listener
855 * The listener to remove
857 public void removeTreeListener(ITimeGraphTreeListener listener
) {
858 if (fTreeListeners
.contains(listener
)) {
859 fTreeListeners
.remove(listener
);
864 * Tree event callback
869 * The expanded state (true for expanded, false for collapsed)
871 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
872 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
873 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
875 listener
.treeExpanded(event
);
877 listener
.treeCollapsed(event
);
883 * Add a menu listener on {@link ITimeGraphEntry}s
886 * The listener to add
888 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
889 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
890 fTimeGraphEntryMenuListeners
.add(listener
);
895 * Remove a menu listener on {@link ITimeGraphEntry}s
898 * The listener to remove
900 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
901 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
902 fTimeGraphEntryMenuListeners
.remove(listener
);
907 * Menu event callback on {@link ITimeGraphEntry}s
910 * The MenuDetectEvent, with field {@link TypedEvent#data} set to
911 * the selected {@link ITimeGraphEntry}
913 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
914 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
915 listener
.menuDetected(event
);
920 * Add a menu listener on {@link ITimeEvent}s
923 * The listener to add
925 public void addTimeEventMenuListener(MenuDetectListener listener
) {
926 if (!fTimeEventMenuListeners
.contains(listener
)) {
927 fTimeEventMenuListeners
.add(listener
);
932 * Remove a menu listener on {@link ITimeEvent}s
935 * The listener to remove
937 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
938 if (fTimeEventMenuListeners
.contains(listener
)) {
939 fTimeEventMenuListeners
.remove(listener
);
944 * Menu event callback on {@link ITimeEvent}s
947 * The MenuDetectEvent, with field {@link TypedEvent#data} set to
948 * the selected {@link ITimeEvent}
950 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
951 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
952 listener
.menuDetected(event
);
957 public boolean setFocus() {
958 if ((fTimeProvider
!= null) && fTimeProvider
.getNameSpace() > 0) {
959 fHasNamespaceFocus
= true;
961 return super.setFocus();
965 * Returns the current selection for this time graph. If a time graph entry
966 * is selected, it will be the first element in the selection. If a time
967 * event is selected, it will be the second element in the selection.
969 * @return the current selection
972 public ISelection
getSelection() {
973 ITimeGraphEntry entry
= getSelectedTrace();
974 if (null != entry
&& null != fTimeProvider
) {
975 long selectedTime
= fTimeProvider
.getSelectionBegin();
976 ITimeEvent event
= Utils
.findEvent(entry
, selectedTime
, 0);
978 return new StructuredSelection(entry
);
980 return new StructuredSelection(new Object
[] { entry
, event
});
982 return StructuredSelection
.EMPTY
;
986 * Get the selection object
988 * @return The selection
990 public ISelection
getSelectionTrace() {
991 ITimeGraphEntry entry
= getSelectedTrace();
993 return new StructuredSelection(entry
);
995 return StructuredSelection
.EMPTY
;
999 * Enable/disable one of the traces in the model
1002 * 1 to enable it, -1 to disable. The method returns immediately
1003 * if another value is used.
1005 public void selectTrace(int n
) {
1006 if ((n
!= 1) && (n
!= -1)) {
1010 boolean changed
= false;
1011 int lastSelection
= -1;
1012 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1013 Item item
= fItemData
.fExpandedItems
[i
];
1014 if (item
.fSelected
) {
1016 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
1017 item
.fSelected
= false;
1018 item
= fItemData
.fExpandedItems
[i
+ 1];
1019 item
.fSelected
= true;
1021 } else if ((-1 == n
) && (i
> 0)) {
1022 item
.fSelected
= false;
1023 item
= fItemData
.fExpandedItems
[i
- 1];
1024 item
.fSelected
= true;
1031 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
1032 Item item
= fItemData
.fExpandedItems
[0];
1033 item
.fSelected
= true;
1038 ensureVisibleItem(-1, false);
1040 fireSelectionChanged();
1048 * 1 for next event, -1 for previous event
1050 * true to extend selection range, false for single selection
1053 public void selectEvent(int n
, boolean extend
) {
1054 if (null == fTimeProvider
) {
1057 ITimeGraphEntry trace
= getSelectedTrace();
1058 if (trace
== null) {
1061 long selectedTime
= fTimeProvider
.getSelectionEnd();
1062 long endTime
= fTimeProvider
.getMaxTime();
1063 ITimeEvent nextEvent
;
1064 if (n
== -1 && selectedTime
> endTime
) {
1065 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
1067 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
1069 if (null == nextEvent
&& n
== -1) {
1070 nextEvent
= Utils
.getFirstEvent(trace
);
1072 if (null != nextEvent
) {
1073 long nextTime
= nextEvent
.getTime();
1074 // If last event detected e.g. going back or not moving to a next
1076 if (nextTime
<= selectedTime
&& n
== 1) {
1077 // Select to the end of this last event
1078 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
1079 // but not beyond the end of the trace
1080 if (nextTime
> endTime
) {
1083 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
1084 // for previous event go to its end time unless we were already
1086 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
1089 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
1091 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
1093 fireSelectionChanged();
1094 } else if (n
== 1) {
1096 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
1098 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
1100 fireSelectionChanged();
1102 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1106 * Select the next event
1109 * true to extend selection range, false for single selection
1112 public void selectNextEvent(boolean extend
) {
1113 selectEvent(1, extend
);
1114 // Notify if visible time window has been adjusted
1115 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1119 * Select the previous event
1122 * true to extend selection range, false for single selection
1125 public void selectPrevEvent(boolean extend
) {
1126 selectEvent(-1, extend
);
1127 // Notify if visible time window has been adjusted
1128 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1132 * Select the next trace
1134 public void selectNextTrace() {
1139 * Select the previous trace
1141 public void selectPrevTrace() {
1146 * Scroll left or right by one half window size
1149 * true to scroll left, false to scroll right
1151 public void horizontalScroll(boolean left
) {
1152 long time0
= fTimeProvider
.getTime0();
1153 long time1
= fTimeProvider
.getTime1();
1154 long timeMin
= fTimeProvider
.getMinTime();
1155 long timeMax
= fTimeProvider
.getMaxTime();
1156 long range
= time1
- time0
;
1160 long increment
= Math
.max(1, range
/ 2);
1162 time0
= Math
.max(time0
- increment
, timeMin
);
1163 time1
= time0
+ range
;
1165 time1
= Math
.min(time1
+ increment
, timeMax
);
1166 time0
= time1
- range
;
1168 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1172 * Zoom based on mouse cursor location with mouse scrolling
1175 * true to zoom in, false to zoom out
1177 public void zoom(boolean zoomIn
) {
1178 int globalX
= getDisplay().getCursorLocation().x
;
1179 Point p
= toControl(globalX
, 0);
1180 int nameSpace
= fTimeProvider
.getNameSpace();
1181 int timeSpace
= fTimeProvider
.getTimeSpace();
1182 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
1183 long time0
= fTimeProvider
.getTime0();
1184 long time1
= fTimeProvider
.getTime1();
1185 long interval
= time1
- time0
;
1186 if (interval
== 0) {
1188 } // to allow getting out of single point interval
1191 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
1193 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
1195 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
1196 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
1197 /* snap to bounds if zooming out of range */
1198 newTime0
= Math
.max(fTimeProvider
.getMinTime(), Math
.min(newTime0
, fTimeProvider
.getMaxTime() - newInterval
));
1199 long newTime1
= newTime0
+ newInterval
;
1200 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
1204 * zoom in using single click
1206 public void zoomIn() {
1207 long prevTime0
= fTimeProvider
.getTime0();
1208 long prevTime1
= fTimeProvider
.getTime1();
1209 long prevRange
= prevTime1
- prevTime0
;
1210 if (prevRange
== 0) {
1213 ITimeDataProvider provider
= fTimeProvider
;
1214 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1215 if (selTime
< prevTime0
|| selTime
> prevTime1
) {
1216 selTime
= (prevTime0
+ prevTime1
) / 2;
1218 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
1219 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
1221 long min
= fTimeProvider
.getMinTimeInterval();
1222 if ((time1
- time0
) < min
) {
1223 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
1224 time1
= time0
+ min
;
1227 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1231 * zoom out using single click
1233 public void zoomOut() {
1234 long prevTime0
= fTimeProvider
.getTime0();
1235 long prevTime1
= fTimeProvider
.getTime1();
1236 ITimeDataProvider provider
= fTimeProvider
;
1237 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
1238 if (selTime
< prevTime0
|| selTime
> prevTime1
) {
1239 selTime
= (prevTime0
+ prevTime1
) / 2;
1243 if (prevTime1
- prevTime0
<= 1) {
1245 time0
= selTime
- 1;
1247 newInterval
= (long) Math
.ceil((prevTime1
- prevTime0
) * ZOOM_FACTOR
);
1248 time0
= selTime
- (long) Math
.ceil((selTime
- prevTime0
) * ZOOM_FACTOR
);
1250 /* snap to bounds if zooming out of range */
1251 time0
= Math
.max(fTimeProvider
.getMinTime(), Math
.min(time0
, fTimeProvider
.getMaxTime() - newInterval
));
1252 long time1
= time0
+ newInterval
;
1254 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1261 * true to zoom in, false to zoom out
1264 public void verticalZoom(boolean zoomIn
) {
1266 fHeightAdjustment
++;
1268 fHeightAdjustment
--;
1269 fHeightAdjustment
= Math
.max(fHeightAdjustment
, 1 - fMaxItemHeight
);
1271 fItemData
.refreshData();
1276 * Reset the vertical zoom to default.
1280 public void resetVerticalZoom() {
1281 fHeightAdjustment
= 0;
1282 fItemData
.refreshData();
1287 * Set the grid lines visibility. The default is true.
1290 * true to show the grid lines, false otherwise
1293 public void setGridLinesVisible(boolean visible
) {
1294 fGridLinesVisible
= visible
;
1298 * Get the grid lines visibility.
1300 * @return true if the grid lines are visible, false otherwise
1303 public boolean getGridLinesVisible() {
1304 return fGridLinesVisible
;
1308 * Set the grid line color. The default is SWT.COLOR_GRAY.
1311 * the grid line color
1314 public void setGridLineColor(Color color
) {
1315 fGridLineColor
= color
;
1319 * Get the grid line color.
1321 * @return the grid line color
1324 public Color
getGridLineColor() {
1325 return fGridLineColor
;
1329 * Set the markers list.
1332 * The markers list, or null
1335 public void setMarkers(List
<IMarkerEvent
> markers
) {
1340 * Get the markers list.
1342 * @return The markers list, or null
1345 public List
<IMarkerEvent
> getMarkers() {
1350 * Set the markers visibility. The default is true.
1353 * true to show the markers, false otherwise
1356 public void setMarkersVisible(boolean visible
) {
1357 fMarkersVisible
= visible
;
1361 * Get the markers visibility.
1363 * @return true if the markers are visible, false otherwise
1366 public boolean getMarkersVisible() {
1367 return fMarkersVisible
;
1374 * true to hide arrows
1376 public void hideArrows(boolean hideArrows
) {
1377 fHideArrows
= hideArrows
;
1381 * Follow the arrow forward
1384 * true to extend selection range, false for single selection
1387 public void followArrowFwd(boolean extend
) {
1388 ITimeGraphEntry trace
= getSelectedTrace();
1389 if (trace
== null) {
1392 long selectedTime
= fTimeProvider
.getSelectionEnd();
1393 for (ILinkEvent link
: fItemData
.fLinks
) {
1394 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1395 selectItem(link
.getDestinationEntry(), false);
1396 if (link
.getDuration() != 0) {
1398 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1400 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1402 // Notify if visible time window has been adjusted
1403 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1405 fireSelectionChanged();
1406 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1410 selectNextEvent(extend
);
1414 * Follow the arrow backward
1417 * true to extend selection range, false for single selection
1420 public void followArrowBwd(boolean extend
) {
1421 ITimeGraphEntry trace
= getSelectedTrace();
1422 if (trace
== null) {
1425 long selectedTime
= fTimeProvider
.getSelectionEnd();
1426 for (ILinkEvent link
: fItemData
.fLinks
) {
1427 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1428 selectItem(link
.getEntry(), false);
1429 if (link
.getDuration() != 0) {
1431 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1433 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1435 // Notify if visible time window has been adjusted
1436 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1438 fireSelectionChanged();
1439 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1443 selectPrevEvent(extend
);
1447 * Return the currently selected trace
1449 * @return The entry matching the trace
1451 public ITimeGraphEntry
getSelectedTrace() {
1452 ITimeGraphEntry trace
= null;
1453 int idx
= getSelectedIndex();
1455 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1461 * Retrieve the index of the currently selected item
1465 public int getSelectedIndex() {
1467 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1468 Item item
= fItemData
.fExpandedItems
[i
];
1469 if (item
.fSelected
) {
1477 boolean toggle(int idx
) {
1478 boolean toggled
= false;
1479 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1480 Item item
= fItemData
.fExpandedItems
[idx
];
1481 if (item
.fHasChildren
) {
1482 item
.fExpanded
= !item
.fExpanded
;
1483 fItemData
.updateExpandedItems();
1486 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1493 * Gets the index of the item at the given location.
1497 * @return the index of the item at the given location, of -1 if none.
1499 protected int getItemIndexAtY(int y
) {
1502 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1503 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1509 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1510 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1519 boolean isOverSplitLine(int x
) {
1520 if (x
< 0 || null == fTimeProvider
) {
1523 int nameWidth
= fTimeProvider
.getNameSpace();
1524 return Math
.abs(x
- nameWidth
) <= SNAP_WIDTH
;
1527 boolean isOverTimeSpace(int x
, int y
) {
1528 Point size
= getSize();
1529 return x
>= fTimeProvider
.getNameSpace() && x
< size
.x
&& y
>= 0 && y
< size
.y
;
1533 * Gets the {@link ITimeGraphEntry} at the given location.
1536 * a point in the widget
1537 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1541 public ITimeGraphEntry
getEntry(Point pt
) {
1542 int idx
= getItemIndexAtY(pt
.y
);
1543 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1547 * Return the arrow event closest to the given point that is no further than
1548 * a maximum distance.
1551 * a point in the widget
1552 * @return The closest arrow event, or null if there is none close enough.
1554 protected ILinkEvent
getArrow(Point pt
) {
1558 ILinkEvent linkEvent
= null;
1559 double minDistance
= Double
.MAX_VALUE
;
1560 for (ILinkEvent event
: fItemData
.fLinks
) {
1561 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1565 int x2
= x1
+ rect
.width
;
1566 int y2
= y1
+ rect
.height
;
1567 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1568 if (minDistance
> d
) {
1574 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1581 public int getXForTime(long time
) {
1582 if (null == fTimeProvider
) {
1585 long time0
= fTimeProvider
.getTime0();
1586 long time1
= fTimeProvider
.getTime1();
1587 int width
= getSize().x
;
1588 int nameSpace
= fTimeProvider
.getNameSpace();
1589 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1590 int x
= SaturatedArithmetic
.add(getBounds().x
+ nameSpace
, (int) ((time
- time0
) * pixelsPerNanoSec
));
1595 public long getTimeAtX(int coord
) {
1596 if (null == fTimeProvider
) {
1600 Point size
= getSize();
1601 long time0
= fTimeProvider
.getTime0();
1602 long time1
= fTimeProvider
.getTime1();
1603 int nameWidth
= fTimeProvider
.getNameSpace();
1604 final int x
= coord
- nameWidth
;
1605 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1606 if (x
>= 0 && size
.x
>= nameWidth
) {
1607 if (time1
- time0
> timeWidth
) {
1608 // nanosecond smaller than one pixel: use the first integer
1609 // nanosecond of this pixel's time range
1610 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1612 // nanosecond greater than one pixel: use the nanosecond that
1613 // covers this pixel start position
1614 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1620 void selectItem(int idx
, boolean addSelection
) {
1621 selectItem(idx
, addSelection
, true);
1624 void selectItem(int idx
, boolean addSelection
, boolean reveal
) {
1625 boolean changed
= false;
1627 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1628 Item item
= fItemData
.fExpandedItems
[idx
];
1629 changed
= !item
.fSelected
;
1630 item
.fSelected
= true;
1633 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1634 Item item
= fItemData
.fExpandedItems
[i
];
1635 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1638 item
.fSelected
= i
== idx
;
1642 changed
|= ensureVisibleItem(idx
, true);
1650 * Select an entry and make it visible
1653 * The entry to select
1654 * @param addSelection
1655 * <code>true</code> to add the entry to the current selection,
1656 * or <code>false</code> to set a new selection
1658 public void selectItem(ITimeGraphEntry entry
, boolean addSelection
) {
1659 int idx
= fItemData
.findItemIndex(entry
);
1660 selectItem(idx
, addSelection
);
1667 * The entry to select
1668 * @param addSelection
1669 * <code>true</code> to add the entry to the current selection,
1670 * or <code>false</code> to set a new selection
1672 * <code>true</code> if the selection is to be made visible, and
1673 * <code>false</code> otherwise
1676 public void selectItem(ITimeGraphEntry entry
, boolean addSelection
, boolean reveal
) {
1677 int idx
= fItemData
.findItemIndex(entry
);
1678 selectItem(idx
, addSelection
, reveal
);
1682 * Retrieve the number of entries shown per page.
1686 public int countPerPage() {
1687 int height
= getSize().y
;
1690 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1691 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1692 if (ySum
>= height
) {
1697 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1698 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1699 if (ySum
>= height
) {
1708 * Get the index of the top element
1712 public int getTopIndex() {
1717 * Get the number of expanded (visible) items
1719 * @return The count of expanded (visible) items
1721 public int getExpandedElementCount() {
1722 return fItemData
.fExpandedItems
.length
;
1726 * Get an array of all expanded (visible) elements
1728 * @return The expanded (visible) elements
1730 public ITimeGraphEntry
[] getExpandedElements() {
1731 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1732 for (Item item
: fItemData
.fExpandedItems
) {
1733 elements
.add(item
.fEntry
);
1735 return elements
.toArray(new ITimeGraphEntry
[0]);
1739 * Get the expanded (visible) element at the specified index.
1743 * @return The expanded (visible) element or null if out of range
1746 public ITimeGraphEntry
getExpandedElement(int index
) {
1747 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1750 return fItemData
.fExpandedItems
[index
].fEntry
;
1754 * Get the bounds of the specified entry relative to its parent time graph.
1757 * the time graph entry
1758 * @return the bounds of the entry, or null if the entry is not visible
1761 public Rectangle
getItemBounds(ITimeGraphEntry entry
) {
1762 int idx
= fItemData
.findItemIndex(entry
);
1764 return getItemRect(getClientArea(), idx
);
1769 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1770 Rectangle rect
= getItemRect(bounds
, idx
);
1771 rect
.width
= nameWidth
;
1775 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1776 Rectangle rect
= getItemRect(bounds
, idx
);
1777 rect
.x
+= nameWidth
;
1778 rect
.width
-= nameWidth
;
1782 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1784 if (idx
>= fTopIndex
) {
1785 for (int i
= fTopIndex
; i
< idx
; i
++) {
1786 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1789 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1790 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1793 int y
= bounds
.y
+ ySum
;
1794 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1795 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1799 void paint(Rectangle bounds
, PaintEvent e
) {
1802 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1806 fIdealNameSpace
= 0;
1807 int nameSpace
= fTimeProvider
.getNameSpace();
1809 // draw the background layer
1810 drawBackground(bounds
, nameSpace
, gc
);
1812 // draw the grid lines
1813 drawGridLines(bounds
, gc
);
1815 // draw the background markers
1816 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1819 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1821 // draw the foreground markers
1822 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1824 // draw the links (arrows)
1825 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1827 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1829 int alpha
= gc
.getAlpha();
1832 long time0
= fTimeProvider
.getTime0();
1833 long time1
= fTimeProvider
.getTime1();
1834 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1835 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1836 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1837 int x0
= SaturatedArithmetic
.add(bounds
.x
+ nameSpace
, (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
));
1838 int x1
= SaturatedArithmetic
.add(bounds
.x
+ nameSpace
, (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
));
1840 // draw selection lines
1841 if (fDragState
!= DRAG_SELECTION
) {
1842 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1843 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1844 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1847 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1848 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1853 // draw selection background
1854 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1855 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1856 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1857 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1859 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1860 } else if (x0
- x1
> 1) {
1861 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1865 // draw drag selection background
1866 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1867 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1868 if (fDragX0
< fDragX
) {
1869 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1870 } else if (fDragX0
> fDragX
) {
1871 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1876 if (DRAG_SPLIT_LINE
== fDragState
||
1877 (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0)) {
1878 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.DARK_GRAY
));
1880 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.GRAY
));
1882 gc
.fillRectangle(bounds
.x
+ nameSpace
- SNAP_WIDTH
, bounds
.y
, SNAP_WIDTH
, bounds
.height
);
1884 if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1885 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1886 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1887 if (fDragX
!= fDragX0
) {
1888 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1890 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1891 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1892 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1893 if (fDragX
!= fDragX0
) {
1894 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1902 * Draw the background layer. Fills the background of the control's name
1903 * space and states space, updates the background of items if necessary, and
1904 * draws the item's name text and middle line.
1907 * The bounds of the control
1909 * The name space width
1914 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1915 // draw empty name space background
1916 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1917 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1919 // draw empty states space background
1920 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1921 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1923 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1924 Rectangle itemRect
= getItemRect(bounds
, i
);
1925 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1928 Item item
= fItemData
.fExpandedItems
[i
];
1929 // draw the background of selected item and items with no time
1931 if (!item
.fEntry
.hasTimeEvents()) {
1932 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1933 gc
.fillRectangle(itemRect
);
1934 } else if (item
.fSelected
) {
1935 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1936 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1937 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1938 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1940 // draw the name space
1941 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1942 drawName(item
, nameRect
, gc
);
1943 if (item
.fEntry
.hasTimeEvents()) {
1944 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1945 drawMidLine(rect
, gc
);
1951 * Draw the grid lines
1954 * The bounds of the control
1959 public void drawGridLines(Rectangle bounds
, GC gc
) {
1960 if (!fGridLinesVisible
) {
1963 gc
.setForeground(fGridLineColor
);
1964 gc
.setAlpha(fGridLineColor
.getAlpha());
1965 for (int x
: fTimeGraphScale
.getTickList()) {
1966 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1975 * The rectangle of the area
1976 * @param timeProvider
1979 * The list of markers
1981 * true to draw the foreground markers, false otherwise
1983 * The width reserved for the names
1985 * Reference to the SWT GC object
1988 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1989 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1992 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1993 /* the list can grow concurrently but cannot shrink */
1994 for (int i
= 0; i
< markers
.size(); i
++) {
1995 IMarkerEvent marker
= markers
.get(i
);
1996 if (marker
.isForeground() == foreground
) {
1997 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
2000 gc
.setClipping((Rectangle
) null);
2004 * Draw a single marker
2009 * The bounds of the control
2010 * @param timeProvider
2013 * The width reserved for the name
2015 * Reference to the SWT GC object
2018 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2019 Rectangle rect
= Utils
.clone(bounds
);
2020 if (marker
.getEntry() != null) {
2021 int index
= fItemData
.findItemIndex(marker
.getEntry());
2025 rect
= getStatesRect(bounds
, index
, nameSpace
);
2026 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
2030 int x0
= getXForTime(marker
.getTime());
2031 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
2032 if (x0
> bounds
.width
|| x1
< nameSpace
) {
2035 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
2036 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
2038 Color color
= getColorScheme().getColor(marker
.getColor());
2039 gc
.setBackground(color
);
2040 gc
.setAlpha(color
.getAlpha());
2041 gc
.fillRectangle(rect
);
2043 String label
= marker
.getLabel();
2044 if (label
!= null && marker
.getEntry() != null) {
2045 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
2046 gc
.setForeground(color
);
2047 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
2052 * Draw many items at once
2055 * The bounds of the control
2056 * @param timeProvider
2059 * The array items to draw
2061 * The index of the first element to draw
2063 * The name space width
2067 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
2068 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
2069 for (int i
= topIndex
; i
< items
.length
; i
++) {
2070 Item item
= items
[i
];
2071 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
2081 * The bounds of the control
2082 * @param timeProvider
2085 * The expanded item index
2087 * The name space width
2091 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
2092 Rectangle itemRect
= getItemRect(bounds
, i
);
2093 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
2097 ITimeGraphEntry entry
= item
.fEntry
;
2098 long time0
= timeProvider
.getTime0();
2099 long time1
= timeProvider
.getTime1();
2100 long selectedTime
= fTimeProvider
.getSelectionEnd();
2102 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
2103 if (rect
.isEmpty() || (time1
<= time0
)) {
2104 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
2108 boolean selected
= item
.fSelected
;
2109 // K pixels per second
2110 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
2112 if (item
.fEntry
.hasTimeEvents()) {
2113 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2114 fillSpace(rect
, gc
, selected
);
2116 int margins
= getMarginForHeight(rect
.height
);
2117 int height
= rect
.height
- margins
;
2118 int topMargin
= (margins
+ 1) / 2;
2119 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
2121 /* Set the font for this item */
2122 setFontForHeight(height
, gc
);
2124 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
2125 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
2128 while (iterator
.hasNext()) {
2129 ITimeEvent event
= iterator
.next();
2130 int x
= SaturatedArithmetic
.add(rect
.x
, (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
));
2131 int xEnd
= SaturatedArithmetic
.add(rect
.x
, (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
));
2132 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
2133 // event is out of bounds
2136 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
2137 stateRect
.x
= Math
.max(rect
.x
, x
);
2138 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
2139 if (stateRect
.x
== lastX
) {
2140 stateRect
.width
-= 1;
2141 if (stateRect
.width
> 0) {
2142 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
2143 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
2147 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
2148 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
2149 lastX
= stateRect
.x
;
2152 gc
.setClipping((Rectangle
) null);
2154 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
2161 * The bounds of the control
2162 * @param timeProvider
2165 * The list of link events
2167 * The name space width
2171 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
2172 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
2176 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
2177 /* the list can grow concurrently but cannot shrink */
2178 for (int i
= 0; i
< links
.size(); i
++) {
2179 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
2181 gc
.setClipping((Rectangle
) null);
2185 * Draws a link type event
2188 * The link event to draw
2190 * The bounds of the control
2191 * @param timeProvider
2194 * The name space width
2198 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
2199 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
2202 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
2203 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
2204 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
2206 if ((srcIndex
== -1) || (destIndex
== -1)) {
2210 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
2211 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
2213 int x0
= getXForTime(event
.getTime());
2214 int x1
= getXForTime(event
.getTime() + event
.getDuration());
2216 // limit the x-coordinates to prevent integer overflow in calculations
2217 // and also GC.drawLine doesn't draw properly with large coordinates
2218 final int limit
= Integer
.MAX_VALUE
/ 1024;
2219 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
2220 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
2222 int y0
= src
.y
+ src
.height
/ 2;
2223 int y1
= dst
.y
+ dst
.height
/ 2;
2224 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
2233 * Time event for which we're drawing the arrow
2235 * The arrow rectangle
2238 * @return true if the arrow was drawn
2240 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
2241 Rectangle rect
, GC gc
) {
2246 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2250 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
2253 Color stateColor
= null;
2254 if (colorIdx
< fEventColorMap
.length
) {
2255 stateColor
= fEventColorMap
[colorIdx
];
2257 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2260 gc
.setForeground(stateColor
);
2261 gc
.setBackground(stateColor
);
2263 /* Draw the arrow */
2264 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
2265 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
2268 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2273 * @author Francis Giraldeau
2276 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2278 * The algorithm was taken from this site, not the code itself
2280 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
) {
2282 double cos
= 0.9510;
2283 double sin
= 0.3090;
2284 long lenx
= x1
- x0
;
2285 long leny
= y1
- y0
;
2286 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
2288 double dx
= factor
* lenx
/ len
;
2289 double dy
= factor
* leny
/ len
;
2290 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
2291 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
2292 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
2293 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
2294 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
2295 gc
.fillPolygon(arrow
);
2299 * Draw the name space of an item.
2304 * The bounds of the item's name space
2308 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2309 // No name space to be drawn
2310 if (fTimeProvider
.getNameSpace() == 0) {
2314 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2315 if (hasTimeEvents
) {
2316 gc
.setClipping(bounds
);
2319 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2320 setFontForHeight(height
, gc
);
2322 String name
= fLabelProvider
== null ? item
.fName
: fLabelProvider
.getColumnText(item
.fEntry
, 0);
2323 Rectangle rect
= Utils
.clone(bounds
);
2324 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2325 TreeColumn
[] columns
= fTree
.getColumns();
2326 int idealNameSpace
= 0;
2327 for (int i
= 0; i
< columns
.length
; i
++) {
2328 int columnIndex
= fTree
.getColumnOrder()[i
];
2329 TreeColumn column
= columns
[columnIndex
];
2330 rect
.width
= column
.getWidth();
2331 gc
.setClipping(rect
.x
, bounds
.y
, Math
.min(rect
.width
, bounds
.x
+ bounds
.width
- rect
.x
- SNAP_WIDTH
), bounds
.height
);
2334 // first visible column
2335 width
+= item
.fLevel
* EXPAND_SIZE
;
2336 if (item
.fHasChildren
) {
2337 // draw expand/collapse arrow
2338 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.DARK_GRAY
));
2339 int arrowHeightHint
= (height
< 4) ? height
: (height
< 6) ? height
- 1 : height
- 2;
2340 int arrowHalfHeight
= Math
.max(1, Math
.min(arrowHeightHint
, (int) Math
.round((EXPAND_SIZE
- 2) / ARROW_RATIO
))) / 2;
2341 int arrowHalfWidth
= (Math
.max(1, Math
.min(EXPAND_SIZE
- 2, (int) Math
.round(arrowHeightHint
* ARROW_RATIO
))) + 1) / 2;
2342 int x1
= bounds
.x
+ width
+ 1;
2343 int x2
= x1
+ 2 * arrowHalfWidth
;
2344 int midy
= bounds
.y
+ bounds
.height
/ 2;
2345 int y1
= midy
- arrowHalfHeight
;
2346 int y2
= midy
+ arrowHalfHeight
;
2347 if (!item
.fExpanded
) { // >
2348 gc
.fillPolygon(new int[] { x1
, y1
, x2
, midy
, x1
, y2
});
2350 int midx
= x1
+ arrowHalfWidth
;
2351 gc
.fillPolygon(new int[] { x1
, y1
, x2
, y1
, midx
, y2
});
2354 width
+= EXPAND_SIZE
+ MARGIN
;
2356 Image img
= fLabelProvider
!= null ? fLabelProvider
.getColumnImage(item
.fEntry
, columnIndex
)
2357 : columnIndex
== 0 ? fTimeGraphProvider
.getItemImage(item
.fEntry
) : null;
2360 int imgHeight
= img
.getImageData().height
;
2361 int imgWidth
= img
.getImageData().width
;
2362 int dstHeight
= Math
.min(bounds
.height
, imgHeight
);
2363 int dstWidth
= dstHeight
* imgWidth
/ imgHeight
;
2365 int y
= bounds
.y
+ (bounds
.height
- dstHeight
) / 2;
2366 gc
.drawImage(img
, 0, 0, imgWidth
, imgHeight
, x
, y
, dstWidth
, dstHeight
);
2367 width
+= imgWidth
+ MARGIN
;
2370 if (fLabelProvider
== null) {
2374 String label
= fLabelProvider
!= null ? fLabelProvider
.getColumnText(item
.fEntry
, columnIndex
)
2375 : columnIndex
== 0 ? item
.fName
: ""; //$NON-NLS-1$
2376 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2377 Rectangle textRect
= new Rectangle(rect
.x
+ width
, rect
.y
, rect
.width
- width
, rect
.height
);
2378 int textWidth
= Utils
.drawText(gc
, label
, textRect
, true);
2379 width
+= textWidth
+ MARGIN
;
2380 if (textWidth
> 0) {
2381 idealNameSpace
= rect
.x
+ width
;
2383 if (columns
.length
== 1) {
2384 drawMidLine(new Rectangle(bounds
.x
+ width
, bounds
.y
, bounds
.x
+ bounds
.width
, bounds
.height
), gc
);
2386 if (fAutoResizeColumns
&& width
> column
.getWidth()) {
2387 column
.setData(PREFERRED_WIDTH
, width
);
2388 column
.setWidth(width
);
2390 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2391 if (i
< columns
.length
- 1) {
2392 // not the last visible column: draw the vertical cell border
2393 int x
= rect
.x
+ rect
.width
- 1;
2394 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
2396 rect
.x
+= rect
.width
;
2398 fIdealNameSpace
= Math
.max(fIdealNameSpace
, idealNameSpace
);
2400 gc
.setClipping((Rectangle
) null);
2404 * Draw the state (color fill)
2409 * Time event for which we're drawing the state
2411 * The state rectangle
2415 * Is this time event currently selected (so it appears
2417 * @param timeSelected
2418 * Is the timestamp currently selected
2419 * @return true if the state was drawn
2421 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2422 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2424 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2425 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2428 boolean visible
= rect
.width
== 0 ?
false : true;
2429 rect
.width
= Math
.max(1, rect
.width
);
2430 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2431 gc
.setForeground(black
);
2432 Map
<String
, Object
> styleMap
= fTimeGraphProvider
.getEventStyle(event
);
2433 float heightFactor
= (float) styleMap
.getOrDefault(ITimeEventStyleStrings
.heightFactor(), 1.0f
);
2434 if (heightFactor
> 1.0 || heightFactor
< 0) {
2435 if (fFirstHeightWarning
) {
2436 TraceCompassLog
.getLogger(this.getClass()).warning("Heightfactor out of range : " + heightFactor
+ " for event " + event
.toString() + " - clamping results"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
2437 fFirstHeightWarning
= false;
2439 heightFactor
= Math
.max(0.0f
, Math
.min(1.0f
, heightFactor
));
2442 int height
= (int) (rect
.height
* heightFactor
);
2443 Rectangle drawRect
= new Rectangle(rect
.x
, rect
.y
+ ((rect
.height
- height
) / 2), rect
.width
, height
);
2445 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2447 // Only draw the top and bottom borders
2448 gc
.drawLine(drawRect
.x
, drawRect
.y
, drawRect
.x
+ drawRect
.width
- 1, drawRect
.y
);
2449 gc
.drawLine(drawRect
.x
, drawRect
.y
+ drawRect
.height
- 1, drawRect
.x
+ drawRect
.width
- 1, drawRect
.y
+ drawRect
.height
- 1);
2450 if (drawRect
.width
== 1) {
2451 gc
.drawPoint(drawRect
.x
, drawRect
.y
- 2);
2454 fTimeGraphProvider
.postDrawEvent(event
, drawRect
, gc
);
2457 Color stateColor
= null;
2458 if (colorIdx
< fEventColorMap
.length
) {
2459 stateColor
= fEventColorMap
[colorIdx
];
2464 boolean reallySelected
= timeSelected
&& selected
;
2465 // fill all rect area
2466 gc
.setBackground(stateColor
);
2468 int prevAlpha
= gc
.getAlpha();
2469 int alpha
= ((int) styleMap
.getOrDefault(ITimeEventStyleStrings
.fillColor(), 0xff)) & 0xff;
2471 gc
.fillRectangle(drawRect
);
2472 gc
.setAlpha(prevAlpha
);
2473 } else if (fBlendSubPixelEvents
) {
2475 gc
.fillRectangle(drawRect
);
2479 if (reallySelected
) {
2480 gc
.drawLine(drawRect
.x
, drawRect
.y
- 1, drawRect
.x
+ drawRect
.width
- 1, drawRect
.y
- 1);
2481 gc
.drawLine(drawRect
.x
, drawRect
.y
+ drawRect
.height
, drawRect
.x
+ drawRect
.width
- 1, drawRect
.y
+ drawRect
.height
);
2484 gc
.drawPoint(drawRect
.x
, drawRect
.y
- 2);
2486 fTimeGraphProvider
.postDrawEvent(event
, drawRect
, gc
);
2491 * Fill an item's states rectangle
2494 * The states rectangle
2498 * true if the item is selected
2500 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2501 /* Nothing to draw */
2505 * Draw a line at the middle height of a rectangle
2512 private void drawMidLine(Rectangle rect
, GC gc
) {
2513 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2514 int midy
= rect
.y
+ rect
.height
/ 2;
2515 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2518 private static int getMarginForHeight(int height
) {
2520 * State rectangle is smaller than the item bounds when height is > 4.
2521 * Don't use any margin if the height is below or equal that threshold.
2522 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2523 * pixels for the state height, but with a minimum margin of 1.
2525 final int MARGIN_THRESHOLD
= 4;
2526 final int PREFERRED_HEIGHT
= 13;
2527 final int MIN_MARGIN
= 1;
2528 final int MAX_MARGIN
= 6;
2529 return height
<= MARGIN_THRESHOLD ?
0 : Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2532 private void setFontForHeight(int pixels
, GC gc
) {
2533 /* convert font height from pixels to points */
2534 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2535 Font font
= fFonts
.get(height
);
2537 FontData fontData
= gc
.getFont().getFontData()[0];
2538 fontData
.setHeight(height
);
2539 font
= new Font(gc
.getDevice(), fontData
);
2540 fFonts
.put(height
, font
);
2546 public void keyTraversed(TraverseEvent e
) {
2547 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2553 public void keyPressed(KeyEvent e
) {
2555 if (fItemData
.fExpandedItems
.length
== 0) {
2558 if (SWT
.HOME
== e
.keyCode
) {
2560 } else if (SWT
.END
== e
.keyCode
) {
2561 idx
= fItemData
.fExpandedItems
.length
- 1;
2562 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2563 idx
= getSelectedIndex();
2566 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2569 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2570 idx
= getSelectedIndex();
2573 } else if (idx
> 0) {
2576 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2577 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2578 selectPrevEvent(extend
);
2579 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2580 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2581 selectNextEvent(extend
);
2582 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2583 int page
= countPerPage();
2584 idx
= getSelectedIndex();
2589 if (idx
>= fItemData
.fExpandedItems
.length
) {
2590 idx
= fItemData
.fExpandedItems
.length
- 1;
2592 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2593 int page
= countPerPage();
2594 idx
= getSelectedIndex();
2602 } else if (SWT
.CR
== e
.keyCode
) {
2603 idx
= getSelectedIndex();
2605 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2608 fireDefaultSelection();
2612 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2613 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2615 if (fVerticalZoomAlignEntry
!= null) {
2616 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2618 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2619 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2620 verticalZoom(false);
2621 if (fVerticalZoomAlignEntry
!= null) {
2622 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2624 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2625 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2626 resetVerticalZoom();
2627 if (fVerticalZoomAlignEntry
!= null) {
2628 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2630 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2631 if (fHasNamespaceFocus
) {
2632 ITimeGraphEntry entry
= getSelectedTrace();
2633 setExpandedState(entry
, 0, true);
2637 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2638 if (fHasNamespaceFocus
) {
2639 ITimeGraphEntry entry
= getSelectedTrace();
2640 if ((entry
!= null) && entry
.hasChildren()) {
2641 setExpandedState(entry
, -1, false);
2646 } else if ((e
.character
== '*') && ((e
.stateMask
& SWT
.CTRL
) == 0)) {
2647 if (fHasNamespaceFocus
) {
2648 ITimeGraphEntry entry
= getSelectedTrace();
2649 if ((entry
!= null) && entry
.hasChildren()) {
2650 setExpandedStateLevel(entry
);
2655 selectItem(idx
, false);
2656 fireSelectionChanged();
2658 int x
= toControl(e
.display
.getCursorLocation()).x
;
2659 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2663 public void keyReleased(KeyEvent e
) {
2664 int x
= toControl(e
.display
.getCursorLocation()).x
;
2665 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2669 public void focusGained(FocusEvent e
) {
2672 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2676 public void focusLost(FocusEvent e
) {
2678 if (DRAG_NONE
!= fDragState
) {
2680 fDragState
= DRAG_NONE
;
2683 updateStatusLine(NO_STATUS
);
2687 * @return If the current view is focused
2689 public boolean isInFocus() {
2694 * Provide the possibility to control the wait cursor externally e.g. data
2695 * requests in progress
2698 * Should we wait indefinitely?
2700 public void waitCursor(boolean waitInd
) {
2701 // Update cursor as indicated
2703 setCursor(fWaitCursor
);
2709 private void updateCursor(int x
, int stateMask
) {
2710 // if Wait cursor not active, check for the need to change the cursor
2711 if (getCursor() == fWaitCursor
) {
2714 Cursor cursor
= null;
2715 if (fDragState
== DRAG_SPLIT_LINE
) {
2716 } else if (fDragState
== DRAG_SELECTION
) {
2717 cursor
= fResizeCursor
;
2718 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2719 cursor
= fDragCursor
;
2720 } else if (fDragState
== DRAG_ZOOM
) {
2721 cursor
= fZoomCursor
;
2722 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2723 cursor
= fDragCursor
;
2724 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2725 cursor
= fResizeCursor
;
2726 } else if (!isOverSplitLine(x
)) {
2727 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2728 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2729 int xBegin
= getXForTime(selectionBegin
);
2730 int xEnd
= getXForTime(selectionEnd
);
2731 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2732 cursor
= fResizeCursor
;
2735 if (getCursor() != cursor
) {
2741 * Update the status line following a change of selection.
2745 public void updateStatusLine() {
2746 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2749 private void updateStatusLine(int x
) {
2750 // use the time provider of the time graph scale for the status line
2751 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2752 if (fStatusLineManager
== null || null == tdp
||
2753 tdp
.getTime0() == tdp
.getTime1()) {
2756 TimeFormat tf
= tdp
.getTimeFormat();
2757 Resolution res
= Resolution
.NANOSEC
;
2758 StringBuilder message
= new StringBuilder();
2759 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2760 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2761 long time
= getTimeAtX(x
);
2763 if (tdp
instanceof ITimeDataProviderConverter
) {
2764 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2766 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2768 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2769 Utils
.formatTime(time
, tf
, res
)
2773 long selectionBegin
= tdp
.getSelectionBegin();
2774 long selectionEnd
= tdp
.getSelectionEnd();
2775 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2777 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2778 Utils
.formatTime(selectionBegin
, tf
, res
)
2780 if (selectionBegin
!= selectionEnd
) {
2781 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2783 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2784 Utils
.formatTime(selectionEnd
, tf
, res
),
2785 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2788 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2789 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2790 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2791 if (tdp
instanceof ITimeDataProviderConverter
) {
2792 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2793 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2795 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2797 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2798 Utils
.formatTime(time0
, tf
, res
),
2799 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2800 Utils
.formatTime(time
, tf
, res
),
2801 Utils
.formatDelta(time
- time0
, tf
, res
)
2804 fStatusLineManager
.setMessage(message
.toString());
2808 public void mouseMove(MouseEvent e
) {
2809 if (null == fTimeProvider
) {
2812 Point size
= getSize();
2813 if (DRAG_TRACE_ITEM
== fDragState
) {
2814 int nameWidth
= fTimeProvider
.getNameSpace();
2815 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2817 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2818 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2819 long time1
= fTime1bak
- timeDelta
;
2820 long maxTime
= fTimeProvider
.getMaxTime();
2821 if (time1
> maxTime
) {
2824 long time0
= time1
- (fTime1bak
- fTime0bak
);
2825 if (time0
< fTimeProvider
.getMinTime()) {
2826 time0
= fTimeProvider
.getMinTime();
2827 time1
= time0
+ (fTime1bak
- fTime0bak
);
2829 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2831 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2833 fTimeProvider
.setNameSpace(e
.x
);
2834 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2835 } else if (DRAG_SELECTION
== fDragState
) {
2836 if (fDragBeginMarker
) {
2837 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2839 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2842 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2843 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2844 } else if (DRAG_ZOOM
== fDragState
) {
2845 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2847 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2848 } else if (DRAG_NONE
== fDragState
) {
2849 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2850 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2853 fMouseOverSplitLine
= mouseOverSplitLine
;
2856 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2857 fHasNamespaceFocus
= false;
2859 fHasNamespaceFocus
= true;
2861 updateCursor(e
.x
, e
.stateMask
);
2862 updateStatusLine(e
.x
);
2866 public void mouseDoubleClick(MouseEvent e
) {
2867 if (null == fTimeProvider
) {
2870 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2871 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2872 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2873 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2874 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2877 fMouseOverSplitLine
= mouseOverSplitLine
;
2878 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2881 int idx
= getItemIndexAtY(e
.y
);
2883 selectItem(idx
, false);
2884 fireDefaultSelection();
2890 public void mouseDown(MouseEvent e
) {
2891 if (fDragState
!= DRAG_NONE
) {
2894 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2895 int nameSpace
= fTimeProvider
.getNameSpace();
2896 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2897 fDragState
= DRAG_SPLIT_LINE
;
2898 fDragButton
= e
.button
;
2902 updateCursor(e
.x
, e
.stateMask
);
2906 if (fTimeProvider
== null ||
2907 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2908 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2912 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2913 int nameSpace
= fTimeProvider
.getNameSpace();
2914 idx
= getItemIndexAtY(e
.y
);
2916 Item item
= fItemData
.fExpandedItems
[idx
];
2917 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2921 selectItem(idx
, false);
2922 fireSelectionChanged();
2924 selectItem(idx
, false); // clear selection
2925 fireSelectionChanged();
2927 long hitTime
= getTimeAtX(e
.x
);
2931 fDragState
= DRAG_SELECTION
;
2932 fDragBeginMarker
= false;
2933 fDragButton
= e
.button
;
2936 fDragTime0
= getTimeAtX(fDragX0
);
2937 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2938 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2939 int xBegin
= getXForTime(selectionBegin
);
2940 int xEnd
= getXForTime(selectionEnd
);
2941 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2942 long time
= getTimeAtX(e
.x
);
2943 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2944 fDragBeginMarker
= true;
2947 fDragTime0
= selectionEnd
;
2950 fDragTime0
= selectionBegin
;
2953 long time
= getTimeAtX(e
.x
);
2954 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2955 fDragBeginMarker
= true;
2958 fDragTime0
= selectionEnd
;
2959 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2961 fDragTime0
= selectionBegin
;
2964 fTime0bak
= fTimeProvider
.getTime0();
2965 fTime1bak
= fTimeProvider
.getTime1();
2967 updateCursor(e
.x
, e
.stateMask
);
2968 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2970 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2971 long hitTime
= getTimeAtX(e
.x
);
2974 fDragState
= DRAG_TRACE_ITEM
;
2975 fDragButton
= e
.button
;
2978 fTime0bak
= fTimeProvider
.getTime0();
2979 fTime1bak
= fTimeProvider
.getTime1();
2980 updateCursor(e
.x
, e
.stateMask
);
2982 } else if (3 == e
.button
) {
2983 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2985 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2987 fDragTime0
= getTimeAtX(fDragX0
);
2988 fDragState
= DRAG_ZOOM
;
2989 fDragButton
= e
.button
;
2991 updateCursor(e
.x
, e
.stateMask
);
2992 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2994 idx
= getItemIndexAtY(e
.y
);
2995 selectItem(idx
, false);
2996 fireSelectionChanged();
3002 public void mouseUp(MouseEvent e
) {
3003 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
3004 if ((fDragState
== DRAG_ZOOM
) && isInDragZoomMargin()) {
3005 // Select entry and time event for single click
3006 long time
= getTimeAtX(fDragX0
);
3007 fTimeProvider
.setSelectionRangeNotify(time
, time
, false);
3008 int idx
= getItemIndexAtY(e
.y
);
3009 selectItem(idx
, false);
3010 fireSelectionChanged();
3012 menuDetected(fPendingMenuDetectEvent
);
3014 if (DRAG_NONE
!= fDragState
) {
3016 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
3017 fDragState
= DRAG_NONE
;
3018 if (fDragX
!= fDragX0
) {
3019 fTimeProvider
.notifyStartFinishTime();
3021 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
3022 fDragState
= DRAG_NONE
;
3024 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
3025 fDragState
= DRAG_NONE
;
3026 if (fDragX
== fDragX0
) { // click without selecting anything
3027 long time
= getTimeAtX(e
.x
);
3028 fTimeProvider
.setSelectedTimeNotify(time
, false);
3030 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
3031 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
3032 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
3035 fTimeGraphScale
.setDragRange(-1, -1);
3036 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
3037 fDragState
= DRAG_NONE
;
3038 int nameWidth
= fTimeProvider
.getNameSpace();
3039 if ((Math
.max(fDragX
, fDragX0
) > nameWidth
) && !isInDragZoomMargin()) {
3040 long time0
= getTimeAtX(fDragX0
);
3041 long time1
= getTimeAtX(fDragX
);
3042 if (time0
< time1
) {
3043 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
3045 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
3050 fTimeGraphScale
.setDragRange(-1, -1);
3053 updateCursor(e
.x
, e
.stateMask
);
3054 updateStatusLine(e
.x
);
3058 public void mouseEnter(MouseEvent e
) {
3062 public void mouseExit(MouseEvent e
) {
3063 if (fMouseOverSplitLine
) {
3064 fMouseOverSplitLine
= false;
3067 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
3071 public void mouseHover(MouseEvent e
) {
3075 public void mouseScrolled(MouseEvent e
) {
3076 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
3081 * On some platforms the mouse scroll event is sent to the control that
3082 * has focus even if it is not under the cursor. Handle the event only
3083 * if over the time graph control.
3085 Point size
= getSize();
3086 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
3087 if (!bounds
.contains(e
.x
, e
.y
)) {
3091 boolean horizontalZoom
= false;
3092 boolean horizontalScroll
= false;
3093 boolean verticalZoom
= false;
3094 boolean verticalScroll
= false;
3096 // over the time graph control
3097 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
3098 verticalZoom
= true;
3099 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
3100 // over the name space
3101 verticalScroll
= true;
3103 // over the state area
3104 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
3105 // over the state area, CTRL pressed
3106 horizontalZoom
= true;
3107 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
3108 // over the state area, SHIFT pressed
3109 horizontalScroll
= true;
3111 // over the state area, no modifier pressed
3112 verticalScroll
= true;
3116 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
3117 verticalZoom(e
.count
> 0);
3118 if (fVerticalZoomAlignEntry
!= null) {
3119 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
3121 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
3123 } else if (horizontalScroll
) {
3124 horizontalScroll(e
.count
> 0);
3125 } else if (verticalScroll
) {
3126 setTopIndex(getTopIndex() - e
.count
);
3131 * Get the vertical zoom alignment entry and position based on the current
3132 * selection. If there is no selection or if the selection is not visible,
3133 * return an alignment entry with a null time graph entry.
3135 * @return a map entry where the key is the selection's time graph entry and
3136 * the value is the center y-coordinate of that entry, or null
3138 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
3139 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
3140 if (alignEntry
!= null) {
3143 int index
= getSelectedIndex();
3144 if (index
== -1 || index
>= getExpandedElementCount()) {
3145 return new SimpleEntry
<>(null, 0);
3147 Rectangle bounds
= getClientArea();
3148 Rectangle itemRect
= getItemRect(bounds
, index
);
3149 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
3150 /* selection is not visible */
3151 return new SimpleEntry
<>(null, 0);
3153 ITimeGraphEntry entry
= getExpandedElement(index
);
3154 int y
= itemRect
.y
+ itemRect
.height
/ 2;
3155 return new SimpleEntry
<>(entry
, y
);
3159 * Get the vertical zoom alignment entry and position at the specified
3163 * the cursor y-coordinate
3164 * @return a map entry where the key is the time graph entry under the
3165 * cursor and the value is the cursor y-coordinate
3167 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
3168 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
3169 if (alignEntry
!= null) {
3172 int index
= getItemIndexAtY(y
);
3174 index
= getExpandedElementCount() - 1;
3176 ITimeGraphEntry entry
= getExpandedElement(index
);
3177 return new SimpleEntry
<>(entry
, y
);
3181 * Get the vertical zoom alignment entry and position if there is an ongoing
3182 * one and we are within the vertical zoom delay, or otherwise return null.
3184 * @return a map entry where the key is a time graph entry and the value is
3185 * a y-coordinate, or null
3187 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
3188 long currentTimeMillis
= System
.currentTimeMillis();
3189 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
3191 * If the vertical zoom is triggered repeatedly in a short amount of
3192 * time, use the initial event's entry and position.
3194 fVerticalZoomAlignTime
= currentTimeMillis
;
3195 return fVerticalZoomAlignEntry
;
3197 fVerticalZoomAlignTime
= currentTimeMillis
;
3202 public void handleEvent(Event event
) {
3203 if (event
.type
== SWT
.MouseWheel
) {
3204 // prevent horizontal scrolling when the mouse wheel is used to
3205 // scroll vertically or zoom
3211 public int getBorderWidth() {
3212 return fBorderWidth
;
3216 * Set the border width
3218 * @param borderWidth
3221 public void setBorderWidth(int borderWidth
) {
3222 this.fBorderWidth
= borderWidth
;
3226 * @return The current height of the header row
3228 public int getHeaderHeight() {
3229 return fHeaderHeight
;
3233 * Set the height of the header row
3235 * @param headerHeight
3238 public void setHeaderHeight(int headerHeight
) {
3239 this.fHeaderHeight
= headerHeight
;
3243 * @return The default height of regular item rows
3245 public int getItemHeight() {
3246 return fGlobalItemHeight
;
3250 * Set the default height of regular item rows.
3255 public void setItemHeight(int rowHeight
) {
3256 this.fGlobalItemHeight
= rowHeight
;
3257 for (Item item
: fItemData
.fItems
) {
3258 item
.fItemHeight
= rowHeight
;
3263 * Set the height of a specific item. Overrides the default item height.
3266 * A time graph entry
3269 * @return true if the height is successfully stored, false otherwise
3271 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
3272 Item item
= fItemData
.findItem(entry
);
3274 item
.fItemHeight
= rowHeight
;
3281 * Set the minimum item width
3286 public void setMinimumItemWidth(int width
) {
3287 this.fMinimumItemWidth
= width
;
3291 * @return The minimum item width
3293 public int getMinimumItemWidth() {
3294 return fMinimumItemWidth
;
3298 * Set whether all time events with a duration shorter than one pixel should
3299 * be blended in. If false, only the first such time event will be drawn and
3300 * the subsequent time events in the same pixel will be discarded. The
3301 * default value is false.
3304 * true if sub-pixel events should be blended, false otherwise.
3307 public void setBlendSubPixelEvents(boolean blend
) {
3308 fBlendSubPixelEvents
= blend
;
3312 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
3313 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
3314 fSelectionChangedListeners
.add(listener
);
3319 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
3320 if (listener
!= null) {
3321 fSelectionChangedListeners
.remove(listener
);
3326 public void setSelection(ISelection selection
) {
3327 if (selection
instanceof IStructuredSelection
) {
3328 Object ob
= ((IStructuredSelection
) selection
).getFirstElement();
3329 if (ob
instanceof ITimeGraphEntry
) {
3330 selectItem((ITimeGraphEntry
) ob
, false);
3338 * The filter object to be attached to the view
3340 public void addFilter(@NonNull ViewerFilter filter
) {
3341 if (!fFilters
.contains(filter
)) {
3342 fFilters
.add(filter
);
3348 * The filter object to be attached to the view
3350 public void removeFilter(@NonNull ViewerFilter filter
) {
3351 fFilters
.remove(filter
);
3355 * Returns this control's filters.
3357 * @return an array of viewer filters
3360 public @NonNull ViewerFilter
[] getFilters() {
3361 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3365 * Sets the filters, replacing any previous filters.
3368 * an array of viewer filters, or null
3371 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3373 if (filters
!= null) {
3374 fFilters
.addAll(Arrays
.asList(filters
));
3379 public void colorSettingsChanged(StateItem
[] stateItems
) {
3380 /* Destroy previous colors from the resource manager */
3381 if (fEventColorMap
!= null) {
3382 for (Color color
: fEventColorMap
) {
3383 fResourceManager
.destroyColor(color
.getRGB());
3386 if (stateItems
!= null) {
3387 fEventColorMap
= new Color
[stateItems
.length
];
3388 for (int i
= 0; i
< stateItems
.length
; i
++) {
3389 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3392 fEventColorMap
= new Color
[] {};
3397 private class ItemData
{
3398 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3399 private Item
[] fExpandedItems
= new Item
[0];
3400 private Item
[] fItems
= new Item
[0];
3401 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3402 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3407 public Item
findItem(ITimeGraphEntry entry
) {
3408 return fItemMap
.get(entry
);
3411 public int findItemIndex(ITimeGraphEntry entry
) {
3412 Item item
= fItemMap
.get(entry
);
3416 return item
.fExpandedIndex
;
3419 public void refreshData() {
3420 ITimeGraphEntry selection
= getSelectedTrace();
3421 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3423 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3424 ITimeGraphEntry entry
= fRootEntries
[i
];
3425 refreshData(itemMap
, null, 0, entry
);
3428 fItems
= fItemMap
.values().toArray(new Item
[0]);
3429 updateExpandedItems();
3430 if (selection
!= null) {
3431 for (Item item
: fExpandedItems
) {
3432 if (item
.fEntry
== selection
) {
3433 item
.fSelected
= true;
3440 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3441 Item item
= new Item(entry
, entry
.getName(), level
);
3442 if (parent
!= null) {
3443 parent
.fChildren
.add(item
);
3445 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3446 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3448 item
.fItemHeight
= fGlobalItemHeight
;
3450 fMaxItemHeight
= Math
.max(fMaxItemHeight
, item
.fItemHeight
);
3451 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3452 itemMap
.put(entry
, item
);
3453 if (entry
.hasChildren()) {
3454 Item oldItem
= fItemMap
.get(entry
);
3455 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3456 /* existing items keep their old expanded state */
3457 item
.fExpanded
= oldItem
.fExpanded
;
3460 * new items set the expanded state according to auto-expand
3463 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3465 item
.fHasChildren
= true;
3466 for (ITimeGraphEntry child
: entry
.getChildren()) {
3467 refreshData(itemMap
, item
, level
+ 1, child
);
3472 public void updateExpandedItems() {
3473 for (Item item
: fItems
) {
3474 item
.fExpandedIndex
= -1;
3476 List
<Item
> expandedItemList
= new ArrayList
<>();
3477 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3478 ITimeGraphEntry entry
= fRootEntries
[i
];
3479 Item item
= findItem(entry
);
3480 refreshExpanded(expandedItemList
, item
);
3482 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3483 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3486 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3487 // Check for filters
3488 boolean display
= true;
3489 for (ViewerFilter filter
: fFilters
) {
3490 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3496 item
.fExpandedIndex
= expandedItemList
.size();
3497 expandedItemList
.add(item
);
3498 if (item
.fHasChildren
&& item
.fExpanded
) {
3499 for (Item child
: item
.fChildren
) {
3500 refreshExpanded(expandedItemList
, child
);
3506 public void refreshData(ITimeGraphEntry
[] entries
) {
3507 if (entries
== null) {
3508 fRootEntries
= null;
3510 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3516 public void refreshArrows(List
<ILinkEvent
> events
) {
3517 /* If links are null, reset the list */
3518 if (events
!= null) {
3521 fLinks
= new ArrayList
<>();
3525 public ITimeGraphEntry
[] getEntries() {
3526 return fRootEntries
;
3530 private class Item
{
3531 private boolean fExpanded
;
3532 private int fExpandedIndex
;
3533 private boolean fSelected
;
3534 private boolean fHasChildren
;
3535 private int fItemHeight
;
3536 private final int fLevel
;
3537 private final List
<Item
> fChildren
;
3538 private final String fName
;
3539 private final ITimeGraphEntry fEntry
;
3541 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3542 this.fEntry
= entry
;
3544 this.fLevel
= level
;
3545 this.fChildren
= new ArrayList
<>();
3549 public String
toString() {
3555 public void menuDetected(MenuDetectEvent e
) {
3556 if (null == fTimeProvider
) {
3560 * This flag indicates if menu was prevented from being shown below and
3561 * therefore must be made visible on callback from mouseUp().
3563 boolean pendingEventCallback
= fPendingMenuDetectEvent
!= null;
3564 Point p
= toControl(e
.x
, e
.y
);
3565 if (e
.detail
== SWT
.MENU_MOUSE
&& isOverTimeSpace(p
.x
, p
.y
)) {
3566 if (fPendingMenuDetectEvent
== null) {
3568 * Feature in Linux. The MenuDetectEvent is received before
3569 * mouseDown. Store the event and trigger it later just before
3570 * handling mouseUp. This allows for the method to detect if
3571 * mouse is used to drag zoom.
3573 fPendingMenuDetectEvent
= e
;
3575 * Prevent the platform to show the menu when returning. The
3576 * menu will be shown (see below) when this method is called
3577 * again during mouseUp().
3582 fPendingMenuDetectEvent
= null;
3583 if (fDragState
!= DRAG_ZOOM
|| !isInDragZoomMargin()) {
3585 * Don't show the menu on mouseUp() if a drag zoom is in
3586 * progress with a drag range outside of the drag zoom margin,
3587 * or if any other drag operation, or none, is in progress.
3593 if (fDragState
!= DRAG_NONE
) {
3595 * Don't show the menu on keyboard menu or mouse menu outside of
3596 * the time space if any drag operation is in progress.
3602 int idx
= getItemIndexAtY(p
.y
);
3603 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3604 Item item
= fItemData
.fExpandedItems
[idx
];
3605 ITimeGraphEntry entry
= item
.fEntry
;
3607 /* Send menu event for the time graph entry */
3610 fireMenuEventOnTimeGraphEntry(e
);
3611 Menu menu
= getMenu();
3612 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3613 menu
.setVisible(true);
3616 /* Send menu event for time event */
3617 if (entry
.hasTimeEvents()) {
3618 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3619 if (event
!= null) {
3622 fireMenuEventOnTimeEvent(e
);
3624 if (pendingEventCallback
&& e
.doit
&& (menu
!= null)) {
3625 menu
.setVisible(true);
3633 * Perform the alignment operation.
3636 * the alignment offset
3638 * @see ITmfTimeAligned
3642 public void performAlign(int offset
) {
3643 fTimeProvider
.setNameSpace(offset
);
3647 * Return the time alignment information
3649 * @return the time alignment information
3651 * @see ITmfTimeAligned
3655 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3656 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());
3659 private boolean isInDragZoomMargin() {
3660 return (Math
.abs(fDragX
- fDragX0
) < DRAG_MARGIN
);