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
.linuxtools
.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
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
50 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
51 import org
.eclipse
.osgi
.util
.NLS
;
52 import org
.eclipse
.swt
.SWT
;
53 import org
.eclipse
.swt
.events
.ControlEvent
;
54 import org
.eclipse
.swt
.events
.ControlListener
;
55 import org
.eclipse
.swt
.events
.FocusEvent
;
56 import org
.eclipse
.swt
.events
.FocusListener
;
57 import org
.eclipse
.swt
.events
.KeyEvent
;
58 import org
.eclipse
.swt
.events
.KeyListener
;
59 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
60 import org
.eclipse
.swt
.events
.MenuDetectListener
;
61 import org
.eclipse
.swt
.events
.MouseEvent
;
62 import org
.eclipse
.swt
.events
.MouseListener
;
63 import org
.eclipse
.swt
.events
.MouseMoveListener
;
64 import org
.eclipse
.swt
.events
.MouseTrackListener
;
65 import org
.eclipse
.swt
.events
.MouseWheelListener
;
66 import org
.eclipse
.swt
.events
.PaintEvent
;
67 import org
.eclipse
.swt
.events
.SelectionEvent
;
68 import org
.eclipse
.swt
.events
.SelectionListener
;
69 import org
.eclipse
.swt
.events
.TraverseEvent
;
70 import org
.eclipse
.swt
.events
.TraverseListener
;
71 import org
.eclipse
.swt
.events
.TypedEvent
;
72 import org
.eclipse
.swt
.graphics
.Color
;
73 import org
.eclipse
.swt
.graphics
.Cursor
;
74 import org
.eclipse
.swt
.graphics
.GC
;
75 import org
.eclipse
.swt
.graphics
.Image
;
76 import org
.eclipse
.swt
.graphics
.Point
;
77 import org
.eclipse
.swt
.graphics
.Rectangle
;
78 import org
.eclipse
.swt
.widgets
.Composite
;
79 import org
.eclipse
.swt
.widgets
.Display
;
80 import org
.eclipse
.swt
.widgets
.Event
;
81 import org
.eclipse
.swt
.widgets
.Listener
;
82 import org
.eclipse
.swt
.widgets
.ScrollBar
;
85 * Time graph control implementation
88 * @author Alvaro Sanchez-Leon
89 * @author Patrick Tasse
91 public class TimeGraphControl
extends TimeGraphBaseControl
92 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
,
93 ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
94 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
{
96 /** Max scrollbar size */
97 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
99 /** Constant indicating that all levels of the time graph should be expanded
101 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
103 private static final int DRAG_NONE
= 0;
104 private static final int DRAG_TRACE_ITEM
= 1;
105 private static final int DRAG_SPLIT_LINE
= 2;
106 private static final int DRAG_ZOOM
= 3;
107 private static final int DRAG_SELECTION
= 4;
109 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
111 private static final double ZOOM_FACTOR
= 1.5;
112 private static final double ZOOM_IN_FACTOR
= 0.8;
113 private static final double ZOOM_OUT_FACTOR
= 1.25;
115 private static final int SNAP_WIDTH
= 2;
116 private static final int ARROW_HOVER_MAX_DIST
= 5;
118 private static final int NO_STATUS
= -1;
120 /** Resource manager */
121 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
123 /** Color map for event types */
124 private Color
[] fEventColorMap
= null;
126 private ITimeDataProvider fTimeProvider
;
127 private IStatusLineManager fStatusLineManager
= null;
128 private TimeGraphScale fTimeGraphScale
= null;
130 private boolean fIsInFocus
= false;
131 private boolean fMouseOverSplitLine
= false;
132 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
133 private int fMinimumItemWidth
= 0;
134 private int fTopIndex
= 0;
135 private int fDragState
= DRAG_NONE
;
136 private int fDragButton
;
137 private int fDragX0
= 0;
138 private int fDragX
= 0;
139 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
140 private int fIdealNameSpace
= 0;
141 private long fTime0bak
;
142 private long fTime1bak
;
143 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
144 private ItemData fItemData
= null;
145 private List
<SelectionListener
> fSelectionListeners
;
146 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
147 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
148 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
149 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
150 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
151 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
152 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
153 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
154 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
155 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
156 private MenuDetectEvent fPendingMenuDetectEvent
= null;
157 private boolean fHideArrows
= false;
158 private int fAutoExpandLevel
= ALL_LEVELS
;
160 private int fBorderWidth
= 0;
161 private int fHeaderHeight
= 0;
163 private Listener fMouseScrollFilterListener
;
165 private MouseScrollNotifier fMouseScrollNotifier
;
166 private final Object fMouseScrollNotifierLock
= new Object();
168 private class MouseScrollNotifier
extends Thread
{
169 private static final long DELAY
= 400L;
170 private static final long POLLING_INTERVAL
= 10L;
171 private long fLastScrollTime
= Long
.MAX_VALUE
;
175 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
177 Thread
.sleep(POLLING_INTERVAL
);
178 } catch (Exception e
) {
182 if (!isInterrupted()) {
183 Display
.getDefault().asyncExec(new Runnable() {
189 fTimeProvider
.notifyStartFinishTime();
193 synchronized (fMouseScrollNotifierLock
) {
194 fMouseScrollNotifier
= null;
198 public void mouseScrolled() {
199 fLastScrollTime
= System
.currentTimeMillis();
204 * Standard constructor
207 * The parent composite object
209 * The color scheme to use
211 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
213 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
215 fItemData
= new ItemData();
217 addFocusListener(this);
218 addMouseListener(this);
219 addMouseMoveListener(this);
220 addMouseTrackListener(this);
221 addMouseWheelListener(this);
222 addTraverseListener(this);
223 addKeyListener(this);
224 addControlListener(this);
225 addMenuDetectListener(this);
226 ScrollBar scrollHor
= getHorizontalBar();
228 if (scrollHor
!= null) {
229 scrollHor
.addSelectionListener(this);
234 public void dispose() {
236 fResourceManager
.dispose();
240 * Sets the timegraph provider used by this timegraph viewer.
242 * @param timeGraphProvider the timegraph provider
244 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
245 fTimeGraphProvider
= timeGraphProvider
;
247 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
248 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
249 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
252 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
253 colorSettingsChanged(stateItems
);
257 * Gets the timegraph provider used by this timegraph viewer.
259 * @return the timegraph provider, or <code>null</code> if not set.
262 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
263 return fTimeGraphProvider
;
267 * Gets the color map used by this timegraph viewer.
269 * @return a color map, or <code>null</code> if not set.
272 public Color
[] getEventColorMap() {
273 return fEventColorMap
;
277 * Assign the given time provider
279 * @param timeProvider
282 public void setTimeProvider(ITimeDataProvider timeProvider
) {
283 fTimeProvider
= timeProvider
;
289 * Assign the status line manager
291 * @param statusLineManager
292 * The status line manager, or null to disable status line messages
295 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
296 if (fStatusLineManager
!= null && statusLineManager
== null) {
297 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
299 fStatusLineManager
= statusLineManager
;
303 * Assign the time graph scale
305 * @param timeGraphScale
306 * The time graph scale
309 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
310 fTimeGraphScale
= timeGraphScale
;
314 * Add a selection listener
317 * The listener to add
319 public void addSelectionListener(SelectionListener listener
) {
320 if (listener
== null) {
321 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
323 if (null == fSelectionListeners
) {
324 fSelectionListeners
= new ArrayList
<>();
326 fSelectionListeners
.add(listener
);
330 * Remove a selection listener
333 * The listener to remove
335 public void removeSelectionListener(SelectionListener listener
) {
336 if (null != fSelectionListeners
) {
337 fSelectionListeners
.remove(listener
);
342 * Selection changed callback
344 public void fireSelectionChanged() {
345 if (null != fSelectionListeners
) {
346 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
347 while (it
.hasNext()) {
348 SelectionListener listener
= it
.next();
349 listener
.widgetSelected(null);
355 * Default selection callback
357 public void fireDefaultSelection() {
358 if (null != fSelectionListeners
) {
359 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
360 while (it
.hasNext()) {
361 SelectionListener listener
= it
.next();
362 listener
.widgetDefaultSelected(null);
368 * Add a drag selection listener
371 * The listener to add
374 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
375 if (listener
== null) {
376 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
378 if (null == fDragSelectionListeners
) {
379 fDragSelectionListeners
= new ArrayList
<>();
381 fDragSelectionListeners
.add(listener
);
385 * Remove a drag selection listener
388 * The listener to remove
391 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
392 if (null != fDragSelectionListeners
) {
393 fDragSelectionListeners
.remove(listener
);
398 * Drag Selection changed callback
401 * Time interval start
406 public void fireDragSelectionChanged(long start
, long end
) {
407 // check for backward intervals
408 long beginTime
, endTime
;
416 // call the listeners
417 if (null != fDragSelectionListeners
) {
418 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
419 while (it
.hasNext()) {
420 ITimeGraphTimeListener listener
= it
.next();
421 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
427 * Get the traces in the model
429 * @return The array of traces
431 public ITimeGraphEntry
[] getTraces() {
432 return fItemData
.getEntries();
436 * Get the on/off trace filters
438 * @return The array of filters
440 public boolean[] getTraceFilter() {
441 return fItemData
.getEntryFilter();
445 * Refresh the data for the thing
447 public void refreshData() {
448 fItemData
.refreshData();
454 * Refresh data for the given traces
457 * The traces to refresh
459 public void refreshData(ITimeGraphEntry
[] traces
) {
460 fItemData
.refreshData(traces
);
466 * Refresh the links (arrows) of this widget
468 * @param events The link events to refresh
471 public void refreshArrows(List
<ILinkEvent
> events
) {
472 fItemData
.refreshArrows(events
);
476 * Adjust the scoll bars
478 public void adjustScrolls() {
479 if (null == fTimeProvider
) {
480 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
486 long time0
= fTimeProvider
.getTime0();
487 long time1
= fTimeProvider
.getTime1();
489 long timeMin
= fTimeProvider
.getMinTime();
490 long timeMax
= fTimeProvider
.getMaxTime();
492 long delta
= timeMax
- timeMin
;
495 int thumb
= H_SCROLLBAR_MAX
;
498 // Thumb size (page size)
499 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
500 // At the beginning of visible window
501 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
504 // position, minimum, maximum, thumb size, increment (half page)t, page
505 // increment size (full page)
506 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
509 boolean ensureVisibleItem(int idx
, boolean redraw
) {
510 boolean changed
= false;
513 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
514 if (fItemData
.fExpandedItems
[index
].fSelected
) {
519 if (index
>= fItemData
.fExpandedItems
.length
) {
522 if (index
< fTopIndex
) {
529 int page
= countPerPage();
530 if (index
>= fTopIndex
+ page
) {
531 setTopIndex(index
- page
+ 1);
542 * Assign the given index as the top one
547 public void setTopIndex(int idx
) {
548 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
549 index
= Math
.max(0, index
);
555 * Sets the auto-expand level to be used when the entries are refreshed
556 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
557 * The value 0 means that there is no auto-expand; 1 means that top-level
558 * entries are expanded, but not their children; 2 means that top-level
559 * entries are expanded, and their children, but not grand-children; and so
562 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
565 * non-negative level, or <code>ALL_LEVELS</code> to expand all
569 public void setAutoExpandLevel(int level
) {
570 fAutoExpandLevel
= level
;
574 * Returns the auto-expand level.
576 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
577 * the tree are expanded automatically
578 * @see #setAutoExpandLevel
581 public int getAutoExpandLevel() {
582 return fAutoExpandLevel
;
586 * Set the expanded state of a given entry
591 * True if expanded, false if collapsed
593 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
594 Item item
= fItemData
.findItem(entry
);
595 if (item
!= null && item
.fExpanded
!= expanded
) {
596 item
.fExpanded
= expanded
;
597 fItemData
.updateExpandedItems();
603 * Collapses all nodes of the viewer's tree, starting with the root.
607 public void collapseAll() {
608 for (Item item
: fItemData
.fItems
) {
609 item
.fExpanded
= false;
611 fItemData
.updateExpandedItems();
616 * Expands all nodes of the viewer's tree, starting with the root.
620 public void expandAll() {
621 for (Item item
: fItemData
.fItems
) {
622 item
.fExpanded
= true;
624 fItemData
.updateExpandedItems();
629 * Add a tree listener
632 * The listener to add
634 public void addTreeListener(ITimeGraphTreeListener listener
) {
635 if (!fTreeListeners
.contains(listener
)) {
636 fTreeListeners
.add(listener
);
641 * Remove a tree listener
644 * The listener to remove
646 public void removeTreeListener(ITimeGraphTreeListener listener
) {
647 if (fTreeListeners
.contains(listener
)) {
648 fTreeListeners
.remove(listener
);
653 * Tree event callback
658 * The expanded state (true for expanded, false for collapsed)
660 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
661 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
662 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
664 listener
.treeExpanded(event
);
666 listener
.treeCollapsed(event
);
672 * Add a menu listener on {@link ITimeGraphEntry}s
674 * The listener to add
677 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
678 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
679 fTimeGraphEntryMenuListeners
.add(listener
);
684 * Remove a menu listener on {@link ITimeGraphEntry}s
687 * The listener to remove
690 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
691 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
692 fTimeGraphEntryMenuListeners
.remove(listener
);
697 * Menu event callback on {@link ITimeGraphEntry}s
700 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
702 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
703 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
704 listener
.menuDetected(event
);
709 * Add a menu listener on {@link ITimeEvent}s
712 * The listener to add
715 public void addTimeEventMenuListener(MenuDetectListener listener
) {
716 if (!fTimeEventMenuListeners
.contains(listener
)) {
717 fTimeEventMenuListeners
.add(listener
);
722 * Remove a menu listener on {@link ITimeEvent}s
725 * The listener to remove
728 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
729 if (fTimeEventMenuListeners
.contains(listener
)) {
730 fTimeEventMenuListeners
.remove(listener
);
735 * Menu event callback on {@link ITimeEvent}s
738 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
740 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
741 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
742 listener
.menuDetected(event
);
747 public ISelection
getSelection() {
748 TimeGraphSelection sel
= new TimeGraphSelection();
749 ITimeGraphEntry trace
= getSelectedTrace();
750 if (null != trace
&& null != fTimeProvider
) {
751 long selectedTime
= fTimeProvider
.getSelectionBegin();
752 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
763 * Get the selection object
765 * @return The selection
767 public ISelection
getSelectionTrace() {
768 TimeGraphSelection sel
= new TimeGraphSelection();
769 ITimeGraphEntry trace
= getSelectedTrace();
777 * Enable/disable one of the traces in the model
780 * 1 to enable it, -1 to disable. The method returns immediately
781 * if another value is used.
783 public void selectTrace(int n
) {
784 if ((n
!= 1) && (n
!= -1)) {
788 boolean changed
= false;
789 int lastSelection
= -1;
790 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
791 Item item
= fItemData
.fExpandedItems
[i
];
792 if (item
.fSelected
) {
794 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
795 item
.fSelected
= false;
796 item
= fItemData
.fExpandedItems
[i
+ 1];
797 item
.fSelected
= true;
799 } else if ((-1 == n
) && (i
> 0)) {
800 item
.fSelected
= false;
801 item
= fItemData
.fExpandedItems
[i
- 1];
802 item
.fSelected
= true;
809 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
810 Item item
= fItemData
.fExpandedItems
[0];
811 item
.fSelected
= true;
816 ensureVisibleItem(-1, false);
818 fireSelectionChanged();
826 * 1 for next event, -1 for previous event
828 public void selectEvent(int n
) {
829 if (null == fTimeProvider
) {
832 ITimeGraphEntry trace
= getSelectedTrace();
836 long selectedTime
= fTimeProvider
.getSelectionBegin();
837 long endTime
= fTimeProvider
.getEndTime();
838 ITimeEvent nextEvent
;
839 if (-1 == n
&& selectedTime
> endTime
) {
840 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
842 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
844 if (null == nextEvent
&& -1 == n
) {
845 nextEvent
= Utils
.getFirstEvent(trace
);
847 if (null != nextEvent
) {
848 long nextTime
= nextEvent
.getTime();
849 // If last event detected e.g. going back or not moving to a next
851 if (nextTime
<= selectedTime
&& n
== 1) {
852 // Select to the end of this last event
853 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
854 // but not beyond the end of the trace
855 if (nextTime
> endTime
) {
858 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
859 // for previous event go to its end time unless we were already there
860 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
862 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
863 fireSelectionChanged();
865 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
866 fireSelectionChanged();
871 * Select the next event
873 public void selectNextEvent() {
875 // Notify if visible time window has been adjusted
876 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
880 * Select the previous event
882 public void selectPrevEvent() {
884 // Notify if visible time window has been adjusted
885 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
889 * Select the next trace
891 public void selectNextTrace() {
896 * Select the previous trace
898 public void selectPrevTrace() {
903 * Zoom based on mouse cursor location with mouse scrolling
905 * @param zoomIn true to zoom in, false to zoom out
907 public void zoom(boolean zoomIn
) {
908 int globalX
= getDisplay().getCursorLocation().x
;
909 Point p
= toControl(globalX
, 0);
910 int nameSpace
= fTimeProvider
.getNameSpace();
911 int timeSpace
= fTimeProvider
.getTimeSpace();
912 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
913 long time0
= fTimeProvider
.getTime0();
914 long time1
= fTimeProvider
.getTime1();
915 long interval
= time1
- time0
;
918 } // to allow getting out of single point interval
921 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
923 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
925 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
926 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
927 long newTime1
= newTime0
+ newInterval
;
928 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
929 synchronized (fMouseScrollNotifierLock
) {
930 if (fMouseScrollNotifier
== null) {
931 fMouseScrollNotifier
= new MouseScrollNotifier();
932 fMouseScrollNotifier
.start();
934 fMouseScrollNotifier
.mouseScrolled();
939 * zoom in using single click
941 public void zoomIn() {
942 long prevTime0
= fTimeProvider
.getTime0();
943 long prevTime1
= fTimeProvider
.getTime1();
944 long prevRange
= prevTime1
- prevTime0
;
945 if (prevRange
== 0) {
948 ITimeDataProvider provider
= fTimeProvider
;
949 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
950 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
951 selTime
= (prevTime0
+ prevTime1
) / 2;
953 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
954 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
956 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
958 if (inaccuracy
> 0 && inaccuracy
< 100) {
959 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
963 long min
= fTimeProvider
.getMinTimeInterval();
964 if ((time1
- time0
) < min
) {
965 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
969 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
973 * zoom out using single click
975 public void zoomOut() {
976 long prevTime0
= fTimeProvider
.getTime0();
977 long prevTime1
= fTimeProvider
.getTime1();
978 ITimeDataProvider provider
= fTimeProvider
;
979 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
980 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
981 selTime
= (prevTime0
+ prevTime1
) / 2;
983 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
984 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
986 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
987 if (inaccuracy
> 0 && inaccuracy
< 100) {
988 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
992 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
998 * @param hideArrows true to hide arrows
1002 public void hideArrows(boolean hideArrows
) {
1003 fHideArrows
= hideArrows
;
1007 * Follow the arrow forward
1011 public void followArrowFwd() {
1012 ITimeGraphEntry trace
= getSelectedTrace();
1013 if (trace
== null) {
1016 long selectedTime
= fTimeProvider
.getSelectionBegin();
1017 for (ILinkEvent link
: fItemData
.fLinks
) {
1018 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1019 selectItem(link
.getDestinationEntry(), false);
1020 if (link
.getDuration() != 0) {
1021 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1022 // Notify if visible time window has been adjusted
1023 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1025 fireSelectionChanged();
1033 * Follow the arrow backward
1037 public void followArrowBwd() {
1038 ITimeGraphEntry trace
= getSelectedTrace();
1039 if (trace
== null) {
1042 long selectedTime
= fTimeProvider
.getSelectionBegin();
1043 for (ILinkEvent link
: fItemData
.fLinks
) {
1044 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1045 selectItem(link
.getEntry(), false);
1046 if (link
.getDuration() != 0) {
1047 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1048 // Notify if visible time window has been adjusted
1049 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1051 fireSelectionChanged();
1059 * Return the currently selected trace
1061 * @return The entry matching the trace
1063 public ITimeGraphEntry
getSelectedTrace() {
1064 ITimeGraphEntry trace
= null;
1065 int idx
= getSelectedIndex();
1067 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1073 * Retrieve the index of the currently selected item
1077 public int getSelectedIndex() {
1079 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1080 Item item
= fItemData
.fExpandedItems
[i
];
1081 if (item
.fSelected
) {
1089 boolean toggle(int idx
) {
1090 boolean toggled
= false;
1091 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1092 Item item
= fItemData
.fExpandedItems
[idx
];
1093 if (item
.fHasChildren
) {
1094 item
.fExpanded
= !item
.fExpanded
;
1095 fItemData
.updateExpandedItems();
1099 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1106 * Gets the index of the item at the given location.
1110 * @return the index of the item at the given location, of -1 if none.
1113 protected int getItemIndexAtY(int y
) {
1118 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1119 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1127 boolean isOverSplitLine(int x
) {
1128 if (x
< 0 || null == fTimeProvider
) {
1131 int nameWidth
= fTimeProvider
.getNameSpace();
1132 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1136 * Gets the {@link ITimeGraphEntry} at the given location.
1139 * a point in the widget
1140 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1144 protected ITimeGraphEntry
getEntry(Point pt
) {
1145 int idx
= getItemIndexAtY(pt
.y
);
1146 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1150 * Return the arrow event closest to the given point that is no further than
1151 * a maximum distance.
1154 * a point in the widget
1155 * @return The closest arrow event, or null if there is none close enough.
1158 protected ILinkEvent
getArrow(Point pt
) {
1162 ILinkEvent linkEvent
= null;
1163 double minDistance
= Double
.MAX_VALUE
;
1164 for (ILinkEvent event
: fItemData
.fLinks
) {
1165 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1169 int x2
= x1
+ rect
.width
;
1170 int y2
= y1
+ rect
.height
;
1171 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1172 if (minDistance
> d
) {
1178 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1188 public int getXForTime(long time
) {
1189 if (null == fTimeProvider
) {
1192 long time0
= fTimeProvider
.getTime0();
1193 long time1
= fTimeProvider
.getTime1();
1194 int width
= getCtrlSize().x
;
1195 int nameSpace
= fTimeProvider
.getNameSpace();
1196 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1197 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1205 public long getTimeAtX(int coord
) {
1206 if (null == fTimeProvider
) {
1210 Point size
= getCtrlSize();
1211 long time0
= fTimeProvider
.getTime0();
1212 long time1
= fTimeProvider
.getTime1();
1213 int nameWidth
= fTimeProvider
.getNameSpace();
1214 final int x
= coord
- nameWidth
;
1215 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1216 if (x
>= 0 && size
.x
>= nameWidth
) {
1217 if (time1
- time0
> timeWidth
) {
1218 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1219 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1221 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1222 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1228 void selectItem(int idx
, boolean addSelection
) {
1229 boolean changed
= false;
1231 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1232 Item item
= fItemData
.fExpandedItems
[idx
];
1233 changed
= !item
.fSelected
;
1234 item
.fSelected
= true;
1237 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1238 Item item
= fItemData
.fExpandedItems
[i
];
1239 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1242 item
.fSelected
= i
== idx
;
1245 changed
|= ensureVisibleItem(idx
, true);
1252 * Callback for item selection
1255 * The entry matching the trace
1256 * @param addSelection
1257 * If the selection is added or removed
1259 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1260 int idx
= fItemData
.findItemIndex(trace
);
1261 selectItem(idx
, addSelection
);
1265 * Retrieve the number of entries shown per page.
1269 public int countPerPage() {
1270 int height
= getCtrlSize().y
;
1273 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1274 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1275 if (ySum
>= height
) {
1280 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1281 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1282 if (ySum
>= height
) {
1291 * Get the index of the top element
1295 public int getTopIndex() {
1300 * Get the number of expanded items
1302 * @return The count of expanded items
1304 public int getExpandedElementCount() {
1305 return fItemData
.fExpandedItems
.length
;
1309 * Get an array of all expanded elements
1311 * @return The expanded elements
1313 public ITimeGraphEntry
[] getExpandedElements() {
1314 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1315 for (Item item
: fItemData
.fExpandedItems
) {
1316 elements
.add(item
.fEntry
);
1318 return elements
.toArray(new ITimeGraphEntry
[0]);
1321 Point
getCtrlSize() {
1322 Point size
= getSize();
1323 if (getHorizontalBar().isVisible()) {
1324 size
.y
-= getHorizontalBar().getSize().y
;
1329 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1330 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1332 rect
.width
= nameWidth
;
1336 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1337 int x
= bound
.x
+ nameWidth
;
1338 int width
= bound
.width
- x
;
1340 if (idx
>= fTopIndex
) {
1341 for (int i
= fTopIndex
; i
< idx
; i
++) {
1342 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1345 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1346 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1349 int y
= bound
.y
+ ySum
;
1350 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1351 return new Rectangle(x
, y
, width
, height
);
1355 void paint(Rectangle bounds
, PaintEvent e
) {
1357 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1358 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1360 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1364 fIdealNameSpace
= 0;
1365 int nameSpace
= fTimeProvider
.getNameSpace();
1367 // draw empty name space background
1368 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1369 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1372 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1373 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1374 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1376 int alpha
= gc
.getAlpha();
1379 long time0
= fTimeProvider
.getTime0();
1380 long time1
= fTimeProvider
.getTime1();
1381 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1382 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1383 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1384 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1385 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1387 // draw selection lines
1388 if (fDragState
!= DRAG_SELECTION
) {
1389 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1390 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1391 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1394 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1395 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1400 // draw selection background
1401 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1402 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1403 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1404 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1406 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1407 } else if (x0
- x1
> 1) {
1408 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1412 // draw drag selection background
1413 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1414 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1415 if (fDragX0
< fDragX
) {
1416 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1417 } else if (fDragX0
> fDragX
) {
1418 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1423 if (DRAG_SPLIT_LINE
== fDragState
) {
1424 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1425 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1426 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1427 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1428 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1429 if (fDragX
!= fDragX0
) {
1430 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1432 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1433 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1434 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1435 if (fDragX
!= fDragX0
) {
1436 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1438 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1439 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1440 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1447 * Draw many items at once
1450 * The rectangle of the area
1451 * @param timeProvider
1454 * The array items to draw
1456 * The index of the first element to draw
1458 * The width reserved for the names
1460 * Reference to the SWT GC object
1462 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1463 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1464 for (int i
= topIndex
; i
< items
.length
; i
++) {
1465 Item item
= items
[i
];
1466 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1473 * @param item the item to draw
1474 * @param bounds the container rectangle
1475 * @param timeProvider Time provider
1476 * @param i the item index
1477 * @param nameSpace the name space
1478 * @param gc Graphics context
1480 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1481 ITimeGraphEntry entry
= item
.fEntry
;
1482 long time0
= timeProvider
.getTime0();
1483 long time1
= timeProvider
.getTime1();
1484 long selectedTime
= fTimeProvider
.getSelectionBegin();
1486 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1487 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1491 if (! item
.fEntry
.hasTimeEvents()) {
1492 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1493 nameRect
.width
+= statesRect
.width
;
1494 drawName(item
, nameRect
, gc
);
1496 drawName(item
, nameRect
, gc
);
1498 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1499 if (rect
.isEmpty()) {
1500 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1503 if (time1
<= time0
) {
1504 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1505 gc
.fillRectangle(rect
);
1506 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1510 // Initialize _rect1 to same values as enclosing rectangle rect
1511 Rectangle stateRect
= Utils
.clone(rect
);
1512 boolean selected
= item
.fSelected
;
1513 // K pixels per second
1514 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1516 if (item
.fEntry
.hasTimeEvents()) {
1517 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1518 fillSpace(rect
, gc
, selected
);
1519 // Drawing rectangle is smaller than reserved space
1521 stateRect
.height
-= 6;
1523 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1524 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1527 while (iterator
.hasNext()) {
1528 ITimeEvent event
= iterator
.next();
1529 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1530 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1531 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1532 // event is out of bounds
1535 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1536 stateRect
.x
= Math
.max(rect
.x
, x
);
1537 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1538 if (stateRect
.x
== lastX
) {
1539 stateRect
.width
-= 1;
1540 if (stateRect
.width
> 0) {
1541 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1542 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1546 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1547 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1551 gc
.setClipping((Rectangle
) null);
1553 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1560 * The rectangle of the area
1561 * @param timeProvider
1564 * The array items to draw
1566 * The width reserved for the names
1568 * Reference to the SWT GC object
1571 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1572 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1576 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1577 for (ILinkEvent event
: links
) {
1578 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1580 gc
.setClipping((Rectangle
) null);
1584 * Draws the link type events of this item
1589 * the container rectangle
1590 * @param timeProvider
1598 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1599 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1602 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1603 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1604 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1606 if ((srcIndex
== -1) || (destIndex
== -1)) {
1610 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1611 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1613 int x0
= getXForTime(event
.getTime());
1614 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1616 // limit the x-coordinates to prevent integer overflow in calculations
1617 // and also GC.drawLine doesn't draw properly with large coordinates
1618 final int limit
= Integer
.MAX_VALUE
/ 1024;
1619 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1620 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1622 int y0
= src
.y
+ src
.height
/ 2;
1623 int y1
= dst
.y
+ dst
.height
/ 2;
1624 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1628 * Draw the state (color fill)
1633 * Time event for which we're drawing the state
1638 * @return true if the state was drawn
1641 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1642 Rectangle rect
, GC gc
) {
1647 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1651 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1654 Color stateColor
= null;
1655 if (colorIdx
< fEventColorMap
.length
) {
1656 stateColor
= fEventColorMap
[colorIdx
];
1658 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1661 gc
.setForeground(stateColor
);
1662 gc
.setBackground(stateColor
);
1664 /* Draw the arrow */
1665 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1666 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1669 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1674 * @author Francis Giraldeau
1677 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1679 * The algorithm was taken from this site, not the code itself
1681 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1684 double cos
= 0.9510;
1685 double sin
= 0.3090;
1686 long lenx
= x1
- x0
;
1687 long leny
= y1
- y0
;
1688 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1690 double dx
= factor
* lenx
/ len
;
1691 double dy
= factor
* leny
/ len
;
1692 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1693 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1694 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1695 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1696 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1697 gc
.fillPolygon(arrow
);
1701 * Draw the name of an item.
1706 * Where to draw the name
1710 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1711 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1712 if (! hasTimeEvents
) {
1713 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1714 gc
.fillRectangle(bounds
);
1715 if (item
.fSelected
&& fIsInFocus
) {
1716 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1717 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1720 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1721 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1722 gc
.fillRectangle(bounds
);
1725 // No name to be drawn
1726 if (fTimeProvider
.getNameSpace() == 0) {
1730 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1731 if (item
.fHasChildren
) {
1732 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1733 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1734 Rectangle rect
= Utils
.clone(bounds
);
1735 rect
.x
+= leftMargin
;
1736 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1737 rect
.width
= EXPAND_SIZE
;
1738 rect
.height
= EXPAND_SIZE
;
1739 gc
.fillRectangle(rect
);
1740 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1741 int midy
= rect
.y
+ rect
.height
/ 2;
1742 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1743 if (!item
.fExpanded
) {
1744 int midx
= rect
.x
+ rect
.width
/ 2;
1745 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1748 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1750 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1753 int imgHeight
= img
.getImageData().height
;
1754 int imgWidth
= img
.getImageData().width
;
1756 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1757 gc
.drawImage(img
, x
, y
);
1758 leftMargin
+= imgWidth
+ MARGIN
;
1760 String name
= item
.fName
;
1761 Point size
= gc
.stringExtent(name
);
1762 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1763 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1765 if (hasTimeEvents
) {
1766 // cut long string with "..."
1767 int width
= bounds
.width
- leftMargin
;
1769 while (size
.x
> width
&& name
.length() > 1) {
1771 name
= name
.substring(0, name
.length() - 1);
1772 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1775 name
+= "..."; //$NON-NLS-1$
1778 Rectangle rect
= Utils
.clone(bounds
);
1779 rect
.x
+= leftMargin
;
1780 rect
.width
-= leftMargin
;
1782 if (rect
.width
> 0) {
1783 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1784 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1785 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1786 leftMargin
+= textWidth
+ MARGIN
;
1789 if (hasTimeEvents
) {
1791 int x
= bounds
.x
+ leftMargin
;
1792 int width
= bounds
.width
- x
;
1793 int midy
= bounds
.y
+ bounds
.height
/ 2;
1794 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1795 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1801 * Draw the state (color fill)
1806 * Time event for which we're drawing the state
1812 * Is this time event currently selected (so it appears
1814 * @param timeSelected
1815 * Is the timestamp currently selected
1816 * @return true if the state was drawn
1819 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1820 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1822 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1823 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1826 boolean visible
= rect
.width
== 0 ?
false : true;
1827 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1828 gc
.setForeground(black
);
1831 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1832 // Only draw the top and bottom borders
1833 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1834 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1835 if (rect
.width
== 1) {
1836 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1840 Color stateColor
= null;
1841 if (colorIdx
< fEventColorMap
.length
) {
1842 stateColor
= fEventColorMap
[colorIdx
];
1847 boolean reallySelected
= timeSelected
&& selected
;
1848 // fill all rect area
1849 gc
.setBackground(stateColor
);
1850 gc
.fillRectangle(rect
);
1852 if (reallySelected
) {
1853 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1854 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1857 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1859 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1864 * Fill the space between two contiguous time events
1871 * Is this time event selected or not
1873 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1874 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1875 gc
.fillRectangle(rect
);
1876 if (fDragState
== DRAG_ZOOM
) {
1877 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1878 if (fDragX0
< fDragX
) {
1879 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1880 } else if (fDragX0
> fDragX
) {
1881 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1885 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1886 int midy
= rect
.y
+ rect
.height
/ 2;
1887 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1891 public void keyTraversed(TraverseEvent e
) {
1892 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1898 public void keyPressed(KeyEvent e
) {
1900 if (fItemData
.fExpandedItems
.length
== 0) {
1903 if (SWT
.HOME
== e
.keyCode
) {
1905 } else if (SWT
.END
== e
.keyCode
) {
1906 idx
= fItemData
.fExpandedItems
.length
- 1;
1907 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1908 idx
= getSelectedIndex();
1911 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1914 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1915 idx
= getSelectedIndex();
1918 } else if (idx
> 0) {
1921 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1923 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1925 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1926 int page
= countPerPage();
1927 idx
= getSelectedIndex();
1932 if (idx
>= fItemData
.fExpandedItems
.length
) {
1933 idx
= fItemData
.fExpandedItems
.length
- 1;
1935 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1936 int page
= countPerPage();
1937 idx
= getSelectedIndex();
1945 } else if (SWT
.CR
== e
.keyCode
) {
1946 idx
= getSelectedIndex();
1948 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1951 fireDefaultSelection();
1957 selectItem(idx
, false);
1958 fireSelectionChanged();
1960 int x
= toControl(e
.display
.getCursorLocation()).x
;
1961 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1965 public void keyReleased(KeyEvent e
) {
1966 int x
= toControl(e
.display
.getCursorLocation()).x
;
1967 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1971 public void focusGained(FocusEvent e
) {
1973 if (fMouseScrollFilterListener
== null) {
1974 fMouseScrollFilterListener
= new Listener() {
1975 // This filter is used to prevent horizontal scrolling of the view
1976 // when the mouse wheel is used to zoom
1978 public void handleEvent(Event event
) {
1982 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1985 updateStatusLine(NO_STATUS
);
1989 public void focusLost(FocusEvent e
) {
1991 if (fMouseScrollFilterListener
!= null) {
1992 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1993 fMouseScrollFilterListener
= null;
1995 if (DRAG_NONE
!= fDragState
) {
1997 fDragState
= DRAG_NONE
;
2000 updateStatusLine(NO_STATUS
);
2004 * @return If the current view is focused
2006 public boolean isInFocus() {
2011 * Provide the possibility to control the wait cursor externally e.g. data
2012 * requests in progress
2014 * @param waitInd Should we wait indefinitely?
2016 public void waitCursor(boolean waitInd
) {
2017 // Update cursor as indicated
2019 setCursor(fWaitCursor
);
2025 private void updateCursor(int x
, int stateMask
) {
2026 // if Wait cursor not active, check for the need to change the cursor
2027 if (getCursor() == fWaitCursor
) {
2030 Cursor cursor
= null;
2031 if (fDragState
== DRAG_SPLIT_LINE
) {
2032 } else if (fDragState
== DRAG_SELECTION
) {
2033 cursor
= fResizeCursor
;
2034 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2035 cursor
= fDragCursor
;
2036 } else if (fDragState
== DRAG_ZOOM
) {
2037 cursor
= fZoomCursor
;
2038 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2039 cursor
= fDragCursor
;
2040 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2041 cursor
= fResizeCursor
;
2042 } else if (!isOverSplitLine(x
)) {
2043 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2044 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2045 int xBegin
= getXForTime(selectionBegin
);
2046 int xEnd
= getXForTime(selectionEnd
);
2047 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2048 cursor
= fResizeCursor
;
2051 if (getCursor() != cursor
) {
2056 private void updateStatusLine(int x
) {
2057 // use the time provider of the time graph scale for the status line
2058 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2059 if (fStatusLineManager
== null || null == tdp
||
2060 tdp
.getTime0() == tdp
.getTime1()) {
2063 TimeFormat tf
= tdp
.getTimeFormat();
2064 Resolution res
= Resolution
.NANOSEC
;
2065 StringBuilder message
= new StringBuilder();
2066 if (x
>= 0 && fDragState
== DRAG_NONE
) {
2067 long time
= getTimeAtX(x
);
2069 if (tdp
instanceof ITimeDataProviderConverter
) {
2070 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2072 long selectionBegin
= tdp
.getSelectionBegin();
2073 long selectionEnd
= tdp
.getSelectionEnd();
2074 message
.append(NLS
.bind("T: {0}{1} T1: {2}{3}", //$NON-NLS-1$
2076 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2077 Utils
.formatTime(time
, tf
, res
),
2078 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
2079 Utils
.formatTime(Math
.min(selectionBegin
, selectionEnd
), tf
, res
)
2081 if (selectionBegin
!= selectionEnd
) {
2082 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2084 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
2085 Utils
.formatTime(Math
.max(selectionBegin
, selectionEnd
), tf
, res
),
2086 Utils
.formatDelta(Math
.abs(selectionBegin
- selectionEnd
), tf
, res
)
2090 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2091 long time0
= fDragTime0
;
2092 long time
= getTimeAtX(fDragX
);
2093 if (tdp
instanceof ITimeDataProviderConverter
) {
2094 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2095 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2097 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2099 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2100 Utils
.formatTime(Math
.min(time
, time0
), tf
, res
),
2101 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2102 Utils
.formatTime(Math
.max(time
, time0
), tf
, res
),
2103 Utils
.formatDelta(Math
.abs(time
- time0
), tf
, res
)
2106 fStatusLineManager
.setMessage(message
.toString());
2110 public void mouseMove(MouseEvent e
) {
2111 if (null == fTimeProvider
) {
2114 Point size
= getCtrlSize();
2115 if (DRAG_TRACE_ITEM
== fDragState
) {
2116 int nameWidth
= fTimeProvider
.getNameSpace();
2117 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2119 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2120 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2121 long time1
= fTime1bak
- timeDelta
;
2122 long maxTime
= fTimeProvider
.getMaxTime();
2123 if (time1
> maxTime
) {
2126 long time0
= time1
- (fTime1bak
- fTime0bak
);
2127 if (time0
< fTimeProvider
.getMinTime()) {
2128 time0
= fTimeProvider
.getMinTime();
2129 time1
= time0
+ (fTime1bak
- fTime0bak
);
2131 fTimeProvider
.setStartFinishTime(time0
, time1
);
2133 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2135 fTimeProvider
.setNameSpace(e
.x
);
2136 } else if (DRAG_SELECTION
== fDragState
) {
2137 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2139 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2140 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2141 } else if (DRAG_ZOOM
== fDragState
) {
2142 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2144 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2145 } else if (DRAG_NONE
== fDragState
) {
2146 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2147 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2150 fMouseOverSplitLine
= mouseOverSplitLine
;
2152 updateCursor(e
.x
, e
.stateMask
);
2153 updateStatusLine(e
.x
);
2157 public void mouseDoubleClick(MouseEvent e
) {
2158 if (null == fTimeProvider
) {
2161 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2162 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2163 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2164 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2165 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2168 fMouseOverSplitLine
= mouseOverSplitLine
;
2171 int idx
= getItemIndexAtY(e
.y
);
2173 selectItem(idx
, false);
2174 fireDefaultSelection();
2180 public void mouseDown(MouseEvent e
) {
2181 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2182 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2183 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2187 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2188 int nameSpace
= fTimeProvider
.getNameSpace();
2189 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2190 fDragState
= DRAG_SPLIT_LINE
;
2191 fDragButton
= e
.button
;
2194 fTime0bak
= fTimeProvider
.getTime0();
2195 fTime1bak
= fTimeProvider
.getTime1();
2197 updateCursor(e
.x
, e
.stateMask
);
2201 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2202 int nameSpace
= fTimeProvider
.getNameSpace();
2203 idx
= getItemIndexAtY(e
.y
);
2205 Item item
= fItemData
.fExpandedItems
[idx
];
2206 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2210 selectItem(idx
, false);
2211 fireSelectionChanged();
2213 selectItem(idx
, false); // clear selection
2214 fireSelectionChanged();
2216 long hitTime
= getTimeAtX(e
.x
);
2220 fDragState
= DRAG_SELECTION
;
2221 fDragButton
= e
.button
;
2224 fDragTime0
= getTimeAtX(fDragX0
);
2225 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2226 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2227 int xBegin
= getXForTime(selectionBegin
);
2228 int xEnd
= getXForTime(selectionEnd
);
2229 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2230 long time
= getTimeAtX(e
.x
);
2231 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2233 fDragTime0
= selectionEnd
;
2236 fDragTime0
= selectionBegin
;
2239 long time
= getTimeAtX(e
.x
);
2240 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2242 fDragTime0
= selectionEnd
;
2243 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2245 fDragTime0
= selectionBegin
;
2248 fTime0bak
= fTimeProvider
.getTime0();
2249 fTime1bak
= fTimeProvider
.getTime1();
2251 updateCursor(e
.x
, e
.stateMask
);
2252 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2254 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2255 long hitTime
= getTimeAtX(e
.x
);
2258 fDragState
= DRAG_TRACE_ITEM
;
2259 fDragButton
= e
.button
;
2262 fTime0bak
= fTimeProvider
.getTime0();
2263 fTime1bak
= fTimeProvider
.getTime1();
2264 updateCursor(e
.x
, e
.stateMask
);
2266 } else if (3 == e
.button
) {
2268 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2270 fDragTime0
= getTimeAtX(fDragX0
);
2271 fDragState
= DRAG_ZOOM
;
2272 fDragButton
= e
.button
;
2274 updateCursor(e
.x
, e
.stateMask
);
2275 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2280 public void mouseUp(MouseEvent e
) {
2281 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2282 menuDetected(fPendingMenuDetectEvent
);
2284 if (DRAG_NONE
!= fDragState
) {
2286 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2287 if (fDragX
!= fDragX0
) {
2288 fTimeProvider
.notifyStartFinishTime();
2290 fDragState
= DRAG_NONE
;
2291 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2292 fDragState
= DRAG_NONE
;
2294 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2295 if (fDragX
== fDragX0
) { // click without selecting anything
2296 long time
= getTimeAtX(e
.x
);
2297 fTimeProvider
.setSelectedTimeNotify(time
, false);
2299 long time0
= fDragTime0
;
2300 long time1
= getTimeAtX(fDragX
);
2301 if (time0
<= time1
) {
2302 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2304 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2307 fDragState
= DRAG_NONE
;
2309 fTimeGraphScale
.setDragRange(-1, -1);
2310 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2311 int nameWidth
= fTimeProvider
.getNameSpace();
2312 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2313 long time0
= getTimeAtX(fDragX0
);
2314 long time1
= getTimeAtX(fDragX
);
2315 if (time0
< time1
) {
2316 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2318 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2323 fDragState
= DRAG_NONE
;
2324 fTimeGraphScale
.setDragRange(-1, -1);
2327 updateCursor(e
.x
, e
.stateMask
);
2328 updateStatusLine(e
.x
);
2332 public void mouseEnter(MouseEvent e
) {
2336 public void mouseExit(MouseEvent e
) {
2337 if (fMouseOverSplitLine
) {
2338 fMouseOverSplitLine
= false;
2341 updateStatusLine(NO_STATUS
);
2345 public void mouseHover(MouseEvent e
) {
2349 public void mouseScrolled(MouseEvent e
) {
2350 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2353 boolean zoomScroll
= false;
2354 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2355 Point parentSize
= getParent().getSize();
2356 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2357 // over the parent control
2358 if (e
.x
> getCtrlSize().x
) {
2359 // over the vertical scroll bar
2361 } else if (e
.y
< 0 || e
.y
>= getCtrlSize().y
) {
2362 // over the time scale or horizontal scroll bar
2365 if (e
.x
< fTimeProvider
.getNameSpace()) {
2366 // over the name space
2369 // over the state area
2370 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2371 // over the state area, CTRL pressed
2374 // over the state area, CTRL not pressed
2380 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2383 } else if (e
.count
< 0) {
2387 setTopIndex(getTopIndex() - e
.count
);
2392 public void controlMoved(ControlEvent e
) {
2396 public void controlResized(ControlEvent e
) {
2401 public void widgetDefaultSelected(SelectionEvent e
) {
2405 public void widgetSelected(SelectionEvent e
) {
2406 if (e
.widget
== getVerticalBar()) {
2407 setTopIndex(getVerticalBar().getSelection());
2408 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2409 int start
= getHorizontalBar().getSelection();
2410 long time0
= fTimeProvider
.getTime0();
2411 long time1
= fTimeProvider
.getTime1();
2412 long timeMin
= fTimeProvider
.getMinTime();
2413 long timeMax
= fTimeProvider
.getMaxTime();
2414 long delta
= timeMax
- timeMin
;
2416 long range
= time1
- time0
;
2417 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2418 time1
= time0
+ range
;
2420 // TODO: Follow-up with Bug 310310
2421 // In Linux SWT.DRAG is the only value received
2422 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2423 if (e
.detail
== SWT
.DRAG
) {
2424 fTimeProvider
.setStartFinishTime(time0
, time1
);
2426 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2432 public int getBorderWidth() {
2433 return fBorderWidth
;
2437 * Set the border width
2439 * @param borderWidth
2442 public void setBorderWidth(int borderWidth
) {
2443 this.fBorderWidth
= borderWidth
;
2447 * @return The current height of the header row
2449 public int getHeaderHeight() {
2450 return fHeaderHeight
;
2454 * Set the height of the header row
2456 * @param headerHeight
2459 public void setHeaderHeight(int headerHeight
) {
2460 this.fHeaderHeight
= headerHeight
;
2464 * @return The default height of regular item rows
2466 public int getItemHeight() {
2467 return fGlobalItemHeight
;
2471 * Set the default height of regular item rows.
2476 public void setItemHeight(int rowHeight
) {
2477 this.fGlobalItemHeight
= rowHeight
;
2481 * Set the height of a specific item. Overrides the default item height.
2484 * A time graph entry
2487 * @return true if the height is successfully stored, false otherwise
2491 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2492 Item item
= fItemData
.findItem(entry
);
2494 item
.fItemHeight
= rowHeight
;
2501 * Set the minimum item width
2503 * @param width The minimum width
2505 public void setMinimumItemWidth(int width
) {
2506 this.fMinimumItemWidth
= width
;
2510 * @return The minimum item width
2512 public int getMinimumItemWidth() {
2513 return fMinimumItemWidth
;
2517 * @return The entries that are currently filtered out
2521 public List
<ITimeGraphEntry
> getFilteredOut() {
2522 return fItemData
.getFilteredOut();
2526 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2527 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2528 fSelectionChangedListeners
.add(listener
);
2533 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2534 if (listener
!= null) {
2535 fSelectionChangedListeners
.remove(listener
);
2540 public void setSelection(ISelection selection
) {
2541 if (selection
instanceof TimeGraphSelection
) {
2542 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2543 Object ob
= sel
.getFirstElement();
2544 if (ob
instanceof ITimeGraphEntry
) {
2545 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2546 selectItem(trace
, false);
2553 * @param filter The filter object to be attached to the view
2556 public void addFilter(ViewerFilter filter
) {
2557 if (!fFilters
.contains(filter
)) {
2558 fFilters
.add(filter
);
2563 * @param filter The filter object to be attached to the view
2566 public void removeFilter(ViewerFilter filter
) {
2567 fFilters
.remove(filter
);
2574 public void colorSettingsChanged(StateItem
[] stateItems
) {
2575 /* Destroy previous colors from the resource manager */
2576 if (fEventColorMap
!= null) {
2577 for (Color color
: fEventColorMap
) {
2578 fResourceManager
.destroyColor(color
.getRGB());
2581 if (stateItems
!= null) {
2582 fEventColorMap
= new Color
[stateItems
.length
];
2583 for (int i
= 0; i
< stateItems
.length
; i
++) {
2584 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2587 fEventColorMap
= new Color
[] { };
2592 private class ItemData
{
2593 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2594 private Item
[] fExpandedItems
= new Item
[0];
2595 private Item
[] fItems
= new Item
[0];
2596 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2597 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2598 private boolean fEntryFilter
[] = new boolean[0];
2599 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2604 public Item
findItem(ITimeGraphEntry entry
) {
2605 return fItemMap
.get(entry
);
2608 public int findItemIndex(ITimeGraphEntry entry
) {
2609 Item item
= fItemMap
.get(entry
);
2613 return item
.fExpandedIndex
;
2616 public void refreshData() {
2618 fFilteredOut
.clear();
2619 ITimeGraphEntry selection
= getSelectedTrace();
2620 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2621 ITimeGraphEntry entry
= fRootEntries
[i
];
2622 refreshData(fItemMap
, null, 0, entry
);
2624 fItems
= fItemMap
.values().toArray(new Item
[0]);
2625 updateExpandedItems();
2626 if (selection
!= null) {
2627 for (Item item
: fExpandedItems
) {
2628 if (item
.fEntry
== selection
) {
2629 item
.fSelected
= true;
2636 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2637 Item item
= new Item(entry
, entry
.getName(), level
);
2638 if (parent
!= null) {
2639 parent
.fChildren
.add(item
);
2641 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2642 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2644 item
.fItemHeight
= fGlobalItemHeight
;
2646 itemMap
.put(entry
, item
);
2647 if (entry
.hasChildren()) {
2648 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2649 item
.fHasChildren
= true;
2650 for (ITimeGraphEntry child
: entry
.getChildren()) {
2651 refreshData(itemMap
, item
, level
+ 1, child
);
2656 public void updateExpandedItems() {
2657 for (Item item
: fItems
) {
2658 item
.fExpandedIndex
= -1;
2660 List
<Item
> expandedItemList
= new ArrayList
<>();
2661 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2662 ITimeGraphEntry entry
= fRootEntries
[i
];
2663 Item item
= findItem(entry
);
2664 refreshExpanded(expandedItemList
, item
);
2666 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2667 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2670 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2671 // Check for filters
2672 boolean display
= true;
2673 for (ViewerFilter filter
: fFilters
) {
2674 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2680 item
.fExpandedIndex
= expandedItemList
.size();
2681 expandedItemList
.add(item
);
2682 if (item
.fHasChildren
&& item
.fExpanded
) {
2683 for (Item child
: item
.fChildren
) {
2684 refreshExpanded(expandedItemList
, child
);
2690 public void refreshData(ITimeGraphEntry
[] entries
) {
2691 if (entries
== null) {
2692 fEntryFilter
= null;
2693 fRootEntries
= null;
2695 if (entries
.length
== 0) {
2696 fEntryFilter
= null;
2697 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2698 fEntryFilter
= new boolean[entries
.length
];
2699 java
.util
.Arrays
.fill(fEntryFilter
, true);
2701 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2707 public void refreshArrows(List
<ILinkEvent
> events
) {
2708 /* If links are null, reset the list */
2709 if (events
!= null) {
2712 fLinks
= new ArrayList
<>();
2716 public ITimeGraphEntry
[] getEntries() {
2717 return fRootEntries
;
2720 public boolean[] getEntryFilter() {
2721 return fEntryFilter
;
2724 public List
<ITimeGraphEntry
> getFilteredOut() {
2725 return fFilteredOut
;
2729 private class Item
{
2730 private boolean fExpanded
;
2731 private int fExpandedIndex
;
2732 private boolean fSelected
;
2733 private boolean fHasChildren
;
2734 private int fItemHeight
;
2735 private final int fLevel
;
2736 private final List
<Item
> fChildren
;
2737 private final String fName
;
2738 private final ITimeGraphEntry fEntry
;
2740 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2741 this.fEntry
= entry
;
2743 this.fLevel
= level
;
2744 this.fChildren
= new ArrayList
<>();
2748 public String
toString() {
2757 public void menuDetected(MenuDetectEvent e
) {
2758 if (null == fTimeProvider
) {
2761 if (e
.detail
== SWT
.MENU_MOUSE
) {
2762 if (fPendingMenuDetectEvent
== null) {
2763 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2764 * Store the event and trigger it later just before handling mouseUp.
2765 * This allows for the method to detect if mouse is used to drag zoom.
2767 fPendingMenuDetectEvent
= e
;
2770 fPendingMenuDetectEvent
= null;
2771 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2775 if (fDragState
!= DRAG_NONE
) {
2779 Point p
= toControl(e
.x
, e
.y
);
2780 int idx
= getItemIndexAtY(p
.y
);
2781 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2782 Item item
= fItemData
.fExpandedItems
[idx
];
2783 ITimeGraphEntry entry
= item
.fEntry
;
2784 if (entry
.hasTimeEvents()) {
2785 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2786 if (event
!= null) {
2788 fireMenuEventOnTimeEvent(e
);
2793 fireMenuEventOnTimeGraphEntry(e
);