1 /*****************************************************************************
2 * Copyright (c) 2007, 2014 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;
156 private MouseScrollNotifier fMouseScrollNotifier
;
157 private final Object fMouseScrollNotifierLock
= new Object();
159 private class MouseScrollNotifier
extends Thread
{
160 private static final long DELAY
= 400L;
161 private static final long POLLING_INTERVAL
= 10L;
162 private long fLastScrollTime
= Long
.MAX_VALUE
;
166 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
168 Thread
.sleep(POLLING_INTERVAL
);
169 } catch (Exception e
) {
173 if (!isInterrupted()) {
174 Display
.getDefault().asyncExec(new Runnable() {
180 fTimeProvider
.notifyStartFinishTime();
184 synchronized (fMouseScrollNotifierLock
) {
185 fMouseScrollNotifier
= null;
189 public void mouseScrolled() {
190 fLastScrollTime
= System
.currentTimeMillis();
195 * Standard constructor
198 * The parent composite object
200 * The color scheme to use
202 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
204 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
206 fItemData
= new ItemData();
208 addFocusListener(this);
209 addMouseListener(this);
210 addMouseMoveListener(this);
211 addMouseTrackListener(this);
212 addMouseWheelListener(this);
213 addTraverseListener(this);
214 addKeyListener(this);
215 addMenuDetectListener(this);
216 addListener(SWT
.MouseWheel
, this);
220 public void dispose() {
222 fResourceManager
.dispose();
226 * Sets the timegraph provider used by this timegraph viewer.
228 * @param timeGraphProvider the timegraph provider
230 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
231 fTimeGraphProvider
= timeGraphProvider
;
233 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
234 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
235 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
238 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
239 colorSettingsChanged(stateItems
);
243 * Gets the timegraph provider used by this timegraph viewer.
245 * @return the timegraph provider, or <code>null</code> if not set.
248 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
249 return fTimeGraphProvider
;
253 * Gets the color map used by this timegraph viewer.
255 * @return a color map, or <code>null</code> if not set.
258 public Color
[] getEventColorMap() {
259 return fEventColorMap
;
263 * Assign the given time provider
265 * @param timeProvider
268 public void setTimeProvider(ITimeDataProvider timeProvider
) {
269 fTimeProvider
= timeProvider
;
274 * Assign the status line manager
276 * @param statusLineManager
277 * The status line manager, or null to disable status line messages
280 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
281 if (fStatusLineManager
!= null && statusLineManager
== null) {
282 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
284 fStatusLineManager
= statusLineManager
;
288 * Assign the time graph scale
290 * @param timeGraphScale
291 * The time graph scale
294 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
295 fTimeGraphScale
= timeGraphScale
;
299 * Add a selection listener
302 * The listener to add
304 public void addSelectionListener(SelectionListener listener
) {
305 if (listener
== null) {
306 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
308 if (null == fSelectionListeners
) {
309 fSelectionListeners
= new ArrayList
<>();
311 fSelectionListeners
.add(listener
);
315 * Remove a selection listener
318 * The listener to remove
320 public void removeSelectionListener(SelectionListener listener
) {
321 if (null != fSelectionListeners
) {
322 fSelectionListeners
.remove(listener
);
327 * Selection changed callback
329 public void fireSelectionChanged() {
330 if (null != fSelectionListeners
) {
331 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
332 while (it
.hasNext()) {
333 SelectionListener listener
= it
.next();
334 listener
.widgetSelected(null);
340 * Default selection callback
342 public void fireDefaultSelection() {
343 if (null != fSelectionListeners
) {
344 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
345 while (it
.hasNext()) {
346 SelectionListener listener
= it
.next();
347 listener
.widgetDefaultSelected(null);
353 * Add a drag selection listener
356 * The listener to add
359 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
360 if (listener
== null) {
361 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
363 if (null == fDragSelectionListeners
) {
364 fDragSelectionListeners
= new ArrayList
<>();
366 fDragSelectionListeners
.add(listener
);
370 * Remove a drag selection listener
373 * The listener to remove
376 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
377 if (null != fDragSelectionListeners
) {
378 fDragSelectionListeners
.remove(listener
);
383 * Drag Selection changed callback
386 * Time interval start
391 public void fireDragSelectionChanged(long start
, long end
) {
392 // check for backward intervals
393 long beginTime
, endTime
;
401 // call the listeners
402 if (null != fDragSelectionListeners
) {
403 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
404 while (it
.hasNext()) {
405 ITimeGraphTimeListener listener
= it
.next();
406 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
412 * Get the traces in the model
414 * @return The array of traces
416 public ITimeGraphEntry
[] getTraces() {
417 return fItemData
.getEntries();
421 * Get the on/off trace filters
423 * @return The array of filters
425 public boolean[] getTraceFilter() {
426 return fItemData
.getEntryFilter();
430 * Refresh the data for the thing
432 public void refreshData() {
433 fItemData
.refreshData();
438 * Refresh data for the given traces
441 * The traces to refresh
443 public void refreshData(ITimeGraphEntry
[] traces
) {
444 fItemData
.refreshData(traces
);
449 * Refresh the links (arrows) of this widget
451 * @param events The link events to refresh
454 public void refreshArrows(List
<ILinkEvent
> events
) {
455 fItemData
.refreshArrows(events
);
458 boolean ensureVisibleItem(int idx
, boolean redraw
) {
459 boolean changed
= false;
462 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
463 if (fItemData
.fExpandedItems
[index
].fSelected
) {
468 if (index
>= fItemData
.fExpandedItems
.length
) {
471 if (index
< fTopIndex
) {
478 int page
= countPerPage();
479 if (index
>= fTopIndex
+ page
) {
480 setTopIndex(index
- page
+ 1);
491 * Assign the given index as the top one
496 public void setTopIndex(int idx
) {
497 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
498 index
= Math
.max(0, index
);
504 * Sets the auto-expand level to be used when the entries are refreshed
505 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
506 * The value 0 means that there is no auto-expand; 1 means that top-level
507 * entries are expanded, but not their children; 2 means that top-level
508 * entries are expanded, and their children, but not grand-children; and so
511 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
514 * non-negative level, or <code>ALL_LEVELS</code> to expand all
518 public void setAutoExpandLevel(int level
) {
519 fAutoExpandLevel
= level
;
523 * Returns the auto-expand level.
525 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
526 * the tree are expanded automatically
527 * @see #setAutoExpandLevel
530 public int getAutoExpandLevel() {
531 return fAutoExpandLevel
;
535 * Set the expanded state of a given entry
540 * True if expanded, false if collapsed
542 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
543 Item item
= fItemData
.findItem(entry
);
544 if (item
!= null && item
.fExpanded
!= expanded
) {
545 item
.fExpanded
= expanded
;
546 fItemData
.updateExpandedItems();
552 * Collapses all nodes of the viewer's tree, starting with the root.
556 public void collapseAll() {
557 for (Item item
: fItemData
.fItems
) {
558 item
.fExpanded
= false;
560 fItemData
.updateExpandedItems();
565 * Expands all nodes of the viewer's tree, starting with the root.
569 public void expandAll() {
570 for (Item item
: fItemData
.fItems
) {
571 item
.fExpanded
= true;
573 fItemData
.updateExpandedItems();
578 * Add a tree listener
581 * The listener to add
583 public void addTreeListener(ITimeGraphTreeListener listener
) {
584 if (!fTreeListeners
.contains(listener
)) {
585 fTreeListeners
.add(listener
);
590 * Remove a tree listener
593 * The listener to remove
595 public void removeTreeListener(ITimeGraphTreeListener listener
) {
596 if (fTreeListeners
.contains(listener
)) {
597 fTreeListeners
.remove(listener
);
602 * Tree event callback
607 * The expanded state (true for expanded, false for collapsed)
609 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
610 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
611 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
613 listener
.treeExpanded(event
);
615 listener
.treeCollapsed(event
);
621 * Add a menu listener on {@link ITimeGraphEntry}s
623 * The listener to add
626 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
627 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
628 fTimeGraphEntryMenuListeners
.add(listener
);
633 * Remove a menu listener on {@link ITimeGraphEntry}s
636 * The listener to remove
639 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
640 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
641 fTimeGraphEntryMenuListeners
.remove(listener
);
646 * Menu event callback on {@link ITimeGraphEntry}s
649 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
651 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
652 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
653 listener
.menuDetected(event
);
658 * Add a menu listener on {@link ITimeEvent}s
661 * The listener to add
664 public void addTimeEventMenuListener(MenuDetectListener listener
) {
665 if (!fTimeEventMenuListeners
.contains(listener
)) {
666 fTimeEventMenuListeners
.add(listener
);
671 * Remove a menu listener on {@link ITimeEvent}s
674 * The listener to remove
677 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
678 if (fTimeEventMenuListeners
.contains(listener
)) {
679 fTimeEventMenuListeners
.remove(listener
);
684 * Menu event callback on {@link ITimeEvent}s
687 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
689 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
690 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
691 listener
.menuDetected(event
);
696 public ISelection
getSelection() {
697 TimeGraphSelection sel
= new TimeGraphSelection();
698 ITimeGraphEntry trace
= getSelectedTrace();
699 if (null != trace
&& null != fTimeProvider
) {
700 long selectedTime
= fTimeProvider
.getSelectionBegin();
701 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
712 * Get the selection object
714 * @return The selection
716 public ISelection
getSelectionTrace() {
717 TimeGraphSelection sel
= new TimeGraphSelection();
718 ITimeGraphEntry trace
= getSelectedTrace();
726 * Enable/disable one of the traces in the model
729 * 1 to enable it, -1 to disable. The method returns immediately
730 * if another value is used.
732 public void selectTrace(int n
) {
733 if ((n
!= 1) && (n
!= -1)) {
737 boolean changed
= false;
738 int lastSelection
= -1;
739 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
740 Item item
= fItemData
.fExpandedItems
[i
];
741 if (item
.fSelected
) {
743 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
744 item
.fSelected
= false;
745 item
= fItemData
.fExpandedItems
[i
+ 1];
746 item
.fSelected
= true;
748 } else if ((-1 == n
) && (i
> 0)) {
749 item
.fSelected
= false;
750 item
= fItemData
.fExpandedItems
[i
- 1];
751 item
.fSelected
= true;
758 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
759 Item item
= fItemData
.fExpandedItems
[0];
760 item
.fSelected
= true;
765 ensureVisibleItem(-1, false);
767 fireSelectionChanged();
775 * 1 for next event, -1 for previous event
777 public void selectEvent(int n
) {
778 if (null == fTimeProvider
) {
781 ITimeGraphEntry trace
= getSelectedTrace();
785 long selectedTime
= fTimeProvider
.getSelectionBegin();
786 long endTime
= fTimeProvider
.getEndTime();
787 ITimeEvent nextEvent
;
788 if (-1 == n
&& selectedTime
> endTime
) {
789 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
791 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
793 if (null == nextEvent
&& -1 == n
) {
794 nextEvent
= Utils
.getFirstEvent(trace
);
796 if (null != nextEvent
) {
797 long nextTime
= nextEvent
.getTime();
798 // If last event detected e.g. going back or not moving to a next
800 if (nextTime
<= selectedTime
&& n
== 1) {
801 // Select to the end of this last event
802 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
803 // but not beyond the end of the trace
804 if (nextTime
> endTime
) {
807 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
808 // for previous event go to its end time unless we were already there
809 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
811 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
812 fireSelectionChanged();
814 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
815 fireSelectionChanged();
820 * Select the next event
822 public void selectNextEvent() {
824 // Notify if visible time window has been adjusted
825 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
829 * Select the previous event
831 public void selectPrevEvent() {
833 // Notify if visible time window has been adjusted
834 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
838 * Select the next trace
840 public void selectNextTrace() {
845 * Select the previous trace
847 public void selectPrevTrace() {
852 * Scroll left or right by one half window size
855 * true to scroll left, false to scroll right
859 public void horizontalScroll(boolean left
) {
860 long time0
= fTimeProvider
.getTime0();
861 long time1
= fTimeProvider
.getTime1();
862 long timeMin
= fTimeProvider
.getMinTime();
863 long timeMax
= fTimeProvider
.getMaxTime();
864 long range
= time1
- time0
;
868 long increment
= Math
.max(1, range
/ 2);
870 time0
= Math
.max(time0
- increment
, timeMin
);
871 time1
= time0
+ range
;
873 time1
= Math
.min(time1
+ increment
, timeMax
);
874 time0
= time1
- range
;
876 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
880 * Zoom based on mouse cursor location with mouse scrolling
882 * @param zoomIn true to zoom in, false to zoom out
884 public void zoom(boolean zoomIn
) {
885 int globalX
= getDisplay().getCursorLocation().x
;
886 Point p
= toControl(globalX
, 0);
887 int nameSpace
= fTimeProvider
.getNameSpace();
888 int timeSpace
= fTimeProvider
.getTimeSpace();
889 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
890 long time0
= fTimeProvider
.getTime0();
891 long time1
= fTimeProvider
.getTime1();
892 long interval
= time1
- time0
;
895 } // to allow getting out of single point interval
898 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
900 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
902 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
903 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
904 long newTime1
= newTime0
+ newInterval
;
905 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
906 synchronized (fMouseScrollNotifierLock
) {
907 if (fMouseScrollNotifier
== null) {
908 fMouseScrollNotifier
= new MouseScrollNotifier();
909 fMouseScrollNotifier
.start();
911 fMouseScrollNotifier
.mouseScrolled();
916 * zoom in using single click
918 public void zoomIn() {
919 long prevTime0
= fTimeProvider
.getTime0();
920 long prevTime1
= fTimeProvider
.getTime1();
921 long prevRange
= prevTime1
- prevTime0
;
922 if (prevRange
== 0) {
925 ITimeDataProvider provider
= fTimeProvider
;
926 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
927 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
928 selTime
= (prevTime0
+ prevTime1
) / 2;
930 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
931 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
933 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
935 if (inaccuracy
> 0 && inaccuracy
< 100) {
936 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
940 long min
= fTimeProvider
.getMinTimeInterval();
941 if ((time1
- time0
) < min
) {
942 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
946 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
950 * zoom out using single click
952 public void zoomOut() {
953 long prevTime0
= fTimeProvider
.getTime0();
954 long prevTime1
= fTimeProvider
.getTime1();
955 ITimeDataProvider provider
= fTimeProvider
;
956 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
957 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
958 selTime
= (prevTime0
+ prevTime1
) / 2;
960 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
961 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
963 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
964 if (inaccuracy
> 0 && inaccuracy
< 100) {
965 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
969 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
975 * @param hideArrows true to hide arrows
979 public void hideArrows(boolean hideArrows
) {
980 fHideArrows
= hideArrows
;
984 * Follow the arrow forward
988 public void followArrowFwd() {
989 ITimeGraphEntry trace
= getSelectedTrace();
993 long selectedTime
= fTimeProvider
.getSelectionBegin();
994 for (ILinkEvent link
: fItemData
.fLinks
) {
995 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
996 selectItem(link
.getDestinationEntry(), false);
997 if (link
.getDuration() != 0) {
998 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
999 // Notify if visible time window has been adjusted
1000 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1002 fireSelectionChanged();
1010 * Follow the arrow backward
1014 public void followArrowBwd() {
1015 ITimeGraphEntry trace
= getSelectedTrace();
1016 if (trace
== null) {
1019 long selectedTime
= fTimeProvider
.getSelectionBegin();
1020 for (ILinkEvent link
: fItemData
.fLinks
) {
1021 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1022 selectItem(link
.getEntry(), false);
1023 if (link
.getDuration() != 0) {
1024 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1025 // Notify if visible time window has been adjusted
1026 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1028 fireSelectionChanged();
1036 * Return the currently selected trace
1038 * @return The entry matching the trace
1040 public ITimeGraphEntry
getSelectedTrace() {
1041 ITimeGraphEntry trace
= null;
1042 int idx
= getSelectedIndex();
1044 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1050 * Retrieve the index of the currently selected item
1054 public int getSelectedIndex() {
1056 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1057 Item item
= fItemData
.fExpandedItems
[i
];
1058 if (item
.fSelected
) {
1066 boolean toggle(int idx
) {
1067 boolean toggled
= false;
1068 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1069 Item item
= fItemData
.fExpandedItems
[idx
];
1070 if (item
.fHasChildren
) {
1071 item
.fExpanded
= !item
.fExpanded
;
1072 fItemData
.updateExpandedItems();
1075 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1082 * Gets the index of the item at the given location.
1086 * @return the index of the item at the given location, of -1 if none.
1089 protected int getItemIndexAtY(int y
) {
1094 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1095 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1103 boolean isOverSplitLine(int x
) {
1104 if (x
< 0 || null == fTimeProvider
) {
1107 int nameWidth
= fTimeProvider
.getNameSpace();
1108 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1112 * Gets the {@link ITimeGraphEntry} at the given location.
1115 * a point in the widget
1116 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1120 protected ITimeGraphEntry
getEntry(Point pt
) {
1121 int idx
= getItemIndexAtY(pt
.y
);
1122 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1126 * Return the arrow event closest to the given point that is no further than
1127 * a maximum distance.
1130 * a point in the widget
1131 * @return The closest arrow event, or null if there is none close enough.
1134 protected ILinkEvent
getArrow(Point pt
) {
1138 ILinkEvent linkEvent
= null;
1139 double minDistance
= Double
.MAX_VALUE
;
1140 for (ILinkEvent event
: fItemData
.fLinks
) {
1141 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1145 int x2
= x1
+ rect
.width
;
1146 int y2
= y1
+ rect
.height
;
1147 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1148 if (minDistance
> d
) {
1154 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1164 public int getXForTime(long time
) {
1165 if (null == fTimeProvider
) {
1168 long time0
= fTimeProvider
.getTime0();
1169 long time1
= fTimeProvider
.getTime1();
1170 int width
= getSize().x
;
1171 int nameSpace
= fTimeProvider
.getNameSpace();
1172 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1173 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1181 public long getTimeAtX(int coord
) {
1182 if (null == fTimeProvider
) {
1186 Point size
= getSize();
1187 long time0
= fTimeProvider
.getTime0();
1188 long time1
= fTimeProvider
.getTime1();
1189 int nameWidth
= fTimeProvider
.getNameSpace();
1190 final int x
= coord
- nameWidth
;
1191 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1192 if (x
>= 0 && size
.x
>= nameWidth
) {
1193 if (time1
- time0
> timeWidth
) {
1194 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1195 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1197 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1198 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1204 void selectItem(int idx
, boolean addSelection
) {
1205 boolean changed
= false;
1207 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1208 Item item
= fItemData
.fExpandedItems
[idx
];
1209 changed
= !item
.fSelected
;
1210 item
.fSelected
= true;
1213 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1214 Item item
= fItemData
.fExpandedItems
[i
];
1215 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1218 item
.fSelected
= i
== idx
;
1221 changed
|= ensureVisibleItem(idx
, true);
1228 * Callback for item selection
1231 * The entry matching the trace
1232 * @param addSelection
1233 * If the selection is added or removed
1235 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1236 int idx
= fItemData
.findItemIndex(trace
);
1237 selectItem(idx
, addSelection
);
1241 * Retrieve the number of entries shown per page.
1245 public int countPerPage() {
1246 int height
= getSize().y
;
1249 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1250 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1251 if (ySum
>= height
) {
1256 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1257 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1258 if (ySum
>= height
) {
1267 * Get the index of the top element
1271 public int getTopIndex() {
1276 * Get the number of expanded items
1278 * @return The count of expanded items
1280 public int getExpandedElementCount() {
1281 return fItemData
.fExpandedItems
.length
;
1285 * Get an array of all expanded elements
1287 * @return The expanded elements
1289 public ITimeGraphEntry
[] getExpandedElements() {
1290 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1291 for (Item item
: fItemData
.fExpandedItems
) {
1292 elements
.add(item
.fEntry
);
1294 return elements
.toArray(new ITimeGraphEntry
[0]);
1297 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1298 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1300 rect
.width
= nameWidth
;
1304 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1305 int x
= bound
.x
+ nameWidth
;
1306 int width
= bound
.width
- x
;
1308 if (idx
>= fTopIndex
) {
1309 for (int i
= fTopIndex
; i
< idx
; i
++) {
1310 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1313 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1314 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1317 int y
= bound
.y
+ ySum
;
1318 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1319 return new Rectangle(x
, y
, width
, height
);
1323 void paint(Rectangle bounds
, PaintEvent e
) {
1325 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1326 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1328 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1332 fIdealNameSpace
= 0;
1333 int nameSpace
= fTimeProvider
.getNameSpace();
1335 // draw empty name space background
1336 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1337 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1340 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1341 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1342 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1344 int alpha
= gc
.getAlpha();
1347 long time0
= fTimeProvider
.getTime0();
1348 long time1
= fTimeProvider
.getTime1();
1349 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1350 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1351 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1352 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1353 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1355 // draw selection lines
1356 if (fDragState
!= DRAG_SELECTION
) {
1357 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1358 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1359 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1362 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1363 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1368 // draw selection background
1369 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1370 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1371 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1372 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1374 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1375 } else if (x0
- x1
> 1) {
1376 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1380 // draw drag selection background
1381 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1382 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1383 if (fDragX0
< fDragX
) {
1384 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1385 } else if (fDragX0
> fDragX
) {
1386 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1391 if (DRAG_SPLIT_LINE
== fDragState
) {
1392 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1393 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1394 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1395 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1396 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1397 if (fDragX
!= fDragX0
) {
1398 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1400 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1401 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1402 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1403 if (fDragX
!= fDragX0
) {
1404 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1406 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1407 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1408 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1415 * Draw many items at once
1418 * The rectangle of the area
1419 * @param timeProvider
1422 * The array items to draw
1424 * The index of the first element to draw
1426 * The width reserved for the names
1428 * Reference to the SWT GC object
1430 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1431 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1432 for (int i
= topIndex
; i
< items
.length
; i
++) {
1433 Item item
= items
[i
];
1434 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1441 * @param item the item to draw
1442 * @param bounds the container rectangle
1443 * @param timeProvider Time provider
1444 * @param i the item index
1445 * @param nameSpace the name space
1446 * @param gc Graphics context
1448 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1449 ITimeGraphEntry entry
= item
.fEntry
;
1450 long time0
= timeProvider
.getTime0();
1451 long time1
= timeProvider
.getTime1();
1452 long selectedTime
= fTimeProvider
.getSelectionBegin();
1454 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1455 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1459 if (! item
.fEntry
.hasTimeEvents()) {
1460 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1461 nameRect
.width
+= statesRect
.width
;
1462 drawName(item
, nameRect
, gc
);
1464 drawName(item
, nameRect
, gc
);
1466 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1467 if (rect
.isEmpty()) {
1468 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1471 if (time1
<= time0
) {
1472 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1473 gc
.fillRectangle(rect
);
1474 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1478 // Initialize _rect1 to same values as enclosing rectangle rect
1479 Rectangle stateRect
= Utils
.clone(rect
);
1480 boolean selected
= item
.fSelected
;
1481 // K pixels per second
1482 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1484 if (item
.fEntry
.hasTimeEvents()) {
1485 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1486 fillSpace(rect
, gc
, selected
);
1487 // Drawing rectangle is smaller than reserved space
1489 stateRect
.height
-= 6;
1491 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1492 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1495 while (iterator
.hasNext()) {
1496 ITimeEvent event
= iterator
.next();
1497 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1498 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1499 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1500 // event is out of bounds
1503 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1504 stateRect
.x
= Math
.max(rect
.x
, x
);
1505 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1506 if (stateRect
.x
== lastX
) {
1507 stateRect
.width
-= 1;
1508 if (stateRect
.width
> 0) {
1509 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1510 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1514 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1515 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1519 gc
.setClipping((Rectangle
) null);
1521 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1528 * The rectangle of the area
1529 * @param timeProvider
1532 * The array items to draw
1534 * The width reserved for the names
1536 * Reference to the SWT GC object
1539 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1540 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1544 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1545 for (ILinkEvent event
: links
) {
1546 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1548 gc
.setClipping((Rectangle
) null);
1552 * Draws the link type events of this item
1557 * the container rectangle
1558 * @param timeProvider
1566 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1567 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1570 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1571 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1572 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1574 if ((srcIndex
== -1) || (destIndex
== -1)) {
1578 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1579 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1581 int x0
= getXForTime(event
.getTime());
1582 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1584 // limit the x-coordinates to prevent integer overflow in calculations
1585 // and also GC.drawLine doesn't draw properly with large coordinates
1586 final int limit
= Integer
.MAX_VALUE
/ 1024;
1587 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1588 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1590 int y0
= src
.y
+ src
.height
/ 2;
1591 int y1
= dst
.y
+ dst
.height
/ 2;
1592 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1596 * Draw the state (color fill)
1601 * Time event for which we're drawing the state
1606 * @return true if the state was drawn
1609 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1610 Rectangle rect
, GC gc
) {
1615 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1619 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1622 Color stateColor
= null;
1623 if (colorIdx
< fEventColorMap
.length
) {
1624 stateColor
= fEventColorMap
[colorIdx
];
1626 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1629 gc
.setForeground(stateColor
);
1630 gc
.setBackground(stateColor
);
1632 /* Draw the arrow */
1633 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1634 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1637 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1642 * @author Francis Giraldeau
1645 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1647 * The algorithm was taken from this site, not the code itself
1649 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1652 double cos
= 0.9510;
1653 double sin
= 0.3090;
1654 long lenx
= x1
- x0
;
1655 long leny
= y1
- y0
;
1656 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1658 double dx
= factor
* lenx
/ len
;
1659 double dy
= factor
* leny
/ len
;
1660 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1661 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1662 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1663 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1664 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1665 gc
.fillPolygon(arrow
);
1669 * Draw the name of an item.
1674 * Where to draw the name
1678 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1679 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1680 if (! hasTimeEvents
) {
1681 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1682 gc
.fillRectangle(bounds
);
1683 if (item
.fSelected
&& fIsInFocus
) {
1684 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1685 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1688 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1689 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1690 gc
.fillRectangle(bounds
);
1693 // No name to be drawn
1694 if (fTimeProvider
.getNameSpace() == 0) {
1698 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1699 if (item
.fHasChildren
) {
1700 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1701 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1702 Rectangle rect
= Utils
.clone(bounds
);
1703 rect
.x
+= leftMargin
;
1704 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1705 rect
.width
= EXPAND_SIZE
;
1706 rect
.height
= EXPAND_SIZE
;
1707 gc
.fillRectangle(rect
);
1708 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1709 int midy
= rect
.y
+ rect
.height
/ 2;
1710 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1711 if (!item
.fExpanded
) {
1712 int midx
= rect
.x
+ rect
.width
/ 2;
1713 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1716 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1718 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1721 int imgHeight
= img
.getImageData().height
;
1722 int imgWidth
= img
.getImageData().width
;
1724 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1725 gc
.drawImage(img
, x
, y
);
1726 leftMargin
+= imgWidth
+ MARGIN
;
1728 String name
= item
.fName
;
1729 Point size
= gc
.stringExtent(name
);
1730 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1731 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1733 if (hasTimeEvents
) {
1734 // cut long string with "..."
1735 int width
= bounds
.width
- leftMargin
;
1737 while (size
.x
> width
&& name
.length() > 1) {
1739 name
= name
.substring(0, name
.length() - 1);
1740 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1743 name
+= "..."; //$NON-NLS-1$
1746 Rectangle rect
= Utils
.clone(bounds
);
1747 rect
.x
+= leftMargin
;
1748 rect
.width
-= leftMargin
;
1750 if (rect
.width
> 0) {
1751 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1752 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1753 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1754 leftMargin
+= textWidth
+ MARGIN
;
1757 if (hasTimeEvents
) {
1759 int x
= bounds
.x
+ leftMargin
;
1760 int width
= bounds
.width
- x
;
1761 int midy
= bounds
.y
+ bounds
.height
/ 2;
1762 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1763 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1769 * Draw the state (color fill)
1774 * Time event for which we're drawing the state
1780 * Is this time event currently selected (so it appears
1782 * @param timeSelected
1783 * Is the timestamp currently selected
1784 * @return true if the state was drawn
1787 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1788 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1790 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1791 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1794 boolean visible
= rect
.width
== 0 ?
false : true;
1795 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1796 gc
.setForeground(black
);
1799 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1800 // Only draw the top and bottom borders
1801 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1802 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1803 if (rect
.width
== 1) {
1804 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1806 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1809 Color stateColor
= null;
1810 if (colorIdx
< fEventColorMap
.length
) {
1811 stateColor
= fEventColorMap
[colorIdx
];
1816 boolean reallySelected
= timeSelected
&& selected
;
1817 // fill all rect area
1818 gc
.setBackground(stateColor
);
1819 gc
.fillRectangle(rect
);
1821 if (reallySelected
) {
1822 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1823 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1826 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1828 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1833 * Fill the space between two contiguous time events
1840 * Is this time event selected or not
1842 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1843 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1844 gc
.fillRectangle(rect
);
1845 if (fDragState
== DRAG_ZOOM
) {
1846 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1847 if (fDragX0
< fDragX
) {
1848 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1849 } else if (fDragX0
> fDragX
) {
1850 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1854 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1855 int midy
= rect
.y
+ rect
.height
/ 2;
1856 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1860 public void keyTraversed(TraverseEvent e
) {
1861 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1867 public void keyPressed(KeyEvent e
) {
1869 if (fItemData
.fExpandedItems
.length
== 0) {
1872 if (SWT
.HOME
== e
.keyCode
) {
1874 } else if (SWT
.END
== e
.keyCode
) {
1875 idx
= fItemData
.fExpandedItems
.length
- 1;
1876 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1877 idx
= getSelectedIndex();
1880 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1883 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1884 idx
= getSelectedIndex();
1887 } else if (idx
> 0) {
1890 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1892 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1894 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1895 int page
= countPerPage();
1896 idx
= getSelectedIndex();
1901 if (idx
>= fItemData
.fExpandedItems
.length
) {
1902 idx
= fItemData
.fExpandedItems
.length
- 1;
1904 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1905 int page
= countPerPage();
1906 idx
= getSelectedIndex();
1914 } else if (SWT
.CR
== e
.keyCode
) {
1915 idx
= getSelectedIndex();
1917 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1920 fireDefaultSelection();
1926 selectItem(idx
, false);
1927 fireSelectionChanged();
1929 int x
= toControl(e
.display
.getCursorLocation()).x
;
1930 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1934 public void keyReleased(KeyEvent e
) {
1935 int x
= toControl(e
.display
.getCursorLocation()).x
;
1936 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1940 public void focusGained(FocusEvent e
) {
1943 updateStatusLine(NO_STATUS
);
1947 public void focusLost(FocusEvent e
) {
1949 if (DRAG_NONE
!= fDragState
) {
1951 fDragState
= DRAG_NONE
;
1954 updateStatusLine(NO_STATUS
);
1958 * @return If the current view is focused
1960 public boolean isInFocus() {
1965 * Provide the possibility to control the wait cursor externally e.g. data
1966 * requests in progress
1968 * @param waitInd Should we wait indefinitely?
1970 public void waitCursor(boolean waitInd
) {
1971 // Update cursor as indicated
1973 setCursor(fWaitCursor
);
1979 private void updateCursor(int x
, int stateMask
) {
1980 // if Wait cursor not active, check for the need to change the cursor
1981 if (getCursor() == fWaitCursor
) {
1984 Cursor cursor
= null;
1985 if (fDragState
== DRAG_SPLIT_LINE
) {
1986 } else if (fDragState
== DRAG_SELECTION
) {
1987 cursor
= fResizeCursor
;
1988 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1989 cursor
= fDragCursor
;
1990 } else if (fDragState
== DRAG_ZOOM
) {
1991 cursor
= fZoomCursor
;
1992 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1993 cursor
= fDragCursor
;
1994 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1995 cursor
= fResizeCursor
;
1996 } else if (!isOverSplitLine(x
)) {
1997 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1998 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1999 int xBegin
= getXForTime(selectionBegin
);
2000 int xEnd
= getXForTime(selectionEnd
);
2001 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2002 cursor
= fResizeCursor
;
2005 if (getCursor() != cursor
) {
2010 private void updateStatusLine(int x
) {
2011 // use the time provider of the time graph scale for the status line
2012 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2013 if (fStatusLineManager
== null || null == tdp
||
2014 tdp
.getTime0() == tdp
.getTime1()) {
2017 TimeFormat tf
= tdp
.getTimeFormat();
2018 Resolution res
= Resolution
.NANOSEC
;
2019 StringBuilder message
= new StringBuilder();
2020 if (x
>= 0 && fDragState
== DRAG_NONE
) {
2021 long time
= getTimeAtX(x
);
2023 if (tdp
instanceof ITimeDataProviderConverter
) {
2024 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2026 long selectionBegin
= tdp
.getSelectionBegin();
2027 long selectionEnd
= tdp
.getSelectionEnd();
2028 message
.append(NLS
.bind("T: {0}{1} T1: {2}{3}", //$NON-NLS-1$
2030 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2031 Utils
.formatTime(time
, tf
, res
),
2032 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
2033 Utils
.formatTime(Math
.min(selectionBegin
, selectionEnd
), tf
, res
)
2035 if (selectionBegin
!= selectionEnd
) {
2036 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2038 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
2039 Utils
.formatTime(Math
.max(selectionBegin
, selectionEnd
), tf
, res
),
2040 Utils
.formatDelta(Math
.abs(selectionBegin
- selectionEnd
), tf
, res
)
2044 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2045 long time0
= fDragTime0
;
2046 long time
= getTimeAtX(fDragX
);
2047 if (tdp
instanceof ITimeDataProviderConverter
) {
2048 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2049 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2051 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2053 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2054 Utils
.formatTime(Math
.min(time
, time0
), tf
, res
),
2055 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2056 Utils
.formatTime(Math
.max(time
, time0
), tf
, res
),
2057 Utils
.formatDelta(Math
.abs(time
- time0
), tf
, res
)
2060 fStatusLineManager
.setMessage(message
.toString());
2064 public void mouseMove(MouseEvent e
) {
2065 if (null == fTimeProvider
) {
2068 Point size
= getSize();
2069 if (DRAG_TRACE_ITEM
== fDragState
) {
2070 int nameWidth
= fTimeProvider
.getNameSpace();
2071 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2073 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2074 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2075 long time1
= fTime1bak
- timeDelta
;
2076 long maxTime
= fTimeProvider
.getMaxTime();
2077 if (time1
> maxTime
) {
2080 long time0
= time1
- (fTime1bak
- fTime0bak
);
2081 if (time0
< fTimeProvider
.getMinTime()) {
2082 time0
= fTimeProvider
.getMinTime();
2083 time1
= time0
+ (fTime1bak
- fTime0bak
);
2085 fTimeProvider
.setStartFinishTime(time0
, time1
);
2087 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2089 fTimeProvider
.setNameSpace(e
.x
);
2090 } else if (DRAG_SELECTION
== fDragState
) {
2091 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2093 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2094 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2095 } else if (DRAG_ZOOM
== fDragState
) {
2096 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2098 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2099 } else if (DRAG_NONE
== fDragState
) {
2100 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2101 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2104 fMouseOverSplitLine
= mouseOverSplitLine
;
2106 updateCursor(e
.x
, e
.stateMask
);
2107 updateStatusLine(e
.x
);
2111 public void mouseDoubleClick(MouseEvent e
) {
2112 if (null == fTimeProvider
) {
2115 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2116 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2117 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2118 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2119 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2122 fMouseOverSplitLine
= mouseOverSplitLine
;
2125 int idx
= getItemIndexAtY(e
.y
);
2127 selectItem(idx
, false);
2128 fireDefaultSelection();
2134 public void mouseDown(MouseEvent e
) {
2135 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2136 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2137 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2141 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2142 int nameSpace
= fTimeProvider
.getNameSpace();
2143 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2144 fDragState
= DRAG_SPLIT_LINE
;
2145 fDragButton
= e
.button
;
2148 fTime0bak
= fTimeProvider
.getTime0();
2149 fTime1bak
= fTimeProvider
.getTime1();
2151 updateCursor(e
.x
, e
.stateMask
);
2155 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2156 int nameSpace
= fTimeProvider
.getNameSpace();
2157 idx
= getItemIndexAtY(e
.y
);
2159 Item item
= fItemData
.fExpandedItems
[idx
];
2160 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2164 selectItem(idx
, false);
2165 fireSelectionChanged();
2167 selectItem(idx
, false); // clear selection
2168 fireSelectionChanged();
2170 long hitTime
= getTimeAtX(e
.x
);
2174 fDragState
= DRAG_SELECTION
;
2175 fDragButton
= e
.button
;
2178 fDragTime0
= getTimeAtX(fDragX0
);
2179 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2180 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2181 int xBegin
= getXForTime(selectionBegin
);
2182 int xEnd
= getXForTime(selectionEnd
);
2183 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2184 long time
= getTimeAtX(e
.x
);
2185 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2187 fDragTime0
= selectionEnd
;
2190 fDragTime0
= selectionBegin
;
2193 long time
= getTimeAtX(e
.x
);
2194 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2196 fDragTime0
= selectionEnd
;
2197 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2199 fDragTime0
= selectionBegin
;
2202 fTime0bak
= fTimeProvider
.getTime0();
2203 fTime1bak
= fTimeProvider
.getTime1();
2205 updateCursor(e
.x
, e
.stateMask
);
2206 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2208 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2209 long hitTime
= getTimeAtX(e
.x
);
2212 fDragState
= DRAG_TRACE_ITEM
;
2213 fDragButton
= e
.button
;
2216 fTime0bak
= fTimeProvider
.getTime0();
2217 fTime1bak
= fTimeProvider
.getTime1();
2218 updateCursor(e
.x
, e
.stateMask
);
2220 } else if (3 == e
.button
) {
2222 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2224 fDragTime0
= getTimeAtX(fDragX0
);
2225 fDragState
= DRAG_ZOOM
;
2226 fDragButton
= e
.button
;
2228 updateCursor(e
.x
, e
.stateMask
);
2229 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2234 public void mouseUp(MouseEvent e
) {
2235 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2236 menuDetected(fPendingMenuDetectEvent
);
2238 if (DRAG_NONE
!= fDragState
) {
2240 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2241 if (fDragX
!= fDragX0
) {
2242 fTimeProvider
.notifyStartFinishTime();
2244 fDragState
= DRAG_NONE
;
2245 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2246 fDragState
= DRAG_NONE
;
2248 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2249 if (fDragX
== fDragX0
) { // click without selecting anything
2250 long time
= getTimeAtX(e
.x
);
2251 fTimeProvider
.setSelectedTimeNotify(time
, false);
2253 long time0
= fDragTime0
;
2254 long time1
= getTimeAtX(fDragX
);
2255 if (time0
<= time1
) {
2256 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2258 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2261 fDragState
= DRAG_NONE
;
2263 fTimeGraphScale
.setDragRange(-1, -1);
2264 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2265 int nameWidth
= fTimeProvider
.getNameSpace();
2266 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2267 long time0
= getTimeAtX(fDragX0
);
2268 long time1
= getTimeAtX(fDragX
);
2269 if (time0
< time1
) {
2270 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2272 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2277 fDragState
= DRAG_NONE
;
2278 fTimeGraphScale
.setDragRange(-1, -1);
2281 updateCursor(e
.x
, e
.stateMask
);
2282 updateStatusLine(e
.x
);
2286 public void mouseEnter(MouseEvent e
) {
2290 public void mouseExit(MouseEvent e
) {
2291 if (fMouseOverSplitLine
) {
2292 fMouseOverSplitLine
= false;
2295 updateStatusLine(NO_STATUS
);
2299 public void mouseHover(MouseEvent e
) {
2303 public void mouseScrolled(MouseEvent e
) {
2304 if (fDragState
!= DRAG_NONE
) {
2307 boolean zoomScroll
= false;
2308 boolean horizontalScroll
= false;
2309 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2310 Point parentSize
= getParent().getSize();
2311 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2312 // over the parent control
2313 if (e
.x
> getSize().x
) {
2314 // over the vertical scroll bar
2316 } else if (e
.y
< 0) {
2317 // over the time scale
2319 } else if (e
.y
>= getSize().y
) {
2320 // over the horizontal scroll bar
2321 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2324 horizontalScroll
= true;
2327 if (e
.x
< fTimeProvider
.getNameSpace()) {
2328 // over the name space
2331 // over the state area
2332 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2333 // over the state area, CTRL pressed
2336 // over the state area, CTRL not pressed
2342 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2345 } else if (e
.count
< 0) {
2348 } else if (horizontalScroll
) {
2349 horizontalScroll(e
.count
> 0);
2351 setTopIndex(getTopIndex() - e
.count
);
2356 public void handleEvent(Event event
) {
2357 if (event
.type
== SWT
.MouseWheel
) {
2358 // prevent horizontal scrolling when the mouse wheel is used to
2359 // scroll vertically or zoom
2365 public int getBorderWidth() {
2366 return fBorderWidth
;
2370 * Set the border width
2372 * @param borderWidth
2375 public void setBorderWidth(int borderWidth
) {
2376 this.fBorderWidth
= borderWidth
;
2380 * @return The current height of the header row
2382 public int getHeaderHeight() {
2383 return fHeaderHeight
;
2387 * Set the height of the header row
2389 * @param headerHeight
2392 public void setHeaderHeight(int headerHeight
) {
2393 this.fHeaderHeight
= headerHeight
;
2397 * @return The default height of regular item rows
2399 public int getItemHeight() {
2400 return fGlobalItemHeight
;
2404 * Set the default height of regular item rows.
2409 public void setItemHeight(int rowHeight
) {
2410 this.fGlobalItemHeight
= rowHeight
;
2414 * Set the height of a specific item. Overrides the default item height.
2417 * A time graph entry
2420 * @return true if the height is successfully stored, false otherwise
2424 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2425 Item item
= fItemData
.findItem(entry
);
2427 item
.fItemHeight
= rowHeight
;
2434 * Set the minimum item width
2436 * @param width The minimum width
2438 public void setMinimumItemWidth(int width
) {
2439 this.fMinimumItemWidth
= width
;
2443 * @return The minimum item width
2445 public int getMinimumItemWidth() {
2446 return fMinimumItemWidth
;
2450 * @return The entries that are currently filtered out
2454 public List
<ITimeGraphEntry
> getFilteredOut() {
2455 return fItemData
.getFilteredOut();
2459 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2460 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2461 fSelectionChangedListeners
.add(listener
);
2466 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2467 if (listener
!= null) {
2468 fSelectionChangedListeners
.remove(listener
);
2473 public void setSelection(ISelection selection
) {
2474 if (selection
instanceof TimeGraphSelection
) {
2475 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2476 Object ob
= sel
.getFirstElement();
2477 if (ob
instanceof ITimeGraphEntry
) {
2478 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2479 selectItem(trace
, false);
2486 * @param filter The filter object to be attached to the view
2489 public void addFilter(ViewerFilter filter
) {
2490 if (!fFilters
.contains(filter
)) {
2491 fFilters
.add(filter
);
2496 * @param filter The filter object to be attached to the view
2499 public void removeFilter(ViewerFilter filter
) {
2500 fFilters
.remove(filter
);
2507 public void colorSettingsChanged(StateItem
[] stateItems
) {
2508 /* Destroy previous colors from the resource manager */
2509 if (fEventColorMap
!= null) {
2510 for (Color color
: fEventColorMap
) {
2511 fResourceManager
.destroyColor(color
.getRGB());
2514 if (stateItems
!= null) {
2515 fEventColorMap
= new Color
[stateItems
.length
];
2516 for (int i
= 0; i
< stateItems
.length
; i
++) {
2517 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2520 fEventColorMap
= new Color
[] { };
2525 private class ItemData
{
2526 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2527 private Item
[] fExpandedItems
= new Item
[0];
2528 private Item
[] fItems
= new Item
[0];
2529 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2530 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2531 private boolean fEntryFilter
[] = new boolean[0];
2532 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2537 public Item
findItem(ITimeGraphEntry entry
) {
2538 return fItemMap
.get(entry
);
2541 public int findItemIndex(ITimeGraphEntry entry
) {
2542 Item item
= fItemMap
.get(entry
);
2546 return item
.fExpandedIndex
;
2549 public void refreshData() {
2551 fFilteredOut
.clear();
2552 ITimeGraphEntry selection
= getSelectedTrace();
2553 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2554 ITimeGraphEntry entry
= fRootEntries
[i
];
2555 refreshData(fItemMap
, null, 0, entry
);
2557 fItems
= fItemMap
.values().toArray(new Item
[0]);
2558 updateExpandedItems();
2559 if (selection
!= null) {
2560 for (Item item
: fExpandedItems
) {
2561 if (item
.fEntry
== selection
) {
2562 item
.fSelected
= true;
2569 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2570 Item item
= new Item(entry
, entry
.getName(), level
);
2571 if (parent
!= null) {
2572 parent
.fChildren
.add(item
);
2574 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2575 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2577 item
.fItemHeight
= fGlobalItemHeight
;
2579 itemMap
.put(entry
, item
);
2580 if (entry
.hasChildren()) {
2581 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2582 item
.fHasChildren
= true;
2583 for (ITimeGraphEntry child
: entry
.getChildren()) {
2584 refreshData(itemMap
, item
, level
+ 1, child
);
2589 public void updateExpandedItems() {
2590 for (Item item
: fItems
) {
2591 item
.fExpandedIndex
= -1;
2593 List
<Item
> expandedItemList
= new ArrayList
<>();
2594 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2595 ITimeGraphEntry entry
= fRootEntries
[i
];
2596 Item item
= findItem(entry
);
2597 refreshExpanded(expandedItemList
, item
);
2599 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2600 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2603 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2604 // Check for filters
2605 boolean display
= true;
2606 for (ViewerFilter filter
: fFilters
) {
2607 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2613 item
.fExpandedIndex
= expandedItemList
.size();
2614 expandedItemList
.add(item
);
2615 if (item
.fHasChildren
&& item
.fExpanded
) {
2616 for (Item child
: item
.fChildren
) {
2617 refreshExpanded(expandedItemList
, child
);
2623 public void refreshData(ITimeGraphEntry
[] entries
) {
2624 if (entries
== null) {
2625 fEntryFilter
= null;
2626 fRootEntries
= null;
2628 if (entries
.length
== 0) {
2629 fEntryFilter
= null;
2630 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2631 fEntryFilter
= new boolean[entries
.length
];
2632 java
.util
.Arrays
.fill(fEntryFilter
, true);
2634 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2640 public void refreshArrows(List
<ILinkEvent
> events
) {
2641 /* If links are null, reset the list */
2642 if (events
!= null) {
2645 fLinks
= new ArrayList
<>();
2649 public ITimeGraphEntry
[] getEntries() {
2650 return fRootEntries
;
2653 public boolean[] getEntryFilter() {
2654 return fEntryFilter
;
2657 public List
<ITimeGraphEntry
> getFilteredOut() {
2658 return fFilteredOut
;
2662 private class Item
{
2663 private boolean fExpanded
;
2664 private int fExpandedIndex
;
2665 private boolean fSelected
;
2666 private boolean fHasChildren
;
2667 private int fItemHeight
;
2668 private final int fLevel
;
2669 private final List
<Item
> fChildren
;
2670 private final String fName
;
2671 private final ITimeGraphEntry fEntry
;
2673 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2674 this.fEntry
= entry
;
2676 this.fLevel
= level
;
2677 this.fChildren
= new ArrayList
<>();
2681 public String
toString() {
2690 public void menuDetected(MenuDetectEvent e
) {
2691 if (null == fTimeProvider
) {
2694 if (e
.detail
== SWT
.MENU_MOUSE
) {
2695 if (fPendingMenuDetectEvent
== null) {
2696 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2697 * Store the event and trigger it later just before handling mouseUp.
2698 * This allows for the method to detect if mouse is used to drag zoom.
2700 fPendingMenuDetectEvent
= e
;
2703 fPendingMenuDetectEvent
= null;
2704 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2708 if (fDragState
!= DRAG_NONE
) {
2712 Point p
= toControl(e
.x
, e
.y
);
2713 int idx
= getItemIndexAtY(p
.y
);
2714 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2715 Item item
= fItemData
.fExpandedItems
[idx
];
2716 ITimeGraphEntry entry
= item
.fEntry
;
2717 if (entry
.hasTimeEvents()) {
2718 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2719 if (event
!= null) {
2721 fireMenuEventOnTimeEvent(e
);
2726 fireMenuEventOnTimeGraphEntry(e
);