1 /*****************************************************************************
2 * Copyright (c) 2007, 2016 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
.AbstractMap
.SimpleEntry
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Arrays
;
26 import java
.util
.Collections
;
27 import java
.util
.HashMap
;
28 import java
.util
.Iterator
;
29 import java
.util
.LinkedHashMap
;
30 import java
.util
.List
;
32 import java
.util
.Map
.Entry
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.jface
.action
.IStatusLineManager
;
36 import org
.eclipse
.jface
.resource
.JFaceResources
;
37 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
38 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
39 import org
.eclipse
.jface
.viewers
.ISelection
;
40 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
41 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
42 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
43 import org
.eclipse
.osgi
.util
.NLS
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.FocusEvent
;
46 import org
.eclipse
.swt
.events
.FocusListener
;
47 import org
.eclipse
.swt
.events
.KeyEvent
;
48 import org
.eclipse
.swt
.events
.KeyListener
;
49 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
50 import org
.eclipse
.swt
.events
.MenuDetectListener
;
51 import org
.eclipse
.swt
.events
.MouseEvent
;
52 import org
.eclipse
.swt
.events
.MouseListener
;
53 import org
.eclipse
.swt
.events
.MouseMoveListener
;
54 import org
.eclipse
.swt
.events
.MouseTrackListener
;
55 import org
.eclipse
.swt
.events
.MouseWheelListener
;
56 import org
.eclipse
.swt
.events
.PaintEvent
;
57 import org
.eclipse
.swt
.events
.SelectionListener
;
58 import org
.eclipse
.swt
.events
.TraverseEvent
;
59 import org
.eclipse
.swt
.events
.TraverseListener
;
60 import org
.eclipse
.swt
.events
.TypedEvent
;
61 import org
.eclipse
.swt
.graphics
.Color
;
62 import org
.eclipse
.swt
.graphics
.Cursor
;
63 import org
.eclipse
.swt
.graphics
.Font
;
64 import org
.eclipse
.swt
.graphics
.FontData
;
65 import org
.eclipse
.swt
.graphics
.GC
;
66 import org
.eclipse
.swt
.graphics
.Image
;
67 import org
.eclipse
.swt
.graphics
.Point
;
68 import org
.eclipse
.swt
.graphics
.Rectangle
;
69 import org
.eclipse
.swt
.widgets
.Composite
;
70 import org
.eclipse
.swt
.widgets
.Display
;
71 import org
.eclipse
.swt
.widgets
.Event
;
72 import org
.eclipse
.swt
.widgets
.Listener
;
73 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
89 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
90 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
92 import com
.google
.common
.collect
.Iterables
;
95 * Time graph control implementation
97 * @author Alvaro Sanchez-Leon
98 * @author Patrick Tasse
100 public class TimeGraphControl
extends TimeGraphBaseControl
101 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
102 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
103 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
105 /** Constant indicating that all levels of the time graph should be expanded */
106 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
108 private static final int DRAG_NONE
= 0;
109 private static final int DRAG_TRACE_ITEM
= 1;
110 private static final int DRAG_SPLIT_LINE
= 2;
111 private static final int DRAG_ZOOM
= 3;
112 private static final int DRAG_SELECTION
= 4;
114 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
116 private static final double ZOOM_FACTOR
= 1.5;
117 private static final double ZOOM_IN_FACTOR
= 0.8;
118 private static final double ZOOM_OUT_FACTOR
= 1.25;
120 private static final int SNAP_WIDTH
= 2;
121 private static final int ARROW_HOVER_MAX_DIST
= 5;
123 private static final int NO_STATUS
= -1;
124 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
126 private static final int MAX_LABEL_LENGTH
= 256;
128 private static final int PPI
= 72; // points per inch
129 private static final int DPI
= Display
.getDefault().getDPI().y
;
131 private static final int VERTICAL_ZOOM_DELAY
= 400;
133 /** Resource manager */
134 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
136 /** Color map for event types */
137 private Color
[] fEventColorMap
= null;
139 private ITimeDataProvider fTimeProvider
;
140 private IStatusLineManager fStatusLineManager
= null;
141 private TimeGraphScale fTimeGraphScale
= null;
143 private boolean fIsInFocus
= false;
144 private boolean fMouseOverSplitLine
= false;
145 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
146 private int fHeightAdjustment
= 0;
147 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
148 private boolean fBlendSubPixelEvents
= false;
149 private int fMinimumItemWidth
= 0;
150 private int fTopIndex
= 0;
151 private int fDragState
= DRAG_NONE
;
152 private boolean fDragBeginMarker
= false;
153 private int fDragButton
;
154 private int fDragX0
= 0;
155 private int fDragX
= 0;
156 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
157 private int fIdealNameSpace
= 0;
158 private long fTime0bak
;
159 private long fTime1bak
;
160 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
161 private ItemData fItemData
= null;
162 private List
<IMarkerEvent
> fMarkers
= null;
163 private boolean fMarkersVisible
= true;
164 private List
<SelectionListener
> fSelectionListeners
;
165 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
166 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
167 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
168 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
169 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
170 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
171 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
172 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
173 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
174 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
175 private MenuDetectEvent fPendingMenuDetectEvent
= null;
176 private boolean fGridLinesVisible
= true;
177 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
178 private boolean fHideArrows
= false;
179 private int fAutoExpandLevel
= ALL_LEVELS
;
180 private Entry
<ITimeGraphEntry
, Integer
> fVerticalZoomAlignEntry
= null;
181 private long fVerticalZoomAlignTime
= 0;
182 private int fBorderWidth
= 0;
183 private int fHeaderHeight
= 0;
186 * Standard constructor
189 * The parent composite object
191 * The color scheme to use
193 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
195 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
197 fItemData
= new ItemData();
199 addFocusListener(this);
200 addMouseListener(this);
201 addMouseMoveListener(this);
202 addMouseTrackListener(this);
203 addMouseWheelListener(this);
204 addTraverseListener(this);
205 addKeyListener(this);
206 addMenuDetectListener(this);
207 addListener(SWT
.MouseWheel
, this);
208 addDisposeListener((e
) -> {
209 fResourceManager
.dispose();
210 for (Font font
: fFonts
.values()) {
217 * Sets the timegraph provider used by this timegraph viewer.
219 * @param timeGraphProvider the timegraph provider
221 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
222 fTimeGraphProvider
= timeGraphProvider
;
224 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
225 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
226 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
229 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
230 colorSettingsChanged(stateItems
);
234 * Gets the timegraph provider used by this timegraph viewer.
236 * @return the timegraph provider, or <code>null</code> if not set.
238 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
239 return fTimeGraphProvider
;
243 * Gets the color map used by this timegraph viewer.
245 * @return a color map, or <code>null</code> if not set.
247 public Color
[] getEventColorMap() {
248 return fEventColorMap
;
252 * Assign the given time provider
254 * @param timeProvider
257 public void setTimeProvider(ITimeDataProvider timeProvider
) {
258 fTimeProvider
= timeProvider
;
263 * Assign the status line manager
265 * @param statusLineManager
266 * The status line manager, or null to disable status line messages
268 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
269 if (fStatusLineManager
!= null && statusLineManager
== null) {
270 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
272 fStatusLineManager
= statusLineManager
;
276 * Assign the time graph scale
278 * @param timeGraphScale
279 * The time graph scale
281 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
282 fTimeGraphScale
= timeGraphScale
;
286 * Add a selection listener
289 * The listener to add
291 public void addSelectionListener(SelectionListener listener
) {
292 if (listener
== null) {
293 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
295 if (null == fSelectionListeners
) {
296 fSelectionListeners
= new ArrayList
<>();
298 fSelectionListeners
.add(listener
);
302 * Remove a selection listener
305 * The listener to remove
307 public void removeSelectionListener(SelectionListener listener
) {
308 if (null != fSelectionListeners
) {
309 fSelectionListeners
.remove(listener
);
314 * Selection changed callback
316 public void fireSelectionChanged() {
317 if (null != fSelectionListeners
) {
318 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
319 while (it
.hasNext()) {
320 SelectionListener listener
= it
.next();
321 listener
.widgetSelected(null);
327 * Default selection callback
329 public void fireDefaultSelection() {
330 if (null != fSelectionListeners
) {
331 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
332 while (it
.hasNext()) {
333 SelectionListener listener
= it
.next();
334 listener
.widgetDefaultSelected(null);
340 * Add a drag selection listener
343 * The listener to add
345 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
346 if (listener
== null) {
347 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
349 if (null == fDragSelectionListeners
) {
350 fDragSelectionListeners
= new ArrayList
<>();
352 fDragSelectionListeners
.add(listener
);
356 * Remove a drag selection listener
359 * The listener to remove
361 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
362 if (null != fDragSelectionListeners
) {
363 fDragSelectionListeners
.remove(listener
);
368 * Drag Selection changed callback
371 * Time interval start
375 public void fireDragSelectionChanged(long start
, long end
) {
376 // check for backward intervals
377 long beginTime
, endTime
;
385 // call the listeners
386 if (null != fDragSelectionListeners
) {
387 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
388 while (it
.hasNext()) {
389 ITimeGraphTimeListener listener
= it
.next();
390 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
396 * Get the traces in the model
398 * @return The array of traces
400 public ITimeGraphEntry
[] getTraces() {
401 return fItemData
.getEntries();
405 * Refresh the data for the thing
407 public void refreshData() {
408 fItemData
.refreshData();
413 * Refresh data for the given traces
416 * The traces to refresh
418 public void refreshData(ITimeGraphEntry
[] traces
) {
419 fItemData
.refreshData(traces
);
424 * Refresh the links (arrows) of this widget
426 * @param events The link event list
428 public void refreshArrows(List
<ILinkEvent
> events
) {
429 fItemData
.refreshArrows(events
);
433 * Get the links (arrows) of this widget
435 * @return The unmodifiable link event list
439 public List
<ILinkEvent
> getArrows() {
440 return Collections
.unmodifiableList(fItemData
.fLinks
);
443 boolean ensureVisibleItem(int idx
, boolean redraw
) {
444 boolean changed
= false;
447 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
448 if (fItemData
.fExpandedItems
[index
].fSelected
) {
453 if (index
>= fItemData
.fExpandedItems
.length
) {
456 if (index
< fTopIndex
) {
463 int page
= countPerPage();
464 if (index
>= fTopIndex
+ page
) {
465 setTopIndex(index
- page
+ 1);
476 * Assign the given index as the top one
481 public void setTopIndex(int idx
) {
482 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
483 index
= Math
.max(0, index
);
489 * Set the top index so that the requested element is at the specified
493 * the time graph entry to be positioned
495 * the requested y-coordinate
498 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
499 Item item
= fItemData
.fItemMap
.get(entry
);
500 if (item
== null || item
.fExpandedIndex
== -1) {
503 int index
= item
.fExpandedIndex
;
504 Rectangle itemRect
= getItemRect(getClientArea(), index
);
505 int delta
= itemRect
.y
+ itemRect
.height
- y
;
506 int topIndex
= getItemIndexAtY(delta
);
507 if (topIndex
!= -1) {
508 setTopIndex(topIndex
);
513 setTopIndex(getExpandedElementCount());
519 * Sets the auto-expand level to be used for new entries discovered when
520 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
521 * . The value 0 means that there is no auto-expand; 1 means that top-level
522 * entries are expanded, but not their children; 2 means that top-level
523 * entries are expanded, and their children, but not grand-children; and so
526 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
530 * non-negative level, or <code>ALL_LEVELS</code> to expand all
533 public void setAutoExpandLevel(int level
) {
534 fAutoExpandLevel
= level
;
538 * Returns the auto-expand level.
540 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
541 * the tree are expanded automatically
542 * @see #setAutoExpandLevel
544 public int getAutoExpandLevel() {
545 return fAutoExpandLevel
;
549 * Get the expanded state of a given entry.
553 * @return true if the entry is expanded, false if collapsed
556 public boolean getExpandedState(ITimeGraphEntry entry
) {
557 Item item
= fItemData
.fItemMap
.get(entry
);
558 return (item
!= null ? item
.fExpanded
: false);
562 * Set the expanded state of a given entry
567 * True if expanded, false if collapsed
569 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
570 Item item
= fItemData
.findItem(entry
);
571 if (item
!= null && item
.fExpanded
!= expanded
) {
572 item
.fExpanded
= expanded
;
573 fItemData
.updateExpandedItems();
579 * Collapses all nodes of the viewer's tree, starting with the root.
581 public void collapseAll() {
582 for (Item item
: fItemData
.fItems
) {
583 item
.fExpanded
= false;
585 fItemData
.updateExpandedItems();
590 * Expands all nodes of the viewer's tree, starting with the root.
592 public void expandAll() {
593 for (Item item
: fItemData
.fItems
) {
594 item
.fExpanded
= true;
596 fItemData
.updateExpandedItems();
601 * Add a tree listener
604 * The listener to add
606 public void addTreeListener(ITimeGraphTreeListener listener
) {
607 if (!fTreeListeners
.contains(listener
)) {
608 fTreeListeners
.add(listener
);
613 * Remove a tree listener
616 * The listener to remove
618 public void removeTreeListener(ITimeGraphTreeListener listener
) {
619 if (fTreeListeners
.contains(listener
)) {
620 fTreeListeners
.remove(listener
);
625 * Tree event callback
630 * The expanded state (true for expanded, false for collapsed)
632 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
633 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
634 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
636 listener
.treeExpanded(event
);
638 listener
.treeCollapsed(event
);
644 * Add a menu listener on {@link ITimeGraphEntry}s
647 * The listener to add
649 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
650 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
651 fTimeGraphEntryMenuListeners
.add(listener
);
656 * Remove a menu listener on {@link ITimeGraphEntry}s
659 * The listener to remove
661 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
662 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
663 fTimeGraphEntryMenuListeners
.remove(listener
);
668 * Menu event callback on {@link ITimeGraphEntry}s
671 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
673 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
674 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
675 listener
.menuDetected(event
);
680 * Add a menu listener on {@link ITimeEvent}s
683 * The listener to add
685 public void addTimeEventMenuListener(MenuDetectListener listener
) {
686 if (!fTimeEventMenuListeners
.contains(listener
)) {
687 fTimeEventMenuListeners
.add(listener
);
692 * Remove a menu listener on {@link ITimeEvent}s
695 * The listener to remove
697 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
698 if (fTimeEventMenuListeners
.contains(listener
)) {
699 fTimeEventMenuListeners
.remove(listener
);
704 * Menu event callback on {@link ITimeEvent}s
707 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
709 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
710 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
711 listener
.menuDetected(event
);
716 public ISelection
getSelection() {
717 TimeGraphSelection sel
= new TimeGraphSelection();
718 ITimeGraphEntry trace
= getSelectedTrace();
719 if (null != trace
&& null != fTimeProvider
) {
720 long selectedTime
= fTimeProvider
.getSelectionBegin();
721 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
732 * Get the selection object
734 * @return The selection
736 public ISelection
getSelectionTrace() {
737 TimeGraphSelection sel
= new TimeGraphSelection();
738 ITimeGraphEntry trace
= getSelectedTrace();
746 * Enable/disable one of the traces in the model
749 * 1 to enable it, -1 to disable. The method returns immediately
750 * if another value is used.
752 public void selectTrace(int n
) {
753 if ((n
!= 1) && (n
!= -1)) {
757 boolean changed
= false;
758 int lastSelection
= -1;
759 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
760 Item item
= fItemData
.fExpandedItems
[i
];
761 if (item
.fSelected
) {
763 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
764 item
.fSelected
= false;
765 item
= fItemData
.fExpandedItems
[i
+ 1];
766 item
.fSelected
= true;
768 } else if ((-1 == n
) && (i
> 0)) {
769 item
.fSelected
= false;
770 item
= fItemData
.fExpandedItems
[i
- 1];
771 item
.fSelected
= true;
778 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
779 Item item
= fItemData
.fExpandedItems
[0];
780 item
.fSelected
= true;
785 ensureVisibleItem(-1, false);
787 fireSelectionChanged();
795 * 1 for next event, -1 for previous event
797 * true to extend selection range, false for single selection
800 public void selectEvent(int n
, boolean extend
) {
801 if (null == fTimeProvider
) {
804 ITimeGraphEntry trace
= getSelectedTrace();
808 long selectedTime
= fTimeProvider
.getSelectionEnd();
809 long endTime
= fTimeProvider
.getMaxTime();
810 ITimeEvent nextEvent
;
811 if (n
== -1 && selectedTime
> endTime
) {
812 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
814 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
816 if (null == nextEvent
&& n
== -1) {
817 nextEvent
= Utils
.getFirstEvent(trace
);
819 if (null != nextEvent
) {
820 long nextTime
= nextEvent
.getTime();
821 // If last event detected e.g. going back or not moving to a next
823 if (nextTime
<= selectedTime
&& n
== 1) {
824 // Select to the end of this last event
825 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
826 // but not beyond the end of the trace
827 if (nextTime
> endTime
) {
830 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
831 // for previous event go to its end time unless we were already there
832 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
835 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
837 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
839 fireSelectionChanged();
842 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
844 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
846 fireSelectionChanged();
848 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
852 * Select the next event
855 * true to extend selection range, false for single selection
858 public void selectNextEvent(boolean extend
) {
859 selectEvent(1, extend
);
860 // Notify if visible time window has been adjusted
861 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
865 * Select the previous event
868 * true to extend selection range, false for single selection
871 public void selectPrevEvent(boolean extend
) {
872 selectEvent(-1, extend
);
873 // Notify if visible time window has been adjusted
874 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
878 * Select the next trace
880 public void selectNextTrace() {
885 * Select the previous trace
887 public void selectPrevTrace() {
892 * Scroll left or right by one half window size
895 * true to scroll left, false to scroll right
897 public void horizontalScroll(boolean left
) {
898 long time0
= fTimeProvider
.getTime0();
899 long time1
= fTimeProvider
.getTime1();
900 long timeMin
= fTimeProvider
.getMinTime();
901 long timeMax
= fTimeProvider
.getMaxTime();
902 long range
= time1
- time0
;
906 long increment
= Math
.max(1, range
/ 2);
908 time0
= Math
.max(time0
- increment
, timeMin
);
909 time1
= time0
+ range
;
911 time1
= Math
.min(time1
+ increment
, timeMax
);
912 time0
= time1
- range
;
914 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
918 * Zoom based on mouse cursor location with mouse scrolling
920 * @param zoomIn true to zoom in, false to zoom out
922 public void zoom(boolean zoomIn
) {
923 int globalX
= getDisplay().getCursorLocation().x
;
924 Point p
= toControl(globalX
, 0);
925 int nameSpace
= fTimeProvider
.getNameSpace();
926 int timeSpace
= fTimeProvider
.getTimeSpace();
927 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
928 long time0
= fTimeProvider
.getTime0();
929 long time1
= fTimeProvider
.getTime1();
930 long interval
= time1
- time0
;
933 } // to allow getting out of single point interval
936 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
938 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
940 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
941 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
942 long newTime1
= newTime0
+ newInterval
;
943 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
947 * zoom in using single click
949 public void zoomIn() {
950 long prevTime0
= fTimeProvider
.getTime0();
951 long prevTime1
= fTimeProvider
.getTime1();
952 long prevRange
= prevTime1
- prevTime0
;
953 if (prevRange
== 0) {
956 ITimeDataProvider provider
= fTimeProvider
;
957 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
958 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
959 selTime
= (prevTime0
+ prevTime1
) / 2;
961 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
962 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
964 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
966 if (inaccuracy
> 0 && inaccuracy
< 100) {
967 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
971 long min
= fTimeProvider
.getMinTimeInterval();
972 if ((time1
- time0
) < min
) {
973 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
977 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
981 * zoom out using single click
983 public void zoomOut() {
984 long prevTime0
= fTimeProvider
.getTime0();
985 long prevTime1
= fTimeProvider
.getTime1();
986 ITimeDataProvider provider
= fTimeProvider
;
987 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
988 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
989 selTime
= (prevTime0
+ prevTime1
) / 2;
991 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
992 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
994 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
995 if (inaccuracy
> 0 && inaccuracy
< 100) {
996 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1000 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1007 * true to zoom in, false to zoom out
1010 public void verticalZoom(boolean zoomIn
) {
1012 fHeightAdjustment
++;
1014 fHeightAdjustment
--;
1016 fItemData
.refreshData();
1021 * Reset the vertical zoom to default.
1025 public void resetVerticalZoom() {
1026 fHeightAdjustment
= 0;
1027 fItemData
.refreshData();
1032 * Set the grid lines visibility. The default is true.
1035 * true to show the grid lines, false otherwise
1038 public void setGridLinesVisible(boolean visible
) {
1039 fGridLinesVisible
= visible
;
1043 * Get the grid lines visibility.
1045 * @return true if the grid lines are visible, false otherwise
1048 public boolean getGridLinesVisible() {
1049 return fGridLinesVisible
;
1053 * Set the grid line color. The default is SWT.COLOR_GRAY.
1056 * the grid line color
1059 public void setGridLineColor(Color color
) {
1060 fGridLineColor
= color
;
1064 * Get the grid line color.
1066 * @return the grid line color
1069 public Color
getGridLineColor() {
1070 return fGridLineColor
;
1074 * Set the markers list.
1077 * The markers list, or null
1080 public void setMarkers(List
<IMarkerEvent
> markers
) {
1085 * Get the markers list.
1087 * @return The markers list, or null
1090 public List
<IMarkerEvent
> getMarkers() {
1095 * Set the markers visibility. The default is true.
1098 * true to show the markers, false otherwise
1101 public void setMarkersVisible(boolean visible
) {
1102 fMarkersVisible
= visible
;
1106 * Get the markers visibility.
1108 * @return true if the markers are visible, false otherwise
1111 public boolean getMarkersVisible() {
1112 return fMarkersVisible
;
1118 * @param hideArrows true to hide arrows
1120 public void hideArrows(boolean hideArrows
) {
1121 fHideArrows
= hideArrows
;
1125 * Follow the arrow forward
1128 * true to extend selection range, false for single selection
1131 public void followArrowFwd(boolean extend
) {
1132 ITimeGraphEntry trace
= getSelectedTrace();
1133 if (trace
== null) {
1136 long selectedTime
= fTimeProvider
.getSelectionEnd();
1137 for (ILinkEvent link
: fItemData
.fLinks
) {
1138 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1139 selectItem(link
.getDestinationEntry(), false);
1140 if (link
.getDuration() != 0) {
1142 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1144 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1146 // Notify if visible time window has been adjusted
1147 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1149 fireSelectionChanged();
1150 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1154 selectNextEvent(extend
);
1158 * Follow the arrow backward
1161 * true to extend selection range, false for single selection
1164 public void followArrowBwd(boolean extend
) {
1165 ITimeGraphEntry trace
= getSelectedTrace();
1166 if (trace
== null) {
1169 long selectedTime
= fTimeProvider
.getSelectionEnd();
1170 for (ILinkEvent link
: fItemData
.fLinks
) {
1171 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1172 selectItem(link
.getEntry(), false);
1173 if (link
.getDuration() != 0) {
1175 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1177 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1179 // Notify if visible time window has been adjusted
1180 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1182 fireSelectionChanged();
1183 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1187 selectPrevEvent(extend
);
1191 * Return the currently selected trace
1193 * @return The entry matching the trace
1195 public ITimeGraphEntry
getSelectedTrace() {
1196 ITimeGraphEntry trace
= null;
1197 int idx
= getSelectedIndex();
1199 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1205 * Retrieve the index of the currently selected item
1209 public int getSelectedIndex() {
1211 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1212 Item item
= fItemData
.fExpandedItems
[i
];
1213 if (item
.fSelected
) {
1221 boolean toggle(int idx
) {
1222 boolean toggled
= false;
1223 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1224 Item item
= fItemData
.fExpandedItems
[idx
];
1225 if (item
.fHasChildren
) {
1226 item
.fExpanded
= !item
.fExpanded
;
1227 fItemData
.updateExpandedItems();
1230 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1237 * Gets the index of the item at the given location.
1241 * @return the index of the item at the given location, of -1 if none.
1243 protected int getItemIndexAtY(int y
) {
1246 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1247 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1253 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1254 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1263 boolean isOverSplitLine(int x
) {
1264 if (x
< 0 || null == fTimeProvider
) {
1267 int nameWidth
= fTimeProvider
.getNameSpace();
1268 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1272 * Gets the {@link ITimeGraphEntry} at the given location.
1275 * a point in the widget
1276 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1280 public ITimeGraphEntry
getEntry(Point pt
) {
1281 int idx
= getItemIndexAtY(pt
.y
);
1282 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1286 * Return the arrow event closest to the given point that is no further than
1287 * a maximum distance.
1290 * a point in the widget
1291 * @return The closest arrow event, or null if there is none close enough.
1293 protected ILinkEvent
getArrow(Point pt
) {
1297 ILinkEvent linkEvent
= null;
1298 double minDistance
= Double
.MAX_VALUE
;
1299 for (ILinkEvent event
: fItemData
.fLinks
) {
1300 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1304 int x2
= x1
+ rect
.width
;
1305 int y2
= y1
+ rect
.height
;
1306 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1307 if (minDistance
> d
) {
1313 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1320 public int getXForTime(long time
) {
1321 if (null == fTimeProvider
) {
1324 long time0
= fTimeProvider
.getTime0();
1325 long time1
= fTimeProvider
.getTime1();
1326 int width
= getSize().x
;
1327 int nameSpace
= fTimeProvider
.getNameSpace();
1328 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1329 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1334 public long getTimeAtX(int coord
) {
1335 if (null == fTimeProvider
) {
1339 Point size
= getSize();
1340 long time0
= fTimeProvider
.getTime0();
1341 long time1
= fTimeProvider
.getTime1();
1342 int nameWidth
= fTimeProvider
.getNameSpace();
1343 final int x
= coord
- nameWidth
;
1344 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1345 if (x
>= 0 && size
.x
>= nameWidth
) {
1346 if (time1
- time0
> timeWidth
) {
1347 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1348 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1350 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1351 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1357 void selectItem(int idx
, boolean addSelection
) {
1358 boolean changed
= false;
1360 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1361 Item item
= fItemData
.fExpandedItems
[idx
];
1362 changed
= !item
.fSelected
;
1363 item
.fSelected
= true;
1366 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1367 Item item
= fItemData
.fExpandedItems
[i
];
1368 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1371 item
.fSelected
= i
== idx
;
1374 changed
|= ensureVisibleItem(idx
, true);
1381 * Callback for item selection
1384 * The entry matching the trace
1385 * @param addSelection
1386 * If the selection is added or removed
1388 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1389 int idx
= fItemData
.findItemIndex(trace
);
1390 selectItem(idx
, addSelection
);
1394 * Retrieve the number of entries shown per page.
1398 public int countPerPage() {
1399 int height
= getSize().y
;
1402 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1403 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1404 if (ySum
>= height
) {
1409 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1410 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1411 if (ySum
>= height
) {
1420 * Get the index of the top element
1424 public int getTopIndex() {
1429 * Get the number of expanded (visible) items
1431 * @return The count of expanded (visible) items
1433 public int getExpandedElementCount() {
1434 return fItemData
.fExpandedItems
.length
;
1438 * Get an array of all expanded (visible) elements
1440 * @return The expanded (visible) elements
1442 public ITimeGraphEntry
[] getExpandedElements() {
1443 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1444 for (Item item
: fItemData
.fExpandedItems
) {
1445 elements
.add(item
.fEntry
);
1447 return elements
.toArray(new ITimeGraphEntry
[0]);
1451 * Get the expanded (visible) element at the specified index.
1455 * @return The expanded (visible) element or null if out of range
1458 public ITimeGraphEntry
getExpandedElement(int index
) {
1459 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1462 return fItemData
.fExpandedItems
[index
].fEntry
;
1465 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1466 Rectangle rect
= getItemRect(bounds
, idx
);
1467 rect
.width
= nameWidth
;
1471 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1472 Rectangle rect
= getItemRect(bounds
, idx
);
1473 rect
.x
+= nameWidth
;
1474 rect
.width
-= nameWidth
;
1478 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1480 if (idx
>= fTopIndex
) {
1481 for (int i
= fTopIndex
; i
< idx
; i
++) {
1482 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1485 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1486 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1489 int y
= bounds
.y
+ ySum
;
1490 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1491 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1495 void paint(Rectangle bounds
, PaintEvent e
) {
1498 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1502 fIdealNameSpace
= 0;
1503 int nameSpace
= fTimeProvider
.getNameSpace();
1505 // draw the background layer
1506 drawBackground(bounds
, nameSpace
, gc
);
1508 // draw the grid lines
1509 drawGridLines(bounds
, gc
);
1511 // draw the background markers
1512 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1515 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1517 // draw the foreground markers
1518 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1520 // draw the links (arrows)
1521 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1523 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1525 int alpha
= gc
.getAlpha();
1528 long time0
= fTimeProvider
.getTime0();
1529 long time1
= fTimeProvider
.getTime1();
1530 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1531 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1532 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1533 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1534 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1536 // draw selection lines
1537 if (fDragState
!= DRAG_SELECTION
) {
1538 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1539 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1540 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1543 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1544 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1549 // draw selection background
1550 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1551 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1552 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1553 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1555 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1556 } else if (x0
- x1
> 1) {
1557 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1561 // draw drag selection background
1562 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1563 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1564 if (fDragX0
< fDragX
) {
1565 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1566 } else if (fDragX0
> fDragX
) {
1567 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1572 if (DRAG_SPLIT_LINE
== fDragState
) {
1573 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1574 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1575 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1576 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1577 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1578 if (fDragX
!= fDragX0
) {
1579 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1581 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1582 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1583 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1584 if (fDragX
!= fDragX0
) {
1585 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1587 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1588 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1589 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1596 * Draw the background layer. Fills the background of the control's name
1597 * space and states space, updates the background of items if necessary,
1598 * and draws the item's name text and middle line.
1601 * The bounds of the control
1603 * The name space width
1608 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1609 // draw empty name space background
1610 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1611 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1613 // draw empty states space background
1614 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1615 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1617 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1618 Rectangle itemRect
= getItemRect(bounds
, i
);
1619 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1622 Item item
= fItemData
.fExpandedItems
[i
];
1623 // draw the background of selected item and items with no time events
1624 if (! item
.fEntry
.hasTimeEvents()) {
1625 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1626 gc
.fillRectangle(itemRect
);
1627 } else if (item
.fSelected
) {
1628 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1629 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1630 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1631 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1633 // draw the name and middle line
1634 if (! item
.fEntry
.hasTimeEvents()) {
1635 drawName(item
, itemRect
, gc
);
1637 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1638 drawName(item
, nameRect
, gc
);
1639 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1640 drawMidLine(rect
, gc
);
1646 * Draw the grid lines
1649 * The bounds of the control
1654 public void drawGridLines(Rectangle bounds
, GC gc
) {
1655 if (!fGridLinesVisible
) {
1658 gc
.setForeground(fGridLineColor
);
1659 gc
.setAlpha(fGridLineColor
.getAlpha());
1660 for (int x
: fTimeGraphScale
.getTickList()) {
1661 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1670 * The rectangle of the area
1671 * @param timeProvider
1674 * The list of markers
1676 * true to draw the foreground markers, false otherwise
1678 * The width reserved for the names
1680 * Reference to the SWT GC object
1683 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1684 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1687 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1688 /* the list can grow concurrently but cannot shrink */
1689 for (int i
= 0; i
< markers
.size(); i
++) {
1690 IMarkerEvent marker
= markers
.get(i
);
1691 if (marker
.isForeground() == foreground
) {
1692 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1695 gc
.setClipping((Rectangle
) null);
1699 * Draw a single marker
1704 * The bounds of the control
1705 * @param timeProvider
1708 * The width reserved for the name
1710 * Reference to the SWT GC object
1713 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1714 Rectangle rect
= Utils
.clone(bounds
);
1715 if (marker
.getEntry() != null) {
1716 int index
= fItemData
.findItemIndex(marker
.getEntry());
1720 rect
= getStatesRect(bounds
, index
, nameSpace
);
1721 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1725 int x0
= getXForTime(marker
.getTime());
1726 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1727 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1730 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1731 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1733 Color color
= getColorScheme().getColor(marker
.getColor());
1734 gc
.setBackground(color
);
1735 gc
.setAlpha(color
.getAlpha());
1736 gc
.fillRectangle(rect
);
1738 String label
= marker
.getLabel();
1739 if (label
!= null && marker
.getEntry() != null) {
1740 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1741 gc
.setForeground(color
);
1742 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1747 * Draw many items at once
1750 * The bounds of the control
1751 * @param timeProvider
1754 * The array items to draw
1756 * The index of the first element to draw
1758 * The name space width
1762 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1763 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1764 for (int i
= topIndex
; i
< items
.length
; i
++) {
1765 Item item
= items
[i
];
1766 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1776 * The bounds of the control
1777 * @param timeProvider
1780 * The expanded item index
1782 * The name space width
1786 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1787 Rectangle itemRect
= getItemRect(bounds
, i
);
1788 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1792 ITimeGraphEntry entry
= item
.fEntry
;
1793 long time0
= timeProvider
.getTime0();
1794 long time1
= timeProvider
.getTime1();
1795 long selectedTime
= fTimeProvider
.getSelectionEnd();
1797 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1798 if (rect
.isEmpty() || (time1
<= time0
)) {
1799 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1803 boolean selected
= item
.fSelected
;
1804 // K pixels per second
1805 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1807 if (item
.fEntry
.hasTimeEvents()) {
1808 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1809 fillSpace(rect
, gc
, selected
);
1811 int margins
= getMarginForHeight(rect
.height
);
1812 int height
= rect
.height
- margins
;
1813 int topMargin
= (margins
+ 1) / 2;
1814 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1816 /* Set the font for this item */
1817 setFontForHeight(height
, gc
);
1819 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1820 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1823 while (iterator
.hasNext()) {
1824 ITimeEvent event
= iterator
.next();
1825 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1826 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1827 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1828 // event is out of bounds
1831 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1832 stateRect
.x
= Math
.max(rect
.x
, x
);
1833 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1834 if (stateRect
.x
== lastX
) {
1835 stateRect
.width
-= 1;
1836 if (stateRect
.width
> 0) {
1837 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1838 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1842 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1843 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1844 lastX
= stateRect
.x
;
1847 gc
.setClipping((Rectangle
) null);
1849 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1856 * The bounds of the control
1857 * @param timeProvider
1860 * The list of link events
1862 * The name space width
1866 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1867 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1871 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1872 /* the list can grow concurrently but cannot shrink */
1873 for (int i
= 0; i
< links
.size(); i
++) {
1874 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1876 gc
.setClipping((Rectangle
) null);
1880 * Draws a link type event
1883 * The link event to draw
1885 * The bounds of the control
1886 * @param timeProvider
1889 * The name space width
1893 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1894 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1897 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1898 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1899 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1901 if ((srcIndex
== -1) || (destIndex
== -1)) {
1905 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1906 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1908 int x0
= getXForTime(event
.getTime());
1909 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1911 // limit the x-coordinates to prevent integer overflow in calculations
1912 // and also GC.drawLine doesn't draw properly with large coordinates
1913 final int limit
= Integer
.MAX_VALUE
/ 1024;
1914 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1915 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1917 int y0
= src
.y
+ src
.height
/ 2;
1918 int y1
= dst
.y
+ dst
.height
/ 2;
1919 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1928 * Time event for which we're drawing the arrow
1930 * The arrow rectangle
1933 * @return true if the arrow was drawn
1935 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1936 Rectangle rect
, GC gc
) {
1941 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1945 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1948 Color stateColor
= null;
1949 if (colorIdx
< fEventColorMap
.length
) {
1950 stateColor
= fEventColorMap
[colorIdx
];
1952 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1955 gc
.setForeground(stateColor
);
1956 gc
.setBackground(stateColor
);
1958 /* Draw the arrow */
1959 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1960 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1963 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1968 * @author Francis Giraldeau
1971 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1973 * The algorithm was taken from this site, not the code itself
1975 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1978 double cos
= 0.9510;
1979 double sin
= 0.3090;
1980 long lenx
= x1
- x0
;
1981 long leny
= y1
- y0
;
1982 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1984 double dx
= factor
* lenx
/ len
;
1985 double dy
= factor
* leny
/ len
;
1986 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1987 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1988 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1989 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1990 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1991 gc
.fillPolygon(arrow
);
1995 * Draw the name of an item.
2000 * The bounds of the item's name space
2004 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2005 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2007 // No name to be drawn
2008 if (fTimeProvider
.getNameSpace() == 0) {
2012 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2013 setFontForHeight(height
, gc
);
2015 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2016 if (item
.fHasChildren
) {
2017 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2018 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2019 Rectangle rect
= Utils
.clone(bounds
);
2020 rect
.x
+= leftMargin
;
2021 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2022 rect
.width
= EXPAND_SIZE
;
2023 rect
.height
= EXPAND_SIZE
;
2024 gc
.fillRectangle(rect
);
2025 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2026 int midy
= rect
.y
+ rect
.height
/ 2;
2027 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2028 if (!item
.fExpanded
) {
2029 int midx
= rect
.x
+ rect
.width
/ 2;
2030 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2033 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2035 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2038 int imgHeight
= img
.getImageData().height
;
2039 int imgWidth
= img
.getImageData().width
;
2041 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2042 gc
.drawImage(img
, x
, y
);
2043 leftMargin
+= imgWidth
+ MARGIN
;
2045 String name
= item
.fName
;
2046 Point size
= gc
.stringExtent(name
);
2047 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2048 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2050 if (hasTimeEvents
) {
2051 // cut long string with "..."
2052 int width
= bounds
.width
- leftMargin
;
2054 while (size
.x
> width
&& name
.length() > 1) {
2056 name
= name
.substring(0, name
.length() - 1);
2057 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2060 name
+= "..."; //$NON-NLS-1$
2063 Rectangle rect
= Utils
.clone(bounds
);
2064 rect
.x
+= leftMargin
;
2065 rect
.width
-= leftMargin
;
2067 if (rect
.width
> 0) {
2068 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2069 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2070 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2071 leftMargin
+= textWidth
+ MARGIN
;
2073 if (hasTimeEvents
) {
2075 rect
.x
= bounds
.x
+ leftMargin
;
2077 rect
.width
= bounds
.width
- rect
.x
;
2078 drawMidLine(rect
, gc
);
2084 * Draw the state (color fill)
2089 * Time event for which we're drawing the state
2091 * The state rectangle
2095 * Is this time event currently selected (so it appears
2097 * @param timeSelected
2098 * Is the timestamp currently selected
2099 * @return true if the state was drawn
2101 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2102 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2104 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2105 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2108 boolean visible
= rect
.width
== 0 ?
false : true;
2109 rect
.width
= Math
.max(1, rect
.width
);
2110 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2111 gc
.setForeground(black
);
2113 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2115 // Only draw the top and bottom borders
2116 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2117 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2118 if (rect
.width
== 1) {
2119 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2122 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2125 Color stateColor
= null;
2126 if (colorIdx
< fEventColorMap
.length
) {
2127 stateColor
= fEventColorMap
[colorIdx
];
2132 boolean reallySelected
= timeSelected
&& selected
;
2133 // fill all rect area
2134 gc
.setBackground(stateColor
);
2136 gc
.fillRectangle(rect
);
2137 } else if (fBlendSubPixelEvents
) {
2139 gc
.fillRectangle(rect
);
2143 if (reallySelected
) {
2144 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2145 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2148 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2150 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2155 * Fill an item's states rectangle
2158 * The states rectangle
2162 * true if the item is selected
2164 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2165 /* Nothing to draw */
2169 * Draw a line at the middle height of a rectangle
2176 private void drawMidLine(Rectangle rect
, GC gc
) {
2177 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2178 int midy
= rect
.y
+ rect
.height
/ 2;
2179 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2182 private static int getMarginForHeight(int height
) {
2184 * State rectangle is smaller than the item bounds when height is > 4.
2185 * Don't use any margin if the height is below or equal that threshold.
2186 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2187 * pixels for the state height, but with a minimum margin of 1.
2189 final int MARGIN_THRESHOLD
= 4;
2190 final int PREFERRED_HEIGHT
= 13;
2191 final int MIN_MARGIN
= 1;
2192 final int MAX_MARGIN
= 6;
2193 return height
<= MARGIN_THRESHOLD ?
0 :
2194 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2197 private void setFontForHeight(int pixels
, GC gc
) {
2198 /* convert font height from pixels to points */
2199 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2200 Font font
= fFonts
.get(height
);
2202 FontData fontData
= gc
.getFont().getFontData()[0];
2203 fontData
.setHeight(height
);
2204 font
= new Font(gc
.getDevice(), fontData
);
2205 fFonts
.put(height
, font
);
2211 public void keyTraversed(TraverseEvent e
) {
2212 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2218 public void keyPressed(KeyEvent e
) {
2220 if (fItemData
.fExpandedItems
.length
== 0) {
2223 if (SWT
.HOME
== e
.keyCode
) {
2225 } else if (SWT
.END
== e
.keyCode
) {
2226 idx
= fItemData
.fExpandedItems
.length
- 1;
2227 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2228 idx
= getSelectedIndex();
2231 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2234 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2235 idx
= getSelectedIndex();
2238 } else if (idx
> 0) {
2241 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2242 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2243 selectPrevEvent(extend
);
2244 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2245 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2246 selectNextEvent(extend
);
2247 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2248 int page
= countPerPage();
2249 idx
= getSelectedIndex();
2254 if (idx
>= fItemData
.fExpandedItems
.length
) {
2255 idx
= fItemData
.fExpandedItems
.length
- 1;
2257 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2258 int page
= countPerPage();
2259 idx
= getSelectedIndex();
2267 } else if (SWT
.CR
== e
.keyCode
) {
2268 idx
= getSelectedIndex();
2270 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2273 fireDefaultSelection();
2277 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2278 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2280 if (fVerticalZoomAlignEntry
!= null) {
2281 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2283 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2284 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2285 verticalZoom(false);
2286 if (fVerticalZoomAlignEntry
!= null) {
2287 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2289 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2290 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2291 resetVerticalZoom();
2292 if (fVerticalZoomAlignEntry
!= null) {
2293 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2297 selectItem(idx
, false);
2298 fireSelectionChanged();
2300 int x
= toControl(e
.display
.getCursorLocation()).x
;
2301 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2305 public void keyReleased(KeyEvent e
) {
2306 int x
= toControl(e
.display
.getCursorLocation()).x
;
2307 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2311 public void focusGained(FocusEvent e
) {
2314 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2318 public void focusLost(FocusEvent e
) {
2320 if (DRAG_NONE
!= fDragState
) {
2322 fDragState
= DRAG_NONE
;
2325 updateStatusLine(NO_STATUS
);
2329 * @return If the current view is focused
2331 public boolean isInFocus() {
2336 * Provide the possibility to control the wait cursor externally e.g. data
2337 * requests in progress
2339 * @param waitInd Should we wait indefinitely?
2341 public void waitCursor(boolean waitInd
) {
2342 // Update cursor as indicated
2344 setCursor(fWaitCursor
);
2350 private void updateCursor(int x
, int stateMask
) {
2351 // if Wait cursor not active, check for the need to change the cursor
2352 if (getCursor() == fWaitCursor
) {
2355 Cursor cursor
= null;
2356 if (fDragState
== DRAG_SPLIT_LINE
) {
2357 } else if (fDragState
== DRAG_SELECTION
) {
2358 cursor
= fResizeCursor
;
2359 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2360 cursor
= fDragCursor
;
2361 } else if (fDragState
== DRAG_ZOOM
) {
2362 cursor
= fZoomCursor
;
2363 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2364 cursor
= fDragCursor
;
2365 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2366 cursor
= fResizeCursor
;
2367 } else if (!isOverSplitLine(x
)) {
2368 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2369 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2370 int xBegin
= getXForTime(selectionBegin
);
2371 int xEnd
= getXForTime(selectionEnd
);
2372 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2373 cursor
= fResizeCursor
;
2376 if (getCursor() != cursor
) {
2382 * Update the status line following a change of selection.
2386 public void updateStatusLine() {
2387 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2390 private void updateStatusLine(int x
) {
2391 // use the time provider of the time graph scale for the status line
2392 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2393 if (fStatusLineManager
== null || null == tdp
||
2394 tdp
.getTime0() == tdp
.getTime1()) {
2397 TimeFormat tf
= tdp
.getTimeFormat();
2398 Resolution res
= Resolution
.NANOSEC
;
2399 StringBuilder message
= new StringBuilder();
2400 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2401 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2402 long time
= getTimeAtX(x
);
2404 if (tdp
instanceof ITimeDataProviderConverter
) {
2405 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2407 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2409 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2410 Utils
.formatTime(time
, tf
, res
)
2414 long selectionBegin
= tdp
.getSelectionBegin();
2415 long selectionEnd
= tdp
.getSelectionEnd();
2416 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2418 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2419 Utils
.formatTime(selectionBegin
, tf
, res
)
2421 if (selectionBegin
!= selectionEnd
) {
2422 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2424 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2425 Utils
.formatTime(selectionEnd
, tf
, res
),
2426 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2429 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2430 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2431 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2432 if (tdp
instanceof ITimeDataProviderConverter
) {
2433 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2434 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2436 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2438 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2439 Utils
.formatTime(time0
, tf
, res
),
2440 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2441 Utils
.formatTime(time
, tf
, res
),
2442 Utils
.formatDelta(time
- time0
, tf
, res
)
2445 fStatusLineManager
.setMessage(message
.toString());
2449 public void mouseMove(MouseEvent e
) {
2450 if (null == fTimeProvider
) {
2453 Point size
= getSize();
2454 if (DRAG_TRACE_ITEM
== fDragState
) {
2455 int nameWidth
= fTimeProvider
.getNameSpace();
2456 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2458 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2459 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2460 long time1
= fTime1bak
- timeDelta
;
2461 long maxTime
= fTimeProvider
.getMaxTime();
2462 if (time1
> maxTime
) {
2465 long time0
= time1
- (fTime1bak
- fTime0bak
);
2466 if (time0
< fTimeProvider
.getMinTime()) {
2467 time0
= fTimeProvider
.getMinTime();
2468 time1
= time0
+ (fTime1bak
- fTime0bak
);
2470 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2472 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2474 fTimeProvider
.setNameSpace(e
.x
);
2475 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2476 } else if (DRAG_SELECTION
== fDragState
) {
2477 if (fDragBeginMarker
) {
2478 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2480 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2483 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2484 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2485 } else if (DRAG_ZOOM
== fDragState
) {
2486 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2488 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2489 } else if (DRAG_NONE
== fDragState
) {
2490 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2491 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2494 fMouseOverSplitLine
= mouseOverSplitLine
;
2496 updateCursor(e
.x
, e
.stateMask
);
2497 updateStatusLine(e
.x
);
2501 public void mouseDoubleClick(MouseEvent e
) {
2502 if (null == fTimeProvider
) {
2505 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2506 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2507 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2508 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2509 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2512 fMouseOverSplitLine
= mouseOverSplitLine
;
2513 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2516 int idx
= getItemIndexAtY(e
.y
);
2518 selectItem(idx
, false);
2519 fireDefaultSelection();
2525 public void mouseDown(MouseEvent e
) {
2526 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2527 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2528 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2532 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2533 int nameSpace
= fTimeProvider
.getNameSpace();
2534 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2535 fDragState
= DRAG_SPLIT_LINE
;
2536 fDragButton
= e
.button
;
2539 fTime0bak
= fTimeProvider
.getTime0();
2540 fTime1bak
= fTimeProvider
.getTime1();
2542 updateCursor(e
.x
, e
.stateMask
);
2546 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2547 int nameSpace
= fTimeProvider
.getNameSpace();
2548 idx
= getItemIndexAtY(e
.y
);
2550 Item item
= fItemData
.fExpandedItems
[idx
];
2551 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2555 selectItem(idx
, false);
2556 fireSelectionChanged();
2558 selectItem(idx
, false); // clear selection
2559 fireSelectionChanged();
2561 long hitTime
= getTimeAtX(e
.x
);
2565 fDragState
= DRAG_SELECTION
;
2566 fDragBeginMarker
= false;
2567 fDragButton
= e
.button
;
2570 fDragTime0
= getTimeAtX(fDragX0
);
2571 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2572 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2573 int xBegin
= getXForTime(selectionBegin
);
2574 int xEnd
= getXForTime(selectionEnd
);
2575 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2576 long time
= getTimeAtX(e
.x
);
2577 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2578 fDragBeginMarker
= true;
2581 fDragTime0
= selectionEnd
;
2584 fDragTime0
= selectionBegin
;
2587 long time
= getTimeAtX(e
.x
);
2588 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2589 fDragBeginMarker
= true;
2592 fDragTime0
= selectionEnd
;
2593 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2595 fDragTime0
= selectionBegin
;
2598 fTime0bak
= fTimeProvider
.getTime0();
2599 fTime1bak
= fTimeProvider
.getTime1();
2601 updateCursor(e
.x
, e
.stateMask
);
2602 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2604 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2605 long hitTime
= getTimeAtX(e
.x
);
2608 fDragState
= DRAG_TRACE_ITEM
;
2609 fDragButton
= e
.button
;
2612 fTime0bak
= fTimeProvider
.getTime0();
2613 fTime1bak
= fTimeProvider
.getTime1();
2614 updateCursor(e
.x
, e
.stateMask
);
2616 } else if (3 == e
.button
) {
2617 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2619 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2621 fDragTime0
= getTimeAtX(fDragX0
);
2622 fDragState
= DRAG_ZOOM
;
2623 fDragButton
= e
.button
;
2625 updateCursor(e
.x
, e
.stateMask
);
2626 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2632 public void mouseUp(MouseEvent e
) {
2633 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2634 menuDetected(fPendingMenuDetectEvent
);
2636 if (DRAG_NONE
!= fDragState
) {
2638 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2639 if (fDragX
!= fDragX0
) {
2640 fTimeProvider
.notifyStartFinishTime();
2642 fDragState
= DRAG_NONE
;
2643 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2644 fDragState
= DRAG_NONE
;
2646 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2647 if (fDragX
== fDragX0
) { // click without selecting anything
2648 long time
= getTimeAtX(e
.x
);
2649 fTimeProvider
.setSelectedTimeNotify(time
, false);
2651 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2652 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2653 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2655 fDragState
= DRAG_NONE
;
2657 fTimeGraphScale
.setDragRange(-1, -1);
2658 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2659 int nameWidth
= fTimeProvider
.getNameSpace();
2660 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2661 long time0
= getTimeAtX(fDragX0
);
2662 long time1
= getTimeAtX(fDragX
);
2663 if (time0
< time1
) {
2664 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2666 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2671 fDragState
= DRAG_NONE
;
2672 fTimeGraphScale
.setDragRange(-1, -1);
2675 updateCursor(e
.x
, e
.stateMask
);
2676 updateStatusLine(e
.x
);
2680 public void mouseEnter(MouseEvent e
) {
2684 public void mouseExit(MouseEvent e
) {
2685 if (fMouseOverSplitLine
) {
2686 fMouseOverSplitLine
= false;
2689 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2693 public void mouseHover(MouseEvent e
) {
2697 public void mouseScrolled(MouseEvent e
) {
2698 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2703 * On some platforms the mouse scroll event is sent to the
2704 * control that has focus even if it is not under the cursor.
2705 * Handle the event only if over the time graph control.
2707 Point size
= getSize();
2708 Rectangle bounds
= new Rectangle(0, 0, size
.x
, size
.y
);
2709 if (!bounds
.contains(e
.x
, e
.y
)) {
2713 boolean horizontalZoom
= false;
2714 boolean horizontalScroll
= false;
2715 boolean verticalZoom
= false;
2716 boolean verticalScroll
= false;
2718 // over the time graph control
2719 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2720 verticalZoom
= true;
2721 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2722 // over the name space
2723 verticalScroll
= true;
2725 // over the state area
2726 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2727 // over the state area, CTRL pressed
2728 horizontalZoom
= true;
2729 } else if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2730 // over the state area, SHIFT pressed
2731 horizontalScroll
= true;
2733 // over the state area, no modifier pressed
2734 verticalScroll
= true;
2738 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2739 verticalZoom(e
.count
> 0);
2740 if (fVerticalZoomAlignEntry
!= null) {
2741 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2743 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2745 } else if (horizontalScroll
) {
2746 horizontalScroll(e
.count
> 0);
2747 } else if (verticalScroll
){
2748 setTopIndex(getTopIndex() - e
.count
);
2753 * Get the vertical zoom alignment entry and position based on the current
2754 * selection. If there is no selection or if the selection is not visible,
2755 * return an alignment entry with a null time graph entry.
2757 * @return a map entry where the key is the selection's time graph entry and
2758 * the value is the center y-coordinate of that entry, or null
2760 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2761 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2762 if (alignEntry
!= null) {
2765 int index
= getSelectedIndex();
2766 if (index
== -1 || index
>= getExpandedElementCount()) {
2767 return new SimpleEntry
<>(null, 0);
2769 Rectangle bounds
= getClientArea();
2770 Rectangle itemRect
= getItemRect(bounds
, index
);
2771 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2772 /* selection is not visible */
2773 return new SimpleEntry
<>(null, 0);
2775 ITimeGraphEntry entry
= getExpandedElement(index
);
2776 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2777 return new SimpleEntry
<>(entry
, y
);
2781 * Get the vertical zoom alignment entry and position at the specified
2785 * the cursor y-coordinate
2786 * @return a map entry where the key is the time graph entry under the
2787 * cursor and the value is the cursor y-coordinate
2789 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2790 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2791 if (alignEntry
!= null) {
2794 int index
= getItemIndexAtY(y
);
2796 index
= getExpandedElementCount() - 1;
2798 ITimeGraphEntry entry
= getExpandedElement(index
);
2799 return new SimpleEntry
<>(entry
, y
);
2803 * Get the vertical zoom alignment entry and position if there is an ongoing
2804 * one and we are within the vertical zoom delay, or otherwise return null.
2806 * @return a map entry where the key is a time graph entry and the value is
2807 * a y-coordinate, or null
2809 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2810 long currentTimeMillis
= System
.currentTimeMillis();
2811 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2813 * If the vertical zoom is triggered repeatedly in a short amount of
2814 * time, use the initial event's entry and position.
2816 fVerticalZoomAlignTime
= currentTimeMillis
;
2817 return fVerticalZoomAlignEntry
;
2819 fVerticalZoomAlignTime
= currentTimeMillis
;
2824 public void handleEvent(Event event
) {
2825 if (event
.type
== SWT
.MouseWheel
) {
2826 // prevent horizontal scrolling when the mouse wheel is used to
2827 // scroll vertically or zoom
2833 public int getBorderWidth() {
2834 return fBorderWidth
;
2838 * Set the border width
2840 * @param borderWidth
2843 public void setBorderWidth(int borderWidth
) {
2844 this.fBorderWidth
= borderWidth
;
2848 * @return The current height of the header row
2850 public int getHeaderHeight() {
2851 return fHeaderHeight
;
2855 * Set the height of the header row
2857 * @param headerHeight
2860 public void setHeaderHeight(int headerHeight
) {
2861 this.fHeaderHeight
= headerHeight
;
2865 * @return The default height of regular item rows
2867 public int getItemHeight() {
2868 return fGlobalItemHeight
;
2872 * Set the default height of regular item rows.
2877 public void setItemHeight(int rowHeight
) {
2878 this.fGlobalItemHeight
= rowHeight
;
2879 for (Item item
: fItemData
.fItems
) {
2880 item
.fItemHeight
= rowHeight
;
2885 * Set the height of a specific item. Overrides the default item height.
2888 * A time graph entry
2891 * @return true if the height is successfully stored, false otherwise
2893 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2894 Item item
= fItemData
.findItem(entry
);
2896 item
.fItemHeight
= rowHeight
;
2903 * Set the minimum item width
2905 * @param width The minimum width
2907 public void setMinimumItemWidth(int width
) {
2908 this.fMinimumItemWidth
= width
;
2912 * @return The minimum item width
2914 public int getMinimumItemWidth() {
2915 return fMinimumItemWidth
;
2919 * Set whether all time events with a duration shorter than one pixel should
2920 * be blended in. If false, only the first such time event will be drawn and
2921 * the subsequent time events in the same pixel will be discarded. The
2922 * default value is false.
2925 * true if sub-pixel events should be blended, false otherwise.
2928 public void setBlendSubPixelEvents(boolean blend
) {
2929 fBlendSubPixelEvents
= blend
;
2933 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2934 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2935 fSelectionChangedListeners
.add(listener
);
2940 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2941 if (listener
!= null) {
2942 fSelectionChangedListeners
.remove(listener
);
2947 public void setSelection(ISelection selection
) {
2948 if (selection
instanceof TimeGraphSelection
) {
2949 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2950 Object ob
= sel
.getFirstElement();
2951 if (ob
instanceof ITimeGraphEntry
) {
2952 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2953 selectItem(trace
, false);
2960 * @param filter The filter object to be attached to the view
2962 public void addFilter(@NonNull ViewerFilter filter
) {
2963 if (!fFilters
.contains(filter
)) {
2964 fFilters
.add(filter
);
2969 * @param filter The filter object to be attached to the view
2971 public void removeFilter(@NonNull ViewerFilter filter
) {
2972 fFilters
.remove(filter
);
2976 * Returns this control's filters.
2978 * @return an array of viewer filters
2981 public @NonNull ViewerFilter
[] getFilters() {
2982 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
2986 * Sets the filters, replacing any previous filters.
2989 * an array of viewer filters, or null
2992 public void setFilters(@NonNull ViewerFilter
[] filters
) {
2994 if (filters
!= null) {
2995 fFilters
.addAll(Arrays
.asList(filters
));
3000 public void colorSettingsChanged(StateItem
[] stateItems
) {
3001 /* Destroy previous colors from the resource manager */
3002 if (fEventColorMap
!= null) {
3003 for (Color color
: fEventColorMap
) {
3004 fResourceManager
.destroyColor(color
.getRGB());
3007 if (stateItems
!= null) {
3008 fEventColorMap
= new Color
[stateItems
.length
];
3009 for (int i
= 0; i
< stateItems
.length
; i
++) {
3010 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3013 fEventColorMap
= new Color
[] { };
3018 private class ItemData
{
3019 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3020 private Item
[] fExpandedItems
= new Item
[0];
3021 private Item
[] fItems
= new Item
[0];
3022 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3023 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3028 public Item
findItem(ITimeGraphEntry entry
) {
3029 return fItemMap
.get(entry
);
3032 public int findItemIndex(ITimeGraphEntry entry
) {
3033 Item item
= fItemMap
.get(entry
);
3037 return item
.fExpandedIndex
;
3040 public void refreshData() {
3041 ITimeGraphEntry selection
= getSelectedTrace();
3042 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3043 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3044 ITimeGraphEntry entry
= fRootEntries
[i
];
3045 refreshData(itemMap
, null, 0, entry
);
3048 fItems
= fItemMap
.values().toArray(new Item
[0]);
3049 updateExpandedItems();
3050 if (selection
!= null) {
3051 for (Item item
: fExpandedItems
) {
3052 if (item
.fEntry
== selection
) {
3053 item
.fSelected
= true;
3060 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3061 Item item
= new Item(entry
, entry
.getName(), level
);
3062 if (parent
!= null) {
3063 parent
.fChildren
.add(item
);
3065 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3066 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3068 item
.fItemHeight
= fGlobalItemHeight
;
3070 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3071 itemMap
.put(entry
, item
);
3072 if (entry
.hasChildren()) {
3073 Item oldItem
= fItemMap
.get(entry
);
3074 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3075 /* existing items keep their old expanded state */
3076 item
.fExpanded
= oldItem
.fExpanded
;
3078 /* new items set the expanded state according to auto-expand level */
3079 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3081 item
.fHasChildren
= true;
3082 for (ITimeGraphEntry child
: entry
.getChildren()) {
3083 refreshData(itemMap
, item
, level
+ 1, child
);
3088 public void updateExpandedItems() {
3089 for (Item item
: fItems
) {
3090 item
.fExpandedIndex
= -1;
3092 List
<Item
> expandedItemList
= new ArrayList
<>();
3093 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3094 ITimeGraphEntry entry
= fRootEntries
[i
];
3095 Item item
= findItem(entry
);
3096 refreshExpanded(expandedItemList
, item
);
3098 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3099 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3102 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3103 // Check for filters
3104 boolean display
= true;
3105 for (ViewerFilter filter
: fFilters
) {
3106 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3112 item
.fExpandedIndex
= expandedItemList
.size();
3113 expandedItemList
.add(item
);
3114 if (item
.fHasChildren
&& item
.fExpanded
) {
3115 for (Item child
: item
.fChildren
) {
3116 refreshExpanded(expandedItemList
, child
);
3122 public void refreshData(ITimeGraphEntry
[] entries
) {
3123 if (entries
== null) {
3124 fRootEntries
= null;
3126 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3132 public void refreshArrows(List
<ILinkEvent
> events
) {
3133 /* If links are null, reset the list */
3134 if (events
!= null) {
3137 fLinks
= new ArrayList
<>();
3141 public ITimeGraphEntry
[] getEntries() {
3142 return fRootEntries
;
3146 private class Item
{
3147 private boolean fExpanded
;
3148 private int fExpandedIndex
;
3149 private boolean fSelected
;
3150 private boolean fHasChildren
;
3151 private int fItemHeight
;
3152 private final int fLevel
;
3153 private final List
<Item
> fChildren
;
3154 private final String fName
;
3155 private final ITimeGraphEntry fEntry
;
3157 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3158 this.fEntry
= entry
;
3160 this.fLevel
= level
;
3161 this.fChildren
= new ArrayList
<>();
3165 public String
toString() {
3171 public void menuDetected(MenuDetectEvent e
) {
3172 if (null == fTimeProvider
) {
3175 if (e
.detail
== SWT
.MENU_MOUSE
) {
3176 if (fPendingMenuDetectEvent
== null) {
3177 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3178 * Store the event and trigger it later just before handling mouseUp.
3179 * This allows for the method to detect if mouse is used to drag zoom.
3181 fPendingMenuDetectEvent
= e
;
3184 fPendingMenuDetectEvent
= null;
3185 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
3189 if (fDragState
!= DRAG_NONE
) {
3193 Point p
= toControl(e
.x
, e
.y
);
3194 int idx
= getItemIndexAtY(p
.y
);
3195 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3196 Item item
= fItemData
.fExpandedItems
[idx
];
3197 ITimeGraphEntry entry
= item
.fEntry
;
3198 if (entry
.hasTimeEvents()) {
3199 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3200 if (event
!= null) {
3202 fireMenuEventOnTimeEvent(e
);
3207 fireMenuEventOnTimeGraphEntry(e
);
3212 * Perform the alignment operation.
3215 * the alignment offset
3217 * @see ITmfTimeAligned
3221 public void performAlign(int offset
) {
3222 fTimeProvider
.setNameSpace(offset
);
3226 * Return the time alignment information
3228 * @return the time alignment information
3230 * @see ITmfTimeAligned
3234 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3235 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());