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
.Iterator
;
26 import java
.util
.LinkedHashMap
;
27 import java
.util
.List
;
30 import org
.eclipse
.jface
.action
.IStatusLineManager
;
31 import org
.eclipse
.jface
.resource
.JFaceResources
;
32 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
33 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
34 import org
.eclipse
.jface
.viewers
.ISelection
;
35 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
36 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
37 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
38 import org
.eclipse
.osgi
.util
.NLS
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.events
.FocusEvent
;
41 import org
.eclipse
.swt
.events
.FocusListener
;
42 import org
.eclipse
.swt
.events
.KeyEvent
;
43 import org
.eclipse
.swt
.events
.KeyListener
;
44 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
45 import org
.eclipse
.swt
.events
.MenuDetectListener
;
46 import org
.eclipse
.swt
.events
.MouseEvent
;
47 import org
.eclipse
.swt
.events
.MouseListener
;
48 import org
.eclipse
.swt
.events
.MouseMoveListener
;
49 import org
.eclipse
.swt
.events
.MouseTrackListener
;
50 import org
.eclipse
.swt
.events
.MouseWheelListener
;
51 import org
.eclipse
.swt
.events
.PaintEvent
;
52 import org
.eclipse
.swt
.events
.SelectionListener
;
53 import org
.eclipse
.swt
.events
.TraverseEvent
;
54 import org
.eclipse
.swt
.events
.TraverseListener
;
55 import org
.eclipse
.swt
.events
.TypedEvent
;
56 import org
.eclipse
.swt
.graphics
.Color
;
57 import org
.eclipse
.swt
.graphics
.Cursor
;
58 import org
.eclipse
.swt
.graphics
.GC
;
59 import org
.eclipse
.swt
.graphics
.Image
;
60 import org
.eclipse
.swt
.graphics
.Point
;
61 import org
.eclipse
.swt
.graphics
.Rectangle
;
62 import org
.eclipse
.swt
.widgets
.Composite
;
63 import org
.eclipse
.swt
.widgets
.Display
;
64 import org
.eclipse
.swt
.widgets
.Event
;
65 import org
.eclipse
.swt
.widgets
.Listener
;
66 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
81 * Time graph control implementation
84 * @author Alvaro Sanchez-Leon
85 * @author Patrick Tasse
87 public class TimeGraphControl
extends TimeGraphBaseControl
88 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
89 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
90 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
92 /** Constant indicating that all levels of the time graph should be expanded
94 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
96 private static final int DRAG_NONE
= 0;
97 private static final int DRAG_TRACE_ITEM
= 1;
98 private static final int DRAG_SPLIT_LINE
= 2;
99 private static final int DRAG_ZOOM
= 3;
100 private static final int DRAG_SELECTION
= 4;
102 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
104 private static final double ZOOM_FACTOR
= 1.5;
105 private static final double ZOOM_IN_FACTOR
= 0.8;
106 private static final double ZOOM_OUT_FACTOR
= 1.25;
108 private static final int SNAP_WIDTH
= 2;
109 private static final int ARROW_HOVER_MAX_DIST
= 5;
111 private static final int NO_STATUS
= -1;
113 /** Resource manager */
114 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
116 /** Color map for event types */
117 private Color
[] fEventColorMap
= null;
119 private ITimeDataProvider fTimeProvider
;
120 private IStatusLineManager fStatusLineManager
= null;
121 private TimeGraphScale fTimeGraphScale
= null;
123 private boolean fIsInFocus
= false;
124 private boolean fMouseOverSplitLine
= false;
125 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
126 private int fMinimumItemWidth
= 0;
127 private int fTopIndex
= 0;
128 private int fDragState
= DRAG_NONE
;
129 private int fDragButton
;
130 private int fDragX0
= 0;
131 private int fDragX
= 0;
132 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
133 private int fIdealNameSpace
= 0;
134 private long fTime0bak
;
135 private long fTime1bak
;
136 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
137 private ItemData fItemData
= null;
138 private List
<SelectionListener
> fSelectionListeners
;
139 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
140 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
141 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
142 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
143 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
144 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
145 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
146 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
147 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
148 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
149 private MenuDetectEvent fPendingMenuDetectEvent
= null;
150 private boolean fHideArrows
= false;
151 private int fAutoExpandLevel
= ALL_LEVELS
;
153 private int fBorderWidth
= 0;
154 private int fHeaderHeight
= 0;
157 * Standard constructor
160 * The parent composite object
162 * The color scheme to use
164 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
166 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
168 fItemData
= new ItemData();
170 addFocusListener(this);
171 addMouseListener(this);
172 addMouseMoveListener(this);
173 addMouseTrackListener(this);
174 addMouseWheelListener(this);
175 addTraverseListener(this);
176 addKeyListener(this);
177 addMenuDetectListener(this);
178 addListener(SWT
.MouseWheel
, this);
182 public void dispose() {
184 fResourceManager
.dispose();
188 * Sets the timegraph provider used by this timegraph viewer.
190 * @param timeGraphProvider the timegraph provider
192 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
193 fTimeGraphProvider
= timeGraphProvider
;
195 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
196 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
197 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
200 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
201 colorSettingsChanged(stateItems
);
205 * Gets the timegraph provider used by this timegraph viewer.
207 * @return the timegraph provider, or <code>null</code> if not set.
210 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
211 return fTimeGraphProvider
;
215 * Gets the color map used by this timegraph viewer.
217 * @return a color map, or <code>null</code> if not set.
220 public Color
[] getEventColorMap() {
221 return fEventColorMap
;
225 * Assign the given time provider
227 * @param timeProvider
230 public void setTimeProvider(ITimeDataProvider timeProvider
) {
231 fTimeProvider
= timeProvider
;
236 * Assign the status line manager
238 * @param statusLineManager
239 * The status line manager, or null to disable status line messages
242 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
243 if (fStatusLineManager
!= null && statusLineManager
== null) {
244 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
246 fStatusLineManager
= statusLineManager
;
250 * Assign the time graph scale
252 * @param timeGraphScale
253 * The time graph scale
256 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
257 fTimeGraphScale
= timeGraphScale
;
261 * Add a selection listener
264 * The listener to add
266 public void addSelectionListener(SelectionListener listener
) {
267 if (listener
== null) {
268 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
270 if (null == fSelectionListeners
) {
271 fSelectionListeners
= new ArrayList
<>();
273 fSelectionListeners
.add(listener
);
277 * Remove a selection listener
280 * The listener to remove
282 public void removeSelectionListener(SelectionListener listener
) {
283 if (null != fSelectionListeners
) {
284 fSelectionListeners
.remove(listener
);
289 * Selection changed callback
291 public void fireSelectionChanged() {
292 if (null != fSelectionListeners
) {
293 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
294 while (it
.hasNext()) {
295 SelectionListener listener
= it
.next();
296 listener
.widgetSelected(null);
302 * Default selection callback
304 public void fireDefaultSelection() {
305 if (null != fSelectionListeners
) {
306 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
307 while (it
.hasNext()) {
308 SelectionListener listener
= it
.next();
309 listener
.widgetDefaultSelected(null);
315 * Add a drag selection listener
318 * The listener to add
321 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
322 if (listener
== null) {
323 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
325 if (null == fDragSelectionListeners
) {
326 fDragSelectionListeners
= new ArrayList
<>();
328 fDragSelectionListeners
.add(listener
);
332 * Remove a drag selection listener
335 * 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
353 public void fireDragSelectionChanged(long start
, long end
) {
354 // check for backward intervals
355 long beginTime
, endTime
;
363 // call the listeners
364 if (null != fDragSelectionListeners
) {
365 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
366 while (it
.hasNext()) {
367 ITimeGraphTimeListener listener
= it
.next();
368 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
374 * Get the traces in the model
376 * @return The array of traces
378 public ITimeGraphEntry
[] getTraces() {
379 return fItemData
.getEntries();
383 * Get the on/off trace filters
385 * @return The array of filters
387 public boolean[] getTraceFilter() {
388 return fItemData
.getEntryFilter();
392 * Refresh the data for the thing
394 public void refreshData() {
395 fItemData
.refreshData();
400 * Refresh data for the given traces
403 * The traces to refresh
405 public void refreshData(ITimeGraphEntry
[] traces
) {
406 fItemData
.refreshData(traces
);
411 * Refresh the links (arrows) of this widget
413 * @param events The link events to refresh
416 public void refreshArrows(List
<ILinkEvent
> events
) {
417 fItemData
.refreshArrows(events
);
420 boolean ensureVisibleItem(int idx
, boolean redraw
) {
421 boolean changed
= false;
424 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
425 if (fItemData
.fExpandedItems
[index
].fSelected
) {
430 if (index
>= fItemData
.fExpandedItems
.length
) {
433 if (index
< fTopIndex
) {
440 int page
= countPerPage();
441 if (index
>= fTopIndex
+ page
) {
442 setTopIndex(index
- page
+ 1);
453 * Assign the given index as the top one
458 public void setTopIndex(int idx
) {
459 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
460 index
= Math
.max(0, index
);
466 * Sets the auto-expand level to be used when the entries are refreshed
467 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
468 * The value 0 means that there is no auto-expand; 1 means that top-level
469 * entries are expanded, but not their children; 2 means that top-level
470 * entries are expanded, and their children, but not grand-children; and so
473 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
476 * non-negative level, or <code>ALL_LEVELS</code> to expand all
480 public void setAutoExpandLevel(int level
) {
481 fAutoExpandLevel
= level
;
485 * Returns the auto-expand level.
487 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
488 * the tree are expanded automatically
489 * @see #setAutoExpandLevel
492 public int getAutoExpandLevel() {
493 return fAutoExpandLevel
;
497 * Set the expanded state of a given entry
502 * True if expanded, false if collapsed
504 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
505 Item item
= fItemData
.findItem(entry
);
506 if (item
!= null && item
.fExpanded
!= expanded
) {
507 item
.fExpanded
= expanded
;
508 fItemData
.updateExpandedItems();
514 * Collapses all nodes of the viewer's tree, starting with the root.
518 public void collapseAll() {
519 for (Item item
: fItemData
.fItems
) {
520 item
.fExpanded
= false;
522 fItemData
.updateExpandedItems();
527 * Expands all nodes of the viewer's tree, starting with the root.
531 public void expandAll() {
532 for (Item item
: fItemData
.fItems
) {
533 item
.fExpanded
= true;
535 fItemData
.updateExpandedItems();
540 * Add a tree listener
543 * The listener to add
545 public void addTreeListener(ITimeGraphTreeListener listener
) {
546 if (!fTreeListeners
.contains(listener
)) {
547 fTreeListeners
.add(listener
);
552 * Remove a tree listener
555 * The listener to remove
557 public void removeTreeListener(ITimeGraphTreeListener listener
) {
558 if (fTreeListeners
.contains(listener
)) {
559 fTreeListeners
.remove(listener
);
564 * Tree event callback
569 * The expanded state (true for expanded, false for collapsed)
571 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
572 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
573 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
575 listener
.treeExpanded(event
);
577 listener
.treeCollapsed(event
);
583 * Add a menu listener on {@link ITimeGraphEntry}s
585 * The listener to add
588 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
589 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
590 fTimeGraphEntryMenuListeners
.add(listener
);
595 * Remove a menu listener on {@link ITimeGraphEntry}s
598 * The listener to remove
601 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
602 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
603 fTimeGraphEntryMenuListeners
.remove(listener
);
608 * Menu event callback on {@link ITimeGraphEntry}s
611 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
613 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
614 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
615 listener
.menuDetected(event
);
620 * Add a menu listener on {@link ITimeEvent}s
623 * The listener to add
626 public void addTimeEventMenuListener(MenuDetectListener listener
) {
627 if (!fTimeEventMenuListeners
.contains(listener
)) {
628 fTimeEventMenuListeners
.add(listener
);
633 * Remove a menu listener on {@link ITimeEvent}s
636 * 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 public void selectEvent(int n
) {
740 if (null == fTimeProvider
) {
743 ITimeGraphEntry trace
= getSelectedTrace();
747 long selectedTime
= fTimeProvider
.getSelectionBegin();
748 long endTime
= fTimeProvider
.getEndTime();
749 ITimeEvent nextEvent
;
750 if (-1 == n
&& selectedTime
> endTime
) {
751 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
753 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
755 if (null == nextEvent
&& -1 == n
) {
756 nextEvent
= Utils
.getFirstEvent(trace
);
758 if (null != nextEvent
) {
759 long nextTime
= nextEvent
.getTime();
760 // If last event detected e.g. going back or not moving to a next
762 if (nextTime
<= selectedTime
&& n
== 1) {
763 // Select to the end of this last event
764 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
765 // but not beyond the end of the trace
766 if (nextTime
> endTime
) {
769 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
770 // for previous event go to its end time unless we were already there
771 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
773 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
774 fireSelectionChanged();
776 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
777 fireSelectionChanged();
782 * Select the next event
784 public void selectNextEvent() {
786 // Notify if visible time window has been adjusted
787 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
791 * Select the previous event
793 public void selectPrevEvent() {
795 // Notify if visible time window has been adjusted
796 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
800 * Select the next trace
802 public void selectNextTrace() {
807 * Select the previous trace
809 public void selectPrevTrace() {
814 * Scroll left or right by one half window size
817 * true to scroll left, false to scroll right
821 public void horizontalScroll(boolean left
) {
822 long time0
= fTimeProvider
.getTime0();
823 long time1
= fTimeProvider
.getTime1();
824 long timeMin
= fTimeProvider
.getMinTime();
825 long timeMax
= fTimeProvider
.getMaxTime();
826 long range
= time1
- time0
;
830 long increment
= Math
.max(1, range
/ 2);
832 time0
= Math
.max(time0
- increment
, timeMin
);
833 time1
= time0
+ range
;
835 time1
= Math
.min(time1
+ increment
, timeMax
);
836 time0
= time1
- range
;
838 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
842 * Zoom based on mouse cursor location with mouse scrolling
844 * @param zoomIn true to zoom in, false to zoom out
846 public void zoom(boolean zoomIn
) {
847 int globalX
= getDisplay().getCursorLocation().x
;
848 Point p
= toControl(globalX
, 0);
849 int nameSpace
= fTimeProvider
.getNameSpace();
850 int timeSpace
= fTimeProvider
.getTimeSpace();
851 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
852 long time0
= fTimeProvider
.getTime0();
853 long time1
= fTimeProvider
.getTime1();
854 long interval
= time1
- time0
;
857 } // to allow getting out of single point interval
860 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
862 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
864 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
865 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
866 long newTime1
= newTime0
+ newInterval
;
867 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
871 * zoom in using single click
873 public void zoomIn() {
874 long prevTime0
= fTimeProvider
.getTime0();
875 long prevTime1
= fTimeProvider
.getTime1();
876 long prevRange
= prevTime1
- prevTime0
;
877 if (prevRange
== 0) {
880 ITimeDataProvider provider
= fTimeProvider
;
881 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
882 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
883 selTime
= (prevTime0
+ prevTime1
) / 2;
885 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
886 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
888 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
890 if (inaccuracy
> 0 && inaccuracy
< 100) {
891 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
895 long min
= fTimeProvider
.getMinTimeInterval();
896 if ((time1
- time0
) < min
) {
897 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
901 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
905 * zoom out using single click
907 public void zoomOut() {
908 long prevTime0
= fTimeProvider
.getTime0();
909 long prevTime1
= fTimeProvider
.getTime1();
910 ITimeDataProvider provider
= fTimeProvider
;
911 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
912 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
913 selTime
= (prevTime0
+ prevTime1
) / 2;
915 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
916 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
918 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
919 if (inaccuracy
> 0 && inaccuracy
< 100) {
920 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
924 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
930 * @param hideArrows true to hide arrows
934 public void hideArrows(boolean hideArrows
) {
935 fHideArrows
= hideArrows
;
939 * Follow the arrow forward
943 public void followArrowFwd() {
944 ITimeGraphEntry trace
= getSelectedTrace();
948 long selectedTime
= fTimeProvider
.getSelectionBegin();
949 for (ILinkEvent link
: fItemData
.fLinks
) {
950 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
951 selectItem(link
.getDestinationEntry(), false);
952 if (link
.getDuration() != 0) {
953 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
954 // Notify if visible time window has been adjusted
955 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
957 fireSelectionChanged();
965 * Follow the arrow backward
969 public void followArrowBwd() {
970 ITimeGraphEntry trace
= getSelectedTrace();
974 long selectedTime
= fTimeProvider
.getSelectionBegin();
975 for (ILinkEvent link
: fItemData
.fLinks
) {
976 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
977 selectItem(link
.getEntry(), false);
978 if (link
.getDuration() != 0) {
979 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
980 // Notify if visible time window has been adjusted
981 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
983 fireSelectionChanged();
991 * Return the currently selected trace
993 * @return The entry matching the trace
995 public ITimeGraphEntry
getSelectedTrace() {
996 ITimeGraphEntry trace
= null;
997 int idx
= getSelectedIndex();
999 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1005 * Retrieve the index of the currently selected item
1009 public int getSelectedIndex() {
1011 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1012 Item item
= fItemData
.fExpandedItems
[i
];
1013 if (item
.fSelected
) {
1021 boolean toggle(int idx
) {
1022 boolean toggled
= false;
1023 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1024 Item item
= fItemData
.fExpandedItems
[idx
];
1025 if (item
.fHasChildren
) {
1026 item
.fExpanded
= !item
.fExpanded
;
1027 fItemData
.updateExpandedItems();
1030 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1037 * Gets the index of the item at the given location.
1041 * @return the index of the item at the given location, of -1 if none.
1044 protected int getItemIndexAtY(int y
) {
1049 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1050 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1058 boolean isOverSplitLine(int x
) {
1059 if (x
< 0 || null == fTimeProvider
) {
1062 int nameWidth
= fTimeProvider
.getNameSpace();
1063 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1067 * Gets the {@link ITimeGraphEntry} at the given location.
1070 * a point in the widget
1071 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1075 protected ITimeGraphEntry
getEntry(Point pt
) {
1076 int idx
= getItemIndexAtY(pt
.y
);
1077 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1081 * Return the arrow event closest to the given point that is no further than
1082 * a maximum distance.
1085 * a point in the widget
1086 * @return The closest arrow event, or null if there is none close enough.
1089 protected ILinkEvent
getArrow(Point pt
) {
1093 ILinkEvent linkEvent
= null;
1094 double minDistance
= Double
.MAX_VALUE
;
1095 for (ILinkEvent event
: fItemData
.fLinks
) {
1096 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1100 int x2
= x1
+ rect
.width
;
1101 int y2
= y1
+ rect
.height
;
1102 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1103 if (minDistance
> d
) {
1109 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1119 public int getXForTime(long time
) {
1120 if (null == fTimeProvider
) {
1123 long time0
= fTimeProvider
.getTime0();
1124 long time1
= fTimeProvider
.getTime1();
1125 int width
= getSize().x
;
1126 int nameSpace
= fTimeProvider
.getNameSpace();
1127 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1128 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1136 public long getTimeAtX(int coord
) {
1137 if (null == fTimeProvider
) {
1141 Point size
= getSize();
1142 long time0
= fTimeProvider
.getTime0();
1143 long time1
= fTimeProvider
.getTime1();
1144 int nameWidth
= fTimeProvider
.getNameSpace();
1145 final int x
= coord
- nameWidth
;
1146 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1147 if (x
>= 0 && size
.x
>= nameWidth
) {
1148 if (time1
- time0
> timeWidth
) {
1149 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1150 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1152 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1153 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1159 void selectItem(int idx
, boolean addSelection
) {
1160 boolean changed
= false;
1162 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1163 Item item
= fItemData
.fExpandedItems
[idx
];
1164 changed
= !item
.fSelected
;
1165 item
.fSelected
= true;
1168 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1169 Item item
= fItemData
.fExpandedItems
[i
];
1170 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1173 item
.fSelected
= i
== idx
;
1176 changed
|= ensureVisibleItem(idx
, true);
1183 * Callback for item selection
1186 * The entry matching the trace
1187 * @param addSelection
1188 * If the selection is added or removed
1190 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1191 int idx
= fItemData
.findItemIndex(trace
);
1192 selectItem(idx
, addSelection
);
1196 * Retrieve the number of entries shown per page.
1200 public int countPerPage() {
1201 int height
= getSize().y
;
1204 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1205 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1206 if (ySum
>= height
) {
1211 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1212 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1213 if (ySum
>= height
) {
1222 * Get the index of the top element
1226 public int getTopIndex() {
1231 * Get the number of expanded items
1233 * @return The count of expanded items
1235 public int getExpandedElementCount() {
1236 return fItemData
.fExpandedItems
.length
;
1240 * Get an array of all expanded elements
1242 * @return The expanded elements
1244 public ITimeGraphEntry
[] getExpandedElements() {
1245 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1246 for (Item item
: fItemData
.fExpandedItems
) {
1247 elements
.add(item
.fEntry
);
1249 return elements
.toArray(new ITimeGraphEntry
[0]);
1252 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1253 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1255 rect
.width
= nameWidth
;
1259 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1260 int x
= bound
.x
+ nameWidth
;
1261 int width
= bound
.width
- x
;
1263 if (idx
>= fTopIndex
) {
1264 for (int i
= fTopIndex
; i
< idx
; i
++) {
1265 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1268 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1269 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1272 int y
= bound
.y
+ ySum
;
1273 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1274 return new Rectangle(x
, y
, width
, height
);
1278 void paint(Rectangle bounds
, PaintEvent e
) {
1280 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1281 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1283 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1287 fIdealNameSpace
= 0;
1288 int nameSpace
= fTimeProvider
.getNameSpace();
1290 // draw empty name space background
1291 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1292 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1295 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1296 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1297 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1299 int alpha
= gc
.getAlpha();
1302 long time0
= fTimeProvider
.getTime0();
1303 long time1
= fTimeProvider
.getTime1();
1304 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1305 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1306 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1307 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1308 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1310 // draw selection lines
1311 if (fDragState
!= DRAG_SELECTION
) {
1312 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1313 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1314 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1317 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1318 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1323 // draw selection background
1324 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1325 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1326 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1327 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1329 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1330 } else if (x0
- x1
> 1) {
1331 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1335 // draw drag selection background
1336 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1337 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1338 if (fDragX0
< fDragX
) {
1339 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1340 } else if (fDragX0
> fDragX
) {
1341 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1346 if (DRAG_SPLIT_LINE
== fDragState
) {
1347 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1348 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1349 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1350 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1351 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1352 if (fDragX
!= fDragX0
) {
1353 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1355 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1356 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1357 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1358 if (fDragX
!= fDragX0
) {
1359 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1361 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1362 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1363 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1370 * Draw many items at once
1373 * The rectangle of the area
1374 * @param timeProvider
1377 * The array items to draw
1379 * The index of the first element to draw
1381 * The width reserved for the names
1383 * Reference to the SWT GC object
1385 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1386 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1387 for (int i
= topIndex
; i
< items
.length
; i
++) {
1388 Item item
= items
[i
];
1389 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1396 * @param item the item to draw
1397 * @param bounds the container rectangle
1398 * @param timeProvider Time provider
1399 * @param i the item index
1400 * @param nameSpace the name space
1401 * @param gc Graphics context
1403 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1404 ITimeGraphEntry entry
= item
.fEntry
;
1405 long time0
= timeProvider
.getTime0();
1406 long time1
= timeProvider
.getTime1();
1407 long selectedTime
= fTimeProvider
.getSelectionBegin();
1409 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1410 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1414 if (! item
.fEntry
.hasTimeEvents()) {
1415 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1416 nameRect
.width
+= statesRect
.width
;
1417 drawName(item
, nameRect
, gc
);
1419 drawName(item
, nameRect
, gc
);
1421 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1422 if (rect
.isEmpty()) {
1423 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1426 if (time1
<= time0
) {
1427 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1428 gc
.fillRectangle(rect
);
1429 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1433 // Initialize _rect1 to same values as enclosing rectangle rect
1434 Rectangle stateRect
= Utils
.clone(rect
);
1435 boolean selected
= item
.fSelected
;
1436 // K pixels per second
1437 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1439 if (item
.fEntry
.hasTimeEvents()) {
1440 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1441 fillSpace(rect
, gc
, selected
);
1442 // Drawing rectangle is smaller than reserved space
1444 stateRect
.height
-= 6;
1446 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1447 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1450 while (iterator
.hasNext()) {
1451 ITimeEvent event
= iterator
.next();
1452 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1453 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1454 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1455 // event is out of bounds
1458 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1459 stateRect
.x
= Math
.max(rect
.x
, x
);
1460 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1461 if (stateRect
.x
== lastX
) {
1462 stateRect
.width
-= 1;
1463 if (stateRect
.width
> 0) {
1464 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1465 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1469 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1470 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1474 gc
.setClipping((Rectangle
) null);
1476 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1483 * The rectangle of the area
1484 * @param timeProvider
1487 * The array items to draw
1489 * The width reserved for the names
1491 * Reference to the SWT GC object
1494 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1495 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1499 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1500 for (ILinkEvent event
: links
) {
1501 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1503 gc
.setClipping((Rectangle
) null);
1507 * Draws the link type events of this item
1512 * the container rectangle
1513 * @param timeProvider
1521 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1522 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1525 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1526 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1527 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1529 if ((srcIndex
== -1) || (destIndex
== -1)) {
1533 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1534 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1536 int x0
= getXForTime(event
.getTime());
1537 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1539 // limit the x-coordinates to prevent integer overflow in calculations
1540 // and also GC.drawLine doesn't draw properly with large coordinates
1541 final int limit
= Integer
.MAX_VALUE
/ 1024;
1542 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1543 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1545 int y0
= src
.y
+ src
.height
/ 2;
1546 int y1
= dst
.y
+ dst
.height
/ 2;
1547 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1551 * Draw the state (color fill)
1556 * Time event for which we're drawing the state
1561 * @return true if the state was drawn
1564 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1565 Rectangle rect
, GC gc
) {
1570 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1574 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1577 Color stateColor
= null;
1578 if (colorIdx
< fEventColorMap
.length
) {
1579 stateColor
= fEventColorMap
[colorIdx
];
1581 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1584 gc
.setForeground(stateColor
);
1585 gc
.setBackground(stateColor
);
1587 /* Draw the arrow */
1588 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1589 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1592 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1597 * @author Francis Giraldeau
1600 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1602 * The algorithm was taken from this site, not the code itself
1604 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1607 double cos
= 0.9510;
1608 double sin
= 0.3090;
1609 long lenx
= x1
- x0
;
1610 long leny
= y1
- y0
;
1611 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1613 double dx
= factor
* lenx
/ len
;
1614 double dy
= factor
* leny
/ len
;
1615 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1616 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1617 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1618 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1619 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1620 gc
.fillPolygon(arrow
);
1624 * Draw the name of an item.
1629 * Where to draw the name
1633 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1634 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1635 if (! hasTimeEvents
) {
1636 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1637 gc
.fillRectangle(bounds
);
1638 if (item
.fSelected
&& fIsInFocus
) {
1639 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1640 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1643 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1644 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1645 gc
.fillRectangle(bounds
);
1648 // No name to be drawn
1649 if (fTimeProvider
.getNameSpace() == 0) {
1653 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1654 if (item
.fHasChildren
) {
1655 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1656 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1657 Rectangle rect
= Utils
.clone(bounds
);
1658 rect
.x
+= leftMargin
;
1659 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1660 rect
.width
= EXPAND_SIZE
;
1661 rect
.height
= EXPAND_SIZE
;
1662 gc
.fillRectangle(rect
);
1663 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1664 int midy
= rect
.y
+ rect
.height
/ 2;
1665 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1666 if (!item
.fExpanded
) {
1667 int midx
= rect
.x
+ rect
.width
/ 2;
1668 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1671 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1673 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1676 int imgHeight
= img
.getImageData().height
;
1677 int imgWidth
= img
.getImageData().width
;
1679 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1680 gc
.drawImage(img
, x
, y
);
1681 leftMargin
+= imgWidth
+ MARGIN
;
1683 String name
= item
.fName
;
1684 Point size
= gc
.stringExtent(name
);
1685 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1686 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1688 if (hasTimeEvents
) {
1689 // cut long string with "..."
1690 int width
= bounds
.width
- leftMargin
;
1692 while (size
.x
> width
&& name
.length() > 1) {
1694 name
= name
.substring(0, name
.length() - 1);
1695 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1698 name
+= "..."; //$NON-NLS-1$
1701 Rectangle rect
= Utils
.clone(bounds
);
1702 rect
.x
+= leftMargin
;
1703 rect
.width
-= leftMargin
;
1705 if (rect
.width
> 0) {
1706 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1707 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1708 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1709 leftMargin
+= textWidth
+ MARGIN
;
1712 if (hasTimeEvents
) {
1714 int x
= bounds
.x
+ leftMargin
;
1715 int width
= bounds
.width
- x
;
1716 int midy
= bounds
.y
+ bounds
.height
/ 2;
1717 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1718 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1724 * Draw the state (color fill)
1729 * Time event for which we're drawing the state
1735 * Is this time event currently selected (so it appears
1737 * @param timeSelected
1738 * Is the timestamp currently selected
1739 * @return true if the state was drawn
1742 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1743 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1745 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1746 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1749 boolean visible
= rect
.width
== 0 ?
false : true;
1750 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1751 gc
.setForeground(black
);
1754 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1755 // Only draw the top and bottom borders
1756 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1757 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1758 if (rect
.width
== 1) {
1759 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1761 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1764 Color stateColor
= null;
1765 if (colorIdx
< fEventColorMap
.length
) {
1766 stateColor
= fEventColorMap
[colorIdx
];
1771 boolean reallySelected
= timeSelected
&& selected
;
1772 // fill all rect area
1773 gc
.setBackground(stateColor
);
1774 gc
.fillRectangle(rect
);
1776 if (reallySelected
) {
1777 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1778 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1781 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1783 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1788 * Fill the space between two contiguous time events
1795 * Is this time event selected or not
1797 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1798 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1799 gc
.fillRectangle(rect
);
1800 if (fDragState
== DRAG_ZOOM
) {
1801 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1802 if (fDragX0
< fDragX
) {
1803 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1804 } else if (fDragX0
> fDragX
) {
1805 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1809 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1810 int midy
= rect
.y
+ rect
.height
/ 2;
1811 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1815 public void keyTraversed(TraverseEvent e
) {
1816 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1822 public void keyPressed(KeyEvent e
) {
1824 if (fItemData
.fExpandedItems
.length
== 0) {
1827 if (SWT
.HOME
== e
.keyCode
) {
1829 } else if (SWT
.END
== e
.keyCode
) {
1830 idx
= fItemData
.fExpandedItems
.length
- 1;
1831 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1832 idx
= getSelectedIndex();
1835 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1838 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1839 idx
= getSelectedIndex();
1842 } else if (idx
> 0) {
1845 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1847 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1849 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1850 int page
= countPerPage();
1851 idx
= getSelectedIndex();
1856 if (idx
>= fItemData
.fExpandedItems
.length
) {
1857 idx
= fItemData
.fExpandedItems
.length
- 1;
1859 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1860 int page
= countPerPage();
1861 idx
= getSelectedIndex();
1869 } else if (SWT
.CR
== e
.keyCode
) {
1870 idx
= getSelectedIndex();
1872 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1875 fireDefaultSelection();
1881 selectItem(idx
, false);
1882 fireSelectionChanged();
1884 int x
= toControl(e
.display
.getCursorLocation()).x
;
1885 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1889 public void keyReleased(KeyEvent e
) {
1890 int x
= toControl(e
.display
.getCursorLocation()).x
;
1891 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1895 public void focusGained(FocusEvent e
) {
1898 updateStatusLine(NO_STATUS
);
1902 public void focusLost(FocusEvent e
) {
1904 if (DRAG_NONE
!= fDragState
) {
1906 fDragState
= DRAG_NONE
;
1909 updateStatusLine(NO_STATUS
);
1913 * @return If the current view is focused
1915 public boolean isInFocus() {
1920 * Provide the possibility to control the wait cursor externally e.g. data
1921 * requests in progress
1923 * @param waitInd Should we wait indefinitely?
1925 public void waitCursor(boolean waitInd
) {
1926 // Update cursor as indicated
1928 setCursor(fWaitCursor
);
1934 private void updateCursor(int x
, int stateMask
) {
1935 // if Wait cursor not active, check for the need to change the cursor
1936 if (getCursor() == fWaitCursor
) {
1939 Cursor cursor
= null;
1940 if (fDragState
== DRAG_SPLIT_LINE
) {
1941 } else if (fDragState
== DRAG_SELECTION
) {
1942 cursor
= fResizeCursor
;
1943 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1944 cursor
= fDragCursor
;
1945 } else if (fDragState
== DRAG_ZOOM
) {
1946 cursor
= fZoomCursor
;
1947 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1948 cursor
= fDragCursor
;
1949 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1950 cursor
= fResizeCursor
;
1951 } else if (!isOverSplitLine(x
)) {
1952 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1953 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1954 int xBegin
= getXForTime(selectionBegin
);
1955 int xEnd
= getXForTime(selectionEnd
);
1956 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1957 cursor
= fResizeCursor
;
1960 if (getCursor() != cursor
) {
1965 private void updateStatusLine(int x
) {
1966 // use the time provider of the time graph scale for the status line
1967 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
1968 if (fStatusLineManager
== null || null == tdp
||
1969 tdp
.getTime0() == tdp
.getTime1()) {
1972 TimeFormat tf
= tdp
.getTimeFormat();
1973 Resolution res
= Resolution
.NANOSEC
;
1974 StringBuilder message
= new StringBuilder();
1975 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1976 long time
= getTimeAtX(x
);
1978 if (tdp
instanceof ITimeDataProviderConverter
) {
1979 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
1981 long selectionBegin
= tdp
.getSelectionBegin();
1982 long selectionEnd
= tdp
.getSelectionEnd();
1983 message
.append(NLS
.bind("T: {0}{1} T1: {2}{3}", //$NON-NLS-1$
1985 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
1986 Utils
.formatTime(time
, tf
, res
),
1987 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
1988 Utils
.formatTime(Math
.min(selectionBegin
, selectionEnd
), tf
, res
)
1990 if (selectionBegin
!= selectionEnd
) {
1991 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
1993 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
1994 Utils
.formatTime(Math
.max(selectionBegin
, selectionEnd
), tf
, res
),
1995 Utils
.formatDelta(Math
.abs(selectionBegin
- selectionEnd
), tf
, res
)
1999 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2000 long time0
= fDragTime0
;
2001 long time
= getTimeAtX(fDragX
);
2002 if (tdp
instanceof ITimeDataProviderConverter
) {
2003 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2004 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2006 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2008 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2009 Utils
.formatTime(Math
.min(time
, time0
), tf
, res
),
2010 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2011 Utils
.formatTime(Math
.max(time
, time0
), tf
, res
),
2012 Utils
.formatDelta(Math
.abs(time
- time0
), tf
, res
)
2015 fStatusLineManager
.setMessage(message
.toString());
2019 public void mouseMove(MouseEvent e
) {
2020 if (null == fTimeProvider
) {
2023 Point size
= getSize();
2024 if (DRAG_TRACE_ITEM
== fDragState
) {
2025 int nameWidth
= fTimeProvider
.getNameSpace();
2026 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2028 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2029 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2030 long time1
= fTime1bak
- timeDelta
;
2031 long maxTime
= fTimeProvider
.getMaxTime();
2032 if (time1
> maxTime
) {
2035 long time0
= time1
- (fTime1bak
- fTime0bak
);
2036 if (time0
< fTimeProvider
.getMinTime()) {
2037 time0
= fTimeProvider
.getMinTime();
2038 time1
= time0
+ (fTime1bak
- fTime0bak
);
2040 fTimeProvider
.setStartFinishTime(time0
, time1
);
2042 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2044 fTimeProvider
.setNameSpace(e
.x
);
2045 } else if (DRAG_SELECTION
== fDragState
) {
2046 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2048 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2049 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2050 } else if (DRAG_ZOOM
== fDragState
) {
2051 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2053 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2054 } else if (DRAG_NONE
== fDragState
) {
2055 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2056 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2059 fMouseOverSplitLine
= mouseOverSplitLine
;
2061 updateCursor(e
.x
, e
.stateMask
);
2062 updateStatusLine(e
.x
);
2066 public void mouseDoubleClick(MouseEvent e
) {
2067 if (null == fTimeProvider
) {
2070 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2071 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2072 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2073 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2074 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2077 fMouseOverSplitLine
= mouseOverSplitLine
;
2080 int idx
= getItemIndexAtY(e
.y
);
2082 selectItem(idx
, false);
2083 fireDefaultSelection();
2089 public void mouseDown(MouseEvent e
) {
2090 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2091 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2092 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2096 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2097 int nameSpace
= fTimeProvider
.getNameSpace();
2098 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2099 fDragState
= DRAG_SPLIT_LINE
;
2100 fDragButton
= e
.button
;
2103 fTime0bak
= fTimeProvider
.getTime0();
2104 fTime1bak
= fTimeProvider
.getTime1();
2106 updateCursor(e
.x
, e
.stateMask
);
2110 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2111 int nameSpace
= fTimeProvider
.getNameSpace();
2112 idx
= getItemIndexAtY(e
.y
);
2114 Item item
= fItemData
.fExpandedItems
[idx
];
2115 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2119 selectItem(idx
, false);
2120 fireSelectionChanged();
2122 selectItem(idx
, false); // clear selection
2123 fireSelectionChanged();
2125 long hitTime
= getTimeAtX(e
.x
);
2129 fDragState
= DRAG_SELECTION
;
2130 fDragButton
= e
.button
;
2133 fDragTime0
= getTimeAtX(fDragX0
);
2134 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2135 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2136 int xBegin
= getXForTime(selectionBegin
);
2137 int xEnd
= getXForTime(selectionEnd
);
2138 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2139 long time
= getTimeAtX(e
.x
);
2140 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2142 fDragTime0
= selectionEnd
;
2145 fDragTime0
= selectionBegin
;
2148 long time
= getTimeAtX(e
.x
);
2149 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2151 fDragTime0
= selectionEnd
;
2152 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2154 fDragTime0
= selectionBegin
;
2157 fTime0bak
= fTimeProvider
.getTime0();
2158 fTime1bak
= fTimeProvider
.getTime1();
2160 updateCursor(e
.x
, e
.stateMask
);
2161 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2163 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2164 long hitTime
= getTimeAtX(e
.x
);
2167 fDragState
= DRAG_TRACE_ITEM
;
2168 fDragButton
= e
.button
;
2171 fTime0bak
= fTimeProvider
.getTime0();
2172 fTime1bak
= fTimeProvider
.getTime1();
2173 updateCursor(e
.x
, e
.stateMask
);
2175 } else if (3 == e
.button
) {
2177 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2179 fDragTime0
= getTimeAtX(fDragX0
);
2180 fDragState
= DRAG_ZOOM
;
2181 fDragButton
= e
.button
;
2183 updateCursor(e
.x
, e
.stateMask
);
2184 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2189 public void mouseUp(MouseEvent e
) {
2190 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2191 menuDetected(fPendingMenuDetectEvent
);
2193 if (DRAG_NONE
!= fDragState
) {
2195 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2196 if (fDragX
!= fDragX0
) {
2197 fTimeProvider
.notifyStartFinishTime();
2199 fDragState
= DRAG_NONE
;
2200 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2201 fDragState
= DRAG_NONE
;
2203 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2204 if (fDragX
== fDragX0
) { // click without selecting anything
2205 long time
= getTimeAtX(e
.x
);
2206 fTimeProvider
.setSelectedTimeNotify(time
, false);
2208 long time0
= fDragTime0
;
2209 long time1
= getTimeAtX(fDragX
);
2210 if (time0
<= time1
) {
2211 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2213 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2216 fDragState
= DRAG_NONE
;
2218 fTimeGraphScale
.setDragRange(-1, -1);
2219 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2220 int nameWidth
= fTimeProvider
.getNameSpace();
2221 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2222 long time0
= getTimeAtX(fDragX0
);
2223 long time1
= getTimeAtX(fDragX
);
2224 if (time0
< time1
) {
2225 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2227 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2232 fDragState
= DRAG_NONE
;
2233 fTimeGraphScale
.setDragRange(-1, -1);
2236 updateCursor(e
.x
, e
.stateMask
);
2237 updateStatusLine(e
.x
);
2241 public void mouseEnter(MouseEvent e
) {
2245 public void mouseExit(MouseEvent e
) {
2246 if (fMouseOverSplitLine
) {
2247 fMouseOverSplitLine
= false;
2250 updateStatusLine(NO_STATUS
);
2254 public void mouseHover(MouseEvent e
) {
2258 public void mouseScrolled(MouseEvent e
) {
2259 if (fDragState
!= DRAG_NONE
) {
2262 boolean zoomScroll
= false;
2263 boolean horizontalScroll
= false;
2264 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2265 Point parentSize
= getParent().getSize();
2266 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2267 // over the parent control
2268 if (e
.x
> getSize().x
) {
2269 // over the vertical scroll bar
2271 } else if (e
.y
< 0) {
2272 // over the time scale
2274 } else if (e
.y
>= getSize().y
) {
2275 // over the horizontal scroll bar
2276 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2279 horizontalScroll
= true;
2282 if (e
.x
< fTimeProvider
.getNameSpace()) {
2283 // over the name space
2286 // over the state area
2287 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2288 // over the state area, CTRL pressed
2291 // over the state area, CTRL not pressed
2297 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2300 } else if (e
.count
< 0) {
2303 } else if (horizontalScroll
) {
2304 horizontalScroll(e
.count
> 0);
2306 setTopIndex(getTopIndex() - e
.count
);
2311 public void handleEvent(Event event
) {
2312 if (event
.type
== SWT
.MouseWheel
) {
2313 // prevent horizontal scrolling when the mouse wheel is used to
2314 // scroll vertically or zoom
2320 public int getBorderWidth() {
2321 return fBorderWidth
;
2325 * Set the border width
2327 * @param borderWidth
2330 public void setBorderWidth(int borderWidth
) {
2331 this.fBorderWidth
= borderWidth
;
2335 * @return The current height of the header row
2337 public int getHeaderHeight() {
2338 return fHeaderHeight
;
2342 * Set the height of the header row
2344 * @param headerHeight
2347 public void setHeaderHeight(int headerHeight
) {
2348 this.fHeaderHeight
= headerHeight
;
2352 * @return The default height of regular item rows
2354 public int getItemHeight() {
2355 return fGlobalItemHeight
;
2359 * Set the default height of regular item rows.
2364 public void setItemHeight(int rowHeight
) {
2365 this.fGlobalItemHeight
= rowHeight
;
2369 * Set the height of a specific item. Overrides the default item height.
2372 * A time graph entry
2375 * @return true if the height is successfully stored, false otherwise
2379 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2380 Item item
= fItemData
.findItem(entry
);
2382 item
.fItemHeight
= rowHeight
;
2389 * Set the minimum item width
2391 * @param width The minimum width
2393 public void setMinimumItemWidth(int width
) {
2394 this.fMinimumItemWidth
= width
;
2398 * @return The minimum item width
2400 public int getMinimumItemWidth() {
2401 return fMinimumItemWidth
;
2405 * @return The entries that are currently filtered out
2409 public List
<ITimeGraphEntry
> getFilteredOut() {
2410 return fItemData
.getFilteredOut();
2414 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2415 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2416 fSelectionChangedListeners
.add(listener
);
2421 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2422 if (listener
!= null) {
2423 fSelectionChangedListeners
.remove(listener
);
2428 public void setSelection(ISelection selection
) {
2429 if (selection
instanceof TimeGraphSelection
) {
2430 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2431 Object ob
= sel
.getFirstElement();
2432 if (ob
instanceof ITimeGraphEntry
) {
2433 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2434 selectItem(trace
, false);
2441 * @param filter The filter object to be attached to the view
2444 public void addFilter(ViewerFilter filter
) {
2445 if (!fFilters
.contains(filter
)) {
2446 fFilters
.add(filter
);
2451 * @param filter The filter object to be attached to the view
2454 public void removeFilter(ViewerFilter filter
) {
2455 fFilters
.remove(filter
);
2462 public void colorSettingsChanged(StateItem
[] stateItems
) {
2463 /* Destroy previous colors from the resource manager */
2464 if (fEventColorMap
!= null) {
2465 for (Color color
: fEventColorMap
) {
2466 fResourceManager
.destroyColor(color
.getRGB());
2469 if (stateItems
!= null) {
2470 fEventColorMap
= new Color
[stateItems
.length
];
2471 for (int i
= 0; i
< stateItems
.length
; i
++) {
2472 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2475 fEventColorMap
= new Color
[] { };
2480 private class ItemData
{
2481 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2482 private Item
[] fExpandedItems
= new Item
[0];
2483 private Item
[] fItems
= new Item
[0];
2484 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2485 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2486 private boolean fEntryFilter
[] = new boolean[0];
2487 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2492 public Item
findItem(ITimeGraphEntry entry
) {
2493 return fItemMap
.get(entry
);
2496 public int findItemIndex(ITimeGraphEntry entry
) {
2497 Item item
= fItemMap
.get(entry
);
2501 return item
.fExpandedIndex
;
2504 public void refreshData() {
2506 fFilteredOut
.clear();
2507 ITimeGraphEntry selection
= getSelectedTrace();
2508 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2509 ITimeGraphEntry entry
= fRootEntries
[i
];
2510 refreshData(fItemMap
, null, 0, entry
);
2512 fItems
= fItemMap
.values().toArray(new Item
[0]);
2513 updateExpandedItems();
2514 if (selection
!= null) {
2515 for (Item item
: fExpandedItems
) {
2516 if (item
.fEntry
== selection
) {
2517 item
.fSelected
= true;
2524 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2525 Item item
= new Item(entry
, entry
.getName(), level
);
2526 if (parent
!= null) {
2527 parent
.fChildren
.add(item
);
2529 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2530 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2532 item
.fItemHeight
= fGlobalItemHeight
;
2534 itemMap
.put(entry
, item
);
2535 if (entry
.hasChildren()) {
2536 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2537 item
.fHasChildren
= true;
2538 for (ITimeGraphEntry child
: entry
.getChildren()) {
2539 refreshData(itemMap
, item
, level
+ 1, child
);
2544 public void updateExpandedItems() {
2545 for (Item item
: fItems
) {
2546 item
.fExpandedIndex
= -1;
2548 List
<Item
> expandedItemList
= new ArrayList
<>();
2549 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2550 ITimeGraphEntry entry
= fRootEntries
[i
];
2551 Item item
= findItem(entry
);
2552 refreshExpanded(expandedItemList
, item
);
2554 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2555 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2558 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2559 // Check for filters
2560 boolean display
= true;
2561 for (ViewerFilter filter
: fFilters
) {
2562 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2568 item
.fExpandedIndex
= expandedItemList
.size();
2569 expandedItemList
.add(item
);
2570 if (item
.fHasChildren
&& item
.fExpanded
) {
2571 for (Item child
: item
.fChildren
) {
2572 refreshExpanded(expandedItemList
, child
);
2578 public void refreshData(ITimeGraphEntry
[] entries
) {
2579 if (entries
== null) {
2580 fEntryFilter
= null;
2581 fRootEntries
= null;
2583 if (entries
.length
== 0) {
2584 fEntryFilter
= null;
2585 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2586 fEntryFilter
= new boolean[entries
.length
];
2587 java
.util
.Arrays
.fill(fEntryFilter
, true);
2589 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2595 public void refreshArrows(List
<ILinkEvent
> events
) {
2596 /* If links are null, reset the list */
2597 if (events
!= null) {
2600 fLinks
= new ArrayList
<>();
2604 public ITimeGraphEntry
[] getEntries() {
2605 return fRootEntries
;
2608 public boolean[] getEntryFilter() {
2609 return fEntryFilter
;
2612 public List
<ITimeGraphEntry
> getFilteredOut() {
2613 return fFilteredOut
;
2617 private class Item
{
2618 private boolean fExpanded
;
2619 private int fExpandedIndex
;
2620 private boolean fSelected
;
2621 private boolean fHasChildren
;
2622 private int fItemHeight
;
2623 private final int fLevel
;
2624 private final List
<Item
> fChildren
;
2625 private final String fName
;
2626 private final ITimeGraphEntry fEntry
;
2628 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2629 this.fEntry
= entry
;
2631 this.fLevel
= level
;
2632 this.fChildren
= new ArrayList
<>();
2636 public String
toString() {
2645 public void menuDetected(MenuDetectEvent e
) {
2646 if (null == fTimeProvider
) {
2649 if (e
.detail
== SWT
.MENU_MOUSE
) {
2650 if (fPendingMenuDetectEvent
== null) {
2651 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2652 * Store the event and trigger it later just before handling mouseUp.
2653 * This allows for the method to detect if mouse is used to drag zoom.
2655 fPendingMenuDetectEvent
= e
;
2658 fPendingMenuDetectEvent
= null;
2659 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2663 if (fDragState
!= DRAG_NONE
) {
2667 Point p
= toControl(e
.x
, e
.y
);
2668 int idx
= getItemIndexAtY(p
.y
);
2669 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2670 Item item
= fItemData
.fExpandedItems
[idx
];
2671 ITimeGraphEntry entry
= item
.fEntry
;
2672 if (entry
.hasTimeEvents()) {
2673 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2674 if (event
!= null) {
2676 fireMenuEventOnTimeEvent(e
);
2681 fireMenuEventOnTimeGraphEntry(e
);