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
.LinkedHashMap
;
26 import java
.util
.List
;
29 import org
.eclipse
.jface
.action
.IStatusLineManager
;
30 import org
.eclipse
.jface
.resource
.JFaceResources
;
31 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
32 import org
.eclipse
.jface
.viewers
.ISelection
;
33 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
34 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
35 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
48 import org
.eclipse
.swt
.SWT
;
49 import org
.eclipse
.swt
.events
.ControlEvent
;
50 import org
.eclipse
.swt
.events
.ControlListener
;
51 import org
.eclipse
.swt
.events
.FocusEvent
;
52 import org
.eclipse
.swt
.events
.FocusListener
;
53 import org
.eclipse
.swt
.events
.KeyEvent
;
54 import org
.eclipse
.swt
.events
.KeyListener
;
55 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
56 import org
.eclipse
.swt
.events
.MenuDetectListener
;
57 import org
.eclipse
.swt
.events
.MouseEvent
;
58 import org
.eclipse
.swt
.events
.MouseListener
;
59 import org
.eclipse
.swt
.events
.MouseMoveListener
;
60 import org
.eclipse
.swt
.events
.MouseTrackListener
;
61 import org
.eclipse
.swt
.events
.MouseWheelListener
;
62 import org
.eclipse
.swt
.events
.PaintEvent
;
63 import org
.eclipse
.swt
.events
.SelectionEvent
;
64 import org
.eclipse
.swt
.events
.SelectionListener
;
65 import org
.eclipse
.swt
.events
.TraverseEvent
;
66 import org
.eclipse
.swt
.events
.TraverseListener
;
67 import org
.eclipse
.swt
.events
.TypedEvent
;
68 import org
.eclipse
.swt
.graphics
.Color
;
69 import org
.eclipse
.swt
.graphics
.Cursor
;
70 import org
.eclipse
.swt
.graphics
.GC
;
71 import org
.eclipse
.swt
.graphics
.Image
;
72 import org
.eclipse
.swt
.graphics
.Point
;
73 import org
.eclipse
.swt
.graphics
.Rectangle
;
74 import org
.eclipse
.swt
.widgets
.Composite
;
75 import org
.eclipse
.swt
.widgets
.Display
;
76 import org
.eclipse
.swt
.widgets
.Event
;
77 import org
.eclipse
.swt
.widgets
.Listener
;
78 import org
.eclipse
.swt
.widgets
.ScrollBar
;
81 * Time graph control implementation
84 * @author Alvaro Sanchez-Leon
85 * @author Patrick Tasse
87 public class TimeGraphControl
extends TimeGraphBaseControl
88 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
,
89 ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
90 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
{
92 /** Max scrollbar size */
93 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
95 private static final int DRAG_NONE
= 0;
96 private static final int DRAG_TRACE_ITEM
= 1;
97 private static final int DRAG_SPLIT_LINE
= 2;
98 private static final int DRAG_ZOOM
= 3;
99 private static final int DRAG_SELECTION
= 4;
101 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
103 private static final double ZOOM_FACTOR
= 1.5;
104 private static final double ZOOM_IN_FACTOR
= 0.8;
105 private static final double ZOOM_OUT_FACTOR
= 1.25;
107 private static final int SNAP_WIDTH
= 2;
109 private static final int NO_STATUS
= -1;
111 /** Resource manager */
112 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
114 /** Color map for event types */
115 private Color
[] fEventColorMap
= null;
117 private ITimeDataProvider fTimeProvider
;
118 private IStatusLineManager fStatusLineManager
= null;
119 private TimeGraphScale fTimeGraphScale
= null;
121 private boolean fIsInFocus
= false;
122 private boolean fMouseOverSplitLine
= false;
123 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
124 private int fMinimumItemWidth
= 0;
125 private int fTopIndex
= 0;
126 private int fDragState
= DRAG_NONE
;
127 private int fDragButton
;
128 private int fDragX0
= 0;
129 private int fDragX
= 0;
130 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
131 private int fIdealNameSpace
= 0;
132 private long fTime0bak
;
133 private long fTime1bak
;
134 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
135 private ItemData fItemData
= null;
136 private List
<SelectionListener
> fSelectionListeners
;
137 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
138 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
139 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
140 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
141 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
142 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
143 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
144 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
145 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
146 private MenuDetectEvent fPendingMenuDetectEvent
= null;
147 private boolean fHideArrows
= false;
149 private int fBorderWidth
= 0;
150 private int fHeaderHeight
= 0;
152 private Listener fMouseScrollFilterListener
;
154 private MouseScrollNotifier fMouseScrollNotifier
;
155 private final Object fMouseScrollNotifierLock
= new Object();
157 private class MouseScrollNotifier
extends Thread
{
158 private static final long DELAY
= 400L;
159 private static final long POLLING_INTERVAL
= 10L;
160 private long fLastScrollTime
= Long
.MAX_VALUE
;
164 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
166 Thread
.sleep(POLLING_INTERVAL
);
167 } catch (Exception e
) {
171 if (!isInterrupted()) {
172 Display
.getDefault().asyncExec(new Runnable() {
178 fTimeProvider
.notifyStartFinishTime();
182 synchronized (fMouseScrollNotifierLock
) {
183 fMouseScrollNotifier
= null;
187 public void mouseScrolled() {
188 fLastScrollTime
= System
.currentTimeMillis();
193 * Standard constructor
196 * The parent composite object
198 * The color scheme to use
200 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
202 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
204 fItemData
= new ItemData();
206 addFocusListener(this);
207 addMouseListener(this);
208 addMouseMoveListener(this);
209 addMouseTrackListener(this);
210 addMouseWheelListener(this);
211 addTraverseListener(this);
212 addKeyListener(this);
213 addControlListener(this);
214 addMenuDetectListener(this);
215 ScrollBar scrollHor
= getHorizontalBar();
217 if (scrollHor
!= null) {
218 scrollHor
.addSelectionListener(this);
223 public void dispose() {
225 fResourceManager
.dispose();
229 * Sets the timegraph provider used by this timegraph viewer.
231 * @param timeGraphProvider the timegraph provider
233 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
234 fTimeGraphProvider
= timeGraphProvider
;
236 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
237 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
238 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
241 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
242 colorSettingsChanged(stateItems
);
246 * Gets the timegraph provider used by this timegraph viewer.
248 * @return the timegraph provider, or <code>null</code> if not set.
251 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
252 return fTimeGraphProvider
;
256 * Gets the color map used by this timegraph viewer.
258 * @return a color map, or <code>null</code> if not set.
261 public Color
[] getEventColorMap() {
262 return fEventColorMap
;
266 * Assign the given time provider
268 * @param timeProvider
271 public void setTimeProvider(ITimeDataProvider timeProvider
) {
272 fTimeProvider
= timeProvider
;
278 * Assign the status line manager
280 * @param statusLineManager
281 * The status line manager, or null to disable status line messages
284 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
285 if (fStatusLineManager
!= null && statusLineManager
== null) {
286 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
288 fStatusLineManager
= statusLineManager
;
292 * Assign the time graph scale
294 * @param timeGraphScale
295 * The time graph scale
298 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
299 fTimeGraphScale
= timeGraphScale
;
303 * Add a selection listener
306 * The listener to add
308 public void addSelectionListener(SelectionListener listener
) {
309 if (listener
== null) {
310 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
312 if (null == fSelectionListeners
) {
313 fSelectionListeners
= new ArrayList
<>();
315 fSelectionListeners
.add(listener
);
319 * Remove a selection listener
322 * The listener to remove
324 public void removeSelectionListener(SelectionListener listener
) {
325 if (null != fSelectionListeners
) {
326 fSelectionListeners
.remove(listener
);
331 * Selection changed callback
333 public void fireSelectionChanged() {
334 if (null != fSelectionListeners
) {
335 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
336 while (it
.hasNext()) {
337 SelectionListener listener
= it
.next();
338 listener
.widgetSelected(null);
344 * Default selection callback
346 public void fireDefaultSelection() {
347 if (null != fSelectionListeners
) {
348 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
349 while (it
.hasNext()) {
350 SelectionListener listener
= it
.next();
351 listener
.widgetDefaultSelected(null);
357 * Get the traces in the model
359 * @return The array of traces
361 public ITimeGraphEntry
[] getTraces() {
362 return fItemData
.getEntries();
366 * Get the on/off trace filters
368 * @return The array of filters
370 public boolean[] getTraceFilter() {
371 return fItemData
.getEntryFilter();
375 * Refresh the data for the thing
377 public void refreshData() {
378 fItemData
.refreshData();
384 * Refresh data for the given traces
387 * The traces to refresh
389 public void refreshData(ITimeGraphEntry
[] traces
) {
390 fItemData
.refreshData(traces
);
396 * Refresh the links (arrows) of this widget
398 * @param events The link events to refresh
401 public void refreshArrows(List
<ILinkEvent
> events
) {
402 fItemData
.refreshArrows(events
);
406 * Adjust the scoll bars
408 public void adjustScrolls() {
409 if (null == fTimeProvider
) {
410 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
416 long time0
= fTimeProvider
.getTime0();
417 long time1
= fTimeProvider
.getTime1();
419 long timeMin
= fTimeProvider
.getMinTime();
420 long timeMax
= fTimeProvider
.getMaxTime();
422 long delta
= timeMax
- timeMin
;
425 int thumb
= H_SCROLLBAR_MAX
;
428 // Thumb size (page size)
429 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
430 // At the beginning of visible window
431 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
434 // position, minimum, maximum, thumb size, increment (half page)t, page
435 // increment size (full page)
436 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
439 boolean ensureVisibleItem(int idx
, boolean redraw
) {
440 boolean changed
= false;
443 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
444 if (fItemData
.fExpandedItems
[index
].fSelected
) {
449 if (index
>= fItemData
.fExpandedItems
.length
) {
452 if (index
< fTopIndex
) {
459 int page
= countPerPage();
460 if (index
>= fTopIndex
+ page
) {
461 setTopIndex(index
- page
+ 1);
472 * Assign the given index as the top one
477 public void setTopIndex(int idx
) {
478 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
479 index
= Math
.max(0, index
);
485 * Set the expanded state of a given entry
490 * True if expanded, false if collapsed
492 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
493 Item item
= fItemData
.findItem(entry
);
494 if (item
!= null && item
.fExpanded
!= expanded
) {
495 item
.fExpanded
= expanded
;
496 fItemData
.updateExpandedItems();
502 * Collapses all nodes of the viewer's tree, starting with the root.
506 public void collapseAll() {
507 for (Item item
: fItemData
.fItems
) {
508 item
.fExpanded
= false;
510 fItemData
.updateExpandedItems();
515 * Expands all nodes of the viewer's tree, starting with the root.
519 public void expandAll() {
520 for (Item item
: fItemData
.fItems
) {
521 item
.fExpanded
= true;
523 fItemData
.updateExpandedItems();
528 * Add a tree listener
531 * The listener to add
533 public void addTreeListener(ITimeGraphTreeListener listener
) {
534 if (!fTreeListeners
.contains(listener
)) {
535 fTreeListeners
.add(listener
);
540 * Remove a tree listener
543 * The listener to remove
545 public void removeTreeListener(ITimeGraphTreeListener listener
) {
546 if (fTreeListeners
.contains(listener
)) {
547 fTreeListeners
.remove(listener
);
552 * Tree event callback
557 * The expanded state (true for expanded, false for collapsed)
559 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
560 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
561 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
563 listener
.treeExpanded(event
);
565 listener
.treeCollapsed(event
);
571 * Add a menu listener on {@link ITimeGraphEntry}s
573 * The listener to add
576 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
577 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
578 fTimeGraphEntryMenuListeners
.add(listener
);
583 * Remove a menu listener on {@link ITimeGraphEntry}s
586 * The listener to remove
589 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
590 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
591 fTimeGraphEntryMenuListeners
.remove(listener
);
596 * Menu event callback on {@link ITimeGraphEntry}s
599 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
601 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
602 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
603 listener
.menuDetected(event
);
608 * Add a menu listener on {@link ITimeEvent}s
611 * The listener to add
614 public void addTimeEventMenuListener(MenuDetectListener listener
) {
615 if (!fTimeEventMenuListeners
.contains(listener
)) {
616 fTimeEventMenuListeners
.add(listener
);
621 * Remove a menu listener on {@link ITimeEvent}s
624 * The listener to remove
627 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
628 if (fTimeEventMenuListeners
.contains(listener
)) {
629 fTimeEventMenuListeners
.remove(listener
);
634 * Menu event callback on {@link ITimeEvent}s
637 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
639 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
640 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
641 listener
.menuDetected(event
);
646 public ISelection
getSelection() {
647 TimeGraphSelection sel
= new TimeGraphSelection();
648 ITimeGraphEntry trace
= getSelectedTrace();
649 if (null != trace
&& null != fTimeProvider
) {
650 long selectedTime
= fTimeProvider
.getSelectionBegin();
651 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
662 * Get the selection object
664 * @return The selection
666 public ISelection
getSelectionTrace() {
667 TimeGraphSelection sel
= new TimeGraphSelection();
668 ITimeGraphEntry trace
= getSelectedTrace();
676 * Enable/disable one of the traces in the model
679 * 1 to enable it, -1 to disable. The method returns immediately
680 * if another value is used.
682 public void selectTrace(int n
) {
683 if ((n
!= 1) && (n
!= -1)) {
687 boolean changed
= false;
688 int lastSelection
= -1;
689 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
690 Item item
= fItemData
.fExpandedItems
[i
];
691 if (item
.fSelected
) {
693 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
694 item
.fSelected
= false;
695 item
= fItemData
.fExpandedItems
[i
+ 1];
696 item
.fSelected
= true;
698 } else if ((-1 == n
) && (i
> 0)) {
699 item
.fSelected
= false;
700 item
= fItemData
.fExpandedItems
[i
- 1];
701 item
.fSelected
= true;
708 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
709 Item item
= fItemData
.fExpandedItems
[0];
710 item
.fSelected
= true;
715 ensureVisibleItem(-1, false);
717 fireSelectionChanged();
725 * 1 for next event, -1 for previous event
727 public void selectEvent(int n
) {
728 if (null == fTimeProvider
) {
731 ITimeGraphEntry trace
= getSelectedTrace();
735 long selectedTime
= fTimeProvider
.getSelectionBegin();
736 long endTime
= fTimeProvider
.getEndTime();
737 ITimeEvent nextEvent
;
738 if (-1 == n
&& selectedTime
> endTime
) {
739 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
741 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
743 if (null == nextEvent
&& -1 == n
) {
744 nextEvent
= Utils
.getFirstEvent(trace
);
746 if (null != nextEvent
) {
747 long nextTime
= nextEvent
.getTime();
748 // If last event detected e.g. going back or not moving to a next
750 if (nextTime
<= selectedTime
&& n
== 1) {
751 // Select to the end of this last event
752 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
753 // but not beyond the end of the trace
754 if (nextTime
> endTime
) {
757 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
758 // for previous event go to its end time unless we were already there
759 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
761 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
762 fireSelectionChanged();
764 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
765 fireSelectionChanged();
770 * Select the next event
772 public void selectNextEvent() {
774 // Notify if visible time window has been adjusted
775 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
779 * Select the previous event
781 public void selectPrevEvent() {
783 // Notify if visible time window has been adjusted
784 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
788 * Select the next trace
790 public void selectNextTrace() {
795 * Select the previous trace
797 public void selectPrevTrace() {
802 * Zoom based on mouse cursor location with mouse scrolling
804 * @param zoomIn true to zoom in, false to zoom out
806 public void zoom(boolean zoomIn
) {
807 int globalX
= getDisplay().getCursorLocation().x
;
808 Point p
= toControl(globalX
, 0);
809 int nameSpace
= fTimeProvider
.getNameSpace();
810 int timeSpace
= fTimeProvider
.getTimeSpace();
811 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
812 long time0
= fTimeProvider
.getTime0();
813 long time1
= fTimeProvider
.getTime1();
814 long interval
= time1
- time0
;
817 } // to allow getting out of single point interval
820 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
822 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
824 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
825 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
826 long newTime1
= newTime0
+ newInterval
;
827 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
828 synchronized (fMouseScrollNotifierLock
) {
829 if (fMouseScrollNotifier
== null) {
830 fMouseScrollNotifier
= new MouseScrollNotifier();
831 fMouseScrollNotifier
.start();
833 fMouseScrollNotifier
.mouseScrolled();
838 * zoom in using single click
840 public void zoomIn() {
841 long prevTime0
= fTimeProvider
.getTime0();
842 long prevTime1
= fTimeProvider
.getTime1();
843 long prevRange
= prevTime1
- prevTime0
;
844 if (prevRange
== 0) {
847 ITimeDataProvider provider
= fTimeProvider
;
848 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
849 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
850 selTime
= (prevTime0
+ prevTime1
) / 2;
852 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
853 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
855 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
857 if (inaccuracy
> 0 && inaccuracy
< 100) {
858 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
862 long min
= fTimeProvider
.getMinTimeInterval();
863 if ((time1
- time0
) < min
) {
864 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
868 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
872 * zoom out using single click
874 public void zoomOut() {
875 long prevTime0
= fTimeProvider
.getTime0();
876 long prevTime1
= fTimeProvider
.getTime1();
877 ITimeDataProvider provider
= fTimeProvider
;
878 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
879 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
880 selTime
= (prevTime0
+ prevTime1
) / 2;
882 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
883 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
885 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
886 if (inaccuracy
> 0 && inaccuracy
< 100) {
887 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
891 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
897 * @param hideArrows true to hide arrows
901 public void hideArrows(boolean hideArrows
) {
902 fHideArrows
= hideArrows
;
906 * Follow the arrow forward
910 public void followArrowFwd() {
911 ITimeGraphEntry trace
= getSelectedTrace();
915 long selectedTime
= fTimeProvider
.getSelectionBegin();
916 for (ILinkEvent link
: fItemData
.fLinks
) {
917 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
918 selectItem(link
.getDestinationEntry(), false);
919 if (link
.getDuration() != 0) {
920 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
921 // Notify if visible time window has been adjusted
922 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
924 fireSelectionChanged();
932 * Follow the arrow backward
936 public void followArrowBwd() {
937 ITimeGraphEntry trace
= getSelectedTrace();
941 long selectedTime
= fTimeProvider
.getSelectionBegin();
942 for (ILinkEvent link
: fItemData
.fLinks
) {
943 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
944 selectItem(link
.getEntry(), false);
945 if (link
.getDuration() != 0) {
946 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
947 // Notify if visible time window has been adjusted
948 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
950 fireSelectionChanged();
958 * Return the currently selected trace
960 * @return The entry matching the trace
962 public ITimeGraphEntry
getSelectedTrace() {
963 ITimeGraphEntry trace
= null;
964 int idx
= getSelectedIndex();
966 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
972 * Retrieve the index of the currently selected item
976 public int getSelectedIndex() {
978 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
979 Item item
= fItemData
.fExpandedItems
[i
];
980 if (item
.fSelected
) {
988 boolean toggle(int idx
) {
989 boolean toggled
= false;
990 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
991 Item item
= fItemData
.fExpandedItems
[idx
];
992 if (item
.fHasChildren
) {
993 item
.fExpanded
= !item
.fExpanded
;
994 fItemData
.updateExpandedItems();
998 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1005 * Gets the index of the item at the given location.
1009 * @return the index of the item at the given location, of -1 if none.
1012 protected int getItemIndexAtY(int y
) {
1017 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1018 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1026 boolean isOverSplitLine(int x
) {
1027 if (x
< 0 || null == fTimeProvider
) {
1030 int nameWidth
= fTimeProvider
.getNameSpace();
1031 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1035 * Gets the {@link ITimeGraphEntry} at the given location.
1038 * a point in the widget
1039 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1043 protected ITimeGraphEntry
getEntry(Point pt
) {
1044 int idx
= getItemIndexAtY(pt
.y
);
1045 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1052 public int getXForTime(long time
) {
1053 if (null == fTimeProvider
) {
1056 long time0
= fTimeProvider
.getTime0();
1057 long time1
= fTimeProvider
.getTime1();
1058 int width
= getCtrlSize().x
;
1059 int nameSpace
= fTimeProvider
.getNameSpace();
1060 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1061 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1069 public long getTimeAtX(int coord
) {
1070 if (null == fTimeProvider
) {
1074 Point size
= getCtrlSize();
1075 long time0
= fTimeProvider
.getTime0();
1076 long time1
= fTimeProvider
.getTime1();
1077 int nameWidth
= fTimeProvider
.getNameSpace();
1078 final int x
= coord
- nameWidth
;
1079 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1080 if (x
>= 0 && size
.x
>= nameWidth
) {
1081 if (time1
- time0
> timeWidth
) {
1082 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1083 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1085 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1086 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1092 void selectItem(int idx
, boolean addSelection
) {
1093 boolean changed
= false;
1095 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1096 Item item
= fItemData
.fExpandedItems
[idx
];
1097 changed
= !item
.fSelected
;
1098 item
.fSelected
= true;
1101 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1102 Item item
= fItemData
.fExpandedItems
[i
];
1103 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1106 item
.fSelected
= i
== idx
;
1109 changed
|= ensureVisibleItem(idx
, true);
1116 * Callback for item selection
1119 * The entry matching the trace
1120 * @param addSelection
1121 * If the selection is added or removed
1123 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1124 int idx
= fItemData
.findItemIndex(trace
);
1125 selectItem(idx
, addSelection
);
1129 * Retrieve the number of entries shown per page.
1133 public int countPerPage() {
1134 int height
= getCtrlSize().y
;
1137 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1138 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1139 if (ySum
>= height
) {
1144 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1145 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1146 if (ySum
>= height
) {
1155 * Get the index of the top element
1159 public int getTopIndex() {
1164 * Get the number of expanded items
1166 * @return The count of expanded items
1168 public int getExpandedElementCount() {
1169 return fItemData
.fExpandedItems
.length
;
1173 * Get an array of all expanded elements
1175 * @return The expanded elements
1177 public ITimeGraphEntry
[] getExpandedElements() {
1178 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1179 for (Item item
: fItemData
.fExpandedItems
) {
1180 elements
.add(item
.fEntry
);
1182 return elements
.toArray(new ITimeGraphEntry
[0]);
1185 Point
getCtrlSize() {
1186 Point size
= getSize();
1187 if (getHorizontalBar().isVisible()) {
1188 size
.y
-= getHorizontalBar().getSize().y
;
1193 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1194 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1196 rect
.width
= nameWidth
;
1200 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1201 int x
= bound
.x
+ nameWidth
;
1202 int width
= bound
.width
- x
;
1204 if (idx
>= fTopIndex
) {
1205 for (int i
= fTopIndex
; i
< idx
; i
++) {
1206 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1209 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1210 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1213 int y
= bound
.y
+ ySum
;
1214 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1215 return new Rectangle(x
, y
, width
, height
);
1219 void paint(Rectangle bounds
, PaintEvent e
) {
1221 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1222 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1224 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1228 fIdealNameSpace
= 0;
1229 int nameSpace
= fTimeProvider
.getNameSpace();
1231 // draw empty name space background
1232 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1233 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1236 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1237 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1238 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1240 int alpha
= gc
.getAlpha();
1243 long time0
= fTimeProvider
.getTime0();
1244 long time1
= fTimeProvider
.getTime1();
1245 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1246 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1247 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1248 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1249 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1251 // draw selection lines
1252 if (fDragState
!= DRAG_SELECTION
) {
1253 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1254 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1255 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1258 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1259 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1264 // draw selection background
1265 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1266 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1267 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1268 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1270 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1271 } else if (x0
- x1
> 1) {
1272 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1276 // draw drag selection background
1277 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1278 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1279 if (fDragX0
< fDragX
) {
1280 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1281 } else if (fDragX0
> fDragX
) {
1282 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1287 if (DRAG_SPLIT_LINE
== fDragState
) {
1288 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1289 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1290 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1291 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1292 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1293 if (fDragX
!= fDragX0
) {
1294 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1296 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1297 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1298 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1299 if (fDragX
!= fDragX0
) {
1300 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1302 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1303 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1304 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1311 * Draw many items at once
1314 * The rectangle of the area
1315 * @param timeProvider
1318 * The array items to draw
1320 * The index of the first element to draw
1322 * The width reserved for the names
1324 * Reference to the SWT GC object
1326 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1327 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1328 for (int i
= topIndex
; i
< items
.length
; i
++) {
1329 Item item
= items
[i
];
1330 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1337 * @param item the item to draw
1338 * @param bounds the container rectangle
1339 * @param timeProvider Time provider
1340 * @param i the item index
1341 * @param nameSpace the name space
1342 * @param gc Graphics context
1344 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1345 ITimeGraphEntry entry
= item
.fEntry
;
1346 long time0
= timeProvider
.getTime0();
1347 long time1
= timeProvider
.getTime1();
1348 long selectedTime
= fTimeProvider
.getSelectionBegin();
1350 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1351 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1355 if (! item
.fEntry
.hasTimeEvents()) {
1356 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1357 nameRect
.width
+= statesRect
.width
;
1358 drawName(item
, nameRect
, gc
);
1360 drawName(item
, nameRect
, gc
);
1362 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1363 if (rect
.isEmpty()) {
1364 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1367 if (time1
<= time0
) {
1368 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1369 gc
.fillRectangle(rect
);
1370 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1374 // Initialize _rect1 to same values as enclosing rectangle rect
1375 Rectangle stateRect
= Utils
.clone(rect
);
1376 boolean selected
= item
.fSelected
;
1377 // K pixels per second
1378 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1380 if (item
.fEntry
.hasTimeEvents()) {
1381 fillSpace(rect
, gc
, selected
);
1382 // Drawing rectangle is smaller than reserved space
1384 stateRect
.height
-= 6;
1386 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1387 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1390 while (iterator
.hasNext()) {
1391 ITimeEvent event
= iterator
.next();
1392 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1393 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1394 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1395 // event is out of bounds
1398 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1399 stateRect
.x
= Math
.max(rect
.x
, x
);
1400 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1401 if (stateRect
.x
== lastX
) {
1402 stateRect
.width
-= 1;
1403 if (stateRect
.width
> 0) {
1404 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1405 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1409 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1410 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1415 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1422 * The rectangle of the area
1423 * @param timeProvider
1426 * The array items to draw
1428 * The width reserved for the names
1430 * Reference to the SWT GC object
1433 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1434 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1438 for (ILinkEvent event
: links
) {
1439 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1444 * Draws the link type events of this item
1449 * the container rectangle
1450 * @param timeProvider
1458 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1459 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1460 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1462 if ((srcIndex
== -1) || (destIndex
== -1)) {
1466 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1467 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1469 int x0
= getXForTime(event
.getTime());
1470 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1472 // limit the x-coordinates to prevent integer overflow in calculations
1473 // and also GC.drawLine doesn't draw properly with large coordinates
1474 final int limit
= Integer
.MAX_VALUE
/ 1024;
1475 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1476 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1478 int y0
= src
.y
+ src
.height
/ 2;
1479 int y1
= dst
.y
+ dst
.height
/ 2;
1480 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1484 * Draw the state (color fill)
1489 * Time event for which we're drawing the state
1494 * @return true if the state was drawn
1497 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1498 Rectangle rect
, GC gc
) {
1500 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1504 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1507 Color stateColor
= null;
1508 if (colorIdx
< fEventColorMap
.length
) {
1509 stateColor
= fEventColorMap
[colorIdx
];
1511 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1514 gc
.setForeground(stateColor
);
1515 gc
.setBackground(stateColor
);
1517 /* Draw the arrow */
1518 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1519 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1522 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1527 * @author Francis Giraldeau
1530 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1532 * The algorithm was taken from this site, not the code itself
1534 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1537 double cos
= 0.9510;
1538 double sin
= 0.3090;
1539 long lenx
= x1
- x0
;
1540 long leny
= y1
- y0
;
1541 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1543 double dx
= factor
* lenx
/ len
;
1544 double dy
= factor
* leny
/ len
;
1545 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1546 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1547 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1548 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1549 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1550 gc
.fillPolygon(arrow
);
1554 * Draw the name of an item.
1559 * Where to draw the name
1563 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1564 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1565 if (! hasTimeEvents
) {
1566 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1567 gc
.fillRectangle(bounds
);
1568 if (item
.fSelected
&& fIsInFocus
) {
1569 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1570 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1573 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1574 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1575 gc
.fillRectangle(bounds
);
1578 // No name to be drawn
1579 if (fTimeProvider
.getNameSpace() == 0) {
1583 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1584 if (item
.fHasChildren
) {
1585 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1586 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1587 Rectangle rect
= Utils
.clone(bounds
);
1588 rect
.x
+= leftMargin
;
1589 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1590 rect
.width
= EXPAND_SIZE
;
1591 rect
.height
= EXPAND_SIZE
;
1592 gc
.fillRectangle(rect
);
1593 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1594 int midy
= rect
.y
+ rect
.height
/ 2;
1595 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1596 if (!item
.fExpanded
) {
1597 int midx
= rect
.x
+ rect
.width
/ 2;
1598 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1601 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1603 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1606 int imgHeight
= img
.getImageData().height
;
1607 int imgWidth
= img
.getImageData().width
;
1609 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1610 gc
.drawImage(img
, x
, y
);
1611 leftMargin
+= imgWidth
+ MARGIN
;
1613 String name
= item
.fName
;
1614 Point size
= gc
.stringExtent(name
);
1615 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1616 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1618 if (hasTimeEvents
) {
1619 // cut long string with "..."
1620 int width
= bounds
.width
- leftMargin
;
1622 while (size
.x
> width
&& name
.length() > 1) {
1624 name
= name
.substring(0, name
.length() - 1);
1625 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1628 name
+= "..."; //$NON-NLS-1$
1631 Rectangle rect
= Utils
.clone(bounds
);
1632 rect
.x
+= leftMargin
;
1633 rect
.width
-= leftMargin
;
1635 if (rect
.width
> 0) {
1636 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1637 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1638 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1639 leftMargin
+= textWidth
+ MARGIN
;
1642 if (hasTimeEvents
) {
1644 int x
= bounds
.x
+ leftMargin
;
1645 int width
= bounds
.width
- x
;
1646 int midy
= bounds
.y
+ bounds
.height
/ 2;
1647 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1648 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1654 * Draw the state (color fill)
1659 * Time event for which we're drawing the state
1665 * Is this time event currently selected (so it appears
1667 * @param timeSelected
1668 * Is the timestamp currently selected
1669 * @return true if the state was drawn
1672 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1673 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1675 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1676 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1679 boolean visible
= rect
.width
== 0 ?
false : true;
1680 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1681 gc
.setForeground(black
);
1684 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1685 // Only draw the top and bottom borders
1686 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1687 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1688 if (rect
.width
== 1) {
1689 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1693 Color stateColor
= null;
1694 if (colorIdx
< fEventColorMap
.length
) {
1695 stateColor
= fEventColorMap
[colorIdx
];
1700 boolean reallySelected
= timeSelected
&& selected
;
1701 // fill all rect area
1702 gc
.setBackground(stateColor
);
1703 gc
.fillRectangle(rect
);
1705 if (reallySelected
) {
1706 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1707 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1710 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1712 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1717 * Fill the space between two contiguous time events
1724 * Is this time event selected or not
1726 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1727 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1728 gc
.fillRectangle(rect
);
1729 if (fDragState
== DRAG_ZOOM
) {
1730 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1731 if (fDragX0
< fDragX
) {
1732 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1733 } else if (fDragX0
> fDragX
) {
1734 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1738 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1739 int midy
= rect
.y
+ rect
.height
/ 2;
1740 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1744 public void keyTraversed(TraverseEvent e
) {
1745 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1751 public void keyPressed(KeyEvent e
) {
1753 if (fItemData
.fExpandedItems
.length
== 0) {
1756 if (SWT
.HOME
== e
.keyCode
) {
1758 } else if (SWT
.END
== e
.keyCode
) {
1759 idx
= fItemData
.fExpandedItems
.length
- 1;
1760 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1761 idx
= getSelectedIndex();
1764 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1767 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1768 idx
= getSelectedIndex();
1771 } else if (idx
> 0) {
1774 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1776 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1778 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1779 int page
= countPerPage();
1780 idx
= getSelectedIndex();
1785 if (idx
>= fItemData
.fExpandedItems
.length
) {
1786 idx
= fItemData
.fExpandedItems
.length
- 1;
1788 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1789 int page
= countPerPage();
1790 idx
= getSelectedIndex();
1798 } else if (SWT
.CR
== e
.keyCode
) {
1799 idx
= getSelectedIndex();
1801 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1804 fireDefaultSelection();
1810 selectItem(idx
, false);
1811 fireSelectionChanged();
1813 int x
= toControl(e
.display
.getCursorLocation()).x
;
1814 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1818 public void keyReleased(KeyEvent e
) {
1819 int x
= toControl(e
.display
.getCursorLocation()).x
;
1820 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1824 public void focusGained(FocusEvent e
) {
1826 if (fMouseScrollFilterListener
== null) {
1827 fMouseScrollFilterListener
= new Listener() {
1828 // This filter is used to prevent horizontal scrolling of the view
1829 // when the mouse wheel is used to zoom
1831 public void handleEvent(Event event
) {
1835 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1838 updateStatusLine(NO_STATUS
);
1842 public void focusLost(FocusEvent e
) {
1844 if (fMouseScrollFilterListener
!= null) {
1845 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1846 fMouseScrollFilterListener
= null;
1848 if (DRAG_NONE
!= fDragState
) {
1850 fDragState
= DRAG_NONE
;
1853 updateStatusLine(NO_STATUS
);
1857 * @return If the current view is focused
1859 public boolean isInFocus() {
1864 * Provide the possibility to control the wait cursor externally e.g. data
1865 * requests in progress
1867 * @param waitInd Should we wait indefinitely?
1869 public void waitCursor(boolean waitInd
) {
1870 // Update cursor as indicated
1872 setCursor(fWaitCursor
);
1878 private void updateCursor(int x
, int stateMask
) {
1879 // if Wait cursor not active, check for the need to change the cursor
1880 if (getCursor() == fWaitCursor
) {
1883 Cursor cursor
= null;
1884 if (fDragState
== DRAG_SPLIT_LINE
) {
1885 } else if (fDragState
== DRAG_SELECTION
) {
1886 cursor
= fResizeCursor
;
1887 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1888 cursor
= fDragCursor
;
1889 } else if (fDragState
== DRAG_ZOOM
) {
1890 cursor
= fZoomCursor
;
1891 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1892 cursor
= fDragCursor
;
1893 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1894 cursor
= fResizeCursor
;
1895 } else if (!isOverSplitLine(x
)) {
1896 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1897 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1898 int xBegin
= getXForTime(selectionBegin
);
1899 int xEnd
= getXForTime(selectionEnd
);
1900 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1901 cursor
= fResizeCursor
;
1904 if (getCursor() != cursor
) {
1909 private void updateStatusLine(int x
) {
1910 if (fStatusLineManager
== null || null == fTimeProvider
||
1911 fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
1914 StringBuilder message
= new StringBuilder();
1915 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1916 long time
= getTimeAtX(x
);
1918 message
.append("T: "); //$NON-NLS-1$
1919 message
.append(new TmfNanoTimestamp(time
).toString());
1920 message
.append(" T1: "); //$NON-NLS-1$
1921 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1922 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1923 message
.append(new TmfNanoTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1924 if (selectionBegin
!= selectionEnd
) {
1925 message
.append(" T2: "); //$NON-NLS-1$
1926 message
.append(new TmfNanoTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1927 message
.append(" \u0394: "); //$NON-NLS-1$
1928 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1931 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1932 long time0
= fDragTime0
;
1933 long time
= getTimeAtX(fDragX
);
1934 message
.append("T1: "); //$NON-NLS-1$
1935 message
.append(new TmfNanoTimestamp(Math
.min(time
, time0
)).toString());
1936 if (time
!= time0
) {
1937 message
.append(" T2: "); //$NON-NLS-1$
1938 message
.append(new TmfNanoTimestamp(Math
.max(time
, time0
)).toString());
1939 message
.append(" \u0394: "); //$NON-NLS-1$
1940 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1943 fStatusLineManager
.setMessage(message
.toString());
1947 public void mouseMove(MouseEvent e
) {
1948 if (null == fTimeProvider
) {
1951 Point size
= getCtrlSize();
1952 if (DRAG_TRACE_ITEM
== fDragState
) {
1953 int nameWidth
= fTimeProvider
.getNameSpace();
1954 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1956 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1957 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1958 long time1
= fTime1bak
- timeDelta
;
1959 long maxTime
= fTimeProvider
.getMaxTime();
1960 if (time1
> maxTime
) {
1963 long time0
= time1
- (fTime1bak
- fTime0bak
);
1964 if (time0
< fTimeProvider
.getMinTime()) {
1965 time0
= fTimeProvider
.getMinTime();
1966 time1
= time0
+ (fTime1bak
- fTime0bak
);
1968 fTimeProvider
.setStartFinishTime(time0
, time1
);
1970 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1972 fTimeProvider
.setNameSpace(e
.x
);
1973 } else if (DRAG_SELECTION
== fDragState
) {
1974 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1976 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1977 } else if (DRAG_ZOOM
== fDragState
) {
1978 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1980 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1981 } else if (DRAG_NONE
== fDragState
) {
1982 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1983 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1986 fMouseOverSplitLine
= mouseOverSplitLine
;
1988 updateCursor(e
.x
, e
.stateMask
);
1989 updateStatusLine(e
.x
);
1993 public void mouseDoubleClick(MouseEvent e
) {
1994 if (null == fTimeProvider
) {
1997 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1998 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
1999 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2000 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2001 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2004 fMouseOverSplitLine
= mouseOverSplitLine
;
2007 int idx
= getItemIndexAtY(e
.y
);
2009 selectItem(idx
, false);
2010 fireDefaultSelection();
2016 public void mouseDown(MouseEvent e
) {
2017 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2018 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2019 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2023 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2024 int nameSpace
= fTimeProvider
.getNameSpace();
2025 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2026 fDragState
= DRAG_SPLIT_LINE
;
2027 fDragButton
= e
.button
;
2030 fTime0bak
= fTimeProvider
.getTime0();
2031 fTime1bak
= fTimeProvider
.getTime1();
2033 updateCursor(e
.x
, e
.stateMask
);
2037 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2038 int nameSpace
= fTimeProvider
.getNameSpace();
2039 idx
= getItemIndexAtY(e
.y
);
2041 Item item
= fItemData
.fExpandedItems
[idx
];
2042 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2046 selectItem(idx
, false);
2047 fireSelectionChanged();
2049 selectItem(idx
, false); // clear selection
2050 fireSelectionChanged();
2052 long hitTime
= getTimeAtX(e
.x
);
2056 fDragState
= DRAG_SELECTION
;
2057 fDragButton
= e
.button
;
2060 fDragTime0
= getTimeAtX(fDragX0
);
2061 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2062 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2063 int xBegin
= getXForTime(selectionBegin
);
2064 int xEnd
= getXForTime(selectionEnd
);
2065 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2066 long time
= getTimeAtX(e
.x
);
2067 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2069 fDragTime0
= selectionEnd
;
2072 fDragTime0
= selectionBegin
;
2075 long time
= getTimeAtX(e
.x
);
2076 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2078 fDragTime0
= selectionEnd
;
2079 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2081 fDragTime0
= selectionBegin
;
2084 fTime0bak
= fTimeProvider
.getTime0();
2085 fTime1bak
= fTimeProvider
.getTime1();
2087 updateCursor(e
.x
, e
.stateMask
);
2088 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2090 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2091 long hitTime
= getTimeAtX(e
.x
);
2094 fDragState
= DRAG_TRACE_ITEM
;
2095 fDragButton
= e
.button
;
2098 fTime0bak
= fTimeProvider
.getTime0();
2099 fTime1bak
= fTimeProvider
.getTime1();
2100 updateCursor(e
.x
, e
.stateMask
);
2102 } else if (3 == e
.button
) {
2104 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2106 fDragState
= DRAG_ZOOM
;
2107 fDragButton
= e
.button
;
2109 updateCursor(e
.x
, e
.stateMask
);
2110 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2115 public void mouseUp(MouseEvent e
) {
2116 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2117 menuDetected(fPendingMenuDetectEvent
);
2119 if (DRAG_NONE
!= fDragState
) {
2121 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2122 if (fDragX
!= fDragX0
) {
2123 fTimeProvider
.notifyStartFinishTime();
2125 fDragState
= DRAG_NONE
;
2126 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2127 fDragState
= DRAG_NONE
;
2129 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2130 if (fDragX
== fDragX0
) { // click without selecting anything
2131 long time
= getTimeAtX(e
.x
);
2132 fTimeProvider
.setSelectedTimeNotify(time
, false);
2134 long time0
= fDragTime0
;
2135 long time1
= getTimeAtX(fDragX
);
2136 if (time0
<= time1
) {
2137 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2139 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2142 fDragState
= DRAG_NONE
;
2144 fTimeGraphScale
.setDragRange(-1, -1);
2145 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2146 int nameWidth
= fTimeProvider
.getNameSpace();
2147 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2148 long time0
= getTimeAtX(fDragX0
);
2149 long time1
= getTimeAtX(fDragX
);
2150 if (time0
< time1
) {
2151 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2153 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2158 fDragState
= DRAG_NONE
;
2159 fTimeGraphScale
.setDragRange(-1, -1);
2162 updateCursor(e
.x
, e
.stateMask
);
2163 updateStatusLine(e
.x
);
2167 public void mouseEnter(MouseEvent e
) {
2171 public void mouseExit(MouseEvent e
) {
2172 if (fMouseOverSplitLine
) {
2173 fMouseOverSplitLine
= false;
2176 updateStatusLine(NO_STATUS
);
2180 public void mouseHover(MouseEvent e
) {
2184 public void mouseScrolled(MouseEvent e
) {
2185 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2188 boolean zoomScroll
= false;
2189 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2190 Point parentSize
= getParent().getSize();
2191 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2192 // over the parent control
2193 if (e
.x
> getCtrlSize().x
) {
2194 // over the horizontal scroll bar
2196 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2197 // over the name space
2203 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2206 } else if (e
.count
< 0) {
2210 setTopIndex(getTopIndex() - e
.count
);
2215 public void controlMoved(ControlEvent e
) {
2219 public void controlResized(ControlEvent e
) {
2224 public void widgetDefaultSelected(SelectionEvent e
) {
2228 public void widgetSelected(SelectionEvent e
) {
2229 if (e
.widget
== getVerticalBar()) {
2230 setTopIndex(getVerticalBar().getSelection());
2231 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2232 int start
= getHorizontalBar().getSelection();
2233 long time0
= fTimeProvider
.getTime0();
2234 long time1
= fTimeProvider
.getTime1();
2235 long timeMin
= fTimeProvider
.getMinTime();
2236 long timeMax
= fTimeProvider
.getMaxTime();
2237 long delta
= timeMax
- timeMin
;
2239 long range
= time1
- time0
;
2240 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2241 time1
= time0
+ range
;
2243 // TODO: Follow-up with Bug 310310
2244 // In Linux SWT.DRAG is the only value received
2245 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2246 if (e
.detail
== SWT
.DRAG
) {
2247 fTimeProvider
.setStartFinishTime(time0
, time1
);
2249 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2255 public int getBorderWidth() {
2256 return fBorderWidth
;
2260 * Set the border width
2262 * @param borderWidth
2265 public void setBorderWidth(int borderWidth
) {
2266 this.fBorderWidth
= borderWidth
;
2270 * @return The current height of the header row
2272 public int getHeaderHeight() {
2273 return fHeaderHeight
;
2277 * Set the height of the header row
2279 * @param headerHeight
2282 public void setHeaderHeight(int headerHeight
) {
2283 this.fHeaderHeight
= headerHeight
;
2287 * @return The default height of regular item rows
2289 public int getItemHeight() {
2290 return fGlobalItemHeight
;
2294 * Set the default height of regular item rows.
2299 public void setItemHeight(int rowHeight
) {
2300 this.fGlobalItemHeight
= rowHeight
;
2304 * Set the height of a specific item. Overrides the default item height.
2307 * A time graph entry
2310 * @return true if the height is successfully stored, false otherwise
2314 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2315 Item item
= fItemData
.findItem(entry
);
2317 item
.fItemHeight
= rowHeight
;
2324 * Set the minimum item width
2326 * @param width The minimum width
2328 public void setMinimumItemWidth(int width
) {
2329 this.fMinimumItemWidth
= width
;
2333 * @return The minimum item width
2335 public int getMinimumItemWidth() {
2336 return fMinimumItemWidth
;
2340 * @return The entries that are currently filtered out
2344 public List
<ITimeGraphEntry
> getFilteredOut() {
2345 return fItemData
.getFilteredOut();
2349 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2350 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2351 fSelectionChangedListeners
.add(listener
);
2356 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2357 if (listener
!= null) {
2358 fSelectionChangedListeners
.remove(listener
);
2363 public void setSelection(ISelection selection
) {
2364 if (selection
instanceof TimeGraphSelection
) {
2365 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2366 Object ob
= sel
.getFirstElement();
2367 if (ob
instanceof ITimeGraphEntry
) {
2368 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2369 selectItem(trace
, false);
2376 * @param filter The filter object to be attached to the view
2379 public void addFilter(ViewerFilter filter
) {
2380 if (!fFilters
.contains(filter
)) {
2381 fFilters
.add(filter
);
2386 * @param filter The filter object to be attached to the view
2389 public void removeFilter(ViewerFilter filter
) {
2390 fFilters
.remove(filter
);
2397 public void colorSettingsChanged(StateItem
[] stateItems
) {
2398 /* Destroy previous colors from the resource manager */
2399 if (fEventColorMap
!= null) {
2400 for (Color color
: fEventColorMap
) {
2401 fResourceManager
.destroyColor(color
.getRGB());
2404 if (stateItems
!= null) {
2405 fEventColorMap
= new Color
[stateItems
.length
];
2406 for (int i
= 0; i
< stateItems
.length
; i
++) {
2407 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2410 fEventColorMap
= new Color
[] { };
2415 private class ItemData
{
2416 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2417 private Item
[] fExpandedItems
= new Item
[0];
2418 private Item
[] fItems
= new Item
[0];
2419 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2420 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2421 private boolean fEntryFilter
[] = new boolean[0];
2422 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2427 public Item
findItem(ITimeGraphEntry entry
) {
2428 return fItemMap
.get(entry
);
2431 public int findItemIndex(ITimeGraphEntry entry
) {
2432 Item item
= fItemMap
.get(entry
);
2436 return item
.fExpandedIndex
;
2439 public void refreshData() {
2441 fFilteredOut
.clear();
2442 ITimeGraphEntry selection
= getSelectedTrace();
2443 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2444 ITimeGraphEntry entry
= fRootEntries
[i
];
2445 refreshData(fItemMap
, null, 0, entry
);
2447 fItems
= fItemMap
.values().toArray(new Item
[0]);
2448 updateExpandedItems();
2449 if (selection
!= null) {
2450 for (Item item
: fExpandedItems
) {
2451 if (item
.fEntry
== selection
) {
2452 item
.fSelected
= true;
2459 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2460 Item item
= new Item(entry
, entry
.getName(), level
);
2461 if (parent
!= null) {
2462 parent
.fChildren
.add(item
);
2464 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2465 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2467 item
.fItemHeight
= fGlobalItemHeight
;
2469 itemMap
.put(entry
, item
);
2470 if (entry
.hasChildren()) {
2471 item
.fExpanded
= true;
2472 item
.fHasChildren
= true;
2473 for (ITimeGraphEntry child
: entry
.getChildren()) {
2474 refreshData(itemMap
, item
, level
+ 1, child
);
2479 public void updateExpandedItems() {
2480 for (Item item
: fItems
) {
2481 item
.fExpandedIndex
= -1;
2483 List
<Item
> expandedItemList
= new ArrayList
<>();
2484 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2485 ITimeGraphEntry entry
= fRootEntries
[i
];
2486 Item item
= findItem(entry
);
2487 refreshExpanded(expandedItemList
, item
);
2489 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2490 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2493 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2494 // Check for filters
2495 boolean display
= true;
2496 for (ViewerFilter filter
: fFilters
) {
2497 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2503 item
.fExpandedIndex
= expandedItemList
.size();
2504 expandedItemList
.add(item
);
2505 if (item
.fHasChildren
&& item
.fExpanded
) {
2506 for (Item child
: item
.fChildren
) {
2507 refreshExpanded(expandedItemList
, child
);
2513 public void refreshData(ITimeGraphEntry
[] entries
) {
2514 if (entries
== null) {
2515 fEntryFilter
= null;
2516 fRootEntries
= null;
2518 if (entries
.length
== 0) {
2519 fEntryFilter
= null;
2520 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2521 fEntryFilter
= new boolean[entries
.length
];
2522 java
.util
.Arrays
.fill(fEntryFilter
, true);
2524 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2530 public void refreshArrows(List
<ILinkEvent
> events
) {
2531 /* If links are null, reset the list */
2532 if (events
!= null) {
2535 fLinks
= new ArrayList
<>();
2539 public ITimeGraphEntry
[] getEntries() {
2540 return fRootEntries
;
2543 public boolean[] getEntryFilter() {
2544 return fEntryFilter
;
2547 public List
<ITimeGraphEntry
> getFilteredOut() {
2548 return fFilteredOut
;
2552 private class Item
{
2553 private boolean fExpanded
;
2554 private int fExpandedIndex
;
2555 private boolean fSelected
;
2556 private boolean fHasChildren
;
2557 private int fItemHeight
;
2558 private final int fLevel
;
2559 private final List
<Item
> fChildren
;
2560 private final String fName
;
2561 private final ITimeGraphEntry fEntry
;
2563 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2564 this.fEntry
= entry
;
2566 this.fLevel
= level
;
2567 this.fChildren
= new ArrayList
<>();
2571 public String
toString() {
2580 public void menuDetected(MenuDetectEvent e
) {
2581 if (null == fTimeProvider
) {
2584 if (e
.detail
== SWT
.MENU_MOUSE
) {
2585 if (fPendingMenuDetectEvent
== null) {
2586 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2587 * Store the event and trigger it later just before handling mouseUp.
2588 * This allows for the method to detect if mouse is used to drag zoom.
2590 fPendingMenuDetectEvent
= e
;
2593 fPendingMenuDetectEvent
= null;
2594 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2598 if (fDragState
!= DRAG_NONE
) {
2602 Point p
= toControl(e
.x
, e
.y
);
2603 int idx
= getItemIndexAtY(p
.y
);
2604 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2605 Item item
= fItemData
.fExpandedItems
[idx
];
2606 ITimeGraphEntry entry
= item
.fEntry
;
2607 if (entry
.hasTimeEvents()) {
2608 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2609 if (event
!= null) {
2611 fireMenuEventOnTimeEvent(e
);
2616 fireMenuEventOnTimeGraphEntry(e
);