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;
114 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
116 /** Resource manager */
117 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
119 /** Color map for event types */
120 private Color
[] fEventColorMap
= null;
122 private ITimeDataProvider fTimeProvider
;
123 private IStatusLineManager fStatusLineManager
= null;
124 private TimeGraphScale fTimeGraphScale
= null;
126 private boolean fIsInFocus
= false;
127 private boolean fMouseOverSplitLine
= false;
128 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
129 private int fMinimumItemWidth
= 0;
130 private int fTopIndex
= 0;
131 private int fDragState
= DRAG_NONE
;
132 private boolean fDragBeginMarker
= false;
133 private int fDragButton
;
134 private int fDragX0
= 0;
135 private int fDragX
= 0;
136 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
137 private int fIdealNameSpace
= 0;
138 private long fTime0bak
;
139 private long fTime1bak
;
140 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
141 private ItemData fItemData
= null;
142 private List
<SelectionListener
> fSelectionListeners
;
143 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
144 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
145 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
146 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
147 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
148 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
149 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
150 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
151 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
152 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
153 private MenuDetectEvent fPendingMenuDetectEvent
= null;
154 private boolean fHideArrows
= false;
155 private int fAutoExpandLevel
= ALL_LEVELS
;
157 private int fBorderWidth
= 0;
158 private int fHeaderHeight
= 0;
161 * Standard constructor
164 * The parent composite object
166 * The color scheme to use
168 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
170 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
172 fItemData
= new ItemData();
174 addFocusListener(this);
175 addMouseListener(this);
176 addMouseMoveListener(this);
177 addMouseTrackListener(this);
178 addMouseWheelListener(this);
179 addTraverseListener(this);
180 addKeyListener(this);
181 addMenuDetectListener(this);
182 addListener(SWT
.MouseWheel
, this);
186 public void dispose() {
188 fResourceManager
.dispose();
192 * Sets the timegraph provider used by this timegraph viewer.
194 * @param timeGraphProvider the timegraph provider
196 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
197 fTimeGraphProvider
= timeGraphProvider
;
199 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
200 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
201 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
204 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
205 colorSettingsChanged(stateItems
);
209 * Gets the timegraph provider used by this timegraph viewer.
211 * @return the timegraph provider, or <code>null</code> if not set.
213 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
214 return fTimeGraphProvider
;
218 * Gets the color map used by this timegraph viewer.
220 * @return a color map, or <code>null</code> if not set.
222 public Color
[] getEventColorMap() {
223 return fEventColorMap
;
227 * Assign the given time provider
229 * @param timeProvider
232 public void setTimeProvider(ITimeDataProvider timeProvider
) {
233 fTimeProvider
= timeProvider
;
238 * Assign the status line manager
240 * @param statusLineManager
241 * The status line manager, or null to disable status line messages
243 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
244 if (fStatusLineManager
!= null && statusLineManager
== null) {
245 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
247 fStatusLineManager
= statusLineManager
;
251 * Assign the time graph scale
253 * @param timeGraphScale
254 * The time graph scale
256 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
257 fTimeGraphScale
= timeGraphScale
;
261 * Add a selection listener
264 * The listener to add
266 public void addSelectionListener(SelectionListener listener
) {
267 if (listener
== null) {
268 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
270 if (null == fSelectionListeners
) {
271 fSelectionListeners
= new ArrayList
<>();
273 fSelectionListeners
.add(listener
);
277 * Remove a selection listener
280 * The listener to remove
282 public void removeSelectionListener(SelectionListener listener
) {
283 if (null != fSelectionListeners
) {
284 fSelectionListeners
.remove(listener
);
289 * Selection changed callback
291 public void fireSelectionChanged() {
292 if (null != fSelectionListeners
) {
293 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
294 while (it
.hasNext()) {
295 SelectionListener listener
= it
.next();
296 listener
.widgetSelected(null);
302 * Default selection callback
304 public void fireDefaultSelection() {
305 if (null != fSelectionListeners
) {
306 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
307 while (it
.hasNext()) {
308 SelectionListener listener
= it
.next();
309 listener
.widgetDefaultSelected(null);
315 * Add a drag selection listener
318 * The listener to add
320 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
321 if (listener
== null) {
322 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
324 if (null == fDragSelectionListeners
) {
325 fDragSelectionListeners
= new ArrayList
<>();
327 fDragSelectionListeners
.add(listener
);
331 * Remove a drag selection listener
334 * The listener to remove
336 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
337 if (null != fDragSelectionListeners
) {
338 fDragSelectionListeners
.remove(listener
);
343 * Drag Selection changed callback
346 * Time interval start
350 public void fireDragSelectionChanged(long start
, long end
) {
351 // check for backward intervals
352 long beginTime
, endTime
;
360 // call the listeners
361 if (null != fDragSelectionListeners
) {
362 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
363 while (it
.hasNext()) {
364 ITimeGraphTimeListener listener
= it
.next();
365 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
371 * Get the traces in the model
373 * @return The array of traces
375 public ITimeGraphEntry
[] getTraces() {
376 return fItemData
.getEntries();
380 * Get the on/off trace filters
382 * @return The array of filters
384 public boolean[] getTraceFilter() {
385 return fItemData
.getEntryFilter();
389 * Refresh the data for the thing
391 public void refreshData() {
392 fItemData
.refreshData();
397 * Refresh data for the given traces
400 * The traces to refresh
402 public void refreshData(ITimeGraphEntry
[] traces
) {
403 fItemData
.refreshData(traces
);
408 * Refresh the links (arrows) of this widget
410 * @param events The link events to refresh
412 public void refreshArrows(List
<ILinkEvent
> events
) {
413 fItemData
.refreshArrows(events
);
416 boolean ensureVisibleItem(int idx
, boolean redraw
) {
417 boolean changed
= false;
420 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
421 if (fItemData
.fExpandedItems
[index
].fSelected
) {
426 if (index
>= fItemData
.fExpandedItems
.length
) {
429 if (index
< fTopIndex
) {
436 int page
= countPerPage();
437 if (index
>= fTopIndex
+ page
) {
438 setTopIndex(index
- page
+ 1);
449 * Assign the given index as the top one
454 public void setTopIndex(int idx
) {
455 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
456 index
= Math
.max(0, index
);
462 * Sets the auto-expand level to be used when the entries are refreshed
463 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
464 * The value 0 means that there is no auto-expand; 1 means that top-level
465 * entries are expanded, but not their children; 2 means that top-level
466 * entries are expanded, and their children, but not grand-children; and so
469 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
472 * non-negative level, or <code>ALL_LEVELS</code> to expand all
475 public void setAutoExpandLevel(int level
) {
476 fAutoExpandLevel
= level
;
480 * Returns the auto-expand level.
482 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
483 * the tree are expanded automatically
484 * @see #setAutoExpandLevel
486 public int getAutoExpandLevel() {
487 return fAutoExpandLevel
;
491 * Set the expanded state of a given entry
496 * True if expanded, false if collapsed
498 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
499 Item item
= fItemData
.findItem(entry
);
500 if (item
!= null && item
.fExpanded
!= expanded
) {
501 item
.fExpanded
= expanded
;
502 fItemData
.updateExpandedItems();
508 * Collapses all nodes of the viewer's tree, starting with the root.
510 public void collapseAll() {
511 for (Item item
: fItemData
.fItems
) {
512 item
.fExpanded
= false;
514 fItemData
.updateExpandedItems();
519 * Expands all nodes of the viewer's tree, starting with the root.
521 public void expandAll() {
522 for (Item item
: fItemData
.fItems
) {
523 item
.fExpanded
= true;
525 fItemData
.updateExpandedItems();
530 * Add a tree listener
533 * The listener to add
535 public void addTreeListener(ITimeGraphTreeListener listener
) {
536 if (!fTreeListeners
.contains(listener
)) {
537 fTreeListeners
.add(listener
);
542 * Remove a tree listener
545 * The listener to remove
547 public void removeTreeListener(ITimeGraphTreeListener listener
) {
548 if (fTreeListeners
.contains(listener
)) {
549 fTreeListeners
.remove(listener
);
554 * Tree event callback
559 * The expanded state (true for expanded, false for collapsed)
561 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
562 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
563 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
565 listener
.treeExpanded(event
);
567 listener
.treeCollapsed(event
);
573 * Add a menu listener on {@link ITimeGraphEntry}s
576 * The listener to add
578 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
579 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
580 fTimeGraphEntryMenuListeners
.add(listener
);
585 * Remove a menu listener on {@link ITimeGraphEntry}s
588 * The listener to remove
590 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
591 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
592 fTimeGraphEntryMenuListeners
.remove(listener
);
597 * Menu event callback on {@link ITimeGraphEntry}s
600 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
602 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
603 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
604 listener
.menuDetected(event
);
609 * Add a menu listener on {@link ITimeEvent}s
612 * The listener to add
614 public void addTimeEventMenuListener(MenuDetectListener listener
) {
615 if (!fTimeEventMenuListeners
.contains(listener
)) {
616 fTimeEventMenuListeners
.add(listener
);
621 * Remove a menu listener on {@link ITimeEvent}s
624 * The listener to remove
626 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
627 if (fTimeEventMenuListeners
.contains(listener
)) {
628 fTimeEventMenuListeners
.remove(listener
);
633 * Menu event callback on {@link ITimeEvent}s
636 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
638 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
639 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
640 listener
.menuDetected(event
);
645 public ISelection
getSelection() {
646 TimeGraphSelection sel
= new TimeGraphSelection();
647 ITimeGraphEntry trace
= getSelectedTrace();
648 if (null != trace
&& null != fTimeProvider
) {
649 long selectedTime
= fTimeProvider
.getSelectionBegin();
650 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
661 * Get the selection object
663 * @return The selection
665 public ISelection
getSelectionTrace() {
666 TimeGraphSelection sel
= new TimeGraphSelection();
667 ITimeGraphEntry trace
= getSelectedTrace();
675 * Enable/disable one of the traces in the model
678 * 1 to enable it, -1 to disable. The method returns immediately
679 * if another value is used.
681 public void selectTrace(int n
) {
682 if ((n
!= 1) && (n
!= -1)) {
686 boolean changed
= false;
687 int lastSelection
= -1;
688 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
689 Item item
= fItemData
.fExpandedItems
[i
];
690 if (item
.fSelected
) {
692 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
693 item
.fSelected
= false;
694 item
= fItemData
.fExpandedItems
[i
+ 1];
695 item
.fSelected
= true;
697 } else if ((-1 == n
) && (i
> 0)) {
698 item
.fSelected
= false;
699 item
= fItemData
.fExpandedItems
[i
- 1];
700 item
.fSelected
= true;
707 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
708 Item item
= fItemData
.fExpandedItems
[0];
709 item
.fSelected
= true;
714 ensureVisibleItem(-1, false);
716 fireSelectionChanged();
724 * 1 for next event, -1 for previous event
726 * true to extend selection range, false for single selection
729 public void selectEvent(int n
, boolean extend
) {
730 if (null == fTimeProvider
) {
733 ITimeGraphEntry trace
= getSelectedTrace();
737 long selectedTime
= fTimeProvider
.getSelectionEnd();
738 long endTime
= fTimeProvider
.getMaxTime();
739 ITimeEvent nextEvent
;
740 if (n
== -1 && selectedTime
> endTime
) {
741 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
743 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
745 if (null == nextEvent
&& n
== -1) {
746 nextEvent
= Utils
.getFirstEvent(trace
);
748 if (null != nextEvent
) {
749 long nextTime
= nextEvent
.getTime();
750 // If last event detected e.g. going back or not moving to a next
752 if (nextTime
<= selectedTime
&& n
== 1) {
753 // Select to the end of this last event
754 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
755 // but not beyond the end of the trace
756 if (nextTime
> endTime
) {
759 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
760 // for previous event go to its end time unless we were already there
761 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
764 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
);
766 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
768 fireSelectionChanged();
771 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
);
773 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
775 fireSelectionChanged();
777 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
781 * Select the next event
784 * true to extend selection range, false for single selection
787 public void selectNextEvent(boolean extend
) {
788 selectEvent(1, extend
);
789 // Notify if visible time window has been adjusted
790 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
794 * Select the previous event
797 * true to extend selection range, false for single selection
800 public void selectPrevEvent(boolean extend
) {
801 selectEvent(-1, extend
);
802 // Notify if visible time window has been adjusted
803 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
807 * Select the next trace
809 public void selectNextTrace() {
814 * Select the previous trace
816 public void selectPrevTrace() {
821 * Scroll left or right by one half window size
824 * true to scroll left, false to scroll right
826 public void horizontalScroll(boolean left
) {
827 long time0
= fTimeProvider
.getTime0();
828 long time1
= fTimeProvider
.getTime1();
829 long timeMin
= fTimeProvider
.getMinTime();
830 long timeMax
= fTimeProvider
.getMaxTime();
831 long range
= time1
- time0
;
835 long increment
= Math
.max(1, range
/ 2);
837 time0
= Math
.max(time0
- increment
, timeMin
);
838 time1
= time0
+ range
;
840 time1
= Math
.min(time1
+ increment
, timeMax
);
841 time0
= time1
- range
;
843 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
847 * Zoom based on mouse cursor location with mouse scrolling
849 * @param zoomIn true to zoom in, false to zoom out
851 public void zoom(boolean zoomIn
) {
852 int globalX
= getDisplay().getCursorLocation().x
;
853 Point p
= toControl(globalX
, 0);
854 int nameSpace
= fTimeProvider
.getNameSpace();
855 int timeSpace
= fTimeProvider
.getTimeSpace();
856 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
857 long time0
= fTimeProvider
.getTime0();
858 long time1
= fTimeProvider
.getTime1();
859 long interval
= time1
- time0
;
862 } // to allow getting out of single point interval
865 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
867 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
869 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
870 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
871 long newTime1
= newTime0
+ newInterval
;
872 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
876 * zoom in using single click
878 public void zoomIn() {
879 long prevTime0
= fTimeProvider
.getTime0();
880 long prevTime1
= fTimeProvider
.getTime1();
881 long prevRange
= prevTime1
- prevTime0
;
882 if (prevRange
== 0) {
885 ITimeDataProvider provider
= fTimeProvider
;
886 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
887 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
888 selTime
= (prevTime0
+ prevTime1
) / 2;
890 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
891 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
893 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
895 if (inaccuracy
> 0 && inaccuracy
< 100) {
896 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
900 long min
= fTimeProvider
.getMinTimeInterval();
901 if ((time1
- time0
) < min
) {
902 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
906 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
910 * zoom out using single click
912 public void zoomOut() {
913 long prevTime0
= fTimeProvider
.getTime0();
914 long prevTime1
= fTimeProvider
.getTime1();
915 ITimeDataProvider provider
= fTimeProvider
;
916 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
917 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
918 selTime
= (prevTime0
+ prevTime1
) / 2;
920 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
921 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
923 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
924 if (inaccuracy
> 0 && inaccuracy
< 100) {
925 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
929 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
935 * @param hideArrows true to hide arrows
937 public void hideArrows(boolean hideArrows
) {
938 fHideArrows
= hideArrows
;
942 * Follow the arrow forward
945 * true to extend selection range, false for single selection
948 public void followArrowFwd(boolean extend
) {
949 ITimeGraphEntry trace
= getSelectedTrace();
953 long selectedTime
= fTimeProvider
.getSelectionEnd();
954 for (ILinkEvent link
: fItemData
.fLinks
) {
955 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
956 selectItem(link
.getDestinationEntry(), false);
957 if (link
.getDuration() != 0) {
959 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration());
961 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
963 // Notify if visible time window has been adjusted
964 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
966 fireSelectionChanged();
967 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
971 selectNextEvent(extend
);
975 * Follow the arrow backward
978 * true to extend selection range, false for single selection
981 public void followArrowBwd(boolean extend
) {
982 ITimeGraphEntry trace
= getSelectedTrace();
986 long selectedTime
= fTimeProvider
.getSelectionEnd();
987 for (ILinkEvent link
: fItemData
.fLinks
) {
988 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
989 selectItem(link
.getEntry(), false);
990 if (link
.getDuration() != 0) {
992 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime());
994 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
996 // Notify if visible time window has been adjusted
997 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
999 fireSelectionChanged();
1000 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1004 selectPrevEvent(extend
);
1008 * Return the currently selected trace
1010 * @return The entry matching the trace
1012 public ITimeGraphEntry
getSelectedTrace() {
1013 ITimeGraphEntry trace
= null;
1014 int idx
= getSelectedIndex();
1016 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1022 * Retrieve the index of the currently selected item
1026 public int getSelectedIndex() {
1028 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1029 Item item
= fItemData
.fExpandedItems
[i
];
1030 if (item
.fSelected
) {
1038 boolean toggle(int idx
) {
1039 boolean toggled
= false;
1040 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1041 Item item
= fItemData
.fExpandedItems
[idx
];
1042 if (item
.fHasChildren
) {
1043 item
.fExpanded
= !item
.fExpanded
;
1044 fItemData
.updateExpandedItems();
1047 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1054 * Gets the index of the item at the given location.
1058 * @return the index of the item at the given location, of -1 if none.
1060 protected int getItemIndexAtY(int y
) {
1065 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1066 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1074 boolean isOverSplitLine(int x
) {
1075 if (x
< 0 || null == fTimeProvider
) {
1078 int nameWidth
= fTimeProvider
.getNameSpace();
1079 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1083 * Gets the {@link ITimeGraphEntry} at the given location.
1086 * a point in the widget
1087 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1090 protected ITimeGraphEntry
getEntry(Point pt
) {
1091 int idx
= getItemIndexAtY(pt
.y
);
1092 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1096 * Return the arrow event closest to the given point that is no further than
1097 * a maximum distance.
1100 * a point in the widget
1101 * @return The closest arrow event, or null if there is none close enough.
1103 protected ILinkEvent
getArrow(Point pt
) {
1107 ILinkEvent linkEvent
= null;
1108 double minDistance
= Double
.MAX_VALUE
;
1109 for (ILinkEvent event
: fItemData
.fLinks
) {
1110 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1114 int x2
= x1
+ rect
.width
;
1115 int y2
= y1
+ rect
.height
;
1116 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1117 if (minDistance
> d
) {
1123 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1130 public int getXForTime(long time
) {
1131 if (null == fTimeProvider
) {
1134 long time0
= fTimeProvider
.getTime0();
1135 long time1
= fTimeProvider
.getTime1();
1136 int width
= getSize().x
;
1137 int nameSpace
= fTimeProvider
.getNameSpace();
1138 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1139 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1144 public long getTimeAtX(int coord
) {
1145 if (null == fTimeProvider
) {
1149 Point size
= getSize();
1150 long time0
= fTimeProvider
.getTime0();
1151 long time1
= fTimeProvider
.getTime1();
1152 int nameWidth
= fTimeProvider
.getNameSpace();
1153 final int x
= coord
- nameWidth
;
1154 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1155 if (x
>= 0 && size
.x
>= nameWidth
) {
1156 if (time1
- time0
> timeWidth
) {
1157 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1158 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1160 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1161 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1167 void selectItem(int idx
, boolean addSelection
) {
1168 boolean changed
= false;
1170 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1171 Item item
= fItemData
.fExpandedItems
[idx
];
1172 changed
= !item
.fSelected
;
1173 item
.fSelected
= true;
1176 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1177 Item item
= fItemData
.fExpandedItems
[i
];
1178 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1181 item
.fSelected
= i
== idx
;
1184 changed
|= ensureVisibleItem(idx
, true);
1191 * Callback for item selection
1194 * The entry matching the trace
1195 * @param addSelection
1196 * If the selection is added or removed
1198 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1199 int idx
= fItemData
.findItemIndex(trace
);
1200 selectItem(idx
, addSelection
);
1204 * Retrieve the number of entries shown per page.
1208 public int countPerPage() {
1209 int height
= getSize().y
;
1212 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1213 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1214 if (ySum
>= height
) {
1219 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1220 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1221 if (ySum
>= height
) {
1230 * Get the index of the top element
1234 public int getTopIndex() {
1239 * Get the number of expanded items
1241 * @return The count of expanded items
1243 public int getExpandedElementCount() {
1244 return fItemData
.fExpandedItems
.length
;
1248 * Get an array of all expanded elements
1250 * @return The expanded elements
1252 public ITimeGraphEntry
[] getExpandedElements() {
1253 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1254 for (Item item
: fItemData
.fExpandedItems
) {
1255 elements
.add(item
.fEntry
);
1257 return elements
.toArray(new ITimeGraphEntry
[0]);
1260 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1261 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1263 rect
.width
= nameWidth
;
1267 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1268 int x
= bound
.x
+ nameWidth
;
1269 int width
= bound
.width
- x
;
1271 if (idx
>= fTopIndex
) {
1272 for (int i
= fTopIndex
; i
< idx
; i
++) {
1273 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1276 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1277 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1280 int y
= bound
.y
+ ySum
;
1281 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1282 return new Rectangle(x
, y
, width
, height
);
1286 void paint(Rectangle bounds
, PaintEvent e
) {
1288 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1289 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1291 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1295 fIdealNameSpace
= 0;
1296 int nameSpace
= fTimeProvider
.getNameSpace();
1298 // draw empty name space background
1299 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1300 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1303 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1304 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1305 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1307 int alpha
= gc
.getAlpha();
1310 long time0
= fTimeProvider
.getTime0();
1311 long time1
= fTimeProvider
.getTime1();
1312 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1313 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1314 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1315 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1316 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1318 // draw selection lines
1319 if (fDragState
!= DRAG_SELECTION
) {
1320 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1321 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1322 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1325 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1326 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1331 // draw selection background
1332 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1333 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1334 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1335 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1337 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1338 } else if (x0
- x1
> 1) {
1339 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1343 // draw drag selection background
1344 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1345 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1346 if (fDragX0
< fDragX
) {
1347 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1348 } else if (fDragX0
> fDragX
) {
1349 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1354 if (DRAG_SPLIT_LINE
== fDragState
) {
1355 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1356 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1357 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1358 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1359 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1360 if (fDragX
!= fDragX0
) {
1361 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1363 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1364 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1365 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1366 if (fDragX
!= fDragX0
) {
1367 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1369 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1370 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1371 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1378 * Draw many items at once
1381 * The rectangle of the area
1382 * @param timeProvider
1385 * The array items to draw
1387 * The index of the first element to draw
1389 * The width reserved for the names
1391 * Reference to the SWT GC object
1393 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1394 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1395 for (int i
= topIndex
; i
< items
.length
; i
++) {
1396 Item item
= items
[i
];
1397 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1404 * @param item the item to draw
1405 * @param bounds the container rectangle
1406 * @param timeProvider Time provider
1407 * @param i the item index
1408 * @param nameSpace the name space
1409 * @param gc Graphics context
1411 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1412 ITimeGraphEntry entry
= item
.fEntry
;
1413 long time0
= timeProvider
.getTime0();
1414 long time1
= timeProvider
.getTime1();
1415 long selectedTime
= fTimeProvider
.getSelectionEnd();
1417 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1418 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1422 if (! item
.fEntry
.hasTimeEvents()) {
1423 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1424 nameRect
.width
+= statesRect
.width
;
1425 drawName(item
, nameRect
, gc
);
1427 drawName(item
, nameRect
, gc
);
1429 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1430 if (rect
.isEmpty()) {
1431 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1434 if (time1
<= time0
) {
1435 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1436 gc
.fillRectangle(rect
);
1437 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1441 // Initialize _rect1 to same values as enclosing rectangle rect
1442 Rectangle stateRect
= Utils
.clone(rect
);
1443 boolean selected
= item
.fSelected
;
1444 // K pixels per second
1445 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1447 if (item
.fEntry
.hasTimeEvents()) {
1448 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1449 fillSpace(rect
, gc
, selected
);
1450 // Drawing rectangle is smaller than reserved space
1452 stateRect
.height
-= 6;
1454 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1455 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1458 while (iterator
.hasNext()) {
1459 ITimeEvent event
= iterator
.next();
1460 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1461 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1462 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1463 // event is out of bounds
1466 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1467 stateRect
.x
= Math
.max(rect
.x
, x
);
1468 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1469 if (stateRect
.x
== lastX
) {
1470 stateRect
.width
-= 1;
1471 if (stateRect
.width
> 0) {
1472 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1473 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1477 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1478 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1482 gc
.setClipping((Rectangle
) null);
1484 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1491 * The rectangle of the area
1492 * @param timeProvider
1495 * The array items to draw
1497 * The width reserved for the names
1499 * Reference to the SWT GC object
1501 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1502 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1506 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1507 for (ILinkEvent event
: links
) {
1508 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1510 gc
.setClipping((Rectangle
) null);
1514 * Draws the link type events of this item
1519 * the container rectangle
1520 * @param timeProvider
1527 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1528 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1531 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1532 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1533 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1535 if ((srcIndex
== -1) || (destIndex
== -1)) {
1539 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1540 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1542 int x0
= getXForTime(event
.getTime());
1543 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1545 // limit the x-coordinates to prevent integer overflow in calculations
1546 // and also GC.drawLine doesn't draw properly with large coordinates
1547 final int limit
= Integer
.MAX_VALUE
/ 1024;
1548 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1549 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1551 int y0
= src
.y
+ src
.height
/ 2;
1552 int y1
= dst
.y
+ dst
.height
/ 2;
1553 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1557 * Draw the state (color fill)
1562 * Time event for which we're drawing the state
1567 * @return true if the state was drawn
1569 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1570 Rectangle rect
, GC gc
) {
1575 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1579 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1582 Color stateColor
= null;
1583 if (colorIdx
< fEventColorMap
.length
) {
1584 stateColor
= fEventColorMap
[colorIdx
];
1586 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1589 gc
.setForeground(stateColor
);
1590 gc
.setBackground(stateColor
);
1592 /* Draw the arrow */
1593 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1594 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1597 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1602 * @author Francis Giraldeau
1605 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1607 * The algorithm was taken from this site, not the code itself
1609 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1612 double cos
= 0.9510;
1613 double sin
= 0.3090;
1614 long lenx
= x1
- x0
;
1615 long leny
= y1
- y0
;
1616 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1618 double dx
= factor
* lenx
/ len
;
1619 double dy
= factor
* leny
/ len
;
1620 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1621 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1622 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1623 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1624 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1625 gc
.fillPolygon(arrow
);
1629 * Draw the name of an item.
1634 * Where to draw the name
1638 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1639 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1640 if (! hasTimeEvents
) {
1641 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1642 gc
.fillRectangle(bounds
);
1643 if (item
.fSelected
&& fIsInFocus
) {
1644 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1645 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1648 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1649 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1650 gc
.fillRectangle(bounds
);
1653 // No name to be drawn
1654 if (fTimeProvider
.getNameSpace() == 0) {
1658 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1659 if (item
.fHasChildren
) {
1660 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1661 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1662 Rectangle rect
= Utils
.clone(bounds
);
1663 rect
.x
+= leftMargin
;
1664 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1665 rect
.width
= EXPAND_SIZE
;
1666 rect
.height
= EXPAND_SIZE
;
1667 gc
.fillRectangle(rect
);
1668 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1669 int midy
= rect
.y
+ rect
.height
/ 2;
1670 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1671 if (!item
.fExpanded
) {
1672 int midx
= rect
.x
+ rect
.width
/ 2;
1673 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1676 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1678 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1681 int imgHeight
= img
.getImageData().height
;
1682 int imgWidth
= img
.getImageData().width
;
1684 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1685 gc
.drawImage(img
, x
, y
);
1686 leftMargin
+= imgWidth
+ MARGIN
;
1688 String name
= item
.fName
;
1689 Point size
= gc
.stringExtent(name
);
1690 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1691 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1693 if (hasTimeEvents
) {
1694 // cut long string with "..."
1695 int width
= bounds
.width
- leftMargin
;
1697 while (size
.x
> width
&& name
.length() > 1) {
1699 name
= name
.substring(0, name
.length() - 1);
1700 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1703 name
+= "..."; //$NON-NLS-1$
1706 Rectangle rect
= Utils
.clone(bounds
);
1707 rect
.x
+= leftMargin
;
1708 rect
.width
-= leftMargin
;
1710 if (rect
.width
> 0) {
1711 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1712 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1713 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1714 leftMargin
+= textWidth
+ MARGIN
;
1717 if (hasTimeEvents
) {
1719 int x
= bounds
.x
+ leftMargin
;
1720 int width
= bounds
.width
- x
;
1721 int midy
= bounds
.y
+ bounds
.height
/ 2;
1722 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1723 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1729 * Draw the state (color fill)
1734 * Time event for which we're drawing the state
1740 * Is this time event currently selected (so it appears
1742 * @param timeSelected
1743 * Is the timestamp currently selected
1744 * @return true if the state was drawn
1746 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1747 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1749 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1750 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1753 boolean visible
= rect
.width
== 0 ?
false : true;
1754 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1755 gc
.setForeground(black
);
1758 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1759 // Only draw the top and bottom borders
1760 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1761 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1762 if (rect
.width
== 1) {
1763 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1765 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1768 Color stateColor
= null;
1769 if (colorIdx
< fEventColorMap
.length
) {
1770 stateColor
= fEventColorMap
[colorIdx
];
1775 boolean reallySelected
= timeSelected
&& selected
;
1776 // fill all rect area
1777 gc
.setBackground(stateColor
);
1778 gc
.fillRectangle(rect
);
1780 if (reallySelected
) {
1781 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1782 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1785 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1787 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1792 * Fill the space between two contiguous time events
1799 * Is this time event selected or not
1801 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1802 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1803 gc
.fillRectangle(rect
);
1804 if (fDragState
== DRAG_ZOOM
) {
1805 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1806 if (fDragX0
< fDragX
) {
1807 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1808 } else if (fDragX0
> fDragX
) {
1809 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1813 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1814 int midy
= rect
.y
+ rect
.height
/ 2;
1815 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1819 public void keyTraversed(TraverseEvent e
) {
1820 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1826 public void keyPressed(KeyEvent e
) {
1828 if (fItemData
.fExpandedItems
.length
== 0) {
1831 if (SWT
.HOME
== e
.keyCode
) {
1833 } else if (SWT
.END
== e
.keyCode
) {
1834 idx
= fItemData
.fExpandedItems
.length
- 1;
1835 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1836 idx
= getSelectedIndex();
1839 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1842 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1843 idx
= getSelectedIndex();
1846 } else if (idx
> 0) {
1849 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
1850 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1851 selectPrevEvent(extend
);
1852 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
1853 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
1854 selectNextEvent(extend
);
1855 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1856 int page
= countPerPage();
1857 idx
= getSelectedIndex();
1862 if (idx
>= fItemData
.fExpandedItems
.length
) {
1863 idx
= fItemData
.fExpandedItems
.length
- 1;
1865 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1866 int page
= countPerPage();
1867 idx
= getSelectedIndex();
1875 } else if (SWT
.CR
== e
.keyCode
) {
1876 idx
= getSelectedIndex();
1878 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1881 fireDefaultSelection();
1887 selectItem(idx
, false);
1888 fireSelectionChanged();
1890 int x
= toControl(e
.display
.getCursorLocation()).x
;
1891 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1895 public void keyReleased(KeyEvent e
) {
1896 int x
= toControl(e
.display
.getCursorLocation()).x
;
1897 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1901 public void focusGained(FocusEvent e
) {
1904 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1908 public void focusLost(FocusEvent e
) {
1910 if (DRAG_NONE
!= fDragState
) {
1912 fDragState
= DRAG_NONE
;
1915 updateStatusLine(NO_STATUS
);
1919 * @return If the current view is focused
1921 public boolean isInFocus() {
1926 * Provide the possibility to control the wait cursor externally e.g. data
1927 * requests in progress
1929 * @param waitInd Should we wait indefinitely?
1931 public void waitCursor(boolean waitInd
) {
1932 // Update cursor as indicated
1934 setCursor(fWaitCursor
);
1940 private void updateCursor(int x
, int stateMask
) {
1941 // if Wait cursor not active, check for the need to change the cursor
1942 if (getCursor() == fWaitCursor
) {
1945 Cursor cursor
= null;
1946 if (fDragState
== DRAG_SPLIT_LINE
) {
1947 } else if (fDragState
== DRAG_SELECTION
) {
1948 cursor
= fResizeCursor
;
1949 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1950 cursor
= fDragCursor
;
1951 } else if (fDragState
== DRAG_ZOOM
) {
1952 cursor
= fZoomCursor
;
1953 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1954 cursor
= fDragCursor
;
1955 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1956 cursor
= fResizeCursor
;
1957 } else if (!isOverSplitLine(x
)) {
1958 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1959 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1960 int xBegin
= getXForTime(selectionBegin
);
1961 int xEnd
= getXForTime(selectionEnd
);
1962 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1963 cursor
= fResizeCursor
;
1966 if (getCursor() != cursor
) {
1971 private void updateStatusLine(int x
) {
1972 // use the time provider of the time graph scale for the status line
1973 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
1974 if (fStatusLineManager
== null || null == tdp
||
1975 tdp
.getTime0() == tdp
.getTime1()) {
1978 TimeFormat tf
= tdp
.getTimeFormat();
1979 Resolution res
= Resolution
.NANOSEC
;
1980 StringBuilder message
= new StringBuilder();
1981 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
1982 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
1983 long time
= getTimeAtX(x
);
1985 if (tdp
instanceof ITimeDataProviderConverter
) {
1986 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
1988 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
1990 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
1991 Utils
.formatTime(time
, tf
, res
)
1995 long selectionBegin
= tdp
.getSelectionBegin();
1996 long selectionEnd
= tdp
.getSelectionEnd();
1997 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
1999 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2000 Utils
.formatTime(selectionBegin
, tf
, res
)
2002 if (selectionBegin
!= selectionEnd
) {
2003 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2005 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2006 Utils
.formatTime(selectionEnd
, tf
, res
),
2007 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2010 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2011 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2012 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2013 if (tdp
instanceof ITimeDataProviderConverter
) {
2014 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2015 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2017 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2019 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2020 Utils
.formatTime(time0
, tf
, res
),
2021 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2022 Utils
.formatTime(time
, tf
, res
),
2023 Utils
.formatDelta(time
- time0
, tf
, res
)
2026 fStatusLineManager
.setMessage(message
.toString());
2030 public void mouseMove(MouseEvent e
) {
2031 if (null == fTimeProvider
) {
2034 Point size
= getSize();
2035 if (DRAG_TRACE_ITEM
== fDragState
) {
2036 int nameWidth
= fTimeProvider
.getNameSpace();
2037 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2039 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2040 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2041 long time1
= fTime1bak
- timeDelta
;
2042 long maxTime
= fTimeProvider
.getMaxTime();
2043 if (time1
> maxTime
) {
2046 long time0
= time1
- (fTime1bak
- fTime0bak
);
2047 if (time0
< fTimeProvider
.getMinTime()) {
2048 time0
= fTimeProvider
.getMinTime();
2049 time1
= time0
+ (fTime1bak
- fTime0bak
);
2051 fTimeProvider
.setStartFinishTime(time0
, time1
);
2053 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2055 fTimeProvider
.setNameSpace(e
.x
);
2056 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2057 } else if (DRAG_SELECTION
== fDragState
) {
2058 if (fDragBeginMarker
) {
2059 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2061 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2064 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2065 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2066 } else if (DRAG_ZOOM
== fDragState
) {
2067 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2069 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2070 } else if (DRAG_NONE
== fDragState
) {
2071 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2072 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2075 fMouseOverSplitLine
= mouseOverSplitLine
;
2077 updateCursor(e
.x
, e
.stateMask
);
2078 updateStatusLine(e
.x
);
2082 public void mouseDoubleClick(MouseEvent e
) {
2083 if (null == fTimeProvider
) {
2086 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2087 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2088 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2089 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2090 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2093 fMouseOverSplitLine
= mouseOverSplitLine
;
2094 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2097 int idx
= getItemIndexAtY(e
.y
);
2099 selectItem(idx
, false);
2100 fireDefaultSelection();
2106 public void mouseDown(MouseEvent e
) {
2107 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2108 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2109 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2113 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2114 int nameSpace
= fTimeProvider
.getNameSpace();
2115 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2116 fDragState
= DRAG_SPLIT_LINE
;
2117 fDragButton
= e
.button
;
2120 fTime0bak
= fTimeProvider
.getTime0();
2121 fTime1bak
= fTimeProvider
.getTime1();
2123 updateCursor(e
.x
, e
.stateMask
);
2127 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2128 int nameSpace
= fTimeProvider
.getNameSpace();
2129 idx
= getItemIndexAtY(e
.y
);
2131 Item item
= fItemData
.fExpandedItems
[idx
];
2132 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2136 selectItem(idx
, false);
2137 fireSelectionChanged();
2139 selectItem(idx
, false); // clear selection
2140 fireSelectionChanged();
2142 long hitTime
= getTimeAtX(e
.x
);
2146 fDragState
= DRAG_SELECTION
;
2147 fDragBeginMarker
= false;
2148 fDragButton
= e
.button
;
2151 fDragTime0
= getTimeAtX(fDragX0
);
2152 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2153 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2154 int xBegin
= getXForTime(selectionBegin
);
2155 int xEnd
= getXForTime(selectionEnd
);
2156 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2157 long time
= getTimeAtX(e
.x
);
2158 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2159 fDragBeginMarker
= true;
2162 fDragTime0
= selectionEnd
;
2165 fDragTime0
= selectionBegin
;
2168 long time
= getTimeAtX(e
.x
);
2169 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2170 fDragBeginMarker
= true;
2173 fDragTime0
= selectionEnd
;
2174 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2176 fDragTime0
= selectionBegin
;
2179 fTime0bak
= fTimeProvider
.getTime0();
2180 fTime1bak
= fTimeProvider
.getTime1();
2182 updateCursor(e
.x
, e
.stateMask
);
2183 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2185 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2186 long hitTime
= getTimeAtX(e
.x
);
2189 fDragState
= DRAG_TRACE_ITEM
;
2190 fDragButton
= e
.button
;
2193 fTime0bak
= fTimeProvider
.getTime0();
2194 fTime1bak
= fTimeProvider
.getTime1();
2195 updateCursor(e
.x
, e
.stateMask
);
2197 } else if (3 == e
.button
) {
2199 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2201 fDragTime0
= getTimeAtX(fDragX0
);
2202 fDragState
= DRAG_ZOOM
;
2203 fDragButton
= e
.button
;
2205 updateCursor(e
.x
, e
.stateMask
);
2206 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2211 public void mouseUp(MouseEvent e
) {
2212 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2213 menuDetected(fPendingMenuDetectEvent
);
2215 if (DRAG_NONE
!= fDragState
) {
2217 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2218 if (fDragX
!= fDragX0
) {
2219 fTimeProvider
.notifyStartFinishTime();
2221 fDragState
= DRAG_NONE
;
2222 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2223 fDragState
= DRAG_NONE
;
2225 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2226 if (fDragX
== fDragX0
) { // click without selecting anything
2227 long time
= getTimeAtX(e
.x
);
2228 fTimeProvider
.setSelectedTimeNotify(time
, false);
2230 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2231 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2232 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2234 fDragState
= DRAG_NONE
;
2236 fTimeGraphScale
.setDragRange(-1, -1);
2237 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2238 int nameWidth
= fTimeProvider
.getNameSpace();
2239 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2240 long time0
= getTimeAtX(fDragX0
);
2241 long time1
= getTimeAtX(fDragX
);
2242 if (time0
< time1
) {
2243 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2245 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2250 fDragState
= DRAG_NONE
;
2251 fTimeGraphScale
.setDragRange(-1, -1);
2254 updateCursor(e
.x
, e
.stateMask
);
2255 updateStatusLine(e
.x
);
2259 public void mouseEnter(MouseEvent e
) {
2263 public void mouseExit(MouseEvent e
) {
2264 if (fMouseOverSplitLine
) {
2265 fMouseOverSplitLine
= false;
2268 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2272 public void mouseHover(MouseEvent e
) {
2276 public void mouseScrolled(MouseEvent e
) {
2277 if (fDragState
!= DRAG_NONE
) {
2280 boolean zoomScroll
= false;
2281 boolean horizontalScroll
= false;
2282 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2283 Point parentSize
= getParent().getSize();
2284 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2285 // over the parent control
2286 if (e
.x
> getSize().x
) {
2287 // over the vertical scroll bar
2289 } else if (e
.y
< 0) {
2290 // over the time scale
2292 } else if (e
.y
>= getSize().y
) {
2293 // over the horizontal scroll bar
2294 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2297 horizontalScroll
= true;
2300 if (e
.x
< fTimeProvider
.getNameSpace()) {
2301 // over the name space
2304 // over the state area
2305 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2306 // over the state area, CTRL pressed
2309 // over the state area, CTRL not pressed
2315 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2318 } else if (e
.count
< 0) {
2321 } else if (horizontalScroll
) {
2322 horizontalScroll(e
.count
> 0);
2324 setTopIndex(getTopIndex() - e
.count
);
2329 public void handleEvent(Event event
) {
2330 if (event
.type
== SWT
.MouseWheel
) {
2331 // prevent horizontal scrolling when the mouse wheel is used to
2332 // scroll vertically or zoom
2338 public int getBorderWidth() {
2339 return fBorderWidth
;
2343 * Set the border width
2345 * @param borderWidth
2348 public void setBorderWidth(int borderWidth
) {
2349 this.fBorderWidth
= borderWidth
;
2353 * @return The current height of the header row
2355 public int getHeaderHeight() {
2356 return fHeaderHeight
;
2360 * Set the height of the header row
2362 * @param headerHeight
2365 public void setHeaderHeight(int headerHeight
) {
2366 this.fHeaderHeight
= headerHeight
;
2370 * @return The default height of regular item rows
2372 public int getItemHeight() {
2373 return fGlobalItemHeight
;
2377 * Set the default height of regular item rows.
2382 public void setItemHeight(int rowHeight
) {
2383 this.fGlobalItemHeight
= rowHeight
;
2387 * Set the height of a specific item. Overrides the default item height.
2390 * A time graph entry
2393 * @return true if the height is successfully stored, false otherwise
2395 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2396 Item item
= fItemData
.findItem(entry
);
2398 item
.fItemHeight
= rowHeight
;
2405 * Set the minimum item width
2407 * @param width The minimum width
2409 public void setMinimumItemWidth(int width
) {
2410 this.fMinimumItemWidth
= width
;
2414 * @return The minimum item width
2416 public int getMinimumItemWidth() {
2417 return fMinimumItemWidth
;
2421 * @return The entries that are currently filtered out
2423 public List
<ITimeGraphEntry
> getFilteredOut() {
2424 return fItemData
.getFilteredOut();
2428 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2429 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2430 fSelectionChangedListeners
.add(listener
);
2435 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2436 if (listener
!= null) {
2437 fSelectionChangedListeners
.remove(listener
);
2442 public void setSelection(ISelection selection
) {
2443 if (selection
instanceof TimeGraphSelection
) {
2444 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2445 Object ob
= sel
.getFirstElement();
2446 if (ob
instanceof ITimeGraphEntry
) {
2447 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2448 selectItem(trace
, false);
2455 * @param filter The filter object to be attached to the view
2457 public void addFilter(ViewerFilter filter
) {
2458 if (!fFilters
.contains(filter
)) {
2459 fFilters
.add(filter
);
2464 * @param filter The filter object to be attached to the view
2466 public void removeFilter(ViewerFilter filter
) {
2467 fFilters
.remove(filter
);
2471 public void colorSettingsChanged(StateItem
[] stateItems
) {
2472 /* Destroy previous colors from the resource manager */
2473 if (fEventColorMap
!= null) {
2474 for (Color color
: fEventColorMap
) {
2475 fResourceManager
.destroyColor(color
.getRGB());
2478 if (stateItems
!= null) {
2479 fEventColorMap
= new Color
[stateItems
.length
];
2480 for (int i
= 0; i
< stateItems
.length
; i
++) {
2481 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2484 fEventColorMap
= new Color
[] { };
2489 private class ItemData
{
2490 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2491 private Item
[] fExpandedItems
= new Item
[0];
2492 private Item
[] fItems
= new Item
[0];
2493 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2494 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2495 private boolean fEntryFilter
[] = new boolean[0];
2496 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2501 public Item
findItem(ITimeGraphEntry entry
) {
2502 return fItemMap
.get(entry
);
2505 public int findItemIndex(ITimeGraphEntry entry
) {
2506 Item item
= fItemMap
.get(entry
);
2510 return item
.fExpandedIndex
;
2513 public void refreshData() {
2515 fFilteredOut
.clear();
2516 ITimeGraphEntry selection
= getSelectedTrace();
2517 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2518 ITimeGraphEntry entry
= fRootEntries
[i
];
2519 refreshData(fItemMap
, null, 0, entry
);
2521 fItems
= fItemMap
.values().toArray(new Item
[0]);
2522 updateExpandedItems();
2523 if (selection
!= null) {
2524 for (Item item
: fExpandedItems
) {
2525 if (item
.fEntry
== selection
) {
2526 item
.fSelected
= true;
2533 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2534 Item item
= new Item(entry
, entry
.getName(), level
);
2535 if (parent
!= null) {
2536 parent
.fChildren
.add(item
);
2538 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2539 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2541 item
.fItemHeight
= fGlobalItemHeight
;
2543 itemMap
.put(entry
, item
);
2544 if (entry
.hasChildren()) {
2545 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2546 item
.fHasChildren
= true;
2547 for (ITimeGraphEntry child
: entry
.getChildren()) {
2548 refreshData(itemMap
, item
, level
+ 1, child
);
2553 public void updateExpandedItems() {
2554 for (Item item
: fItems
) {
2555 item
.fExpandedIndex
= -1;
2557 List
<Item
> expandedItemList
= new ArrayList
<>();
2558 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2559 ITimeGraphEntry entry
= fRootEntries
[i
];
2560 Item item
= findItem(entry
);
2561 refreshExpanded(expandedItemList
, item
);
2563 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2564 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2567 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2568 // Check for filters
2569 boolean display
= true;
2570 for (ViewerFilter filter
: fFilters
) {
2571 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2577 item
.fExpandedIndex
= expandedItemList
.size();
2578 expandedItemList
.add(item
);
2579 if (item
.fHasChildren
&& item
.fExpanded
) {
2580 for (Item child
: item
.fChildren
) {
2581 refreshExpanded(expandedItemList
, child
);
2587 public void refreshData(ITimeGraphEntry
[] entries
) {
2588 if (entries
== null) {
2589 fEntryFilter
= null;
2590 fRootEntries
= null;
2592 if (entries
.length
== 0) {
2593 fEntryFilter
= null;
2594 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2595 fEntryFilter
= new boolean[entries
.length
];
2596 java
.util
.Arrays
.fill(fEntryFilter
, true);
2598 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2604 public void refreshArrows(List
<ILinkEvent
> events
) {
2605 /* If links are null, reset the list */
2606 if (events
!= null) {
2609 fLinks
= new ArrayList
<>();
2613 public ITimeGraphEntry
[] getEntries() {
2614 return fRootEntries
;
2617 public boolean[] getEntryFilter() {
2618 return fEntryFilter
;
2621 public List
<ITimeGraphEntry
> getFilteredOut() {
2622 return fFilteredOut
;
2626 private class Item
{
2627 private boolean fExpanded
;
2628 private int fExpandedIndex
;
2629 private boolean fSelected
;
2630 private boolean fHasChildren
;
2631 private int fItemHeight
;
2632 private final int fLevel
;
2633 private final List
<Item
> fChildren
;
2634 private final String fName
;
2635 private final ITimeGraphEntry fEntry
;
2637 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2638 this.fEntry
= entry
;
2640 this.fLevel
= level
;
2641 this.fChildren
= new ArrayList
<>();
2645 public String
toString() {
2651 public void menuDetected(MenuDetectEvent e
) {
2652 if (null == fTimeProvider
) {
2655 if (e
.detail
== SWT
.MENU_MOUSE
) {
2656 if (fPendingMenuDetectEvent
== null) {
2657 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2658 * Store the event and trigger it later just before handling mouseUp.
2659 * This allows for the method to detect if mouse is used to drag zoom.
2661 fPendingMenuDetectEvent
= e
;
2664 fPendingMenuDetectEvent
= null;
2665 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2669 if (fDragState
!= DRAG_NONE
) {
2673 Point p
= toControl(e
.x
, e
.y
);
2674 int idx
= getItemIndexAtY(p
.y
);
2675 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2676 Item item
= fItemData
.fExpandedItems
[idx
];
2677 ITimeGraphEntry entry
= item
.fEntry
;
2678 if (entry
.hasTimeEvents()) {
2679 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2680 if (event
!= null) {
2682 fireMenuEventOnTimeEvent(e
);
2687 fireMenuEventOnTimeGraphEntry(e
);
2692 * Perform the alignment operation.
2695 * the alignment offset
2697 * @see ITmfTimeAligned
2701 public void performAlign(int offset
) {
2702 fTimeProvider
.setNameSpace(offset
);
2706 * Return the time alignment information
2708 * @return the time alignment information
2710 * @see ITmfTimeAligned
2714 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
2715 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());