1 /*****************************************************************************
2 * Copyright (c) 2007, 2014 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 *****************************************************************************/
20 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Arrays
;
24 import java
.util
.Iterator
;
25 import java
.util
.List
;
27 import org
.eclipse
.jface
.action
.IStatusLineManager
;
28 import org
.eclipse
.jface
.resource
.JFaceResources
;
29 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
30 import org
.eclipse
.jface
.viewers
.ISelection
;
31 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
32 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
33 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
46 import org
.eclipse
.swt
.SWT
;
47 import org
.eclipse
.swt
.events
.ControlEvent
;
48 import org
.eclipse
.swt
.events
.ControlListener
;
49 import org
.eclipse
.swt
.events
.FocusEvent
;
50 import org
.eclipse
.swt
.events
.FocusListener
;
51 import org
.eclipse
.swt
.events
.KeyEvent
;
52 import org
.eclipse
.swt
.events
.KeyListener
;
53 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
54 import org
.eclipse
.swt
.events
.MenuDetectListener
;
55 import org
.eclipse
.swt
.events
.MouseEvent
;
56 import org
.eclipse
.swt
.events
.MouseListener
;
57 import org
.eclipse
.swt
.events
.MouseMoveListener
;
58 import org
.eclipse
.swt
.events
.MouseTrackListener
;
59 import org
.eclipse
.swt
.events
.MouseWheelListener
;
60 import org
.eclipse
.swt
.events
.PaintEvent
;
61 import org
.eclipse
.swt
.events
.SelectionEvent
;
62 import org
.eclipse
.swt
.events
.SelectionListener
;
63 import org
.eclipse
.swt
.events
.TraverseEvent
;
64 import org
.eclipse
.swt
.events
.TraverseListener
;
65 import org
.eclipse
.swt
.events
.TypedEvent
;
66 import org
.eclipse
.swt
.graphics
.Color
;
67 import org
.eclipse
.swt
.graphics
.Cursor
;
68 import org
.eclipse
.swt
.graphics
.GC
;
69 import org
.eclipse
.swt
.graphics
.Image
;
70 import org
.eclipse
.swt
.graphics
.Point
;
71 import org
.eclipse
.swt
.graphics
.Rectangle
;
72 import org
.eclipse
.swt
.widgets
.Composite
;
73 import org
.eclipse
.swt
.widgets
.Display
;
74 import org
.eclipse
.swt
.widgets
.Event
;
75 import org
.eclipse
.swt
.widgets
.Listener
;
76 import org
.eclipse
.swt
.widgets
.ScrollBar
;
79 * Time graph control implementation
82 * @author Alvaro Sanchez-Leon
83 * @author Patrick Tasse
85 public class TimeGraphControl
extends TimeGraphBaseControl
86 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
,
87 ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
88 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
{
90 /** Max scrollbar size */
91 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
93 private static final int DRAG_NONE
= 0;
94 private static final int DRAG_TRACE_ITEM
= 1;
95 private static final int DRAG_SPLIT_LINE
= 2;
96 private static final int DRAG_ZOOM
= 3;
97 private static final int DRAG_SELECTION
= 4;
99 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
101 private static final double ZOOM_FACTOR
= 1.5;
102 private static final double ZOOM_IN_FACTOR
= 0.8;
103 private static final double ZOOM_OUT_FACTOR
= 1.25;
105 private static final int SNAP_WIDTH
= 2;
107 private static final int NO_STATUS
= -1;
109 /** Resource manager */
110 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
112 /** Color map for event types */
113 private Color
[] fEventColorMap
= null;
115 private ITimeDataProvider fTimeProvider
;
116 private IStatusLineManager fStatusLineManager
= null;
117 private TimeGraphScale fTimeGraphScale
= null;
119 private boolean fIsInFocus
= false;
120 private boolean fMouseOverSplitLine
= false;
121 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
122 private int fMinimumItemWidth
= 0;
123 private int fTopIndex
= 0;
124 private int fDragState
= DRAG_NONE
;
125 private int fDragButton
;
126 private int fDragX0
= 0;
127 private int fDragX
= 0;
128 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
129 private int fIdealNameSpace
= 0;
130 private long fTime0bak
;
131 private long fTime1bak
;
132 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
133 private ItemData fItemData
= null;
134 private List
<SelectionListener
> fSelectionListeners
;
135 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
136 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
137 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
138 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
139 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
140 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
141 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
142 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
143 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
144 private MenuDetectEvent fPendingMenuDetectEvent
= null;
145 private boolean fHideArrows
= false;
147 private int fBorderWidth
= 0;
148 private int fHeaderHeight
= 0;
150 private Listener fMouseScrollFilterListener
;
152 private MouseScrollNotifier fMouseScrollNotifier
;
153 private final Object fMouseScrollNotifierLock
= new Object();
155 private class MouseScrollNotifier
extends Thread
{
156 private static final long DELAY
= 400L;
157 private static final long POLLING_INTERVAL
= 10L;
158 private long fLastScrollTime
= Long
.MAX_VALUE
;
162 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
164 Thread
.sleep(POLLING_INTERVAL
);
165 } catch (Exception e
) {
169 if (!isInterrupted()) {
170 Display
.getDefault().asyncExec(new Runnable() {
176 fTimeProvider
.notifyStartFinishTime();
180 synchronized (fMouseScrollNotifierLock
) {
181 fMouseScrollNotifier
= null;
185 public void mouseScrolled() {
186 fLastScrollTime
= System
.currentTimeMillis();
191 * Standard constructor
194 * The parent composite object
196 * The color scheme to use
198 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
200 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
202 fItemData
= new ItemData();
204 addFocusListener(this);
205 addMouseListener(this);
206 addMouseMoveListener(this);
207 addMouseTrackListener(this);
208 addMouseWheelListener(this);
209 addTraverseListener(this);
210 addKeyListener(this);
211 addControlListener(this);
212 addMenuDetectListener(this);
213 ScrollBar scrollHor
= getHorizontalBar();
215 if (scrollHor
!= null) {
216 scrollHor
.addSelectionListener(this);
221 public void dispose() {
223 fResourceManager
.dispose();
227 * Sets the timegraph provider used by this timegraph viewer.
229 * @param timeGraphProvider the timegraph provider
231 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
232 fTimeGraphProvider
= timeGraphProvider
;
234 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
235 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
236 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
239 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
240 colorSettingsChanged(stateItems
);
244 * Gets the timegraph provider used by this timegraph viewer.
246 * @return the timegraph provider, or <code>null</code> if not set.
249 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
250 return fTimeGraphProvider
;
254 * Gets the color map used by this timegraph viewer.
256 * @return a color map, or <code>null</code> if not set.
259 public Color
[] getEventColorMap() {
260 return fEventColorMap
;
264 * Assign the given time provider
266 * @param timeProvider
269 public void setTimeProvider(ITimeDataProvider timeProvider
) {
270 fTimeProvider
= timeProvider
;
276 * Assign the status line manager
278 * @param statusLineManager
279 * The status line manager, or null to disable status line messages
282 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
283 if (fStatusLineManager
!= null && statusLineManager
== null) {
284 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
286 fStatusLineManager
= statusLineManager
;
290 * Assign the time graph scale
292 * @param timeGraphScale
293 * The time graph scale
296 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
297 fTimeGraphScale
= timeGraphScale
;
301 * Add a selection listener
304 * The listener to add
306 public void addSelectionListener(SelectionListener listener
) {
307 if (listener
== null) {
308 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
310 if (null == fSelectionListeners
) {
311 fSelectionListeners
= new ArrayList
<>();
313 fSelectionListeners
.add(listener
);
317 * Remove a selection listener
320 * The listener to remove
322 public void removeSelectionListener(SelectionListener listener
) {
323 if (null != fSelectionListeners
) {
324 fSelectionListeners
.remove(listener
);
329 * Selection changed callback
331 public void fireSelectionChanged() {
332 if (null != fSelectionListeners
) {
333 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
334 while (it
.hasNext()) {
335 SelectionListener listener
= it
.next();
336 listener
.widgetSelected(null);
342 * Default selection callback
344 public void fireDefaultSelection() {
345 if (null != fSelectionListeners
) {
346 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
347 while (it
.hasNext()) {
348 SelectionListener listener
= it
.next();
349 listener
.widgetDefaultSelected(null);
355 * Get the traces in the model
357 * @return The array of traces
359 public ITimeGraphEntry
[] getTraces() {
360 return fItemData
.getTraces();
364 * Get the on/off trace filters
366 * @return The array of filters
368 public boolean[] getTraceFilter() {
369 return fItemData
.getTraceFilter();
373 * Refresh the data for the thing
375 public void refreshData() {
376 fItemData
.refreshData();
382 * Refresh data for the given traces
385 * The traces to refresh
387 public void refreshData(ITimeGraphEntry
[] traces
) {
388 fItemData
.refreshData(traces
);
394 * Refresh the links (arrows) of this widget
396 * @param events The link events to refresh
399 public void refreshArrows(List
<ILinkEvent
> events
) {
400 fItemData
.refreshArrows(events
);
404 * Adjust the scoll bars
406 public void adjustScrolls() {
407 if (null == fTimeProvider
) {
408 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
414 long time0
= fTimeProvider
.getTime0();
415 long time1
= fTimeProvider
.getTime1();
417 long timeMin
= fTimeProvider
.getMinTime();
418 long timeMax
= fTimeProvider
.getMaxTime();
420 long delta
= timeMax
- timeMin
;
423 int thumb
= H_SCROLLBAR_MAX
;
426 // Thumb size (page size)
427 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
428 // At the beginning of visible window
429 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
432 // position, minimum, maximum, thumb size, increment (half page)t, page
433 // increment size (full page)
434 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
437 boolean ensureVisibleItem(int idx
, boolean redraw
) {
438 boolean changed
= false;
441 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
442 if (fItemData
.fExpandedItems
[index
].fSelected
) {
447 if (index
>= fItemData
.fExpandedItems
.length
) {
450 if (index
< fTopIndex
) {
457 int page
= countPerPage();
458 if (index
>= fTopIndex
+ page
) {
459 setTopIndex(index
- page
+ 1);
470 * Assign the given index as the top one
475 public void setTopIndex(int idx
) {
476 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
477 index
= Math
.max(0, index
);
483 * Set the expanded state of a given entry
488 * True if expanded, false if collapsed
490 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
491 Item item
= fItemData
.findItem(entry
);
492 if (item
!= null && item
.fExpanded
!= expanded
) {
493 item
.fExpanded
= expanded
;
494 fItemData
.updateExpandedItems();
500 * Collapses all nodes of the viewer's tree, starting with the root.
504 public void collapseAll() {
505 for (Item item
: fItemData
.fItems
) {
506 item
.fExpanded
= false;
508 fItemData
.updateExpandedItems();
513 * Expands all nodes of the viewer's tree, starting with the root.
517 public void expandAll() {
518 for (Item item
: fItemData
.fItems
) {
519 item
.fExpanded
= true;
521 fItemData
.updateExpandedItems();
526 * Add a tree listener
529 * The listener to add
531 public void addTreeListener(ITimeGraphTreeListener listener
) {
532 if (!fTreeListeners
.contains(listener
)) {
533 fTreeListeners
.add(listener
);
538 * Remove a tree listener
541 * The listener to remove
543 public void removeTreeListener(ITimeGraphTreeListener listener
) {
544 if (fTreeListeners
.contains(listener
)) {
545 fTreeListeners
.remove(listener
);
550 * Tree event callback
555 * The expanded state (true for expanded, false for collapsed)
557 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
558 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
559 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
561 listener
.treeExpanded(event
);
563 listener
.treeCollapsed(event
);
569 * Add a menu listener on {@link ITimeGraphEntry}s
571 * The listener to add
574 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
575 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
576 fTimeGraphEntryMenuListeners
.add(listener
);
581 * Remove a menu listener on {@link ITimeGraphEntry}s
584 * The listener to remove
587 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
588 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
589 fTimeGraphEntryMenuListeners
.remove(listener
);
594 * Menu event callback on {@link ITimeGraphEntry}s
597 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
599 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
600 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
601 listener
.menuDetected(event
);
606 * Add a menu listener on {@link ITimeEvent}s
609 * The listener to add
612 public void addTimeEventMenuListener(MenuDetectListener listener
) {
613 if (!fTimeEventMenuListeners
.contains(listener
)) {
614 fTimeEventMenuListeners
.add(listener
);
619 * Remove a menu listener on {@link ITimeEvent}s
622 * The listener to remove
625 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
626 if (fTimeEventMenuListeners
.contains(listener
)) {
627 fTimeEventMenuListeners
.remove(listener
);
632 * Menu event callback on {@link ITimeEvent}s
635 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
637 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
638 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
639 listener
.menuDetected(event
);
644 public ISelection
getSelection() {
645 TimeGraphSelection sel
= new TimeGraphSelection();
646 ITimeGraphEntry trace
= getSelectedTrace();
647 if (null != trace
&& null != fTimeProvider
) {
648 long selectedTime
= fTimeProvider
.getSelectionBegin();
649 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
660 * Get the selection object
662 * @return The selection
664 public ISelection
getSelectionTrace() {
665 TimeGraphSelection sel
= new TimeGraphSelection();
666 ITimeGraphEntry trace
= getSelectedTrace();
674 * Enable/disable one of the traces in the model
677 * 1 to enable it, -1 to disable. The method returns immediately
678 * if another value is used.
680 public void selectTrace(int n
) {
681 if ((n
!= 1) && (n
!= -1)) {
685 boolean changed
= false;
686 int lastSelection
= -1;
687 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
688 Item item
= fItemData
.fExpandedItems
[i
];
689 if (item
.fSelected
) {
691 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
692 item
.fSelected
= false;
693 item
= fItemData
.fExpandedItems
[i
+ 1];
694 item
.fSelected
= true;
696 } else if ((-1 == n
) && (i
> 0)) {
697 item
.fSelected
= false;
698 item
= fItemData
.fExpandedItems
[i
- 1];
699 item
.fSelected
= true;
706 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
707 Item item
= fItemData
.fExpandedItems
[0];
708 item
.fSelected
= true;
713 ensureVisibleItem(-1, false);
715 fireSelectionChanged();
723 * 1 for next event, -1 for previous event
725 public void selectEvent(int n
) {
726 if (null == fTimeProvider
) {
729 ITimeGraphEntry trace
= getSelectedTrace();
733 long selectedTime
= fTimeProvider
.getSelectionBegin();
734 long endTime
= fTimeProvider
.getEndTime();
735 ITimeEvent nextEvent
;
736 if (-1 == n
&& selectedTime
> endTime
) {
737 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
739 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
741 if (null == nextEvent
&& -1 == n
) {
742 nextEvent
= Utils
.getFirstEvent(trace
);
744 if (null != nextEvent
) {
745 long nextTime
= nextEvent
.getTime();
746 // If last event detected e.g. going back or not moving to a next
748 if (nextTime
<= selectedTime
&& n
== 1) {
749 // Select to the end of this last event
750 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
751 // but not beyond the end of the trace
752 if (nextTime
> endTime
) {
755 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
756 // for previous event go to its end time unless we were already there
757 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
759 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
760 fireSelectionChanged();
762 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
763 fireSelectionChanged();
768 * Select the next event
770 public void selectNextEvent() {
772 // Notify if visible time window has been adjusted
773 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
777 * Select the previous event
779 public void selectPrevEvent() {
781 // Notify if visible time window has been adjusted
782 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
786 * Select the next trace
788 public void selectNextTrace() {
793 * Select the previous trace
795 public void selectPrevTrace() {
800 * Zoom based on mouse cursor location with mouse scrolling
802 * @param zoomIn true to zoom in, false to zoom out
804 public void zoom(boolean zoomIn
) {
805 int globalX
= getDisplay().getCursorLocation().x
;
806 Point p
= toControl(globalX
, 0);
807 int nameSpace
= fTimeProvider
.getNameSpace();
808 int timeSpace
= fTimeProvider
.getTimeSpace();
809 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
810 long time0
= fTimeProvider
.getTime0();
811 long time1
= fTimeProvider
.getTime1();
812 long interval
= time1
- time0
;
815 } // to allow getting out of single point interval
818 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
820 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
822 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
823 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
824 long newTime1
= newTime0
+ newInterval
;
825 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
826 synchronized (fMouseScrollNotifierLock
) {
827 if (fMouseScrollNotifier
== null) {
828 fMouseScrollNotifier
= new MouseScrollNotifier();
829 fMouseScrollNotifier
.start();
831 fMouseScrollNotifier
.mouseScrolled();
836 * zoom in using single click
838 public void zoomIn() {
839 long prevTime0
= fTimeProvider
.getTime0();
840 long prevTime1
= fTimeProvider
.getTime1();
841 long prevRange
= prevTime1
- prevTime0
;
842 if (prevRange
== 0) {
845 ITimeDataProvider provider
= fTimeProvider
;
846 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
847 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
848 selTime
= (prevTime0
+ prevTime1
) / 2;
850 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
851 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
853 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
855 if (inaccuracy
> 0 && inaccuracy
< 100) {
856 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
860 long min
= fTimeProvider
.getMinTimeInterval();
861 if ((time1
- time0
) < min
) {
862 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
866 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
870 * zoom out using single click
872 public void zoomOut() {
873 long prevTime0
= fTimeProvider
.getTime0();
874 long prevTime1
= fTimeProvider
.getTime1();
875 ITimeDataProvider provider
= fTimeProvider
;
876 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
877 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
878 selTime
= (prevTime0
+ prevTime1
) / 2;
880 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
881 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
883 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
884 if (inaccuracy
> 0 && inaccuracy
< 100) {
885 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
889 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
895 * @param hideArrows true to hide arrows
899 public void hideArrows(boolean hideArrows
) {
900 fHideArrows
= hideArrows
;
904 * Follow the arrow forward
908 public void followArrowFwd() {
909 ITimeGraphEntry trace
= getSelectedTrace();
913 long selectedTime
= fTimeProvider
.getSelectionBegin();
914 for (ILinkEvent link
: fItemData
.fLinks
) {
915 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
916 selectItem(link
.getDestinationEntry(), false);
917 if (link
.getDuration() != 0) {
918 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
919 // Notify if visible time window has been adjusted
920 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
922 fireSelectionChanged();
930 * Follow the arrow backward
934 public void followArrowBwd() {
935 ITimeGraphEntry trace
= getSelectedTrace();
939 long selectedTime
= fTimeProvider
.getSelectionBegin();
940 for (ILinkEvent link
: fItemData
.fLinks
) {
941 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
942 selectItem(link
.getEntry(), false);
943 if (link
.getDuration() != 0) {
944 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
945 // Notify if visible time window has been adjusted
946 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
948 fireSelectionChanged();
956 * Return the currently selected trace
958 * @return The entry matching the trace
960 public ITimeGraphEntry
getSelectedTrace() {
961 ITimeGraphEntry trace
= null;
962 int idx
= getSelectedIndex();
964 trace
= fItemData
.fExpandedItems
[idx
].fTrace
;
970 * Retrieve the index of the currently selected item
974 public int getSelectedIndex() {
976 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
977 Item item
= fItemData
.fExpandedItems
[i
];
978 if (item
.fSelected
) {
986 boolean toggle(int idx
) {
987 boolean toggled
= false;
988 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
989 Item item
= fItemData
.fExpandedItems
[idx
];
990 if (item
.fHasChildren
) {
991 item
.fExpanded
= !item
.fExpanded
;
992 fItemData
.updateExpandedItems();
996 fireTreeEvent(item
.fTrace
, item
.fExpanded
);
1002 int getItemIndexAtY(int y
) {
1007 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1008 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1016 boolean isOverSplitLine(int x
) {
1017 if (x
< 0 || null == fTimeProvider
) {
1020 int nameWidth
= fTimeProvider
.getNameSpace();
1021 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1024 ITimeGraphEntry
getEntry(Point pt
) {
1025 int idx
= getItemIndexAtY(pt
.y
);
1026 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fTrace
: null;
1033 public int getXForTime(long time
) {
1034 if (null == fTimeProvider
) {
1037 long time0
= fTimeProvider
.getTime0();
1038 long time1
= fTimeProvider
.getTime1();
1039 int width
= getCtrlSize().x
;
1040 int nameSpace
= fTimeProvider
.getNameSpace();
1041 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1042 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1050 public long getTimeAtX(int coord
) {
1051 if (null == fTimeProvider
) {
1055 Point size
= getCtrlSize();
1056 long time0
= fTimeProvider
.getTime0();
1057 long time1
= fTimeProvider
.getTime1();
1058 int nameWidth
= fTimeProvider
.getNameSpace();
1059 final int x
= coord
- nameWidth
;
1060 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1061 if (x
>= 0 && size
.x
>= nameWidth
) {
1062 if (time1
- time0
> timeWidth
) {
1063 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1064 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1066 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1067 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1073 void selectItem(int idx
, boolean addSelection
) {
1074 boolean changed
= false;
1076 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1077 Item item
= fItemData
.fExpandedItems
[idx
];
1078 changed
= !item
.fSelected
;
1079 item
.fSelected
= true;
1082 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1083 Item item
= fItemData
.fExpandedItems
[i
];
1084 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1087 item
.fSelected
= i
== idx
;
1090 changed
|= ensureVisibleItem(idx
, true);
1097 * Callback for item selection
1100 * The entry matching the trace
1101 * @param addSelection
1102 * If the selection is added or removed
1104 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1105 int idx
= fItemData
.findItemIndex(trace
);
1106 selectItem(idx
, addSelection
);
1110 * Retrieve the number of entries shown per page.
1114 public int countPerPage() {
1115 int height
= getCtrlSize().y
;
1118 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1119 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1120 if (ySum
>= height
) {
1125 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1126 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1127 if (ySum
>= height
) {
1136 * Get the index of the top element
1140 public int getTopIndex() {
1145 * Get the number of expanded items
1147 * @return The count of expanded items
1149 public int getExpandedElementCount() {
1150 return fItemData
.fExpandedItems
.length
;
1154 * Get an array of all expanded elements
1156 * @return The expanded elements
1158 public ITimeGraphEntry
[] getExpandedElements() {
1159 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1160 for (Item item
: fItemData
.fExpandedItems
) {
1161 elements
.add(item
.fTrace
);
1163 return elements
.toArray(new ITimeGraphEntry
[0]);
1166 Point
getCtrlSize() {
1167 Point size
= getSize();
1168 if (getHorizontalBar().isVisible()) {
1169 size
.y
-= getHorizontalBar().getSize().y
;
1174 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1175 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1177 rect
.width
= nameWidth
;
1181 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1182 int x
= bound
.x
+ nameWidth
;
1183 int width
= bound
.width
- x
;
1185 if (idx
>= fTopIndex
) {
1186 for (int i
= fTopIndex
; i
< idx
; i
++) {
1187 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1190 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1191 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1194 int y
= bound
.y
+ ySum
;
1195 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1196 return new Rectangle(x
, y
, width
, height
);
1200 void paint(Rectangle bounds
, PaintEvent e
) {
1202 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1203 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1205 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1209 fIdealNameSpace
= 0;
1210 int nameSpace
= fTimeProvider
.getNameSpace();
1212 // draw empty name space background
1213 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1214 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1217 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1218 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1219 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1221 int alpha
= gc
.getAlpha();
1224 long time0
= fTimeProvider
.getTime0();
1225 long time1
= fTimeProvider
.getTime1();
1226 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1227 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1228 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1229 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1230 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1232 // draw selection lines
1233 if (fDragState
!= DRAG_SELECTION
) {
1234 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1235 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1236 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1239 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1240 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1245 // draw selection background
1246 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1247 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1248 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1249 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1251 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1252 } else if (x0
- x1
> 1) {
1253 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1257 // draw drag selection background
1258 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1259 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1260 if (fDragX0
< fDragX
) {
1261 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1262 } else if (fDragX0
> fDragX
) {
1263 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1268 if (DRAG_SPLIT_LINE
== fDragState
) {
1269 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1270 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1271 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1272 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1273 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1274 if (fDragX
!= fDragX0
) {
1275 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1277 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1278 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1279 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1280 if (fDragX
!= fDragX0
) {
1281 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1283 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1284 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1285 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1292 * Draw many items at once
1295 * The rectangle of the area
1296 * @param timeProvider
1299 * The array items to draw
1301 * The index of the first element to draw
1303 * The width reserved for the names
1305 * Reference to the SWT GC object
1307 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1308 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1309 for (int i
= topIndex
; i
< items
.length
; i
++) {
1310 Item item
= items
[i
];
1311 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1318 * @param item the item to draw
1319 * @param bounds the container rectangle
1320 * @param timeProvider Time provider
1321 * @param i the item index
1322 * @param nameSpace the name space
1323 * @param gc Graphics context
1325 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1326 ITimeGraphEntry entry
= item
.fTrace
;
1327 long time0
= timeProvider
.getTime0();
1328 long time1
= timeProvider
.getTime1();
1329 long selectedTime
= fTimeProvider
.getSelectionBegin();
1331 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1332 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1336 if (! item
.fTrace
.hasTimeEvents()) {
1337 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1338 nameRect
.width
+= statesRect
.width
;
1339 drawName(item
, nameRect
, gc
);
1341 drawName(item
, nameRect
, gc
);
1343 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1344 if (rect
.isEmpty()) {
1345 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1348 if (time1
<= time0
) {
1349 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1350 gc
.fillRectangle(rect
);
1351 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1355 // Initialize _rect1 to same values as enclosing rectangle rect
1356 Rectangle stateRect
= Utils
.clone(rect
);
1357 boolean selected
= item
.fSelected
;
1358 // K pixels per second
1359 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1361 if (item
.fTrace
.hasTimeEvents()) {
1362 fillSpace(rect
, gc
, selected
);
1363 // Drawing rectangle is smaller than reserved space
1365 stateRect
.height
-= 6;
1367 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1368 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1371 while (iterator
.hasNext()) {
1372 ITimeEvent event
= iterator
.next();
1373 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1374 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1375 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1376 // event is out of bounds
1379 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1380 stateRect
.x
= Math
.max(rect
.x
, x
);
1381 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1382 if (stateRect
.x
== lastX
) {
1383 stateRect
.width
-= 1;
1384 if (stateRect
.width
> 0) {
1385 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1386 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1390 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1391 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1396 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1403 * The rectangle of the area
1404 * @param timeProvider
1407 * The array items to draw
1409 * The width reserved for the names
1411 * Reference to the SWT GC object
1414 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1415 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1419 for (ILinkEvent event
: links
) {
1420 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1425 * Draws the link type events of this item
1430 * the container rectangle
1431 * @param timeProvider
1439 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1440 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1441 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1443 if ((srcIndex
== -1) || (destIndex
== -1)) {
1447 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1448 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1450 int x0
= getXForTime(event
.getTime());
1451 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1453 // limit the x-coordinates to prevent integer overflow in calculations
1454 // and also GC.drawLine doesn't draw properly with large coordinates
1455 final int limit
= Integer
.MAX_VALUE
/ 1024;
1456 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1457 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1459 int y0
= src
.y
+ src
.height
/ 2;
1460 int y1
= dst
.y
+ dst
.height
/ 2;
1461 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1465 * Draw the state (color fill)
1470 * Time event for which we're drawing the state
1475 * @return true if the state was drawn
1478 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1479 Rectangle rect
, GC gc
) {
1481 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1485 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1488 Color stateColor
= null;
1489 if (colorIdx
< fEventColorMap
.length
) {
1490 stateColor
= fEventColorMap
[colorIdx
];
1492 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1495 gc
.setForeground(stateColor
);
1496 gc
.setBackground(stateColor
);
1498 /* Draw the arrow */
1499 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1500 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1503 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1508 * @author Francis Giraldeau
1511 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1513 * The algorithm was taken from this site, not the code itself
1515 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1518 double cos
= 0.9510;
1519 double sin
= 0.3090;
1520 long lenx
= x1
- x0
;
1521 long leny
= y1
- y0
;
1522 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1524 double dx
= factor
* lenx
/ len
;
1525 double dy
= factor
* leny
/ len
;
1526 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1527 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1528 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1529 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1530 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1531 gc
.fillPolygon(arrow
);
1535 * Draw the name of an item.
1540 * Where to draw the name
1544 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1545 boolean hasTimeEvents
= item
.fTrace
.hasTimeEvents();
1546 if (! hasTimeEvents
) {
1547 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1548 gc
.fillRectangle(bounds
);
1549 if (item
.fSelected
&& fIsInFocus
) {
1550 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1551 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1554 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1555 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1556 gc
.fillRectangle(bounds
);
1559 // No name to be drawn
1560 if (fTimeProvider
.getNameSpace() == 0) {
1564 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1565 if (item
.fHasChildren
) {
1566 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1567 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1568 Rectangle rect
= Utils
.clone(bounds
);
1569 rect
.x
+= leftMargin
;
1570 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1571 rect
.width
= EXPAND_SIZE
;
1572 rect
.height
= EXPAND_SIZE
;
1573 gc
.fillRectangle(rect
);
1574 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1575 int midy
= rect
.y
+ rect
.height
/ 2;
1576 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1577 if (!item
.fExpanded
) {
1578 int midx
= rect
.x
+ rect
.width
/ 2;
1579 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1582 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1584 Image img
= fTimeGraphProvider
.getItemImage(item
.fTrace
);
1587 int imgHeight
= img
.getImageData().height
;
1588 int imgWidth
= img
.getImageData().width
;
1590 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1591 gc
.drawImage(img
, x
, y
);
1592 leftMargin
+= imgWidth
+ MARGIN
;
1594 String name
= item
.fName
;
1595 Point size
= gc
.stringExtent(name
);
1596 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1597 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1599 if (hasTimeEvents
) {
1600 // cut long string with "..."
1601 int width
= bounds
.width
- leftMargin
;
1603 while (size
.x
> width
&& name
.length() > 1) {
1605 name
= name
.substring(0, name
.length() - 1);
1606 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1609 name
+= "..."; //$NON-NLS-1$
1612 Rectangle rect
= Utils
.clone(bounds
);
1613 rect
.x
+= leftMargin
;
1614 rect
.width
-= leftMargin
;
1616 if (rect
.width
> 0) {
1617 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1618 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1619 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1620 leftMargin
+= textWidth
+ MARGIN
;
1623 if (hasTimeEvents
) {
1625 int x
= bounds
.x
+ leftMargin
;
1626 int width
= bounds
.width
- x
;
1627 int midy
= bounds
.y
+ bounds
.height
/ 2;
1628 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1629 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1635 * Draw the state (color fill)
1640 * Time event for which we're drawing the state
1646 * Is this time event currently selected (so it appears
1648 * @param timeSelected
1649 * Is the timestamp currently selected
1650 * @return true if the state was drawn
1653 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1654 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1656 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1657 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1660 boolean visible
= rect
.width
== 0 ?
false : true;
1661 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1662 gc
.setForeground(black
);
1665 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1666 // Only draw the top and bottom borders
1667 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1668 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1669 if (rect
.width
== 1) {
1670 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1674 Color stateColor
= null;
1675 if (colorIdx
< fEventColorMap
.length
) {
1676 stateColor
= fEventColorMap
[colorIdx
];
1681 boolean reallySelected
= timeSelected
&& selected
;
1682 // fill all rect area
1683 gc
.setBackground(stateColor
);
1684 gc
.fillRectangle(rect
);
1686 if (reallySelected
) {
1687 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1688 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1691 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1693 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1698 * Fill the space between two contiguous time events
1705 * Is this time event selected or not
1707 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1708 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1709 gc
.fillRectangle(rect
);
1710 if (fDragState
== DRAG_ZOOM
) {
1711 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1712 if (fDragX0
< fDragX
) {
1713 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1714 } else if (fDragX0
> fDragX
) {
1715 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1719 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1720 int midy
= rect
.y
+ rect
.height
/ 2;
1721 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1725 public void keyTraversed(TraverseEvent e
) {
1726 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1732 public void keyPressed(KeyEvent e
) {
1734 if (fItemData
.fExpandedItems
.length
== 0) {
1737 if (SWT
.HOME
== e
.keyCode
) {
1739 } else if (SWT
.END
== e
.keyCode
) {
1740 idx
= fItemData
.fExpandedItems
.length
- 1;
1741 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1742 idx
= getSelectedIndex();
1745 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1748 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1749 idx
= getSelectedIndex();
1752 } else if (idx
> 0) {
1755 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1757 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1759 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1760 int page
= countPerPage();
1761 idx
= getSelectedIndex();
1766 if (idx
>= fItemData
.fExpandedItems
.length
) {
1767 idx
= fItemData
.fExpandedItems
.length
- 1;
1769 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1770 int page
= countPerPage();
1771 idx
= getSelectedIndex();
1779 } else if (SWT
.CR
== e
.keyCode
) {
1780 idx
= getSelectedIndex();
1782 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1785 fireDefaultSelection();
1791 selectItem(idx
, false);
1792 fireSelectionChanged();
1794 int x
= toControl(e
.display
.getCursorLocation()).x
;
1795 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1799 public void keyReleased(KeyEvent e
) {
1800 int x
= toControl(e
.display
.getCursorLocation()).x
;
1801 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1805 public void focusGained(FocusEvent e
) {
1807 if (fMouseScrollFilterListener
== null) {
1808 fMouseScrollFilterListener
= new Listener() {
1809 // This filter is used to prevent horizontal scrolling of the view
1810 // when the mouse wheel is used to zoom
1812 public void handleEvent(Event event
) {
1816 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1819 updateStatusLine(NO_STATUS
);
1823 public void focusLost(FocusEvent e
) {
1825 if (fMouseScrollFilterListener
!= null) {
1826 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1827 fMouseScrollFilterListener
= null;
1829 if (DRAG_NONE
!= fDragState
) {
1831 fDragState
= DRAG_NONE
;
1834 updateStatusLine(NO_STATUS
);
1838 * @return If the current view is focused
1840 public boolean isInFocus() {
1845 * Provide the possibility to control the wait cursor externally e.g. data
1846 * requests in progress
1848 * @param waitInd Should we wait indefinitely?
1850 public void waitCursor(boolean waitInd
) {
1851 // Update cursor as indicated
1853 setCursor(fWaitCursor
);
1859 private void updateCursor(int x
, int stateMask
) {
1860 // if Wait cursor not active, check for the need to change the cursor
1861 if (getCursor() == fWaitCursor
) {
1864 Cursor cursor
= null;
1865 if (fDragState
== DRAG_SPLIT_LINE
) {
1866 } else if (fDragState
== DRAG_SELECTION
) {
1867 cursor
= fResizeCursor
;
1868 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1869 cursor
= fDragCursor
;
1870 } else if (fDragState
== DRAG_ZOOM
) {
1871 cursor
= fZoomCursor
;
1872 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1873 cursor
= fDragCursor
;
1874 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1875 cursor
= fResizeCursor
;
1876 } else if (!isOverSplitLine(x
)) {
1877 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1878 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1879 int xBegin
= getXForTime(selectionBegin
);
1880 int xEnd
= getXForTime(selectionEnd
);
1881 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1882 cursor
= fResizeCursor
;
1885 if (getCursor() != cursor
) {
1890 private void updateStatusLine(int x
) {
1891 if (fStatusLineManager
== null || null == fTimeProvider
||
1892 fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
1895 StringBuilder message
= new StringBuilder();
1896 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1897 long time
= getTimeAtX(x
);
1899 message
.append("T: "); //$NON-NLS-1$
1900 message
.append(new TmfNanoTimestamp(time
).toString());
1901 message
.append(" T1: "); //$NON-NLS-1$
1902 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1903 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1904 message
.append(new TmfNanoTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1905 if (selectionBegin
!= selectionEnd
) {
1906 message
.append(" T2: "); //$NON-NLS-1$
1907 message
.append(new TmfNanoTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1908 message
.append(" \u0394: "); //$NON-NLS-1$
1909 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1912 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1913 long time0
= fDragTime0
;
1914 long time
= getTimeAtX(fDragX
);
1915 message
.append("T1: "); //$NON-NLS-1$
1916 message
.append(new TmfNanoTimestamp(Math
.min(time
, time0
)).toString());
1917 if (time
!= time0
) {
1918 message
.append(" T2: "); //$NON-NLS-1$
1919 message
.append(new TmfNanoTimestamp(Math
.max(time
, time0
)).toString());
1920 message
.append(" \u0394: "); //$NON-NLS-1$
1921 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1924 fStatusLineManager
.setMessage(message
.toString());
1928 public void mouseMove(MouseEvent e
) {
1929 if (null == fTimeProvider
) {
1932 Point size
= getCtrlSize();
1933 if (DRAG_TRACE_ITEM
== fDragState
) {
1934 int nameWidth
= fTimeProvider
.getNameSpace();
1935 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1937 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1938 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1939 long time1
= fTime1bak
- timeDelta
;
1940 long maxTime
= fTimeProvider
.getMaxTime();
1941 if (time1
> maxTime
) {
1944 long time0
= time1
- (fTime1bak
- fTime0bak
);
1945 if (time0
< fTimeProvider
.getMinTime()) {
1946 time0
= fTimeProvider
.getMinTime();
1947 time1
= time0
+ (fTime1bak
- fTime0bak
);
1949 fTimeProvider
.setStartFinishTime(time0
, time1
);
1951 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1953 fTimeProvider
.setNameSpace(e
.x
);
1954 } else if (DRAG_SELECTION
== fDragState
) {
1955 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1957 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1958 } else if (DRAG_ZOOM
== fDragState
) {
1959 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1961 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1962 } else if (DRAG_NONE
== fDragState
) {
1963 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1964 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1967 fMouseOverSplitLine
= mouseOverSplitLine
;
1969 updateCursor(e
.x
, e
.stateMask
);
1970 updateStatusLine(e
.x
);
1974 public void mouseDoubleClick(MouseEvent e
) {
1975 if (null == fTimeProvider
) {
1978 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1979 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
1980 fTimeProvider
.setNameSpace(fIdealNameSpace
);
1981 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1982 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1985 fMouseOverSplitLine
= mouseOverSplitLine
;
1988 int idx
= getItemIndexAtY(e
.y
);
1990 selectItem(idx
, false);
1991 fireDefaultSelection();
1997 public void mouseDown(MouseEvent e
) {
1998 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
1999 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2000 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2004 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2005 int nameSpace
= fTimeProvider
.getNameSpace();
2006 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2007 fDragState
= DRAG_SPLIT_LINE
;
2008 fDragButton
= e
.button
;
2011 fTime0bak
= fTimeProvider
.getTime0();
2012 fTime1bak
= fTimeProvider
.getTime1();
2014 updateCursor(e
.x
, e
.stateMask
);
2018 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2019 int nameSpace
= fTimeProvider
.getNameSpace();
2020 idx
= getItemIndexAtY(e
.y
);
2022 Item item
= fItemData
.fExpandedItems
[idx
];
2023 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2027 selectItem(idx
, false);
2028 fireSelectionChanged();
2030 selectItem(idx
, false); // clear selection
2031 fireSelectionChanged();
2033 long hitTime
= getTimeAtX(e
.x
);
2037 fDragState
= DRAG_SELECTION
;
2038 fDragButton
= e
.button
;
2041 fDragTime0
= getTimeAtX(fDragX0
);
2042 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2043 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2044 int xBegin
= getXForTime(selectionBegin
);
2045 int xEnd
= getXForTime(selectionEnd
);
2046 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2047 long time
= getTimeAtX(e
.x
);
2048 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2050 fDragTime0
= selectionEnd
;
2053 fDragTime0
= selectionBegin
;
2056 long time
= getTimeAtX(e
.x
);
2057 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2059 fDragTime0
= selectionEnd
;
2060 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2062 fDragTime0
= selectionBegin
;
2065 fTime0bak
= fTimeProvider
.getTime0();
2066 fTime1bak
= fTimeProvider
.getTime1();
2068 updateCursor(e
.x
, e
.stateMask
);
2069 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2071 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2072 long hitTime
= getTimeAtX(e
.x
);
2075 fDragState
= DRAG_TRACE_ITEM
;
2076 fDragButton
= e
.button
;
2079 fTime0bak
= fTimeProvider
.getTime0();
2080 fTime1bak
= fTimeProvider
.getTime1();
2081 updateCursor(e
.x
, e
.stateMask
);
2083 } else if (3 == e
.button
) {
2085 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2087 fDragState
= DRAG_ZOOM
;
2088 fDragButton
= e
.button
;
2090 updateCursor(e
.x
, e
.stateMask
);
2091 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2096 public void mouseUp(MouseEvent e
) {
2097 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2098 menuDetected(fPendingMenuDetectEvent
);
2100 if (DRAG_NONE
!= fDragState
) {
2102 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2103 if (fDragX
!= fDragX0
) {
2104 fTimeProvider
.notifyStartFinishTime();
2106 fDragState
= DRAG_NONE
;
2107 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2108 fDragState
= DRAG_NONE
;
2110 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2111 if (fDragX
== fDragX0
) { // click without selecting anything
2112 long time
= getTimeAtX(e
.x
);
2113 fTimeProvider
.setSelectedTimeNotify(time
, false);
2115 long time0
= fDragTime0
;
2116 long time1
= getTimeAtX(fDragX
);
2117 if (time0
<= time1
) {
2118 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2120 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2123 fDragState
= DRAG_NONE
;
2125 fTimeGraphScale
.setDragRange(-1, -1);
2126 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2127 int nameWidth
= fTimeProvider
.getNameSpace();
2128 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2129 long time0
= getTimeAtX(fDragX0
);
2130 long time1
= getTimeAtX(fDragX
);
2131 if (time0
< time1
) {
2132 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2134 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2139 fDragState
= DRAG_NONE
;
2140 fTimeGraphScale
.setDragRange(-1, -1);
2143 updateCursor(e
.x
, e
.stateMask
);
2144 updateStatusLine(e
.x
);
2148 public void mouseEnter(MouseEvent e
) {
2152 public void mouseExit(MouseEvent e
) {
2153 if (fMouseOverSplitLine
) {
2154 fMouseOverSplitLine
= false;
2157 updateStatusLine(NO_STATUS
);
2161 public void mouseHover(MouseEvent e
) {
2165 public void mouseScrolled(MouseEvent e
) {
2166 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2169 boolean zoomScroll
= false;
2170 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2171 Point parentSize
= getParent().getSize();
2172 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2173 // over the parent control
2174 if (e
.x
> getCtrlSize().x
) {
2175 // over the horizontal scroll bar
2177 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2178 // over the name space
2184 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2187 } else if (e
.count
< 0) {
2191 setTopIndex(getTopIndex() - e
.count
);
2196 public void controlMoved(ControlEvent e
) {
2200 public void controlResized(ControlEvent e
) {
2205 public void widgetDefaultSelected(SelectionEvent e
) {
2209 public void widgetSelected(SelectionEvent e
) {
2210 if (e
.widget
== getVerticalBar()) {
2211 setTopIndex(getVerticalBar().getSelection());
2212 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2213 int start
= getHorizontalBar().getSelection();
2214 long time0
= fTimeProvider
.getTime0();
2215 long time1
= fTimeProvider
.getTime1();
2216 long timeMin
= fTimeProvider
.getMinTime();
2217 long timeMax
= fTimeProvider
.getMaxTime();
2218 long delta
= timeMax
- timeMin
;
2220 long range
= time1
- time0
;
2221 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2222 time1
= time0
+ range
;
2224 // TODO: Follow-up with Bug 310310
2225 // In Linux SWT.DRAG is the only value received
2226 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2227 if (e
.detail
== SWT
.DRAG
) {
2228 fTimeProvider
.setStartFinishTime(time0
, time1
);
2230 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2236 public int getBorderWidth() {
2237 return fBorderWidth
;
2241 * Set the border width
2243 * @param borderWidth
2246 public void setBorderWidth(int borderWidth
) {
2247 this.fBorderWidth
= borderWidth
;
2251 * @return The current height of the header row
2253 public int getHeaderHeight() {
2254 return fHeaderHeight
;
2258 * Set the height of the header row
2260 * @param headerHeight
2263 public void setHeaderHeight(int headerHeight
) {
2264 this.fHeaderHeight
= headerHeight
;
2268 * @return The default height of regular item rows
2270 public int getItemHeight() {
2271 return fGlobalItemHeight
;
2275 * Set the default height of regular item rows.
2280 public void setItemHeight(int rowHeight
) {
2281 this.fGlobalItemHeight
= rowHeight
;
2285 * Set the height of a specific item. Overrides the default item height.
2288 * A time graph entry
2291 * @return true if the height is successfully stored, false otherwise
2295 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2296 Item item
= fItemData
.findItem(entry
);
2298 item
.fItemHeight
= rowHeight
;
2305 * Set the minimum item width
2307 * @param width The minimum width
2309 public void setMinimumItemWidth(int width
) {
2310 this.fMinimumItemWidth
= width
;
2314 * @return The minimum item width
2316 public int getMinimumItemWidth() {
2317 return fMinimumItemWidth
;
2321 * @return The entries that are currently filtered out
2325 public List
<ITimeGraphEntry
> getFilteredOut() {
2326 return fItemData
.getFilteredOut();
2330 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2331 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2332 fSelectionChangedListeners
.add(listener
);
2337 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2338 if (listener
!= null) {
2339 fSelectionChangedListeners
.remove(listener
);
2344 public void setSelection(ISelection selection
) {
2345 if (selection
instanceof TimeGraphSelection
) {
2346 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2347 Object ob
= sel
.getFirstElement();
2348 if (ob
instanceof ITimeGraphEntry
) {
2349 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2350 selectItem(trace
, false);
2357 * @param filter The filter object to be attached to the view
2360 public void addFilter(ViewerFilter filter
) {
2361 if (!fFilters
.contains(filter
)) {
2362 fFilters
.add(filter
);
2367 * @param filter The filter object to be attached to the view
2370 public void removeFilter(ViewerFilter filter
) {
2371 fFilters
.remove(filter
);
2378 public void colorSettingsChanged(StateItem
[] stateItems
) {
2379 /* Destroy previous colors from the resource manager */
2380 if (fEventColorMap
!= null) {
2381 for (Color color
: fEventColorMap
) {
2382 fResourceManager
.destroyColor(color
.getRGB());
2385 if (stateItems
!= null) {
2386 fEventColorMap
= new Color
[stateItems
.length
];
2387 for (int i
= 0; i
< stateItems
.length
; i
++) {
2388 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2391 fEventColorMap
= new Color
[] { };
2396 private class ItemData
{
2397 private Item
[] fExpandedItems
= new Item
[0];
2398 private Item
[] fItems
= new Item
[0];
2399 private ITimeGraphEntry fTraces
[] = new ITimeGraphEntry
[0];
2400 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2401 private boolean fTraceFilter
[] = new boolean[0];
2402 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2407 Item
findItem(ITimeGraphEntry entry
) {
2408 if (entry
== null) {
2412 for (int i
= 0; i
< fItems
.length
; i
++) {
2413 Item item
= fItems
[i
];
2414 if (item
.fTrace
== entry
) {
2422 int findItemIndex(ITimeGraphEntry trace
) {
2423 if (trace
== null) {
2427 for (int i
= 0; i
< fExpandedItems
.length
; i
++) {
2428 Item item
= fExpandedItems
[i
];
2429 if (item
.fTrace
== trace
) {
2437 public void refreshData() {
2438 List
<Item
> itemList
= new ArrayList
<>();
2439 fFilteredOut
.clear();
2440 ITimeGraphEntry selection
= getSelectedTrace();
2441 for (int i
= 0; i
< fTraces
.length
; i
++) {
2442 ITimeGraphEntry entry
= fTraces
[i
];
2443 refreshData(itemList
, null, 0, entry
);
2445 fItems
= itemList
.toArray(new Item
[0]);
2446 updateExpandedItems();
2447 if (selection
!= null) {
2448 for (Item item
: fExpandedItems
) {
2449 if (item
.fTrace
== selection
) {
2450 item
.fSelected
= true;
2457 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2458 Item item
= new Item(entry
, entry
.getName(), level
);
2459 if (parent
!= null) {
2460 parent
.fChildren
.add(item
);
2462 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2463 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2465 item
.fItemHeight
= fGlobalItemHeight
;
2468 if (entry
.hasChildren()) {
2469 item
.fExpanded
= true;
2470 item
.fHasChildren
= true;
2471 for (ITimeGraphEntry child
: entry
.getChildren()) {
2472 refreshData(itemList
, item
, level
+ 1, child
);
2477 public void updateExpandedItems() {
2478 List
<Item
> expandedItemList
= new ArrayList
<>();
2479 for (int i
= 0; i
< fTraces
.length
; i
++) {
2480 ITimeGraphEntry entry
= fTraces
[i
];
2481 Item item
= findItem(entry
);
2482 refreshExpanded(expandedItemList
, item
);
2484 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2485 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2488 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2489 // Check for filters
2490 boolean display
= true;
2491 for (ViewerFilter filter
: fFilters
) {
2492 if (!filter
.select(null, item
.fTrace
.getParent(), item
.fTrace
)) {
2498 expandedItemList
.add(item
);
2499 if (item
.fHasChildren
&& item
.fExpanded
) {
2500 for (Item child
: item
.fChildren
) {
2501 refreshExpanded(expandedItemList
, child
);
2507 public void refreshData(ITimeGraphEntry traces
[]) {
2508 if (traces
== null) {
2509 fTraceFilter
= null;
2512 if (traces
.length
== 0) {
2513 fTraceFilter
= null;
2514 } else if (fTraceFilter
== null || traces
.length
!= fTraceFilter
.length
) {
2515 fTraceFilter
= new boolean[traces
.length
];
2516 java
.util
.Arrays
.fill(fTraceFilter
, true);
2518 fTraces
= Arrays
.copyOf(traces
, traces
.length
);
2524 public void refreshArrows(List
<ILinkEvent
> events
) {
2525 /* If links are null, reset the list */
2526 if (events
!= null) {
2529 fLinks
= new ArrayList
<>();
2533 public ITimeGraphEntry
[] getTraces() {
2537 public boolean[] getTraceFilter() {
2538 return fTraceFilter
;
2541 public List
<ITimeGraphEntry
> getFilteredOut() {
2542 return fFilteredOut
;
2546 private class Item
{
2547 private boolean fExpanded
;
2548 private boolean fSelected
;
2549 private boolean fHasChildren
;
2550 private int fItemHeight
;
2552 private List
<Item
> fChildren
;
2553 private String fName
;
2554 private ITimeGraphEntry fTrace
;
2556 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2557 this.fTrace
= trace
;
2559 this.fLevel
= level
;
2560 this.fChildren
= new ArrayList
<>();
2564 public String
toString() {
2573 public void menuDetected(MenuDetectEvent e
) {
2574 if (null == fTimeProvider
) {
2577 if (e
.detail
== SWT
.MENU_MOUSE
) {
2578 if (fPendingMenuDetectEvent
== null) {
2579 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2580 * Store the event and trigger it later just before handling mouseUp.
2581 * This allows for the method to detect if mouse is used to drag zoom.
2583 fPendingMenuDetectEvent
= e
;
2586 fPendingMenuDetectEvent
= null;
2587 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2591 if (fDragState
!= DRAG_NONE
) {
2595 Point p
= toControl(e
.x
, e
.y
);
2596 int idx
= getItemIndexAtY(p
.y
);
2597 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2598 Item item
= fItemData
.fExpandedItems
[idx
];
2599 ITimeGraphEntry entry
= item
.fTrace
;
2600 if (entry
.hasTimeEvents()) {
2601 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2602 if (event
!= null) {
2604 fireMenuEventOnTimeEvent(e
);
2609 fireMenuEventOnTimeGraphEntry(e
);