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 fillSpace(rect
, gc
, selected
);
1419 // Drawing rectangle is smaller than reserved space
1421 stateRect
.height
-= 6;
1423 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1424 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1427 while (iterator
.hasNext()) {
1428 ITimeEvent event
= iterator
.next();
1429 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1430 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1431 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1432 // event is out of bounds
1435 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1436 stateRect
.x
= Math
.max(rect
.x
, x
);
1437 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1438 if (stateRect
.x
== lastX
) {
1439 stateRect
.width
-= 1;
1440 if (stateRect
.width
> 0) {
1441 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1442 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1446 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1447 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1452 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1459 * The rectangle of the area
1460 * @param timeProvider
1463 * The array items to draw
1465 * The width reserved for the names
1467 * Reference to the SWT GC object
1470 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1471 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1475 for (ILinkEvent event
: links
) {
1476 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1481 * Draws the link type events of this item
1486 * the container rectangle
1487 * @param timeProvider
1495 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1496 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1497 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1499 if ((srcIndex
== -1) || (destIndex
== -1)) {
1503 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1504 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1506 int x0
= getXForTime(event
.getTime());
1507 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1509 // limit the x-coordinates to prevent integer overflow in calculations
1510 // and also GC.drawLine doesn't draw properly with large coordinates
1511 final int limit
= Integer
.MAX_VALUE
/ 1024;
1512 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1513 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1515 int y0
= src
.y
+ src
.height
/ 2;
1516 int y1
= dst
.y
+ dst
.height
/ 2;
1517 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1521 * Draw the state (color fill)
1526 * Time event for which we're drawing the state
1531 * @return true if the state was drawn
1534 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1535 Rectangle rect
, GC gc
) {
1537 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1541 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1544 Color stateColor
= null;
1545 if (colorIdx
< fEventColorMap
.length
) {
1546 stateColor
= fEventColorMap
[colorIdx
];
1548 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1551 gc
.setForeground(stateColor
);
1552 gc
.setBackground(stateColor
);
1554 /* Draw the arrow */
1555 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1556 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1559 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1564 * @author Francis Giraldeau
1567 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1569 * The algorithm was taken from this site, not the code itself
1571 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1574 double cos
= 0.9510;
1575 double sin
= 0.3090;
1576 long lenx
= x1
- x0
;
1577 long leny
= y1
- y0
;
1578 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1580 double dx
= factor
* lenx
/ len
;
1581 double dy
= factor
* leny
/ len
;
1582 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1583 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1584 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1585 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1586 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1587 gc
.fillPolygon(arrow
);
1591 * Draw the name of an item.
1596 * Where to draw the name
1600 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1601 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1602 if (! hasTimeEvents
) {
1603 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1604 gc
.fillRectangle(bounds
);
1605 if (item
.fSelected
&& fIsInFocus
) {
1606 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1607 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1610 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1611 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1612 gc
.fillRectangle(bounds
);
1615 // No name to be drawn
1616 if (fTimeProvider
.getNameSpace() == 0) {
1620 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1621 if (item
.fHasChildren
) {
1622 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1623 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1624 Rectangle rect
= Utils
.clone(bounds
);
1625 rect
.x
+= leftMargin
;
1626 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1627 rect
.width
= EXPAND_SIZE
;
1628 rect
.height
= EXPAND_SIZE
;
1629 gc
.fillRectangle(rect
);
1630 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1631 int midy
= rect
.y
+ rect
.height
/ 2;
1632 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1633 if (!item
.fExpanded
) {
1634 int midx
= rect
.x
+ rect
.width
/ 2;
1635 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1638 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1640 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1643 int imgHeight
= img
.getImageData().height
;
1644 int imgWidth
= img
.getImageData().width
;
1646 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1647 gc
.drawImage(img
, x
, y
);
1648 leftMargin
+= imgWidth
+ MARGIN
;
1650 String name
= item
.fName
;
1651 Point size
= gc
.stringExtent(name
);
1652 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1653 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1655 if (hasTimeEvents
) {
1656 // cut long string with "..."
1657 int width
= bounds
.width
- leftMargin
;
1659 while (size
.x
> width
&& name
.length() > 1) {
1661 name
= name
.substring(0, name
.length() - 1);
1662 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1665 name
+= "..."; //$NON-NLS-1$
1668 Rectangle rect
= Utils
.clone(bounds
);
1669 rect
.x
+= leftMargin
;
1670 rect
.width
-= leftMargin
;
1672 if (rect
.width
> 0) {
1673 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1674 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1675 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1676 leftMargin
+= textWidth
+ MARGIN
;
1679 if (hasTimeEvents
) {
1681 int x
= bounds
.x
+ leftMargin
;
1682 int width
= bounds
.width
- x
;
1683 int midy
= bounds
.y
+ bounds
.height
/ 2;
1684 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1685 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1691 * Draw the state (color fill)
1696 * Time event for which we're drawing the state
1702 * Is this time event currently selected (so it appears
1704 * @param timeSelected
1705 * Is the timestamp currently selected
1706 * @return true if the state was drawn
1709 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1710 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1712 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1713 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1716 boolean visible
= rect
.width
== 0 ?
false : true;
1717 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1718 gc
.setForeground(black
);
1721 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1722 // Only draw the top and bottom borders
1723 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1724 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1725 if (rect
.width
== 1) {
1726 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1730 Color stateColor
= null;
1731 if (colorIdx
< fEventColorMap
.length
) {
1732 stateColor
= fEventColorMap
[colorIdx
];
1737 boolean reallySelected
= timeSelected
&& selected
;
1738 // fill all rect area
1739 gc
.setBackground(stateColor
);
1740 gc
.fillRectangle(rect
);
1742 if (reallySelected
) {
1743 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1744 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1747 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1749 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1754 * Fill the space between two contiguous time events
1761 * Is this time event selected or not
1763 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1764 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1765 gc
.fillRectangle(rect
);
1766 if (fDragState
== DRAG_ZOOM
) {
1767 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1768 if (fDragX0
< fDragX
) {
1769 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1770 } else if (fDragX0
> fDragX
) {
1771 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1775 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1776 int midy
= rect
.y
+ rect
.height
/ 2;
1777 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1781 public void keyTraversed(TraverseEvent e
) {
1782 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1788 public void keyPressed(KeyEvent e
) {
1790 if (fItemData
.fExpandedItems
.length
== 0) {
1793 if (SWT
.HOME
== e
.keyCode
) {
1795 } else if (SWT
.END
== e
.keyCode
) {
1796 idx
= fItemData
.fExpandedItems
.length
- 1;
1797 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1798 idx
= getSelectedIndex();
1801 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1804 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1805 idx
= getSelectedIndex();
1808 } else if (idx
> 0) {
1811 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1813 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1815 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1816 int page
= countPerPage();
1817 idx
= getSelectedIndex();
1822 if (idx
>= fItemData
.fExpandedItems
.length
) {
1823 idx
= fItemData
.fExpandedItems
.length
- 1;
1825 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1826 int page
= countPerPage();
1827 idx
= getSelectedIndex();
1835 } else if (SWT
.CR
== e
.keyCode
) {
1836 idx
= getSelectedIndex();
1838 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1841 fireDefaultSelection();
1847 selectItem(idx
, false);
1848 fireSelectionChanged();
1850 int x
= toControl(e
.display
.getCursorLocation()).x
;
1851 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1855 public void keyReleased(KeyEvent e
) {
1856 int x
= toControl(e
.display
.getCursorLocation()).x
;
1857 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1861 public void focusGained(FocusEvent e
) {
1863 if (fMouseScrollFilterListener
== null) {
1864 fMouseScrollFilterListener
= new Listener() {
1865 // This filter is used to prevent horizontal scrolling of the view
1866 // when the mouse wheel is used to zoom
1868 public void handleEvent(Event event
) {
1872 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1875 updateStatusLine(NO_STATUS
);
1879 public void focusLost(FocusEvent e
) {
1881 if (fMouseScrollFilterListener
!= null) {
1882 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1883 fMouseScrollFilterListener
= null;
1885 if (DRAG_NONE
!= fDragState
) {
1887 fDragState
= DRAG_NONE
;
1890 updateStatusLine(NO_STATUS
);
1894 * @return If the current view is focused
1896 public boolean isInFocus() {
1901 * Provide the possibility to control the wait cursor externally e.g. data
1902 * requests in progress
1904 * @param waitInd Should we wait indefinitely?
1906 public void waitCursor(boolean waitInd
) {
1907 // Update cursor as indicated
1909 setCursor(fWaitCursor
);
1915 private void updateCursor(int x
, int stateMask
) {
1916 // if Wait cursor not active, check for the need to change the cursor
1917 if (getCursor() == fWaitCursor
) {
1920 Cursor cursor
= null;
1921 if (fDragState
== DRAG_SPLIT_LINE
) {
1922 } else if (fDragState
== DRAG_SELECTION
) {
1923 cursor
= fResizeCursor
;
1924 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1925 cursor
= fDragCursor
;
1926 } else if (fDragState
== DRAG_ZOOM
) {
1927 cursor
= fZoomCursor
;
1928 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1929 cursor
= fDragCursor
;
1930 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1931 cursor
= fResizeCursor
;
1932 } else if (!isOverSplitLine(x
)) {
1933 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1934 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1935 int xBegin
= getXForTime(selectionBegin
);
1936 int xEnd
= getXForTime(selectionEnd
);
1937 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1938 cursor
= fResizeCursor
;
1941 if (getCursor() != cursor
) {
1946 private void updateStatusLine(int x
) {
1947 if (fStatusLineManager
== null || null == fTimeProvider
||
1948 fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
1951 StringBuilder message
= new StringBuilder();
1952 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1953 long time
= getTimeAtX(x
);
1955 message
.append("T: "); //$NON-NLS-1$
1956 message
.append(new TmfNanoTimestamp(time
).toString());
1957 message
.append(" T1: "); //$NON-NLS-1$
1958 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1959 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1960 message
.append(new TmfNanoTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1961 if (selectionBegin
!= selectionEnd
) {
1962 message
.append(" T2: "); //$NON-NLS-1$
1963 message
.append(new TmfNanoTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1964 message
.append(" \u0394: "); //$NON-NLS-1$
1965 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1968 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1969 long time0
= fDragTime0
;
1970 long time
= getTimeAtX(fDragX
);
1971 message
.append("T1: "); //$NON-NLS-1$
1972 message
.append(new TmfNanoTimestamp(Math
.min(time
, time0
)).toString());
1973 if (time
!= time0
) {
1974 message
.append(" T2: "); //$NON-NLS-1$
1975 message
.append(new TmfNanoTimestamp(Math
.max(time
, time0
)).toString());
1976 message
.append(" \u0394: "); //$NON-NLS-1$
1977 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1980 fStatusLineManager
.setMessage(message
.toString());
1984 public void mouseMove(MouseEvent e
) {
1985 if (null == fTimeProvider
) {
1988 Point size
= getCtrlSize();
1989 if (DRAG_TRACE_ITEM
== fDragState
) {
1990 int nameWidth
= fTimeProvider
.getNameSpace();
1991 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1993 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1994 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1995 long time1
= fTime1bak
- timeDelta
;
1996 long maxTime
= fTimeProvider
.getMaxTime();
1997 if (time1
> maxTime
) {
2000 long time0
= time1
- (fTime1bak
- fTime0bak
);
2001 if (time0
< fTimeProvider
.getMinTime()) {
2002 time0
= fTimeProvider
.getMinTime();
2003 time1
= time0
+ (fTime1bak
- fTime0bak
);
2005 fTimeProvider
.setStartFinishTime(time0
, time1
);
2007 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2009 fTimeProvider
.setNameSpace(e
.x
);
2010 } else if (DRAG_SELECTION
== fDragState
) {
2011 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2013 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2014 } else if (DRAG_ZOOM
== fDragState
) {
2015 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2017 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2018 } else if (DRAG_NONE
== fDragState
) {
2019 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2020 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2023 fMouseOverSplitLine
= mouseOverSplitLine
;
2025 updateCursor(e
.x
, e
.stateMask
);
2026 updateStatusLine(e
.x
);
2030 public void mouseDoubleClick(MouseEvent e
) {
2031 if (null == fTimeProvider
) {
2034 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2035 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2036 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2037 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2038 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2041 fMouseOverSplitLine
= mouseOverSplitLine
;
2044 int idx
= getItemIndexAtY(e
.y
);
2046 selectItem(idx
, false);
2047 fireDefaultSelection();
2053 public void mouseDown(MouseEvent e
) {
2054 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2055 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2056 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2060 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2061 int nameSpace
= fTimeProvider
.getNameSpace();
2062 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2063 fDragState
= DRAG_SPLIT_LINE
;
2064 fDragButton
= e
.button
;
2067 fTime0bak
= fTimeProvider
.getTime0();
2068 fTime1bak
= fTimeProvider
.getTime1();
2070 updateCursor(e
.x
, e
.stateMask
);
2074 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2075 int nameSpace
= fTimeProvider
.getNameSpace();
2076 idx
= getItemIndexAtY(e
.y
);
2078 Item item
= fItemData
.fExpandedItems
[idx
];
2079 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2083 selectItem(idx
, false);
2084 fireSelectionChanged();
2086 selectItem(idx
, false); // clear selection
2087 fireSelectionChanged();
2089 long hitTime
= getTimeAtX(e
.x
);
2093 fDragState
= DRAG_SELECTION
;
2094 fDragButton
= e
.button
;
2097 fDragTime0
= getTimeAtX(fDragX0
);
2098 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2099 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2100 int xBegin
= getXForTime(selectionBegin
);
2101 int xEnd
= getXForTime(selectionEnd
);
2102 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2103 long time
= getTimeAtX(e
.x
);
2104 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2106 fDragTime0
= selectionEnd
;
2109 fDragTime0
= selectionBegin
;
2112 long time
= getTimeAtX(e
.x
);
2113 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2115 fDragTime0
= selectionEnd
;
2116 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2118 fDragTime0
= selectionBegin
;
2121 fTime0bak
= fTimeProvider
.getTime0();
2122 fTime1bak
= fTimeProvider
.getTime1();
2124 updateCursor(e
.x
, e
.stateMask
);
2125 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2127 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2128 long hitTime
= getTimeAtX(e
.x
);
2131 fDragState
= DRAG_TRACE_ITEM
;
2132 fDragButton
= e
.button
;
2135 fTime0bak
= fTimeProvider
.getTime0();
2136 fTime1bak
= fTimeProvider
.getTime1();
2137 updateCursor(e
.x
, e
.stateMask
);
2139 } else if (3 == e
.button
) {
2141 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2143 fDragState
= DRAG_ZOOM
;
2144 fDragButton
= e
.button
;
2146 updateCursor(e
.x
, e
.stateMask
);
2147 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2152 public void mouseUp(MouseEvent e
) {
2153 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2154 menuDetected(fPendingMenuDetectEvent
);
2156 if (DRAG_NONE
!= fDragState
) {
2158 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2159 if (fDragX
!= fDragX0
) {
2160 fTimeProvider
.notifyStartFinishTime();
2162 fDragState
= DRAG_NONE
;
2163 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2164 fDragState
= DRAG_NONE
;
2166 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2167 if (fDragX
== fDragX0
) { // click without selecting anything
2168 long time
= getTimeAtX(e
.x
);
2169 fTimeProvider
.setSelectedTimeNotify(time
, false);
2171 long time0
= fDragTime0
;
2172 long time1
= getTimeAtX(fDragX
);
2173 if (time0
<= time1
) {
2174 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2176 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2179 fDragState
= DRAG_NONE
;
2181 fTimeGraphScale
.setDragRange(-1, -1);
2182 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2183 int nameWidth
= fTimeProvider
.getNameSpace();
2184 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2185 long time0
= getTimeAtX(fDragX0
);
2186 long time1
= getTimeAtX(fDragX
);
2187 if (time0
< time1
) {
2188 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2190 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2195 fDragState
= DRAG_NONE
;
2196 fTimeGraphScale
.setDragRange(-1, -1);
2199 updateCursor(e
.x
, e
.stateMask
);
2200 updateStatusLine(e
.x
);
2204 public void mouseEnter(MouseEvent e
) {
2208 public void mouseExit(MouseEvent e
) {
2209 if (fMouseOverSplitLine
) {
2210 fMouseOverSplitLine
= false;
2213 updateStatusLine(NO_STATUS
);
2217 public void mouseHover(MouseEvent e
) {
2221 public void mouseScrolled(MouseEvent e
) {
2222 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2225 boolean zoomScroll
= false;
2226 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2227 Point parentSize
= getParent().getSize();
2228 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2229 // over the parent control
2230 if (e
.x
> getCtrlSize().x
) {
2231 // over the horizontal scroll bar
2233 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2234 // over the name space
2240 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2243 } else if (e
.count
< 0) {
2247 setTopIndex(getTopIndex() - e
.count
);
2252 public void controlMoved(ControlEvent e
) {
2256 public void controlResized(ControlEvent e
) {
2261 public void widgetDefaultSelected(SelectionEvent e
) {
2265 public void widgetSelected(SelectionEvent e
) {
2266 if (e
.widget
== getVerticalBar()) {
2267 setTopIndex(getVerticalBar().getSelection());
2268 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2269 int start
= getHorizontalBar().getSelection();
2270 long time0
= fTimeProvider
.getTime0();
2271 long time1
= fTimeProvider
.getTime1();
2272 long timeMin
= fTimeProvider
.getMinTime();
2273 long timeMax
= fTimeProvider
.getMaxTime();
2274 long delta
= timeMax
- timeMin
;
2276 long range
= time1
- time0
;
2277 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2278 time1
= time0
+ range
;
2280 // TODO: Follow-up with Bug 310310
2281 // In Linux SWT.DRAG is the only value received
2282 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2283 if (e
.detail
== SWT
.DRAG
) {
2284 fTimeProvider
.setStartFinishTime(time0
, time1
);
2286 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2292 public int getBorderWidth() {
2293 return fBorderWidth
;
2297 * Set the border width
2299 * @param borderWidth
2302 public void setBorderWidth(int borderWidth
) {
2303 this.fBorderWidth
= borderWidth
;
2307 * @return The current height of the header row
2309 public int getHeaderHeight() {
2310 return fHeaderHeight
;
2314 * Set the height of the header row
2316 * @param headerHeight
2319 public void setHeaderHeight(int headerHeight
) {
2320 this.fHeaderHeight
= headerHeight
;
2324 * @return The default height of regular item rows
2326 public int getItemHeight() {
2327 return fGlobalItemHeight
;
2331 * Set the default height of regular item rows.
2336 public void setItemHeight(int rowHeight
) {
2337 this.fGlobalItemHeight
= rowHeight
;
2341 * Set the height of a specific item. Overrides the default item height.
2344 * A time graph entry
2347 * @return true if the height is successfully stored, false otherwise
2351 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2352 Item item
= fItemData
.findItem(entry
);
2354 item
.fItemHeight
= rowHeight
;
2361 * Set the minimum item width
2363 * @param width The minimum width
2365 public void setMinimumItemWidth(int width
) {
2366 this.fMinimumItemWidth
= width
;
2370 * @return The minimum item width
2372 public int getMinimumItemWidth() {
2373 return fMinimumItemWidth
;
2377 * @return The entries that are currently filtered out
2381 public List
<ITimeGraphEntry
> getFilteredOut() {
2382 return fItemData
.getFilteredOut();
2386 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2387 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2388 fSelectionChangedListeners
.add(listener
);
2393 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2394 if (listener
!= null) {
2395 fSelectionChangedListeners
.remove(listener
);
2400 public void setSelection(ISelection selection
) {
2401 if (selection
instanceof TimeGraphSelection
) {
2402 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2403 Object ob
= sel
.getFirstElement();
2404 if (ob
instanceof ITimeGraphEntry
) {
2405 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2406 selectItem(trace
, false);
2413 * @param filter The filter object to be attached to the view
2416 public void addFilter(ViewerFilter filter
) {
2417 if (!fFilters
.contains(filter
)) {
2418 fFilters
.add(filter
);
2423 * @param filter The filter object to be attached to the view
2426 public void removeFilter(ViewerFilter filter
) {
2427 fFilters
.remove(filter
);
2434 public void colorSettingsChanged(StateItem
[] stateItems
) {
2435 /* Destroy previous colors from the resource manager */
2436 if (fEventColorMap
!= null) {
2437 for (Color color
: fEventColorMap
) {
2438 fResourceManager
.destroyColor(color
.getRGB());
2441 if (stateItems
!= null) {
2442 fEventColorMap
= new Color
[stateItems
.length
];
2443 for (int i
= 0; i
< stateItems
.length
; i
++) {
2444 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2447 fEventColorMap
= new Color
[] { };
2452 private class ItemData
{
2453 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2454 private Item
[] fExpandedItems
= new Item
[0];
2455 private Item
[] fItems
= new Item
[0];
2456 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2457 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2458 private boolean fEntryFilter
[] = new boolean[0];
2459 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2464 public Item
findItem(ITimeGraphEntry entry
) {
2465 return fItemMap
.get(entry
);
2468 public int findItemIndex(ITimeGraphEntry entry
) {
2469 Item item
= fItemMap
.get(entry
);
2473 return item
.fExpandedIndex
;
2476 public void refreshData() {
2478 fFilteredOut
.clear();
2479 ITimeGraphEntry selection
= getSelectedTrace();
2480 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2481 ITimeGraphEntry entry
= fRootEntries
[i
];
2482 refreshData(fItemMap
, null, 0, entry
);
2484 fItems
= fItemMap
.values().toArray(new Item
[0]);
2485 updateExpandedItems();
2486 if (selection
!= null) {
2487 for (Item item
: fExpandedItems
) {
2488 if (item
.fEntry
== selection
) {
2489 item
.fSelected
= true;
2496 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2497 Item item
= new Item(entry
, entry
.getName(), level
);
2498 if (parent
!= null) {
2499 parent
.fChildren
.add(item
);
2501 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2502 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2504 item
.fItemHeight
= fGlobalItemHeight
;
2506 itemMap
.put(entry
, item
);
2507 if (entry
.hasChildren()) {
2508 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2509 item
.fHasChildren
= true;
2510 for (ITimeGraphEntry child
: entry
.getChildren()) {
2511 refreshData(itemMap
, item
, level
+ 1, child
);
2516 public void updateExpandedItems() {
2517 for (Item item
: fItems
) {
2518 item
.fExpandedIndex
= -1;
2520 List
<Item
> expandedItemList
= new ArrayList
<>();
2521 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2522 ITimeGraphEntry entry
= fRootEntries
[i
];
2523 Item item
= findItem(entry
);
2524 refreshExpanded(expandedItemList
, item
);
2526 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2527 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2530 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2531 // Check for filters
2532 boolean display
= true;
2533 for (ViewerFilter filter
: fFilters
) {
2534 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2540 item
.fExpandedIndex
= expandedItemList
.size();
2541 expandedItemList
.add(item
);
2542 if (item
.fHasChildren
&& item
.fExpanded
) {
2543 for (Item child
: item
.fChildren
) {
2544 refreshExpanded(expandedItemList
, child
);
2550 public void refreshData(ITimeGraphEntry
[] entries
) {
2551 if (entries
== null) {
2552 fEntryFilter
= null;
2553 fRootEntries
= null;
2555 if (entries
.length
== 0) {
2556 fEntryFilter
= null;
2557 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2558 fEntryFilter
= new boolean[entries
.length
];
2559 java
.util
.Arrays
.fill(fEntryFilter
, true);
2561 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2567 public void refreshArrows(List
<ILinkEvent
> events
) {
2568 /* If links are null, reset the list */
2569 if (events
!= null) {
2572 fLinks
= new ArrayList
<>();
2576 public ITimeGraphEntry
[] getEntries() {
2577 return fRootEntries
;
2580 public boolean[] getEntryFilter() {
2581 return fEntryFilter
;
2584 public List
<ITimeGraphEntry
> getFilteredOut() {
2585 return fFilteredOut
;
2589 private class Item
{
2590 private boolean fExpanded
;
2591 private int fExpandedIndex
;
2592 private boolean fSelected
;
2593 private boolean fHasChildren
;
2594 private int fItemHeight
;
2595 private final int fLevel
;
2596 private final List
<Item
> fChildren
;
2597 private final String fName
;
2598 private final ITimeGraphEntry fEntry
;
2600 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2601 this.fEntry
= entry
;
2603 this.fLevel
= level
;
2604 this.fChildren
= new ArrayList
<>();
2608 public String
toString() {
2617 public void menuDetected(MenuDetectEvent e
) {
2618 if (null == fTimeProvider
) {
2621 if (e
.detail
== SWT
.MENU_MOUSE
) {
2622 if (fPendingMenuDetectEvent
== null) {
2623 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2624 * Store the event and trigger it later just before handling mouseUp.
2625 * This allows for the method to detect if mouse is used to drag zoom.
2627 fPendingMenuDetectEvent
= e
;
2630 fPendingMenuDetectEvent
= null;
2631 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2635 if (fDragState
!= DRAG_NONE
) {
2639 Point p
= toControl(e
.x
, e
.y
);
2640 int idx
= getItemIndexAtY(p
.y
);
2641 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2642 Item item
= fItemData
.fExpandedItems
[idx
];
2643 ITimeGraphEntry entry
= item
.fEntry
;
2644 if (entry
.hasTimeEvents()) {
2645 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2646 if (event
!= null) {
2648 fireMenuEventOnTimeEvent(e
);
2653 fireMenuEventOnTimeGraphEntry(e
);