1 /*****************************************************************************
2 * Copyright (c) 2007, 2013 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 *****************************************************************************/
19 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
26 import org
.eclipse
.jface
.action
.IStatusLineManager
;
27 import org
.eclipse
.jface
.resource
.JFaceResources
;
28 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
32 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
45 import org
.eclipse
.swt
.SWT
;
46 import org
.eclipse
.swt
.events
.ControlEvent
;
47 import org
.eclipse
.swt
.events
.ControlListener
;
48 import org
.eclipse
.swt
.events
.FocusEvent
;
49 import org
.eclipse
.swt
.events
.FocusListener
;
50 import org
.eclipse
.swt
.events
.KeyEvent
;
51 import org
.eclipse
.swt
.events
.KeyListener
;
52 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
53 import org
.eclipse
.swt
.events
.MenuDetectListener
;
54 import org
.eclipse
.swt
.events
.MouseEvent
;
55 import org
.eclipse
.swt
.events
.MouseListener
;
56 import org
.eclipse
.swt
.events
.MouseMoveListener
;
57 import org
.eclipse
.swt
.events
.MouseTrackListener
;
58 import org
.eclipse
.swt
.events
.MouseWheelListener
;
59 import org
.eclipse
.swt
.events
.PaintEvent
;
60 import org
.eclipse
.swt
.events
.SelectionEvent
;
61 import org
.eclipse
.swt
.events
.SelectionListener
;
62 import org
.eclipse
.swt
.events
.TraverseEvent
;
63 import org
.eclipse
.swt
.events
.TraverseListener
;
64 import org
.eclipse
.swt
.events
.TypedEvent
;
65 import org
.eclipse
.swt
.graphics
.Color
;
66 import org
.eclipse
.swt
.graphics
.Cursor
;
67 import org
.eclipse
.swt
.graphics
.GC
;
68 import org
.eclipse
.swt
.graphics
.Image
;
69 import org
.eclipse
.swt
.graphics
.Point
;
70 import org
.eclipse
.swt
.graphics
.Rectangle
;
71 import org
.eclipse
.swt
.widgets
.Composite
;
72 import org
.eclipse
.swt
.widgets
.Display
;
73 import org
.eclipse
.swt
.widgets
.Event
;
74 import org
.eclipse
.swt
.widgets
.Listener
;
75 import org
.eclipse
.swt
.widgets
.ScrollBar
;
78 * Time graph control implementation
81 * @author Alvaro Sanchez-Leon
82 * @author Patrick Tasse
84 public class TimeGraphControl
extends TimeGraphBaseControl
85 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
,
86 ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
87 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
{
89 /** Max scrollbar size */
90 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
92 private static final int DRAG_NONE
= 0;
93 private static final int DRAG_TRACE_ITEM
= 1;
94 private static final int DRAG_SPLIT_LINE
= 2;
95 private static final int DRAG_ZOOM
= 3;
96 private static final int DRAG_SELECTION
= 4;
98 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
100 private static final double ZOOM_FACTOR
= 1.5;
101 private static final double ZOOM_IN_FACTOR
= 0.8;
102 private static final double ZOOM_OUT_FACTOR
= 1.25;
104 private static final int SNAP_WIDTH
= 2;
106 private static final int NO_STATUS
= -1;
108 /** Resource manager */
109 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
111 /** Color map for event types */
112 private Color
[] fEventColorMap
= null;
114 private ITimeDataProvider fTimeProvider
;
115 private IStatusLineManager fStatusLineManager
= null;
116 private TimeGraphScale fTimeGraphScale
= null;
118 private boolean fIsInFocus
= false;
119 private boolean fMouseOverSplitLine
= false;
120 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
121 private int fMinimumItemWidth
= 0;
122 private int fTopIndex
= 0;
123 private int fDragState
= DRAG_NONE
;
124 private int fDragButton
;
125 private int fDragX0
= 0;
126 private int fDragX
= 0;
127 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
128 private int fIdealNameSpace
= 0;
129 private long fTime0bak
;
130 private long fTime1bak
;
131 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
132 private ItemData fItemData
= null;
133 private List
<SelectionListener
> fSelectionListeners
;
134 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
135 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
136 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
137 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
138 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
139 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
140 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
141 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
142 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
143 private MenuDetectEvent fPendingMenuDetectEvent
= null;
144 private boolean fHideArrows
= false;
146 private int fBorderWidth
= 0;
147 private int fHeaderHeight
= 0;
149 private Listener fMouseScrollFilterListener
;
151 private MouseScrollNotifier fMouseScrollNotifier
;
152 private final Object fMouseScrollNotifierLock
= new Object();
154 private class MouseScrollNotifier
extends Thread
{
155 private static final long DELAY
= 400L;
156 private static final long POLLING_INTERVAL
= 10L;
157 private long fLastScrollTime
= Long
.MAX_VALUE
;
161 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
163 Thread
.sleep(POLLING_INTERVAL
);
164 } catch (Exception e
) {
168 if (!isInterrupted()) {
169 Display
.getDefault().asyncExec(new Runnable() {
175 fTimeProvider
.notifyStartFinishTime();
179 synchronized (fMouseScrollNotifierLock
) {
180 fMouseScrollNotifier
= null;
184 public void mouseScrolled() {
185 fLastScrollTime
= System
.currentTimeMillis();
190 * Standard constructor
193 * The parent composite object
195 * The color scheme to use
197 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
199 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
201 fItemData
= new ItemData();
203 addFocusListener(this);
204 addMouseListener(this);
205 addMouseMoveListener(this);
206 addMouseTrackListener(this);
207 addMouseWheelListener(this);
208 addTraverseListener(this);
209 addKeyListener(this);
210 addControlListener(this);
211 addMenuDetectListener(this);
212 ScrollBar scrollHor
= getHorizontalBar();
214 if (scrollHor
!= null) {
215 scrollHor
.addSelectionListener(this);
220 public void dispose() {
222 fResourceManager
.dispose();
226 * Sets the timegraph provider used by this timegraph viewer.
228 * @param timeGraphProvider the timegraph provider
230 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
231 fTimeGraphProvider
= timeGraphProvider
;
233 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
234 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
235 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
238 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
239 colorSettingsChanged(stateItems
);
243 * Assign the given time provider
245 * @param timeProvider
248 public void setTimeProvider(ITimeDataProvider timeProvider
) {
249 fTimeProvider
= timeProvider
;
255 * Assign the status line manager
257 * @param statusLineManager
258 * The status line manager, or null to disable status line messages
261 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
262 if (fStatusLineManager
!= null && statusLineManager
== null) {
263 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
265 fStatusLineManager
= statusLineManager
;
269 * Assign the time graph scale
271 * @param timeGraphScale
272 * The time graph scale
275 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
276 fTimeGraphScale
= timeGraphScale
;
280 * Add a selection listener
283 * The listener to add
285 public void addSelectionListener(SelectionListener listener
) {
286 if (listener
== null) {
287 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
289 if (null == fSelectionListeners
) {
290 fSelectionListeners
= new ArrayList
<>();
292 fSelectionListeners
.add(listener
);
296 * Remove a selection listener
299 * The listener to remove
301 public void removeSelectionListener(SelectionListener listener
) {
302 if (null != fSelectionListeners
) {
303 fSelectionListeners
.remove(listener
);
308 * Selection changed callback
310 public void fireSelectionChanged() {
311 if (null != fSelectionListeners
) {
312 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
313 while (it
.hasNext()) {
314 SelectionListener listener
= it
.next();
315 listener
.widgetSelected(null);
321 * Default selection callback
323 public void fireDefaultSelection() {
324 if (null != fSelectionListeners
) {
325 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
326 while (it
.hasNext()) {
327 SelectionListener listener
= it
.next();
328 listener
.widgetDefaultSelected(null);
334 * Get the traces in the model
336 * @return The array of traces
338 public ITimeGraphEntry
[] getTraces() {
339 return fItemData
.getTraces();
343 * Get the on/off trace filters
345 * @return The array of filters
347 public boolean[] getTraceFilter() {
348 return fItemData
.getTraceFilter();
352 * Refresh the data for the thing
354 public void refreshData() {
355 fItemData
.refreshData();
361 * Refresh data for the given traces
364 * The traces to refresh
366 public void refreshData(ITimeGraphEntry
[] traces
) {
367 fItemData
.refreshData(traces
);
373 * Refresh the links (arrows) of this widget
375 * @param events The link events to refresh
378 public void refreshArrows(List
<ILinkEvent
> events
) {
379 fItemData
.refreshArrows(events
);
383 * Adjust the scoll bars
385 public void adjustScrolls() {
386 if (null == fTimeProvider
) {
387 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
393 long time0
= fTimeProvider
.getTime0();
394 long time1
= fTimeProvider
.getTime1();
396 long timeMin
= fTimeProvider
.getMinTime();
397 long timeMax
= fTimeProvider
.getMaxTime();
399 long delta
= timeMax
- timeMin
;
402 int thumb
= H_SCROLLBAR_MAX
;
405 // Thumb size (page size)
406 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
407 // At the beginning of visible window
408 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
411 // position, minimum, maximum, thumb size, increment (half page)t, page
412 // increment size (full page)
413 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
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 * Set the expanded state of a given entry
467 * True if expanded, false if collapsed
469 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
470 Item item
= fItemData
.findItem(entry
);
471 if (item
!= null && item
.fExpanded
!= expanded
) {
472 item
.fExpanded
= expanded
;
473 fItemData
.updateExpandedItems();
479 * Collapses all nodes of the viewer's tree, starting with the root.
483 public void collapseAll() {
484 for (Item item
: fItemData
.fItems
) {
485 item
.fExpanded
= false;
487 fItemData
.updateExpandedItems();
492 * Expands all nodes of the viewer's tree, starting with the root.
496 public void expandAll() {
497 for (Item item
: fItemData
.fItems
) {
498 item
.fExpanded
= true;
500 fItemData
.updateExpandedItems();
505 * Add a tree listener
508 * The listener to add
510 public void addTreeListener(ITimeGraphTreeListener listener
) {
511 if (!fTreeListeners
.contains(listener
)) {
512 fTreeListeners
.add(listener
);
517 * Remove a tree listener
520 * The listener to remove
522 public void removeTreeListener(ITimeGraphTreeListener listener
) {
523 if (fTreeListeners
.contains(listener
)) {
524 fTreeListeners
.remove(listener
);
529 * Tree event callback
534 * The expanded state (true for expanded, false for collapsed)
536 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
537 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
538 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
540 listener
.treeExpanded(event
);
542 listener
.treeCollapsed(event
);
548 * Add a menu listener on {@link ITimeGraphEntry}s
550 * The listener to add
553 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
554 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
555 fTimeGraphEntryMenuListeners
.add(listener
);
560 * Remove a menu listener on {@link ITimeGraphEntry}s
563 * The listener to remove
566 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
567 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
568 fTimeGraphEntryMenuListeners
.remove(listener
);
573 * Menu event callback on {@link ITimeGraphEntry}s
576 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
578 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
579 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
580 listener
.menuDetected(event
);
585 * Add a menu listener on {@link ITimeEvent}s
588 * The listener to add
591 public void addTimeEventMenuListener(MenuDetectListener listener
) {
592 if (!fTimeEventMenuListeners
.contains(listener
)) {
593 fTimeEventMenuListeners
.add(listener
);
598 * Remove a menu listener on {@link ITimeEvent}s
601 * The listener to remove
604 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
605 if (fTimeEventMenuListeners
.contains(listener
)) {
606 fTimeEventMenuListeners
.remove(listener
);
611 * Menu event callback on {@link ITimeEvent}s
614 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
616 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
617 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
618 listener
.menuDetected(event
);
622 @SuppressWarnings("deprecation")
624 public ISelection
getSelection() {
625 TimeGraphSelection sel
= new TimeGraphSelection();
626 ITimeGraphEntry trace
= getSelectedTrace();
627 if (null != trace
&& null != fTimeProvider
) {
629 if (fTimeProvider
instanceof ITimeDataProvider2
) {
630 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
632 selectedTime
= fTimeProvider
.getSelectedTime();
634 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
645 * Get the selection object
647 * @return The selection
649 public ISelection
getSelectionTrace() {
650 TimeGraphSelection sel
= new TimeGraphSelection();
651 ITimeGraphEntry trace
= getSelectedTrace();
659 * Enable/disable one of the traces in the model
662 * 1 to enable it, -1 to disable. The method returns immediately
663 * if another value is used.
665 public void selectTrace(int n
) {
666 if ((n
!= 1) && (n
!= -1)) {
670 boolean changed
= false;
671 int lastSelection
= -1;
672 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
673 Item item
= fItemData
.fExpandedItems
[i
];
674 if (item
.fSelected
) {
676 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
677 item
.fSelected
= false;
678 item
= fItemData
.fExpandedItems
[i
+ 1];
679 item
.fSelected
= true;
681 } else if ((-1 == n
) && (i
> 0)) {
682 item
.fSelected
= false;
683 item
= fItemData
.fExpandedItems
[i
- 1];
684 item
.fSelected
= true;
691 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
692 Item item
= fItemData
.fExpandedItems
[0];
693 item
.fSelected
= true;
698 ensureVisibleItem(-1, false);
700 fireSelectionChanged();
708 * 1 for next event, -1 for previous event
710 public void selectEvent(int n
) {
711 if (null == fTimeProvider
) {
714 ITimeGraphEntry trace
= getSelectedTrace();
719 if (fTimeProvider
instanceof ITimeDataProvider2
) {
720 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
722 @SuppressWarnings("deprecation")
723 long time
= fTimeProvider
.getSelectedTime();
726 long endTime
= fTimeProvider
.getEndTime();
727 ITimeEvent nextEvent
;
728 if (-1 == n
&& selectedTime
> endTime
) {
729 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
731 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
733 if (null == nextEvent
&& -1 == n
) {
734 nextEvent
= Utils
.getFirstEvent(trace
);
736 if (null != nextEvent
) {
737 long nextTime
= nextEvent
.getTime();
738 // If last event detected e.g. going back or not moving to a next
740 if (nextTime
<= selectedTime
&& n
== 1) {
741 // Select to the end of this last event
742 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
743 // but not beyond the end of the trace
744 if (nextTime
> endTime
) {
747 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
748 // for previous event go to its end time unless we were already there
749 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
751 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
752 fireSelectionChanged();
754 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
755 fireSelectionChanged();
760 * Select the next event
762 public void selectNextEvent() {
764 // Notify if visible time window has been adjusted
765 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
769 * Select the previous event
771 public void selectPrevEvent() {
773 // Notify if visible time window has been adjusted
774 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
778 * Select the next trace
780 public void selectNextTrace() {
785 * Select the previous trace
787 public void selectPrevTrace() {
792 * Zoom based on mouse cursor location with mouse scrolling
794 * @param zoomIn true to zoom in, false to zoom out
796 public void zoom(boolean zoomIn
) {
797 int globalX
= getDisplay().getCursorLocation().x
;
798 Point p
= toControl(globalX
, 0);
799 int nameSpace
= fTimeProvider
.getNameSpace();
800 int timeSpace
= fTimeProvider
.getTimeSpace();
801 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
802 long time0
= fTimeProvider
.getTime0();
803 long time1
= fTimeProvider
.getTime1();
804 long interval
= time1
- time0
;
807 } // to allow getting out of single point interval
810 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
812 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
814 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
815 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
816 long newTime1
= newTime0
+ newInterval
;
817 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
818 synchronized (fMouseScrollNotifierLock
) {
819 if (fMouseScrollNotifier
== null) {
820 fMouseScrollNotifier
= new MouseScrollNotifier();
821 fMouseScrollNotifier
.start();
823 fMouseScrollNotifier
.mouseScrolled();
828 * zoom in using single click
830 public void zoomIn() {
831 long prevTime0
= fTimeProvider
.getTime0();
832 long prevTime1
= fTimeProvider
.getTime1();
833 long prevRange
= prevTime1
- prevTime0
;
834 if (prevRange
== 0) {
838 if (fTimeProvider
instanceof ITimeDataProvider2
) {
839 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
840 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
842 @SuppressWarnings("deprecation")
843 long selectedTime
= fTimeProvider
.getSelectedTime();
844 selTime
= selectedTime
;
846 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
847 selTime
= (prevTime0
+ prevTime1
) / 2;
849 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
850 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
852 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
854 if (inaccuracy
> 0 && inaccuracy
< 100) {
855 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
859 long min
= fTimeProvider
.getMinTimeInterval();
860 if ((time1
- time0
) < min
) {
861 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
865 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
869 * zoom out using single click
871 public void zoomOut() {
872 long prevTime0
= fTimeProvider
.getTime0();
873 long prevTime1
= fTimeProvider
.getTime1();
875 if (fTimeProvider
instanceof ITimeDataProvider2
) {
876 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
877 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
879 @SuppressWarnings("deprecation")
880 long selectedTime
= fTimeProvider
.getSelectedTime();
881 selTime
= selectedTime
;
883 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
884 selTime
= (prevTime0
+ prevTime1
) / 2;
886 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
887 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
889 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
890 if (inaccuracy
> 0 && inaccuracy
< 100) {
891 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
895 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
901 * @param hideArrows true to hide arrows
905 public void hideArrows(boolean hideArrows
) {
906 fHideArrows
= hideArrows
;
910 * Follow the arrow forward
914 public void followArrowFwd() {
915 ITimeGraphEntry trace
= getSelectedTrace();
919 long selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
920 for (ILinkEvent link
: fItemData
.fLinks
) {
921 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
922 selectItem(link
.getDestinationEntry(), false);
923 if (link
.getDuration() != 0) {
924 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
925 // Notify if visible time window has been adjusted
926 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
928 fireSelectionChanged();
936 * Follow the arrow backward
940 public void followArrowBwd() {
941 ITimeGraphEntry trace
= getSelectedTrace();
945 long selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
946 for (ILinkEvent link
: fItemData
.fLinks
) {
947 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
948 selectItem(link
.getEntry(), false);
949 if (link
.getDuration() != 0) {
950 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
951 // Notify if visible time window has been adjusted
952 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
954 fireSelectionChanged();
962 * Return the currently selected trace
964 * @return The entry matching the trace
966 public ITimeGraphEntry
getSelectedTrace() {
967 ITimeGraphEntry trace
= null;
968 int idx
= getSelectedIndex();
970 trace
= fItemData
.fExpandedItems
[idx
].fTrace
;
976 * Retrieve the index of the currently selected item
980 public int getSelectedIndex() {
982 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
983 Item item
= fItemData
.fExpandedItems
[i
];
984 if (item
.fSelected
) {
992 boolean toggle(int idx
) {
993 boolean toggled
= false;
994 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
995 Item item
= fItemData
.fExpandedItems
[idx
];
996 if (item
.fHasChildren
) {
997 item
.fExpanded
= !item
.fExpanded
;
998 fItemData
.updateExpandedItems();
1002 fireTreeEvent(item
.fTrace
, item
.fExpanded
);
1008 int getItemIndexAtY(int y
) {
1013 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1014 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1022 boolean isOverSplitLine(int x
) {
1023 if (x
< 0 || null == fTimeProvider
) {
1026 int nameWidth
= fTimeProvider
.getNameSpace();
1027 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1030 ITimeGraphEntry
getEntry(Point pt
) {
1031 int idx
= getItemIndexAtY(pt
.y
);
1032 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fTrace
: null;
1039 public int getXForTime(long time
) {
1040 if (null == fTimeProvider
) {
1043 long time0
= fTimeProvider
.getTime0();
1044 long time1
= fTimeProvider
.getTime1();
1045 int width
= getCtrlSize().x
;
1046 int nameSpace
= fTimeProvider
.getNameSpace();
1047 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1048 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1056 public long getTimeAtX(int coord
) {
1057 if (null == fTimeProvider
) {
1061 Point size
= getCtrlSize();
1062 long time0
= fTimeProvider
.getTime0();
1063 long time1
= fTimeProvider
.getTime1();
1064 int nameWidth
= fTimeProvider
.getNameSpace();
1065 final int x
= coord
- nameWidth
;
1066 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1067 if (x
>= 0 && size
.x
>= nameWidth
) {
1068 if (time1
- time0
> timeWidth
) {
1069 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1070 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1072 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1073 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1079 void selectItem(int idx
, boolean addSelection
) {
1080 boolean changed
= false;
1082 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1083 Item item
= fItemData
.fExpandedItems
[idx
];
1084 changed
= !item
.fSelected
;
1085 item
.fSelected
= true;
1088 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1089 Item item
= fItemData
.fExpandedItems
[i
];
1090 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1093 item
.fSelected
= i
== idx
;
1096 changed
|= ensureVisibleItem(idx
, true);
1103 * Callback for item selection
1106 * The entry matching the trace
1107 * @param addSelection
1108 * If the selection is added or removed
1110 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1111 int idx
= fItemData
.findItemIndex(trace
);
1112 selectItem(idx
, addSelection
);
1116 * Retrieve the number of entries shown per page.
1120 public int countPerPage() {
1121 int height
= getCtrlSize().y
;
1124 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1125 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1126 if (ySum
>= height
) {
1131 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1132 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1133 if (ySum
>= height
) {
1142 * Get the index of the top element
1146 public int getTopIndex() {
1151 * Get the number of expanded items
1153 * @return The count of expanded items
1155 public int getExpandedElementCount() {
1156 return fItemData
.fExpandedItems
.length
;
1160 * Get an array of all expanded elements
1162 * @return The expanded elements
1164 public ITimeGraphEntry
[] getExpandedElements() {
1165 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1166 for (Item item
: fItemData
.fExpandedItems
) {
1167 elements
.add(item
.fTrace
);
1169 return elements
.toArray(new ITimeGraphEntry
[0]);
1172 Point
getCtrlSize() {
1173 Point size
= getSize();
1174 if (getHorizontalBar().isVisible()) {
1175 size
.y
-= getHorizontalBar().getSize().y
;
1180 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1181 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1183 rect
.width
= nameWidth
;
1187 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1188 int x
= bound
.x
+ nameWidth
;
1189 int width
= bound
.width
- x
;
1191 if (idx
>= fTopIndex
) {
1192 for (int i
= fTopIndex
; i
< idx
; i
++) {
1193 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1196 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1197 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1200 int y
= bound
.y
+ ySum
;
1201 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1202 return new Rectangle(x
, y
, width
, height
);
1206 void paint(Rectangle bounds
, PaintEvent e
) {
1208 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1209 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1211 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1215 fIdealNameSpace
= 0;
1216 int nameSpace
= fTimeProvider
.getNameSpace();
1218 // draw empty name space background
1219 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1220 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1223 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1224 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1225 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1227 int alpha
= gc
.getAlpha();
1230 long time0
= fTimeProvider
.getTime0();
1231 long time1
= fTimeProvider
.getTime1();
1232 long selectionBegin
;
1234 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1235 selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1236 selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1238 @SuppressWarnings("deprecation")
1239 long selectedTime
= fTimeProvider
.getSelectedTime();
1240 selectionBegin
= selectedTime
;
1241 selectionEnd
= selectedTime
;
1243 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1244 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1245 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1247 // draw selection lines
1248 if (fDragState
!= DRAG_SELECTION
) {
1249 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1250 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1251 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1254 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1255 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1260 // draw selection background
1261 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1262 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1263 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1264 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1266 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1267 } else if (x0
- x1
> 1) {
1268 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1272 // draw drag selection background
1273 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1274 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1275 if (fDragX0
< fDragX
) {
1276 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1277 } else if (fDragX0
> fDragX
) {
1278 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1283 if (DRAG_SPLIT_LINE
== fDragState
) {
1284 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1285 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1286 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1287 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1288 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1289 if (fDragX
!= fDragX0
) {
1290 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1292 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1293 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1294 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1295 if (fDragX
!= fDragX0
) {
1296 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1298 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1299 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1300 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1307 * Draw many items at once
1310 * The rectangle of the area
1311 * @param timeProvider
1314 * The array items to draw
1316 * The index of the first element to draw
1318 * The width reserved for the names
1320 * Reference to the SWT GC object
1322 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1323 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1324 for (int i
= topIndex
; i
< items
.length
; i
++) {
1325 Item item
= items
[i
];
1326 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1333 * @param item the item to draw
1334 * @param bounds the container rectangle
1335 * @param timeProvider Time provider
1336 * @param i the item index
1337 * @param nameSpace the name space
1338 * @param gc Graphics context
1340 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1341 ITimeGraphEntry entry
= item
.fTrace
;
1342 long time0
= timeProvider
.getTime0();
1343 long time1
= timeProvider
.getTime1();
1345 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1346 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1348 @SuppressWarnings("deprecation")
1349 long time
= fTimeProvider
.getSelectedTime();
1350 selectedTime
= time
;
1353 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1354 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1358 if (! item
.fTrace
.hasTimeEvents()) {
1359 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1360 nameRect
.width
+= statesRect
.width
;
1361 drawName(item
, nameRect
, gc
);
1363 drawName(item
, nameRect
, gc
);
1365 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1366 if (rect
.isEmpty()) {
1367 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1370 if (time1
<= time0
) {
1371 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1372 gc
.fillRectangle(rect
);
1373 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1377 // Initialize _rect1 to same values as enclosing rectangle rect
1378 Rectangle stateRect
= Utils
.clone(rect
);
1379 boolean selected
= item
.fSelected
;
1380 // K pixels per second
1381 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1383 if (item
.fTrace
.hasTimeEvents()) {
1384 fillSpace(rect
, gc
, selected
);
1385 // Drawing rectangle is smaller than reserved space
1387 stateRect
.height
-= 6;
1389 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1390 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1393 while (iterator
.hasNext()) {
1394 ITimeEvent event
= iterator
.next();
1395 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1396 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1397 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1398 // event is out of bounds
1401 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1402 stateRect
.x
= Math
.max(rect
.x
, x
);
1403 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1404 if (stateRect
.x
== lastX
) {
1405 stateRect
.width
-= 1;
1406 if (stateRect
.width
> 0) {
1407 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1408 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1412 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1413 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1418 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1425 * The rectangle of the area
1426 * @param timeProvider
1429 * The array items to draw
1431 * The width reserved for the names
1433 * Reference to the SWT GC object
1436 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1437 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1441 for (ILinkEvent event
: links
) {
1442 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1447 * Draws the link type events of this item
1452 * the container rectangle
1453 * @param timeProvider
1461 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1462 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1463 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1465 if ((srcIndex
== -1) || (destIndex
== -1)) {
1469 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1470 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1472 int x0
= getXForTime(event
.getTime());
1473 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1475 // limit the x-coordinates to prevent integer overflow in calculations
1476 // and also GC.drawLine doesn't draw properly with large coordinates
1477 final int limit
= Integer
.MAX_VALUE
/ 1024;
1478 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1479 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1481 int y0
= src
.y
+ src
.height
/ 2;
1482 int y1
= dst
.y
+ dst
.height
/ 2;
1483 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1487 * Draw the state (color fill)
1492 * Time event for which we're drawing the state
1497 * @return true if the state was drawn
1500 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1501 Rectangle rect
, GC gc
) {
1503 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1507 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1510 Color stateColor
= null;
1511 if (colorIdx
< fEventColorMap
.length
) {
1512 stateColor
= fEventColorMap
[colorIdx
];
1514 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1517 gc
.setForeground(stateColor
);
1518 gc
.setBackground(stateColor
);
1520 /* Draw the arrow */
1521 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1522 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1525 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1530 * @author Francis Giraldeau
1533 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1535 * The algorithm was taken from this site, not the code itself
1537 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1540 double cos
= 0.9510;
1541 double sin
= 0.3090;
1542 long lenx
= x1
- x0
;
1543 long leny
= y1
- y0
;
1544 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1546 double dx
= factor
* lenx
/ len
;
1547 double dy
= factor
* leny
/ len
;
1548 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1549 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1550 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1551 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1552 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1553 gc
.fillPolygon(arrow
);
1557 * Draw the name of an item.
1562 * Where to draw the name
1566 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1567 boolean hasTimeEvents
= item
.fTrace
.hasTimeEvents();
1568 if (! hasTimeEvents
) {
1569 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1570 gc
.fillRectangle(bounds
);
1571 if (item
.fSelected
&& fIsInFocus
) {
1572 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1573 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1576 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1577 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1578 gc
.fillRectangle(bounds
);
1581 // No name to be drawn
1582 if (fTimeProvider
.getNameSpace() == 0) {
1586 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1587 if (item
.fHasChildren
) {
1588 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1589 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1590 Rectangle rect
= Utils
.clone(bounds
);
1591 rect
.x
+= leftMargin
;
1592 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1593 rect
.width
= EXPAND_SIZE
;
1594 rect
.height
= EXPAND_SIZE
;
1595 gc
.fillRectangle(rect
);
1596 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1597 int midy
= rect
.y
+ rect
.height
/ 2;
1598 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1599 if (!item
.fExpanded
) {
1600 int midx
= rect
.x
+ rect
.width
/ 2;
1601 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1604 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1606 Image img
= fTimeGraphProvider
.getItemImage(item
.fTrace
);
1609 int imgHeight
= img
.getImageData().height
;
1610 int imgWidth
= img
.getImageData().width
;
1612 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1613 gc
.drawImage(img
, x
, y
);
1614 leftMargin
+= imgWidth
+ MARGIN
;
1616 String name
= item
.fName
;
1617 Point size
= gc
.stringExtent(name
);
1618 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1619 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1621 if (hasTimeEvents
) {
1622 // cut long string with "..."
1623 int width
= bounds
.width
- leftMargin
;
1625 while (size
.x
> width
&& name
.length() > 1) {
1627 name
= name
.substring(0, name
.length() - 1);
1628 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1631 name
+= "..."; //$NON-NLS-1$
1634 Rectangle rect
= Utils
.clone(bounds
);
1635 rect
.x
+= leftMargin
;
1636 rect
.width
-= leftMargin
;
1638 if (rect
.width
> 0) {
1639 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1640 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1641 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1642 leftMargin
+= textWidth
+ MARGIN
;
1645 if (hasTimeEvents
) {
1647 int x
= bounds
.x
+ leftMargin
;
1648 int width
= bounds
.width
- x
;
1649 int midy
= bounds
.y
+ bounds
.height
/ 2;
1650 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1651 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1657 * Draw the state (color fill)
1662 * Time event for which we're drawing the state
1668 * Is this time event currently selected (so it appears
1670 * @param timeSelected
1671 * Is the timestamp currently selected
1672 * @return true if the state was drawn
1675 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1676 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1678 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1679 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1682 boolean visible
= rect
.width
== 0 ?
false : true;
1685 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1686 // Only draw the top and bottom borders
1687 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1688 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1689 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1690 if (rect
.width
== 1) {
1691 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1692 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1696 Color stateColor
= null;
1697 if (colorIdx
< fEventColorMap
.length
) {
1698 stateColor
= fEventColorMap
[colorIdx
];
1700 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1703 boolean reallySelected
= timeSelected
&& selected
;
1704 // fill all rect area
1705 gc
.setBackground(stateColor
);
1706 gc
.fillRectangle(rect
);
1707 // get the border color?
1708 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1711 if (!reallySelected
) {
1712 // Draw the top and bottom borders i.e. no side borders
1713 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1714 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1717 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1718 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1720 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1725 * Fill the space between two contiguous time events
1732 * Is this time event selected or not
1734 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1735 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1736 gc
.fillRectangle(rect
);
1737 if (fDragState
== DRAG_ZOOM
) {
1738 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1739 if (fDragX0
< fDragX
) {
1740 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1741 } else if (fDragX0
> fDragX
) {
1742 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1746 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1747 int midy
= rect
.y
+ rect
.height
/ 2;
1748 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1752 public void keyTraversed(TraverseEvent e
) {
1753 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1759 public void keyPressed(KeyEvent e
) {
1761 if (fItemData
.fExpandedItems
.length
== 0) {
1764 if (SWT
.HOME
== e
.keyCode
) {
1766 } else if (SWT
.END
== e
.keyCode
) {
1767 idx
= fItemData
.fExpandedItems
.length
- 1;
1768 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1769 idx
= getSelectedIndex();
1772 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1775 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1776 idx
= getSelectedIndex();
1779 } else if (idx
> 0) {
1782 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1784 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1786 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1787 int page
= countPerPage();
1788 idx
= getSelectedIndex();
1793 if (idx
>= fItemData
.fExpandedItems
.length
) {
1794 idx
= fItemData
.fExpandedItems
.length
- 1;
1796 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1797 int page
= countPerPage();
1798 idx
= getSelectedIndex();
1806 } else if (SWT
.CR
== e
.keyCode
) {
1807 idx
= getSelectedIndex();
1809 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1812 fireDefaultSelection();
1818 selectItem(idx
, false);
1819 fireSelectionChanged();
1821 int x
= toControl(e
.display
.getCursorLocation()).x
;
1822 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1826 public void keyReleased(KeyEvent e
) {
1827 int x
= toControl(e
.display
.getCursorLocation()).x
;
1828 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1832 public void focusGained(FocusEvent e
) {
1834 if (fMouseScrollFilterListener
== null) {
1835 fMouseScrollFilterListener
= new Listener() {
1836 // This filter is used to prevent horizontal scrolling of the view
1837 // when the mouse wheel is used to zoom
1839 public void handleEvent(Event event
) {
1843 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1846 updateStatusLine(NO_STATUS
);
1850 public void focusLost(FocusEvent e
) {
1852 if (fMouseScrollFilterListener
!= null) {
1853 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1854 fMouseScrollFilterListener
= null;
1856 if (DRAG_NONE
!= fDragState
) {
1858 fDragState
= DRAG_NONE
;
1861 updateStatusLine(NO_STATUS
);
1865 * @return If the current view is focused
1867 public boolean isInFocus() {
1872 * Provide the possibility to control the wait cursor externally e.g. data
1873 * requests in progress
1875 * @param waitInd Should we wait indefinitely?
1877 public void waitCursor(boolean waitInd
) {
1878 // Update cursor as indicated
1880 setCursor(fWaitCursor
);
1886 private void updateCursor(int x
, int stateMask
) {
1887 // if Wait cursor not active, check for the need to change the cursor
1888 if (getCursor() == fWaitCursor
) {
1891 Cursor cursor
= null;
1892 if (fDragState
== DRAG_SPLIT_LINE
) {
1893 } else if (fDragState
== DRAG_SELECTION
) {
1894 cursor
= fResizeCursor
;
1895 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1896 cursor
= fDragCursor
;
1897 } else if (fDragState
== DRAG_ZOOM
) {
1898 cursor
= fZoomCursor
;
1899 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1900 cursor
= fDragCursor
;
1901 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1902 cursor
= fResizeCursor
;
1904 if (!isOverSplitLine(x
) &&fTimeProvider
instanceof ITimeDataProvider2
) {
1905 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1906 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1907 int xBegin
= getXForTime(selectionBegin
);
1908 int xEnd
= getXForTime(selectionEnd
);
1909 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1910 cursor
= fResizeCursor
;
1914 if (getCursor() != cursor
) {
1919 private void updateStatusLine(int x
) {
1920 if (fStatusLineManager
== null || null == fTimeProvider
||
1921 fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
1924 StringBuilder message
= new StringBuilder();
1925 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1926 long time
= getTimeAtX(x
);
1928 message
.append("T: "); //$NON-NLS-1$
1929 message
.append(new TmfNanoTimestamp(time
).toString());
1930 message
.append(" T1: "); //$NON-NLS-1$
1931 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1932 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1933 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1934 message
.append(new TmfNanoTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1935 if (selectionBegin
!= selectionEnd
) {
1936 message
.append(" T2: "); //$NON-NLS-1$
1937 message
.append(new TmfNanoTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1938 message
.append(" \u0394: "); //$NON-NLS-1$
1939 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1942 @SuppressWarnings("deprecation")
1943 long selectedTime
= fTimeProvider
.getSelectedTime();
1944 message
.append(new TmfNanoTimestamp(selectedTime
));
1947 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1948 long time0
= fDragTime0
;
1949 long time
= getTimeAtX(fDragX
);
1950 message
.append("T1: "); //$NON-NLS-1$
1951 message
.append(new TmfNanoTimestamp(Math
.min(time
, time0
)).toString());
1952 if (time
!= time0
) {
1953 message
.append(" T2: "); //$NON-NLS-1$
1954 message
.append(new TmfNanoTimestamp(Math
.max(time
, time0
)).toString());
1955 message
.append(" \u0394: "); //$NON-NLS-1$
1956 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1959 fStatusLineManager
.setMessage(message
.toString());
1963 public void mouseMove(MouseEvent e
) {
1964 if (null == fTimeProvider
) {
1967 Point size
= getCtrlSize();
1968 if (DRAG_TRACE_ITEM
== fDragState
) {
1969 int nameWidth
= fTimeProvider
.getNameSpace();
1970 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1972 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1973 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1974 long time1
= fTime1bak
- timeDelta
;
1975 long maxTime
= fTimeProvider
.getMaxTime();
1976 if (time1
> maxTime
) {
1979 long time0
= time1
- (fTime1bak
- fTime0bak
);
1980 if (time0
< fTimeProvider
.getMinTime()) {
1981 time0
= fTimeProvider
.getMinTime();
1982 time1
= time0
+ (fTime1bak
- fTime0bak
);
1984 fTimeProvider
.setStartFinishTime(time0
, time1
);
1986 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1988 fTimeProvider
.setNameSpace(e
.x
);
1989 } else if (DRAG_SELECTION
== fDragState
) {
1990 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1991 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1993 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1995 } else if (DRAG_ZOOM
== fDragState
) {
1996 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1998 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1999 } else if (DRAG_NONE
== fDragState
) {
2000 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2001 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2004 fMouseOverSplitLine
= mouseOverSplitLine
;
2006 updateCursor(e
.x
, e
.stateMask
);
2007 updateStatusLine(e
.x
);
2011 public void mouseDoubleClick(MouseEvent e
) {
2012 if (null == fTimeProvider
) {
2015 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2016 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2017 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2018 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2019 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2022 fMouseOverSplitLine
= mouseOverSplitLine
;
2025 int idx
= getItemIndexAtY(e
.y
);
2027 selectItem(idx
, false);
2028 fireDefaultSelection();
2034 public void mouseDown(MouseEvent e
) {
2035 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2036 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2037 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2041 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2042 int nameSpace
= fTimeProvider
.getNameSpace();
2043 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2044 fDragState
= DRAG_SPLIT_LINE
;
2045 fDragButton
= e
.button
;
2048 fTime0bak
= fTimeProvider
.getTime0();
2049 fTime1bak
= fTimeProvider
.getTime1();
2051 updateCursor(e
.x
, e
.stateMask
);
2055 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2056 int nameSpace
= fTimeProvider
.getNameSpace();
2057 idx
= getItemIndexAtY(e
.y
);
2059 Item item
= fItemData
.fExpandedItems
[idx
];
2060 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2064 selectItem(idx
, false);
2065 fireSelectionChanged();
2067 selectItem(idx
, false); // clear selection
2068 fireSelectionChanged();
2070 long hitTime
= getTimeAtX(e
.x
);
2074 fDragState
= DRAG_SELECTION
;
2075 fDragButton
= e
.button
;
2078 fDragTime0
= getTimeAtX(fDragX0
);
2079 if (fTimeProvider
instanceof ITimeDataProvider2
) {
2080 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
2081 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
2082 int xBegin
= getXForTime(selectionBegin
);
2083 int xEnd
= getXForTime(selectionEnd
);
2084 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2085 long time
= getTimeAtX(e
.x
);
2086 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2088 fDragTime0
= selectionEnd
;
2091 fDragTime0
= selectionBegin
;
2094 long time
= getTimeAtX(e
.x
);
2095 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2097 fDragTime0
= selectionEnd
;
2098 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2100 fDragTime0
= selectionBegin
;
2104 fTime0bak
= fTimeProvider
.getTime0();
2105 fTime1bak
= fTimeProvider
.getTime1();
2107 updateCursor(e
.x
, e
.stateMask
);
2108 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2110 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2111 long hitTime
= getTimeAtX(e
.x
);
2114 fDragState
= DRAG_TRACE_ITEM
;
2115 fDragButton
= e
.button
;
2118 fTime0bak
= fTimeProvider
.getTime0();
2119 fTime1bak
= fTimeProvider
.getTime1();
2120 updateCursor(e
.x
, e
.stateMask
);
2122 } else if (3 == e
.button
) {
2124 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2126 fDragState
= DRAG_ZOOM
;
2127 fDragButton
= e
.button
;
2129 updateCursor(e
.x
, e
.stateMask
);
2130 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2135 public void mouseUp(MouseEvent e
) {
2136 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2137 menuDetected(fPendingMenuDetectEvent
);
2139 if (DRAG_NONE
!= fDragState
) {
2141 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2142 if (fDragX
!= fDragX0
) {
2143 fTimeProvider
.notifyStartFinishTime();
2145 fDragState
= DRAG_NONE
;
2146 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2147 fDragState
= DRAG_NONE
;
2149 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2150 if (fDragX
== fDragX0
) { // click without selecting anything
2151 long time
= getTimeAtX(e
.x
);
2152 fTimeProvider
.setSelectedTimeNotify(time
, false);
2154 long time0
= fDragTime0
;
2155 long time1
= getTimeAtX(fDragX
);
2156 if (time0
<= time1
) {
2157 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time0
, time1
);
2159 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time1
, time0
);
2162 fDragState
= DRAG_NONE
;
2164 fTimeGraphScale
.setDragRange(-1, -1);
2165 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2166 int nameWidth
= fTimeProvider
.getNameSpace();
2167 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2168 long time0
= getTimeAtX(fDragX0
);
2169 long time1
= getTimeAtX(fDragX
);
2170 if (time0
< time1
) {
2171 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2173 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2178 fDragState
= DRAG_NONE
;
2179 fTimeGraphScale
.setDragRange(-1, -1);
2182 updateCursor(e
.x
, e
.stateMask
);
2183 updateStatusLine(e
.x
);
2187 public void mouseEnter(MouseEvent e
) {
2191 public void mouseExit(MouseEvent e
) {
2192 if (fMouseOverSplitLine
) {
2193 fMouseOverSplitLine
= false;
2196 updateStatusLine(NO_STATUS
);
2200 public void mouseHover(MouseEvent e
) {
2204 public void mouseScrolled(MouseEvent e
) {
2205 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2208 boolean zoomScroll
= false;
2209 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2210 Point parentSize
= getParent().getSize();
2211 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2212 // over the parent control
2213 if (e
.x
> getCtrlSize().x
) {
2214 // over the horizontal scroll bar
2216 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2217 // over the name space
2223 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2226 } else if (e
.count
< 0) {
2230 setTopIndex(getTopIndex() - e
.count
);
2235 public void controlMoved(ControlEvent e
) {
2239 public void controlResized(ControlEvent e
) {
2244 public void widgetDefaultSelected(SelectionEvent e
) {
2248 public void widgetSelected(SelectionEvent e
) {
2249 if (e
.widget
== getVerticalBar()) {
2250 setTopIndex(getVerticalBar().getSelection());
2251 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2252 int start
= getHorizontalBar().getSelection();
2253 long time0
= fTimeProvider
.getTime0();
2254 long time1
= fTimeProvider
.getTime1();
2255 long timeMin
= fTimeProvider
.getMinTime();
2256 long timeMax
= fTimeProvider
.getMaxTime();
2257 long delta
= timeMax
- timeMin
;
2259 long range
= time1
- time0
;
2260 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2261 time1
= time0
+ range
;
2263 // TODO: Follow-up with Bug 310310
2264 // In Linux SWT.DRAG is the only value received
2265 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2266 if (e
.detail
== SWT
.DRAG
) {
2267 fTimeProvider
.setStartFinishTime(time0
, time1
);
2269 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2275 public int getBorderWidth() {
2276 return fBorderWidth
;
2280 * Set the border width
2282 * @param borderWidth
2285 public void setBorderWidth(int borderWidth
) {
2286 this.fBorderWidth
= borderWidth
;
2290 * @return The current height of the header row
2292 public int getHeaderHeight() {
2293 return fHeaderHeight
;
2297 * Set the height of the header row
2299 * @param headerHeight
2302 public void setHeaderHeight(int headerHeight
) {
2303 this.fHeaderHeight
= headerHeight
;
2307 * @return The default height of regular item rows
2309 public int getItemHeight() {
2310 return fGlobalItemHeight
;
2314 * Set the default height of regular item rows.
2319 public void setItemHeight(int rowHeight
) {
2320 this.fGlobalItemHeight
= rowHeight
;
2324 * Set the height of a specific item. Overrides the default item height.
2327 * A time graph entry
2330 * @return true if the height is successfully stored, false otherwise
2334 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2335 Item item
= fItemData
.findItem(entry
);
2337 item
.fItemHeight
= rowHeight
;
2344 * Set the minimum item width
2346 * @param width The minimum width
2348 public void setMinimumItemWidth(int width
) {
2349 this.fMinimumItemWidth
= width
;
2353 * @return The minimum item width
2355 public int getMinimumItemWidth() {
2356 return fMinimumItemWidth
;
2360 * @return The entries that are currently filtered out
2364 public List
<ITimeGraphEntry
> getFilteredOut() {
2365 return fItemData
.getFilteredOut();
2369 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2370 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2371 fSelectionChangedListeners
.add(listener
);
2376 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2377 if (listener
!= null) {
2378 fSelectionChangedListeners
.remove(listener
);
2383 public void setSelection(ISelection selection
) {
2384 if (selection
instanceof TimeGraphSelection
) {
2385 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2386 Object ob
= sel
.getFirstElement();
2387 if (ob
instanceof ITimeGraphEntry
) {
2388 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2389 selectItem(trace
, false);
2396 * @param filter The filter object to be attached to the view
2399 public void addFilter(ViewerFilter filter
) {
2400 if (!fFilters
.contains(filter
)) {
2401 fFilters
.add(filter
);
2406 * @param filter The filter object to be attached to the view
2409 public void removeFilter(ViewerFilter filter
) {
2410 fFilters
.remove(filter
);
2417 public void colorSettingsChanged(StateItem
[] stateItems
) {
2418 /* Destroy previous colors from the resource manager */
2419 if (fEventColorMap
!= null) {
2420 for (Color color
: fEventColorMap
) {
2421 fResourceManager
.destroyColor(color
.getRGB());
2424 if (stateItems
!= null) {
2425 fEventColorMap
= new Color
[stateItems
.length
];
2426 for (int i
= 0; i
< stateItems
.length
; i
++) {
2427 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2430 fEventColorMap
= new Color
[] { };
2435 private class ItemData
{
2436 private Item
[] fExpandedItems
= new Item
[0];
2437 private Item
[] fItems
= new Item
[0];
2438 private ITimeGraphEntry fTraces
[] = new ITimeGraphEntry
[0];
2439 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2440 private boolean fTraceFilter
[] = new boolean[0];
2441 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2446 Item
findItem(ITimeGraphEntry entry
) {
2447 if (entry
== null) {
2451 for (int i
= 0; i
< fItems
.length
; i
++) {
2452 Item item
= fItems
[i
];
2453 if (item
.fTrace
== entry
) {
2461 int findItemIndex(ITimeGraphEntry trace
) {
2462 if (trace
== null) {
2466 for (int i
= 0; i
< fExpandedItems
.length
; i
++) {
2467 Item item
= fExpandedItems
[i
];
2468 if (item
.fTrace
== trace
) {
2476 public void refreshData() {
2477 List
<Item
> itemList
= new ArrayList
<>();
2478 fFilteredOut
.clear();
2479 ITimeGraphEntry selection
= getSelectedTrace();
2480 for (int i
= 0; i
< fTraces
.length
; i
++) {
2481 ITimeGraphEntry entry
= fTraces
[i
];
2482 refreshData(itemList
, null, 0, entry
);
2484 fItems
= itemList
.toArray(new Item
[0]);
2485 updateExpandedItems();
2486 if (selection
!= null) {
2487 for (Item item
: fExpandedItems
) {
2488 if (item
.fTrace
== selection
) {
2489 item
.fSelected
= true;
2496 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2497 Item item
= new Item(entry
, entry
.getName(), level
);
2498 if (parent
!= null) {
2499 parent
.fChildren
.add(item
);
2501 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2502 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2504 item
.fItemHeight
= fGlobalItemHeight
;
2507 if (entry
.hasChildren()) {
2508 item
.fExpanded
= true;
2509 item
.fHasChildren
= true;
2510 for (ITimeGraphEntry child
: entry
.getChildren()) {
2511 refreshData(itemList
, item
, level
+ 1, child
);
2516 public void updateExpandedItems() {
2517 List
<Item
> expandedItemList
= new ArrayList
<>();
2518 for (int i
= 0; i
< fTraces
.length
; i
++) {
2519 ITimeGraphEntry entry
= fTraces
[i
];
2520 Item item
= findItem(entry
);
2521 refreshExpanded(expandedItemList
, item
);
2523 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2524 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2527 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2528 // Check for filters
2529 boolean display
= true;
2530 for (ViewerFilter filter
: fFilters
) {
2531 if (!filter
.select(null, item
.fTrace
.getParent(), item
.fTrace
)) {
2537 expandedItemList
.add(item
);
2538 if (item
.fHasChildren
&& item
.fExpanded
) {
2539 for (Item child
: item
.fChildren
) {
2540 refreshExpanded(expandedItemList
, child
);
2546 public void refreshData(ITimeGraphEntry traces
[]) {
2547 if (traces
== null) {
2548 fTraceFilter
= null;
2551 if (traces
.length
== 0) {
2552 fTraceFilter
= null;
2553 } else if (fTraceFilter
== null || traces
.length
!= fTraceFilter
.length
) {
2554 fTraceFilter
= new boolean[traces
.length
];
2555 java
.util
.Arrays
.fill(fTraceFilter
, true);
2557 fTraces
= Arrays
.copyOf(traces
, traces
.length
);
2563 public void refreshArrows(List
<ILinkEvent
> events
) {
2564 /* If links are null, reset the list */
2565 if (events
!= null) {
2568 fLinks
= new ArrayList
<>();
2572 public ITimeGraphEntry
[] getTraces() {
2576 public boolean[] getTraceFilter() {
2577 return fTraceFilter
;
2580 public List
<ITimeGraphEntry
> getFilteredOut() {
2581 return fFilteredOut
;
2585 private class Item
{
2586 private boolean fExpanded
;
2587 private boolean fSelected
;
2588 private boolean fHasChildren
;
2589 private int fItemHeight
;
2591 private List
<Item
> fChildren
;
2592 private String fName
;
2593 private ITimeGraphEntry fTrace
;
2595 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2596 this.fTrace
= trace
;
2598 this.fLevel
= level
;
2599 this.fChildren
= new ArrayList
<>();
2603 public String
toString() {
2612 public void menuDetected(MenuDetectEvent e
) {
2613 if (null == fTimeProvider
) {
2616 if (e
.detail
== SWT
.MENU_MOUSE
) {
2617 if (fPendingMenuDetectEvent
== null) {
2618 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2619 * Store the event and trigger it later just before handling mouseUp.
2620 * This allows for the method to detect if mouse is used to drag zoom.
2622 fPendingMenuDetectEvent
= e
;
2625 fPendingMenuDetectEvent
= null;
2626 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2630 if (fDragState
!= DRAG_NONE
) {
2634 Point p
= toControl(e
.x
, e
.y
);
2635 int idx
= getItemIndexAtY(p
.y
);
2636 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2637 Item item
= fItemData
.fExpandedItems
[idx
];
2638 ITimeGraphEntry entry
= item
.fTrace
;
2639 if (entry
.hasTimeEvents()) {
2640 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2641 if (event
!= null) {
2643 fireMenuEventOnTimeEvent(e
);
2648 fireMenuEventOnTimeGraphEntry(e
);