1 /*****************************************************************************
2 * Copyright (c) 2007, 2015 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Iterator
;
26 import java
.util
.LinkedHashMap
;
27 import java
.util
.List
;
30 import org
.eclipse
.jface
.action
.IStatusLineManager
;
31 import org
.eclipse
.jface
.resource
.JFaceResources
;
32 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
33 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
34 import org
.eclipse
.jface
.viewers
.ISelection
;
35 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
36 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
37 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
38 import org
.eclipse
.osgi
.util
.NLS
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.events
.FocusEvent
;
41 import org
.eclipse
.swt
.events
.FocusListener
;
42 import org
.eclipse
.swt
.events
.KeyEvent
;
43 import org
.eclipse
.swt
.events
.KeyListener
;
44 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
45 import org
.eclipse
.swt
.events
.MenuDetectListener
;
46 import org
.eclipse
.swt
.events
.MouseEvent
;
47 import org
.eclipse
.swt
.events
.MouseListener
;
48 import org
.eclipse
.swt
.events
.MouseMoveListener
;
49 import org
.eclipse
.swt
.events
.MouseTrackListener
;
50 import org
.eclipse
.swt
.events
.MouseWheelListener
;
51 import org
.eclipse
.swt
.events
.PaintEvent
;
52 import org
.eclipse
.swt
.events
.SelectionListener
;
53 import org
.eclipse
.swt
.events
.TraverseEvent
;
54 import org
.eclipse
.swt
.events
.TraverseListener
;
55 import org
.eclipse
.swt
.events
.TypedEvent
;
56 import org
.eclipse
.swt
.graphics
.Color
;
57 import org
.eclipse
.swt
.graphics
.Cursor
;
58 import org
.eclipse
.swt
.graphics
.GC
;
59 import org
.eclipse
.swt
.graphics
.Image
;
60 import org
.eclipse
.swt
.graphics
.Point
;
61 import org
.eclipse
.swt
.graphics
.Rectangle
;
62 import org
.eclipse
.swt
.widgets
.Composite
;
63 import org
.eclipse
.swt
.widgets
.Display
;
64 import org
.eclipse
.swt
.widgets
.Event
;
65 import org
.eclipse
.swt
.widgets
.Listener
;
66 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
67 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
85 * Time graph control implementation
87 * @author Alvaro Sanchez-Leon
88 * @author Patrick Tasse
90 public class TimeGraphControl
extends TimeGraphBaseControl
91 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
92 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
93 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
95 /** Constant indicating that all levels of the time graph should be expanded */
96 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
98 private static final int DRAG_NONE
= 0;
99 private static final int DRAG_TRACE_ITEM
= 1;
100 private static final int DRAG_SPLIT_LINE
= 2;
101 private static final int DRAG_ZOOM
= 3;
102 private static final int DRAG_SELECTION
= 4;
104 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
106 private static final double ZOOM_FACTOR
= 1.5;
107 private static final double ZOOM_IN_FACTOR
= 0.8;
108 private static final double ZOOM_OUT_FACTOR
= 1.25;
110 private static final int SNAP_WIDTH
= 2;
111 private static final int ARROW_HOVER_MAX_DIST
= 5;
113 private static final int NO_STATUS
= -1;
115 /** Resource manager */
116 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
118 /** Color map for event types */
119 private Color
[] fEventColorMap
= null;
121 private ITimeDataProvider fTimeProvider
;
122 private IStatusLineManager fStatusLineManager
= null;
123 private TimeGraphScale fTimeGraphScale
= null;
125 private boolean fIsInFocus
= false;
126 private boolean fMouseOverSplitLine
= false;
127 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
128 private int fMinimumItemWidth
= 0;
129 private int fTopIndex
= 0;
130 private int fDragState
= DRAG_NONE
;
131 private int fDragButton
;
132 private int fDragX0
= 0;
133 private int fDragX
= 0;
134 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
135 private int fIdealNameSpace
= 0;
136 private long fTime0bak
;
137 private long fTime1bak
;
138 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
139 private ItemData fItemData
= null;
140 private List
<SelectionListener
> fSelectionListeners
;
141 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
142 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
143 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
144 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
145 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
146 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
147 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
148 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
149 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
150 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
151 private MenuDetectEvent fPendingMenuDetectEvent
= null;
152 private boolean fHideArrows
= false;
153 private int fAutoExpandLevel
= ALL_LEVELS
;
155 private int fBorderWidth
= 0;
156 private int fHeaderHeight
= 0;
159 * Standard constructor
162 * The parent composite object
164 * The color scheme to use
166 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
168 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
170 fItemData
= new ItemData();
172 addFocusListener(this);
173 addMouseListener(this);
174 addMouseMoveListener(this);
175 addMouseTrackListener(this);
176 addMouseWheelListener(this);
177 addTraverseListener(this);
178 addKeyListener(this);
179 addMenuDetectListener(this);
180 addListener(SWT
.MouseWheel
, this);
184 public void dispose() {
186 fResourceManager
.dispose();
190 * Sets the timegraph provider used by this timegraph viewer.
192 * @param timeGraphProvider the timegraph provider
194 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
195 fTimeGraphProvider
= timeGraphProvider
;
197 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
198 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
199 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
202 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
203 colorSettingsChanged(stateItems
);
207 * Gets the timegraph provider used by this timegraph viewer.
209 * @return the timegraph provider, or <code>null</code> if not set.
211 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
212 return fTimeGraphProvider
;
216 * Gets the color map used by this timegraph viewer.
218 * @return a color map, or <code>null</code> if not set.
220 public Color
[] getEventColorMap() {
221 return fEventColorMap
;
225 * Assign the given time provider
227 * @param timeProvider
230 public void setTimeProvider(ITimeDataProvider timeProvider
) {
231 fTimeProvider
= timeProvider
;
236 * Assign the status line manager
238 * @param statusLineManager
239 * The status line manager, or null to disable status line messages
241 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
242 if (fStatusLineManager
!= null && statusLineManager
== null) {
243 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
245 fStatusLineManager
= statusLineManager
;
249 * Assign the time graph scale
251 * @param timeGraphScale
252 * The time graph scale
254 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
255 fTimeGraphScale
= timeGraphScale
;
259 * Add a selection listener
262 * The listener to add
264 public void addSelectionListener(SelectionListener listener
) {
265 if (listener
== null) {
266 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
268 if (null == fSelectionListeners
) {
269 fSelectionListeners
= new ArrayList
<>();
271 fSelectionListeners
.add(listener
);
275 * Remove a selection listener
278 * The listener to remove
280 public void removeSelectionListener(SelectionListener listener
) {
281 if (null != fSelectionListeners
) {
282 fSelectionListeners
.remove(listener
);
287 * Selection changed callback
289 public void fireSelectionChanged() {
290 if (null != fSelectionListeners
) {
291 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
292 while (it
.hasNext()) {
293 SelectionListener listener
= it
.next();
294 listener
.widgetSelected(null);
300 * Default selection callback
302 public void fireDefaultSelection() {
303 if (null != fSelectionListeners
) {
304 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
305 while (it
.hasNext()) {
306 SelectionListener listener
= it
.next();
307 listener
.widgetDefaultSelected(null);
313 * Add a drag selection listener
316 * The listener to add
318 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
319 if (listener
== null) {
320 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
322 if (null == fDragSelectionListeners
) {
323 fDragSelectionListeners
= new ArrayList
<>();
325 fDragSelectionListeners
.add(listener
);
329 * Remove a drag selection listener
332 * The listener to remove
334 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
335 if (null != fDragSelectionListeners
) {
336 fDragSelectionListeners
.remove(listener
);
341 * Drag Selection changed callback
344 * Time interval start
348 public void fireDragSelectionChanged(long start
, long end
) {
349 // check for backward intervals
350 long beginTime
, endTime
;
358 // call the listeners
359 if (null != fDragSelectionListeners
) {
360 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
361 while (it
.hasNext()) {
362 ITimeGraphTimeListener listener
= it
.next();
363 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
369 * Get the traces in the model
371 * @return The array of traces
373 public ITimeGraphEntry
[] getTraces() {
374 return fItemData
.getEntries();
378 * Get the on/off trace filters
380 * @return The array of filters
382 public boolean[] getTraceFilter() {
383 return fItemData
.getEntryFilter();
387 * Refresh the data for the thing
389 public void refreshData() {
390 fItemData
.refreshData();
395 * Refresh data for the given traces
398 * The traces to refresh
400 public void refreshData(ITimeGraphEntry
[] traces
) {
401 fItemData
.refreshData(traces
);
406 * Refresh the links (arrows) of this widget
408 * @param events The link events to refresh
410 public void refreshArrows(List
<ILinkEvent
> events
) {
411 fItemData
.refreshArrows(events
);
414 boolean ensureVisibleItem(int idx
, boolean redraw
) {
415 boolean changed
= false;
418 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
419 if (fItemData
.fExpandedItems
[index
].fSelected
) {
424 if (index
>= fItemData
.fExpandedItems
.length
) {
427 if (index
< fTopIndex
) {
434 int page
= countPerPage();
435 if (index
>= fTopIndex
+ page
) {
436 setTopIndex(index
- page
+ 1);
447 * Assign the given index as the top one
452 public void setTopIndex(int idx
) {
453 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
454 index
= Math
.max(0, index
);
460 * Sets the auto-expand level to be used when the entries are refreshed
461 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
462 * The value 0 means that there is no auto-expand; 1 means that top-level
463 * entries are expanded, but not their children; 2 means that top-level
464 * entries are expanded, and their children, but not grand-children; and so
467 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
470 * non-negative level, or <code>ALL_LEVELS</code> to expand all
473 public void setAutoExpandLevel(int level
) {
474 fAutoExpandLevel
= level
;
478 * Returns the auto-expand level.
480 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
481 * the tree are expanded automatically
482 * @see #setAutoExpandLevel
484 public int getAutoExpandLevel() {
485 return fAutoExpandLevel
;
489 * Set the expanded state of a given entry
494 * True if expanded, false if collapsed
496 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
497 Item item
= fItemData
.findItem(entry
);
498 if (item
!= null && item
.fExpanded
!= expanded
) {
499 item
.fExpanded
= expanded
;
500 fItemData
.updateExpandedItems();
506 * Collapses all nodes of the viewer's tree, starting with the root.
508 public void collapseAll() {
509 for (Item item
: fItemData
.fItems
) {
510 item
.fExpanded
= false;
512 fItemData
.updateExpandedItems();
517 * Expands all nodes of the viewer's tree, starting with the root.
519 public void expandAll() {
520 for (Item item
: fItemData
.fItems
) {
521 item
.fExpanded
= true;
523 fItemData
.updateExpandedItems();
528 * Add a tree listener
531 * The listener to add
533 public void addTreeListener(ITimeGraphTreeListener listener
) {
534 if (!fTreeListeners
.contains(listener
)) {
535 fTreeListeners
.add(listener
);
540 * Remove a tree listener
543 * The listener to remove
545 public void removeTreeListener(ITimeGraphTreeListener listener
) {
546 if (fTreeListeners
.contains(listener
)) {
547 fTreeListeners
.remove(listener
);
552 * Tree event callback
557 * The expanded state (true for expanded, false for collapsed)
559 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
560 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
561 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
563 listener
.treeExpanded(event
);
565 listener
.treeCollapsed(event
);
571 * Add a menu listener on {@link ITimeGraphEntry}s
574 * The listener to add
576 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
577 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
578 fTimeGraphEntryMenuListeners
.add(listener
);
583 * Remove a menu listener on {@link ITimeGraphEntry}s
586 * The listener to remove
588 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
589 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
590 fTimeGraphEntryMenuListeners
.remove(listener
);
595 * Menu event callback on {@link ITimeGraphEntry}s
598 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
600 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
601 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
602 listener
.menuDetected(event
);
607 * Add a menu listener on {@link ITimeEvent}s
610 * The listener to add
612 public void addTimeEventMenuListener(MenuDetectListener listener
) {
613 if (!fTimeEventMenuListeners
.contains(listener
)) {
614 fTimeEventMenuListeners
.add(listener
);
619 * Remove a menu listener on {@link ITimeEvent}s
622 * The listener to remove
624 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
625 if (fTimeEventMenuListeners
.contains(listener
)) {
626 fTimeEventMenuListeners
.remove(listener
);
631 * Menu event callback on {@link ITimeEvent}s
634 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
636 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
637 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
638 listener
.menuDetected(event
);
643 public ISelection
getSelection() {
644 TimeGraphSelection sel
= new TimeGraphSelection();
645 ITimeGraphEntry trace
= getSelectedTrace();
646 if (null != trace
&& null != fTimeProvider
) {
647 long selectedTime
= fTimeProvider
.getSelectionBegin();
648 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
659 * Get the selection object
661 * @return The selection
663 public ISelection
getSelectionTrace() {
664 TimeGraphSelection sel
= new TimeGraphSelection();
665 ITimeGraphEntry trace
= getSelectedTrace();
673 * Enable/disable one of the traces in the model
676 * 1 to enable it, -1 to disable. The method returns immediately
677 * if another value is used.
679 public void selectTrace(int n
) {
680 if ((n
!= 1) && (n
!= -1)) {
684 boolean changed
= false;
685 int lastSelection
= -1;
686 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
687 Item item
= fItemData
.fExpandedItems
[i
];
688 if (item
.fSelected
) {
690 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
691 item
.fSelected
= false;
692 item
= fItemData
.fExpandedItems
[i
+ 1];
693 item
.fSelected
= true;
695 } else if ((-1 == n
) && (i
> 0)) {
696 item
.fSelected
= false;
697 item
= fItemData
.fExpandedItems
[i
- 1];
698 item
.fSelected
= true;
705 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
706 Item item
= fItemData
.fExpandedItems
[0];
707 item
.fSelected
= true;
712 ensureVisibleItem(-1, false);
714 fireSelectionChanged();
722 * 1 for next event, -1 for previous event
724 public void selectEvent(int n
) {
725 if (null == fTimeProvider
) {
728 ITimeGraphEntry trace
= getSelectedTrace();
732 long selectedTime
= fTimeProvider
.getSelectionBegin();
733 long endTime
= fTimeProvider
.getMaxTime();
734 ITimeEvent nextEvent
;
735 if (-1 == n
&& selectedTime
> endTime
) {
736 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
738 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
740 if (null == nextEvent
&& -1 == n
) {
741 nextEvent
= Utils
.getFirstEvent(trace
);
743 if (null != nextEvent
) {
744 long nextTime
= nextEvent
.getTime();
745 // If last event detected e.g. going back or not moving to a next
747 if (nextTime
<= selectedTime
&& n
== 1) {
748 // Select to the end of this last event
749 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
750 // but not beyond the end of the trace
751 if (nextTime
> endTime
) {
754 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
755 // for previous event go to its end time unless we were already there
756 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
758 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
759 fireSelectionChanged();
761 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
762 fireSelectionChanged();
767 * Select the next event
769 public void selectNextEvent() {
771 // Notify if visible time window has been adjusted
772 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
776 * Select the previous event
778 public void selectPrevEvent() {
780 // Notify if visible time window has been adjusted
781 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
785 * Select the next trace
787 public void selectNextTrace() {
792 * Select the previous trace
794 public void selectPrevTrace() {
799 * Scroll left or right by one half window size
802 * true to scroll left, false to scroll right
804 public void horizontalScroll(boolean left
) {
805 long time0
= fTimeProvider
.getTime0();
806 long time1
= fTimeProvider
.getTime1();
807 long timeMin
= fTimeProvider
.getMinTime();
808 long timeMax
= fTimeProvider
.getMaxTime();
809 long range
= time1
- time0
;
813 long increment
= Math
.max(1, range
/ 2);
815 time0
= Math
.max(time0
- increment
, timeMin
);
816 time1
= time0
+ range
;
818 time1
= Math
.min(time1
+ increment
, timeMax
);
819 time0
= time1
- range
;
821 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
825 * Zoom based on mouse cursor location with mouse scrolling
827 * @param zoomIn true to zoom in, false to zoom out
829 public void zoom(boolean zoomIn
) {
830 int globalX
= getDisplay().getCursorLocation().x
;
831 Point p
= toControl(globalX
, 0);
832 int nameSpace
= fTimeProvider
.getNameSpace();
833 int timeSpace
= fTimeProvider
.getTimeSpace();
834 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
835 long time0
= fTimeProvider
.getTime0();
836 long time1
= fTimeProvider
.getTime1();
837 long interval
= time1
- time0
;
840 } // to allow getting out of single point interval
843 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
845 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
847 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
848 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
849 long newTime1
= newTime0
+ newInterval
;
850 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
854 * zoom in using single click
856 public void zoomIn() {
857 long prevTime0
= fTimeProvider
.getTime0();
858 long prevTime1
= fTimeProvider
.getTime1();
859 long prevRange
= prevTime1
- prevTime0
;
860 if (prevRange
== 0) {
863 ITimeDataProvider provider
= fTimeProvider
;
864 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
865 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
866 selTime
= (prevTime0
+ prevTime1
) / 2;
868 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
869 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
871 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
873 if (inaccuracy
> 0 && inaccuracy
< 100) {
874 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
878 long min
= fTimeProvider
.getMinTimeInterval();
879 if ((time1
- time0
) < min
) {
880 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
884 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
888 * zoom out using single click
890 public void zoomOut() {
891 long prevTime0
= fTimeProvider
.getTime0();
892 long prevTime1
= fTimeProvider
.getTime1();
893 ITimeDataProvider provider
= fTimeProvider
;
894 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
895 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
896 selTime
= (prevTime0
+ prevTime1
) / 2;
898 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
899 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
901 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
902 if (inaccuracy
> 0 && inaccuracy
< 100) {
903 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
907 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
913 * @param hideArrows true to hide arrows
915 public void hideArrows(boolean hideArrows
) {
916 fHideArrows
= hideArrows
;
920 * Follow the arrow forward
922 public void followArrowFwd() {
923 ITimeGraphEntry trace
= getSelectedTrace();
927 long selectedTime
= fTimeProvider
.getSelectionBegin();
928 for (ILinkEvent link
: fItemData
.fLinks
) {
929 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
930 selectItem(link
.getDestinationEntry(), false);
931 if (link
.getDuration() != 0) {
932 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
933 // Notify if visible time window has been adjusted
934 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
936 fireSelectionChanged();
944 * Follow the arrow backward
946 public void followArrowBwd() {
947 ITimeGraphEntry trace
= getSelectedTrace();
951 long selectedTime
= fTimeProvider
.getSelectionBegin();
952 for (ILinkEvent link
: fItemData
.fLinks
) {
953 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
954 selectItem(link
.getEntry(), false);
955 if (link
.getDuration() != 0) {
956 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
957 // Notify if visible time window has been adjusted
958 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
960 fireSelectionChanged();
968 * Return the currently selected trace
970 * @return The entry matching the trace
972 public ITimeGraphEntry
getSelectedTrace() {
973 ITimeGraphEntry trace
= null;
974 int idx
= getSelectedIndex();
976 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
982 * Retrieve the index of the currently selected item
986 public int getSelectedIndex() {
988 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
989 Item item
= fItemData
.fExpandedItems
[i
];
990 if (item
.fSelected
) {
998 boolean toggle(int idx
) {
999 boolean toggled
= false;
1000 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1001 Item item
= fItemData
.fExpandedItems
[idx
];
1002 if (item
.fHasChildren
) {
1003 item
.fExpanded
= !item
.fExpanded
;
1004 fItemData
.updateExpandedItems();
1007 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1014 * Gets the index of the item at the given location.
1018 * @return the index of the item at the given location, of -1 if none.
1020 protected int getItemIndexAtY(int y
) {
1025 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1026 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1034 boolean isOverSplitLine(int x
) {
1035 if (x
< 0 || null == fTimeProvider
) {
1038 int nameWidth
= fTimeProvider
.getNameSpace();
1039 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1043 * Gets the {@link ITimeGraphEntry} at the given location.
1046 * a point in the widget
1047 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1050 protected ITimeGraphEntry
getEntry(Point pt
) {
1051 int idx
= getItemIndexAtY(pt
.y
);
1052 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1056 * Return the arrow event closest to the given point that is no further than
1057 * a maximum distance.
1060 * a point in the widget
1061 * @return The closest arrow event, or null if there is none close enough.
1063 protected ILinkEvent
getArrow(Point pt
) {
1067 ILinkEvent linkEvent
= null;
1068 double minDistance
= Double
.MAX_VALUE
;
1069 for (ILinkEvent event
: fItemData
.fLinks
) {
1070 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1074 int x2
= x1
+ rect
.width
;
1075 int y2
= y1
+ rect
.height
;
1076 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1077 if (minDistance
> d
) {
1083 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1090 public int getXForTime(long time
) {
1091 if (null == fTimeProvider
) {
1094 long time0
= fTimeProvider
.getTime0();
1095 long time1
= fTimeProvider
.getTime1();
1096 int width
= getSize().x
;
1097 int nameSpace
= fTimeProvider
.getNameSpace();
1098 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1099 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1104 public long getTimeAtX(int coord
) {
1105 if (null == fTimeProvider
) {
1109 Point size
= getSize();
1110 long time0
= fTimeProvider
.getTime0();
1111 long time1
= fTimeProvider
.getTime1();
1112 int nameWidth
= fTimeProvider
.getNameSpace();
1113 final int x
= coord
- nameWidth
;
1114 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1115 if (x
>= 0 && size
.x
>= nameWidth
) {
1116 if (time1
- time0
> timeWidth
) {
1117 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1118 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1120 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1121 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1127 void selectItem(int idx
, boolean addSelection
) {
1128 boolean changed
= false;
1130 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1131 Item item
= fItemData
.fExpandedItems
[idx
];
1132 changed
= !item
.fSelected
;
1133 item
.fSelected
= true;
1136 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1137 Item item
= fItemData
.fExpandedItems
[i
];
1138 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1141 item
.fSelected
= i
== idx
;
1144 changed
|= ensureVisibleItem(idx
, true);
1151 * Callback for item selection
1154 * The entry matching the trace
1155 * @param addSelection
1156 * If the selection is added or removed
1158 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1159 int idx
= fItemData
.findItemIndex(trace
);
1160 selectItem(idx
, addSelection
);
1164 * Retrieve the number of entries shown per page.
1168 public int countPerPage() {
1169 int height
= getSize().y
;
1172 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1173 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1174 if (ySum
>= height
) {
1179 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1180 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1181 if (ySum
>= height
) {
1190 * Get the index of the top element
1194 public int getTopIndex() {
1199 * Get the number of expanded items
1201 * @return The count of expanded items
1203 public int getExpandedElementCount() {
1204 return fItemData
.fExpandedItems
.length
;
1208 * Get an array of all expanded elements
1210 * @return The expanded elements
1212 public ITimeGraphEntry
[] getExpandedElements() {
1213 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1214 for (Item item
: fItemData
.fExpandedItems
) {
1215 elements
.add(item
.fEntry
);
1217 return elements
.toArray(new ITimeGraphEntry
[0]);
1220 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1221 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1223 rect
.width
= nameWidth
;
1227 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1228 int x
= bound
.x
+ nameWidth
;
1229 int width
= bound
.width
- x
;
1231 if (idx
>= fTopIndex
) {
1232 for (int i
= fTopIndex
; i
< idx
; i
++) {
1233 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1236 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1237 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1240 int y
= bound
.y
+ ySum
;
1241 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1242 return new Rectangle(x
, y
, width
, height
);
1246 void paint(Rectangle bounds
, PaintEvent e
) {
1248 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1249 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1251 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1255 fIdealNameSpace
= 0;
1256 int nameSpace
= fTimeProvider
.getNameSpace();
1258 // draw empty name space background
1259 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1260 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1263 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1264 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1265 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1267 int alpha
= gc
.getAlpha();
1270 long time0
= fTimeProvider
.getTime0();
1271 long time1
= fTimeProvider
.getTime1();
1272 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1273 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1274 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1275 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1276 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1278 // draw selection lines
1279 if (fDragState
!= DRAG_SELECTION
) {
1280 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1281 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1282 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1285 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1286 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1291 // draw selection background
1292 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1293 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1294 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1295 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1297 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1298 } else if (x0
- x1
> 1) {
1299 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1303 // draw drag selection background
1304 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1305 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1306 if (fDragX0
< fDragX
) {
1307 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1308 } else if (fDragX0
> fDragX
) {
1309 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1314 if (DRAG_SPLIT_LINE
== fDragState
) {
1315 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1316 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1317 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1318 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1319 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1320 if (fDragX
!= fDragX0
) {
1321 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1323 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1324 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1325 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1326 if (fDragX
!= fDragX0
) {
1327 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1329 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1330 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1331 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1338 * Draw many items at once
1341 * The rectangle of the area
1342 * @param timeProvider
1345 * The array items to draw
1347 * The index of the first element to draw
1349 * The width reserved for the names
1351 * Reference to the SWT GC object
1353 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1354 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1355 for (int i
= topIndex
; i
< items
.length
; i
++) {
1356 Item item
= items
[i
];
1357 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1364 * @param item the item to draw
1365 * @param bounds the container rectangle
1366 * @param timeProvider Time provider
1367 * @param i the item index
1368 * @param nameSpace the name space
1369 * @param gc Graphics context
1371 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1372 ITimeGraphEntry entry
= item
.fEntry
;
1373 long time0
= timeProvider
.getTime0();
1374 long time1
= timeProvider
.getTime1();
1375 long selectedTime
= fTimeProvider
.getSelectionBegin();
1377 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1378 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1382 if (! item
.fEntry
.hasTimeEvents()) {
1383 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1384 nameRect
.width
+= statesRect
.width
;
1385 drawName(item
, nameRect
, gc
);
1387 drawName(item
, nameRect
, gc
);
1389 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1390 if (rect
.isEmpty()) {
1391 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1394 if (time1
<= time0
) {
1395 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1396 gc
.fillRectangle(rect
);
1397 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1401 // Initialize _rect1 to same values as enclosing rectangle rect
1402 Rectangle stateRect
= Utils
.clone(rect
);
1403 boolean selected
= item
.fSelected
;
1404 // K pixels per second
1405 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1407 if (item
.fEntry
.hasTimeEvents()) {
1408 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1409 fillSpace(rect
, gc
, selected
);
1410 // Drawing rectangle is smaller than reserved space
1412 stateRect
.height
-= 6;
1414 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1415 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1418 while (iterator
.hasNext()) {
1419 ITimeEvent event
= iterator
.next();
1420 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1421 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1422 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1423 // event is out of bounds
1426 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1427 stateRect
.x
= Math
.max(rect
.x
, x
);
1428 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1429 if (stateRect
.x
== lastX
) {
1430 stateRect
.width
-= 1;
1431 if (stateRect
.width
> 0) {
1432 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1433 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1437 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1438 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1442 gc
.setClipping((Rectangle
) null);
1444 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1451 * The rectangle of the area
1452 * @param timeProvider
1455 * The array items to draw
1457 * The width reserved for the names
1459 * Reference to the SWT GC object
1461 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1462 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1466 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1467 for (ILinkEvent event
: links
) {
1468 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1470 gc
.setClipping((Rectangle
) null);
1474 * Draws the link type events of this item
1479 * the container rectangle
1480 * @param timeProvider
1487 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1488 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1491 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1492 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1493 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1495 if ((srcIndex
== -1) || (destIndex
== -1)) {
1499 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1500 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1502 int x0
= getXForTime(event
.getTime());
1503 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1505 // limit the x-coordinates to prevent integer overflow in calculations
1506 // and also GC.drawLine doesn't draw properly with large coordinates
1507 final int limit
= Integer
.MAX_VALUE
/ 1024;
1508 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1509 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1511 int y0
= src
.y
+ src
.height
/ 2;
1512 int y1
= dst
.y
+ dst
.height
/ 2;
1513 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1517 * Draw the state (color fill)
1522 * Time event for which we're drawing the state
1527 * @return true if the state was drawn
1529 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1530 Rectangle rect
, GC gc
) {
1535 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1539 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1542 Color stateColor
= null;
1543 if (colorIdx
< fEventColorMap
.length
) {
1544 stateColor
= fEventColorMap
[colorIdx
];
1546 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1549 gc
.setForeground(stateColor
);
1550 gc
.setBackground(stateColor
);
1552 /* Draw the arrow */
1553 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1554 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1557 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1562 * @author Francis Giraldeau
1565 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1567 * The algorithm was taken from this site, not the code itself
1569 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1572 double cos
= 0.9510;
1573 double sin
= 0.3090;
1574 long lenx
= x1
- x0
;
1575 long leny
= y1
- y0
;
1576 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1578 double dx
= factor
* lenx
/ len
;
1579 double dy
= factor
* leny
/ len
;
1580 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1581 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1582 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1583 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1584 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1585 gc
.fillPolygon(arrow
);
1589 * Draw the name of an item.
1594 * Where to draw the name
1598 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1599 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1600 if (! hasTimeEvents
) {
1601 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1602 gc
.fillRectangle(bounds
);
1603 if (item
.fSelected
&& fIsInFocus
) {
1604 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1605 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1608 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1609 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1610 gc
.fillRectangle(bounds
);
1613 // No name to be drawn
1614 if (fTimeProvider
.getNameSpace() == 0) {
1618 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1619 if (item
.fHasChildren
) {
1620 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1621 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1622 Rectangle rect
= Utils
.clone(bounds
);
1623 rect
.x
+= leftMargin
;
1624 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1625 rect
.width
= EXPAND_SIZE
;
1626 rect
.height
= EXPAND_SIZE
;
1627 gc
.fillRectangle(rect
);
1628 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1629 int midy
= rect
.y
+ rect
.height
/ 2;
1630 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1631 if (!item
.fExpanded
) {
1632 int midx
= rect
.x
+ rect
.width
/ 2;
1633 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1636 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1638 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1641 int imgHeight
= img
.getImageData().height
;
1642 int imgWidth
= img
.getImageData().width
;
1644 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1645 gc
.drawImage(img
, x
, y
);
1646 leftMargin
+= imgWidth
+ MARGIN
;
1648 String name
= item
.fName
;
1649 Point size
= gc
.stringExtent(name
);
1650 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1651 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1653 if (hasTimeEvents
) {
1654 // cut long string with "..."
1655 int width
= bounds
.width
- leftMargin
;
1657 while (size
.x
> width
&& name
.length() > 1) {
1659 name
= name
.substring(0, name
.length() - 1);
1660 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1663 name
+= "..."; //$NON-NLS-1$
1666 Rectangle rect
= Utils
.clone(bounds
);
1667 rect
.x
+= leftMargin
;
1668 rect
.width
-= leftMargin
;
1670 if (rect
.width
> 0) {
1671 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1672 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1673 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1674 leftMargin
+= textWidth
+ MARGIN
;
1677 if (hasTimeEvents
) {
1679 int x
= bounds
.x
+ leftMargin
;
1680 int width
= bounds
.width
- x
;
1681 int midy
= bounds
.y
+ bounds
.height
/ 2;
1682 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1683 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1689 * Draw the state (color fill)
1694 * Time event for which we're drawing the state
1700 * Is this time event currently selected (so it appears
1702 * @param timeSelected
1703 * Is the timestamp currently selected
1704 * @return true if the state was drawn
1706 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1707 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1709 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1710 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1713 boolean visible
= rect
.width
== 0 ?
false : true;
1714 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1715 gc
.setForeground(black
);
1718 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1719 // Only draw the top and bottom borders
1720 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1721 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1722 if (rect
.width
== 1) {
1723 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1725 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1728 Color stateColor
= null;
1729 if (colorIdx
< fEventColorMap
.length
) {
1730 stateColor
= fEventColorMap
[colorIdx
];
1735 boolean reallySelected
= timeSelected
&& selected
;
1736 // fill all rect area
1737 gc
.setBackground(stateColor
);
1738 gc
.fillRectangle(rect
);
1740 if (reallySelected
) {
1741 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1742 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1745 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1747 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1752 * Fill the space between two contiguous time events
1759 * Is this time event selected or not
1761 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1762 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1763 gc
.fillRectangle(rect
);
1764 if (fDragState
== DRAG_ZOOM
) {
1765 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1766 if (fDragX0
< fDragX
) {
1767 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1768 } else if (fDragX0
> fDragX
) {
1769 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1773 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1774 int midy
= rect
.y
+ rect
.height
/ 2;
1775 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1779 public void keyTraversed(TraverseEvent e
) {
1780 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1786 public void keyPressed(KeyEvent e
) {
1788 if (fItemData
.fExpandedItems
.length
== 0) {
1791 if (SWT
.HOME
== e
.keyCode
) {
1793 } else if (SWT
.END
== e
.keyCode
) {
1794 idx
= fItemData
.fExpandedItems
.length
- 1;
1795 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1796 idx
= getSelectedIndex();
1799 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1802 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1803 idx
= getSelectedIndex();
1806 } else if (idx
> 0) {
1809 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1811 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1813 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1814 int page
= countPerPage();
1815 idx
= getSelectedIndex();
1820 if (idx
>= fItemData
.fExpandedItems
.length
) {
1821 idx
= fItemData
.fExpandedItems
.length
- 1;
1823 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1824 int page
= countPerPage();
1825 idx
= getSelectedIndex();
1833 } else if (SWT
.CR
== e
.keyCode
) {
1834 idx
= getSelectedIndex();
1836 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1839 fireDefaultSelection();
1845 selectItem(idx
, false);
1846 fireSelectionChanged();
1848 int x
= toControl(e
.display
.getCursorLocation()).x
;
1849 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1853 public void keyReleased(KeyEvent e
) {
1854 int x
= toControl(e
.display
.getCursorLocation()).x
;
1855 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1859 public void focusGained(FocusEvent e
) {
1862 updateStatusLine(NO_STATUS
);
1866 public void focusLost(FocusEvent e
) {
1868 if (DRAG_NONE
!= fDragState
) {
1870 fDragState
= DRAG_NONE
;
1873 updateStatusLine(NO_STATUS
);
1877 * @return If the current view is focused
1879 public boolean isInFocus() {
1884 * Provide the possibility to control the wait cursor externally e.g. data
1885 * requests in progress
1887 * @param waitInd Should we wait indefinitely?
1889 public void waitCursor(boolean waitInd
) {
1890 // Update cursor as indicated
1892 setCursor(fWaitCursor
);
1898 private void updateCursor(int x
, int stateMask
) {
1899 // if Wait cursor not active, check for the need to change the cursor
1900 if (getCursor() == fWaitCursor
) {
1903 Cursor cursor
= null;
1904 if (fDragState
== DRAG_SPLIT_LINE
) {
1905 } else if (fDragState
== DRAG_SELECTION
) {
1906 cursor
= fResizeCursor
;
1907 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1908 cursor
= fDragCursor
;
1909 } else if (fDragState
== DRAG_ZOOM
) {
1910 cursor
= fZoomCursor
;
1911 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1912 cursor
= fDragCursor
;
1913 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1914 cursor
= fResizeCursor
;
1915 } else if (!isOverSplitLine(x
)) {
1916 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1917 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1918 int xBegin
= getXForTime(selectionBegin
);
1919 int xEnd
= getXForTime(selectionEnd
);
1920 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1921 cursor
= fResizeCursor
;
1924 if (getCursor() != cursor
) {
1929 private void updateStatusLine(int x
) {
1930 // use the time provider of the time graph scale for the status line
1931 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
1932 if (fStatusLineManager
== null || null == tdp
||
1933 tdp
.getTime0() == tdp
.getTime1()) {
1936 TimeFormat tf
= tdp
.getTimeFormat();
1937 Resolution res
= Resolution
.NANOSEC
;
1938 StringBuilder message
= new StringBuilder();
1939 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1940 long time
= getTimeAtX(x
);
1942 if (tdp
instanceof ITimeDataProviderConverter
) {
1943 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
1945 long selectionBegin
= tdp
.getSelectionBegin();
1946 long selectionEnd
= tdp
.getSelectionEnd();
1947 message
.append(NLS
.bind("T: {0}{1} T1: {2}{3}", //$NON-NLS-1$
1949 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
1950 Utils
.formatTime(time
, tf
, res
),
1951 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
1952 Utils
.formatTime(Math
.min(selectionBegin
, selectionEnd
), tf
, res
)
1954 if (selectionBegin
!= selectionEnd
) {
1955 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
1957 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
1958 Utils
.formatTime(Math
.max(selectionBegin
, selectionEnd
), tf
, res
),
1959 Utils
.formatDelta(Math
.abs(selectionBegin
- selectionEnd
), tf
, res
)
1963 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1964 long time0
= fDragTime0
;
1965 long time
= getTimeAtX(fDragX
);
1966 if (tdp
instanceof ITimeDataProviderConverter
) {
1967 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
1968 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
1970 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
1972 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(time
, time0
)) + ' ' : "", //$NON-NLS-1$
1973 Utils
.formatTime(Math
.min(time
, time0
), tf
, res
),
1974 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(time
, time0
)) + ' ' : "", //$NON-NLS-1$
1975 Utils
.formatTime(Math
.max(time
, time0
), tf
, res
),
1976 Utils
.formatDelta(Math
.abs(time
- time0
), tf
, res
)
1979 fStatusLineManager
.setMessage(message
.toString());
1983 public void mouseMove(MouseEvent e
) {
1984 if (null == fTimeProvider
) {
1987 Point size
= getSize();
1988 if (DRAG_TRACE_ITEM
== fDragState
) {
1989 int nameWidth
= fTimeProvider
.getNameSpace();
1990 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1992 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1993 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1994 long time1
= fTime1bak
- timeDelta
;
1995 long maxTime
= fTimeProvider
.getMaxTime();
1996 if (time1
> maxTime
) {
1999 long time0
= time1
- (fTime1bak
- fTime0bak
);
2000 if (time0
< fTimeProvider
.getMinTime()) {
2001 time0
= fTimeProvider
.getMinTime();
2002 time1
= time0
+ (fTime1bak
- fTime0bak
);
2004 fTimeProvider
.setStartFinishTime(time0
, time1
);
2006 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2008 fTimeProvider
.setNameSpace(e
.x
);
2009 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2010 } else if (DRAG_SELECTION
== fDragState
) {
2011 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2013 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2014 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2015 } else if (DRAG_ZOOM
== fDragState
) {
2016 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2018 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2019 } else if (DRAG_NONE
== fDragState
) {
2020 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2021 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2024 fMouseOverSplitLine
= mouseOverSplitLine
;
2026 updateCursor(e
.x
, e
.stateMask
);
2027 updateStatusLine(e
.x
);
2031 public void mouseDoubleClick(MouseEvent e
) {
2032 if (null == fTimeProvider
) {
2035 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2036 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2037 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2038 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2039 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2042 fMouseOverSplitLine
= mouseOverSplitLine
;
2043 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2046 int idx
= getItemIndexAtY(e
.y
);
2048 selectItem(idx
, false);
2049 fireDefaultSelection();
2055 public void mouseDown(MouseEvent e
) {
2056 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2057 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2058 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2062 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2063 int nameSpace
= fTimeProvider
.getNameSpace();
2064 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2065 fDragState
= DRAG_SPLIT_LINE
;
2066 fDragButton
= e
.button
;
2069 fTime0bak
= fTimeProvider
.getTime0();
2070 fTime1bak
= fTimeProvider
.getTime1();
2072 updateCursor(e
.x
, e
.stateMask
);
2076 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2077 int nameSpace
= fTimeProvider
.getNameSpace();
2078 idx
= getItemIndexAtY(e
.y
);
2080 Item item
= fItemData
.fExpandedItems
[idx
];
2081 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2085 selectItem(idx
, false);
2086 fireSelectionChanged();
2088 selectItem(idx
, false); // clear selection
2089 fireSelectionChanged();
2091 long hitTime
= getTimeAtX(e
.x
);
2095 fDragState
= DRAG_SELECTION
;
2096 fDragButton
= e
.button
;
2099 fDragTime0
= getTimeAtX(fDragX0
);
2100 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2101 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2102 int xBegin
= getXForTime(selectionBegin
);
2103 int xEnd
= getXForTime(selectionEnd
);
2104 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2105 long time
= getTimeAtX(e
.x
);
2106 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2108 fDragTime0
= selectionEnd
;
2111 fDragTime0
= selectionBegin
;
2114 long time
= getTimeAtX(e
.x
);
2115 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2117 fDragTime0
= selectionEnd
;
2118 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2120 fDragTime0
= selectionBegin
;
2123 fTime0bak
= fTimeProvider
.getTime0();
2124 fTime1bak
= fTimeProvider
.getTime1();
2126 updateCursor(e
.x
, e
.stateMask
);
2127 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2129 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2130 long hitTime
= getTimeAtX(e
.x
);
2133 fDragState
= DRAG_TRACE_ITEM
;
2134 fDragButton
= e
.button
;
2137 fTime0bak
= fTimeProvider
.getTime0();
2138 fTime1bak
= fTimeProvider
.getTime1();
2139 updateCursor(e
.x
, e
.stateMask
);
2141 } else if (3 == e
.button
) {
2143 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2145 fDragTime0
= getTimeAtX(fDragX0
);
2146 fDragState
= DRAG_ZOOM
;
2147 fDragButton
= e
.button
;
2149 updateCursor(e
.x
, e
.stateMask
);
2150 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2155 public void mouseUp(MouseEvent e
) {
2156 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2157 menuDetected(fPendingMenuDetectEvent
);
2159 if (DRAG_NONE
!= fDragState
) {
2161 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2162 if (fDragX
!= fDragX0
) {
2163 fTimeProvider
.notifyStartFinishTime();
2165 fDragState
= DRAG_NONE
;
2166 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2167 fDragState
= DRAG_NONE
;
2169 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2170 if (fDragX
== fDragX0
) { // click without selecting anything
2171 long time
= getTimeAtX(e
.x
);
2172 fTimeProvider
.setSelectedTimeNotify(time
, false);
2174 long time0
= fDragTime0
;
2175 long time1
= getTimeAtX(fDragX
);
2176 if (time0
<= time1
) {
2177 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2179 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2182 fDragState
= DRAG_NONE
;
2184 fTimeGraphScale
.setDragRange(-1, -1);
2185 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2186 int nameWidth
= fTimeProvider
.getNameSpace();
2187 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2188 long time0
= getTimeAtX(fDragX0
);
2189 long time1
= getTimeAtX(fDragX
);
2190 if (time0
< time1
) {
2191 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2193 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2198 fDragState
= DRAG_NONE
;
2199 fTimeGraphScale
.setDragRange(-1, -1);
2202 updateCursor(e
.x
, e
.stateMask
);
2203 updateStatusLine(e
.x
);
2207 public void mouseEnter(MouseEvent e
) {
2211 public void mouseExit(MouseEvent e
) {
2212 if (fMouseOverSplitLine
) {
2213 fMouseOverSplitLine
= false;
2216 updateStatusLine(NO_STATUS
);
2220 public void mouseHover(MouseEvent e
) {
2224 public void mouseScrolled(MouseEvent e
) {
2225 if (fDragState
!= DRAG_NONE
) {
2228 boolean zoomScroll
= false;
2229 boolean horizontalScroll
= false;
2230 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2231 Point parentSize
= getParent().getSize();
2232 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2233 // over the parent control
2234 if (e
.x
> getSize().x
) {
2235 // over the vertical scroll bar
2237 } else if (e
.y
< 0) {
2238 // over the time scale
2240 } else if (e
.y
>= getSize().y
) {
2241 // over the horizontal scroll bar
2242 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2245 horizontalScroll
= true;
2248 if (e
.x
< fTimeProvider
.getNameSpace()) {
2249 // over the name space
2252 // over the state area
2253 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2254 // over the state area, CTRL pressed
2257 // over the state area, CTRL not pressed
2263 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2266 } else if (e
.count
< 0) {
2269 } else if (horizontalScroll
) {
2270 horizontalScroll(e
.count
> 0);
2272 setTopIndex(getTopIndex() - e
.count
);
2277 public void handleEvent(Event event
) {
2278 if (event
.type
== SWT
.MouseWheel
) {
2279 // prevent horizontal scrolling when the mouse wheel is used to
2280 // scroll vertically or zoom
2286 public int getBorderWidth() {
2287 return fBorderWidth
;
2291 * Set the border width
2293 * @param borderWidth
2296 public void setBorderWidth(int borderWidth
) {
2297 this.fBorderWidth
= borderWidth
;
2301 * @return The current height of the header row
2303 public int getHeaderHeight() {
2304 return fHeaderHeight
;
2308 * Set the height of the header row
2310 * @param headerHeight
2313 public void setHeaderHeight(int headerHeight
) {
2314 this.fHeaderHeight
= headerHeight
;
2318 * @return The default height of regular item rows
2320 public int getItemHeight() {
2321 return fGlobalItemHeight
;
2325 * Set the default height of regular item rows.
2330 public void setItemHeight(int rowHeight
) {
2331 this.fGlobalItemHeight
= rowHeight
;
2335 * Set the height of a specific item. Overrides the default item height.
2338 * A time graph entry
2341 * @return true if the height is successfully stored, false otherwise
2343 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2344 Item item
= fItemData
.findItem(entry
);
2346 item
.fItemHeight
= rowHeight
;
2353 * Set the minimum item width
2355 * @param width The minimum width
2357 public void setMinimumItemWidth(int width
) {
2358 this.fMinimumItemWidth
= width
;
2362 * @return The minimum item width
2364 public int getMinimumItemWidth() {
2365 return fMinimumItemWidth
;
2369 * @return The entries that are currently filtered out
2371 public List
<ITimeGraphEntry
> getFilteredOut() {
2372 return fItemData
.getFilteredOut();
2376 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2377 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2378 fSelectionChangedListeners
.add(listener
);
2383 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2384 if (listener
!= null) {
2385 fSelectionChangedListeners
.remove(listener
);
2390 public void setSelection(ISelection selection
) {
2391 if (selection
instanceof TimeGraphSelection
) {
2392 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2393 Object ob
= sel
.getFirstElement();
2394 if (ob
instanceof ITimeGraphEntry
) {
2395 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2396 selectItem(trace
, false);
2403 * @param filter The filter object to be attached to the view
2405 public void addFilter(ViewerFilter filter
) {
2406 if (!fFilters
.contains(filter
)) {
2407 fFilters
.add(filter
);
2412 * @param filter The filter object to be attached to the view
2414 public void removeFilter(ViewerFilter filter
) {
2415 fFilters
.remove(filter
);
2419 public void colorSettingsChanged(StateItem
[] stateItems
) {
2420 /* Destroy previous colors from the resource manager */
2421 if (fEventColorMap
!= null) {
2422 for (Color color
: fEventColorMap
) {
2423 fResourceManager
.destroyColor(color
.getRGB());
2426 if (stateItems
!= null) {
2427 fEventColorMap
= new Color
[stateItems
.length
];
2428 for (int i
= 0; i
< stateItems
.length
; i
++) {
2429 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2432 fEventColorMap
= new Color
[] { };
2437 private class ItemData
{
2438 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2439 private Item
[] fExpandedItems
= new Item
[0];
2440 private Item
[] fItems
= new Item
[0];
2441 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2442 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2443 private boolean fEntryFilter
[] = new boolean[0];
2444 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2449 public Item
findItem(ITimeGraphEntry entry
) {
2450 return fItemMap
.get(entry
);
2453 public int findItemIndex(ITimeGraphEntry entry
) {
2454 Item item
= fItemMap
.get(entry
);
2458 return item
.fExpandedIndex
;
2461 public void refreshData() {
2463 fFilteredOut
.clear();
2464 ITimeGraphEntry selection
= getSelectedTrace();
2465 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2466 ITimeGraphEntry entry
= fRootEntries
[i
];
2467 refreshData(fItemMap
, null, 0, entry
);
2469 fItems
= fItemMap
.values().toArray(new Item
[0]);
2470 updateExpandedItems();
2471 if (selection
!= null) {
2472 for (Item item
: fExpandedItems
) {
2473 if (item
.fEntry
== selection
) {
2474 item
.fSelected
= true;
2481 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2482 Item item
= new Item(entry
, entry
.getName(), level
);
2483 if (parent
!= null) {
2484 parent
.fChildren
.add(item
);
2486 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2487 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2489 item
.fItemHeight
= fGlobalItemHeight
;
2491 itemMap
.put(entry
, item
);
2492 if (entry
.hasChildren()) {
2493 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2494 item
.fHasChildren
= true;
2495 for (ITimeGraphEntry child
: entry
.getChildren()) {
2496 refreshData(itemMap
, item
, level
+ 1, child
);
2501 public void updateExpandedItems() {
2502 for (Item item
: fItems
) {
2503 item
.fExpandedIndex
= -1;
2505 List
<Item
> expandedItemList
= new ArrayList
<>();
2506 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2507 ITimeGraphEntry entry
= fRootEntries
[i
];
2508 Item item
= findItem(entry
);
2509 refreshExpanded(expandedItemList
, item
);
2511 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2512 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2515 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2516 // Check for filters
2517 boolean display
= true;
2518 for (ViewerFilter filter
: fFilters
) {
2519 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2525 item
.fExpandedIndex
= expandedItemList
.size();
2526 expandedItemList
.add(item
);
2527 if (item
.fHasChildren
&& item
.fExpanded
) {
2528 for (Item child
: item
.fChildren
) {
2529 refreshExpanded(expandedItemList
, child
);
2535 public void refreshData(ITimeGraphEntry
[] entries
) {
2536 if (entries
== null) {
2537 fEntryFilter
= null;
2538 fRootEntries
= null;
2540 if (entries
.length
== 0) {
2541 fEntryFilter
= null;
2542 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2543 fEntryFilter
= new boolean[entries
.length
];
2544 java
.util
.Arrays
.fill(fEntryFilter
, true);
2546 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2552 public void refreshArrows(List
<ILinkEvent
> events
) {
2553 /* If links are null, reset the list */
2554 if (events
!= null) {
2557 fLinks
= new ArrayList
<>();
2561 public ITimeGraphEntry
[] getEntries() {
2562 return fRootEntries
;
2565 public boolean[] getEntryFilter() {
2566 return fEntryFilter
;
2569 public List
<ITimeGraphEntry
> getFilteredOut() {
2570 return fFilteredOut
;
2574 private class Item
{
2575 private boolean fExpanded
;
2576 private int fExpandedIndex
;
2577 private boolean fSelected
;
2578 private boolean fHasChildren
;
2579 private int fItemHeight
;
2580 private final int fLevel
;
2581 private final List
<Item
> fChildren
;
2582 private final String fName
;
2583 private final ITimeGraphEntry fEntry
;
2585 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2586 this.fEntry
= entry
;
2588 this.fLevel
= level
;
2589 this.fChildren
= new ArrayList
<>();
2593 public String
toString() {
2599 public void menuDetected(MenuDetectEvent e
) {
2600 if (null == fTimeProvider
) {
2603 if (e
.detail
== SWT
.MENU_MOUSE
) {
2604 if (fPendingMenuDetectEvent
== null) {
2605 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2606 * Store the event and trigger it later just before handling mouseUp.
2607 * This allows for the method to detect if mouse is used to drag zoom.
2609 fPendingMenuDetectEvent
= e
;
2612 fPendingMenuDetectEvent
= null;
2613 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2617 if (fDragState
!= DRAG_NONE
) {
2621 Point p
= toControl(e
.x
, e
.y
);
2622 int idx
= getItemIndexAtY(p
.y
);
2623 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2624 Item item
= fItemData
.fExpandedItems
[idx
];
2625 ITimeGraphEntry entry
= item
.fEntry
;
2626 if (entry
.hasTimeEvents()) {
2627 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2628 if (event
!= null) {
2630 fireMenuEventOnTimeEvent(e
);
2635 fireMenuEventOnTimeGraphEntry(e
);
2640 * Perform the alignment operation.
2643 * the alignment offset
2645 * @see ITmfTimeAligned
2649 public void performAlign(int offset
) {
2650 fTimeProvider
.setNameSpace(offset
);
2654 * Return the time alignment information
2656 * @return the time alignment information
2658 * @see ITmfTimeAligned
2662 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
2663 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());