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
.jface
.action
.IStatusLineManager
;
32 import org
.eclipse
.jface
.resource
.JFaceResources
;
33 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
34 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
35 import org
.eclipse
.jface
.viewers
.ISelection
;
36 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
37 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
38 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
39 import org
.eclipse
.osgi
.util
.NLS
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.events
.FocusEvent
;
42 import org
.eclipse
.swt
.events
.FocusListener
;
43 import org
.eclipse
.swt
.events
.KeyEvent
;
44 import org
.eclipse
.swt
.events
.KeyListener
;
45 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
46 import org
.eclipse
.swt
.events
.MenuDetectListener
;
47 import org
.eclipse
.swt
.events
.MouseEvent
;
48 import org
.eclipse
.swt
.events
.MouseListener
;
49 import org
.eclipse
.swt
.events
.MouseMoveListener
;
50 import org
.eclipse
.swt
.events
.MouseTrackListener
;
51 import org
.eclipse
.swt
.events
.MouseWheelListener
;
52 import org
.eclipse
.swt
.events
.PaintEvent
;
53 import org
.eclipse
.swt
.events
.SelectionListener
;
54 import org
.eclipse
.swt
.events
.TraverseEvent
;
55 import org
.eclipse
.swt
.events
.TraverseListener
;
56 import org
.eclipse
.swt
.events
.TypedEvent
;
57 import org
.eclipse
.swt
.graphics
.Color
;
58 import org
.eclipse
.swt
.graphics
.Cursor
;
59 import org
.eclipse
.swt
.graphics
.GC
;
60 import org
.eclipse
.swt
.graphics
.Image
;
61 import org
.eclipse
.swt
.graphics
.Point
;
62 import org
.eclipse
.swt
.graphics
.Rectangle
;
63 import org
.eclipse
.swt
.widgets
.Composite
;
64 import org
.eclipse
.swt
.widgets
.Display
;
65 import org
.eclipse
.swt
.widgets
.Event
;
66 import org
.eclipse
.swt
.widgets
.Listener
;
67 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
86 * Time graph control implementation
88 * @author Alvaro Sanchez-Leon
89 * @author Patrick Tasse
91 public class TimeGraphControl
extends TimeGraphBaseControl
92 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
93 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
94 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
96 /** Constant indicating that all levels of the time graph should be expanded */
97 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
99 private static final int DRAG_NONE
= 0;
100 private static final int DRAG_TRACE_ITEM
= 1;
101 private static final int DRAG_SPLIT_LINE
= 2;
102 private static final int DRAG_ZOOM
= 3;
103 private static final int DRAG_SELECTION
= 4;
105 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
107 private static final double ZOOM_FACTOR
= 1.5;
108 private static final double ZOOM_IN_FACTOR
= 0.8;
109 private static final double ZOOM_OUT_FACTOR
= 1.25;
111 private static final int SNAP_WIDTH
= 2;
112 private static final int ARROW_HOVER_MAX_DIST
= 5;
114 private static final int NO_STATUS
= -1;
115 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
117 /** Resource manager */
118 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
120 /** Color map for event types */
121 private Color
[] fEventColorMap
= null;
123 private ITimeDataProvider fTimeProvider
;
124 private IStatusLineManager fStatusLineManager
= null;
125 private TimeGraphScale fTimeGraphScale
= null;
127 private boolean fIsInFocus
= false;
128 private boolean fMouseOverSplitLine
= false;
129 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
130 private boolean fBlendSubPixelEvents
= false;
131 private int fMinimumItemWidth
= 0;
132 private int fTopIndex
= 0;
133 private int fDragState
= DRAG_NONE
;
134 private boolean fDragBeginMarker
= false;
135 private int fDragButton
;
136 private int fDragX0
= 0;
137 private int fDragX
= 0;
138 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
139 private int fIdealNameSpace
= 0;
140 private long fTime0bak
;
141 private long fTime1bak
;
142 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
143 private ItemData fItemData
= null;
144 private List
<SelectionListener
> fSelectionListeners
;
145 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
146 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
147 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
148 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
149 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
150 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
151 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
152 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
153 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
154 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
155 private MenuDetectEvent fPendingMenuDetectEvent
= null;
156 private boolean fHideArrows
= false;
157 private int fAutoExpandLevel
= ALL_LEVELS
;
159 private int fBorderWidth
= 0;
160 private int fHeaderHeight
= 0;
163 * Standard constructor
166 * The parent composite object
168 * The color scheme to use
170 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
172 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
174 fItemData
= new ItemData();
176 addFocusListener(this);
177 addMouseListener(this);
178 addMouseMoveListener(this);
179 addMouseTrackListener(this);
180 addMouseWheelListener(this);
181 addTraverseListener(this);
182 addKeyListener(this);
183 addMenuDetectListener(this);
184 addListener(SWT
.MouseWheel
, this);
188 public void dispose() {
190 fResourceManager
.dispose();
194 * Sets the timegraph provider used by this timegraph viewer.
196 * @param timeGraphProvider the timegraph provider
198 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
199 fTimeGraphProvider
= timeGraphProvider
;
201 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
202 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
203 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
206 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
207 colorSettingsChanged(stateItems
);
211 * Gets the timegraph provider used by this timegraph viewer.
213 * @return the timegraph provider, or <code>null</code> if not set.
215 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
216 return fTimeGraphProvider
;
220 * Gets the color map used by this timegraph viewer.
222 * @return a color map, or <code>null</code> if not set.
224 public Color
[] getEventColorMap() {
225 return fEventColorMap
;
229 * Assign the given time provider
231 * @param timeProvider
234 public void setTimeProvider(ITimeDataProvider timeProvider
) {
235 fTimeProvider
= timeProvider
;
240 * Assign the status line manager
242 * @param statusLineManager
243 * The status line manager, or null to disable status line messages
245 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
246 if (fStatusLineManager
!= null && statusLineManager
== null) {
247 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
249 fStatusLineManager
= statusLineManager
;
253 * Assign the time graph scale
255 * @param timeGraphScale
256 * The time graph scale
258 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
259 fTimeGraphScale
= timeGraphScale
;
263 * Add a selection listener
266 * The listener to add
268 public void addSelectionListener(SelectionListener listener
) {
269 if (listener
== null) {
270 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
272 if (null == fSelectionListeners
) {
273 fSelectionListeners
= new ArrayList
<>();
275 fSelectionListeners
.add(listener
);
279 * Remove a selection listener
282 * The listener to remove
284 public void removeSelectionListener(SelectionListener listener
) {
285 if (null != fSelectionListeners
) {
286 fSelectionListeners
.remove(listener
);
291 * Selection changed callback
293 public void fireSelectionChanged() {
294 if (null != fSelectionListeners
) {
295 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
296 while (it
.hasNext()) {
297 SelectionListener listener
= it
.next();
298 listener
.widgetSelected(null);
304 * Default selection callback
306 public void fireDefaultSelection() {
307 if (null != fSelectionListeners
) {
308 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
309 while (it
.hasNext()) {
310 SelectionListener listener
= it
.next();
311 listener
.widgetDefaultSelected(null);
317 * Add a drag selection listener
320 * The listener to add
322 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
323 if (listener
== null) {
324 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
326 if (null == fDragSelectionListeners
) {
327 fDragSelectionListeners
= new ArrayList
<>();
329 fDragSelectionListeners
.add(listener
);
333 * Remove a drag selection listener
336 * The listener to remove
338 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
339 if (null != fDragSelectionListeners
) {
340 fDragSelectionListeners
.remove(listener
);
345 * Drag Selection changed callback
348 * Time interval start
352 public void fireDragSelectionChanged(long start
, long end
) {
353 // check for backward intervals
354 long beginTime
, endTime
;
362 // call the listeners
363 if (null != fDragSelectionListeners
) {
364 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
365 while (it
.hasNext()) {
366 ITimeGraphTimeListener listener
= it
.next();
367 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
373 * Get the traces in the model
375 * @return The array of traces
377 public ITimeGraphEntry
[] getTraces() {
378 return fItemData
.getEntries();
382 * Get the on/off trace filters
384 * @return The array of filters
386 public boolean[] getTraceFilter() {
387 return fItemData
.getEntryFilter();
391 * Refresh the data for the thing
393 public void refreshData() {
394 fItemData
.refreshData();
399 * Refresh data for the given traces
402 * The traces to refresh
404 public void refreshData(ITimeGraphEntry
[] traces
) {
405 fItemData
.refreshData(traces
);
410 * Refresh the links (arrows) of this widget
412 * @param events The link event list
414 public void refreshArrows(List
<ILinkEvent
> events
) {
415 fItemData
.refreshArrows(events
);
419 * Get the links (arrows) of this widget
421 * @return The unmodifiable link event list
425 public List
<ILinkEvent
> getArrows() {
426 return Collections
.unmodifiableList(fItemData
.fLinks
);
429 boolean ensureVisibleItem(int idx
, boolean redraw
) {
430 boolean changed
= false;
433 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
434 if (fItemData
.fExpandedItems
[index
].fSelected
) {
439 if (index
>= fItemData
.fExpandedItems
.length
) {
442 if (index
< fTopIndex
) {
449 int page
= countPerPage();
450 if (index
>= fTopIndex
+ page
) {
451 setTopIndex(index
- page
+ 1);
462 * Assign the given index as the top one
467 public void setTopIndex(int idx
) {
468 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
469 index
= Math
.max(0, index
);
475 * Sets the auto-expand level to be used when the entries are refreshed
476 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
477 * The value 0 means that there is no auto-expand; 1 means that top-level
478 * entries are expanded, but not their children; 2 means that top-level
479 * entries are expanded, and their children, but not grand-children; and so
482 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
485 * non-negative level, or <code>ALL_LEVELS</code> to expand all
488 public void setAutoExpandLevel(int level
) {
489 fAutoExpandLevel
= level
;
493 * Returns the auto-expand level.
495 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
496 * the tree are expanded automatically
497 * @see #setAutoExpandLevel
499 public int getAutoExpandLevel() {
500 return fAutoExpandLevel
;
504 * Set the expanded state of a given entry
509 * True if expanded, false if collapsed
511 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
512 Item item
= fItemData
.findItem(entry
);
513 if (item
!= null && item
.fExpanded
!= expanded
) {
514 item
.fExpanded
= expanded
;
515 fItemData
.updateExpandedItems();
521 * Collapses all nodes of the viewer's tree, starting with the root.
523 public void collapseAll() {
524 for (Item item
: fItemData
.fItems
) {
525 item
.fExpanded
= false;
527 fItemData
.updateExpandedItems();
532 * Expands all nodes of the viewer's tree, starting with the root.
534 public void expandAll() {
535 for (Item item
: fItemData
.fItems
) {
536 item
.fExpanded
= true;
538 fItemData
.updateExpandedItems();
543 * Add a tree listener
546 * The listener to add
548 public void addTreeListener(ITimeGraphTreeListener listener
) {
549 if (!fTreeListeners
.contains(listener
)) {
550 fTreeListeners
.add(listener
);
555 * Remove a tree listener
558 * The listener to remove
560 public void removeTreeListener(ITimeGraphTreeListener listener
) {
561 if (fTreeListeners
.contains(listener
)) {
562 fTreeListeners
.remove(listener
);
567 * Tree event callback
572 * The expanded state (true for expanded, false for collapsed)
574 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
575 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
576 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
578 listener
.treeExpanded(event
);
580 listener
.treeCollapsed(event
);
586 * Add a menu listener on {@link ITimeGraphEntry}s
589 * The listener to add
591 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
592 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
593 fTimeGraphEntryMenuListeners
.add(listener
);
598 * Remove a menu listener on {@link ITimeGraphEntry}s
601 * The listener to remove
603 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
604 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
605 fTimeGraphEntryMenuListeners
.remove(listener
);
610 * Menu event callback on {@link ITimeGraphEntry}s
613 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
615 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
616 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
617 listener
.menuDetected(event
);
622 * Add a menu listener on {@link ITimeEvent}s
625 * The listener to add
627 public void addTimeEventMenuListener(MenuDetectListener listener
) {
628 if (!fTimeEventMenuListeners
.contains(listener
)) {
629 fTimeEventMenuListeners
.add(listener
);
634 * Remove a menu listener on {@link ITimeEvent}s
637 * The listener to remove
639 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
640 if (fTimeEventMenuListeners
.contains(listener
)) {
641 fTimeEventMenuListeners
.remove(listener
);
646 * Menu event callback on {@link ITimeEvent}s
649 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
651 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
652 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
653 listener
.menuDetected(event
);
658 public ISelection
getSelection() {
659 TimeGraphSelection sel
= new TimeGraphSelection();
660 ITimeGraphEntry trace
= getSelectedTrace();
661 if (null != trace
&& null != fTimeProvider
) {
662 long selectedTime
= fTimeProvider
.getSelectionBegin();
663 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
674 * Get the selection object
676 * @return The selection
678 public ISelection
getSelectionTrace() {
679 TimeGraphSelection sel
= new TimeGraphSelection();
680 ITimeGraphEntry trace
= getSelectedTrace();
688 * Enable/disable one of the traces in the model
691 * 1 to enable it, -1 to disable. The method returns immediately
692 * if another value is used.
694 public void selectTrace(int n
) {
695 if ((n
!= 1) && (n
!= -1)) {
699 boolean changed
= false;
700 int lastSelection
= -1;
701 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
702 Item item
= fItemData
.fExpandedItems
[i
];
703 if (item
.fSelected
) {
705 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
706 item
.fSelected
= false;
707 item
= fItemData
.fExpandedItems
[i
+ 1];
708 item
.fSelected
= true;
710 } else if ((-1 == n
) && (i
> 0)) {
711 item
.fSelected
= false;
712 item
= fItemData
.fExpandedItems
[i
- 1];
713 item
.fSelected
= true;
720 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
721 Item item
= fItemData
.fExpandedItems
[0];
722 item
.fSelected
= true;
727 ensureVisibleItem(-1, false);
729 fireSelectionChanged();
737 * 1 for next event, -1 for previous event
739 * true to extend selection range, false for single selection
742 public void selectEvent(int n
, boolean extend
) {
743 if (null == fTimeProvider
) {
746 ITimeGraphEntry trace
= getSelectedTrace();
750 long selectedTime
= fTimeProvider
.getSelectionEnd();
751 long endTime
= fTimeProvider
.getMaxTime();
752 ITimeEvent nextEvent
;
753 if (n
== -1 && selectedTime
> endTime
) {
754 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
756 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
758 if (null == nextEvent
&& n
== -1) {
759 nextEvent
= Utils
.getFirstEvent(trace
);
761 if (null != nextEvent
) {
762 long nextTime
= nextEvent
.getTime();
763 // If last event detected e.g. going back or not moving to a next
765 if (nextTime
<= selectedTime
&& n
== 1) {
766 // Select to the end of this last event
767 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
768 // but not beyond the end of the trace
769 if (nextTime
> endTime
) {
772 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
773 // for previous event go to its end time unless we were already there
774 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
777 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
);
779 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
781 fireSelectionChanged();
784 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
);
786 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
788 fireSelectionChanged();
790 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
794 * Select the next event
797 * true to extend selection range, false for single selection
800 public void selectNextEvent(boolean extend
) {
801 selectEvent(1, extend
);
802 // Notify if visible time window has been adjusted
803 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
807 * Select the previous event
810 * true to extend selection range, false for single selection
813 public void selectPrevEvent(boolean extend
) {
814 selectEvent(-1, extend
);
815 // Notify if visible time window has been adjusted
816 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
820 * Select the next trace
822 public void selectNextTrace() {
827 * Select the previous trace
829 public void selectPrevTrace() {
834 * Scroll left or right by one half window size
837 * true to scroll left, false to scroll right
839 public void horizontalScroll(boolean left
) {
840 long time0
= fTimeProvider
.getTime0();
841 long time1
= fTimeProvider
.getTime1();
842 long timeMin
= fTimeProvider
.getMinTime();
843 long timeMax
= fTimeProvider
.getMaxTime();
844 long range
= time1
- time0
;
848 long increment
= Math
.max(1, range
/ 2);
850 time0
= Math
.max(time0
- increment
, timeMin
);
851 time1
= time0
+ range
;
853 time1
= Math
.min(time1
+ increment
, timeMax
);
854 time0
= time1
- range
;
856 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
860 * Zoom based on mouse cursor location with mouse scrolling
862 * @param zoomIn true to zoom in, false to zoom out
864 public void zoom(boolean zoomIn
) {
865 int globalX
= getDisplay().getCursorLocation().x
;
866 Point p
= toControl(globalX
, 0);
867 int nameSpace
= fTimeProvider
.getNameSpace();
868 int timeSpace
= fTimeProvider
.getTimeSpace();
869 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
870 long time0
= fTimeProvider
.getTime0();
871 long time1
= fTimeProvider
.getTime1();
872 long interval
= time1
- time0
;
875 } // to allow getting out of single point interval
878 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
880 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
882 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
883 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
884 long newTime1
= newTime0
+ newInterval
;
885 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
889 * zoom in using single click
891 public void zoomIn() {
892 long prevTime0
= fTimeProvider
.getTime0();
893 long prevTime1
= fTimeProvider
.getTime1();
894 long prevRange
= prevTime1
- prevTime0
;
895 if (prevRange
== 0) {
898 ITimeDataProvider provider
= fTimeProvider
;
899 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
900 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
901 selTime
= (prevTime0
+ prevTime1
) / 2;
903 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
904 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
906 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
908 if (inaccuracy
> 0 && inaccuracy
< 100) {
909 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
913 long min
= fTimeProvider
.getMinTimeInterval();
914 if ((time1
- time0
) < min
) {
915 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
919 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
923 * zoom out using single click
925 public void zoomOut() {
926 long prevTime0
= fTimeProvider
.getTime0();
927 long prevTime1
= fTimeProvider
.getTime1();
928 ITimeDataProvider provider
= fTimeProvider
;
929 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
930 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
931 selTime
= (prevTime0
+ prevTime1
) / 2;
933 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
934 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
936 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
937 if (inaccuracy
> 0 && inaccuracy
< 100) {
938 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
942 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
948 * @param hideArrows true to hide arrows
950 public void hideArrows(boolean hideArrows
) {
951 fHideArrows
= hideArrows
;
955 * Follow the arrow forward
958 * true to extend selection range, false for single selection
961 public void followArrowFwd(boolean extend
) {
962 ITimeGraphEntry trace
= getSelectedTrace();
966 long selectedTime
= fTimeProvider
.getSelectionEnd();
967 for (ILinkEvent link
: fItemData
.fLinks
) {
968 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
969 selectItem(link
.getDestinationEntry(), false);
970 if (link
.getDuration() != 0) {
972 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration());
974 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
976 // Notify if visible time window has been adjusted
977 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
979 fireSelectionChanged();
980 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
984 selectNextEvent(extend
);
988 * Follow the arrow backward
991 * true to extend selection range, false for single selection
994 public void followArrowBwd(boolean extend
) {
995 ITimeGraphEntry trace
= getSelectedTrace();
999 long selectedTime
= fTimeProvider
.getSelectionEnd();
1000 for (ILinkEvent link
: fItemData
.fLinks
) {
1001 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1002 selectItem(link
.getEntry(), false);
1003 if (link
.getDuration() != 0) {
1005 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime());
1007 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1009 // Notify if visible time window has been adjusted
1010 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1012 fireSelectionChanged();
1013 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1017 selectPrevEvent(extend
);
1021 * Return the currently selected trace
1023 * @return The entry matching the trace
1025 public ITimeGraphEntry
getSelectedTrace() {
1026 ITimeGraphEntry trace
= null;
1027 int idx
= getSelectedIndex();
1029 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1035 * Retrieve the index of the currently selected item
1039 public int getSelectedIndex() {
1041 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1042 Item item
= fItemData
.fExpandedItems
[i
];
1043 if (item
.fSelected
) {
1051 boolean toggle(int idx
) {
1052 boolean toggled
= false;
1053 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1054 Item item
= fItemData
.fExpandedItems
[idx
];
1055 if (item
.fHasChildren
) {
1056 item
.fExpanded
= !item
.fExpanded
;
1057 fItemData
.updateExpandedItems();
1060 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1067 * Gets the index of the item at the given location.
1071 * @return the index of the item at the given location, of -1 if none.
1073 protected int getItemIndexAtY(int y
) {
1078 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1079 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1087 boolean isOverSplitLine(int x
) {
1088 if (x
< 0 || null == fTimeProvider
) {
1091 int nameWidth
= fTimeProvider
.getNameSpace();
1092 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1096 * Gets the {@link ITimeGraphEntry} at the given location.
1099 * a point in the widget
1100 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1103 protected ITimeGraphEntry
getEntry(Point pt
) {
1104 int idx
= getItemIndexAtY(pt
.y
);
1105 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1109 * Return the arrow event closest to the given point that is no further than
1110 * a maximum distance.
1113 * a point in the widget
1114 * @return The closest arrow event, or null if there is none close enough.
1116 protected ILinkEvent
getArrow(Point pt
) {
1120 ILinkEvent linkEvent
= null;
1121 double minDistance
= Double
.MAX_VALUE
;
1122 for (ILinkEvent event
: fItemData
.fLinks
) {
1123 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1127 int x2
= x1
+ rect
.width
;
1128 int y2
= y1
+ rect
.height
;
1129 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1130 if (minDistance
> d
) {
1136 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1143 public int getXForTime(long time
) {
1144 if (null == fTimeProvider
) {
1147 long time0
= fTimeProvider
.getTime0();
1148 long time1
= fTimeProvider
.getTime1();
1149 int width
= getSize().x
;
1150 int nameSpace
= fTimeProvider
.getNameSpace();
1151 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1152 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1157 public long getTimeAtX(int coord
) {
1158 if (null == fTimeProvider
) {
1162 Point size
= getSize();
1163 long time0
= fTimeProvider
.getTime0();
1164 long time1
= fTimeProvider
.getTime1();
1165 int nameWidth
= fTimeProvider
.getNameSpace();
1166 final int x
= coord
- nameWidth
;
1167 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1168 if (x
>= 0 && size
.x
>= nameWidth
) {
1169 if (time1
- time0
> timeWidth
) {
1170 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1171 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1173 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1174 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1180 void selectItem(int idx
, boolean addSelection
) {
1181 boolean changed
= false;
1183 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1184 Item item
= fItemData
.fExpandedItems
[idx
];
1185 changed
= !item
.fSelected
;
1186 item
.fSelected
= true;
1189 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1190 Item item
= fItemData
.fExpandedItems
[i
];
1191 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1194 item
.fSelected
= i
== idx
;
1197 changed
|= ensureVisibleItem(idx
, true);
1204 * Callback for item selection
1207 * The entry matching the trace
1208 * @param addSelection
1209 * If the selection is added or removed
1211 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1212 int idx
= fItemData
.findItemIndex(trace
);
1213 selectItem(idx
, addSelection
);
1217 * Retrieve the number of entries shown per page.
1221 public int countPerPage() {
1222 int height
= getSize().y
;
1225 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1226 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1227 if (ySum
>= height
) {
1232 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1233 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1234 if (ySum
>= height
) {
1243 * Get the index of the top element
1247 public int getTopIndex() {
1252 * Get the number of expanded items
1254 * @return The count of expanded items
1256 public int getExpandedElementCount() {
1257 return fItemData
.fExpandedItems
.length
;
1261 * Get an array of all expanded elements
1263 * @return The expanded elements
1265 public ITimeGraphEntry
[] getExpandedElements() {
1266 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1267 for (Item item
: fItemData
.fExpandedItems
) {
1268 elements
.add(item
.fEntry
);
1270 return elements
.toArray(new ITimeGraphEntry
[0]);
1273 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1274 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1276 rect
.width
= nameWidth
;
1280 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1281 int x
= bound
.x
+ nameWidth
;
1282 int width
= bound
.width
- x
;
1284 if (idx
>= fTopIndex
) {
1285 for (int i
= fTopIndex
; i
< idx
; i
++) {
1286 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1289 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1290 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1293 int y
= bound
.y
+ ySum
;
1294 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1295 return new Rectangle(x
, y
, width
, height
);
1299 void paint(Rectangle bounds
, PaintEvent e
) {
1301 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1302 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1304 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1308 fIdealNameSpace
= 0;
1309 int nameSpace
= fTimeProvider
.getNameSpace();
1311 // draw empty name space background
1312 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1313 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1316 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1317 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1318 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1320 int alpha
= gc
.getAlpha();
1323 long time0
= fTimeProvider
.getTime0();
1324 long time1
= fTimeProvider
.getTime1();
1325 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1326 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1327 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1328 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1329 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1331 // draw selection lines
1332 if (fDragState
!= DRAG_SELECTION
) {
1333 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1334 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1335 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1338 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1339 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1344 // draw selection background
1345 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1346 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1347 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1348 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1350 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1351 } else if (x0
- x1
> 1) {
1352 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1356 // draw drag selection background
1357 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1358 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1359 if (fDragX0
< fDragX
) {
1360 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1361 } else if (fDragX0
> fDragX
) {
1362 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1367 if (DRAG_SPLIT_LINE
== fDragState
) {
1368 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1369 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1370 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1371 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1372 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1373 if (fDragX
!= fDragX0
) {
1374 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1376 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1377 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1378 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1379 if (fDragX
!= fDragX0
) {
1380 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1382 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1383 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1384 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1391 * Draw many items at once
1394 * The rectangle of the area
1395 * @param timeProvider
1398 * The array items to draw
1400 * The index of the first element to draw
1402 * The width reserved for the names
1404 * Reference to the SWT GC object
1406 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1407 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1408 for (int i
= topIndex
; i
< items
.length
; i
++) {
1409 Item item
= items
[i
];
1410 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1417 * @param item the item to draw
1418 * @param bounds the container rectangle
1419 * @param timeProvider Time provider
1420 * @param i the item index
1421 * @param nameSpace the name space
1422 * @param gc Graphics context
1424 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1425 ITimeGraphEntry entry
= item
.fEntry
;
1426 long time0
= timeProvider
.getTime0();
1427 long time1
= timeProvider
.getTime1();
1428 long selectedTime
= fTimeProvider
.getSelectionEnd();
1430 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1431 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1435 if (! item
.fEntry
.hasTimeEvents()) {
1436 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1437 nameRect
.width
+= statesRect
.width
;
1438 drawName(item
, nameRect
, gc
);
1440 drawName(item
, nameRect
, gc
);
1442 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1443 if (rect
.isEmpty()) {
1444 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1447 if (time1
<= time0
) {
1448 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1449 gc
.fillRectangle(rect
);
1450 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1454 // Initialize _rect1 to same values as enclosing rectangle rect
1455 Rectangle stateRect
= Utils
.clone(rect
);
1456 boolean selected
= item
.fSelected
;
1457 // K pixels per second
1458 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1460 if (item
.fEntry
.hasTimeEvents()) {
1461 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1462 fillSpace(rect
, gc
, selected
);
1463 // Drawing rectangle is smaller than reserved space
1465 stateRect
.height
-= 6;
1467 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1468 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1471 while (iterator
.hasNext()) {
1472 ITimeEvent event
= iterator
.next();
1473 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1474 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1475 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1476 // event is out of bounds
1479 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1480 stateRect
.x
= Math
.max(rect
.x
, x
);
1481 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1482 if (stateRect
.x
== lastX
) {
1483 stateRect
.width
-= 1;
1484 if (stateRect
.width
> 0) {
1485 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1486 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1490 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1491 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1492 lastX
= stateRect
.x
;
1495 gc
.setClipping((Rectangle
) null);
1497 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1504 * The rectangle of the area
1505 * @param timeProvider
1508 * The array items to draw
1510 * The width reserved for the names
1512 * Reference to the SWT GC object
1514 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1515 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1519 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1520 for (ILinkEvent event
: links
) {
1521 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1523 gc
.setClipping((Rectangle
) null);
1527 * Draws the link type events of this item
1532 * the container rectangle
1533 * @param timeProvider
1540 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1541 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1544 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1545 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1546 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1548 if ((srcIndex
== -1) || (destIndex
== -1)) {
1552 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1553 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1555 int x0
= getXForTime(event
.getTime());
1556 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1558 // limit the x-coordinates to prevent integer overflow in calculations
1559 // and also GC.drawLine doesn't draw properly with large coordinates
1560 final int limit
= Integer
.MAX_VALUE
/ 1024;
1561 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1562 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1564 int y0
= src
.y
+ src
.height
/ 2;
1565 int y1
= dst
.y
+ dst
.height
/ 2;
1566 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1570 * Draw the state (color fill)
1575 * Time event for which we're drawing the state
1580 * @return true if the state was drawn
1582 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1583 Rectangle rect
, GC gc
) {
1588 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1592 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1595 Color stateColor
= null;
1596 if (colorIdx
< fEventColorMap
.length
) {
1597 stateColor
= fEventColorMap
[colorIdx
];
1599 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1602 gc
.setForeground(stateColor
);
1603 gc
.setBackground(stateColor
);
1605 /* Draw the arrow */
1606 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1607 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1610 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1615 * @author Francis Giraldeau
1618 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1620 * The algorithm was taken from this site, not the code itself
1622 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1625 double cos
= 0.9510;
1626 double sin
= 0.3090;
1627 long lenx
= x1
- x0
;
1628 long leny
= y1
- y0
;
1629 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1631 double dx
= factor
* lenx
/ len
;
1632 double dy
= factor
* leny
/ len
;
1633 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1634 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1635 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1636 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1637 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1638 gc
.fillPolygon(arrow
);
1642 * Draw the name of an item.
1647 * Where to draw the name
1651 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1652 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1653 if (! hasTimeEvents
) {
1654 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1655 gc
.fillRectangle(bounds
);
1656 if (item
.fSelected
&& fIsInFocus
) {
1657 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1658 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1661 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1662 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1663 gc
.fillRectangle(bounds
);
1666 // No name to be drawn
1667 if (fTimeProvider
.getNameSpace() == 0) {
1671 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1672 if (item
.fHasChildren
) {
1673 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1674 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1675 Rectangle rect
= Utils
.clone(bounds
);
1676 rect
.x
+= leftMargin
;
1677 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1678 rect
.width
= EXPAND_SIZE
;
1679 rect
.height
= EXPAND_SIZE
;
1680 gc
.fillRectangle(rect
);
1681 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1682 int midy
= rect
.y
+ rect
.height
/ 2;
1683 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1684 if (!item
.fExpanded
) {
1685 int midx
= rect
.x
+ rect
.width
/ 2;
1686 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1689 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1691 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1694 int imgHeight
= img
.getImageData().height
;
1695 int imgWidth
= img
.getImageData().width
;
1697 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1698 gc
.drawImage(img
, x
, y
);
1699 leftMargin
+= imgWidth
+ MARGIN
;
1701 String name
= item
.fName
;
1702 Point size
= gc
.stringExtent(name
);
1703 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1704 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1706 if (hasTimeEvents
) {
1707 // cut long string with "..."
1708 int width
= bounds
.width
- leftMargin
;
1710 while (size
.x
> width
&& name
.length() > 1) {
1712 name
= name
.substring(0, name
.length() - 1);
1713 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1716 name
+= "..."; //$NON-NLS-1$
1719 Rectangle rect
= Utils
.clone(bounds
);
1720 rect
.x
+= leftMargin
;
1721 rect
.width
-= leftMargin
;
1723 if (rect
.width
> 0) {
1724 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1725 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1726 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1727 leftMargin
+= textWidth
+ MARGIN
;
1730 if (hasTimeEvents
) {
1732 int x
= bounds
.x
+ leftMargin
;
1733 int width
= bounds
.width
- x
;
1734 int midy
= bounds
.y
+ bounds
.height
/ 2;
1735 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1736 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1742 * Draw the state (color fill)
1747 * Time event for which we're drawing the state
1753 * Is this time event currently selected (so it appears
1755 * @param timeSelected
1756 * Is the timestamp currently selected
1757 * @return true if the state was drawn
1759 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1760 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1762 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1763 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1766 boolean visible
= rect
.width
== 0 ?
false : true;
1767 rect
.width
= Math
.max(1, rect
.width
);
1768 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1769 gc
.setForeground(black
);
1771 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1773 // Only draw the top and bottom borders
1774 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1775 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1776 if (rect
.width
== 1) {
1777 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1780 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1783 Color stateColor
= null;
1784 if (colorIdx
< fEventColorMap
.length
) {
1785 stateColor
= fEventColorMap
[colorIdx
];
1790 boolean reallySelected
= timeSelected
&& selected
;
1791 // fill all rect area
1792 gc
.setBackground(stateColor
);
1794 gc
.fillRectangle(rect
);
1795 } else if (fBlendSubPixelEvents
) {
1797 gc
.fillRectangle(rect
);
1801 if (reallySelected
) {
1802 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1803 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1806 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1808 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1813 * Fill the space between two contiguous time events
1820 * Is this time event selected or not
1822 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1823 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1824 gc
.fillRectangle(rect
);
1825 if (fDragState
== DRAG_ZOOM
) {
1826 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1827 if (fDragX0
< fDragX
) {
1828 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1829 } else if (fDragX0
> fDragX
) {
1830 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1834 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1835 int midy
= rect
.y
+ rect
.height
/ 2;
1836 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1840 public void keyTraversed(TraverseEvent e
) {
1841 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1847 public void keyPressed(KeyEvent e
) {
1849 if (fItemData
.fExpandedItems
.length
== 0) {
1852 if (SWT
.HOME
== e
.keyCode
) {
1854 } else if (SWT
.END
== e
.keyCode
) {
1855 idx
= fItemData
.fExpandedItems
.length
- 1;
1856 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1857 idx
= getSelectedIndex();
1860 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1863 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1864 idx
= getSelectedIndex();
1867 } else if (idx
> 0) {
1870 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
1871 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1872 selectPrevEvent(extend
);
1873 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
1874 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1875 selectNextEvent(extend
);
1876 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1877 int page
= countPerPage();
1878 idx
= getSelectedIndex();
1883 if (idx
>= fItemData
.fExpandedItems
.length
) {
1884 idx
= fItemData
.fExpandedItems
.length
- 1;
1886 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1887 int page
= countPerPage();
1888 idx
= getSelectedIndex();
1896 } else if (SWT
.CR
== e
.keyCode
) {
1897 idx
= getSelectedIndex();
1899 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1902 fireDefaultSelection();
1908 selectItem(idx
, false);
1909 fireSelectionChanged();
1911 int x
= toControl(e
.display
.getCursorLocation()).x
;
1912 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1916 public void keyReleased(KeyEvent e
) {
1917 int x
= toControl(e
.display
.getCursorLocation()).x
;
1918 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1922 public void focusGained(FocusEvent e
) {
1925 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1929 public void focusLost(FocusEvent e
) {
1931 if (DRAG_NONE
!= fDragState
) {
1933 fDragState
= DRAG_NONE
;
1936 updateStatusLine(NO_STATUS
);
1940 * @return If the current view is focused
1942 public boolean isInFocus() {
1947 * Provide the possibility to control the wait cursor externally e.g. data
1948 * requests in progress
1950 * @param waitInd Should we wait indefinitely?
1952 public void waitCursor(boolean waitInd
) {
1953 // Update cursor as indicated
1955 setCursor(fWaitCursor
);
1961 private void updateCursor(int x
, int stateMask
) {
1962 // if Wait cursor not active, check for the need to change the cursor
1963 if (getCursor() == fWaitCursor
) {
1966 Cursor cursor
= null;
1967 if (fDragState
== DRAG_SPLIT_LINE
) {
1968 } else if (fDragState
== DRAG_SELECTION
) {
1969 cursor
= fResizeCursor
;
1970 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1971 cursor
= fDragCursor
;
1972 } else if (fDragState
== DRAG_ZOOM
) {
1973 cursor
= fZoomCursor
;
1974 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1975 cursor
= fDragCursor
;
1976 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1977 cursor
= fResizeCursor
;
1978 } else if (!isOverSplitLine(x
)) {
1979 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1980 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1981 int xBegin
= getXForTime(selectionBegin
);
1982 int xEnd
= getXForTime(selectionEnd
);
1983 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1984 cursor
= fResizeCursor
;
1987 if (getCursor() != cursor
) {
1992 private void updateStatusLine(int x
) {
1993 // use the time provider of the time graph scale for the status line
1994 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
1995 if (fStatusLineManager
== null || null == tdp
||
1996 tdp
.getTime0() == tdp
.getTime1()) {
1999 TimeFormat tf
= tdp
.getTimeFormat();
2000 Resolution res
= Resolution
.NANOSEC
;
2001 StringBuilder message
= new StringBuilder();
2002 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2003 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2004 long time
= getTimeAtX(x
);
2006 if (tdp
instanceof ITimeDataProviderConverter
) {
2007 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2009 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2011 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2012 Utils
.formatTime(time
, tf
, res
)
2016 long selectionBegin
= tdp
.getSelectionBegin();
2017 long selectionEnd
= tdp
.getSelectionEnd();
2018 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2020 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2021 Utils
.formatTime(selectionBegin
, tf
, res
)
2023 if (selectionBegin
!= selectionEnd
) {
2024 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2026 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2027 Utils
.formatTime(selectionEnd
, tf
, res
),
2028 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2031 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2032 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2033 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2034 if (tdp
instanceof ITimeDataProviderConverter
) {
2035 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2036 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2038 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2040 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2041 Utils
.formatTime(time0
, tf
, res
),
2042 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2043 Utils
.formatTime(time
, tf
, res
),
2044 Utils
.formatDelta(time
- time0
, tf
, res
)
2047 fStatusLineManager
.setMessage(message
.toString());
2051 public void mouseMove(MouseEvent e
) {
2052 if (null == fTimeProvider
) {
2055 Point size
= getSize();
2056 if (DRAG_TRACE_ITEM
== fDragState
) {
2057 int nameWidth
= fTimeProvider
.getNameSpace();
2058 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2060 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2061 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2062 long time1
= fTime1bak
- timeDelta
;
2063 long maxTime
= fTimeProvider
.getMaxTime();
2064 if (time1
> maxTime
) {
2067 long time0
= time1
- (fTime1bak
- fTime0bak
);
2068 if (time0
< fTimeProvider
.getMinTime()) {
2069 time0
= fTimeProvider
.getMinTime();
2070 time1
= time0
+ (fTime1bak
- fTime0bak
);
2072 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2074 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2076 fTimeProvider
.setNameSpace(e
.x
);
2077 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2078 } else if (DRAG_SELECTION
== fDragState
) {
2079 if (fDragBeginMarker
) {
2080 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2082 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2085 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2086 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2087 } else if (DRAG_ZOOM
== fDragState
) {
2088 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2090 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2091 } else if (DRAG_NONE
== fDragState
) {
2092 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2093 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2096 fMouseOverSplitLine
= mouseOverSplitLine
;
2098 updateCursor(e
.x
, e
.stateMask
);
2099 updateStatusLine(e
.x
);
2103 public void mouseDoubleClick(MouseEvent e
) {
2104 if (null == fTimeProvider
) {
2107 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2108 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2109 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2110 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2111 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2114 fMouseOverSplitLine
= mouseOverSplitLine
;
2115 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2118 int idx
= getItemIndexAtY(e
.y
);
2120 selectItem(idx
, false);
2121 fireDefaultSelection();
2127 public void mouseDown(MouseEvent e
) {
2128 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2129 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2130 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2134 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2135 int nameSpace
= fTimeProvider
.getNameSpace();
2136 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2137 fDragState
= DRAG_SPLIT_LINE
;
2138 fDragButton
= e
.button
;
2141 fTime0bak
= fTimeProvider
.getTime0();
2142 fTime1bak
= fTimeProvider
.getTime1();
2144 updateCursor(e
.x
, e
.stateMask
);
2148 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2149 int nameSpace
= fTimeProvider
.getNameSpace();
2150 idx
= getItemIndexAtY(e
.y
);
2152 Item item
= fItemData
.fExpandedItems
[idx
];
2153 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2157 selectItem(idx
, false);
2158 fireSelectionChanged();
2160 selectItem(idx
, false); // clear selection
2161 fireSelectionChanged();
2163 long hitTime
= getTimeAtX(e
.x
);
2167 fDragState
= DRAG_SELECTION
;
2168 fDragBeginMarker
= false;
2169 fDragButton
= e
.button
;
2172 fDragTime0
= getTimeAtX(fDragX0
);
2173 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2174 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2175 int xBegin
= getXForTime(selectionBegin
);
2176 int xEnd
= getXForTime(selectionEnd
);
2177 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2178 long time
= getTimeAtX(e
.x
);
2179 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2180 fDragBeginMarker
= true;
2183 fDragTime0
= selectionEnd
;
2186 fDragTime0
= selectionBegin
;
2189 long time
= getTimeAtX(e
.x
);
2190 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2191 fDragBeginMarker
= true;
2194 fDragTime0
= selectionEnd
;
2195 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2197 fDragTime0
= selectionBegin
;
2200 fTime0bak
= fTimeProvider
.getTime0();
2201 fTime1bak
= fTimeProvider
.getTime1();
2203 updateCursor(e
.x
, e
.stateMask
);
2204 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2206 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2207 long hitTime
= getTimeAtX(e
.x
);
2210 fDragState
= DRAG_TRACE_ITEM
;
2211 fDragButton
= e
.button
;
2214 fTime0bak
= fTimeProvider
.getTime0();
2215 fTime1bak
= fTimeProvider
.getTime1();
2216 updateCursor(e
.x
, e
.stateMask
);
2218 } else if (3 == e
.button
) {
2220 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2222 fDragTime0
= getTimeAtX(fDragX0
);
2223 fDragState
= DRAG_ZOOM
;
2224 fDragButton
= e
.button
;
2226 updateCursor(e
.x
, e
.stateMask
);
2227 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2232 public void mouseUp(MouseEvent e
) {
2233 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2234 menuDetected(fPendingMenuDetectEvent
);
2236 if (DRAG_NONE
!= fDragState
) {
2238 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2239 if (fDragX
!= fDragX0
) {
2240 fTimeProvider
.notifyStartFinishTime();
2242 fDragState
= DRAG_NONE
;
2243 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2244 fDragState
= DRAG_NONE
;
2246 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2247 if (fDragX
== fDragX0
) { // click without selecting anything
2248 long time
= getTimeAtX(e
.x
);
2249 fTimeProvider
.setSelectedTimeNotify(time
, false);
2251 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2252 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2253 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2255 fDragState
= DRAG_NONE
;
2257 fTimeGraphScale
.setDragRange(-1, -1);
2258 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2259 int nameWidth
= fTimeProvider
.getNameSpace();
2260 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2261 long time0
= getTimeAtX(fDragX0
);
2262 long time1
= getTimeAtX(fDragX
);
2263 if (time0
< time1
) {
2264 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2266 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2271 fDragState
= DRAG_NONE
;
2272 fTimeGraphScale
.setDragRange(-1, -1);
2275 updateCursor(e
.x
, e
.stateMask
);
2276 updateStatusLine(e
.x
);
2280 public void mouseEnter(MouseEvent e
) {
2284 public void mouseExit(MouseEvent e
) {
2285 if (fMouseOverSplitLine
) {
2286 fMouseOverSplitLine
= false;
2289 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2293 public void mouseHover(MouseEvent e
) {
2297 public void mouseScrolled(MouseEvent e
) {
2298 if (fDragState
!= DRAG_NONE
) {
2301 boolean zoomScroll
= false;
2302 boolean horizontalScroll
= false;
2303 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2304 Point parentSize
= getParent().getSize();
2305 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2306 // over the parent control
2307 if (e
.x
> getSize().x
) {
2308 // over the vertical scroll bar
2310 } else if (e
.y
< 0) {
2311 // over the time scale
2313 } else if (e
.y
>= getSize().y
) {
2314 // over the horizontal scroll bar
2315 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2318 horizontalScroll
= true;
2321 if (e
.x
< fTimeProvider
.getNameSpace()) {
2322 // over the name space
2325 // over the state area
2326 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2327 // over the state area, CTRL pressed
2330 // over the state area, CTRL not pressed
2336 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2339 } else if (e
.count
< 0) {
2342 } else if (horizontalScroll
) {
2343 horizontalScroll(e
.count
> 0);
2345 setTopIndex(getTopIndex() - e
.count
);
2350 public void handleEvent(Event event
) {
2351 if (event
.type
== SWT
.MouseWheel
) {
2352 // prevent horizontal scrolling when the mouse wheel is used to
2353 // scroll vertically or zoom
2359 public int getBorderWidth() {
2360 return fBorderWidth
;
2364 * Set the border width
2366 * @param borderWidth
2369 public void setBorderWidth(int borderWidth
) {
2370 this.fBorderWidth
= borderWidth
;
2374 * @return The current height of the header row
2376 public int getHeaderHeight() {
2377 return fHeaderHeight
;
2381 * Set the height of the header row
2383 * @param headerHeight
2386 public void setHeaderHeight(int headerHeight
) {
2387 this.fHeaderHeight
= headerHeight
;
2391 * @return The default height of regular item rows
2393 public int getItemHeight() {
2394 return fGlobalItemHeight
;
2398 * Set the default height of regular item rows.
2403 public void setItemHeight(int rowHeight
) {
2404 this.fGlobalItemHeight
= rowHeight
;
2408 * Set the height of a specific item. Overrides the default item height.
2411 * A time graph entry
2414 * @return true if the height is successfully stored, false otherwise
2416 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2417 Item item
= fItemData
.findItem(entry
);
2419 item
.fItemHeight
= rowHeight
;
2426 * Set the minimum item width
2428 * @param width The minimum width
2430 public void setMinimumItemWidth(int width
) {
2431 this.fMinimumItemWidth
= width
;
2435 * @return The minimum item width
2437 public int getMinimumItemWidth() {
2438 return fMinimumItemWidth
;
2442 * Set whether all time events with a duration shorter than one pixel should
2443 * be blended in. If false, only the first such time event will be drawn and
2444 * the subsequent time events in the same pixel will be discarded. The
2445 * default value is false.
2448 * true if sub-pixel events should be blended, false otherwise.
2451 public void setBlendSubPixelEvents(boolean blend
) {
2452 fBlendSubPixelEvents
= blend
;
2456 * @return The entries that are currently filtered out
2458 public List
<ITimeGraphEntry
> getFilteredOut() {
2459 return fItemData
.getFilteredOut();
2463 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2464 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2465 fSelectionChangedListeners
.add(listener
);
2470 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2471 if (listener
!= null) {
2472 fSelectionChangedListeners
.remove(listener
);
2477 public void setSelection(ISelection selection
) {
2478 if (selection
instanceof TimeGraphSelection
) {
2479 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2480 Object ob
= sel
.getFirstElement();
2481 if (ob
instanceof ITimeGraphEntry
) {
2482 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2483 selectItem(trace
, false);
2490 * @param filter The filter object to be attached to the view
2492 public void addFilter(ViewerFilter filter
) {
2493 if (!fFilters
.contains(filter
)) {
2494 fFilters
.add(filter
);
2499 * @param filter The filter object to be attached to the view
2501 public void removeFilter(ViewerFilter filter
) {
2502 fFilters
.remove(filter
);
2506 public void colorSettingsChanged(StateItem
[] stateItems
) {
2507 /* Destroy previous colors from the resource manager */
2508 if (fEventColorMap
!= null) {
2509 for (Color color
: fEventColorMap
) {
2510 fResourceManager
.destroyColor(color
.getRGB());
2513 if (stateItems
!= null) {
2514 fEventColorMap
= new Color
[stateItems
.length
];
2515 for (int i
= 0; i
< stateItems
.length
; i
++) {
2516 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2519 fEventColorMap
= new Color
[] { };
2524 private class ItemData
{
2525 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2526 private Item
[] fExpandedItems
= new Item
[0];
2527 private Item
[] fItems
= new Item
[0];
2528 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2529 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2530 private boolean fEntryFilter
[] = new boolean[0];
2531 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2536 public Item
findItem(ITimeGraphEntry entry
) {
2537 return fItemMap
.get(entry
);
2540 public int findItemIndex(ITimeGraphEntry entry
) {
2541 Item item
= fItemMap
.get(entry
);
2545 return item
.fExpandedIndex
;
2548 public void refreshData() {
2550 fFilteredOut
.clear();
2551 ITimeGraphEntry selection
= getSelectedTrace();
2552 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2553 ITimeGraphEntry entry
= fRootEntries
[i
];
2554 refreshData(fItemMap
, null, 0, entry
);
2556 fItems
= fItemMap
.values().toArray(new Item
[0]);
2557 updateExpandedItems();
2558 if (selection
!= null) {
2559 for (Item item
: fExpandedItems
) {
2560 if (item
.fEntry
== selection
) {
2561 item
.fSelected
= true;
2568 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2569 Item item
= new Item(entry
, entry
.getName(), level
);
2570 if (parent
!= null) {
2571 parent
.fChildren
.add(item
);
2573 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2574 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2576 item
.fItemHeight
= fGlobalItemHeight
;
2578 itemMap
.put(entry
, item
);
2579 if (entry
.hasChildren()) {
2580 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2581 item
.fHasChildren
= true;
2582 for (ITimeGraphEntry child
: entry
.getChildren()) {
2583 refreshData(itemMap
, item
, level
+ 1, child
);
2588 public void updateExpandedItems() {
2589 for (Item item
: fItems
) {
2590 item
.fExpandedIndex
= -1;
2592 List
<Item
> expandedItemList
= new ArrayList
<>();
2593 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2594 ITimeGraphEntry entry
= fRootEntries
[i
];
2595 Item item
= findItem(entry
);
2596 refreshExpanded(expandedItemList
, item
);
2598 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2599 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2602 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2603 // Check for filters
2604 boolean display
= true;
2605 for (ViewerFilter filter
: fFilters
) {
2606 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2612 item
.fExpandedIndex
= expandedItemList
.size();
2613 expandedItemList
.add(item
);
2614 if (item
.fHasChildren
&& item
.fExpanded
) {
2615 for (Item child
: item
.fChildren
) {
2616 refreshExpanded(expandedItemList
, child
);
2622 public void refreshData(ITimeGraphEntry
[] entries
) {
2623 if (entries
== null) {
2624 fEntryFilter
= null;
2625 fRootEntries
= null;
2627 if (entries
.length
== 0) {
2628 fEntryFilter
= null;
2629 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2630 fEntryFilter
= new boolean[entries
.length
];
2631 java
.util
.Arrays
.fill(fEntryFilter
, true);
2633 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2639 public void refreshArrows(List
<ILinkEvent
> events
) {
2640 /* If links are null, reset the list */
2641 if (events
!= null) {
2644 fLinks
= new ArrayList
<>();
2648 public ITimeGraphEntry
[] getEntries() {
2649 return fRootEntries
;
2652 public boolean[] getEntryFilter() {
2653 return fEntryFilter
;
2656 public List
<ITimeGraphEntry
> getFilteredOut() {
2657 return fFilteredOut
;
2661 private class Item
{
2662 private boolean fExpanded
;
2663 private int fExpandedIndex
;
2664 private boolean fSelected
;
2665 private boolean fHasChildren
;
2666 private int fItemHeight
;
2667 private final int fLevel
;
2668 private final List
<Item
> fChildren
;
2669 private final String fName
;
2670 private final ITimeGraphEntry fEntry
;
2672 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2673 this.fEntry
= entry
;
2675 this.fLevel
= level
;
2676 this.fChildren
= new ArrayList
<>();
2680 public String
toString() {
2686 public void menuDetected(MenuDetectEvent e
) {
2687 if (null == fTimeProvider
) {
2690 if (e
.detail
== SWT
.MENU_MOUSE
) {
2691 if (fPendingMenuDetectEvent
== null) {
2692 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2693 * Store the event and trigger it later just before handling mouseUp.
2694 * This allows for the method to detect if mouse is used to drag zoom.
2696 fPendingMenuDetectEvent
= e
;
2699 fPendingMenuDetectEvent
= null;
2700 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2704 if (fDragState
!= DRAG_NONE
) {
2708 Point p
= toControl(e
.x
, e
.y
);
2709 int idx
= getItemIndexAtY(p
.y
);
2710 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2711 Item item
= fItemData
.fExpandedItems
[idx
];
2712 ITimeGraphEntry entry
= item
.fEntry
;
2713 if (entry
.hasTimeEvents()) {
2714 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2715 if (event
!= null) {
2717 fireMenuEventOnTimeEvent(e
);
2722 fireMenuEventOnTimeGraphEntry(e
);
2727 * Perform the alignment operation.
2730 * the alignment offset
2732 * @see ITmfTimeAligned
2736 public void performAlign(int offset
) {
2737 fTimeProvider
.setNameSpace(offset
);
2741 * Return the time alignment information
2743 * @return the time alignment information
2745 * @see ITmfTimeAligned
2749 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
2750 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());