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
.AbstractTreeViewer
;
33 import org
.eclipse
.jface
.viewers
.ISelection
;
34 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
35 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
36 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
49 import org
.eclipse
.swt
.SWT
;
50 import org
.eclipse
.swt
.events
.ControlEvent
;
51 import org
.eclipse
.swt
.events
.ControlListener
;
52 import org
.eclipse
.swt
.events
.FocusEvent
;
53 import org
.eclipse
.swt
.events
.FocusListener
;
54 import org
.eclipse
.swt
.events
.KeyEvent
;
55 import org
.eclipse
.swt
.events
.KeyListener
;
56 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
57 import org
.eclipse
.swt
.events
.MenuDetectListener
;
58 import org
.eclipse
.swt
.events
.MouseEvent
;
59 import org
.eclipse
.swt
.events
.MouseListener
;
60 import org
.eclipse
.swt
.events
.MouseMoveListener
;
61 import org
.eclipse
.swt
.events
.MouseTrackListener
;
62 import org
.eclipse
.swt
.events
.MouseWheelListener
;
63 import org
.eclipse
.swt
.events
.PaintEvent
;
64 import org
.eclipse
.swt
.events
.SelectionEvent
;
65 import org
.eclipse
.swt
.events
.SelectionListener
;
66 import org
.eclipse
.swt
.events
.TraverseEvent
;
67 import org
.eclipse
.swt
.events
.TraverseListener
;
68 import org
.eclipse
.swt
.events
.TypedEvent
;
69 import org
.eclipse
.swt
.graphics
.Color
;
70 import org
.eclipse
.swt
.graphics
.Cursor
;
71 import org
.eclipse
.swt
.graphics
.GC
;
72 import org
.eclipse
.swt
.graphics
.Image
;
73 import org
.eclipse
.swt
.graphics
.Point
;
74 import org
.eclipse
.swt
.graphics
.Rectangle
;
75 import org
.eclipse
.swt
.widgets
.Composite
;
76 import org
.eclipse
.swt
.widgets
.Display
;
77 import org
.eclipse
.swt
.widgets
.Event
;
78 import org
.eclipse
.swt
.widgets
.Listener
;
79 import org
.eclipse
.swt
.widgets
.ScrollBar
;
82 * Time graph control implementation
85 * @author Alvaro Sanchez-Leon
86 * @author Patrick Tasse
88 public class TimeGraphControl
extends TimeGraphBaseControl
89 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
,
90 ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
91 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
{
93 /** Max scrollbar size */
94 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
96 /** Constant indicating that all levels of the time graph should be expanded
98 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
100 private static final int DRAG_NONE
= 0;
101 private static final int DRAG_TRACE_ITEM
= 1;
102 private static final int DRAG_SPLIT_LINE
= 2;
103 private static final int DRAG_ZOOM
= 3;
104 private static final int DRAG_SELECTION
= 4;
106 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
108 private static final double ZOOM_FACTOR
= 1.5;
109 private static final double ZOOM_IN_FACTOR
= 0.8;
110 private static final double ZOOM_OUT_FACTOR
= 1.25;
112 private static final int SNAP_WIDTH
= 2;
114 private static final int NO_STATUS
= -1;
116 /** Resource manager */
117 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
119 /** Color map for event types */
120 private Color
[] fEventColorMap
= null;
122 private ITimeDataProvider fTimeProvider
;
123 private IStatusLineManager fStatusLineManager
= null;
124 private TimeGraphScale fTimeGraphScale
= null;
126 private boolean fIsInFocus
= false;
127 private boolean fMouseOverSplitLine
= false;
128 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
129 private int fMinimumItemWidth
= 0;
130 private int fTopIndex
= 0;
131 private int fDragState
= DRAG_NONE
;
132 private int fDragButton
;
133 private int fDragX0
= 0;
134 private int fDragX
= 0;
135 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
136 private int fIdealNameSpace
= 0;
137 private long fTime0bak
;
138 private long fTime1bak
;
139 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
140 private ItemData fItemData
= null;
141 private List
<SelectionListener
> fSelectionListeners
;
142 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
143 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
144 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
145 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
146 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
147 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
148 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
149 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
150 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
151 private MenuDetectEvent fPendingMenuDetectEvent
= null;
152 private boolean fHideArrows
= false;
153 private int fAutoExpandLevel
= ALL_LEVELS
;
155 private int fBorderWidth
= 0;
156 private int fHeaderHeight
= 0;
158 private Listener fMouseScrollFilterListener
;
160 private MouseScrollNotifier fMouseScrollNotifier
;
161 private final Object fMouseScrollNotifierLock
= new Object();
163 private class MouseScrollNotifier
extends Thread
{
164 private static final long DELAY
= 400L;
165 private static final long POLLING_INTERVAL
= 10L;
166 private long fLastScrollTime
= Long
.MAX_VALUE
;
170 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
172 Thread
.sleep(POLLING_INTERVAL
);
173 } catch (Exception e
) {
177 if (!isInterrupted()) {
178 Display
.getDefault().asyncExec(new Runnable() {
184 fTimeProvider
.notifyStartFinishTime();
188 synchronized (fMouseScrollNotifierLock
) {
189 fMouseScrollNotifier
= null;
193 public void mouseScrolled() {
194 fLastScrollTime
= System
.currentTimeMillis();
199 * Standard constructor
202 * The parent composite object
204 * The color scheme to use
206 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
208 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
210 fItemData
= new ItemData();
212 addFocusListener(this);
213 addMouseListener(this);
214 addMouseMoveListener(this);
215 addMouseTrackListener(this);
216 addMouseWheelListener(this);
217 addTraverseListener(this);
218 addKeyListener(this);
219 addControlListener(this);
220 addMenuDetectListener(this);
221 ScrollBar scrollHor
= getHorizontalBar();
223 if (scrollHor
!= null) {
224 scrollHor
.addSelectionListener(this);
229 public void dispose() {
231 fResourceManager
.dispose();
235 * Sets the timegraph provider used by this timegraph viewer.
237 * @param timeGraphProvider the timegraph provider
239 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
240 fTimeGraphProvider
= timeGraphProvider
;
242 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
243 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
244 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
247 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
248 colorSettingsChanged(stateItems
);
252 * Gets the timegraph provider used by this timegraph viewer.
254 * @return the timegraph provider, or <code>null</code> if not set.
257 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
258 return fTimeGraphProvider
;
262 * Gets the color map used by this timegraph viewer.
264 * @return a color map, or <code>null</code> if not set.
267 public Color
[] getEventColorMap() {
268 return fEventColorMap
;
272 * Assign the given time provider
274 * @param timeProvider
277 public void setTimeProvider(ITimeDataProvider timeProvider
) {
278 fTimeProvider
= timeProvider
;
284 * Assign the status line manager
286 * @param statusLineManager
287 * The status line manager, or null to disable status line messages
290 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
291 if (fStatusLineManager
!= null && statusLineManager
== null) {
292 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
294 fStatusLineManager
= statusLineManager
;
298 * Assign the time graph scale
300 * @param timeGraphScale
301 * The time graph scale
304 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
305 fTimeGraphScale
= timeGraphScale
;
309 * Add a selection listener
312 * The listener to add
314 public void addSelectionListener(SelectionListener listener
) {
315 if (listener
== null) {
316 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
318 if (null == fSelectionListeners
) {
319 fSelectionListeners
= new ArrayList
<>();
321 fSelectionListeners
.add(listener
);
325 * Remove a selection listener
328 * The listener to remove
330 public void removeSelectionListener(SelectionListener listener
) {
331 if (null != fSelectionListeners
) {
332 fSelectionListeners
.remove(listener
);
337 * Selection changed callback
339 public void fireSelectionChanged() {
340 if (null != fSelectionListeners
) {
341 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
342 while (it
.hasNext()) {
343 SelectionListener listener
= it
.next();
344 listener
.widgetSelected(null);
350 * Default selection callback
352 public void fireDefaultSelection() {
353 if (null != fSelectionListeners
) {
354 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
355 while (it
.hasNext()) {
356 SelectionListener listener
= it
.next();
357 listener
.widgetDefaultSelected(null);
363 * Get the traces in the model
365 * @return The array of traces
367 public ITimeGraphEntry
[] getTraces() {
368 return fItemData
.getEntries();
372 * Get the on/off trace filters
374 * @return The array of filters
376 public boolean[] getTraceFilter() {
377 return fItemData
.getEntryFilter();
381 * Refresh the data for the thing
383 public void refreshData() {
384 fItemData
.refreshData();
390 * Refresh data for the given traces
393 * The traces to refresh
395 public void refreshData(ITimeGraphEntry
[] traces
) {
396 fItemData
.refreshData(traces
);
402 * Refresh the links (arrows) of this widget
404 * @param events The link events to refresh
407 public void refreshArrows(List
<ILinkEvent
> events
) {
408 fItemData
.refreshArrows(events
);
412 * Adjust the scoll bars
414 public void adjustScrolls() {
415 if (null == fTimeProvider
) {
416 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
422 long time0
= fTimeProvider
.getTime0();
423 long time1
= fTimeProvider
.getTime1();
425 long timeMin
= fTimeProvider
.getMinTime();
426 long timeMax
= fTimeProvider
.getMaxTime();
428 long delta
= timeMax
- timeMin
;
431 int thumb
= H_SCROLLBAR_MAX
;
434 // Thumb size (page size)
435 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
436 // At the beginning of visible window
437 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
440 // position, minimum, maximum, thumb size, increment (half page)t, page
441 // increment size (full page)
442 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
445 boolean ensureVisibleItem(int idx
, boolean redraw
) {
446 boolean changed
= false;
449 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
450 if (fItemData
.fExpandedItems
[index
].fSelected
) {
455 if (index
>= fItemData
.fExpandedItems
.length
) {
458 if (index
< fTopIndex
) {
465 int page
= countPerPage();
466 if (index
>= fTopIndex
+ page
) {
467 setTopIndex(index
- page
+ 1);
478 * Assign the given index as the top one
483 public void setTopIndex(int idx
) {
484 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
485 index
= Math
.max(0, index
);
491 * Sets the auto-expand level to be used when the entries are refreshed
492 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
493 * The value 0 means that there is no auto-expand; 1 means that top-level
494 * entries are expanded, but not their children; 2 means that top-level
495 * entries are expanded, and their children, but not grand-children; and so
498 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
501 * non-negative level, or <code>ALL_LEVELS</code> to expand all
505 public void setAutoExpandLevel(int level
) {
506 fAutoExpandLevel
= level
;
510 * Returns the auto-expand level.
512 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
513 * the tree are expanded automatically
514 * @see #setAutoExpandLevel
517 public int getAutoExpandLevel() {
518 return fAutoExpandLevel
;
522 * Set the expanded state of a given entry
527 * True if expanded, false if collapsed
529 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
530 Item item
= fItemData
.findItem(entry
);
531 if (item
!= null && item
.fExpanded
!= expanded
) {
532 item
.fExpanded
= expanded
;
533 fItemData
.updateExpandedItems();
539 * Collapses all nodes of the viewer's tree, starting with the root.
543 public void collapseAll() {
544 for (Item item
: fItemData
.fItems
) {
545 item
.fExpanded
= false;
547 fItemData
.updateExpandedItems();
552 * Expands all nodes of the viewer's tree, starting with the root.
556 public void expandAll() {
557 for (Item item
: fItemData
.fItems
) {
558 item
.fExpanded
= true;
560 fItemData
.updateExpandedItems();
565 * Add a tree listener
568 * The listener to add
570 public void addTreeListener(ITimeGraphTreeListener listener
) {
571 if (!fTreeListeners
.contains(listener
)) {
572 fTreeListeners
.add(listener
);
577 * Remove a tree listener
580 * The listener to remove
582 public void removeTreeListener(ITimeGraphTreeListener listener
) {
583 if (fTreeListeners
.contains(listener
)) {
584 fTreeListeners
.remove(listener
);
589 * Tree event callback
594 * The expanded state (true for expanded, false for collapsed)
596 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
597 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
598 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
600 listener
.treeExpanded(event
);
602 listener
.treeCollapsed(event
);
608 * Add a menu listener on {@link ITimeGraphEntry}s
610 * The listener to add
613 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
614 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
615 fTimeGraphEntryMenuListeners
.add(listener
);
620 * Remove a menu listener on {@link ITimeGraphEntry}s
623 * The listener to remove
626 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
627 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
628 fTimeGraphEntryMenuListeners
.remove(listener
);
633 * Menu event callback on {@link ITimeGraphEntry}s
636 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
638 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
639 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
640 listener
.menuDetected(event
);
645 * Add a menu listener on {@link ITimeEvent}s
648 * The listener to add
651 public void addTimeEventMenuListener(MenuDetectListener listener
) {
652 if (!fTimeEventMenuListeners
.contains(listener
)) {
653 fTimeEventMenuListeners
.add(listener
);
658 * Remove a menu listener on {@link ITimeEvent}s
661 * The listener to remove
664 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
665 if (fTimeEventMenuListeners
.contains(listener
)) {
666 fTimeEventMenuListeners
.remove(listener
);
671 * Menu event callback on {@link ITimeEvent}s
674 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
676 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
677 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
678 listener
.menuDetected(event
);
683 public ISelection
getSelection() {
684 TimeGraphSelection sel
= new TimeGraphSelection();
685 ITimeGraphEntry trace
= getSelectedTrace();
686 if (null != trace
&& null != fTimeProvider
) {
687 long selectedTime
= fTimeProvider
.getSelectionBegin();
688 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
699 * Get the selection object
701 * @return The selection
703 public ISelection
getSelectionTrace() {
704 TimeGraphSelection sel
= new TimeGraphSelection();
705 ITimeGraphEntry trace
= getSelectedTrace();
713 * Enable/disable one of the traces in the model
716 * 1 to enable it, -1 to disable. The method returns immediately
717 * if another value is used.
719 public void selectTrace(int n
) {
720 if ((n
!= 1) && (n
!= -1)) {
724 boolean changed
= false;
725 int lastSelection
= -1;
726 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
727 Item item
= fItemData
.fExpandedItems
[i
];
728 if (item
.fSelected
) {
730 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
731 item
.fSelected
= false;
732 item
= fItemData
.fExpandedItems
[i
+ 1];
733 item
.fSelected
= true;
735 } else if ((-1 == n
) && (i
> 0)) {
736 item
.fSelected
= false;
737 item
= fItemData
.fExpandedItems
[i
- 1];
738 item
.fSelected
= true;
745 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
746 Item item
= fItemData
.fExpandedItems
[0];
747 item
.fSelected
= true;
752 ensureVisibleItem(-1, false);
754 fireSelectionChanged();
762 * 1 for next event, -1 for previous event
764 public void selectEvent(int n
) {
765 if (null == fTimeProvider
) {
768 ITimeGraphEntry trace
= getSelectedTrace();
772 long selectedTime
= fTimeProvider
.getSelectionBegin();
773 long endTime
= fTimeProvider
.getEndTime();
774 ITimeEvent nextEvent
;
775 if (-1 == n
&& selectedTime
> endTime
) {
776 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
778 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
780 if (null == nextEvent
&& -1 == n
) {
781 nextEvent
= Utils
.getFirstEvent(trace
);
783 if (null != nextEvent
) {
784 long nextTime
= nextEvent
.getTime();
785 // If last event detected e.g. going back or not moving to a next
787 if (nextTime
<= selectedTime
&& n
== 1) {
788 // Select to the end of this last event
789 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
790 // but not beyond the end of the trace
791 if (nextTime
> endTime
) {
794 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
795 // for previous event go to its end time unless we were already there
796 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
798 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
799 fireSelectionChanged();
801 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
802 fireSelectionChanged();
807 * Select the next event
809 public void selectNextEvent() {
811 // Notify if visible time window has been adjusted
812 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
816 * Select the previous event
818 public void selectPrevEvent() {
820 // Notify if visible time window has been adjusted
821 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
825 * Select the next trace
827 public void selectNextTrace() {
832 * Select the previous trace
834 public void selectPrevTrace() {
839 * Zoom based on mouse cursor location with mouse scrolling
841 * @param zoomIn true to zoom in, false to zoom out
843 public void zoom(boolean zoomIn
) {
844 int globalX
= getDisplay().getCursorLocation().x
;
845 Point p
= toControl(globalX
, 0);
846 int nameSpace
= fTimeProvider
.getNameSpace();
847 int timeSpace
= fTimeProvider
.getTimeSpace();
848 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
849 long time0
= fTimeProvider
.getTime0();
850 long time1
= fTimeProvider
.getTime1();
851 long interval
= time1
- time0
;
854 } // to allow getting out of single point interval
857 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
859 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
861 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
862 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
863 long newTime1
= newTime0
+ newInterval
;
864 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
865 synchronized (fMouseScrollNotifierLock
) {
866 if (fMouseScrollNotifier
== null) {
867 fMouseScrollNotifier
= new MouseScrollNotifier();
868 fMouseScrollNotifier
.start();
870 fMouseScrollNotifier
.mouseScrolled();
875 * zoom in using single click
877 public void zoomIn() {
878 long prevTime0
= fTimeProvider
.getTime0();
879 long prevTime1
= fTimeProvider
.getTime1();
880 long prevRange
= prevTime1
- prevTime0
;
881 if (prevRange
== 0) {
884 ITimeDataProvider provider
= fTimeProvider
;
885 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
886 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
887 selTime
= (prevTime0
+ prevTime1
) / 2;
889 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
890 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
892 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
894 if (inaccuracy
> 0 && inaccuracy
< 100) {
895 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
899 long min
= fTimeProvider
.getMinTimeInterval();
900 if ((time1
- time0
) < min
) {
901 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
905 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
909 * zoom out using single click
911 public void zoomOut() {
912 long prevTime0
= fTimeProvider
.getTime0();
913 long prevTime1
= fTimeProvider
.getTime1();
914 ITimeDataProvider provider
= fTimeProvider
;
915 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
916 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
917 selTime
= (prevTime0
+ prevTime1
) / 2;
919 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
920 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
922 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
923 if (inaccuracy
> 0 && inaccuracy
< 100) {
924 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
928 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
934 * @param hideArrows true to hide arrows
938 public void hideArrows(boolean hideArrows
) {
939 fHideArrows
= hideArrows
;
943 * Follow the arrow forward
947 public void followArrowFwd() {
948 ITimeGraphEntry trace
= getSelectedTrace();
952 long selectedTime
= fTimeProvider
.getSelectionBegin();
953 for (ILinkEvent link
: fItemData
.fLinks
) {
954 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
955 selectItem(link
.getDestinationEntry(), false);
956 if (link
.getDuration() != 0) {
957 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
958 // Notify if visible time window has been adjusted
959 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
961 fireSelectionChanged();
969 * Follow the arrow backward
973 public void followArrowBwd() {
974 ITimeGraphEntry trace
= getSelectedTrace();
978 long selectedTime
= fTimeProvider
.getSelectionBegin();
979 for (ILinkEvent link
: fItemData
.fLinks
) {
980 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
981 selectItem(link
.getEntry(), false);
982 if (link
.getDuration() != 0) {
983 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
984 // Notify if visible time window has been adjusted
985 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
987 fireSelectionChanged();
995 * Return the currently selected trace
997 * @return The entry matching the trace
999 public ITimeGraphEntry
getSelectedTrace() {
1000 ITimeGraphEntry trace
= null;
1001 int idx
= getSelectedIndex();
1003 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1009 * Retrieve the index of the currently selected item
1013 public int getSelectedIndex() {
1015 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1016 Item item
= fItemData
.fExpandedItems
[i
];
1017 if (item
.fSelected
) {
1025 boolean toggle(int idx
) {
1026 boolean toggled
= false;
1027 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1028 Item item
= fItemData
.fExpandedItems
[idx
];
1029 if (item
.fHasChildren
) {
1030 item
.fExpanded
= !item
.fExpanded
;
1031 fItemData
.updateExpandedItems();
1035 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1042 * Gets the index of the item at the given location.
1046 * @return the index of the item at the given location, of -1 if none.
1049 protected int getItemIndexAtY(int y
) {
1054 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1055 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1063 boolean isOverSplitLine(int x
) {
1064 if (x
< 0 || null == fTimeProvider
) {
1067 int nameWidth
= fTimeProvider
.getNameSpace();
1068 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1072 * Gets the {@link ITimeGraphEntry} at the given location.
1075 * a point in the widget
1076 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1080 protected ITimeGraphEntry
getEntry(Point pt
) {
1081 int idx
= getItemIndexAtY(pt
.y
);
1082 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1089 public int getXForTime(long time
) {
1090 if (null == fTimeProvider
) {
1093 long time0
= fTimeProvider
.getTime0();
1094 long time1
= fTimeProvider
.getTime1();
1095 int width
= getCtrlSize().x
;
1096 int nameSpace
= fTimeProvider
.getNameSpace();
1097 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1098 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1106 public long getTimeAtX(int coord
) {
1107 if (null == fTimeProvider
) {
1111 Point size
= getCtrlSize();
1112 long time0
= fTimeProvider
.getTime0();
1113 long time1
= fTimeProvider
.getTime1();
1114 int nameWidth
= fTimeProvider
.getNameSpace();
1115 final int x
= coord
- nameWidth
;
1116 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1117 if (x
>= 0 && size
.x
>= nameWidth
) {
1118 if (time1
- time0
> timeWidth
) {
1119 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1120 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1122 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1123 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1129 void selectItem(int idx
, boolean addSelection
) {
1130 boolean changed
= false;
1132 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1133 Item item
= fItemData
.fExpandedItems
[idx
];
1134 changed
= !item
.fSelected
;
1135 item
.fSelected
= true;
1138 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1139 Item item
= fItemData
.fExpandedItems
[i
];
1140 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1143 item
.fSelected
= i
== idx
;
1146 changed
|= ensureVisibleItem(idx
, true);
1153 * Callback for item selection
1156 * The entry matching the trace
1157 * @param addSelection
1158 * If the selection is added or removed
1160 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1161 int idx
= fItemData
.findItemIndex(trace
);
1162 selectItem(idx
, addSelection
);
1166 * Retrieve the number of entries shown per page.
1170 public int countPerPage() {
1171 int height
= getCtrlSize().y
;
1174 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1175 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1176 if (ySum
>= height
) {
1181 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1182 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1183 if (ySum
>= height
) {
1192 * Get the index of the top element
1196 public int getTopIndex() {
1201 * Get the number of expanded items
1203 * @return The count of expanded items
1205 public int getExpandedElementCount() {
1206 return fItemData
.fExpandedItems
.length
;
1210 * Get an array of all expanded elements
1212 * @return The expanded elements
1214 public ITimeGraphEntry
[] getExpandedElements() {
1215 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1216 for (Item item
: fItemData
.fExpandedItems
) {
1217 elements
.add(item
.fEntry
);
1219 return elements
.toArray(new ITimeGraphEntry
[0]);
1222 Point
getCtrlSize() {
1223 Point size
= getSize();
1224 if (getHorizontalBar().isVisible()) {
1225 size
.y
-= getHorizontalBar().getSize().y
;
1230 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1231 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1233 rect
.width
= nameWidth
;
1237 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1238 int x
= bound
.x
+ nameWidth
;
1239 int width
= bound
.width
- x
;
1241 if (idx
>= fTopIndex
) {
1242 for (int i
= fTopIndex
; i
< idx
; i
++) {
1243 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1246 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1247 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1250 int y
= bound
.y
+ ySum
;
1251 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1252 return new Rectangle(x
, y
, width
, height
);
1256 void paint(Rectangle bounds
, PaintEvent e
) {
1258 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1259 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1261 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1265 fIdealNameSpace
= 0;
1266 int nameSpace
= fTimeProvider
.getNameSpace();
1268 // draw empty name space background
1269 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1270 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1273 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1274 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1275 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1277 int alpha
= gc
.getAlpha();
1280 long time0
= fTimeProvider
.getTime0();
1281 long time1
= fTimeProvider
.getTime1();
1282 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1283 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1284 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1285 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1286 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1288 // draw selection lines
1289 if (fDragState
!= DRAG_SELECTION
) {
1290 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1291 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1292 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1295 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1296 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1301 // draw selection background
1302 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1303 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1304 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1305 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1307 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1308 } else if (x0
- x1
> 1) {
1309 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1313 // draw drag selection background
1314 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1315 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1316 if (fDragX0
< fDragX
) {
1317 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1318 } else if (fDragX0
> fDragX
) {
1319 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1324 if (DRAG_SPLIT_LINE
== fDragState
) {
1325 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1326 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1327 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1328 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1329 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1330 if (fDragX
!= fDragX0
) {
1331 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1333 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1334 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1335 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1336 if (fDragX
!= fDragX0
) {
1337 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1339 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1340 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1341 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1348 * Draw many items at once
1351 * The rectangle of the area
1352 * @param timeProvider
1355 * The array items to draw
1357 * The index of the first element to draw
1359 * The width reserved for the names
1361 * Reference to the SWT GC object
1363 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1364 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1365 for (int i
= topIndex
; i
< items
.length
; i
++) {
1366 Item item
= items
[i
];
1367 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1374 * @param item the item to draw
1375 * @param bounds the container rectangle
1376 * @param timeProvider Time provider
1377 * @param i the item index
1378 * @param nameSpace the name space
1379 * @param gc Graphics context
1381 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1382 ITimeGraphEntry entry
= item
.fEntry
;
1383 long time0
= timeProvider
.getTime0();
1384 long time1
= timeProvider
.getTime1();
1385 long selectedTime
= fTimeProvider
.getSelectionBegin();
1387 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1388 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1392 if (! item
.fEntry
.hasTimeEvents()) {
1393 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1394 nameRect
.width
+= statesRect
.width
;
1395 drawName(item
, nameRect
, gc
);
1397 drawName(item
, nameRect
, gc
);
1399 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1400 if (rect
.isEmpty()) {
1401 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1404 if (time1
<= time0
) {
1405 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1406 gc
.fillRectangle(rect
);
1407 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1411 // Initialize _rect1 to same values as enclosing rectangle rect
1412 Rectangle stateRect
= Utils
.clone(rect
);
1413 boolean selected
= item
.fSelected
;
1414 // K pixels per second
1415 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1417 if (item
.fEntry
.hasTimeEvents()) {
1418 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1419 fillSpace(rect
, gc
, selected
);
1420 // Drawing rectangle is smaller than reserved space
1422 stateRect
.height
-= 6;
1424 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1425 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1428 while (iterator
.hasNext()) {
1429 ITimeEvent event
= iterator
.next();
1430 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1431 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1432 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1433 // event is out of bounds
1436 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1437 stateRect
.x
= Math
.max(rect
.x
, x
);
1438 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1439 if (stateRect
.x
== lastX
) {
1440 stateRect
.width
-= 1;
1441 if (stateRect
.width
> 0) {
1442 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1443 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1447 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1448 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1452 gc
.setClipping((Rectangle
) null);
1454 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1461 * The rectangle of the area
1462 * @param timeProvider
1465 * The array items to draw
1467 * The width reserved for the names
1469 * Reference to the SWT GC object
1472 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1473 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1477 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1478 for (ILinkEvent event
: links
) {
1479 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1481 gc
.setClipping((Rectangle
) null);
1485 * Draws the link type events of this item
1490 * the container rectangle
1491 * @param timeProvider
1499 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1500 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1501 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1503 if ((srcIndex
== -1) || (destIndex
== -1)) {
1507 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1508 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1510 int x0
= getXForTime(event
.getTime());
1511 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1513 // limit the x-coordinates to prevent integer overflow in calculations
1514 // and also GC.drawLine doesn't draw properly with large coordinates
1515 final int limit
= Integer
.MAX_VALUE
/ 1024;
1516 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1517 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1519 int y0
= src
.y
+ src
.height
/ 2;
1520 int y1
= dst
.y
+ dst
.height
/ 2;
1521 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1525 * Draw the state (color fill)
1530 * Time event for which we're drawing the state
1535 * @return true if the state was drawn
1538 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1539 Rectangle rect
, GC gc
) {
1541 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1545 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1548 Color stateColor
= null;
1549 if (colorIdx
< fEventColorMap
.length
) {
1550 stateColor
= fEventColorMap
[colorIdx
];
1552 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1555 gc
.setForeground(stateColor
);
1556 gc
.setBackground(stateColor
);
1558 /* Draw the arrow */
1559 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1560 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1563 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1568 * @author Francis Giraldeau
1571 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1573 * The algorithm was taken from this site, not the code itself
1575 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1578 double cos
= 0.9510;
1579 double sin
= 0.3090;
1580 long lenx
= x1
- x0
;
1581 long leny
= y1
- y0
;
1582 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1584 double dx
= factor
* lenx
/ len
;
1585 double dy
= factor
* leny
/ len
;
1586 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1587 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1588 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1589 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1590 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1591 gc
.fillPolygon(arrow
);
1595 * Draw the name of an item.
1600 * Where to draw the name
1604 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1605 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1606 if (! hasTimeEvents
) {
1607 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1608 gc
.fillRectangle(bounds
);
1609 if (item
.fSelected
&& fIsInFocus
) {
1610 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1611 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1614 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1615 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1616 gc
.fillRectangle(bounds
);
1619 // No name to be drawn
1620 if (fTimeProvider
.getNameSpace() == 0) {
1624 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1625 if (item
.fHasChildren
) {
1626 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1627 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1628 Rectangle rect
= Utils
.clone(bounds
);
1629 rect
.x
+= leftMargin
;
1630 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1631 rect
.width
= EXPAND_SIZE
;
1632 rect
.height
= EXPAND_SIZE
;
1633 gc
.fillRectangle(rect
);
1634 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1635 int midy
= rect
.y
+ rect
.height
/ 2;
1636 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1637 if (!item
.fExpanded
) {
1638 int midx
= rect
.x
+ rect
.width
/ 2;
1639 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1642 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1644 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1647 int imgHeight
= img
.getImageData().height
;
1648 int imgWidth
= img
.getImageData().width
;
1650 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1651 gc
.drawImage(img
, x
, y
);
1652 leftMargin
+= imgWidth
+ MARGIN
;
1654 String name
= item
.fName
;
1655 Point size
= gc
.stringExtent(name
);
1656 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1657 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1659 if (hasTimeEvents
) {
1660 // cut long string with "..."
1661 int width
= bounds
.width
- leftMargin
;
1663 while (size
.x
> width
&& name
.length() > 1) {
1665 name
= name
.substring(0, name
.length() - 1);
1666 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1669 name
+= "..."; //$NON-NLS-1$
1672 Rectangle rect
= Utils
.clone(bounds
);
1673 rect
.x
+= leftMargin
;
1674 rect
.width
-= leftMargin
;
1676 if (rect
.width
> 0) {
1677 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1678 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1679 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1680 leftMargin
+= textWidth
+ MARGIN
;
1683 if (hasTimeEvents
) {
1685 int x
= bounds
.x
+ leftMargin
;
1686 int width
= bounds
.width
- x
;
1687 int midy
= bounds
.y
+ bounds
.height
/ 2;
1688 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1689 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1695 * Draw the state (color fill)
1700 * Time event for which we're drawing the state
1706 * Is this time event currently selected (so it appears
1708 * @param timeSelected
1709 * Is the timestamp currently selected
1710 * @return true if the state was drawn
1713 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1714 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1716 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1717 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1720 boolean visible
= rect
.width
== 0 ?
false : true;
1721 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1722 gc
.setForeground(black
);
1725 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1726 // Only draw the top and bottom borders
1727 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1728 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1729 if (rect
.width
== 1) {
1730 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1734 Color stateColor
= null;
1735 if (colorIdx
< fEventColorMap
.length
) {
1736 stateColor
= fEventColorMap
[colorIdx
];
1741 boolean reallySelected
= timeSelected
&& selected
;
1742 // fill all rect area
1743 gc
.setBackground(stateColor
);
1744 gc
.fillRectangle(rect
);
1746 if (reallySelected
) {
1747 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1748 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1751 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1753 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1758 * Fill the space between two contiguous time events
1765 * Is this time event selected or not
1767 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1768 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1769 gc
.fillRectangle(rect
);
1770 if (fDragState
== DRAG_ZOOM
) {
1771 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1772 if (fDragX0
< fDragX
) {
1773 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1774 } else if (fDragX0
> fDragX
) {
1775 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1779 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1780 int midy
= rect
.y
+ rect
.height
/ 2;
1781 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1785 public void keyTraversed(TraverseEvent e
) {
1786 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1792 public void keyPressed(KeyEvent e
) {
1794 if (fItemData
.fExpandedItems
.length
== 0) {
1797 if (SWT
.HOME
== e
.keyCode
) {
1799 } else if (SWT
.END
== e
.keyCode
) {
1800 idx
= fItemData
.fExpandedItems
.length
- 1;
1801 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1802 idx
= getSelectedIndex();
1805 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1808 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1809 idx
= getSelectedIndex();
1812 } else if (idx
> 0) {
1815 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1817 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1819 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1820 int page
= countPerPage();
1821 idx
= getSelectedIndex();
1826 if (idx
>= fItemData
.fExpandedItems
.length
) {
1827 idx
= fItemData
.fExpandedItems
.length
- 1;
1829 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1830 int page
= countPerPage();
1831 idx
= getSelectedIndex();
1839 } else if (SWT
.CR
== e
.keyCode
) {
1840 idx
= getSelectedIndex();
1842 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1845 fireDefaultSelection();
1851 selectItem(idx
, false);
1852 fireSelectionChanged();
1854 int x
= toControl(e
.display
.getCursorLocation()).x
;
1855 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1859 public void keyReleased(KeyEvent e
) {
1860 int x
= toControl(e
.display
.getCursorLocation()).x
;
1861 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1865 public void focusGained(FocusEvent e
) {
1867 if (fMouseScrollFilterListener
== null) {
1868 fMouseScrollFilterListener
= new Listener() {
1869 // This filter is used to prevent horizontal scrolling of the view
1870 // when the mouse wheel is used to zoom
1872 public void handleEvent(Event event
) {
1876 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1879 updateStatusLine(NO_STATUS
);
1883 public void focusLost(FocusEvent e
) {
1885 if (fMouseScrollFilterListener
!= null) {
1886 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1887 fMouseScrollFilterListener
= null;
1889 if (DRAG_NONE
!= fDragState
) {
1891 fDragState
= DRAG_NONE
;
1894 updateStatusLine(NO_STATUS
);
1898 * @return If the current view is focused
1900 public boolean isInFocus() {
1905 * Provide the possibility to control the wait cursor externally e.g. data
1906 * requests in progress
1908 * @param waitInd Should we wait indefinitely?
1910 public void waitCursor(boolean waitInd
) {
1911 // Update cursor as indicated
1913 setCursor(fWaitCursor
);
1919 private void updateCursor(int x
, int stateMask
) {
1920 // if Wait cursor not active, check for the need to change the cursor
1921 if (getCursor() == fWaitCursor
) {
1924 Cursor cursor
= null;
1925 if (fDragState
== DRAG_SPLIT_LINE
) {
1926 } else if (fDragState
== DRAG_SELECTION
) {
1927 cursor
= fResizeCursor
;
1928 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1929 cursor
= fDragCursor
;
1930 } else if (fDragState
== DRAG_ZOOM
) {
1931 cursor
= fZoomCursor
;
1932 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1933 cursor
= fDragCursor
;
1934 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1935 cursor
= fResizeCursor
;
1936 } else if (!isOverSplitLine(x
)) {
1937 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1938 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1939 int xBegin
= getXForTime(selectionBegin
);
1940 int xEnd
= getXForTime(selectionEnd
);
1941 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1942 cursor
= fResizeCursor
;
1945 if (getCursor() != cursor
) {
1950 private void updateStatusLine(int x
) {
1951 if (fStatusLineManager
== null || null == fTimeProvider
||
1952 fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
1955 StringBuilder message
= new StringBuilder();
1956 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1957 long time
= getTimeAtX(x
);
1959 message
.append("T: "); //$NON-NLS-1$
1960 message
.append(new TmfNanoTimestamp(time
).toString());
1961 message
.append(" T1: "); //$NON-NLS-1$
1962 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1963 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1964 message
.append(new TmfNanoTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1965 if (selectionBegin
!= selectionEnd
) {
1966 message
.append(" T2: "); //$NON-NLS-1$
1967 message
.append(new TmfNanoTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1968 message
.append(" \u0394: "); //$NON-NLS-1$
1969 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1972 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1973 long time0
= fDragTime0
;
1974 long time
= getTimeAtX(fDragX
);
1975 message
.append("T1: "); //$NON-NLS-1$
1976 message
.append(new TmfNanoTimestamp(Math
.min(time
, time0
)).toString());
1977 if (time
!= time0
) {
1978 message
.append(" T2: "); //$NON-NLS-1$
1979 message
.append(new TmfNanoTimestamp(Math
.max(time
, time0
)).toString());
1980 message
.append(" \u0394: "); //$NON-NLS-1$
1981 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1984 fStatusLineManager
.setMessage(message
.toString());
1988 public void mouseMove(MouseEvent e
) {
1989 if (null == fTimeProvider
) {
1992 Point size
= getCtrlSize();
1993 if (DRAG_TRACE_ITEM
== fDragState
) {
1994 int nameWidth
= fTimeProvider
.getNameSpace();
1995 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1997 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1998 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1999 long time1
= fTime1bak
- timeDelta
;
2000 long maxTime
= fTimeProvider
.getMaxTime();
2001 if (time1
> maxTime
) {
2004 long time0
= time1
- (fTime1bak
- fTime0bak
);
2005 if (time0
< fTimeProvider
.getMinTime()) {
2006 time0
= fTimeProvider
.getMinTime();
2007 time1
= time0
+ (fTime1bak
- fTime0bak
);
2009 fTimeProvider
.setStartFinishTime(time0
, time1
);
2011 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2013 fTimeProvider
.setNameSpace(e
.x
);
2014 } else if (DRAG_SELECTION
== fDragState
) {
2015 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2017 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2018 } else if (DRAG_ZOOM
== fDragState
) {
2019 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2021 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2022 } else if (DRAG_NONE
== fDragState
) {
2023 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2024 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2027 fMouseOverSplitLine
= mouseOverSplitLine
;
2029 updateCursor(e
.x
, e
.stateMask
);
2030 updateStatusLine(e
.x
);
2034 public void mouseDoubleClick(MouseEvent e
) {
2035 if (null == fTimeProvider
) {
2038 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2039 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2040 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2041 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2042 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2045 fMouseOverSplitLine
= mouseOverSplitLine
;
2048 int idx
= getItemIndexAtY(e
.y
);
2050 selectItem(idx
, false);
2051 fireDefaultSelection();
2057 public void mouseDown(MouseEvent e
) {
2058 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2059 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2060 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2064 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2065 int nameSpace
= fTimeProvider
.getNameSpace();
2066 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2067 fDragState
= DRAG_SPLIT_LINE
;
2068 fDragButton
= e
.button
;
2071 fTime0bak
= fTimeProvider
.getTime0();
2072 fTime1bak
= fTimeProvider
.getTime1();
2074 updateCursor(e
.x
, e
.stateMask
);
2078 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2079 int nameSpace
= fTimeProvider
.getNameSpace();
2080 idx
= getItemIndexAtY(e
.y
);
2082 Item item
= fItemData
.fExpandedItems
[idx
];
2083 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2087 selectItem(idx
, false);
2088 fireSelectionChanged();
2090 selectItem(idx
, false); // clear selection
2091 fireSelectionChanged();
2093 long hitTime
= getTimeAtX(e
.x
);
2097 fDragState
= DRAG_SELECTION
;
2098 fDragButton
= e
.button
;
2101 fDragTime0
= getTimeAtX(fDragX0
);
2102 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2103 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2104 int xBegin
= getXForTime(selectionBegin
);
2105 int xEnd
= getXForTime(selectionEnd
);
2106 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2107 long time
= getTimeAtX(e
.x
);
2108 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2110 fDragTime0
= selectionEnd
;
2113 fDragTime0
= selectionBegin
;
2116 long time
= getTimeAtX(e
.x
);
2117 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2119 fDragTime0
= selectionEnd
;
2120 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2122 fDragTime0
= selectionBegin
;
2125 fTime0bak
= fTimeProvider
.getTime0();
2126 fTime1bak
= fTimeProvider
.getTime1();
2128 updateCursor(e
.x
, e
.stateMask
);
2129 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2131 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2132 long hitTime
= getTimeAtX(e
.x
);
2135 fDragState
= DRAG_TRACE_ITEM
;
2136 fDragButton
= e
.button
;
2139 fTime0bak
= fTimeProvider
.getTime0();
2140 fTime1bak
= fTimeProvider
.getTime1();
2141 updateCursor(e
.x
, e
.stateMask
);
2143 } else if (3 == e
.button
) {
2145 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2147 fDragState
= DRAG_ZOOM
;
2148 fDragButton
= e
.button
;
2150 updateCursor(e
.x
, e
.stateMask
);
2151 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2156 public void mouseUp(MouseEvent e
) {
2157 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2158 menuDetected(fPendingMenuDetectEvent
);
2160 if (DRAG_NONE
!= fDragState
) {
2162 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2163 if (fDragX
!= fDragX0
) {
2164 fTimeProvider
.notifyStartFinishTime();
2166 fDragState
= DRAG_NONE
;
2167 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2168 fDragState
= DRAG_NONE
;
2170 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2171 if (fDragX
== fDragX0
) { // click without selecting anything
2172 long time
= getTimeAtX(e
.x
);
2173 fTimeProvider
.setSelectedTimeNotify(time
, false);
2175 long time0
= fDragTime0
;
2176 long time1
= getTimeAtX(fDragX
);
2177 if (time0
<= time1
) {
2178 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2180 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2183 fDragState
= DRAG_NONE
;
2185 fTimeGraphScale
.setDragRange(-1, -1);
2186 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2187 int nameWidth
= fTimeProvider
.getNameSpace();
2188 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2189 long time0
= getTimeAtX(fDragX0
);
2190 long time1
= getTimeAtX(fDragX
);
2191 if (time0
< time1
) {
2192 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2194 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2199 fDragState
= DRAG_NONE
;
2200 fTimeGraphScale
.setDragRange(-1, -1);
2203 updateCursor(e
.x
, e
.stateMask
);
2204 updateStatusLine(e
.x
);
2208 public void mouseEnter(MouseEvent e
) {
2212 public void mouseExit(MouseEvent e
) {
2213 if (fMouseOverSplitLine
) {
2214 fMouseOverSplitLine
= false;
2217 updateStatusLine(NO_STATUS
);
2221 public void mouseHover(MouseEvent e
) {
2225 public void mouseScrolled(MouseEvent e
) {
2226 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2229 boolean zoomScroll
= false;
2230 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2231 Point parentSize
= getParent().getSize();
2232 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2233 // over the parent control
2234 if (e
.x
> getCtrlSize().x
) {
2235 // over the horizontal scroll bar
2237 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2238 // over the name space
2244 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2247 } else if (e
.count
< 0) {
2251 setTopIndex(getTopIndex() - e
.count
);
2256 public void controlMoved(ControlEvent e
) {
2260 public void controlResized(ControlEvent e
) {
2265 public void widgetDefaultSelected(SelectionEvent e
) {
2269 public void widgetSelected(SelectionEvent e
) {
2270 if (e
.widget
== getVerticalBar()) {
2271 setTopIndex(getVerticalBar().getSelection());
2272 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2273 int start
= getHorizontalBar().getSelection();
2274 long time0
= fTimeProvider
.getTime0();
2275 long time1
= fTimeProvider
.getTime1();
2276 long timeMin
= fTimeProvider
.getMinTime();
2277 long timeMax
= fTimeProvider
.getMaxTime();
2278 long delta
= timeMax
- timeMin
;
2280 long range
= time1
- time0
;
2281 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2282 time1
= time0
+ range
;
2284 // TODO: Follow-up with Bug 310310
2285 // In Linux SWT.DRAG is the only value received
2286 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2287 if (e
.detail
== SWT
.DRAG
) {
2288 fTimeProvider
.setStartFinishTime(time0
, time1
);
2290 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2296 public int getBorderWidth() {
2297 return fBorderWidth
;
2301 * Set the border width
2303 * @param borderWidth
2306 public void setBorderWidth(int borderWidth
) {
2307 this.fBorderWidth
= borderWidth
;
2311 * @return The current height of the header row
2313 public int getHeaderHeight() {
2314 return fHeaderHeight
;
2318 * Set the height of the header row
2320 * @param headerHeight
2323 public void setHeaderHeight(int headerHeight
) {
2324 this.fHeaderHeight
= headerHeight
;
2328 * @return The default height of regular item rows
2330 public int getItemHeight() {
2331 return fGlobalItemHeight
;
2335 * Set the default height of regular item rows.
2340 public void setItemHeight(int rowHeight
) {
2341 this.fGlobalItemHeight
= rowHeight
;
2345 * Set the height of a specific item. Overrides the default item height.
2348 * A time graph entry
2351 * @return true if the height is successfully stored, false otherwise
2355 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2356 Item item
= fItemData
.findItem(entry
);
2358 item
.fItemHeight
= rowHeight
;
2365 * Set the minimum item width
2367 * @param width The minimum width
2369 public void setMinimumItemWidth(int width
) {
2370 this.fMinimumItemWidth
= width
;
2374 * @return The minimum item width
2376 public int getMinimumItemWidth() {
2377 return fMinimumItemWidth
;
2381 * @return The entries that are currently filtered out
2385 public List
<ITimeGraphEntry
> getFilteredOut() {
2386 return fItemData
.getFilteredOut();
2390 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2391 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2392 fSelectionChangedListeners
.add(listener
);
2397 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2398 if (listener
!= null) {
2399 fSelectionChangedListeners
.remove(listener
);
2404 public void setSelection(ISelection selection
) {
2405 if (selection
instanceof TimeGraphSelection
) {
2406 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2407 Object ob
= sel
.getFirstElement();
2408 if (ob
instanceof ITimeGraphEntry
) {
2409 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2410 selectItem(trace
, false);
2417 * @param filter The filter object to be attached to the view
2420 public void addFilter(ViewerFilter filter
) {
2421 if (!fFilters
.contains(filter
)) {
2422 fFilters
.add(filter
);
2427 * @param filter The filter object to be attached to the view
2430 public void removeFilter(ViewerFilter filter
) {
2431 fFilters
.remove(filter
);
2438 public void colorSettingsChanged(StateItem
[] stateItems
) {
2439 /* Destroy previous colors from the resource manager */
2440 if (fEventColorMap
!= null) {
2441 for (Color color
: fEventColorMap
) {
2442 fResourceManager
.destroyColor(color
.getRGB());
2445 if (stateItems
!= null) {
2446 fEventColorMap
= new Color
[stateItems
.length
];
2447 for (int i
= 0; i
< stateItems
.length
; i
++) {
2448 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2451 fEventColorMap
= new Color
[] { };
2456 private class ItemData
{
2457 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2458 private Item
[] fExpandedItems
= new Item
[0];
2459 private Item
[] fItems
= new Item
[0];
2460 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2461 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2462 private boolean fEntryFilter
[] = new boolean[0];
2463 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2468 public Item
findItem(ITimeGraphEntry entry
) {
2469 return fItemMap
.get(entry
);
2472 public int findItemIndex(ITimeGraphEntry entry
) {
2473 Item item
= fItemMap
.get(entry
);
2477 return item
.fExpandedIndex
;
2480 public void refreshData() {
2482 fFilteredOut
.clear();
2483 ITimeGraphEntry selection
= getSelectedTrace();
2484 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2485 ITimeGraphEntry entry
= fRootEntries
[i
];
2486 refreshData(fItemMap
, null, 0, entry
);
2488 fItems
= fItemMap
.values().toArray(new Item
[0]);
2489 updateExpandedItems();
2490 if (selection
!= null) {
2491 for (Item item
: fExpandedItems
) {
2492 if (item
.fEntry
== selection
) {
2493 item
.fSelected
= true;
2500 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2501 Item item
= new Item(entry
, entry
.getName(), level
);
2502 if (parent
!= null) {
2503 parent
.fChildren
.add(item
);
2505 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2506 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2508 item
.fItemHeight
= fGlobalItemHeight
;
2510 itemMap
.put(entry
, item
);
2511 if (entry
.hasChildren()) {
2512 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2513 item
.fHasChildren
= true;
2514 for (ITimeGraphEntry child
: entry
.getChildren()) {
2515 refreshData(itemMap
, item
, level
+ 1, child
);
2520 public void updateExpandedItems() {
2521 for (Item item
: fItems
) {
2522 item
.fExpandedIndex
= -1;
2524 List
<Item
> expandedItemList
= new ArrayList
<>();
2525 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2526 ITimeGraphEntry entry
= fRootEntries
[i
];
2527 Item item
= findItem(entry
);
2528 refreshExpanded(expandedItemList
, item
);
2530 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2531 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2534 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2535 // Check for filters
2536 boolean display
= true;
2537 for (ViewerFilter filter
: fFilters
) {
2538 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2544 item
.fExpandedIndex
= expandedItemList
.size();
2545 expandedItemList
.add(item
);
2546 if (item
.fHasChildren
&& item
.fExpanded
) {
2547 for (Item child
: item
.fChildren
) {
2548 refreshExpanded(expandedItemList
, child
);
2554 public void refreshData(ITimeGraphEntry
[] entries
) {
2555 if (entries
== null) {
2556 fEntryFilter
= null;
2557 fRootEntries
= null;
2559 if (entries
.length
== 0) {
2560 fEntryFilter
= null;
2561 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2562 fEntryFilter
= new boolean[entries
.length
];
2563 java
.util
.Arrays
.fill(fEntryFilter
, true);
2565 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2571 public void refreshArrows(List
<ILinkEvent
> events
) {
2572 /* If links are null, reset the list */
2573 if (events
!= null) {
2576 fLinks
= new ArrayList
<>();
2580 public ITimeGraphEntry
[] getEntries() {
2581 return fRootEntries
;
2584 public boolean[] getEntryFilter() {
2585 return fEntryFilter
;
2588 public List
<ITimeGraphEntry
> getFilteredOut() {
2589 return fFilteredOut
;
2593 private class Item
{
2594 private boolean fExpanded
;
2595 private int fExpandedIndex
;
2596 private boolean fSelected
;
2597 private boolean fHasChildren
;
2598 private int fItemHeight
;
2599 private final int fLevel
;
2600 private final List
<Item
> fChildren
;
2601 private final String fName
;
2602 private final ITimeGraphEntry fEntry
;
2604 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2605 this.fEntry
= entry
;
2607 this.fLevel
= level
;
2608 this.fChildren
= new ArrayList
<>();
2612 public String
toString() {
2621 public void menuDetected(MenuDetectEvent e
) {
2622 if (null == fTimeProvider
) {
2625 if (e
.detail
== SWT
.MENU_MOUSE
) {
2626 if (fPendingMenuDetectEvent
== null) {
2627 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2628 * Store the event and trigger it later just before handling mouseUp.
2629 * This allows for the method to detect if mouse is used to drag zoom.
2631 fPendingMenuDetectEvent
= e
;
2634 fPendingMenuDetectEvent
= null;
2635 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2639 if (fDragState
!= DRAG_NONE
) {
2643 Point p
= toControl(e
.x
, e
.y
);
2644 int idx
= getItemIndexAtY(p
.y
);
2645 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2646 Item item
= fItemData
.fExpandedItems
[idx
];
2647 ITimeGraphEntry entry
= item
.fEntry
;
2648 if (entry
.hasTimeEvents()) {
2649 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2650 if (event
!= null) {
2652 fireMenuEventOnTimeEvent(e
);
2657 fireMenuEventOnTimeGraphEntry(e
);