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 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Iterator
;
26 import java
.util
.LinkedHashMap
;
27 import java
.util
.List
;
30 import org
.eclipse
.jface
.action
.IStatusLineManager
;
31 import org
.eclipse
.jface
.resource
.JFaceResources
;
32 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
33 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
34 import org
.eclipse
.jface
.viewers
.ISelection
;
35 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
36 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
37 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
38 import org
.eclipse
.osgi
.util
.NLS
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.events
.ControlEvent
;
41 import org
.eclipse
.swt
.events
.ControlListener
;
42 import org
.eclipse
.swt
.events
.FocusEvent
;
43 import org
.eclipse
.swt
.events
.FocusListener
;
44 import org
.eclipse
.swt
.events
.KeyEvent
;
45 import org
.eclipse
.swt
.events
.KeyListener
;
46 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
47 import org
.eclipse
.swt
.events
.MenuDetectListener
;
48 import org
.eclipse
.swt
.events
.MouseEvent
;
49 import org
.eclipse
.swt
.events
.MouseListener
;
50 import org
.eclipse
.swt
.events
.MouseMoveListener
;
51 import org
.eclipse
.swt
.events
.MouseTrackListener
;
52 import org
.eclipse
.swt
.events
.MouseWheelListener
;
53 import org
.eclipse
.swt
.events
.PaintEvent
;
54 import org
.eclipse
.swt
.events
.SelectionEvent
;
55 import org
.eclipse
.swt
.events
.SelectionListener
;
56 import org
.eclipse
.swt
.events
.TraverseEvent
;
57 import org
.eclipse
.swt
.events
.TraverseListener
;
58 import org
.eclipse
.swt
.events
.TypedEvent
;
59 import org
.eclipse
.swt
.graphics
.Color
;
60 import org
.eclipse
.swt
.graphics
.Cursor
;
61 import org
.eclipse
.swt
.graphics
.GC
;
62 import org
.eclipse
.swt
.graphics
.Image
;
63 import org
.eclipse
.swt
.graphics
.Point
;
64 import org
.eclipse
.swt
.graphics
.Rectangle
;
65 import org
.eclipse
.swt
.widgets
.Composite
;
66 import org
.eclipse
.swt
.widgets
.Display
;
67 import org
.eclipse
.swt
.widgets
.Event
;
68 import org
.eclipse
.swt
.widgets
.Listener
;
69 import org
.eclipse
.swt
.widgets
.ScrollBar
;
70 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
71 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
85 * Time graph control implementation
88 * @author Alvaro Sanchez-Leon
89 * @author Patrick Tasse
91 public class TimeGraphControl
extends TimeGraphBaseControl
92 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
,
93 ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
94 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
96 /** Max scrollbar size */
97 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
99 /** Constant indicating that all levels of the time graph should be expanded
101 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
103 private static final int DRAG_NONE
= 0;
104 private static final int DRAG_TRACE_ITEM
= 1;
105 private static final int DRAG_SPLIT_LINE
= 2;
106 private static final int DRAG_ZOOM
= 3;
107 private static final int DRAG_SELECTION
= 4;
109 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
111 private static final double ZOOM_FACTOR
= 1.5;
112 private static final double ZOOM_IN_FACTOR
= 0.8;
113 private static final double ZOOM_OUT_FACTOR
= 1.25;
115 private static final int SNAP_WIDTH
= 2;
116 private static final int ARROW_HOVER_MAX_DIST
= 5;
118 private static final int NO_STATUS
= -1;
120 /** Resource manager */
121 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
123 /** Color map for event types */
124 private Color
[] fEventColorMap
= null;
126 private ITimeDataProvider fTimeProvider
;
127 private IStatusLineManager fStatusLineManager
= null;
128 private TimeGraphScale fTimeGraphScale
= null;
130 private boolean fIsInFocus
= false;
131 private boolean fMouseOverSplitLine
= false;
132 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
133 private int fMinimumItemWidth
= 0;
134 private int fTopIndex
= 0;
135 private int fDragState
= DRAG_NONE
;
136 private int fDragButton
;
137 private int fDragX0
= 0;
138 private int fDragX
= 0;
139 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
140 private int fIdealNameSpace
= 0;
141 private long fTime0bak
;
142 private long fTime1bak
;
143 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
144 private ItemData fItemData
= null;
145 private List
<SelectionListener
> fSelectionListeners
;
146 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
147 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
148 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
149 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
150 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
151 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
152 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
153 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
154 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
155 private final List
<ViewerFilter
> fFilters
= new ArrayList
<>();
156 private MenuDetectEvent fPendingMenuDetectEvent
= null;
157 private boolean fHideArrows
= false;
158 private int fAutoExpandLevel
= ALL_LEVELS
;
160 private int fBorderWidth
= 0;
161 private int fHeaderHeight
= 0;
163 private MouseScrollNotifier fMouseScrollNotifier
;
164 private final Object fMouseScrollNotifierLock
= new Object();
166 private class MouseScrollNotifier
extends Thread
{
167 private static final long DELAY
= 400L;
168 private static final long POLLING_INTERVAL
= 10L;
169 private long fLastScrollTime
= Long
.MAX_VALUE
;
173 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
175 Thread
.sleep(POLLING_INTERVAL
);
176 } catch (Exception e
) {
180 if (!isInterrupted()) {
181 Display
.getDefault().asyncExec(new Runnable() {
187 fTimeProvider
.notifyStartFinishTime();
191 synchronized (fMouseScrollNotifierLock
) {
192 fMouseScrollNotifier
= null;
196 public void mouseScrolled() {
197 fLastScrollTime
= System
.currentTimeMillis();
202 * Standard constructor
205 * The parent composite object
207 * The color scheme to use
209 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
211 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
213 fItemData
= new ItemData();
215 addFocusListener(this);
216 addMouseListener(this);
217 addMouseMoveListener(this);
218 addMouseTrackListener(this);
219 addMouseWheelListener(this);
220 addTraverseListener(this);
221 addKeyListener(this);
222 addControlListener(this);
223 addMenuDetectListener(this);
224 addListener(SWT
.MouseWheel
, this);
225 ScrollBar scrollHor
= getHorizontalBar();
227 if (scrollHor
!= null) {
228 scrollHor
.addSelectionListener(this);
233 public void dispose() {
235 fResourceManager
.dispose();
239 * Sets the timegraph provider used by this timegraph viewer.
241 * @param timeGraphProvider the timegraph provider
243 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
244 fTimeGraphProvider
= timeGraphProvider
;
246 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
247 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
248 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
251 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
252 colorSettingsChanged(stateItems
);
256 * Gets the timegraph provider used by this timegraph viewer.
258 * @return the timegraph provider, or <code>null</code> if not set.
261 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
262 return fTimeGraphProvider
;
266 * Gets the color map used by this timegraph viewer.
268 * @return a color map, or <code>null</code> if not set.
271 public Color
[] getEventColorMap() {
272 return fEventColorMap
;
276 * Assign the given time provider
278 * @param timeProvider
281 public void setTimeProvider(ITimeDataProvider timeProvider
) {
282 fTimeProvider
= timeProvider
;
288 * Assign the status line manager
290 * @param statusLineManager
291 * The status line manager, or null to disable status line messages
294 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
295 if (fStatusLineManager
!= null && statusLineManager
== null) {
296 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
298 fStatusLineManager
= statusLineManager
;
302 * Assign the time graph scale
304 * @param timeGraphScale
305 * The time graph scale
308 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
309 fTimeGraphScale
= timeGraphScale
;
313 * Add a selection listener
316 * The listener to add
318 public void addSelectionListener(SelectionListener listener
) {
319 if (listener
== null) {
320 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
322 if (null == fSelectionListeners
) {
323 fSelectionListeners
= new ArrayList
<>();
325 fSelectionListeners
.add(listener
);
329 * Remove a selection listener
332 * The listener to remove
334 public void removeSelectionListener(SelectionListener listener
) {
335 if (null != fSelectionListeners
) {
336 fSelectionListeners
.remove(listener
);
341 * Selection changed callback
343 public void fireSelectionChanged() {
344 if (null != fSelectionListeners
) {
345 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
346 while (it
.hasNext()) {
347 SelectionListener listener
= it
.next();
348 listener
.widgetSelected(null);
354 * Default selection callback
356 public void fireDefaultSelection() {
357 if (null != fSelectionListeners
) {
358 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
359 while (it
.hasNext()) {
360 SelectionListener listener
= it
.next();
361 listener
.widgetDefaultSelected(null);
367 * Add a drag selection listener
370 * The listener to add
373 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
374 if (listener
== null) {
375 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
377 if (null == fDragSelectionListeners
) {
378 fDragSelectionListeners
= new ArrayList
<>();
380 fDragSelectionListeners
.add(listener
);
384 * Remove a drag selection listener
387 * The listener to remove
390 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
391 if (null != fDragSelectionListeners
) {
392 fDragSelectionListeners
.remove(listener
);
397 * Drag Selection changed callback
400 * Time interval start
405 public void fireDragSelectionChanged(long start
, long end
) {
406 // check for backward intervals
407 long beginTime
, endTime
;
415 // call the listeners
416 if (null != fDragSelectionListeners
) {
417 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
418 while (it
.hasNext()) {
419 ITimeGraphTimeListener listener
= it
.next();
420 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
426 * Get the traces in the model
428 * @return The array of traces
430 public ITimeGraphEntry
[] getTraces() {
431 return fItemData
.getEntries();
435 * Get the on/off trace filters
437 * @return The array of filters
439 public boolean[] getTraceFilter() {
440 return fItemData
.getEntryFilter();
444 * Refresh the data for the thing
446 public void refreshData() {
447 fItemData
.refreshData();
453 * Refresh data for the given traces
456 * The traces to refresh
458 public void refreshData(ITimeGraphEntry
[] traces
) {
459 fItemData
.refreshData(traces
);
465 * Refresh the links (arrows) of this widget
467 * @param events The link events to refresh
470 public void refreshArrows(List
<ILinkEvent
> events
) {
471 fItemData
.refreshArrows(events
);
475 * Adjust the scoll bars
477 public void adjustScrolls() {
478 if (null == fTimeProvider
) {
479 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
485 long time0
= fTimeProvider
.getTime0();
486 long time1
= fTimeProvider
.getTime1();
488 long timeMin
= fTimeProvider
.getMinTime();
489 long timeMax
= fTimeProvider
.getMaxTime();
491 long delta
= timeMax
- timeMin
;
494 int thumb
= H_SCROLLBAR_MAX
;
497 // Thumb size (page size)
498 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
499 // At the beginning of visible window
500 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
503 // position, minimum, maximum, thumb size, increment (half page)t, page
504 // increment size (full page)
505 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
508 boolean ensureVisibleItem(int idx
, boolean redraw
) {
509 boolean changed
= false;
512 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
513 if (fItemData
.fExpandedItems
[index
].fSelected
) {
518 if (index
>= fItemData
.fExpandedItems
.length
) {
521 if (index
< fTopIndex
) {
528 int page
= countPerPage();
529 if (index
>= fTopIndex
+ page
) {
530 setTopIndex(index
- page
+ 1);
541 * Assign the given index as the top one
546 public void setTopIndex(int idx
) {
547 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
548 index
= Math
.max(0, index
);
554 * Sets the auto-expand level to be used when the entries are refreshed
555 * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}.
556 * The value 0 means that there is no auto-expand; 1 means that top-level
557 * entries are expanded, but not their children; 2 means that top-level
558 * entries are expanded, and their children, but not grand-children; and so
561 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
564 * non-negative level, or <code>ALL_LEVELS</code> to expand all
568 public void setAutoExpandLevel(int level
) {
569 fAutoExpandLevel
= level
;
573 * Returns the auto-expand level.
575 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
576 * the tree are expanded automatically
577 * @see #setAutoExpandLevel
580 public int getAutoExpandLevel() {
581 return fAutoExpandLevel
;
585 * Set the expanded state of a given entry
590 * True if expanded, false if collapsed
592 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
593 Item item
= fItemData
.findItem(entry
);
594 if (item
!= null && item
.fExpanded
!= expanded
) {
595 item
.fExpanded
= expanded
;
596 fItemData
.updateExpandedItems();
602 * Collapses all nodes of the viewer's tree, starting with the root.
606 public void collapseAll() {
607 for (Item item
: fItemData
.fItems
) {
608 item
.fExpanded
= false;
610 fItemData
.updateExpandedItems();
615 * Expands all nodes of the viewer's tree, starting with the root.
619 public void expandAll() {
620 for (Item item
: fItemData
.fItems
) {
621 item
.fExpanded
= true;
623 fItemData
.updateExpandedItems();
628 * Add a tree listener
631 * The listener to add
633 public void addTreeListener(ITimeGraphTreeListener listener
) {
634 if (!fTreeListeners
.contains(listener
)) {
635 fTreeListeners
.add(listener
);
640 * Remove a tree listener
643 * The listener to remove
645 public void removeTreeListener(ITimeGraphTreeListener listener
) {
646 if (fTreeListeners
.contains(listener
)) {
647 fTreeListeners
.remove(listener
);
652 * Tree event callback
657 * The expanded state (true for expanded, false for collapsed)
659 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
660 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
661 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
663 listener
.treeExpanded(event
);
665 listener
.treeCollapsed(event
);
671 * Add a menu listener on {@link ITimeGraphEntry}s
673 * The listener to add
676 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
677 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
678 fTimeGraphEntryMenuListeners
.add(listener
);
683 * Remove a menu listener on {@link ITimeGraphEntry}s
686 * The listener to remove
689 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
690 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
691 fTimeGraphEntryMenuListeners
.remove(listener
);
696 * Menu event callback on {@link ITimeGraphEntry}s
699 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
701 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
702 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
703 listener
.menuDetected(event
);
708 * Add a menu listener on {@link ITimeEvent}s
711 * The listener to add
714 public void addTimeEventMenuListener(MenuDetectListener listener
) {
715 if (!fTimeEventMenuListeners
.contains(listener
)) {
716 fTimeEventMenuListeners
.add(listener
);
721 * Remove a menu listener on {@link ITimeEvent}s
724 * The listener to remove
727 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
728 if (fTimeEventMenuListeners
.contains(listener
)) {
729 fTimeEventMenuListeners
.remove(listener
);
734 * Menu event callback on {@link ITimeEvent}s
737 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
739 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
740 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
741 listener
.menuDetected(event
);
746 public ISelection
getSelection() {
747 TimeGraphSelection sel
= new TimeGraphSelection();
748 ITimeGraphEntry trace
= getSelectedTrace();
749 if (null != trace
&& null != fTimeProvider
) {
750 long selectedTime
= fTimeProvider
.getSelectionBegin();
751 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
762 * Get the selection object
764 * @return The selection
766 public ISelection
getSelectionTrace() {
767 TimeGraphSelection sel
= new TimeGraphSelection();
768 ITimeGraphEntry trace
= getSelectedTrace();
776 * Enable/disable one of the traces in the model
779 * 1 to enable it, -1 to disable. The method returns immediately
780 * if another value is used.
782 public void selectTrace(int n
) {
783 if ((n
!= 1) && (n
!= -1)) {
787 boolean changed
= false;
788 int lastSelection
= -1;
789 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
790 Item item
= fItemData
.fExpandedItems
[i
];
791 if (item
.fSelected
) {
793 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
794 item
.fSelected
= false;
795 item
= fItemData
.fExpandedItems
[i
+ 1];
796 item
.fSelected
= true;
798 } else if ((-1 == n
) && (i
> 0)) {
799 item
.fSelected
= false;
800 item
= fItemData
.fExpandedItems
[i
- 1];
801 item
.fSelected
= true;
808 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
809 Item item
= fItemData
.fExpandedItems
[0];
810 item
.fSelected
= true;
815 ensureVisibleItem(-1, false);
817 fireSelectionChanged();
825 * 1 for next event, -1 for previous event
827 public void selectEvent(int n
) {
828 if (null == fTimeProvider
) {
831 ITimeGraphEntry trace
= getSelectedTrace();
835 long selectedTime
= fTimeProvider
.getSelectionBegin();
836 long endTime
= fTimeProvider
.getEndTime();
837 ITimeEvent nextEvent
;
838 if (-1 == n
&& selectedTime
> endTime
) {
839 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
841 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
843 if (null == nextEvent
&& -1 == n
) {
844 nextEvent
= Utils
.getFirstEvent(trace
);
846 if (null != nextEvent
) {
847 long nextTime
= nextEvent
.getTime();
848 // If last event detected e.g. going back or not moving to a next
850 if (nextTime
<= selectedTime
&& n
== 1) {
851 // Select to the end of this last event
852 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
853 // but not beyond the end of the trace
854 if (nextTime
> endTime
) {
857 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
858 // for previous event go to its end time unless we were already there
859 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
861 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
862 fireSelectionChanged();
864 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
865 fireSelectionChanged();
870 * Select the next event
872 public void selectNextEvent() {
874 // Notify if visible time window has been adjusted
875 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
879 * Select the previous event
881 public void selectPrevEvent() {
883 // Notify if visible time window has been adjusted
884 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
888 * Select the next trace
890 public void selectNextTrace() {
895 * Select the previous trace
897 public void selectPrevTrace() {
902 * Zoom based on mouse cursor location with mouse scrolling
904 * @param zoomIn true to zoom in, false to zoom out
906 public void zoom(boolean zoomIn
) {
907 int globalX
= getDisplay().getCursorLocation().x
;
908 Point p
= toControl(globalX
, 0);
909 int nameSpace
= fTimeProvider
.getNameSpace();
910 int timeSpace
= fTimeProvider
.getTimeSpace();
911 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
912 long time0
= fTimeProvider
.getTime0();
913 long time1
= fTimeProvider
.getTime1();
914 long interval
= time1
- time0
;
917 } // to allow getting out of single point interval
920 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
922 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
924 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
925 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
926 long newTime1
= newTime0
+ newInterval
;
927 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
928 synchronized (fMouseScrollNotifierLock
) {
929 if (fMouseScrollNotifier
== null) {
930 fMouseScrollNotifier
= new MouseScrollNotifier();
931 fMouseScrollNotifier
.start();
933 fMouseScrollNotifier
.mouseScrolled();
938 * zoom in using single click
940 public void zoomIn() {
941 long prevTime0
= fTimeProvider
.getTime0();
942 long prevTime1
= fTimeProvider
.getTime1();
943 long prevRange
= prevTime1
- prevTime0
;
944 if (prevRange
== 0) {
947 ITimeDataProvider provider
= fTimeProvider
;
948 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
949 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
950 selTime
= (prevTime0
+ prevTime1
) / 2;
952 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
953 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
955 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
957 if (inaccuracy
> 0 && inaccuracy
< 100) {
958 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
962 long min
= fTimeProvider
.getMinTimeInterval();
963 if ((time1
- time0
) < min
) {
964 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
968 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
972 * zoom out using single click
974 public void zoomOut() {
975 long prevTime0
= fTimeProvider
.getTime0();
976 long prevTime1
= fTimeProvider
.getTime1();
977 ITimeDataProvider provider
= fTimeProvider
;
978 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
979 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
980 selTime
= (prevTime0
+ prevTime1
) / 2;
982 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
983 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
985 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
986 if (inaccuracy
> 0 && inaccuracy
< 100) {
987 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
991 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
997 * @param hideArrows true to hide arrows
1001 public void hideArrows(boolean hideArrows
) {
1002 fHideArrows
= hideArrows
;
1006 * Follow the arrow forward
1010 public void followArrowFwd() {
1011 ITimeGraphEntry trace
= getSelectedTrace();
1012 if (trace
== null) {
1015 long selectedTime
= fTimeProvider
.getSelectionBegin();
1016 for (ILinkEvent link
: fItemData
.fLinks
) {
1017 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1018 selectItem(link
.getDestinationEntry(), false);
1019 if (link
.getDuration() != 0) {
1020 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1021 // Notify if visible time window has been adjusted
1022 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1024 fireSelectionChanged();
1032 * Follow the arrow backward
1036 public void followArrowBwd() {
1037 ITimeGraphEntry trace
= getSelectedTrace();
1038 if (trace
== null) {
1041 long selectedTime
= fTimeProvider
.getSelectionBegin();
1042 for (ILinkEvent link
: fItemData
.fLinks
) {
1043 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1044 selectItem(link
.getEntry(), false);
1045 if (link
.getDuration() != 0) {
1046 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1047 // Notify if visible time window has been adjusted
1048 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1050 fireSelectionChanged();
1058 * Return the currently selected trace
1060 * @return The entry matching the trace
1062 public ITimeGraphEntry
getSelectedTrace() {
1063 ITimeGraphEntry trace
= null;
1064 int idx
= getSelectedIndex();
1066 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1072 * Retrieve the index of the currently selected item
1076 public int getSelectedIndex() {
1078 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1079 Item item
= fItemData
.fExpandedItems
[i
];
1080 if (item
.fSelected
) {
1088 boolean toggle(int idx
) {
1089 boolean toggled
= false;
1090 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1091 Item item
= fItemData
.fExpandedItems
[idx
];
1092 if (item
.fHasChildren
) {
1093 item
.fExpanded
= !item
.fExpanded
;
1094 fItemData
.updateExpandedItems();
1098 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1105 * Gets the index of the item at the given location.
1109 * @return the index of the item at the given location, of -1 if none.
1112 protected int getItemIndexAtY(int y
) {
1117 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1118 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1126 boolean isOverSplitLine(int x
) {
1127 if (x
< 0 || null == fTimeProvider
) {
1130 int nameWidth
= fTimeProvider
.getNameSpace();
1131 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1135 * Gets the {@link ITimeGraphEntry} at the given location.
1138 * a point in the widget
1139 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1143 protected ITimeGraphEntry
getEntry(Point pt
) {
1144 int idx
= getItemIndexAtY(pt
.y
);
1145 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1149 * Return the arrow event closest to the given point that is no further than
1150 * a maximum distance.
1153 * a point in the widget
1154 * @return The closest arrow event, or null if there is none close enough.
1157 protected ILinkEvent
getArrow(Point pt
) {
1161 ILinkEvent linkEvent
= null;
1162 double minDistance
= Double
.MAX_VALUE
;
1163 for (ILinkEvent event
: fItemData
.fLinks
) {
1164 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1168 int x2
= x1
+ rect
.width
;
1169 int y2
= y1
+ rect
.height
;
1170 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1171 if (minDistance
> d
) {
1177 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1187 public int getXForTime(long time
) {
1188 if (null == fTimeProvider
) {
1191 long time0
= fTimeProvider
.getTime0();
1192 long time1
= fTimeProvider
.getTime1();
1193 int width
= getCtrlSize().x
;
1194 int nameSpace
= fTimeProvider
.getNameSpace();
1195 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1196 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1204 public long getTimeAtX(int coord
) {
1205 if (null == fTimeProvider
) {
1209 Point size
= getCtrlSize();
1210 long time0
= fTimeProvider
.getTime0();
1211 long time1
= fTimeProvider
.getTime1();
1212 int nameWidth
= fTimeProvider
.getNameSpace();
1213 final int x
= coord
- nameWidth
;
1214 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1215 if (x
>= 0 && size
.x
>= nameWidth
) {
1216 if (time1
- time0
> timeWidth
) {
1217 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1218 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1220 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1221 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1227 void selectItem(int idx
, boolean addSelection
) {
1228 boolean changed
= false;
1230 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1231 Item item
= fItemData
.fExpandedItems
[idx
];
1232 changed
= !item
.fSelected
;
1233 item
.fSelected
= true;
1236 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1237 Item item
= fItemData
.fExpandedItems
[i
];
1238 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1241 item
.fSelected
= i
== idx
;
1244 changed
|= ensureVisibleItem(idx
, true);
1251 * Callback for item selection
1254 * The entry matching the trace
1255 * @param addSelection
1256 * If the selection is added or removed
1258 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1259 int idx
= fItemData
.findItemIndex(trace
);
1260 selectItem(idx
, addSelection
);
1264 * Retrieve the number of entries shown per page.
1268 public int countPerPage() {
1269 int height
= getCtrlSize().y
;
1272 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1273 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1274 if (ySum
>= height
) {
1279 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1280 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1281 if (ySum
>= height
) {
1290 * Get the index of the top element
1294 public int getTopIndex() {
1299 * Get the number of expanded items
1301 * @return The count of expanded items
1303 public int getExpandedElementCount() {
1304 return fItemData
.fExpandedItems
.length
;
1308 * Get an array of all expanded elements
1310 * @return The expanded elements
1312 public ITimeGraphEntry
[] getExpandedElements() {
1313 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1314 for (Item item
: fItemData
.fExpandedItems
) {
1315 elements
.add(item
.fEntry
);
1317 return elements
.toArray(new ITimeGraphEntry
[0]);
1320 Point
getCtrlSize() {
1321 Point size
= getSize();
1322 if (getHorizontalBar().isVisible()) {
1323 size
.y
-= getHorizontalBar().getSize().y
;
1328 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1329 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1331 rect
.width
= nameWidth
;
1335 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1336 int x
= bound
.x
+ nameWidth
;
1337 int width
= bound
.width
- x
;
1339 if (idx
>= fTopIndex
) {
1340 for (int i
= fTopIndex
; i
< idx
; i
++) {
1341 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1344 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1345 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1348 int y
= bound
.y
+ ySum
;
1349 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1350 return new Rectangle(x
, y
, width
, height
);
1354 void paint(Rectangle bounds
, PaintEvent e
) {
1356 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1357 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1359 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1363 fIdealNameSpace
= 0;
1364 int nameSpace
= fTimeProvider
.getNameSpace();
1366 // draw empty name space background
1367 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1368 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1371 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1372 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1373 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1375 int alpha
= gc
.getAlpha();
1378 long time0
= fTimeProvider
.getTime0();
1379 long time1
= fTimeProvider
.getTime1();
1380 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1381 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1382 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1383 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1384 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1386 // draw selection lines
1387 if (fDragState
!= DRAG_SELECTION
) {
1388 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1389 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1390 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1393 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1394 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1399 // draw selection background
1400 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1401 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1402 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1403 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1405 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1406 } else if (x0
- x1
> 1) {
1407 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1411 // draw drag selection background
1412 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1413 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1414 if (fDragX0
< fDragX
) {
1415 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1416 } else if (fDragX0
> fDragX
) {
1417 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1422 if (DRAG_SPLIT_LINE
== fDragState
) {
1423 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1424 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1425 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1426 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1427 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1428 if (fDragX
!= fDragX0
) {
1429 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1431 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1432 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1433 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1434 if (fDragX
!= fDragX0
) {
1435 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1437 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1438 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1439 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1446 * Draw many items at once
1449 * The rectangle of the area
1450 * @param timeProvider
1453 * The array items to draw
1455 * The index of the first element to draw
1457 * The width reserved for the names
1459 * Reference to the SWT GC object
1461 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1462 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1463 for (int i
= topIndex
; i
< items
.length
; i
++) {
1464 Item item
= items
[i
];
1465 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1472 * @param item the item to draw
1473 * @param bounds the container rectangle
1474 * @param timeProvider Time provider
1475 * @param i the item index
1476 * @param nameSpace the name space
1477 * @param gc Graphics context
1479 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1480 ITimeGraphEntry entry
= item
.fEntry
;
1481 long time0
= timeProvider
.getTime0();
1482 long time1
= timeProvider
.getTime1();
1483 long selectedTime
= fTimeProvider
.getSelectionBegin();
1485 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1486 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1490 if (! item
.fEntry
.hasTimeEvents()) {
1491 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1492 nameRect
.width
+= statesRect
.width
;
1493 drawName(item
, nameRect
, gc
);
1495 drawName(item
, nameRect
, gc
);
1497 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1498 if (rect
.isEmpty()) {
1499 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1502 if (time1
<= time0
) {
1503 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1504 gc
.fillRectangle(rect
);
1505 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1509 // Initialize _rect1 to same values as enclosing rectangle rect
1510 Rectangle stateRect
= Utils
.clone(rect
);
1511 boolean selected
= item
.fSelected
;
1512 // K pixels per second
1513 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1515 if (item
.fEntry
.hasTimeEvents()) {
1516 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1517 fillSpace(rect
, gc
, selected
);
1518 // Drawing rectangle is smaller than reserved space
1520 stateRect
.height
-= 6;
1522 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1523 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1526 while (iterator
.hasNext()) {
1527 ITimeEvent event
= iterator
.next();
1528 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1529 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1530 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1531 // event is out of bounds
1534 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1535 stateRect
.x
= Math
.max(rect
.x
, x
);
1536 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1537 if (stateRect
.x
== lastX
) {
1538 stateRect
.width
-= 1;
1539 if (stateRect
.width
> 0) {
1540 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1541 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1545 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1546 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1550 gc
.setClipping((Rectangle
) null);
1552 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1559 * The rectangle of the area
1560 * @param timeProvider
1563 * The array items to draw
1565 * The width reserved for the names
1567 * Reference to the SWT GC object
1570 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1571 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1575 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1576 for (ILinkEvent event
: links
) {
1577 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1579 gc
.setClipping((Rectangle
) null);
1583 * Draws the link type events of this item
1588 * the container rectangle
1589 * @param timeProvider
1597 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1598 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1601 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1602 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1603 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1605 if ((srcIndex
== -1) || (destIndex
== -1)) {
1609 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1610 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1612 int x0
= getXForTime(event
.getTime());
1613 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1615 // limit the x-coordinates to prevent integer overflow in calculations
1616 // and also GC.drawLine doesn't draw properly with large coordinates
1617 final int limit
= Integer
.MAX_VALUE
/ 1024;
1618 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1619 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1621 int y0
= src
.y
+ src
.height
/ 2;
1622 int y1
= dst
.y
+ dst
.height
/ 2;
1623 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1627 * Draw the state (color fill)
1632 * Time event for which we're drawing the state
1637 * @return true if the state was drawn
1640 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1641 Rectangle rect
, GC gc
) {
1646 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1650 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1653 Color stateColor
= null;
1654 if (colorIdx
< fEventColorMap
.length
) {
1655 stateColor
= fEventColorMap
[colorIdx
];
1657 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1660 gc
.setForeground(stateColor
);
1661 gc
.setBackground(stateColor
);
1663 /* Draw the arrow */
1664 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1665 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1668 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1673 * @author Francis Giraldeau
1676 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1678 * The algorithm was taken from this site, not the code itself
1680 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1683 double cos
= 0.9510;
1684 double sin
= 0.3090;
1685 long lenx
= x1
- x0
;
1686 long leny
= y1
- y0
;
1687 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1689 double dx
= factor
* lenx
/ len
;
1690 double dy
= factor
* leny
/ len
;
1691 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1692 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1693 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1694 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1695 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1696 gc
.fillPolygon(arrow
);
1700 * Draw the name of an item.
1705 * Where to draw the name
1709 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1710 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1711 if (! hasTimeEvents
) {
1712 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1713 gc
.fillRectangle(bounds
);
1714 if (item
.fSelected
&& fIsInFocus
) {
1715 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1716 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1719 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1720 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1721 gc
.fillRectangle(bounds
);
1724 // No name to be drawn
1725 if (fTimeProvider
.getNameSpace() == 0) {
1729 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1730 if (item
.fHasChildren
) {
1731 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1732 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1733 Rectangle rect
= Utils
.clone(bounds
);
1734 rect
.x
+= leftMargin
;
1735 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1736 rect
.width
= EXPAND_SIZE
;
1737 rect
.height
= EXPAND_SIZE
;
1738 gc
.fillRectangle(rect
);
1739 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1740 int midy
= rect
.y
+ rect
.height
/ 2;
1741 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1742 if (!item
.fExpanded
) {
1743 int midx
= rect
.x
+ rect
.width
/ 2;
1744 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1747 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1749 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1752 int imgHeight
= img
.getImageData().height
;
1753 int imgWidth
= img
.getImageData().width
;
1755 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1756 gc
.drawImage(img
, x
, y
);
1757 leftMargin
+= imgWidth
+ MARGIN
;
1759 String name
= item
.fName
;
1760 Point size
= gc
.stringExtent(name
);
1761 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1762 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1764 if (hasTimeEvents
) {
1765 // cut long string with "..."
1766 int width
= bounds
.width
- leftMargin
;
1768 while (size
.x
> width
&& name
.length() > 1) {
1770 name
= name
.substring(0, name
.length() - 1);
1771 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1774 name
+= "..."; //$NON-NLS-1$
1777 Rectangle rect
= Utils
.clone(bounds
);
1778 rect
.x
+= leftMargin
;
1779 rect
.width
-= leftMargin
;
1781 if (rect
.width
> 0) {
1782 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1783 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1784 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1785 leftMargin
+= textWidth
+ MARGIN
;
1788 if (hasTimeEvents
) {
1790 int x
= bounds
.x
+ leftMargin
;
1791 int width
= bounds
.width
- x
;
1792 int midy
= bounds
.y
+ bounds
.height
/ 2;
1793 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1794 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1800 * Draw the state (color fill)
1805 * Time event for which we're drawing the state
1811 * Is this time event currently selected (so it appears
1813 * @param timeSelected
1814 * Is the timestamp currently selected
1815 * @return true if the state was drawn
1818 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1819 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1821 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1822 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1825 boolean visible
= rect
.width
== 0 ?
false : true;
1826 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1827 gc
.setForeground(black
);
1830 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1831 // Only draw the top and bottom borders
1832 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1833 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1834 if (rect
.width
== 1) {
1835 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1837 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1840 Color stateColor
= null;
1841 if (colorIdx
< fEventColorMap
.length
) {
1842 stateColor
= fEventColorMap
[colorIdx
];
1847 boolean reallySelected
= timeSelected
&& selected
;
1848 // fill all rect area
1849 gc
.setBackground(stateColor
);
1850 gc
.fillRectangle(rect
);
1852 if (reallySelected
) {
1853 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
1854 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
1857 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1859 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1864 * Fill the space between two contiguous time events
1871 * Is this time event selected or not
1873 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1874 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1875 gc
.fillRectangle(rect
);
1876 if (fDragState
== DRAG_ZOOM
) {
1877 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1878 if (fDragX0
< fDragX
) {
1879 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1880 } else if (fDragX0
> fDragX
) {
1881 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1885 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1886 int midy
= rect
.y
+ rect
.height
/ 2;
1887 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1891 public void keyTraversed(TraverseEvent e
) {
1892 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1898 public void keyPressed(KeyEvent e
) {
1900 if (fItemData
.fExpandedItems
.length
== 0) {
1903 if (SWT
.HOME
== e
.keyCode
) {
1905 } else if (SWT
.END
== e
.keyCode
) {
1906 idx
= fItemData
.fExpandedItems
.length
- 1;
1907 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1908 idx
= getSelectedIndex();
1911 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1914 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1915 idx
= getSelectedIndex();
1918 } else if (idx
> 0) {
1921 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1923 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1925 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1926 int page
= countPerPage();
1927 idx
= getSelectedIndex();
1932 if (idx
>= fItemData
.fExpandedItems
.length
) {
1933 idx
= fItemData
.fExpandedItems
.length
- 1;
1935 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1936 int page
= countPerPage();
1937 idx
= getSelectedIndex();
1945 } else if (SWT
.CR
== e
.keyCode
) {
1946 idx
= getSelectedIndex();
1948 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1951 fireDefaultSelection();
1957 selectItem(idx
, false);
1958 fireSelectionChanged();
1960 int x
= toControl(e
.display
.getCursorLocation()).x
;
1961 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1965 public void keyReleased(KeyEvent e
) {
1966 int x
= toControl(e
.display
.getCursorLocation()).x
;
1967 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1971 public void focusGained(FocusEvent e
) {
1974 updateStatusLine(NO_STATUS
);
1978 public void focusLost(FocusEvent e
) {
1980 if (DRAG_NONE
!= fDragState
) {
1982 fDragState
= DRAG_NONE
;
1985 updateStatusLine(NO_STATUS
);
1989 * @return If the current view is focused
1991 public boolean isInFocus() {
1996 * Provide the possibility to control the wait cursor externally e.g. data
1997 * requests in progress
1999 * @param waitInd Should we wait indefinitely?
2001 public void waitCursor(boolean waitInd
) {
2002 // Update cursor as indicated
2004 setCursor(fWaitCursor
);
2010 private void updateCursor(int x
, int stateMask
) {
2011 // if Wait cursor not active, check for the need to change the cursor
2012 if (getCursor() == fWaitCursor
) {
2015 Cursor cursor
= null;
2016 if (fDragState
== DRAG_SPLIT_LINE
) {
2017 } else if (fDragState
== DRAG_SELECTION
) {
2018 cursor
= fResizeCursor
;
2019 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2020 cursor
= fDragCursor
;
2021 } else if (fDragState
== DRAG_ZOOM
) {
2022 cursor
= fZoomCursor
;
2023 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2024 cursor
= fDragCursor
;
2025 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2026 cursor
= fResizeCursor
;
2027 } else if (!isOverSplitLine(x
)) {
2028 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2029 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2030 int xBegin
= getXForTime(selectionBegin
);
2031 int xEnd
= getXForTime(selectionEnd
);
2032 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2033 cursor
= fResizeCursor
;
2036 if (getCursor() != cursor
) {
2041 private void updateStatusLine(int x
) {
2042 // use the time provider of the time graph scale for the status line
2043 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2044 if (fStatusLineManager
== null || null == tdp
||
2045 tdp
.getTime0() == tdp
.getTime1()) {
2048 TimeFormat tf
= tdp
.getTimeFormat();
2049 Resolution res
= Resolution
.NANOSEC
;
2050 StringBuilder message
= new StringBuilder();
2051 if (x
>= 0 && fDragState
== DRAG_NONE
) {
2052 long time
= getTimeAtX(x
);
2054 if (tdp
instanceof ITimeDataProviderConverter
) {
2055 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2057 long selectionBegin
= tdp
.getSelectionBegin();
2058 long selectionEnd
= tdp
.getSelectionEnd();
2059 message
.append(NLS
.bind("T: {0}{1} T1: {2}{3}", //$NON-NLS-1$
2061 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2062 Utils
.formatTime(time
, tf
, res
),
2063 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
2064 Utils
.formatTime(Math
.min(selectionBegin
, selectionEnd
), tf
, res
)
2066 if (selectionBegin
!= selectionEnd
) {
2067 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2069 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(selectionBegin
, selectionEnd
)) + ' ' : "", //$NON-NLS-1$
2070 Utils
.formatTime(Math
.max(selectionBegin
, selectionEnd
), tf
, res
),
2071 Utils
.formatDelta(Math
.abs(selectionBegin
- selectionEnd
), tf
, res
)
2075 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2076 long time0
= fDragTime0
;
2077 long time
= getTimeAtX(fDragX
);
2078 if (tdp
instanceof ITimeDataProviderConverter
) {
2079 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2080 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2082 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2084 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.min(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2085 Utils
.formatTime(Math
.min(time
, time0
), tf
, res
),
2086 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(Math
.max(time
, time0
)) + ' ' : "", //$NON-NLS-1$
2087 Utils
.formatTime(Math
.max(time
, time0
), tf
, res
),
2088 Utils
.formatDelta(Math
.abs(time
- time0
), tf
, res
)
2091 fStatusLineManager
.setMessage(message
.toString());
2095 public void mouseMove(MouseEvent e
) {
2096 if (null == fTimeProvider
) {
2099 Point size
= getCtrlSize();
2100 if (DRAG_TRACE_ITEM
== fDragState
) {
2101 int nameWidth
= fTimeProvider
.getNameSpace();
2102 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2104 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2105 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2106 long time1
= fTime1bak
- timeDelta
;
2107 long maxTime
= fTimeProvider
.getMaxTime();
2108 if (time1
> maxTime
) {
2111 long time0
= time1
- (fTime1bak
- fTime0bak
);
2112 if (time0
< fTimeProvider
.getMinTime()) {
2113 time0
= fTimeProvider
.getMinTime();
2114 time1
= time0
+ (fTime1bak
- fTime0bak
);
2116 fTimeProvider
.setStartFinishTime(time0
, time1
);
2118 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2120 fTimeProvider
.setNameSpace(e
.x
);
2121 } else if (DRAG_SELECTION
== fDragState
) {
2122 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2124 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2125 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2126 } else if (DRAG_ZOOM
== fDragState
) {
2127 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2129 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2130 } else if (DRAG_NONE
== fDragState
) {
2131 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2132 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2135 fMouseOverSplitLine
= mouseOverSplitLine
;
2137 updateCursor(e
.x
, e
.stateMask
);
2138 updateStatusLine(e
.x
);
2142 public void mouseDoubleClick(MouseEvent e
) {
2143 if (null == fTimeProvider
) {
2146 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2147 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2148 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2149 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2150 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2153 fMouseOverSplitLine
= mouseOverSplitLine
;
2156 int idx
= getItemIndexAtY(e
.y
);
2158 selectItem(idx
, false);
2159 fireDefaultSelection();
2165 public void mouseDown(MouseEvent e
) {
2166 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2167 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2168 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2172 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2173 int nameSpace
= fTimeProvider
.getNameSpace();
2174 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2175 fDragState
= DRAG_SPLIT_LINE
;
2176 fDragButton
= e
.button
;
2179 fTime0bak
= fTimeProvider
.getTime0();
2180 fTime1bak
= fTimeProvider
.getTime1();
2182 updateCursor(e
.x
, e
.stateMask
);
2186 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2187 int nameSpace
= fTimeProvider
.getNameSpace();
2188 idx
= getItemIndexAtY(e
.y
);
2190 Item item
= fItemData
.fExpandedItems
[idx
];
2191 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2195 selectItem(idx
, false);
2196 fireSelectionChanged();
2198 selectItem(idx
, false); // clear selection
2199 fireSelectionChanged();
2201 long hitTime
= getTimeAtX(e
.x
);
2205 fDragState
= DRAG_SELECTION
;
2206 fDragButton
= e
.button
;
2209 fDragTime0
= getTimeAtX(fDragX0
);
2210 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2211 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2212 int xBegin
= getXForTime(selectionBegin
);
2213 int xEnd
= getXForTime(selectionEnd
);
2214 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2215 long time
= getTimeAtX(e
.x
);
2216 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2218 fDragTime0
= selectionEnd
;
2221 fDragTime0
= selectionBegin
;
2224 long time
= getTimeAtX(e
.x
);
2225 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2227 fDragTime0
= selectionEnd
;
2228 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2230 fDragTime0
= selectionBegin
;
2233 fTime0bak
= fTimeProvider
.getTime0();
2234 fTime1bak
= fTimeProvider
.getTime1();
2236 updateCursor(e
.x
, e
.stateMask
);
2237 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2239 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2240 long hitTime
= getTimeAtX(e
.x
);
2243 fDragState
= DRAG_TRACE_ITEM
;
2244 fDragButton
= e
.button
;
2247 fTime0bak
= fTimeProvider
.getTime0();
2248 fTime1bak
= fTimeProvider
.getTime1();
2249 updateCursor(e
.x
, e
.stateMask
);
2251 } else if (3 == e
.button
) {
2253 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2255 fDragTime0
= getTimeAtX(fDragX0
);
2256 fDragState
= DRAG_ZOOM
;
2257 fDragButton
= e
.button
;
2259 updateCursor(e
.x
, e
.stateMask
);
2260 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2265 public void mouseUp(MouseEvent e
) {
2266 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2267 menuDetected(fPendingMenuDetectEvent
);
2269 if (DRAG_NONE
!= fDragState
) {
2271 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2272 if (fDragX
!= fDragX0
) {
2273 fTimeProvider
.notifyStartFinishTime();
2275 fDragState
= DRAG_NONE
;
2276 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2277 fDragState
= DRAG_NONE
;
2279 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2280 if (fDragX
== fDragX0
) { // click without selecting anything
2281 long time
= getTimeAtX(e
.x
);
2282 fTimeProvider
.setSelectedTimeNotify(time
, false);
2284 long time0
= fDragTime0
;
2285 long time1
= getTimeAtX(fDragX
);
2286 if (time0
<= time1
) {
2287 fTimeProvider
.setSelectionRangeNotify(time0
, time1
);
2289 fTimeProvider
.setSelectionRangeNotify(time1
, time0
);
2292 fDragState
= DRAG_NONE
;
2294 fTimeGraphScale
.setDragRange(-1, -1);
2295 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2296 int nameWidth
= fTimeProvider
.getNameSpace();
2297 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2298 long time0
= getTimeAtX(fDragX0
);
2299 long time1
= getTimeAtX(fDragX
);
2300 if (time0
< time1
) {
2301 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2303 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2308 fDragState
= DRAG_NONE
;
2309 fTimeGraphScale
.setDragRange(-1, -1);
2312 updateCursor(e
.x
, e
.stateMask
);
2313 updateStatusLine(e
.x
);
2317 public void mouseEnter(MouseEvent e
) {
2321 public void mouseExit(MouseEvent e
) {
2322 if (fMouseOverSplitLine
) {
2323 fMouseOverSplitLine
= false;
2326 updateStatusLine(NO_STATUS
);
2330 public void mouseHover(MouseEvent e
) {
2334 public void mouseScrolled(MouseEvent e
) {
2335 if (fDragState
!= DRAG_NONE
) {
2338 boolean zoomScroll
= false;
2339 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2340 Point parentSize
= getParent().getSize();
2341 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2342 // over the parent control
2343 if (e
.x
> getCtrlSize().x
) {
2344 // over the vertical scroll bar
2346 } else if (e
.y
< 0 || e
.y
>= getCtrlSize().y
) {
2347 // over the time scale or horizontal scroll bar
2350 if (e
.x
< fTimeProvider
.getNameSpace()) {
2351 // over the name space
2354 // over the state area
2355 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2356 // over the state area, CTRL pressed
2359 // over the state area, CTRL not pressed
2365 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2368 } else if (e
.count
< 0) {
2372 setTopIndex(getTopIndex() - e
.count
);
2377 public void handleEvent(Event event
) {
2378 if (event
.type
== SWT
.MouseWheel
) {
2379 // prevent horizontal scrolling when the mouse wheel is used to
2380 // scroll vertically or zoom
2386 public void controlMoved(ControlEvent e
) {
2390 public void controlResized(ControlEvent e
) {
2395 public void widgetDefaultSelected(SelectionEvent e
) {
2399 public void widgetSelected(SelectionEvent e
) {
2400 if (e
.widget
== getVerticalBar()) {
2401 setTopIndex(getVerticalBar().getSelection());
2402 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2403 int start
= getHorizontalBar().getSelection();
2404 long time0
= fTimeProvider
.getTime0();
2405 long time1
= fTimeProvider
.getTime1();
2406 long timeMin
= fTimeProvider
.getMinTime();
2407 long timeMax
= fTimeProvider
.getMaxTime();
2408 long delta
= timeMax
- timeMin
;
2410 long range
= time1
- time0
;
2411 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2412 time1
= time0
+ range
;
2414 // TODO: Follow-up with Bug 310310
2415 // In Linux SWT.DRAG is the only value received
2416 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2417 if (e
.detail
== SWT
.DRAG
) {
2418 fTimeProvider
.setStartFinishTime(time0
, time1
);
2420 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2426 public int getBorderWidth() {
2427 return fBorderWidth
;
2431 * Set the border width
2433 * @param borderWidth
2436 public void setBorderWidth(int borderWidth
) {
2437 this.fBorderWidth
= borderWidth
;
2441 * @return The current height of the header row
2443 public int getHeaderHeight() {
2444 return fHeaderHeight
;
2448 * Set the height of the header row
2450 * @param headerHeight
2453 public void setHeaderHeight(int headerHeight
) {
2454 this.fHeaderHeight
= headerHeight
;
2458 * @return The default height of regular item rows
2460 public int getItemHeight() {
2461 return fGlobalItemHeight
;
2465 * Set the default height of regular item rows.
2470 public void setItemHeight(int rowHeight
) {
2471 this.fGlobalItemHeight
= rowHeight
;
2475 * Set the height of a specific item. Overrides the default item height.
2478 * A time graph entry
2481 * @return true if the height is successfully stored, false otherwise
2485 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2486 Item item
= fItemData
.findItem(entry
);
2488 item
.fItemHeight
= rowHeight
;
2495 * Set the minimum item width
2497 * @param width The minimum width
2499 public void setMinimumItemWidth(int width
) {
2500 this.fMinimumItemWidth
= width
;
2504 * @return The minimum item width
2506 public int getMinimumItemWidth() {
2507 return fMinimumItemWidth
;
2511 * @return The entries that are currently filtered out
2515 public List
<ITimeGraphEntry
> getFilteredOut() {
2516 return fItemData
.getFilteredOut();
2520 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2521 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2522 fSelectionChangedListeners
.add(listener
);
2527 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2528 if (listener
!= null) {
2529 fSelectionChangedListeners
.remove(listener
);
2534 public void setSelection(ISelection selection
) {
2535 if (selection
instanceof TimeGraphSelection
) {
2536 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2537 Object ob
= sel
.getFirstElement();
2538 if (ob
instanceof ITimeGraphEntry
) {
2539 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2540 selectItem(trace
, false);
2547 * @param filter The filter object to be attached to the view
2550 public void addFilter(ViewerFilter filter
) {
2551 if (!fFilters
.contains(filter
)) {
2552 fFilters
.add(filter
);
2557 * @param filter The filter object to be attached to the view
2560 public void removeFilter(ViewerFilter filter
) {
2561 fFilters
.remove(filter
);
2568 public void colorSettingsChanged(StateItem
[] stateItems
) {
2569 /* Destroy previous colors from the resource manager */
2570 if (fEventColorMap
!= null) {
2571 for (Color color
: fEventColorMap
) {
2572 fResourceManager
.destroyColor(color
.getRGB());
2575 if (stateItems
!= null) {
2576 fEventColorMap
= new Color
[stateItems
.length
];
2577 for (int i
= 0; i
< stateItems
.length
; i
++) {
2578 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2581 fEventColorMap
= new Color
[] { };
2586 private class ItemData
{
2587 private final Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2588 private Item
[] fExpandedItems
= new Item
[0];
2589 private Item
[] fItems
= new Item
[0];
2590 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2591 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2592 private boolean fEntryFilter
[] = new boolean[0];
2593 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<>();
2598 public Item
findItem(ITimeGraphEntry entry
) {
2599 return fItemMap
.get(entry
);
2602 public int findItemIndex(ITimeGraphEntry entry
) {
2603 Item item
= fItemMap
.get(entry
);
2607 return item
.fExpandedIndex
;
2610 public void refreshData() {
2612 fFilteredOut
.clear();
2613 ITimeGraphEntry selection
= getSelectedTrace();
2614 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2615 ITimeGraphEntry entry
= fRootEntries
[i
];
2616 refreshData(fItemMap
, null, 0, entry
);
2618 fItems
= fItemMap
.values().toArray(new Item
[0]);
2619 updateExpandedItems();
2620 if (selection
!= null) {
2621 for (Item item
: fExpandedItems
) {
2622 if (item
.fEntry
== selection
) {
2623 item
.fSelected
= true;
2630 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2631 Item item
= new Item(entry
, entry
.getName(), level
);
2632 if (parent
!= null) {
2633 parent
.fChildren
.add(item
);
2635 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2636 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2638 item
.fItemHeight
= fGlobalItemHeight
;
2640 itemMap
.put(entry
, item
);
2641 if (entry
.hasChildren()) {
2642 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2643 item
.fHasChildren
= true;
2644 for (ITimeGraphEntry child
: entry
.getChildren()) {
2645 refreshData(itemMap
, item
, level
+ 1, child
);
2650 public void updateExpandedItems() {
2651 for (Item item
: fItems
) {
2652 item
.fExpandedIndex
= -1;
2654 List
<Item
> expandedItemList
= new ArrayList
<>();
2655 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2656 ITimeGraphEntry entry
= fRootEntries
[i
];
2657 Item item
= findItem(entry
);
2658 refreshExpanded(expandedItemList
, item
);
2660 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2661 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2664 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2665 // Check for filters
2666 boolean display
= true;
2667 for (ViewerFilter filter
: fFilters
) {
2668 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2674 item
.fExpandedIndex
= expandedItemList
.size();
2675 expandedItemList
.add(item
);
2676 if (item
.fHasChildren
&& item
.fExpanded
) {
2677 for (Item child
: item
.fChildren
) {
2678 refreshExpanded(expandedItemList
, child
);
2684 public void refreshData(ITimeGraphEntry
[] entries
) {
2685 if (entries
== null) {
2686 fEntryFilter
= null;
2687 fRootEntries
= null;
2689 if (entries
.length
== 0) {
2690 fEntryFilter
= null;
2691 } else if (fEntryFilter
== null || entries
.length
!= fEntryFilter
.length
) {
2692 fEntryFilter
= new boolean[entries
.length
];
2693 java
.util
.Arrays
.fill(fEntryFilter
, true);
2695 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
2701 public void refreshArrows(List
<ILinkEvent
> events
) {
2702 /* If links are null, reset the list */
2703 if (events
!= null) {
2706 fLinks
= new ArrayList
<>();
2710 public ITimeGraphEntry
[] getEntries() {
2711 return fRootEntries
;
2714 public boolean[] getEntryFilter() {
2715 return fEntryFilter
;
2718 public List
<ITimeGraphEntry
> getFilteredOut() {
2719 return fFilteredOut
;
2723 private class Item
{
2724 private boolean fExpanded
;
2725 private int fExpandedIndex
;
2726 private boolean fSelected
;
2727 private boolean fHasChildren
;
2728 private int fItemHeight
;
2729 private final int fLevel
;
2730 private final List
<Item
> fChildren
;
2731 private final String fName
;
2732 private final ITimeGraphEntry fEntry
;
2734 public Item(ITimeGraphEntry entry
, String name
, int level
) {
2735 this.fEntry
= entry
;
2737 this.fLevel
= level
;
2738 this.fChildren
= new ArrayList
<>();
2742 public String
toString() {
2751 public void menuDetected(MenuDetectEvent e
) {
2752 if (null == fTimeProvider
) {
2755 if (e
.detail
== SWT
.MENU_MOUSE
) {
2756 if (fPendingMenuDetectEvent
== null) {
2757 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2758 * Store the event and trigger it later just before handling mouseUp.
2759 * This allows for the method to detect if mouse is used to drag zoom.
2761 fPendingMenuDetectEvent
= e
;
2764 fPendingMenuDetectEvent
= null;
2765 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2769 if (fDragState
!= DRAG_NONE
) {
2773 Point p
= toControl(e
.x
, e
.y
);
2774 int idx
= getItemIndexAtY(p
.y
);
2775 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2776 Item item
= fItemData
.fExpandedItems
[idx
];
2777 ITimeGraphEntry entry
= item
.fEntry
;
2778 if (entry
.hasTimeEvents()) {
2779 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2780 if (event
!= null) {
2782 fireMenuEventOnTimeEvent(e
);
2787 fireMenuEventOnTimeGraphEntry(e
);