1 /*****************************************************************************
2 * Copyright (c) 2007, 2015 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
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Collections
;
26 import java
.util
.Iterator
;
27 import java
.util
.LinkedHashMap
;
28 import java
.util
.List
;
31 import org
.eclipse
.jdt
.annotation
.NonNull
;
32 import org
.eclipse
.jface
.action
.IStatusLineManager
;
33 import org
.eclipse
.jface
.resource
.JFaceResources
;
34 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
35 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
36 import org
.eclipse
.jface
.viewers
.ISelection
;
37 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
38 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
39 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
40 import org
.eclipse
.osgi
.util
.NLS
;
41 import org
.eclipse
.swt
.SWT
;
42 import org
.eclipse
.swt
.events
.FocusEvent
;
43 import org
.eclipse
.swt
.events
.FocusListener
;
44 import org
.eclipse
.swt
.events
.KeyEvent
;
45 import org
.eclipse
.swt
.events
.KeyListener
;
46 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
47 import org
.eclipse
.swt
.events
.MenuDetectListener
;
48 import org
.eclipse
.swt
.events
.MouseEvent
;
49 import org
.eclipse
.swt
.events
.MouseListener
;
50 import org
.eclipse
.swt
.events
.MouseMoveListener
;
51 import org
.eclipse
.swt
.events
.MouseTrackListener
;
52 import org
.eclipse
.swt
.events
.MouseWheelListener
;
53 import org
.eclipse
.swt
.events
.PaintEvent
;
54 import org
.eclipse
.swt
.events
.SelectionListener
;
55 import org
.eclipse
.swt
.events
.TraverseEvent
;
56 import org
.eclipse
.swt
.events
.TraverseListener
;
57 import org
.eclipse
.swt
.events
.TypedEvent
;
58 import org
.eclipse
.swt
.graphics
.Color
;
59 import org
.eclipse
.swt
.graphics
.Cursor
;
60 import org
.eclipse
.swt
.graphics
.Font
;
61 import org
.eclipse
.swt
.graphics
.FontData
;
62 import org
.eclipse
.swt
.graphics
.GC
;
63 import org
.eclipse
.swt
.graphics
.Image
;
64 import org
.eclipse
.swt
.graphics
.Point
;
65 import org
.eclipse
.swt
.graphics
.Rectangle
;
66 import org
.eclipse
.swt
.widgets
.Composite
;
67 import org
.eclipse
.swt
.widgets
.Display
;
68 import org
.eclipse
.swt
.widgets
.Event
;
69 import org
.eclipse
.swt
.widgets
.Listener
;
70 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
89 import com
.google
.common
.collect
.Iterables
;
92 * Time graph control implementation
94 * @author Alvaro Sanchez-Leon
95 * @author Patrick Tasse
97 public class TimeGraphControl
extends TimeGraphBaseControl
98 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
99 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
100 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
102 /** Constant indicating that all levels of the time graph should be expanded */
103 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
105 private static final int DRAG_NONE
= 0;
106 private static final int DRAG_TRACE_ITEM
= 1;
107 private static final int DRAG_SPLIT_LINE
= 2;
108 private static final int DRAG_ZOOM
= 3;
109 private static final int DRAG_SELECTION
= 4;
111 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
113 private static final double ZOOM_FACTOR
= 1.5;
114 private static final double ZOOM_IN_FACTOR
= 0.8;
115 private static final double ZOOM_OUT_FACTOR
= 1.25;
117 private static final int SNAP_WIDTH
= 2;
118 private static final int ARROW_HOVER_MAX_DIST
= 5;
120 private static final int NO_STATUS
= -1;
121 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
123 private static final int MAX_LABEL_LENGTH
= 256;
125 /** Resource manager */
126 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
128 /** Color map for event types */
129 private Color
[] fEventColorMap
= null;
131 private ITimeDataProvider fTimeProvider
;
132 private IStatusLineManager fStatusLineManager
= null;
133 private TimeGraphScale fTimeGraphScale
= null;
135 private boolean fIsInFocus
= false;
136 private boolean fMouseOverSplitLine
= false;
137 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
138 private int fHeightAdjustment
= 0;
139 private int fInitialFontHeight
;
141 private boolean fBlendSubPixelEvents
= false;
142 private int fMinimumItemWidth
= 0;
143 private int fTopIndex
= 0;
144 private int fDragState
= DRAG_NONE
;
145 private boolean fDragBeginMarker
= false;
146 private int fDragButton
;
147 private int fDragX0
= 0;
148 private int fDragX
= 0;
149 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
150 private int fIdealNameSpace
= 0;
151 private long fTime0bak
;
152 private long fTime1bak
;
153 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
154 private ItemData fItemData
= null;
155 private List
<IMarkerEvent
> fMarkers
= null;
156 private boolean fMarkersVisible
= true;
157 private List
<SelectionListener
> fSelectionListeners
;
158 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
159 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
160 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
161 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
162 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
163 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
164 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
165 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
166 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
167 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
168 private MenuDetectEvent fPendingMenuDetectEvent
= null;
169 private boolean fGridLinesVisible
= true;
170 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
171 private boolean fHideArrows
= false;
172 private int fAutoExpandLevel
= ALL_LEVELS
;
174 private int fBorderWidth
= 0;
175 private int fHeaderHeight
= 0;
178 * Standard constructor
181 * The parent composite object
183 * The color scheme to use
185 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
187 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
189 fItemData
= new ItemData();
191 addFocusListener(this);
192 addMouseListener(this);
193 addMouseMoveListener(this);
194 addMouseTrackListener(this);
195 addMouseWheelListener(this);
196 addTraverseListener(this);
197 addKeyListener(this);
198 addMenuDetectListener(this);
199 addListener(SWT
.MouseWheel
, this);
203 public void dispose() {
205 fResourceManager
.dispose();
212 * Sets the timegraph provider used by this timegraph viewer.
214 * @param timeGraphProvider the timegraph provider
216 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
217 fTimeGraphProvider
= timeGraphProvider
;
219 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
220 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
221 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
224 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
225 colorSettingsChanged(stateItems
);
229 * Gets the timegraph provider used by this timegraph viewer.
231 * @return the timegraph provider, or <code>null</code> if not set.
233 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
234 return fTimeGraphProvider
;
238 * Gets the color map used by this timegraph viewer.
240 * @return a color map, or <code>null</code> if not set.
242 public Color
[] getEventColorMap() {
243 return fEventColorMap
;
247 * Assign the given time provider
249 * @param timeProvider
252 public void setTimeProvider(ITimeDataProvider timeProvider
) {
253 fTimeProvider
= timeProvider
;
258 * Assign the status line manager
260 * @param statusLineManager
261 * The status line manager, or null to disable status line messages
263 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
264 if (fStatusLineManager
!= null && statusLineManager
== null) {
265 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
267 fStatusLineManager
= statusLineManager
;
271 * Assign the time graph scale
273 * @param timeGraphScale
274 * The time graph scale
276 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
277 fTimeGraphScale
= timeGraphScale
;
281 * Add a selection listener
284 * The listener to add
286 public void addSelectionListener(SelectionListener listener
) {
287 if (listener
== null) {
288 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
290 if (null == fSelectionListeners
) {
291 fSelectionListeners
= new ArrayList
<>();
293 fSelectionListeners
.add(listener
);
297 * Remove a selection listener
300 * The listener to remove
302 public void removeSelectionListener(SelectionListener listener
) {
303 if (null != fSelectionListeners
) {
304 fSelectionListeners
.remove(listener
);
309 * Selection changed callback
311 public void fireSelectionChanged() {
312 if (null != fSelectionListeners
) {
313 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
314 while (it
.hasNext()) {
315 SelectionListener listener
= it
.next();
316 listener
.widgetSelected(null);
322 * Default selection callback
324 public void fireDefaultSelection() {
325 if (null != fSelectionListeners
) {
326 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
327 while (it
.hasNext()) {
328 SelectionListener listener
= it
.next();
329 listener
.widgetDefaultSelected(null);
335 * Add a drag selection listener
338 * The listener to add
340 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
341 if (listener
== null) {
342 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
344 if (null == fDragSelectionListeners
) {
345 fDragSelectionListeners
= new ArrayList
<>();
347 fDragSelectionListeners
.add(listener
);
351 * Remove a drag selection listener
354 * The listener to remove
356 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
357 if (null != fDragSelectionListeners
) {
358 fDragSelectionListeners
.remove(listener
);
363 * Drag Selection changed callback
366 * Time interval start
370 public void fireDragSelectionChanged(long start
, long end
) {
371 // check for backward intervals
372 long beginTime
, endTime
;
380 // call the listeners
381 if (null != fDragSelectionListeners
) {
382 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
383 while (it
.hasNext()) {
384 ITimeGraphTimeListener listener
= it
.next();
385 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
391 * Get the traces in the model
393 * @return The array of traces
395 public ITimeGraphEntry
[] getTraces() {
396 return fItemData
.getEntries();
400 * Refresh the data for the thing
402 public void refreshData() {
403 fItemData
.refreshData();
408 * Refresh data for the given traces
411 * The traces to refresh
413 public void refreshData(ITimeGraphEntry
[] traces
) {
414 fItemData
.refreshData(traces
);
419 * Refresh the links (arrows) of this widget
421 * @param events The link event list
423 public void refreshArrows(List
<ILinkEvent
> events
) {
424 fItemData
.refreshArrows(events
);
428 * Get the links (arrows) of this widget
430 * @return The unmodifiable link event list
434 public List
<ILinkEvent
> getArrows() {
435 return Collections
.unmodifiableList(fItemData
.fLinks
);
438 boolean ensureVisibleItem(int idx
, boolean redraw
) {
439 boolean changed
= false;
442 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
443 if (fItemData
.fExpandedItems
[index
].fSelected
) {
448 if (index
>= fItemData
.fExpandedItems
.length
) {
451 if (index
< fTopIndex
) {
458 int page
= countPerPage();
459 if (index
>= fTopIndex
+ page
) {
460 setTopIndex(index
- page
+ 1);
471 * Assign the given index as the top one
476 public void setTopIndex(int idx
) {
477 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
478 index
= Math
.max(0, index
);
484 * Sets the auto-expand level to be used for new entries discovered when
485 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
486 * . The value 0 means that there is no auto-expand; 1 means that top-level
487 * entries are expanded, but not their children; 2 means that top-level
488 * entries are expanded, and their children, but not grand-children; and so
491 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
495 * non-negative level, or <code>ALL_LEVELS</code> to expand all
498 public void setAutoExpandLevel(int level
) {
499 fAutoExpandLevel
= level
;
503 * Returns the auto-expand level.
505 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
506 * the tree are expanded automatically
507 * @see #setAutoExpandLevel
509 public int getAutoExpandLevel() {
510 return fAutoExpandLevel
;
514 * Get the expanded state of a given entry.
518 * @return true if the entry is expanded, false if collapsed
521 public boolean getExpandedState(ITimeGraphEntry entry
) {
522 Item item
= fItemData
.fItemMap
.get(entry
);
523 return (item
!= null ? item
.fExpanded
: false);
527 * Set the expanded state of a given entry
532 * True if expanded, false if collapsed
534 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
535 Item item
= fItemData
.findItem(entry
);
536 if (item
!= null && item
.fExpanded
!= expanded
) {
537 item
.fExpanded
= expanded
;
538 fItemData
.updateExpandedItems();
544 * Collapses all nodes of the viewer's tree, starting with the root.
546 public void collapseAll() {
547 for (Item item
: fItemData
.fItems
) {
548 item
.fExpanded
= false;
550 fItemData
.updateExpandedItems();
555 * Expands all nodes of the viewer's tree, starting with the root.
557 public void expandAll() {
558 for (Item item
: fItemData
.fItems
) {
559 item
.fExpanded
= true;
561 fItemData
.updateExpandedItems();
566 * Add a tree listener
569 * The listener to add
571 public void addTreeListener(ITimeGraphTreeListener listener
) {
572 if (!fTreeListeners
.contains(listener
)) {
573 fTreeListeners
.add(listener
);
578 * Remove a tree listener
581 * The listener to remove
583 public void removeTreeListener(ITimeGraphTreeListener listener
) {
584 if (fTreeListeners
.contains(listener
)) {
585 fTreeListeners
.remove(listener
);
590 * Tree event callback
595 * The expanded state (true for expanded, false for collapsed)
597 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
598 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
599 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
601 listener
.treeExpanded(event
);
603 listener
.treeCollapsed(event
);
609 * Add a menu listener on {@link ITimeGraphEntry}s
612 * The listener to add
614 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
615 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
616 fTimeGraphEntryMenuListeners
.add(listener
);
621 * Remove a menu listener on {@link ITimeGraphEntry}s
624 * The listener to remove
626 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
627 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
628 fTimeGraphEntryMenuListeners
.remove(listener
);
633 * Menu event callback on {@link ITimeGraphEntry}s
636 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
638 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
639 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
640 listener
.menuDetected(event
);
645 * Add a menu listener on {@link ITimeEvent}s
648 * The listener to add
650 public void addTimeEventMenuListener(MenuDetectListener listener
) {
651 if (!fTimeEventMenuListeners
.contains(listener
)) {
652 fTimeEventMenuListeners
.add(listener
);
657 * Remove a menu listener on {@link ITimeEvent}s
660 * The listener to remove
662 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
663 if (fTimeEventMenuListeners
.contains(listener
)) {
664 fTimeEventMenuListeners
.remove(listener
);
669 * Menu event callback on {@link ITimeEvent}s
672 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
674 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
675 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
676 listener
.menuDetected(event
);
681 public ISelection
getSelection() {
682 TimeGraphSelection sel
= new TimeGraphSelection();
683 ITimeGraphEntry trace
= getSelectedTrace();
684 if (null != trace
&& null != fTimeProvider
) {
685 long selectedTime
= fTimeProvider
.getSelectionBegin();
686 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
697 * Get the selection object
699 * @return The selection
701 public ISelection
getSelectionTrace() {
702 TimeGraphSelection sel
= new TimeGraphSelection();
703 ITimeGraphEntry trace
= getSelectedTrace();
711 * Enable/disable one of the traces in the model
714 * 1 to enable it, -1 to disable. The method returns immediately
715 * if another value is used.
717 public void selectTrace(int n
) {
718 if ((n
!= 1) && (n
!= -1)) {
722 boolean changed
= false;
723 int lastSelection
= -1;
724 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
725 Item item
= fItemData
.fExpandedItems
[i
];
726 if (item
.fSelected
) {
728 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
729 item
.fSelected
= false;
730 item
= fItemData
.fExpandedItems
[i
+ 1];
731 item
.fSelected
= true;
733 } else if ((-1 == n
) && (i
> 0)) {
734 item
.fSelected
= false;
735 item
= fItemData
.fExpandedItems
[i
- 1];
736 item
.fSelected
= true;
743 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
744 Item item
= fItemData
.fExpandedItems
[0];
745 item
.fSelected
= true;
750 ensureVisibleItem(-1, false);
752 fireSelectionChanged();
760 * 1 for next event, -1 for previous event
762 * true to extend selection range, false for single selection
765 public void selectEvent(int n
, boolean extend
) {
766 if (null == fTimeProvider
) {
769 ITimeGraphEntry trace
= getSelectedTrace();
773 long selectedTime
= fTimeProvider
.getSelectionEnd();
774 long endTime
= fTimeProvider
.getMaxTime();
775 ITimeEvent nextEvent
;
776 if (n
== -1 && selectedTime
> endTime
) {
777 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
779 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
781 if (null == nextEvent
&& n
== -1) {
782 nextEvent
= Utils
.getFirstEvent(trace
);
784 if (null != nextEvent
) {
785 long nextTime
= nextEvent
.getTime();
786 // If last event detected e.g. going back or not moving to a next
788 if (nextTime
<= selectedTime
&& n
== 1) {
789 // Select to the end of this last event
790 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
791 // but not beyond the end of the trace
792 if (nextTime
> endTime
) {
795 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
796 // for previous event go to its end time unless we were already there
797 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
800 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
802 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
804 fireSelectionChanged();
807 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
809 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
811 fireSelectionChanged();
813 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
817 * Select the next event
820 * true to extend selection range, false for single selection
823 public void selectNextEvent(boolean extend
) {
824 selectEvent(1, extend
);
825 // Notify if visible time window has been adjusted
826 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
830 * Select the previous event
833 * true to extend selection range, false for single selection
836 public void selectPrevEvent(boolean extend
) {
837 selectEvent(-1, extend
);
838 // Notify if visible time window has been adjusted
839 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
843 * Select the next trace
845 public void selectNextTrace() {
850 * Select the previous trace
852 public void selectPrevTrace() {
857 * Scroll left or right by one half window size
860 * true to scroll left, false to scroll right
862 public void horizontalScroll(boolean left
) {
863 long time0
= fTimeProvider
.getTime0();
864 long time1
= fTimeProvider
.getTime1();
865 long timeMin
= fTimeProvider
.getMinTime();
866 long timeMax
= fTimeProvider
.getMaxTime();
867 long range
= time1
- time0
;
871 long increment
= Math
.max(1, range
/ 2);
873 time0
= Math
.max(time0
- increment
, timeMin
);
874 time1
= time0
+ range
;
876 time1
= Math
.min(time1
+ increment
, timeMax
);
877 time0
= time1
- range
;
879 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
883 * Zoom based on mouse cursor location with mouse scrolling
885 * @param zoomIn true to zoom in, false to zoom out
887 public void zoom(boolean zoomIn
) {
888 int globalX
= getDisplay().getCursorLocation().x
;
889 Point p
= toControl(globalX
, 0);
890 int nameSpace
= fTimeProvider
.getNameSpace();
891 int timeSpace
= fTimeProvider
.getTimeSpace();
892 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
893 long time0
= fTimeProvider
.getTime0();
894 long time1
= fTimeProvider
.getTime1();
895 long interval
= time1
- time0
;
898 } // to allow getting out of single point interval
901 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
903 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
905 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
906 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
907 long newTime1
= newTime0
+ newInterval
;
908 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
912 * zoom in using single click
914 public void zoomIn() {
915 long prevTime0
= fTimeProvider
.getTime0();
916 long prevTime1
= fTimeProvider
.getTime1();
917 long prevRange
= prevTime1
- prevTime0
;
918 if (prevRange
== 0) {
921 ITimeDataProvider provider
= fTimeProvider
;
922 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
923 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
924 selTime
= (prevTime0
+ prevTime1
) / 2;
926 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
927 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
929 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
931 if (inaccuracy
> 0 && inaccuracy
< 100) {
932 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
936 long min
= fTimeProvider
.getMinTimeInterval();
937 if ((time1
- time0
) < min
) {
938 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
942 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
946 * zoom out using single click
948 public void zoomOut() {
949 long prevTime0
= fTimeProvider
.getTime0();
950 long prevTime1
= fTimeProvider
.getTime1();
951 ITimeDataProvider provider
= fTimeProvider
;
952 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
953 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
954 selTime
= (prevTime0
+ prevTime1
) / 2;
956 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
957 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
959 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
960 if (inaccuracy
> 0 && inaccuracy
< 100) {
961 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
965 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
972 * true to zoom in, false to zoom out
974 * true to adjust item heights, false to adjust font only
977 public void verticalZoom(boolean zoomIn
, boolean adjustItems
) {
983 FontData fontData
= getFont().getFontData()[0];
984 if (fInitialFontHeight
== 0) {
985 fInitialFontHeight
= fontData
.getHeight();
987 int height
= Math
.max(1, fInitialFontHeight
+ fHeightAdjustment
);
988 fontData
.setHeight(height
);
992 fFont
= new Font(getDisplay(), fontData
);
995 fItemData
.refreshData();
1001 * Reset the vertical zoom to default.
1003 * @param adjustItems
1004 * true to reset item heights, false to reset font only
1007 public void resetVerticalZoom(boolean adjustItems
) {
1008 fHeightAdjustment
= 0;
1009 if (fFont
!= null) {
1015 fItemData
.refreshData();
1021 * Set the grid lines visibility. The default is true.
1024 * true to show the grid lines, false otherwise
1027 public void setGridLinesVisible(boolean visible
) {
1028 fGridLinesVisible
= visible
;
1032 * Get the grid lines visibility.
1034 * @return true if the grid lines are visible, false otherwise
1037 public boolean getGridLinesVisible() {
1038 return fGridLinesVisible
;
1042 * Set the grid line color. The default is SWT.COLOR_GRAY.
1045 * the grid line color
1048 public void setGridLineColor(Color color
) {
1049 fGridLineColor
= color
;
1053 * Get the grid line color.
1055 * @return the grid line color
1058 public Color
getGridLineColor() {
1059 return fGridLineColor
;
1063 * Set the markers list.
1066 * The markers list, or null
1069 public void setMarkers(List
<IMarkerEvent
> markers
) {
1071 fTimeGraphScale
.setMarkers(markers
);
1075 * Get the markers list.
1077 * @return The markers list, or null
1080 public List
<IMarkerEvent
> getMarkers() {
1085 * Set the markers visibility. The default is true.
1088 * true to show the markers, false otherwise
1091 public void setMarkersVisible(boolean visible
) {
1092 fMarkersVisible
= visible
;
1093 fTimeGraphScale
.setMarkersVisible(visible
);
1097 * Get the markers visibility.
1099 * @return true if the markers are visible, false otherwise
1102 public boolean getMarkersVisible() {
1103 return fMarkersVisible
;
1109 * @param hideArrows true to hide arrows
1111 public void hideArrows(boolean hideArrows
) {
1112 fHideArrows
= hideArrows
;
1116 * Follow the arrow forward
1119 * true to extend selection range, false for single selection
1122 public void followArrowFwd(boolean extend
) {
1123 ITimeGraphEntry trace
= getSelectedTrace();
1124 if (trace
== null) {
1127 long selectedTime
= fTimeProvider
.getSelectionEnd();
1128 for (ILinkEvent link
: fItemData
.fLinks
) {
1129 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1130 selectItem(link
.getDestinationEntry(), false);
1131 if (link
.getDuration() != 0) {
1133 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1135 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1137 // Notify if visible time window has been adjusted
1138 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1140 fireSelectionChanged();
1141 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1145 selectNextEvent(extend
);
1149 * Follow the arrow backward
1152 * true to extend selection range, false for single selection
1155 public void followArrowBwd(boolean extend
) {
1156 ITimeGraphEntry trace
= getSelectedTrace();
1157 if (trace
== null) {
1160 long selectedTime
= fTimeProvider
.getSelectionEnd();
1161 for (ILinkEvent link
: fItemData
.fLinks
) {
1162 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1163 selectItem(link
.getEntry(), false);
1164 if (link
.getDuration() != 0) {
1166 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1168 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1170 // Notify if visible time window has been adjusted
1171 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1173 fireSelectionChanged();
1174 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1178 selectPrevEvent(extend
);
1182 * Return the currently selected trace
1184 * @return The entry matching the trace
1186 public ITimeGraphEntry
getSelectedTrace() {
1187 ITimeGraphEntry trace
= null;
1188 int idx
= getSelectedIndex();
1190 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1196 * Retrieve the index of the currently selected item
1200 public int getSelectedIndex() {
1202 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1203 Item item
= fItemData
.fExpandedItems
[i
];
1204 if (item
.fSelected
) {
1212 boolean toggle(int idx
) {
1213 boolean toggled
= false;
1214 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1215 Item item
= fItemData
.fExpandedItems
[idx
];
1216 if (item
.fHasChildren
) {
1217 item
.fExpanded
= !item
.fExpanded
;
1218 fItemData
.updateExpandedItems();
1221 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1228 * Gets the index of the item at the given location.
1232 * @return the index of the item at the given location, of -1 if none.
1234 protected int getItemIndexAtY(int y
) {
1239 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1240 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1248 boolean isOverSplitLine(int x
) {
1249 if (x
< 0 || null == fTimeProvider
) {
1252 int nameWidth
= fTimeProvider
.getNameSpace();
1253 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1257 * Gets the {@link ITimeGraphEntry} at the given location.
1260 * a point in the widget
1261 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1264 protected ITimeGraphEntry
getEntry(Point pt
) {
1265 int idx
= getItemIndexAtY(pt
.y
);
1266 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1270 * Return the arrow event closest to the given point that is no further than
1271 * a maximum distance.
1274 * a point in the widget
1275 * @return The closest arrow event, or null if there is none close enough.
1277 protected ILinkEvent
getArrow(Point pt
) {
1281 ILinkEvent linkEvent
= null;
1282 double minDistance
= Double
.MAX_VALUE
;
1283 for (ILinkEvent event
: fItemData
.fLinks
) {
1284 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1288 int x2
= x1
+ rect
.width
;
1289 int y2
= y1
+ rect
.height
;
1290 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1291 if (minDistance
> d
) {
1297 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1304 public int getXForTime(long time
) {
1305 if (null == fTimeProvider
) {
1308 long time0
= fTimeProvider
.getTime0();
1309 long time1
= fTimeProvider
.getTime1();
1310 int width
= getSize().x
;
1311 int nameSpace
= fTimeProvider
.getNameSpace();
1312 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1313 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1318 public long getTimeAtX(int coord
) {
1319 if (null == fTimeProvider
) {
1323 Point size
= getSize();
1324 long time0
= fTimeProvider
.getTime0();
1325 long time1
= fTimeProvider
.getTime1();
1326 int nameWidth
= fTimeProvider
.getNameSpace();
1327 final int x
= coord
- nameWidth
;
1328 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1329 if (x
>= 0 && size
.x
>= nameWidth
) {
1330 if (time1
- time0
> timeWidth
) {
1331 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1332 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1334 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1335 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1341 void selectItem(int idx
, boolean addSelection
) {
1342 boolean changed
= false;
1344 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1345 Item item
= fItemData
.fExpandedItems
[idx
];
1346 changed
= !item
.fSelected
;
1347 item
.fSelected
= true;
1350 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1351 Item item
= fItemData
.fExpandedItems
[i
];
1352 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1355 item
.fSelected
= i
== idx
;
1358 changed
|= ensureVisibleItem(idx
, true);
1365 * Callback for item selection
1368 * The entry matching the trace
1369 * @param addSelection
1370 * If the selection is added or removed
1372 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1373 int idx
= fItemData
.findItemIndex(trace
);
1374 selectItem(idx
, addSelection
);
1378 * Retrieve the number of entries shown per page.
1382 public int countPerPage() {
1383 int height
= getSize().y
;
1386 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1387 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1388 if (ySum
>= height
) {
1393 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1394 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1395 if (ySum
>= height
) {
1404 * Get the index of the top element
1408 public int getTopIndex() {
1413 * Get the number of expanded (visible) items
1415 * @return The count of expanded (visible) items
1417 public int getExpandedElementCount() {
1418 return fItemData
.fExpandedItems
.length
;
1422 * Get an array of all expanded (visible) elements
1424 * @return The expanded (visible) elements
1426 public ITimeGraphEntry
[] getExpandedElements() {
1427 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1428 for (Item item
: fItemData
.fExpandedItems
) {
1429 elements
.add(item
.fEntry
);
1431 return elements
.toArray(new ITimeGraphEntry
[0]);
1434 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1435 Rectangle rect
= getItemRect(bounds
, idx
);
1436 rect
.width
= nameWidth
;
1440 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1441 Rectangle rect
= getItemRect(bounds
, idx
);
1442 rect
.x
+= nameWidth
;
1443 rect
.width
-= nameWidth
;
1447 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1449 if (idx
>= fTopIndex
) {
1450 for (int i
= fTopIndex
; i
< idx
; i
++) {
1451 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1454 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1455 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1458 int y
= bounds
.y
+ ySum
;
1459 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1460 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1464 void paint(Rectangle bounds
, PaintEvent e
) {
1467 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1471 fIdealNameSpace
= 0;
1472 int nameSpace
= fTimeProvider
.getNameSpace();
1474 // draw the background layer
1475 drawBackground(bounds
, nameSpace
, gc
);
1477 // draw the grid lines
1478 drawGridLines(bounds
, gc
);
1480 // draw the background markers
1481 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1484 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1486 // draw the foreground markers
1487 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1489 // draw the links (arrows)
1490 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1492 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1494 int alpha
= gc
.getAlpha();
1497 long time0
= fTimeProvider
.getTime0();
1498 long time1
= fTimeProvider
.getTime1();
1499 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1500 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1501 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1502 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1503 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1505 // draw selection lines
1506 if (fDragState
!= DRAG_SELECTION
) {
1507 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1508 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1509 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1512 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1513 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1518 // draw selection background
1519 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1520 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1521 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1522 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1524 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1525 } else if (x0
- x1
> 1) {
1526 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1530 // draw drag selection background
1531 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1532 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1533 if (fDragX0
< fDragX
) {
1534 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1535 } else if (fDragX0
> fDragX
) {
1536 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1541 if (DRAG_SPLIT_LINE
== fDragState
) {
1542 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1543 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1544 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1545 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1546 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1547 if (fDragX
!= fDragX0
) {
1548 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1550 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1551 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1552 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1553 if (fDragX
!= fDragX0
) {
1554 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1556 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1557 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1558 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1565 * Draw the background layer. Fills the background of the control's name
1566 * space and states space, updates the background of items if necessary,
1567 * and draws the item's name text and middle line.
1570 * The bounds of the control
1572 * The name space width
1577 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1578 // draw empty name space background
1579 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1580 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1582 // draw empty states space background
1583 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1584 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1586 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1587 Rectangle itemRect
= getItemRect(bounds
, i
);
1588 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1591 Item item
= fItemData
.fExpandedItems
[i
];
1592 // draw the background of selected item and items with no time events
1593 if (! item
.fEntry
.hasTimeEvents()) {
1594 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1595 gc
.fillRectangle(itemRect
);
1596 } else if (item
.fSelected
) {
1597 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1598 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1599 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1600 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1602 // draw the name and middle line
1603 if (! item
.fEntry
.hasTimeEvents()) {
1604 drawName(item
, itemRect
, gc
);
1606 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1607 drawName(item
, nameRect
, gc
);
1608 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1609 drawMidLine(rect
, gc
);
1615 * Draw the grid lines
1618 * The bounds of the control
1623 public void drawGridLines(Rectangle bounds
, GC gc
) {
1624 if (!fGridLinesVisible
) {
1627 gc
.setForeground(fGridLineColor
);
1628 gc
.setAlpha(fGridLineColor
.getAlpha());
1629 for (int x
: fTimeGraphScale
.getTickList()) {
1630 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1639 * The rectangle of the area
1640 * @param timeProvider
1643 * The list of markers
1645 * true to draw the foreground markers, false otherwise
1647 * The width reserved for the names
1649 * Reference to the SWT GC object
1652 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1653 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1656 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1657 /* the list can grow concurrently but cannot shrink */
1658 for (int i
= 0; i
< markers
.size(); i
++) {
1659 IMarkerEvent marker
= markers
.get(i
);
1660 if (marker
.isForeground() == foreground
) {
1661 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1664 gc
.setClipping((Rectangle
) null);
1668 * Draw a single marker
1673 * The bounds of the control
1674 * @param timeProvider
1677 * The width reserved for the name
1679 * Reference to the SWT GC object
1682 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1683 Rectangle rect
= Utils
.clone(bounds
);
1684 if (marker
.getEntry() != null) {
1685 int index
= fItemData
.findItemIndex(marker
.getEntry());
1689 rect
= getStatesRect(bounds
, index
, nameSpace
);
1690 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1694 int x0
= getXForTime(marker
.getTime());
1695 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1696 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1699 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1700 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1702 gc
.setBackground(marker
.getColor());
1703 gc
.setAlpha(marker
.getColor().getAlpha());
1704 gc
.fillRectangle(rect
);
1706 String label
= marker
.getLabel();
1707 if (label
!= null && marker
.getEntry() != null) {
1708 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1709 gc
.setForeground(marker
.getColor());
1710 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1715 * Draw many items at once
1718 * The bounds of the control
1719 * @param timeProvider
1722 * The array items to draw
1724 * The index of the first element to draw
1726 * The name space width
1730 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1731 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1732 for (int i
= topIndex
; i
< items
.length
; i
++) {
1733 Item item
= items
[i
];
1734 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1744 * The bounds of the control
1745 * @param timeProvider
1748 * The expanded item index
1750 * The name space width
1754 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1755 Rectangle itemRect
= getItemRect(bounds
, i
);
1756 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1760 ITimeGraphEntry entry
= item
.fEntry
;
1761 long time0
= timeProvider
.getTime0();
1762 long time1
= timeProvider
.getTime1();
1763 long selectedTime
= fTimeProvider
.getSelectionEnd();
1765 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1766 if (rect
.isEmpty() || (time1
<= time0
)) {
1767 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1771 boolean selected
= item
.fSelected
;
1772 // K pixels per second
1773 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1775 if (item
.fEntry
.hasTimeEvents()) {
1776 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1777 fillSpace(rect
, gc
, selected
);
1779 * State rectangle is smaller than item bounds. Use a margin height
1780 * of 3 pixels, keep at least 3 pixels for the state, but not more
1781 * than the item height. Favor the top margin for the remainder.
1783 int height
= Math
.min(rect
.height
, Math
.max(3, rect
.height
- 6));
1784 int margin
= (rect
.height
- height
+ 1) / 2;
1785 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ margin
, rect
.width
, height
);
1787 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1788 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1791 while (iterator
.hasNext()) {
1792 ITimeEvent event
= iterator
.next();
1793 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1794 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1795 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1796 // event is out of bounds
1799 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1800 stateRect
.x
= Math
.max(rect
.x
, x
);
1801 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1802 if (stateRect
.x
== lastX
) {
1803 stateRect
.width
-= 1;
1804 if (stateRect
.width
> 0) {
1805 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1806 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1810 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1811 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1812 lastX
= stateRect
.x
;
1815 gc
.setClipping((Rectangle
) null);
1817 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1824 * The bounds of the control
1825 * @param timeProvider
1828 * The list of link events
1830 * The name space width
1834 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1835 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1839 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1840 /* the list can grow concurrently but cannot shrink */
1841 for (int i
= 0; i
< links
.size(); i
++) {
1842 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1844 gc
.setClipping((Rectangle
) null);
1848 * Draws a link type event
1851 * The link event to draw
1853 * The bounds of the control
1854 * @param timeProvider
1857 * The name space width
1861 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1862 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1865 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1866 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1867 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1869 if ((srcIndex
== -1) || (destIndex
== -1)) {
1873 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1874 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1876 int x0
= getXForTime(event
.getTime());
1877 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1879 // limit the x-coordinates to prevent integer overflow in calculations
1880 // and also GC.drawLine doesn't draw properly with large coordinates
1881 final int limit
= Integer
.MAX_VALUE
/ 1024;
1882 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1883 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1885 int y0
= src
.y
+ src
.height
/ 2;
1886 int y1
= dst
.y
+ dst
.height
/ 2;
1887 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1896 * Time event for which we're drawing the arrow
1898 * The arrow rectangle
1901 * @return true if the arrow was drawn
1903 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1904 Rectangle rect
, GC gc
) {
1909 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1913 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1916 Color stateColor
= null;
1917 if (colorIdx
< fEventColorMap
.length
) {
1918 stateColor
= fEventColorMap
[colorIdx
];
1920 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1923 gc
.setForeground(stateColor
);
1924 gc
.setBackground(stateColor
);
1926 /* Draw the arrow */
1927 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1928 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1931 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1936 * @author Francis Giraldeau
1939 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1941 * The algorithm was taken from this site, not the code itself
1943 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1946 double cos
= 0.9510;
1947 double sin
= 0.3090;
1948 long lenx
= x1
- x0
;
1949 long leny
= y1
- y0
;
1950 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1952 double dx
= factor
* lenx
/ len
;
1953 double dy
= factor
* leny
/ len
;
1954 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1955 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1956 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1957 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1958 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1959 gc
.fillPolygon(arrow
);
1963 * Draw the name of an item.
1968 * The bounds of the item's name space
1972 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1973 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1975 // No name to be drawn
1976 if (fTimeProvider
.getNameSpace() == 0) {
1980 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1981 if (item
.fHasChildren
) {
1982 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1983 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1984 Rectangle rect
= Utils
.clone(bounds
);
1985 rect
.x
+= leftMargin
;
1986 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1987 rect
.width
= EXPAND_SIZE
;
1988 rect
.height
= EXPAND_SIZE
;
1989 gc
.fillRectangle(rect
);
1990 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1991 int midy
= rect
.y
+ rect
.height
/ 2;
1992 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1993 if (!item
.fExpanded
) {
1994 int midx
= rect
.x
+ rect
.width
/ 2;
1995 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1998 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2000 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2003 int imgHeight
= img
.getImageData().height
;
2004 int imgWidth
= img
.getImageData().width
;
2006 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2007 gc
.drawImage(img
, x
, y
);
2008 leftMargin
+= imgWidth
+ MARGIN
;
2010 String name
= item
.fName
;
2011 Point size
= gc
.stringExtent(name
);
2012 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2013 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2015 if (hasTimeEvents
) {
2016 // cut long string with "..."
2017 int width
= bounds
.width
- leftMargin
;
2019 while (size
.x
> width
&& name
.length() > 1) {
2021 name
= name
.substring(0, name
.length() - 1);
2022 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2025 name
+= "..."; //$NON-NLS-1$
2028 Rectangle rect
= Utils
.clone(bounds
);
2029 rect
.x
+= leftMargin
;
2030 rect
.width
-= leftMargin
;
2032 if (rect
.width
> 0) {
2033 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2034 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2035 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2036 leftMargin
+= textWidth
+ MARGIN
;
2038 if (hasTimeEvents
) {
2040 rect
.x
= bounds
.x
+ leftMargin
;
2042 rect
.width
= bounds
.width
- rect
.x
;
2043 drawMidLine(rect
, gc
);
2049 * Draw the state (color fill)
2054 * Time event for which we're drawing the state
2056 * The state rectangle
2060 * Is this time event currently selected (so it appears
2062 * @param timeSelected
2063 * Is the timestamp currently selected
2064 * @return true if the state was drawn
2066 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2067 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2069 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2070 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2073 boolean visible
= rect
.width
== 0 ?
false : true;
2074 rect
.width
= Math
.max(1, rect
.width
);
2075 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2076 gc
.setForeground(black
);
2078 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2080 // Only draw the top and bottom borders
2081 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2082 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2083 if (rect
.width
== 1) {
2084 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2087 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2090 Color stateColor
= null;
2091 if (colorIdx
< fEventColorMap
.length
) {
2092 stateColor
= fEventColorMap
[colorIdx
];
2097 boolean reallySelected
= timeSelected
&& selected
;
2098 // fill all rect area
2099 gc
.setBackground(stateColor
);
2101 gc
.fillRectangle(rect
);
2102 } else if (fBlendSubPixelEvents
) {
2104 gc
.fillRectangle(rect
);
2108 if (reallySelected
) {
2109 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2110 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2113 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2115 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2120 * Fill an item's states rectangle
2123 * The states rectangle
2127 * true if the item is selected
2129 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2130 /* Nothing to draw */
2134 * Draw a line at the middle height of a rectangle
2141 private void drawMidLine(Rectangle rect
, GC gc
) {
2142 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2143 int midy
= rect
.y
+ rect
.height
/ 2;
2144 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2148 public void keyTraversed(TraverseEvent e
) {
2149 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2155 public void keyPressed(KeyEvent e
) {
2157 if (fItemData
.fExpandedItems
.length
== 0) {
2160 if (SWT
.HOME
== e
.keyCode
) {
2162 } else if (SWT
.END
== e
.keyCode
) {
2163 idx
= fItemData
.fExpandedItems
.length
- 1;
2164 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2165 idx
= getSelectedIndex();
2168 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2171 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2172 idx
= getSelectedIndex();
2175 } else if (idx
> 0) {
2178 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2179 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2180 selectPrevEvent(extend
);
2181 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2182 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2183 selectNextEvent(extend
);
2184 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2185 int page
= countPerPage();
2186 idx
= getSelectedIndex();
2191 if (idx
>= fItemData
.fExpandedItems
.length
) {
2192 idx
= fItemData
.fExpandedItems
.length
- 1;
2194 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2195 int page
= countPerPage();
2196 idx
= getSelectedIndex();
2204 } else if (SWT
.CR
== e
.keyCode
) {
2205 idx
= getSelectedIndex();
2207 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2210 fireDefaultSelection();
2214 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2215 verticalZoom(true, true);
2216 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2217 verticalZoom(false, true);
2218 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2219 resetVerticalZoom(true);
2222 selectItem(idx
, false);
2223 fireSelectionChanged();
2225 int x
= toControl(e
.display
.getCursorLocation()).x
;
2226 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2230 public void keyReleased(KeyEvent e
) {
2231 int x
= toControl(e
.display
.getCursorLocation()).x
;
2232 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2236 public void focusGained(FocusEvent e
) {
2239 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2243 public void focusLost(FocusEvent e
) {
2245 if (DRAG_NONE
!= fDragState
) {
2247 fDragState
= DRAG_NONE
;
2250 updateStatusLine(NO_STATUS
);
2254 * @return If the current view is focused
2256 public boolean isInFocus() {
2261 * Provide the possibility to control the wait cursor externally e.g. data
2262 * requests in progress
2264 * @param waitInd Should we wait indefinitely?
2266 public void waitCursor(boolean waitInd
) {
2267 // Update cursor as indicated
2269 setCursor(fWaitCursor
);
2275 private void updateCursor(int x
, int stateMask
) {
2276 // if Wait cursor not active, check for the need to change the cursor
2277 if (getCursor() == fWaitCursor
) {
2280 Cursor cursor
= null;
2281 if (fDragState
== DRAG_SPLIT_LINE
) {
2282 } else if (fDragState
== DRAG_SELECTION
) {
2283 cursor
= fResizeCursor
;
2284 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2285 cursor
= fDragCursor
;
2286 } else if (fDragState
== DRAG_ZOOM
) {
2287 cursor
= fZoomCursor
;
2288 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2289 cursor
= fDragCursor
;
2290 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2291 cursor
= fResizeCursor
;
2292 } else if (!isOverSplitLine(x
)) {
2293 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2294 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2295 int xBegin
= getXForTime(selectionBegin
);
2296 int xEnd
= getXForTime(selectionEnd
);
2297 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2298 cursor
= fResizeCursor
;
2301 if (getCursor() != cursor
) {
2307 * Update the status line following a change of selection.
2311 public void updateStatusLine() {
2312 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2315 private void updateStatusLine(int x
) {
2316 // use the time provider of the time graph scale for the status line
2317 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2318 if (fStatusLineManager
== null || null == tdp
||
2319 tdp
.getTime0() == tdp
.getTime1()) {
2322 TimeFormat tf
= tdp
.getTimeFormat();
2323 Resolution res
= Resolution
.NANOSEC
;
2324 StringBuilder message
= new StringBuilder();
2325 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2326 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2327 long time
= getTimeAtX(x
);
2329 if (tdp
instanceof ITimeDataProviderConverter
) {
2330 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2332 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2334 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2335 Utils
.formatTime(time
, tf
, res
)
2339 long selectionBegin
= tdp
.getSelectionBegin();
2340 long selectionEnd
= tdp
.getSelectionEnd();
2341 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2343 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2344 Utils
.formatTime(selectionBegin
, tf
, res
)
2346 if (selectionBegin
!= selectionEnd
) {
2347 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2349 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2350 Utils
.formatTime(selectionEnd
, tf
, res
),
2351 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2354 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2355 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2356 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2357 if (tdp
instanceof ITimeDataProviderConverter
) {
2358 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2359 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2361 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2363 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2364 Utils
.formatTime(time0
, tf
, res
),
2365 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2366 Utils
.formatTime(time
, tf
, res
),
2367 Utils
.formatDelta(time
- time0
, tf
, res
)
2370 fStatusLineManager
.setMessage(message
.toString());
2374 public void mouseMove(MouseEvent e
) {
2375 if (null == fTimeProvider
) {
2378 Point size
= getSize();
2379 if (DRAG_TRACE_ITEM
== fDragState
) {
2380 int nameWidth
= fTimeProvider
.getNameSpace();
2381 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2383 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2384 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2385 long time1
= fTime1bak
- timeDelta
;
2386 long maxTime
= fTimeProvider
.getMaxTime();
2387 if (time1
> maxTime
) {
2390 long time0
= time1
- (fTime1bak
- fTime0bak
);
2391 if (time0
< fTimeProvider
.getMinTime()) {
2392 time0
= fTimeProvider
.getMinTime();
2393 time1
= time0
+ (fTime1bak
- fTime0bak
);
2395 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2397 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2399 fTimeProvider
.setNameSpace(e
.x
);
2400 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2401 } else if (DRAG_SELECTION
== fDragState
) {
2402 if (fDragBeginMarker
) {
2403 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2405 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2408 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2409 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2410 } else if (DRAG_ZOOM
== fDragState
) {
2411 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2413 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2414 } else if (DRAG_NONE
== fDragState
) {
2415 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2416 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2419 fMouseOverSplitLine
= mouseOverSplitLine
;
2421 updateCursor(e
.x
, e
.stateMask
);
2422 updateStatusLine(e
.x
);
2426 public void mouseDoubleClick(MouseEvent e
) {
2427 if (null == fTimeProvider
) {
2430 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2431 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2432 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2433 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2434 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2437 fMouseOverSplitLine
= mouseOverSplitLine
;
2438 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2441 int idx
= getItemIndexAtY(e
.y
);
2443 selectItem(idx
, false);
2444 fireDefaultSelection();
2450 public void mouseDown(MouseEvent e
) {
2451 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2452 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2453 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2457 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2458 int nameSpace
= fTimeProvider
.getNameSpace();
2459 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2460 fDragState
= DRAG_SPLIT_LINE
;
2461 fDragButton
= e
.button
;
2464 fTime0bak
= fTimeProvider
.getTime0();
2465 fTime1bak
= fTimeProvider
.getTime1();
2467 updateCursor(e
.x
, e
.stateMask
);
2471 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2472 int nameSpace
= fTimeProvider
.getNameSpace();
2473 idx
= getItemIndexAtY(e
.y
);
2475 Item item
= fItemData
.fExpandedItems
[idx
];
2476 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2480 selectItem(idx
, false);
2481 fireSelectionChanged();
2483 selectItem(idx
, false); // clear selection
2484 fireSelectionChanged();
2486 long hitTime
= getTimeAtX(e
.x
);
2490 fDragState
= DRAG_SELECTION
;
2491 fDragBeginMarker
= false;
2492 fDragButton
= e
.button
;
2495 fDragTime0
= getTimeAtX(fDragX0
);
2496 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2497 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2498 int xBegin
= getXForTime(selectionBegin
);
2499 int xEnd
= getXForTime(selectionEnd
);
2500 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2501 long time
= getTimeAtX(e
.x
);
2502 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2503 fDragBeginMarker
= true;
2506 fDragTime0
= selectionEnd
;
2509 fDragTime0
= selectionBegin
;
2512 long time
= getTimeAtX(e
.x
);
2513 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2514 fDragBeginMarker
= true;
2517 fDragTime0
= selectionEnd
;
2518 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2520 fDragTime0
= selectionBegin
;
2523 fTime0bak
= fTimeProvider
.getTime0();
2524 fTime1bak
= fTimeProvider
.getTime1();
2526 updateCursor(e
.x
, e
.stateMask
);
2527 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2529 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2530 long hitTime
= getTimeAtX(e
.x
);
2533 fDragState
= DRAG_TRACE_ITEM
;
2534 fDragButton
= e
.button
;
2537 fTime0bak
= fTimeProvider
.getTime0();
2538 fTime1bak
= fTimeProvider
.getTime1();
2539 updateCursor(e
.x
, e
.stateMask
);
2541 } else if (3 == e
.button
) {
2543 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2545 fDragTime0
= getTimeAtX(fDragX0
);
2546 fDragState
= DRAG_ZOOM
;
2547 fDragButton
= e
.button
;
2549 updateCursor(e
.x
, e
.stateMask
);
2550 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2555 public void mouseUp(MouseEvent e
) {
2556 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2557 menuDetected(fPendingMenuDetectEvent
);
2559 if (DRAG_NONE
!= fDragState
) {
2561 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2562 if (fDragX
!= fDragX0
) {
2563 fTimeProvider
.notifyStartFinishTime();
2565 fDragState
= DRAG_NONE
;
2566 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2567 fDragState
= DRAG_NONE
;
2569 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2570 if (fDragX
== fDragX0
) { // click without selecting anything
2571 long time
= getTimeAtX(e
.x
);
2572 fTimeProvider
.setSelectedTimeNotify(time
, false);
2574 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2575 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2576 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2578 fDragState
= DRAG_NONE
;
2580 fTimeGraphScale
.setDragRange(-1, -1);
2581 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2582 int nameWidth
= fTimeProvider
.getNameSpace();
2583 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2584 long time0
= getTimeAtX(fDragX0
);
2585 long time1
= getTimeAtX(fDragX
);
2586 if (time0
< time1
) {
2587 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2589 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2594 fDragState
= DRAG_NONE
;
2595 fTimeGraphScale
.setDragRange(-1, -1);
2598 updateCursor(e
.x
, e
.stateMask
);
2599 updateStatusLine(e
.x
);
2603 public void mouseEnter(MouseEvent e
) {
2607 public void mouseExit(MouseEvent e
) {
2608 if (fMouseOverSplitLine
) {
2609 fMouseOverSplitLine
= false;
2612 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2616 public void mouseHover(MouseEvent e
) {
2620 public void mouseScrolled(MouseEvent e
) {
2621 if (fDragState
!= DRAG_NONE
) {
2624 boolean horizontalZoom
= false;
2625 boolean horizontalScroll
= false;
2626 boolean verticalZoom
= false;
2627 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2628 Point parentSize
= getParent().getSize();
2629 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2630 // over the parent control
2631 if (e
.x
> getSize().x
) {
2632 // over the vertical scroll bar
2633 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2634 verticalZoom
= true;
2636 } else if (e
.y
< 0) {
2637 // over the time scale
2638 horizontalZoom
= true;
2639 } else if (e
.y
>= getSize().y
) {
2640 // over the horizontal scroll bar
2641 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2642 horizontalZoom
= true;
2644 horizontalScroll
= true;
2647 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2648 verticalZoom
= true;
2649 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2650 // over the name space
2651 horizontalZoom
= false;
2653 // over the state area
2654 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2655 // over the state area, CTRL pressed
2656 horizontalZoom
= true;
2658 // over the state area, CTRL not pressed
2659 horizontalZoom
= false;
2666 verticalZoom(true, true);
2667 } else if (e
.count
< 0) {
2668 verticalZoom(false, true);
2670 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2673 } else if (e
.count
< 0) {
2676 } else if (horizontalScroll
) {
2677 horizontalScroll(e
.count
> 0);
2679 setTopIndex(getTopIndex() - e
.count
);
2684 public void handleEvent(Event event
) {
2685 if (event
.type
== SWT
.MouseWheel
) {
2686 // prevent horizontal scrolling when the mouse wheel is used to
2687 // scroll vertically or zoom
2693 public int getBorderWidth() {
2694 return fBorderWidth
;
2698 * Set the border width
2700 * @param borderWidth
2703 public void setBorderWidth(int borderWidth
) {
2704 this.fBorderWidth
= borderWidth
;
2708 * @return The current height of the header row
2710 public int getHeaderHeight() {
2711 return fHeaderHeight
;
2715 * Set the height of the header row
2717 * @param headerHeight
2720 public void setHeaderHeight(int headerHeight
) {
2721 this.fHeaderHeight
= headerHeight
;
2725 * @return The default height of regular item rows
2727 public int getItemHeight() {
2728 return fGlobalItemHeight
;
2732 * Set the default height of regular item rows.
2737 public void setItemHeight(int rowHeight
) {
2738 this.fGlobalItemHeight
= rowHeight
;
2739 for (Item item
: fItemData
.fItems
) {
2740 item
.fItemHeight
= rowHeight
;
2745 * Set the height of a specific item. Overrides the default item height.
2748 * A time graph entry
2751 * @return true if the height is successfully stored, false otherwise
2753 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2754 Item item
= fItemData
.findItem(entry
);
2756 item
.fItemHeight
= rowHeight
;
2763 * Set the minimum item width
2765 * @param width The minimum width
2767 public void setMinimumItemWidth(int width
) {
2768 this.fMinimumItemWidth
= width
;
2772 * @return The minimum item width
2774 public int getMinimumItemWidth() {
2775 return fMinimumItemWidth
;
2779 * Set whether all time events with a duration shorter than one pixel should
2780 * be blended in. If false, only the first such time event will be drawn and
2781 * the subsequent time events in the same pixel will be discarded. The
2782 * default value is false.
2785 * true if sub-pixel events should be blended, false otherwise.
2788 public void setBlendSubPixelEvents(boolean blend
) {
2789 fBlendSubPixelEvents
= blend
;
2793 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2794 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2795 fSelectionChangedListeners
.add(listener
);
2800 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2801 if (listener
!= null) {
2802 fSelectionChangedListeners
.remove(listener
);
2807 public void setSelection(ISelection selection
) {
2808 if (selection
instanceof TimeGraphSelection
) {
2809 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2810 Object ob
= sel
.getFirstElement();
2811 if (ob
instanceof ITimeGraphEntry
) {
2812 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2813 selectItem(trace
, false);
2820 * @param filter The filter object to be attached to the view
2822 public void addFilter(@NonNull ViewerFilter filter
) {
2823 if (!fFilters
.contains(filter
)) {
2824 fFilters
.add(filter
);
2829 * @param filter The filter object to be attached to the view
2831 public void removeFilter(@NonNull ViewerFilter filter
) {
2832 fFilters
.remove(filter
);
2836 * Returns this control's filters.
2838 * @return an array of viewer filters
2841 public @NonNull ViewerFilter
[] getFilters() {
2842 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
2846 * Sets the filters, replacing any previous filters.
2849 * an array of viewer filters, or null
2852 public void setFilters(@NonNull ViewerFilter
[] filters
) {
2854 if (filters
!= null) {
2855 fFilters
.addAll(Arrays
.asList(filters
));
2860 public void colorSettingsChanged(StateItem
[] stateItems
) {
2861 /* Destroy previous colors from the resource manager */
2862 if (fEventColorMap
!= null) {
2863 for (Color color
: fEventColorMap
) {
2864 fResourceManager
.destroyColor(color
.getRGB());
2867 if (stateItems
!= null) {
2868 fEventColorMap
= new Color
[stateItems
.length
];
2869 for (int i
= 0; i
< stateItems
.length
; i
++) {
2870 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2873 fEventColorMap
= new Color
[] { };
2878 private class ItemData
{
2879 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2880 private Item
[] fExpandedItems
= new Item
[0];
2881 private Item
[] fItems
= new Item
[0];
2882 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2883 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2888 public Item
findItem(ITimeGraphEntry entry
) {
2889 return fItemMap
.get(entry
);
2892 public int findItemIndex(ITimeGraphEntry entry
) {
2893 Item item
= fItemMap
.get(entry
);
2897 return item
.fExpandedIndex
;
2900 public void refreshData() {
2901 ITimeGraphEntry selection
= getSelectedTrace();
2902 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
2903 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2904 ITimeGraphEntry entry
= fRootEntries
[i
];
2905 refreshData(itemMap
, null, 0, entry
);
2908 fItems
= fItemMap
.values().toArray(new Item
[0]);
2909 updateExpandedItems();
2910 if (selection
!= null) {
2911 for (Item item
: fExpandedItems
) {
2912 if (item
.fEntry
== selection
) {
2913 item
.fSelected
= true;
2920 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2921 Item item
= new Item(entry
, entry
.getName(), level
);
2922 if (parent
!= null) {
2923 parent
.fChildren
.add(item
);
2925 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2926 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2928 item
.fItemHeight
= fGlobalItemHeight
;
2930 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
2931 itemMap
.put(entry
, item
);
2932 if (entry
.hasChildren()) {
2933 Item oldItem
= fItemMap
.get(entry
);
2934 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
2935 /* existing items keep their old expanded state */
2936 item
.fExpanded
= oldItem
.fExpanded
;
2938 /* new items set the expanded state according to auto-expand level */
2939 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2941 item
.fHasChildren
= true;
2942 for (ITimeGraphEntry child
: entry
.getChildren()) {
2943 refreshData(itemMap
, item
, level
+ 1, child
);
2948 public void updateExpandedItems() {
2949 for (Item item
: fItems
) {
2950 item
.fExpandedIndex
= -1;
2952 List
<Item
> expandedItemList
= new ArrayList
<>();
2953 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2954 ITimeGraphEntry entry
= fRootEntries
[i
];
2955 Item item
= findItem(entry
);
2956 refreshExpanded(expandedItemList
, item
);
2958 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2959 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2962 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2963 // Check for filters
2964 boolean display
= true;
2965 for (ViewerFilter filter
: fFilters
) {
2966 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2972 item
.fExpandedIndex
= expandedItemList
.size();
2973 expandedItemList
.add(item
);
2974 if (item
.fHasChildren
&& item
.fExpanded
) {
2975 for (Item child
: item
.fChildren
) {
2976 refreshExpanded(expandedItemList
, child
);
2982 public void refreshData(ITimeGraphEntry
[] entries
) {
2983 if (entries
== null) {
2984 fRootEntries
= null;
2986 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2992 public void refreshArrows(List
<ILinkEvent
> events
) {
2993 /* If links are null, reset the list */
2994 if (events
!= null) {
2997 fLinks
= new ArrayList
<>();
3001 public ITimeGraphEntry
[] getEntries() {
3002 return fRootEntries
;
3006 private class Item
{
3007 private boolean fExpanded
;
3008 private int fExpandedIndex
;
3009 private boolean fSelected
;
3010 private boolean fHasChildren
;
3011 private int fItemHeight
;
3012 private final int fLevel
;
3013 private final List
<Item
> fChildren
;
3014 private final String fName
;
3015 private final ITimeGraphEntry fEntry
;
3017 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3018 this.fEntry
= entry
;
3020 this.fLevel
= level
;
3021 this.fChildren
= new ArrayList
<>();
3025 public String
toString() {
3031 public void menuDetected(MenuDetectEvent e
) {
3032 if (null == fTimeProvider
) {
3035 if (e
.detail
== SWT
.MENU_MOUSE
) {
3036 if (fPendingMenuDetectEvent
== null) {
3037 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3038 * Store the event and trigger it later just before handling mouseUp.
3039 * This allows for the method to detect if mouse is used to drag zoom.
3041 fPendingMenuDetectEvent
= e
;
3044 fPendingMenuDetectEvent
= null;
3045 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
3049 if (fDragState
!= DRAG_NONE
) {
3053 Point p
= toControl(e
.x
, e
.y
);
3054 int idx
= getItemIndexAtY(p
.y
);
3055 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3056 Item item
= fItemData
.fExpandedItems
[idx
];
3057 ITimeGraphEntry entry
= item
.fEntry
;
3058 if (entry
.hasTimeEvents()) {
3059 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3060 if (event
!= null) {
3062 fireMenuEventOnTimeEvent(e
);
3067 fireMenuEventOnTimeGraphEntry(e
);
3072 * Perform the alignment operation.
3075 * the alignment offset
3077 * @see ITmfTimeAligned
3081 public void performAlign(int offset
) {
3082 fTimeProvider
.setNameSpace(offset
);
3086 * Return the time alignment information
3088 * @return the time alignment information
3090 * @see ITmfTimeAligned
3094 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3095 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());