1 /*****************************************************************************
2 * Copyright (c) 2007, 2015 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Collections
;
26 import java
.util
.Iterator
;
27 import java
.util
.LinkedHashMap
;
28 import java
.util
.List
;
31 import org
.eclipse
.jface
.action
.IStatusLineManager
;
32 import org
.eclipse
.jface
.resource
.JFaceResources
;
33 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
34 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
35 import org
.eclipse
.jface
.viewers
.ISelection
;
36 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
37 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
38 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
39 import org
.eclipse
.osgi
.util
.NLS
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.events
.FocusEvent
;
42 import org
.eclipse
.swt
.events
.FocusListener
;
43 import org
.eclipse
.swt
.events
.KeyEvent
;
44 import org
.eclipse
.swt
.events
.KeyListener
;
45 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
46 import org
.eclipse
.swt
.events
.MenuDetectListener
;
47 import org
.eclipse
.swt
.events
.MouseEvent
;
48 import org
.eclipse
.swt
.events
.MouseListener
;
49 import org
.eclipse
.swt
.events
.MouseMoveListener
;
50 import org
.eclipse
.swt
.events
.MouseTrackListener
;
51 import org
.eclipse
.swt
.events
.MouseWheelListener
;
52 import org
.eclipse
.swt
.events
.PaintEvent
;
53 import org
.eclipse
.swt
.events
.SelectionListener
;
54 import org
.eclipse
.swt
.events
.TraverseEvent
;
55 import org
.eclipse
.swt
.events
.TraverseListener
;
56 import org
.eclipse
.swt
.events
.TypedEvent
;
57 import org
.eclipse
.swt
.graphics
.Color
;
58 import org
.eclipse
.swt
.graphics
.Cursor
;
59 import org
.eclipse
.swt
.graphics
.GC
;
60 import org
.eclipse
.swt
.graphics
.Image
;
61 import org
.eclipse
.swt
.graphics
.Point
;
62 import org
.eclipse
.swt
.graphics
.Rectangle
;
63 import org
.eclipse
.swt
.widgets
.Composite
;
64 import org
.eclipse
.swt
.widgets
.Display
;
65 import org
.eclipse
.swt
.widgets
.Event
;
66 import org
.eclipse
.swt
.widgets
.Listener
;
67 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
68 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
69 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
86 * Time graph control implementation
88 * @author Alvaro Sanchez-Leon
89 * @author Patrick Tasse
91 public class TimeGraphControl
extends TimeGraphBaseControl
92 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
93 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
94 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
96 /** Constant indicating that all levels of the time graph should be expanded */
97 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
99 private static final int DRAG_NONE
= 0;
100 private static final int DRAG_TRACE_ITEM
= 1;
101 private static final int DRAG_SPLIT_LINE
= 2;
102 private static final int DRAG_ZOOM
= 3;
103 private static final int DRAG_SELECTION
= 4;
105 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
107 private static final double ZOOM_FACTOR
= 1.5;
108 private static final double ZOOM_IN_FACTOR
= 0.8;
109 private static final double ZOOM_OUT_FACTOR
= 1.25;
111 private static final int SNAP_WIDTH
= 2;
112 private static final int ARROW_HOVER_MAX_DIST
= 5;
114 private static final int NO_STATUS
= -1;
115 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
117 /** Resource manager */
118 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
120 /** Color map for event types */
121 private Color
[] fEventColorMap
= null;
123 private ITimeDataProvider fTimeProvider
;
124 private IStatusLineManager fStatusLineManager
= null;
125 private TimeGraphScale fTimeGraphScale
= null;
127 private boolean fIsInFocus
= false;
128 private boolean fMouseOverSplitLine
= false;
129 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
130 private boolean fBlendSubPixelEvents
= false;
131 private int fMinimumItemWidth
= 0;
132 private int fTopIndex
= 0;
133 private int fDragState
= DRAG_NONE
;
134 private boolean fDragBeginMarker
= false;
135 private int fDragButton
;
136 private int fDragX0
= 0;
137 private int fDragX
= 0;
138 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
139 private int fIdealNameSpace
= 0;
140 private long fTime0bak
;
141 private long fTime1bak
;
142 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
143 private ItemData fItemData
= null;
144 private List
<SelectionListener
> fSelectionListeners
;
145 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
146 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
147 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
148 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
149 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
150 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
151 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
152 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
153 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
154 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
155 private MenuDetectEvent fPendingMenuDetectEvent
= null;
156 private boolean fGridLinesVisible
= true;
157 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
158 private boolean fHideArrows
= false;
159 private int fAutoExpandLevel
= ALL_LEVELS
;
161 private int fBorderWidth
= 0;
162 private int fHeaderHeight
= 0;
165 * Standard constructor
168 * The parent composite object
170 * The color scheme to use
172 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
174 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
176 fItemData
= new ItemData();
178 addFocusListener(this);
179 addMouseListener(this);
180 addMouseMoveListener(this);
181 addMouseTrackListener(this);
182 addMouseWheelListener(this);
183 addTraverseListener(this);
184 addKeyListener(this);
185 addMenuDetectListener(this);
186 addListener(SWT
.MouseWheel
, this);
190 public void dispose() {
192 fResourceManager
.dispose();
196 * Sets the timegraph provider used by this timegraph viewer.
198 * @param timeGraphProvider the timegraph provider
200 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
201 fTimeGraphProvider
= timeGraphProvider
;
203 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
204 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
205 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
208 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
209 colorSettingsChanged(stateItems
);
213 * Gets the timegraph provider used by this timegraph viewer.
215 * @return the timegraph provider, or <code>null</code> if not set.
217 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
218 return fTimeGraphProvider
;
222 * Gets the color map used by this timegraph viewer.
224 * @return a color map, or <code>null</code> if not set.
226 public Color
[] getEventColorMap() {
227 return fEventColorMap
;
231 * Assign the given time provider
233 * @param timeProvider
236 public void setTimeProvider(ITimeDataProvider timeProvider
) {
237 fTimeProvider
= timeProvider
;
242 * Assign the status line manager
244 * @param statusLineManager
245 * The status line manager, or null to disable status line messages
247 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
248 if (fStatusLineManager
!= null && statusLineManager
== null) {
249 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
251 fStatusLineManager
= statusLineManager
;
255 * Assign the time graph scale
257 * @param timeGraphScale
258 * The time graph scale
260 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
261 fTimeGraphScale
= timeGraphScale
;
265 * Add a selection listener
268 * The listener to add
270 public void addSelectionListener(SelectionListener listener
) {
271 if (listener
== null) {
272 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
274 if (null == fSelectionListeners
) {
275 fSelectionListeners
= new ArrayList
<>();
277 fSelectionListeners
.add(listener
);
281 * Remove a selection listener
284 * The listener to remove
286 public void removeSelectionListener(SelectionListener listener
) {
287 if (null != fSelectionListeners
) {
288 fSelectionListeners
.remove(listener
);
293 * Selection changed callback
295 public void fireSelectionChanged() {
296 if (null != fSelectionListeners
) {
297 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
298 while (it
.hasNext()) {
299 SelectionListener listener
= it
.next();
300 listener
.widgetSelected(null);
306 * Default selection callback
308 public void fireDefaultSelection() {
309 if (null != fSelectionListeners
) {
310 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
311 while (it
.hasNext()) {
312 SelectionListener listener
= it
.next();
313 listener
.widgetDefaultSelected(null);
319 * Add a drag selection listener
322 * The listener to add
324 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
325 if (listener
== null) {
326 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
328 if (null == fDragSelectionListeners
) {
329 fDragSelectionListeners
= new ArrayList
<>();
331 fDragSelectionListeners
.add(listener
);
335 * Remove a drag selection listener
338 * The listener to remove
340 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
341 if (null != fDragSelectionListeners
) {
342 fDragSelectionListeners
.remove(listener
);
347 * Drag Selection changed callback
350 * Time interval start
354 public void fireDragSelectionChanged(long start
, long end
) {
355 // check for backward intervals
356 long beginTime
, endTime
;
364 // call the listeners
365 if (null != fDragSelectionListeners
) {
366 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
367 while (it
.hasNext()) {
368 ITimeGraphTimeListener listener
= it
.next();
369 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
375 * Get the traces in the model
377 * @return The array of traces
379 public ITimeGraphEntry
[] getTraces() {
380 return fItemData
.getEntries();
384 * Refresh the data for the thing
386 public void refreshData() {
387 fItemData
.refreshData();
392 * Refresh data for the given traces
395 * The traces to refresh
397 public void refreshData(ITimeGraphEntry
[] traces
) {
398 fItemData
.refreshData(traces
);
403 * Refresh the links (arrows) of this widget
405 * @param events The link event list
407 public void refreshArrows(List
<ILinkEvent
> events
) {
408 fItemData
.refreshArrows(events
);
412 * Get the links (arrows) of this widget
414 * @return The unmodifiable link event list
418 public List
<ILinkEvent
> getArrows() {
419 return Collections
.unmodifiableList(fItemData
.fLinks
);
422 boolean ensureVisibleItem(int idx
, boolean redraw
) {
423 boolean changed
= false;
426 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
427 if (fItemData
.fExpandedItems
[index
].fSelected
) {
432 if (index
>= fItemData
.fExpandedItems
.length
) {
435 if (index
< fTopIndex
) {
442 int page
= countPerPage();
443 if (index
>= fTopIndex
+ page
) {
444 setTopIndex(index
- page
+ 1);
455 * Assign the given index as the top one
460 public void setTopIndex(int idx
) {
461 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
462 index
= Math
.max(0, index
);
468 * Sets the auto-expand level to be used for new entries discovered when
469 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
470 * . The value 0 means that there is no auto-expand; 1 means that top-level
471 * entries are expanded, but not their children; 2 means that top-level
472 * entries are expanded, and their children, but not grand-children; and so
475 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
479 * non-negative level, or <code>ALL_LEVELS</code> to expand all
482 public void setAutoExpandLevel(int level
) {
483 fAutoExpandLevel
= level
;
487 * Returns the auto-expand level.
489 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
490 * the tree are expanded automatically
491 * @see #setAutoExpandLevel
493 public int getAutoExpandLevel() {
494 return fAutoExpandLevel
;
498 * Get the expanded state of a given entry.
502 * @return true if the entry is expanded, false if collapsed
505 public boolean getExpandedState(ITimeGraphEntry entry
) {
506 Item item
= fItemData
.fItemMap
.get(entry
);
507 return (item
!= null ? item
.fExpanded
: false);
511 * Set the expanded state of a given entry
516 * True if expanded, false if collapsed
518 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
519 Item item
= fItemData
.findItem(entry
);
520 if (item
!= null && item
.fExpanded
!= expanded
) {
521 item
.fExpanded
= expanded
;
522 fItemData
.updateExpandedItems();
528 * Collapses all nodes of the viewer's tree, starting with the root.
530 public void collapseAll() {
531 for (Item item
: fItemData
.fItems
) {
532 item
.fExpanded
= false;
534 fItemData
.updateExpandedItems();
539 * Expands all nodes of the viewer's tree, starting with the root.
541 public void expandAll() {
542 for (Item item
: fItemData
.fItems
) {
543 item
.fExpanded
= true;
545 fItemData
.updateExpandedItems();
550 * Add a tree listener
553 * The listener to add
555 public void addTreeListener(ITimeGraphTreeListener listener
) {
556 if (!fTreeListeners
.contains(listener
)) {
557 fTreeListeners
.add(listener
);
562 * Remove a tree listener
565 * The listener to remove
567 public void removeTreeListener(ITimeGraphTreeListener listener
) {
568 if (fTreeListeners
.contains(listener
)) {
569 fTreeListeners
.remove(listener
);
574 * Tree event callback
579 * The expanded state (true for expanded, false for collapsed)
581 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
582 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
583 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
585 listener
.treeExpanded(event
);
587 listener
.treeCollapsed(event
);
593 * Add a menu listener on {@link ITimeGraphEntry}s
596 * The listener to add
598 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
599 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
600 fTimeGraphEntryMenuListeners
.add(listener
);
605 * Remove a menu listener on {@link ITimeGraphEntry}s
608 * The listener to remove
610 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
611 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
612 fTimeGraphEntryMenuListeners
.remove(listener
);
617 * Menu event callback on {@link ITimeGraphEntry}s
620 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
622 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
623 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
624 listener
.menuDetected(event
);
629 * Add a menu listener on {@link ITimeEvent}s
632 * The listener to add
634 public void addTimeEventMenuListener(MenuDetectListener listener
) {
635 if (!fTimeEventMenuListeners
.contains(listener
)) {
636 fTimeEventMenuListeners
.add(listener
);
641 * Remove a menu listener on {@link ITimeEvent}s
644 * The listener to remove
646 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
647 if (fTimeEventMenuListeners
.contains(listener
)) {
648 fTimeEventMenuListeners
.remove(listener
);
653 * Menu event callback on {@link ITimeEvent}s
656 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
658 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
659 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
660 listener
.menuDetected(event
);
665 public ISelection
getSelection() {
666 TimeGraphSelection sel
= new TimeGraphSelection();
667 ITimeGraphEntry trace
= getSelectedTrace();
668 if (null != trace
&& null != fTimeProvider
) {
669 long selectedTime
= fTimeProvider
.getSelectionBegin();
670 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
681 * Get the selection object
683 * @return The selection
685 public ISelection
getSelectionTrace() {
686 TimeGraphSelection sel
= new TimeGraphSelection();
687 ITimeGraphEntry trace
= getSelectedTrace();
695 * Enable/disable one of the traces in the model
698 * 1 to enable it, -1 to disable. The method returns immediately
699 * if another value is used.
701 public void selectTrace(int n
) {
702 if ((n
!= 1) && (n
!= -1)) {
706 boolean changed
= false;
707 int lastSelection
= -1;
708 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
709 Item item
= fItemData
.fExpandedItems
[i
];
710 if (item
.fSelected
) {
712 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
713 item
.fSelected
= false;
714 item
= fItemData
.fExpandedItems
[i
+ 1];
715 item
.fSelected
= true;
717 } else if ((-1 == n
) && (i
> 0)) {
718 item
.fSelected
= false;
719 item
= fItemData
.fExpandedItems
[i
- 1];
720 item
.fSelected
= true;
727 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
728 Item item
= fItemData
.fExpandedItems
[0];
729 item
.fSelected
= true;
734 ensureVisibleItem(-1, false);
736 fireSelectionChanged();
744 * 1 for next event, -1 for previous event
746 * true to extend selection range, false for single selection
749 public void selectEvent(int n
, boolean extend
) {
750 if (null == fTimeProvider
) {
753 ITimeGraphEntry trace
= getSelectedTrace();
757 long selectedTime
= fTimeProvider
.getSelectionEnd();
758 long endTime
= fTimeProvider
.getMaxTime();
759 ITimeEvent nextEvent
;
760 if (n
== -1 && selectedTime
> endTime
) {
761 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
763 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
765 if (null == nextEvent
&& n
== -1) {
766 nextEvent
= Utils
.getFirstEvent(trace
);
768 if (null != nextEvent
) {
769 long nextTime
= nextEvent
.getTime();
770 // If last event detected e.g. going back or not moving to a next
772 if (nextTime
<= selectedTime
&& n
== 1) {
773 // Select to the end of this last event
774 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
775 // but not beyond the end of the trace
776 if (nextTime
> endTime
) {
779 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
780 // for previous event go to its end time unless we were already there
781 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
784 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
);
786 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
788 fireSelectionChanged();
791 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
);
793 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
795 fireSelectionChanged();
797 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
801 * Select the next event
804 * true to extend selection range, false for single selection
807 public void selectNextEvent(boolean extend
) {
808 selectEvent(1, extend
);
809 // Notify if visible time window has been adjusted
810 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
814 * Select the previous event
817 * true to extend selection range, false for single selection
820 public void selectPrevEvent(boolean extend
) {
821 selectEvent(-1, extend
);
822 // Notify if visible time window has been adjusted
823 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
827 * Select the next trace
829 public void selectNextTrace() {
834 * Select the previous trace
836 public void selectPrevTrace() {
841 * Scroll left or right by one half window size
844 * true to scroll left, false to scroll right
846 public void horizontalScroll(boolean left
) {
847 long time0
= fTimeProvider
.getTime0();
848 long time1
= fTimeProvider
.getTime1();
849 long timeMin
= fTimeProvider
.getMinTime();
850 long timeMax
= fTimeProvider
.getMaxTime();
851 long range
= time1
- time0
;
855 long increment
= Math
.max(1, range
/ 2);
857 time0
= Math
.max(time0
- increment
, timeMin
);
858 time1
= time0
+ range
;
860 time1
= Math
.min(time1
+ increment
, timeMax
);
861 time0
= time1
- range
;
863 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
867 * Zoom based on mouse cursor location with mouse scrolling
869 * @param zoomIn true to zoom in, false to zoom out
871 public void zoom(boolean zoomIn
) {
872 int globalX
= getDisplay().getCursorLocation().x
;
873 Point p
= toControl(globalX
, 0);
874 int nameSpace
= fTimeProvider
.getNameSpace();
875 int timeSpace
= fTimeProvider
.getTimeSpace();
876 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
877 long time0
= fTimeProvider
.getTime0();
878 long time1
= fTimeProvider
.getTime1();
879 long interval
= time1
- time0
;
882 } // to allow getting out of single point interval
885 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
887 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
889 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
890 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
891 long newTime1
= newTime0
+ newInterval
;
892 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
896 * zoom in using single click
898 public void zoomIn() {
899 long prevTime0
= fTimeProvider
.getTime0();
900 long prevTime1
= fTimeProvider
.getTime1();
901 long prevRange
= prevTime1
- prevTime0
;
902 if (prevRange
== 0) {
905 ITimeDataProvider provider
= fTimeProvider
;
906 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
907 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
908 selTime
= (prevTime0
+ prevTime1
) / 2;
910 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
911 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
913 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
915 if (inaccuracy
> 0 && inaccuracy
< 100) {
916 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
920 long min
= fTimeProvider
.getMinTimeInterval();
921 if ((time1
- time0
) < min
) {
922 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
926 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
930 * zoom out using single click
932 public void zoomOut() {
933 long prevTime0
= fTimeProvider
.getTime0();
934 long prevTime1
= fTimeProvider
.getTime1();
935 ITimeDataProvider provider
= fTimeProvider
;
936 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
937 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
938 selTime
= (prevTime0
+ prevTime1
) / 2;
940 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
941 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
943 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
944 if (inaccuracy
> 0 && inaccuracy
< 100) {
945 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
949 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
953 * Set the grid lines visibility. The default is true.
956 * true to show the grid lines, false otherwise
959 public void setGridLinesVisible(boolean visible
) {
960 fGridLinesVisible
= visible
;
964 * Get the grid lines visibility.
966 * @return true if the grid lines are visible, false otherwise
969 public boolean getGridLinesVisible() {
970 return fGridLinesVisible
;
974 * Set the grid line color. The default is SWT.COLOR_GRAY.
977 * the grid line color
980 public void setGridLineColor(Color color
) {
981 fGridLineColor
= color
;
985 * Get the grid line color.
987 * @return the grid line color
990 public Color
getGridLineColor() {
991 return fGridLineColor
;
997 * @param hideArrows true to hide arrows
999 public void hideArrows(boolean hideArrows
) {
1000 fHideArrows
= hideArrows
;
1004 * Follow the arrow forward
1007 * true to extend selection range, false for single selection
1010 public void followArrowFwd(boolean extend
) {
1011 ITimeGraphEntry trace
= getSelectedTrace();
1012 if (trace
== null) {
1015 long selectedTime
= fTimeProvider
.getSelectionEnd();
1016 for (ILinkEvent link
: fItemData
.fLinks
) {
1017 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1018 selectItem(link
.getDestinationEntry(), false);
1019 if (link
.getDuration() != 0) {
1021 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration());
1023 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1025 // Notify if visible time window has been adjusted
1026 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1028 fireSelectionChanged();
1029 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1033 selectNextEvent(extend
);
1037 * Follow the arrow backward
1040 * true to extend selection range, false for single selection
1043 public void followArrowBwd(boolean extend
) {
1044 ITimeGraphEntry trace
= getSelectedTrace();
1045 if (trace
== null) {
1048 long selectedTime
= fTimeProvider
.getSelectionEnd();
1049 for (ILinkEvent link
: fItemData
.fLinks
) {
1050 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1051 selectItem(link
.getEntry(), false);
1052 if (link
.getDuration() != 0) {
1054 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime());
1056 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1058 // Notify if visible time window has been adjusted
1059 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1061 fireSelectionChanged();
1062 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1066 selectPrevEvent(extend
);
1070 * Return the currently selected trace
1072 * @return The entry matching the trace
1074 public ITimeGraphEntry
getSelectedTrace() {
1075 ITimeGraphEntry trace
= null;
1076 int idx
= getSelectedIndex();
1078 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1084 * Retrieve the index of the currently selected item
1088 public int getSelectedIndex() {
1090 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1091 Item item
= fItemData
.fExpandedItems
[i
];
1092 if (item
.fSelected
) {
1100 boolean toggle(int idx
) {
1101 boolean toggled
= false;
1102 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1103 Item item
= fItemData
.fExpandedItems
[idx
];
1104 if (item
.fHasChildren
) {
1105 item
.fExpanded
= !item
.fExpanded
;
1106 fItemData
.updateExpandedItems();
1109 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1116 * Gets the index of the item at the given location.
1120 * @return the index of the item at the given location, of -1 if none.
1122 protected int getItemIndexAtY(int y
) {
1127 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1128 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1136 boolean isOverSplitLine(int x
) {
1137 if (x
< 0 || null == fTimeProvider
) {
1140 int nameWidth
= fTimeProvider
.getNameSpace();
1141 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1145 * Gets the {@link ITimeGraphEntry} at the given location.
1148 * a point in the widget
1149 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1152 protected ITimeGraphEntry
getEntry(Point pt
) {
1153 int idx
= getItemIndexAtY(pt
.y
);
1154 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1158 * Return the arrow event closest to the given point that is no further than
1159 * a maximum distance.
1162 * a point in the widget
1163 * @return The closest arrow event, or null if there is none close enough.
1165 protected ILinkEvent
getArrow(Point pt
) {
1169 ILinkEvent linkEvent
= null;
1170 double minDistance
= Double
.MAX_VALUE
;
1171 for (ILinkEvent event
: fItemData
.fLinks
) {
1172 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1176 int x2
= x1
+ rect
.width
;
1177 int y2
= y1
+ rect
.height
;
1178 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1179 if (minDistance
> d
) {
1185 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1192 public int getXForTime(long time
) {
1193 if (null == fTimeProvider
) {
1196 long time0
= fTimeProvider
.getTime0();
1197 long time1
= fTimeProvider
.getTime1();
1198 int width
= getSize().x
;
1199 int nameSpace
= fTimeProvider
.getNameSpace();
1200 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1201 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1206 public long getTimeAtX(int coord
) {
1207 if (null == fTimeProvider
) {
1211 Point size
= getSize();
1212 long time0
= fTimeProvider
.getTime0();
1213 long time1
= fTimeProvider
.getTime1();
1214 int nameWidth
= fTimeProvider
.getNameSpace();
1215 final int x
= coord
- nameWidth
;
1216 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1217 if (x
>= 0 && size
.x
>= nameWidth
) {
1218 if (time1
- time0
> timeWidth
) {
1219 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1220 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1222 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1223 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1229 void selectItem(int idx
, boolean addSelection
) {
1230 boolean changed
= false;
1232 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1233 Item item
= fItemData
.fExpandedItems
[idx
];
1234 changed
= !item
.fSelected
;
1235 item
.fSelected
= true;
1238 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1239 Item item
= fItemData
.fExpandedItems
[i
];
1240 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1243 item
.fSelected
= i
== idx
;
1246 changed
|= ensureVisibleItem(idx
, true);
1253 * Callback for item selection
1256 * The entry matching the trace
1257 * @param addSelection
1258 * If the selection is added or removed
1260 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1261 int idx
= fItemData
.findItemIndex(trace
);
1262 selectItem(idx
, addSelection
);
1266 * Retrieve the number of entries shown per page.
1270 public int countPerPage() {
1271 int height
= getSize().y
;
1274 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1275 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1276 if (ySum
>= height
) {
1281 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1282 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1283 if (ySum
>= height
) {
1292 * Get the index of the top element
1296 public int getTopIndex() {
1301 * Get the number of expanded (visible) items
1303 * @return The count of expanded (visible) items
1305 public int getExpandedElementCount() {
1306 return fItemData
.fExpandedItems
.length
;
1310 * Get an array of all expanded (visible) elements
1312 * @return The expanded (visible) elements
1314 public ITimeGraphEntry
[] getExpandedElements() {
1315 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1316 for (Item item
: fItemData
.fExpandedItems
) {
1317 elements
.add(item
.fEntry
);
1319 return elements
.toArray(new ITimeGraphEntry
[0]);
1322 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1323 Rectangle rect
= getItemRect(bounds
, idx
);
1324 rect
.width
= nameWidth
;
1328 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1329 Rectangle rect
= getItemRect(bounds
, idx
);
1330 rect
.x
+= nameWidth
;
1331 rect
.width
-= nameWidth
;
1335 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1337 if (idx
>= fTopIndex
) {
1338 for (int i
= fTopIndex
; i
< idx
; i
++) {
1339 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1342 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1343 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1346 int y
= bounds
.y
+ ySum
;
1347 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1348 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1352 void paint(Rectangle bounds
, PaintEvent e
) {
1355 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1359 fIdealNameSpace
= 0;
1360 int nameSpace
= fTimeProvider
.getNameSpace();
1362 drawBackground(bounds
, nameSpace
, gc
);
1364 // draw the grid lines
1365 drawGridLines(bounds
, gc
);
1368 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1369 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1370 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1372 int alpha
= gc
.getAlpha();
1375 long time0
= fTimeProvider
.getTime0();
1376 long time1
= fTimeProvider
.getTime1();
1377 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1378 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1379 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1380 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1381 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1383 // draw selection lines
1384 if (fDragState
!= DRAG_SELECTION
) {
1385 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1386 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1387 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1390 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1391 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1396 // draw selection background
1397 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1398 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1399 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1400 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1402 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1403 } else if (x0
- x1
> 1) {
1404 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1408 // draw drag selection background
1409 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1410 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1411 if (fDragX0
< fDragX
) {
1412 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1413 } else if (fDragX0
> fDragX
) {
1414 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1419 if (DRAG_SPLIT_LINE
== fDragState
) {
1420 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1421 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1422 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1423 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1424 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1425 if (fDragX
!= fDragX0
) {
1426 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1428 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1429 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1430 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1431 if (fDragX
!= fDragX0
) {
1432 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1434 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1435 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1436 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1443 * Draw the background
1446 * The rectangle of the area
1448 * The width reserved for the names
1450 * Reference to the SWT GC object
1453 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1454 // draw empty name space background
1455 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1456 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1458 // draw empty states space background
1459 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1460 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1462 // draw the background of selected item and items with no time events
1463 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1464 Rectangle itemRect
= getItemRect(bounds
, i
);
1465 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1468 Item item
= fItemData
.fExpandedItems
[i
];
1469 if (! item
.fEntry
.hasTimeEvents()) {
1470 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1471 gc
.fillRectangle(itemRect
);
1472 } else if (item
.fSelected
) {
1473 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1474 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1475 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1476 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1482 * Draw the grid lines
1485 * The rectangle of the area
1487 * Reference to the SWT GC object
1490 public void drawGridLines(Rectangle bounds
, GC gc
) {
1491 if (!fGridLinesVisible
) {
1494 gc
.setForeground(fGridLineColor
);
1495 for (int x
: fTimeGraphScale
.getTickList()) {
1496 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1501 * Draw many items at once
1504 * The rectangle of the area
1505 * @param timeProvider
1508 * The array items to draw
1510 * The index of the first element to draw
1512 * The width reserved for the names
1514 * Reference to the SWT GC object
1516 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1517 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1518 for (int i
= topIndex
; i
< items
.length
; i
++) {
1519 Item item
= items
[i
];
1520 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1527 * @param item the item to draw
1528 * @param bounds the container rectangle
1529 * @param timeProvider Time provider
1530 * @param i the item index
1531 * @param nameSpace the name space
1532 * @param gc Graphics context
1534 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1535 Rectangle itemRect
= getItemRect(bounds
, i
);
1536 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1540 ITimeGraphEntry entry
= item
.fEntry
;
1541 long time0
= timeProvider
.getTime0();
1542 long time1
= timeProvider
.getTime1();
1543 long selectedTime
= fTimeProvider
.getSelectionEnd();
1545 if (! item
.fEntry
.hasTimeEvents()) {
1546 drawName(item
, itemRect
, gc
);
1548 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1549 drawName(item
, nameRect
, gc
);
1551 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1552 if (rect
.isEmpty()) {
1553 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1556 if (time1
<= time0
) {
1557 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1558 gc
.fillRectangle(rect
);
1559 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1563 boolean selected
= item
.fSelected
;
1564 // K pixels per second
1565 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1567 if (item
.fEntry
.hasTimeEvents()) {
1568 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1569 fillSpace(rect
, gc
, selected
);
1571 * State rectangle is smaller than item bounds. Use a margin height
1572 * of 3 pixels, keep at least 3 pixels for the state, but not more
1573 * than the item height. Favor the top margin for the remainder.
1575 int height
= Math
.min(rect
.height
, Math
.max(3, rect
.height
- 6));
1576 int margin
= (rect
.height
- height
+ 1) / 2;
1577 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ margin
, rect
.width
, height
);
1579 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1580 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1583 while (iterator
.hasNext()) {
1584 ITimeEvent event
= iterator
.next();
1585 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1586 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1587 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1588 // event is out of bounds
1591 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1592 stateRect
.x
= Math
.max(rect
.x
, x
);
1593 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1594 if (stateRect
.x
== lastX
) {
1595 stateRect
.width
-= 1;
1596 if (stateRect
.width
> 0) {
1597 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1598 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1602 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1603 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1604 lastX
= stateRect
.x
;
1607 gc
.setClipping((Rectangle
) null);
1609 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1616 * The rectangle of the area
1617 * @param timeProvider
1620 * The list of link events
1622 * The width reserved for the names
1624 * Reference to the SWT GC object
1626 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1627 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1631 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1632 /* the list can grow concurrently but cannot shrink */
1633 for (int i
= 0; i
< links
.size(); i
++) {
1634 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1636 gc
.setClipping((Rectangle
) null);
1640 * Draws the link type events of this item
1645 * the container rectangle
1646 * @param timeProvider
1653 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1654 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1657 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1658 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1659 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1661 if ((srcIndex
== -1) || (destIndex
== -1)) {
1665 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1666 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1668 int x0
= getXForTime(event
.getTime());
1669 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1671 // limit the x-coordinates to prevent integer overflow in calculations
1672 // and also GC.drawLine doesn't draw properly with large coordinates
1673 final int limit
= Integer
.MAX_VALUE
/ 1024;
1674 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1675 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1677 int y0
= src
.y
+ src
.height
/ 2;
1678 int y1
= dst
.y
+ dst
.height
/ 2;
1679 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1683 * Draw the state (color fill)
1688 * Time event for which we're drawing the state
1693 * @return true if the state was drawn
1695 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1696 Rectangle rect
, GC gc
) {
1701 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1705 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1708 Color stateColor
= null;
1709 if (colorIdx
< fEventColorMap
.length
) {
1710 stateColor
= fEventColorMap
[colorIdx
];
1712 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1715 gc
.setForeground(stateColor
);
1716 gc
.setBackground(stateColor
);
1718 /* Draw the arrow */
1719 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1720 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1723 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1728 * @author Francis Giraldeau
1731 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1733 * The algorithm was taken from this site, not the code itself
1735 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1738 double cos
= 0.9510;
1739 double sin
= 0.3090;
1740 long lenx
= x1
- x0
;
1741 long leny
= y1
- y0
;
1742 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1744 double dx
= factor
* lenx
/ len
;
1745 double dy
= factor
* leny
/ len
;
1746 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1747 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1748 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1749 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1750 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1751 gc
.fillPolygon(arrow
);
1755 * Draw the name of an item.
1760 * Where to draw the name
1764 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1765 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1767 // No name to be drawn
1768 if (fTimeProvider
.getNameSpace() == 0) {
1772 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1773 if (item
.fHasChildren
) {
1774 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1775 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1776 Rectangle rect
= Utils
.clone(bounds
);
1777 rect
.x
+= leftMargin
;
1778 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1779 rect
.width
= EXPAND_SIZE
;
1780 rect
.height
= EXPAND_SIZE
;
1781 gc
.fillRectangle(rect
);
1782 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1783 int midy
= rect
.y
+ rect
.height
/ 2;
1784 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1785 if (!item
.fExpanded
) {
1786 int midx
= rect
.x
+ rect
.width
/ 2;
1787 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1790 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1792 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1795 int imgHeight
= img
.getImageData().height
;
1796 int imgWidth
= img
.getImageData().width
;
1798 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1799 gc
.drawImage(img
, x
, y
);
1800 leftMargin
+= imgWidth
+ MARGIN
;
1802 String name
= item
.fName
;
1803 Point size
= gc
.stringExtent(name
);
1804 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1805 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1807 if (hasTimeEvents
) {
1808 // cut long string with "..."
1809 int width
= bounds
.width
- leftMargin
;
1811 while (size
.x
> width
&& name
.length() > 1) {
1813 name
= name
.substring(0, name
.length() - 1);
1814 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1817 name
+= "..."; //$NON-NLS-1$
1820 Rectangle rect
= Utils
.clone(bounds
);
1821 rect
.x
+= leftMargin
;
1822 rect
.width
-= leftMargin
;
1824 if (rect
.width
> 0) {
1825 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1826 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1827 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1828 leftMargin
+= textWidth
+ MARGIN
;
1831 if (hasTimeEvents
) {
1833 int x
= bounds
.x
+ leftMargin
;
1834 int width
= bounds
.width
- x
;
1835 int midy
= bounds
.y
+ bounds
.height
/ 2;
1836 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1837 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1843 * Draw the state (color fill)
1848 * Time event for which we're drawing the state
1854 * Is this time event currently selected (so it appears
1856 * @param timeSelected
1857 * Is the timestamp currently selected
1858 * @return true if the state was drawn
1860 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1861 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1863 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1864 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1867 boolean visible
= rect
.width
== 0 ?
false : true;
1868 rect
.width
= Math
.max(1, rect
.width
);
1869 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1870 gc
.setForeground(black
);
1872 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1874 // Only draw the top and bottom borders
1875 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1876 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1877 if (rect
.width
== 1) {
1878 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1881 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1884 Color stateColor
= null;
1885 if (colorIdx
< fEventColorMap
.length
) {
1886 stateColor
= fEventColorMap
[colorIdx
];
1891 boolean reallySelected
= timeSelected
&& selected
;
1892 // fill all rect area
1893 gc
.setBackground(stateColor
);
1895 gc
.fillRectangle(rect
);
1896 } else if (fBlendSubPixelEvents
) {
1898 gc
.fillRectangle(rect
);
1902 if (reallySelected
) {
1903 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1904 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1907 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1909 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1914 * Fill an item's states rectangle
1921 * true if the item is selected
1923 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1925 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1926 int midy
= rect
.y
+ rect
.height
/ 2;
1927 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1931 public void keyTraversed(TraverseEvent e
) {
1932 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1938 public void keyPressed(KeyEvent e
) {
1940 if (fItemData
.fExpandedItems
.length
== 0) {
1943 if (SWT
.HOME
== e
.keyCode
) {
1945 } else if (SWT
.END
== e
.keyCode
) {
1946 idx
= fItemData
.fExpandedItems
.length
- 1;
1947 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1948 idx
= getSelectedIndex();
1951 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1954 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1955 idx
= getSelectedIndex();
1958 } else if (idx
> 0) {
1961 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
1962 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1963 selectPrevEvent(extend
);
1964 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
1965 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1966 selectNextEvent(extend
);
1967 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1968 int page
= countPerPage();
1969 idx
= getSelectedIndex();
1974 if (idx
>= fItemData
.fExpandedItems
.length
) {
1975 idx
= fItemData
.fExpandedItems
.length
- 1;
1977 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1978 int page
= countPerPage();
1979 idx
= getSelectedIndex();
1987 } else if (SWT
.CR
== e
.keyCode
) {
1988 idx
= getSelectedIndex();
1990 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1993 fireDefaultSelection();
1999 selectItem(idx
, false);
2000 fireSelectionChanged();
2002 int x
= toControl(e
.display
.getCursorLocation()).x
;
2003 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2007 public void keyReleased(KeyEvent e
) {
2008 int x
= toControl(e
.display
.getCursorLocation()).x
;
2009 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2013 public void focusGained(FocusEvent e
) {
2016 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2020 public void focusLost(FocusEvent e
) {
2022 if (DRAG_NONE
!= fDragState
) {
2024 fDragState
= DRAG_NONE
;
2027 updateStatusLine(NO_STATUS
);
2031 * @return If the current view is focused
2033 public boolean isInFocus() {
2038 * Provide the possibility to control the wait cursor externally e.g. data
2039 * requests in progress
2041 * @param waitInd Should we wait indefinitely?
2043 public void waitCursor(boolean waitInd
) {
2044 // Update cursor as indicated
2046 setCursor(fWaitCursor
);
2052 private void updateCursor(int x
, int stateMask
) {
2053 // if Wait cursor not active, check for the need to change the cursor
2054 if (getCursor() == fWaitCursor
) {
2057 Cursor cursor
= null;
2058 if (fDragState
== DRAG_SPLIT_LINE
) {
2059 } else if (fDragState
== DRAG_SELECTION
) {
2060 cursor
= fResizeCursor
;
2061 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2062 cursor
= fDragCursor
;
2063 } else if (fDragState
== DRAG_ZOOM
) {
2064 cursor
= fZoomCursor
;
2065 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2066 cursor
= fDragCursor
;
2067 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2068 cursor
= fResizeCursor
;
2069 } else if (!isOverSplitLine(x
)) {
2070 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2071 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2072 int xBegin
= getXForTime(selectionBegin
);
2073 int xEnd
= getXForTime(selectionEnd
);
2074 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2075 cursor
= fResizeCursor
;
2078 if (getCursor() != cursor
) {
2083 private void updateStatusLine(int x
) {
2084 // use the time provider of the time graph scale for the status line
2085 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2086 if (fStatusLineManager
== null || null == tdp
||
2087 tdp
.getTime0() == tdp
.getTime1()) {
2090 TimeFormat tf
= tdp
.getTimeFormat();
2091 Resolution res
= Resolution
.NANOSEC
;
2092 StringBuilder message
= new StringBuilder();
2093 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2094 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2095 long time
= getTimeAtX(x
);
2097 if (tdp
instanceof ITimeDataProviderConverter
) {
2098 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2100 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2102 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2103 Utils
.formatTime(time
, tf
, res
)
2107 long selectionBegin
= tdp
.getSelectionBegin();
2108 long selectionEnd
= tdp
.getSelectionEnd();
2109 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2111 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2112 Utils
.formatTime(selectionBegin
, tf
, res
)
2114 if (selectionBegin
!= selectionEnd
) {
2115 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2117 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2118 Utils
.formatTime(selectionEnd
, tf
, res
),
2119 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2122 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2123 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2124 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2125 if (tdp
instanceof ITimeDataProviderConverter
) {
2126 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2127 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2129 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2131 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2132 Utils
.formatTime(time0
, tf
, res
),
2133 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2134 Utils
.formatTime(time
, tf
, res
),
2135 Utils
.formatDelta(time
- time0
, tf
, res
)
2138 fStatusLineManager
.setMessage(message
.toString());
2142 public void mouseMove(MouseEvent e
) {
2143 if (null == fTimeProvider
) {
2146 Point size
= getSize();
2147 if (DRAG_TRACE_ITEM
== fDragState
) {
2148 int nameWidth
= fTimeProvider
.getNameSpace();
2149 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2151 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2152 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2153 long time1
= fTime1bak
- timeDelta
;
2154 long maxTime
= fTimeProvider
.getMaxTime();
2155 if (time1
> maxTime
) {
2158 long time0
= time1
- (fTime1bak
- fTime0bak
);
2159 if (time0
< fTimeProvider
.getMinTime()) {
2160 time0
= fTimeProvider
.getMinTime();
2161 time1
= time0
+ (fTime1bak
- fTime0bak
);
2163 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2165 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2167 fTimeProvider
.setNameSpace(e
.x
);
2168 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2169 } else if (DRAG_SELECTION
== fDragState
) {
2170 if (fDragBeginMarker
) {
2171 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2173 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2176 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2177 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2178 } else if (DRAG_ZOOM
== fDragState
) {
2179 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2181 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2182 } else if (DRAG_NONE
== fDragState
) {
2183 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2184 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2187 fMouseOverSplitLine
= mouseOverSplitLine
;
2189 updateCursor(e
.x
, e
.stateMask
);
2190 updateStatusLine(e
.x
);
2194 public void mouseDoubleClick(MouseEvent e
) {
2195 if (null == fTimeProvider
) {
2198 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2199 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2200 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2201 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2202 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2205 fMouseOverSplitLine
= mouseOverSplitLine
;
2206 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2209 int idx
= getItemIndexAtY(e
.y
);
2211 selectItem(idx
, false);
2212 fireDefaultSelection();
2218 public void mouseDown(MouseEvent e
) {
2219 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2220 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2221 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2225 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2226 int nameSpace
= fTimeProvider
.getNameSpace();
2227 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2228 fDragState
= DRAG_SPLIT_LINE
;
2229 fDragButton
= e
.button
;
2232 fTime0bak
= fTimeProvider
.getTime0();
2233 fTime1bak
= fTimeProvider
.getTime1();
2235 updateCursor(e
.x
, e
.stateMask
);
2239 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2240 int nameSpace
= fTimeProvider
.getNameSpace();
2241 idx
= getItemIndexAtY(e
.y
);
2243 Item item
= fItemData
.fExpandedItems
[idx
];
2244 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2248 selectItem(idx
, false);
2249 fireSelectionChanged();
2251 selectItem(idx
, false); // clear selection
2252 fireSelectionChanged();
2254 long hitTime
= getTimeAtX(e
.x
);
2258 fDragState
= DRAG_SELECTION
;
2259 fDragBeginMarker
= false;
2260 fDragButton
= e
.button
;
2263 fDragTime0
= getTimeAtX(fDragX0
);
2264 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2265 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2266 int xBegin
= getXForTime(selectionBegin
);
2267 int xEnd
= getXForTime(selectionEnd
);
2268 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2269 long time
= getTimeAtX(e
.x
);
2270 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2271 fDragBeginMarker
= true;
2274 fDragTime0
= selectionEnd
;
2277 fDragTime0
= selectionBegin
;
2280 long time
= getTimeAtX(e
.x
);
2281 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2282 fDragBeginMarker
= true;
2285 fDragTime0
= selectionEnd
;
2286 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2288 fDragTime0
= selectionBegin
;
2291 fTime0bak
= fTimeProvider
.getTime0();
2292 fTime1bak
= fTimeProvider
.getTime1();
2294 updateCursor(e
.x
, e
.stateMask
);
2295 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2297 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2298 long hitTime
= getTimeAtX(e
.x
);
2301 fDragState
= DRAG_TRACE_ITEM
;
2302 fDragButton
= e
.button
;
2305 fTime0bak
= fTimeProvider
.getTime0();
2306 fTime1bak
= fTimeProvider
.getTime1();
2307 updateCursor(e
.x
, e
.stateMask
);
2309 } else if (3 == e
.button
) {
2311 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2313 fDragTime0
= getTimeAtX(fDragX0
);
2314 fDragState
= DRAG_ZOOM
;
2315 fDragButton
= e
.button
;
2317 updateCursor(e
.x
, e
.stateMask
);
2318 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2323 public void mouseUp(MouseEvent e
) {
2324 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2325 menuDetected(fPendingMenuDetectEvent
);
2327 if (DRAG_NONE
!= fDragState
) {
2329 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2330 if (fDragX
!= fDragX0
) {
2331 fTimeProvider
.notifyStartFinishTime();
2333 fDragState
= DRAG_NONE
;
2334 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2335 fDragState
= DRAG_NONE
;
2337 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2338 if (fDragX
== fDragX0
) { // click without selecting anything
2339 long time
= getTimeAtX(e
.x
);
2340 fTimeProvider
.setSelectedTimeNotify(time
, false);
2342 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2343 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2344 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2346 fDragState
= DRAG_NONE
;
2348 fTimeGraphScale
.setDragRange(-1, -1);
2349 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2350 int nameWidth
= fTimeProvider
.getNameSpace();
2351 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2352 long time0
= getTimeAtX(fDragX0
);
2353 long time1
= getTimeAtX(fDragX
);
2354 if (time0
< time1
) {
2355 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2357 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2362 fDragState
= DRAG_NONE
;
2363 fTimeGraphScale
.setDragRange(-1, -1);
2366 updateCursor(e
.x
, e
.stateMask
);
2367 updateStatusLine(e
.x
);
2371 public void mouseEnter(MouseEvent e
) {
2375 public void mouseExit(MouseEvent e
) {
2376 if (fMouseOverSplitLine
) {
2377 fMouseOverSplitLine
= false;
2380 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2384 public void mouseHover(MouseEvent e
) {
2388 public void mouseScrolled(MouseEvent e
) {
2389 if (fDragState
!= DRAG_NONE
) {
2392 boolean zoomScroll
= false;
2393 boolean horizontalScroll
= false;
2394 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2395 Point parentSize
= getParent().getSize();
2396 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2397 // over the parent control
2398 if (e
.x
> getSize().x
) {
2399 // over the vertical scroll bar
2401 } else if (e
.y
< 0) {
2402 // over the time scale
2404 } else if (e
.y
>= getSize().y
) {
2405 // over the horizontal scroll bar
2406 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2409 horizontalScroll
= true;
2412 if (e
.x
< fTimeProvider
.getNameSpace()) {
2413 // over the name space
2416 // over the state area
2417 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2418 // over the state area, CTRL pressed
2421 // over the state area, CTRL not pressed
2427 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2430 } else if (e
.count
< 0) {
2433 } else if (horizontalScroll
) {
2434 horizontalScroll(e
.count
> 0);
2436 setTopIndex(getTopIndex() - e
.count
);
2441 public void handleEvent(Event event
) {
2442 if (event
.type
== SWT
.MouseWheel
) {
2443 // prevent horizontal scrolling when the mouse wheel is used to
2444 // scroll vertically or zoom
2450 public int getBorderWidth() {
2451 return fBorderWidth
;
2455 * Set the border width
2457 * @param borderWidth
2460 public void setBorderWidth(int borderWidth
) {
2461 this.fBorderWidth
= borderWidth
;
2465 * @return The current height of the header row
2467 public int getHeaderHeight() {
2468 return fHeaderHeight
;
2472 * Set the height of the header row
2474 * @param headerHeight
2477 public void setHeaderHeight(int headerHeight
) {
2478 this.fHeaderHeight
= headerHeight
;
2482 * @return The default height of regular item rows
2484 public int getItemHeight() {
2485 return fGlobalItemHeight
;
2489 * Set the default height of regular item rows.
2494 public void setItemHeight(int rowHeight
) {
2495 this.fGlobalItemHeight
= rowHeight
;
2496 for (Item item
: fItemData
.fItems
) {
2497 item
.fItemHeight
= rowHeight
;
2502 * Set the height of a specific item. Overrides the default item height.
2505 * A time graph entry
2508 * @return true if the height is successfully stored, false otherwise
2510 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2511 Item item
= fItemData
.findItem(entry
);
2513 item
.fItemHeight
= rowHeight
;
2520 * Set the minimum item width
2522 * @param width The minimum width
2524 public void setMinimumItemWidth(int width
) {
2525 this.fMinimumItemWidth
= width
;
2529 * @return The minimum item width
2531 public int getMinimumItemWidth() {
2532 return fMinimumItemWidth
;
2536 * Set whether all time events with a duration shorter than one pixel should
2537 * be blended in. If false, only the first such time event will be drawn and
2538 * the subsequent time events in the same pixel will be discarded. The
2539 * default value is false.
2542 * true if sub-pixel events should be blended, false otherwise.
2545 public void setBlendSubPixelEvents(boolean blend
) {
2546 fBlendSubPixelEvents
= blend
;
2550 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2551 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2552 fSelectionChangedListeners
.add(listener
);
2557 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2558 if (listener
!= null) {
2559 fSelectionChangedListeners
.remove(listener
);
2564 public void setSelection(ISelection selection
) {
2565 if (selection
instanceof TimeGraphSelection
) {
2566 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2567 Object ob
= sel
.getFirstElement();
2568 if (ob
instanceof ITimeGraphEntry
) {
2569 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2570 selectItem(trace
, false);
2577 * @param filter The filter object to be attached to the view
2579 public void addFilter(ViewerFilter filter
) {
2580 if (!fFilters
.contains(filter
)) {
2581 fFilters
.add(filter
);
2586 * @param filter The filter object to be attached to the view
2588 public void removeFilter(ViewerFilter filter
) {
2589 fFilters
.remove(filter
);
2593 public void colorSettingsChanged(StateItem
[] stateItems
) {
2594 /* Destroy previous colors from the resource manager */
2595 if (fEventColorMap
!= null) {
2596 for (Color color
: fEventColorMap
) {
2597 fResourceManager
.destroyColor(color
.getRGB());
2600 if (stateItems
!= null) {
2601 fEventColorMap
= new Color
[stateItems
.length
];
2602 for (int i
= 0; i
< stateItems
.length
; i
++) {
2603 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2606 fEventColorMap
= new Color
[] { };
2611 private class ItemData
{
2612 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2613 private Item
[] fExpandedItems
= new Item
[0];
2614 private Item
[] fItems
= new Item
[0];
2615 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2616 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2621 public Item
findItem(ITimeGraphEntry entry
) {
2622 return fItemMap
.get(entry
);
2625 public int findItemIndex(ITimeGraphEntry entry
) {
2626 Item item
= fItemMap
.get(entry
);
2630 return item
.fExpandedIndex
;
2633 public void refreshData() {
2634 ITimeGraphEntry selection
= getSelectedTrace();
2635 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
2636 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2637 ITimeGraphEntry entry
= fRootEntries
[i
];
2638 refreshData(itemMap
, null, 0, entry
);
2641 fItems
= fItemMap
.values().toArray(new Item
[0]);
2642 updateExpandedItems();
2643 if (selection
!= null) {
2644 for (Item item
: fExpandedItems
) {
2645 if (item
.fEntry
== selection
) {
2646 item
.fSelected
= true;
2653 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2654 Item item
= new Item(entry
, entry
.getName(), level
);
2655 if (parent
!= null) {
2656 parent
.fChildren
.add(item
);
2658 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2659 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2661 item
.fItemHeight
= fGlobalItemHeight
;
2663 itemMap
.put(entry
, item
);
2664 if (entry
.hasChildren()) {
2665 Item oldItem
= fItemMap
.get(entry
);
2666 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
2667 /* existing items keep their old expanded state */
2668 item
.fExpanded
= oldItem
.fExpanded
;
2670 /* new items set the expanded state according to auto-expand level */
2671 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2673 item
.fHasChildren
= true;
2674 for (ITimeGraphEntry child
: entry
.getChildren()) {
2675 refreshData(itemMap
, item
, level
+ 1, child
);
2680 public void updateExpandedItems() {
2681 for (Item item
: fItems
) {
2682 item
.fExpandedIndex
= -1;
2684 List
<Item
> expandedItemList
= new ArrayList
<>();
2685 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2686 ITimeGraphEntry entry
= fRootEntries
[i
];
2687 Item item
= findItem(entry
);
2688 refreshExpanded(expandedItemList
, item
);
2690 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2691 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2694 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2695 // Check for filters
2696 boolean display
= true;
2697 for (ViewerFilter filter
: fFilters
) {
2698 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2704 item
.fExpandedIndex
= expandedItemList
.size();
2705 expandedItemList
.add(item
);
2706 if (item
.fHasChildren
&& item
.fExpanded
) {
2707 for (Item child
: item
.fChildren
) {
2708 refreshExpanded(expandedItemList
, child
);
2714 public void refreshData(ITimeGraphEntry
[] entries
) {
2715 if (entries
== null) {
2716 fRootEntries
= null;
2718 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2724 public void refreshArrows(List
<ILinkEvent
> events
) {
2725 /* If links are null, reset the list */
2726 if (events
!= null) {
2729 fLinks
= new ArrayList
<>();
2733 public ITimeGraphEntry
[] getEntries() {
2734 return fRootEntries
;
2738 private class Item
{
2739 private boolean fExpanded
;
2740 private int fExpandedIndex
;
2741 private boolean fSelected
;
2742 private boolean fHasChildren
;
2743 private int fItemHeight
;
2744 private final int fLevel
;
2745 private final List
<Item
> fChildren
;
2746 private final String fName
;
2747 private final ITimeGraphEntry fEntry
;
2749 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2750 this.fEntry
= entry
;
2752 this.fLevel
= level
;
2753 this.fChildren
= new ArrayList
<>();
2757 public String
toString() {
2763 public void menuDetected(MenuDetectEvent e
) {
2764 if (null == fTimeProvider
) {
2767 if (e
.detail
== SWT
.MENU_MOUSE
) {
2768 if (fPendingMenuDetectEvent
== null) {
2769 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2770 * Store the event and trigger it later just before handling mouseUp.
2771 * This allows for the method to detect if mouse is used to drag zoom.
2773 fPendingMenuDetectEvent
= e
;
2776 fPendingMenuDetectEvent
= null;
2777 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2781 if (fDragState
!= DRAG_NONE
) {
2785 Point p
= toControl(e
.x
, e
.y
);
2786 int idx
= getItemIndexAtY(p
.y
);
2787 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2788 Item item
= fItemData
.fExpandedItems
[idx
];
2789 ITimeGraphEntry entry
= item
.fEntry
;
2790 if (entry
.hasTimeEvents()) {
2791 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2792 if (event
!= null) {
2794 fireMenuEventOnTimeEvent(e
);
2799 fireMenuEventOnTimeGraphEntry(e
);
2804 * Perform the alignment operation.
2807 * the alignment offset
2809 * @see ITmfTimeAligned
2813 public void performAlign(int offset
) {
2814 fTimeProvider
.setNameSpace(offset
);
2818 * Return the time alignment information
2820 * @return the time alignment information
2822 * @see ITmfTimeAligned
2826 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
2827 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());