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);
211 public void dispose() {
213 fResourceManager
.dispose();
214 for (Font font
: fFonts
.values()) {
220 * Sets the timegraph provider used by this timegraph viewer.
222 * @param timeGraphProvider the timegraph provider
224 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
225 fTimeGraphProvider
= timeGraphProvider
;
227 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
228 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
229 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
232 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
233 colorSettingsChanged(stateItems
);
237 * Gets the timegraph provider used by this timegraph viewer.
239 * @return the timegraph provider, or <code>null</code> if not set.
241 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
242 return fTimeGraphProvider
;
246 * Gets the color map used by this timegraph viewer.
248 * @return a color map, or <code>null</code> if not set.
250 public Color
[] getEventColorMap() {
251 return fEventColorMap
;
255 * Assign the given time provider
257 * @param timeProvider
260 public void setTimeProvider(ITimeDataProvider timeProvider
) {
261 fTimeProvider
= timeProvider
;
266 * Assign the status line manager
268 * @param statusLineManager
269 * The status line manager, or null to disable status line messages
271 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
272 if (fStatusLineManager
!= null && statusLineManager
== null) {
273 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
275 fStatusLineManager
= statusLineManager
;
279 * Assign the time graph scale
281 * @param timeGraphScale
282 * The time graph scale
284 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
285 fTimeGraphScale
= timeGraphScale
;
289 * Add a selection listener
292 * The listener to add
294 public void addSelectionListener(SelectionListener listener
) {
295 if (listener
== null) {
296 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
298 if (null == fSelectionListeners
) {
299 fSelectionListeners
= new ArrayList
<>();
301 fSelectionListeners
.add(listener
);
305 * Remove a selection listener
308 * The listener to remove
310 public void removeSelectionListener(SelectionListener listener
) {
311 if (null != fSelectionListeners
) {
312 fSelectionListeners
.remove(listener
);
317 * Selection changed callback
319 public void fireSelectionChanged() {
320 if (null != fSelectionListeners
) {
321 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
322 while (it
.hasNext()) {
323 SelectionListener listener
= it
.next();
324 listener
.widgetSelected(null);
330 * Default selection callback
332 public void fireDefaultSelection() {
333 if (null != fSelectionListeners
) {
334 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
335 while (it
.hasNext()) {
336 SelectionListener listener
= it
.next();
337 listener
.widgetDefaultSelected(null);
343 * Add a drag selection listener
346 * The listener to add
348 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
349 if (listener
== null) {
350 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
352 if (null == fDragSelectionListeners
) {
353 fDragSelectionListeners
= new ArrayList
<>();
355 fDragSelectionListeners
.add(listener
);
359 * Remove a drag selection listener
362 * The listener to remove
364 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
365 if (null != fDragSelectionListeners
) {
366 fDragSelectionListeners
.remove(listener
);
371 * Drag Selection changed callback
374 * Time interval start
378 public void fireDragSelectionChanged(long start
, long end
) {
379 // check for backward intervals
380 long beginTime
, endTime
;
388 // call the listeners
389 if (null != fDragSelectionListeners
) {
390 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
391 while (it
.hasNext()) {
392 ITimeGraphTimeListener listener
= it
.next();
393 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
399 * Get the traces in the model
401 * @return The array of traces
403 public ITimeGraphEntry
[] getTraces() {
404 return fItemData
.getEntries();
408 * Refresh the data for the thing
410 public void refreshData() {
411 fItemData
.refreshData();
416 * Refresh data for the given traces
419 * The traces to refresh
421 public void refreshData(ITimeGraphEntry
[] traces
) {
422 fItemData
.refreshData(traces
);
427 * Refresh the links (arrows) of this widget
429 * @param events The link event list
431 public void refreshArrows(List
<ILinkEvent
> events
) {
432 fItemData
.refreshArrows(events
);
436 * Get the links (arrows) of this widget
438 * @return The unmodifiable link event list
442 public List
<ILinkEvent
> getArrows() {
443 return Collections
.unmodifiableList(fItemData
.fLinks
);
446 boolean ensureVisibleItem(int idx
, boolean redraw
) {
447 boolean changed
= false;
450 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
451 if (fItemData
.fExpandedItems
[index
].fSelected
) {
456 if (index
>= fItemData
.fExpandedItems
.length
) {
459 if (index
< fTopIndex
) {
466 int page
= countPerPage();
467 if (index
>= fTopIndex
+ page
) {
468 setTopIndex(index
- page
+ 1);
479 * Assign the given index as the top one
484 public void setTopIndex(int idx
) {
485 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
486 index
= Math
.max(0, index
);
492 * Set the top index so that the requested element is at the specified
496 * the time graph entry to be positioned
498 * the requested y-coordinate
501 public void setElementPosition(ITimeGraphEntry entry
, int y
) {
502 Item item
= fItemData
.fItemMap
.get(entry
);
503 if (item
== null || item
.fExpandedIndex
== -1) {
506 int index
= item
.fExpandedIndex
;
507 Rectangle itemRect
= getItemRect(getClientArea(), index
);
508 int delta
= itemRect
.y
+ itemRect
.height
- y
;
509 int topIndex
= getItemIndexAtY(delta
);
510 if (topIndex
!= -1) {
511 setTopIndex(topIndex
);
516 setTopIndex(getExpandedElementCount());
522 * Sets the auto-expand level to be used for new entries discovered when
523 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
524 * . The value 0 means that there is no auto-expand; 1 means that top-level
525 * entries are expanded, but not their children; 2 means that top-level
526 * entries are expanded, and their children, but not grand-children; and so
529 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
533 * non-negative level, or <code>ALL_LEVELS</code> to expand all
536 public void setAutoExpandLevel(int level
) {
537 fAutoExpandLevel
= level
;
541 * Returns the auto-expand level.
543 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
544 * the tree are expanded automatically
545 * @see #setAutoExpandLevel
547 public int getAutoExpandLevel() {
548 return fAutoExpandLevel
;
552 * Get the expanded state of a given entry.
556 * @return true if the entry is expanded, false if collapsed
559 public boolean getExpandedState(ITimeGraphEntry entry
) {
560 Item item
= fItemData
.fItemMap
.get(entry
);
561 return (item
!= null ? item
.fExpanded
: false);
565 * Set the expanded state of a given entry
570 * True if expanded, false if collapsed
572 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
573 Item item
= fItemData
.findItem(entry
);
574 if (item
!= null && item
.fExpanded
!= expanded
) {
575 item
.fExpanded
= expanded
;
576 fItemData
.updateExpandedItems();
582 * Collapses all nodes of the viewer's tree, starting with the root.
584 public void collapseAll() {
585 for (Item item
: fItemData
.fItems
) {
586 item
.fExpanded
= false;
588 fItemData
.updateExpandedItems();
593 * Expands all nodes of the viewer's tree, starting with the root.
595 public void expandAll() {
596 for (Item item
: fItemData
.fItems
) {
597 item
.fExpanded
= true;
599 fItemData
.updateExpandedItems();
604 * Add a tree listener
607 * The listener to add
609 public void addTreeListener(ITimeGraphTreeListener listener
) {
610 if (!fTreeListeners
.contains(listener
)) {
611 fTreeListeners
.add(listener
);
616 * Remove a tree listener
619 * The listener to remove
621 public void removeTreeListener(ITimeGraphTreeListener listener
) {
622 if (fTreeListeners
.contains(listener
)) {
623 fTreeListeners
.remove(listener
);
628 * Tree event callback
633 * The expanded state (true for expanded, false for collapsed)
635 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
636 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
637 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
639 listener
.treeExpanded(event
);
641 listener
.treeCollapsed(event
);
647 * Add a menu listener on {@link ITimeGraphEntry}s
650 * The listener to add
652 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
653 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
654 fTimeGraphEntryMenuListeners
.add(listener
);
659 * Remove a menu listener on {@link ITimeGraphEntry}s
662 * The listener to remove
664 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
665 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
666 fTimeGraphEntryMenuListeners
.remove(listener
);
671 * Menu event callback on {@link ITimeGraphEntry}s
674 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
676 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
677 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
678 listener
.menuDetected(event
);
683 * Add a menu listener on {@link ITimeEvent}s
686 * The listener to add
688 public void addTimeEventMenuListener(MenuDetectListener listener
) {
689 if (!fTimeEventMenuListeners
.contains(listener
)) {
690 fTimeEventMenuListeners
.add(listener
);
695 * Remove a menu listener on {@link ITimeEvent}s
698 * The listener to remove
700 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
701 if (fTimeEventMenuListeners
.contains(listener
)) {
702 fTimeEventMenuListeners
.remove(listener
);
707 * Menu event callback on {@link ITimeEvent}s
710 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
712 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
713 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
714 listener
.menuDetected(event
);
719 public ISelection
getSelection() {
720 TimeGraphSelection sel
= new TimeGraphSelection();
721 ITimeGraphEntry trace
= getSelectedTrace();
722 if (null != trace
&& null != fTimeProvider
) {
723 long selectedTime
= fTimeProvider
.getSelectionBegin();
724 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
735 * Get the selection object
737 * @return The selection
739 public ISelection
getSelectionTrace() {
740 TimeGraphSelection sel
= new TimeGraphSelection();
741 ITimeGraphEntry trace
= getSelectedTrace();
749 * Enable/disable one of the traces in the model
752 * 1 to enable it, -1 to disable. The method returns immediately
753 * if another value is used.
755 public void selectTrace(int n
) {
756 if ((n
!= 1) && (n
!= -1)) {
760 boolean changed
= false;
761 int lastSelection
= -1;
762 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
763 Item item
= fItemData
.fExpandedItems
[i
];
764 if (item
.fSelected
) {
766 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
767 item
.fSelected
= false;
768 item
= fItemData
.fExpandedItems
[i
+ 1];
769 item
.fSelected
= true;
771 } else if ((-1 == n
) && (i
> 0)) {
772 item
.fSelected
= false;
773 item
= fItemData
.fExpandedItems
[i
- 1];
774 item
.fSelected
= true;
781 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
782 Item item
= fItemData
.fExpandedItems
[0];
783 item
.fSelected
= true;
788 ensureVisibleItem(-1, false);
790 fireSelectionChanged();
798 * 1 for next event, -1 for previous event
800 * true to extend selection range, false for single selection
803 public void selectEvent(int n
, boolean extend
) {
804 if (null == fTimeProvider
) {
807 ITimeGraphEntry trace
= getSelectedTrace();
811 long selectedTime
= fTimeProvider
.getSelectionEnd();
812 long endTime
= fTimeProvider
.getMaxTime();
813 ITimeEvent nextEvent
;
814 if (n
== -1 && selectedTime
> endTime
) {
815 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
817 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
819 if (null == nextEvent
&& n
== -1) {
820 nextEvent
= Utils
.getFirstEvent(trace
);
822 if (null != nextEvent
) {
823 long nextTime
= nextEvent
.getTime();
824 // If last event detected e.g. going back or not moving to a next
826 if (nextTime
<= selectedTime
&& n
== 1) {
827 // Select to the end of this last event
828 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
829 // but not beyond the end of the trace
830 if (nextTime
> endTime
) {
833 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
834 // for previous event go to its end time unless we were already there
835 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
838 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
840 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
842 fireSelectionChanged();
845 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
847 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
849 fireSelectionChanged();
851 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
855 * Select the next event
858 * true to extend selection range, false for single selection
861 public void selectNextEvent(boolean extend
) {
862 selectEvent(1, extend
);
863 // Notify if visible time window has been adjusted
864 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
868 * Select the previous event
871 * true to extend selection range, false for single selection
874 public void selectPrevEvent(boolean extend
) {
875 selectEvent(-1, extend
);
876 // Notify if visible time window has been adjusted
877 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
881 * Select the next trace
883 public void selectNextTrace() {
888 * Select the previous trace
890 public void selectPrevTrace() {
895 * Scroll left or right by one half window size
898 * true to scroll left, false to scroll right
900 public void horizontalScroll(boolean left
) {
901 long time0
= fTimeProvider
.getTime0();
902 long time1
= fTimeProvider
.getTime1();
903 long timeMin
= fTimeProvider
.getMinTime();
904 long timeMax
= fTimeProvider
.getMaxTime();
905 long range
= time1
- time0
;
909 long increment
= Math
.max(1, range
/ 2);
911 time0
= Math
.max(time0
- increment
, timeMin
);
912 time1
= time0
+ range
;
914 time1
= Math
.min(time1
+ increment
, timeMax
);
915 time0
= time1
- range
;
917 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
921 * Zoom based on mouse cursor location with mouse scrolling
923 * @param zoomIn true to zoom in, false to zoom out
925 public void zoom(boolean zoomIn
) {
926 int globalX
= getDisplay().getCursorLocation().x
;
927 Point p
= toControl(globalX
, 0);
928 int nameSpace
= fTimeProvider
.getNameSpace();
929 int timeSpace
= fTimeProvider
.getTimeSpace();
930 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
931 long time0
= fTimeProvider
.getTime0();
932 long time1
= fTimeProvider
.getTime1();
933 long interval
= time1
- time0
;
936 } // to allow getting out of single point interval
939 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
941 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
943 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
944 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
945 long newTime1
= newTime0
+ newInterval
;
946 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
950 * zoom in using single click
952 public void zoomIn() {
953 long prevTime0
= fTimeProvider
.getTime0();
954 long prevTime1
= fTimeProvider
.getTime1();
955 long prevRange
= prevTime1
- prevTime0
;
956 if (prevRange
== 0) {
959 ITimeDataProvider provider
= fTimeProvider
;
960 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
961 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
962 selTime
= (prevTime0
+ prevTime1
) / 2;
964 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
965 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
967 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
969 if (inaccuracy
> 0 && inaccuracy
< 100) {
970 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
974 long min
= fTimeProvider
.getMinTimeInterval();
975 if ((time1
- time0
) < min
) {
976 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
980 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
984 * zoom out using single click
986 public void zoomOut() {
987 long prevTime0
= fTimeProvider
.getTime0();
988 long prevTime1
= fTimeProvider
.getTime1();
989 ITimeDataProvider provider
= fTimeProvider
;
990 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
991 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
992 selTime
= (prevTime0
+ prevTime1
) / 2;
994 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
995 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
997 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
998 if (inaccuracy
> 0 && inaccuracy
< 100) {
999 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
1003 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1010 * true to zoom in, false to zoom out
1013 public void verticalZoom(boolean zoomIn
) {
1015 fHeightAdjustment
++;
1017 fHeightAdjustment
--;
1019 fItemData
.refreshData();
1024 * Reset the vertical zoom to default.
1028 public void resetVerticalZoom() {
1029 fHeightAdjustment
= 0;
1030 fItemData
.refreshData();
1035 * Set the grid lines visibility. The default is true.
1038 * true to show the grid lines, false otherwise
1041 public void setGridLinesVisible(boolean visible
) {
1042 fGridLinesVisible
= visible
;
1046 * Get the grid lines visibility.
1048 * @return true if the grid lines are visible, false otherwise
1051 public boolean getGridLinesVisible() {
1052 return fGridLinesVisible
;
1056 * Set the grid line color. The default is SWT.COLOR_GRAY.
1059 * the grid line color
1062 public void setGridLineColor(Color color
) {
1063 fGridLineColor
= color
;
1067 * Get the grid line color.
1069 * @return the grid line color
1072 public Color
getGridLineColor() {
1073 return fGridLineColor
;
1077 * Set the markers list.
1080 * The markers list, or null
1083 public void setMarkers(List
<IMarkerEvent
> markers
) {
1088 * Get the markers list.
1090 * @return The markers list, or null
1093 public List
<IMarkerEvent
> getMarkers() {
1098 * Set the markers visibility. The default is true.
1101 * true to show the markers, false otherwise
1104 public void setMarkersVisible(boolean visible
) {
1105 fMarkersVisible
= visible
;
1109 * Get the markers visibility.
1111 * @return true if the markers are visible, false otherwise
1114 public boolean getMarkersVisible() {
1115 return fMarkersVisible
;
1121 * @param hideArrows true to hide arrows
1123 public void hideArrows(boolean hideArrows
) {
1124 fHideArrows
= hideArrows
;
1128 * Follow the arrow forward
1131 * true to extend selection range, false for single selection
1134 public void followArrowFwd(boolean extend
) {
1135 ITimeGraphEntry trace
= getSelectedTrace();
1136 if (trace
== null) {
1139 long selectedTime
= fTimeProvider
.getSelectionEnd();
1140 for (ILinkEvent link
: fItemData
.fLinks
) {
1141 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1142 selectItem(link
.getDestinationEntry(), false);
1143 if (link
.getDuration() != 0) {
1145 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1147 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1149 // Notify if visible time window has been adjusted
1150 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1152 fireSelectionChanged();
1153 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1157 selectNextEvent(extend
);
1161 * Follow the arrow backward
1164 * true to extend selection range, false for single selection
1167 public void followArrowBwd(boolean extend
) {
1168 ITimeGraphEntry trace
= getSelectedTrace();
1169 if (trace
== null) {
1172 long selectedTime
= fTimeProvider
.getSelectionEnd();
1173 for (ILinkEvent link
: fItemData
.fLinks
) {
1174 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1175 selectItem(link
.getEntry(), false);
1176 if (link
.getDuration() != 0) {
1178 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1180 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1182 // Notify if visible time window has been adjusted
1183 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1185 fireSelectionChanged();
1186 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1190 selectPrevEvent(extend
);
1194 * Return the currently selected trace
1196 * @return The entry matching the trace
1198 public ITimeGraphEntry
getSelectedTrace() {
1199 ITimeGraphEntry trace
= null;
1200 int idx
= getSelectedIndex();
1202 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1208 * Retrieve the index of the currently selected item
1212 public int getSelectedIndex() {
1214 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1215 Item item
= fItemData
.fExpandedItems
[i
];
1216 if (item
.fSelected
) {
1224 boolean toggle(int idx
) {
1225 boolean toggled
= false;
1226 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1227 Item item
= fItemData
.fExpandedItems
[idx
];
1228 if (item
.fHasChildren
) {
1229 item
.fExpanded
= !item
.fExpanded
;
1230 fItemData
.updateExpandedItems();
1233 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1240 * Gets the index of the item at the given location.
1244 * @return the index of the item at the given location, of -1 if none.
1246 protected int getItemIndexAtY(int y
) {
1249 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1250 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1256 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1257 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1266 boolean isOverSplitLine(int x
) {
1267 if (x
< 0 || null == fTimeProvider
) {
1270 int nameWidth
= fTimeProvider
.getNameSpace();
1271 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1275 * Gets the {@link ITimeGraphEntry} at the given location.
1278 * a point in the widget
1279 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1282 protected ITimeGraphEntry
getEntry(Point pt
) {
1283 int idx
= getItemIndexAtY(pt
.y
);
1284 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1288 * Return the arrow event closest to the given point that is no further than
1289 * a maximum distance.
1292 * a point in the widget
1293 * @return The closest arrow event, or null if there is none close enough.
1295 protected ILinkEvent
getArrow(Point pt
) {
1299 ILinkEvent linkEvent
= null;
1300 double minDistance
= Double
.MAX_VALUE
;
1301 for (ILinkEvent event
: fItemData
.fLinks
) {
1302 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1306 int x2
= x1
+ rect
.width
;
1307 int y2
= y1
+ rect
.height
;
1308 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1309 if (minDistance
> d
) {
1315 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1322 public int getXForTime(long time
) {
1323 if (null == fTimeProvider
) {
1326 long time0
= fTimeProvider
.getTime0();
1327 long time1
= fTimeProvider
.getTime1();
1328 int width
= getSize().x
;
1329 int nameSpace
= fTimeProvider
.getNameSpace();
1330 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1331 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1336 public long getTimeAtX(int coord
) {
1337 if (null == fTimeProvider
) {
1341 Point size
= getSize();
1342 long time0
= fTimeProvider
.getTime0();
1343 long time1
= fTimeProvider
.getTime1();
1344 int nameWidth
= fTimeProvider
.getNameSpace();
1345 final int x
= coord
- nameWidth
;
1346 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1347 if (x
>= 0 && size
.x
>= nameWidth
) {
1348 if (time1
- time0
> timeWidth
) {
1349 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1350 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1352 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1353 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1359 void selectItem(int idx
, boolean addSelection
) {
1360 boolean changed
= false;
1362 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1363 Item item
= fItemData
.fExpandedItems
[idx
];
1364 changed
= !item
.fSelected
;
1365 item
.fSelected
= true;
1368 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1369 Item item
= fItemData
.fExpandedItems
[i
];
1370 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1373 item
.fSelected
= i
== idx
;
1376 changed
|= ensureVisibleItem(idx
, true);
1383 * Callback for item selection
1386 * The entry matching the trace
1387 * @param addSelection
1388 * If the selection is added or removed
1390 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1391 int idx
= fItemData
.findItemIndex(trace
);
1392 selectItem(idx
, addSelection
);
1396 * Retrieve the number of entries shown per page.
1400 public int countPerPage() {
1401 int height
= getSize().y
;
1404 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1405 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1406 if (ySum
>= height
) {
1411 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1412 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1413 if (ySum
>= height
) {
1422 * Get the index of the top element
1426 public int getTopIndex() {
1431 * Get the number of expanded (visible) items
1433 * @return The count of expanded (visible) items
1435 public int getExpandedElementCount() {
1436 return fItemData
.fExpandedItems
.length
;
1440 * Get an array of all expanded (visible) elements
1442 * @return The expanded (visible) elements
1444 public ITimeGraphEntry
[] getExpandedElements() {
1445 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1446 for (Item item
: fItemData
.fExpandedItems
) {
1447 elements
.add(item
.fEntry
);
1449 return elements
.toArray(new ITimeGraphEntry
[0]);
1453 * Get the expanded (visible) element at the specified index.
1457 * @return The expanded (visible) element or null if out of range
1460 public ITimeGraphEntry
getExpandedElement(int index
) {
1461 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1464 return fItemData
.fExpandedItems
[index
].fEntry
;
1467 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1468 Rectangle rect
= getItemRect(bounds
, idx
);
1469 rect
.width
= nameWidth
;
1473 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1474 Rectangle rect
= getItemRect(bounds
, idx
);
1475 rect
.x
+= nameWidth
;
1476 rect
.width
-= nameWidth
;
1480 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1482 if (idx
>= fTopIndex
) {
1483 for (int i
= fTopIndex
; i
< idx
; i
++) {
1484 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1487 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1488 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1491 int y
= bounds
.y
+ ySum
;
1492 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1493 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1497 void paint(Rectangle bounds
, PaintEvent e
) {
1500 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1504 fIdealNameSpace
= 0;
1505 int nameSpace
= fTimeProvider
.getNameSpace();
1507 // draw the background layer
1508 drawBackground(bounds
, nameSpace
, gc
);
1510 // draw the grid lines
1511 drawGridLines(bounds
, gc
);
1513 // draw the background markers
1514 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1517 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1519 // draw the foreground markers
1520 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1522 // draw the links (arrows)
1523 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1525 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1527 int alpha
= gc
.getAlpha();
1530 long time0
= fTimeProvider
.getTime0();
1531 long time1
= fTimeProvider
.getTime1();
1532 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1533 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1534 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1535 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1536 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1538 // draw selection lines
1539 if (fDragState
!= DRAG_SELECTION
) {
1540 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1541 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1542 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1545 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1546 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1551 // draw selection background
1552 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1553 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1554 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1555 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1557 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1558 } else if (x0
- x1
> 1) {
1559 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1563 // draw drag selection background
1564 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1565 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1566 if (fDragX0
< fDragX
) {
1567 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1568 } else if (fDragX0
> fDragX
) {
1569 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1574 if (DRAG_SPLIT_LINE
== fDragState
) {
1575 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1576 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1577 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1578 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1579 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1580 if (fDragX
!= fDragX0
) {
1581 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1583 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1584 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1585 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1586 if (fDragX
!= fDragX0
) {
1587 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1589 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1590 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1591 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1598 * Draw the background layer. Fills the background of the control's name
1599 * space and states space, updates the background of items if necessary,
1600 * and draws the item's name text and middle line.
1603 * The bounds of the control
1605 * The name space width
1610 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1611 // draw empty name space background
1612 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1613 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1615 // draw empty states space background
1616 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1617 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1619 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1620 Rectangle itemRect
= getItemRect(bounds
, i
);
1621 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1624 Item item
= fItemData
.fExpandedItems
[i
];
1625 // draw the background of selected item and items with no time events
1626 if (! item
.fEntry
.hasTimeEvents()) {
1627 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1628 gc
.fillRectangle(itemRect
);
1629 } else if (item
.fSelected
) {
1630 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1631 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1632 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1633 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1635 // draw the name and middle line
1636 if (! item
.fEntry
.hasTimeEvents()) {
1637 drawName(item
, itemRect
, gc
);
1639 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1640 drawName(item
, nameRect
, gc
);
1641 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1642 drawMidLine(rect
, gc
);
1648 * Draw the grid lines
1651 * The bounds of the control
1656 public void drawGridLines(Rectangle bounds
, GC gc
) {
1657 if (!fGridLinesVisible
) {
1660 gc
.setForeground(fGridLineColor
);
1661 gc
.setAlpha(fGridLineColor
.getAlpha());
1662 for (int x
: fTimeGraphScale
.getTickList()) {
1663 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1672 * The rectangle of the area
1673 * @param timeProvider
1676 * The list of markers
1678 * true to draw the foreground markers, false otherwise
1680 * The width reserved for the names
1682 * Reference to the SWT GC object
1685 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1686 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1689 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1690 /* the list can grow concurrently but cannot shrink */
1691 for (int i
= 0; i
< markers
.size(); i
++) {
1692 IMarkerEvent marker
= markers
.get(i
);
1693 if (marker
.isForeground() == foreground
) {
1694 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1697 gc
.setClipping((Rectangle
) null);
1701 * Draw a single marker
1706 * The bounds of the control
1707 * @param timeProvider
1710 * The width reserved for the name
1712 * Reference to the SWT GC object
1715 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1716 Rectangle rect
= Utils
.clone(bounds
);
1717 if (marker
.getEntry() != null) {
1718 int index
= fItemData
.findItemIndex(marker
.getEntry());
1722 rect
= getStatesRect(bounds
, index
, nameSpace
);
1723 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1727 int x0
= getXForTime(marker
.getTime());
1728 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1729 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1732 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1733 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1735 Color color
= getColorScheme().getColor(marker
.getColor());
1736 gc
.setBackground(color
);
1737 gc
.setAlpha(color
.getAlpha());
1738 gc
.fillRectangle(rect
);
1740 String label
= marker
.getLabel();
1741 if (label
!= null && marker
.getEntry() != null) {
1742 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1743 gc
.setForeground(color
);
1744 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1749 * Draw many items at once
1752 * The bounds of the control
1753 * @param timeProvider
1756 * The array items to draw
1758 * The index of the first element to draw
1760 * The name space width
1764 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1765 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1766 for (int i
= topIndex
; i
< items
.length
; i
++) {
1767 Item item
= items
[i
];
1768 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1778 * The bounds of the control
1779 * @param timeProvider
1782 * The expanded item index
1784 * The name space width
1788 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1789 Rectangle itemRect
= getItemRect(bounds
, i
);
1790 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1794 ITimeGraphEntry entry
= item
.fEntry
;
1795 long time0
= timeProvider
.getTime0();
1796 long time1
= timeProvider
.getTime1();
1797 long selectedTime
= fTimeProvider
.getSelectionEnd();
1799 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1800 if (rect
.isEmpty() || (time1
<= time0
)) {
1801 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1805 boolean selected
= item
.fSelected
;
1806 // K pixels per second
1807 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1809 if (item
.fEntry
.hasTimeEvents()) {
1810 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1811 fillSpace(rect
, gc
, selected
);
1813 int margins
= getMarginForHeight(rect
.height
);
1814 int height
= rect
.height
- margins
;
1815 int topMargin
= (margins
+ 1) / 2;
1816 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1818 /* Set the font for this item */
1819 setFontForHeight(height
, gc
);
1821 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1822 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1825 while (iterator
.hasNext()) {
1826 ITimeEvent event
= iterator
.next();
1827 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1828 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1829 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1830 // event is out of bounds
1833 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1834 stateRect
.x
= Math
.max(rect
.x
, x
);
1835 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1836 if (stateRect
.x
== lastX
) {
1837 stateRect
.width
-= 1;
1838 if (stateRect
.width
> 0) {
1839 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1840 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1844 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1845 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1846 lastX
= stateRect
.x
;
1849 gc
.setClipping((Rectangle
) null);
1851 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1858 * The bounds of the control
1859 * @param timeProvider
1862 * The list of link events
1864 * The name space width
1868 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1869 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1873 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1874 /* the list can grow concurrently but cannot shrink */
1875 for (int i
= 0; i
< links
.size(); i
++) {
1876 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1878 gc
.setClipping((Rectangle
) null);
1882 * Draws a link type event
1885 * The link event to draw
1887 * The bounds of the control
1888 * @param timeProvider
1891 * The name space width
1895 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1896 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1899 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1900 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1901 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1903 if ((srcIndex
== -1) || (destIndex
== -1)) {
1907 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1908 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1910 int x0
= getXForTime(event
.getTime());
1911 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1913 // limit the x-coordinates to prevent integer overflow in calculations
1914 // and also GC.drawLine doesn't draw properly with large coordinates
1915 final int limit
= Integer
.MAX_VALUE
/ 1024;
1916 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1917 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1919 int y0
= src
.y
+ src
.height
/ 2;
1920 int y1
= dst
.y
+ dst
.height
/ 2;
1921 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1930 * Time event for which we're drawing the arrow
1932 * The arrow rectangle
1935 * @return true if the arrow was drawn
1937 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1938 Rectangle rect
, GC gc
) {
1943 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1947 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1950 Color stateColor
= null;
1951 if (colorIdx
< fEventColorMap
.length
) {
1952 stateColor
= fEventColorMap
[colorIdx
];
1954 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1957 gc
.setForeground(stateColor
);
1958 gc
.setBackground(stateColor
);
1960 /* Draw the arrow */
1961 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1962 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1965 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1970 * @author Francis Giraldeau
1973 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1975 * The algorithm was taken from this site, not the code itself
1977 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1980 double cos
= 0.9510;
1981 double sin
= 0.3090;
1982 long lenx
= x1
- x0
;
1983 long leny
= y1
- y0
;
1984 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1986 double dx
= factor
* lenx
/ len
;
1987 double dy
= factor
* leny
/ len
;
1988 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1989 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1990 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1991 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1992 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1993 gc
.fillPolygon(arrow
);
1997 * Draw the name of an item.
2002 * The bounds of the item's name space
2006 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2007 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2009 // No name to be drawn
2010 if (fTimeProvider
.getNameSpace() == 0) {
2014 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2015 setFontForHeight(height
, gc
);
2017 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2018 if (item
.fHasChildren
) {
2019 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2020 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2021 Rectangle rect
= Utils
.clone(bounds
);
2022 rect
.x
+= leftMargin
;
2023 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2024 rect
.width
= EXPAND_SIZE
;
2025 rect
.height
= EXPAND_SIZE
;
2026 gc
.fillRectangle(rect
);
2027 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2028 int midy
= rect
.y
+ rect
.height
/ 2;
2029 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2030 if (!item
.fExpanded
) {
2031 int midx
= rect
.x
+ rect
.width
/ 2;
2032 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2035 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2037 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2040 int imgHeight
= img
.getImageData().height
;
2041 int imgWidth
= img
.getImageData().width
;
2043 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2044 gc
.drawImage(img
, x
, y
);
2045 leftMargin
+= imgWidth
+ MARGIN
;
2047 String name
= item
.fName
;
2048 Point size
= gc
.stringExtent(name
);
2049 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2050 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2052 if (hasTimeEvents
) {
2053 // cut long string with "..."
2054 int width
= bounds
.width
- leftMargin
;
2056 while (size
.x
> width
&& name
.length() > 1) {
2058 name
= name
.substring(0, name
.length() - 1);
2059 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2062 name
+= "..."; //$NON-NLS-1$
2065 Rectangle rect
= Utils
.clone(bounds
);
2066 rect
.x
+= leftMargin
;
2067 rect
.width
-= leftMargin
;
2069 if (rect
.width
> 0) {
2070 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2071 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2072 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2073 leftMargin
+= textWidth
+ MARGIN
;
2075 if (hasTimeEvents
) {
2077 rect
.x
= bounds
.x
+ leftMargin
;
2079 rect
.width
= bounds
.width
- rect
.x
;
2080 drawMidLine(rect
, gc
);
2086 * Draw the state (color fill)
2091 * Time event for which we're drawing the state
2093 * The state rectangle
2097 * Is this time event currently selected (so it appears
2099 * @param timeSelected
2100 * Is the timestamp currently selected
2101 * @return true if the state was drawn
2103 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2104 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2106 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2107 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2110 boolean visible
= rect
.width
== 0 ?
false : true;
2111 rect
.width
= Math
.max(1, rect
.width
);
2112 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2113 gc
.setForeground(black
);
2115 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2117 // Only draw the top and bottom borders
2118 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2119 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2120 if (rect
.width
== 1) {
2121 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2124 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2127 Color stateColor
= null;
2128 if (colorIdx
< fEventColorMap
.length
) {
2129 stateColor
= fEventColorMap
[colorIdx
];
2134 boolean reallySelected
= timeSelected
&& selected
;
2135 // fill all rect area
2136 gc
.setBackground(stateColor
);
2138 gc
.fillRectangle(rect
);
2139 } else if (fBlendSubPixelEvents
) {
2141 gc
.fillRectangle(rect
);
2145 if (reallySelected
) {
2146 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2147 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2150 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2152 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2157 * Fill an item's states rectangle
2160 * The states rectangle
2164 * true if the item is selected
2166 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2167 /* Nothing to draw */
2171 * Draw a line at the middle height of a rectangle
2178 private void drawMidLine(Rectangle rect
, GC gc
) {
2179 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2180 int midy
= rect
.y
+ rect
.height
/ 2;
2181 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2184 private static int getMarginForHeight(int height
) {
2186 * State rectangle is smaller than the item bounds when height is > 4.
2187 * Don't use any margin if the height is below or equal that threshold.
2188 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2189 * pixels for the state height, but with a minimum margin of 1.
2191 final int MARGIN_THRESHOLD
= 4;
2192 final int PREFERRED_HEIGHT
= 13;
2193 final int MIN_MARGIN
= 1;
2194 final int MAX_MARGIN
= 6;
2195 return height
<= MARGIN_THRESHOLD ?
0 :
2196 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2199 private void setFontForHeight(int pixels
, GC gc
) {
2200 /* convert font height from pixels to points */
2201 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2202 Font font
= fFonts
.get(height
);
2204 FontData fontData
= gc
.getFont().getFontData()[0];
2205 fontData
.setHeight(height
);
2206 font
= new Font(gc
.getDevice(), fontData
);
2207 fFonts
.put(height
, font
);
2213 public void keyTraversed(TraverseEvent e
) {
2214 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2220 public void keyPressed(KeyEvent e
) {
2222 if (fItemData
.fExpandedItems
.length
== 0) {
2225 if (SWT
.HOME
== e
.keyCode
) {
2227 } else if (SWT
.END
== e
.keyCode
) {
2228 idx
= fItemData
.fExpandedItems
.length
- 1;
2229 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2230 idx
= getSelectedIndex();
2233 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2236 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2237 idx
= getSelectedIndex();
2240 } else if (idx
> 0) {
2243 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2244 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2245 selectPrevEvent(extend
);
2246 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2247 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2248 selectNextEvent(extend
);
2249 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2250 int page
= countPerPage();
2251 idx
= getSelectedIndex();
2256 if (idx
>= fItemData
.fExpandedItems
.length
) {
2257 idx
= fItemData
.fExpandedItems
.length
- 1;
2259 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2260 int page
= countPerPage();
2261 idx
= getSelectedIndex();
2269 } else if (SWT
.CR
== e
.keyCode
) {
2270 idx
= getSelectedIndex();
2272 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2275 fireDefaultSelection();
2279 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2280 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2282 if (fVerticalZoomAlignEntry
!= null) {
2283 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2285 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2286 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2287 verticalZoom(false);
2288 if (fVerticalZoomAlignEntry
!= null) {
2289 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2291 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2292 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2293 resetVerticalZoom();
2294 if (fVerticalZoomAlignEntry
!= null) {
2295 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2299 selectItem(idx
, false);
2300 fireSelectionChanged();
2302 int x
= toControl(e
.display
.getCursorLocation()).x
;
2303 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2307 public void keyReleased(KeyEvent e
) {
2308 int x
= toControl(e
.display
.getCursorLocation()).x
;
2309 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2313 public void focusGained(FocusEvent e
) {
2316 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2320 public void focusLost(FocusEvent e
) {
2322 if (DRAG_NONE
!= fDragState
) {
2324 fDragState
= DRAG_NONE
;
2327 updateStatusLine(NO_STATUS
);
2331 * @return If the current view is focused
2333 public boolean isInFocus() {
2338 * Provide the possibility to control the wait cursor externally e.g. data
2339 * requests in progress
2341 * @param waitInd Should we wait indefinitely?
2343 public void waitCursor(boolean waitInd
) {
2344 // Update cursor as indicated
2346 setCursor(fWaitCursor
);
2352 private void updateCursor(int x
, int stateMask
) {
2353 // if Wait cursor not active, check for the need to change the cursor
2354 if (getCursor() == fWaitCursor
) {
2357 Cursor cursor
= null;
2358 if (fDragState
== DRAG_SPLIT_LINE
) {
2359 } else if (fDragState
== DRAG_SELECTION
) {
2360 cursor
= fResizeCursor
;
2361 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2362 cursor
= fDragCursor
;
2363 } else if (fDragState
== DRAG_ZOOM
) {
2364 cursor
= fZoomCursor
;
2365 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2366 cursor
= fDragCursor
;
2367 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2368 cursor
= fResizeCursor
;
2369 } else if (!isOverSplitLine(x
)) {
2370 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2371 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2372 int xBegin
= getXForTime(selectionBegin
);
2373 int xEnd
= getXForTime(selectionEnd
);
2374 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2375 cursor
= fResizeCursor
;
2378 if (getCursor() != cursor
) {
2384 * Update the status line following a change of selection.
2388 public void updateStatusLine() {
2389 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2392 private void updateStatusLine(int x
) {
2393 // use the time provider of the time graph scale for the status line
2394 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2395 if (fStatusLineManager
== null || null == tdp
||
2396 tdp
.getTime0() == tdp
.getTime1()) {
2399 TimeFormat tf
= tdp
.getTimeFormat();
2400 Resolution res
= Resolution
.NANOSEC
;
2401 StringBuilder message
= new StringBuilder();
2402 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2403 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2404 long time
= getTimeAtX(x
);
2406 if (tdp
instanceof ITimeDataProviderConverter
) {
2407 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2409 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2411 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2412 Utils
.formatTime(time
, tf
, res
)
2416 long selectionBegin
= tdp
.getSelectionBegin();
2417 long selectionEnd
= tdp
.getSelectionEnd();
2418 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2420 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2421 Utils
.formatTime(selectionBegin
, tf
, res
)
2423 if (selectionBegin
!= selectionEnd
) {
2424 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2426 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2427 Utils
.formatTime(selectionEnd
, tf
, res
),
2428 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2431 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2432 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2433 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2434 if (tdp
instanceof ITimeDataProviderConverter
) {
2435 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2436 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2438 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2440 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2441 Utils
.formatTime(time0
, tf
, res
),
2442 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2443 Utils
.formatTime(time
, tf
, res
),
2444 Utils
.formatDelta(time
- time0
, tf
, res
)
2447 fStatusLineManager
.setMessage(message
.toString());
2451 public void mouseMove(MouseEvent e
) {
2452 if (null == fTimeProvider
) {
2455 Point size
= getSize();
2456 if (DRAG_TRACE_ITEM
== fDragState
) {
2457 int nameWidth
= fTimeProvider
.getNameSpace();
2458 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2460 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2461 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2462 long time1
= fTime1bak
- timeDelta
;
2463 long maxTime
= fTimeProvider
.getMaxTime();
2464 if (time1
> maxTime
) {
2467 long time0
= time1
- (fTime1bak
- fTime0bak
);
2468 if (time0
< fTimeProvider
.getMinTime()) {
2469 time0
= fTimeProvider
.getMinTime();
2470 time1
= time0
+ (fTime1bak
- fTime0bak
);
2472 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2474 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2476 fTimeProvider
.setNameSpace(e
.x
);
2477 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2478 } else if (DRAG_SELECTION
== fDragState
) {
2479 if (fDragBeginMarker
) {
2480 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2482 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2485 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2486 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2487 } else if (DRAG_ZOOM
== fDragState
) {
2488 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2490 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2491 } else if (DRAG_NONE
== fDragState
) {
2492 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2493 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2496 fMouseOverSplitLine
= mouseOverSplitLine
;
2498 updateCursor(e
.x
, e
.stateMask
);
2499 updateStatusLine(e
.x
);
2503 public void mouseDoubleClick(MouseEvent e
) {
2504 if (null == fTimeProvider
) {
2507 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2508 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2509 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2510 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2511 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2514 fMouseOverSplitLine
= mouseOverSplitLine
;
2515 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2518 int idx
= getItemIndexAtY(e
.y
);
2520 selectItem(idx
, false);
2521 fireDefaultSelection();
2527 public void mouseDown(MouseEvent e
) {
2528 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2529 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2530 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2534 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2535 int nameSpace
= fTimeProvider
.getNameSpace();
2536 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2537 fDragState
= DRAG_SPLIT_LINE
;
2538 fDragButton
= e
.button
;
2541 fTime0bak
= fTimeProvider
.getTime0();
2542 fTime1bak
= fTimeProvider
.getTime1();
2544 updateCursor(e
.x
, e
.stateMask
);
2548 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2549 int nameSpace
= fTimeProvider
.getNameSpace();
2550 idx
= getItemIndexAtY(e
.y
);
2552 Item item
= fItemData
.fExpandedItems
[idx
];
2553 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2557 selectItem(idx
, false);
2558 fireSelectionChanged();
2560 selectItem(idx
, false); // clear selection
2561 fireSelectionChanged();
2563 long hitTime
= getTimeAtX(e
.x
);
2567 fDragState
= DRAG_SELECTION
;
2568 fDragBeginMarker
= false;
2569 fDragButton
= e
.button
;
2572 fDragTime0
= getTimeAtX(fDragX0
);
2573 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2574 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2575 int xBegin
= getXForTime(selectionBegin
);
2576 int xEnd
= getXForTime(selectionEnd
);
2577 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2578 long time
= getTimeAtX(e
.x
);
2579 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2580 fDragBeginMarker
= true;
2583 fDragTime0
= selectionEnd
;
2586 fDragTime0
= selectionBegin
;
2589 long time
= getTimeAtX(e
.x
);
2590 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2591 fDragBeginMarker
= true;
2594 fDragTime0
= selectionEnd
;
2595 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2597 fDragTime0
= selectionBegin
;
2600 fTime0bak
= fTimeProvider
.getTime0();
2601 fTime1bak
= fTimeProvider
.getTime1();
2603 updateCursor(e
.x
, e
.stateMask
);
2604 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2606 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2607 long hitTime
= getTimeAtX(e
.x
);
2610 fDragState
= DRAG_TRACE_ITEM
;
2611 fDragButton
= e
.button
;
2614 fTime0bak
= fTimeProvider
.getTime0();
2615 fTime1bak
= fTimeProvider
.getTime1();
2616 updateCursor(e
.x
, e
.stateMask
);
2618 } else if (3 == e
.button
) {
2619 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2621 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2623 fDragTime0
= getTimeAtX(fDragX0
);
2624 fDragState
= DRAG_ZOOM
;
2625 fDragButton
= e
.button
;
2627 updateCursor(e
.x
, e
.stateMask
);
2628 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2634 public void mouseUp(MouseEvent e
) {
2635 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2636 menuDetected(fPendingMenuDetectEvent
);
2638 if (DRAG_NONE
!= fDragState
) {
2640 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2641 if (fDragX
!= fDragX0
) {
2642 fTimeProvider
.notifyStartFinishTime();
2644 fDragState
= DRAG_NONE
;
2645 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2646 fDragState
= DRAG_NONE
;
2648 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2649 if (fDragX
== fDragX0
) { // click without selecting anything
2650 long time
= getTimeAtX(e
.x
);
2651 fTimeProvider
.setSelectedTimeNotify(time
, false);
2653 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2654 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2655 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2657 fDragState
= DRAG_NONE
;
2659 fTimeGraphScale
.setDragRange(-1, -1);
2660 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2661 int nameWidth
= fTimeProvider
.getNameSpace();
2662 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2663 long time0
= getTimeAtX(fDragX0
);
2664 long time1
= getTimeAtX(fDragX
);
2665 if (time0
< time1
) {
2666 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2668 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2673 fDragState
= DRAG_NONE
;
2674 fTimeGraphScale
.setDragRange(-1, -1);
2677 updateCursor(e
.x
, e
.stateMask
);
2678 updateStatusLine(e
.x
);
2682 public void mouseEnter(MouseEvent e
) {
2686 public void mouseExit(MouseEvent e
) {
2687 if (fMouseOverSplitLine
) {
2688 fMouseOverSplitLine
= false;
2691 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2695 public void mouseHover(MouseEvent e
) {
2699 public void mouseScrolled(MouseEvent e
) {
2700 if (fDragState
!= DRAG_NONE
|| e
.count
== 0) {
2703 boolean horizontalZoom
= false;
2704 boolean horizontalScroll
= false;
2705 boolean verticalZoom
= false;
2706 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2707 Point parentSize
= getParent().getSize();
2708 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2709 // over the parent control
2710 if (e
.x
> getSize().x
) {
2711 // over the vertical scroll bar
2712 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2713 verticalZoom
= true;
2715 } else if (e
.y
< 0) {
2716 // over the time scale
2717 horizontalZoom
= true;
2718 } else if (e
.y
>= getSize().y
) {
2719 // over the marker axis
2720 horizontalZoom
= true;
2722 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2723 verticalZoom
= true;
2724 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2725 // over the name space
2726 horizontalZoom
= false;
2728 // over the state area
2729 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2730 // over the state area, CTRL pressed
2731 horizontalZoom
= true;
2733 // over the state area, CTRL not pressed
2734 horizontalZoom
= false;
2740 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2743 } else if (e
.count
< 0) {
2744 verticalZoom(false);
2746 if (fVerticalZoomAlignEntry
!= null) {
2747 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2749 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2752 } else if (e
.count
< 0) {
2755 } else if (horizontalScroll
) {
2756 horizontalScroll(e
.count
> 0);
2758 setTopIndex(getTopIndex() - e
.count
);
2763 * Get the vertical zoom alignment entry and position based on the current
2764 * selection. If there is no selection or if the selection is not visible,
2765 * return an alignment entry with a null time graph entry.
2767 * @return a map entry where the key is the selection's time graph entry and
2768 * the value is the center y-coordinate of that entry, or null
2770 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2771 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2772 if (alignEntry
!= null) {
2775 int index
= getSelectedIndex();
2776 if (index
== -1 || index
>= getExpandedElementCount()) {
2777 return new SimpleEntry
<>(null, 0);
2779 Rectangle bounds
= getClientArea();
2780 Rectangle itemRect
= getItemRect(bounds
, index
);
2781 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2782 /* selection is not visible */
2783 return new SimpleEntry
<>(null, 0);
2785 ITimeGraphEntry entry
= getExpandedElement(index
);
2786 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2787 return new SimpleEntry
<>(entry
, y
);
2791 * Get the vertical zoom alignment entry and position at the specified
2795 * the cursor y-coordinate
2796 * @return a map entry where the key is the time graph entry under the
2797 * cursor and the value is the cursor y-coordinate
2799 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2800 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2801 if (alignEntry
!= null) {
2804 int index
= getItemIndexAtY(y
);
2806 index
= getExpandedElementCount() - 1;
2808 ITimeGraphEntry entry
= getExpandedElement(index
);
2809 return new SimpleEntry
<>(entry
, y
);
2813 * Get the vertical zoom alignment entry and position if there is an ongoing
2814 * one and we are within the vertical zoom delay, or otherwise return null.
2816 * @return a map entry where the key is a time graph entry and the value is
2817 * a y-coordinate, or null
2819 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2820 long currentTimeMillis
= System
.currentTimeMillis();
2821 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2823 * If the vertical zoom is triggered repeatedly in a short amount of
2824 * time, use the initial event's entry and position.
2826 fVerticalZoomAlignTime
= currentTimeMillis
;
2827 return fVerticalZoomAlignEntry
;
2829 fVerticalZoomAlignTime
= currentTimeMillis
;
2834 public void handleEvent(Event event
) {
2835 if (event
.type
== SWT
.MouseWheel
) {
2836 // prevent horizontal scrolling when the mouse wheel is used to
2837 // scroll vertically or zoom
2843 public int getBorderWidth() {
2844 return fBorderWidth
;
2848 * Set the border width
2850 * @param borderWidth
2853 public void setBorderWidth(int borderWidth
) {
2854 this.fBorderWidth
= borderWidth
;
2858 * @return The current height of the header row
2860 public int getHeaderHeight() {
2861 return fHeaderHeight
;
2865 * Set the height of the header row
2867 * @param headerHeight
2870 public void setHeaderHeight(int headerHeight
) {
2871 this.fHeaderHeight
= headerHeight
;
2875 * @return The default height of regular item rows
2877 public int getItemHeight() {
2878 return fGlobalItemHeight
;
2882 * Set the default height of regular item rows.
2887 public void setItemHeight(int rowHeight
) {
2888 this.fGlobalItemHeight
= rowHeight
;
2889 for (Item item
: fItemData
.fItems
) {
2890 item
.fItemHeight
= rowHeight
;
2895 * Set the height of a specific item. Overrides the default item height.
2898 * A time graph entry
2901 * @return true if the height is successfully stored, false otherwise
2903 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2904 Item item
= fItemData
.findItem(entry
);
2906 item
.fItemHeight
= rowHeight
;
2913 * Set the minimum item width
2915 * @param width The minimum width
2917 public void setMinimumItemWidth(int width
) {
2918 this.fMinimumItemWidth
= width
;
2922 * @return The minimum item width
2924 public int getMinimumItemWidth() {
2925 return fMinimumItemWidth
;
2929 * Set whether all time events with a duration shorter than one pixel should
2930 * be blended in. If false, only the first such time event will be drawn and
2931 * the subsequent time events in the same pixel will be discarded. The
2932 * default value is false.
2935 * true if sub-pixel events should be blended, false otherwise.
2938 public void setBlendSubPixelEvents(boolean blend
) {
2939 fBlendSubPixelEvents
= blend
;
2943 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2944 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2945 fSelectionChangedListeners
.add(listener
);
2950 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2951 if (listener
!= null) {
2952 fSelectionChangedListeners
.remove(listener
);
2957 public void setSelection(ISelection selection
) {
2958 if (selection
instanceof TimeGraphSelection
) {
2959 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2960 Object ob
= sel
.getFirstElement();
2961 if (ob
instanceof ITimeGraphEntry
) {
2962 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2963 selectItem(trace
, false);
2970 * @param filter The filter object to be attached to the view
2972 public void addFilter(@NonNull ViewerFilter filter
) {
2973 if (!fFilters
.contains(filter
)) {
2974 fFilters
.add(filter
);
2979 * @param filter The filter object to be attached to the view
2981 public void removeFilter(@NonNull ViewerFilter filter
) {
2982 fFilters
.remove(filter
);
2986 * Returns this control's filters.
2988 * @return an array of viewer filters
2991 public @NonNull ViewerFilter
[] getFilters() {
2992 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
2996 * Sets the filters, replacing any previous filters.
2999 * an array of viewer filters, or null
3002 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3004 if (filters
!= null) {
3005 fFilters
.addAll(Arrays
.asList(filters
));
3010 public void colorSettingsChanged(StateItem
[] stateItems
) {
3011 /* Destroy previous colors from the resource manager */
3012 if (fEventColorMap
!= null) {
3013 for (Color color
: fEventColorMap
) {
3014 fResourceManager
.destroyColor(color
.getRGB());
3017 if (stateItems
!= null) {
3018 fEventColorMap
= new Color
[stateItems
.length
];
3019 for (int i
= 0; i
< stateItems
.length
; i
++) {
3020 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3023 fEventColorMap
= new Color
[] { };
3028 private class ItemData
{
3029 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3030 private Item
[] fExpandedItems
= new Item
[0];
3031 private Item
[] fItems
= new Item
[0];
3032 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3033 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3038 public Item
findItem(ITimeGraphEntry entry
) {
3039 return fItemMap
.get(entry
);
3042 public int findItemIndex(ITimeGraphEntry entry
) {
3043 Item item
= fItemMap
.get(entry
);
3047 return item
.fExpandedIndex
;
3050 public void refreshData() {
3051 ITimeGraphEntry selection
= getSelectedTrace();
3052 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3053 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3054 ITimeGraphEntry entry
= fRootEntries
[i
];
3055 refreshData(itemMap
, null, 0, entry
);
3058 fItems
= fItemMap
.values().toArray(new Item
[0]);
3059 updateExpandedItems();
3060 if (selection
!= null) {
3061 for (Item item
: fExpandedItems
) {
3062 if (item
.fEntry
== selection
) {
3063 item
.fSelected
= true;
3070 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3071 Item item
= new Item(entry
, entry
.getName(), level
);
3072 if (parent
!= null) {
3073 parent
.fChildren
.add(item
);
3075 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3076 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3078 item
.fItemHeight
= fGlobalItemHeight
;
3080 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3081 itemMap
.put(entry
, item
);
3082 if (entry
.hasChildren()) {
3083 Item oldItem
= fItemMap
.get(entry
);
3084 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3085 /* existing items keep their old expanded state */
3086 item
.fExpanded
= oldItem
.fExpanded
;
3088 /* new items set the expanded state according to auto-expand level */
3089 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3091 item
.fHasChildren
= true;
3092 for (ITimeGraphEntry child
: entry
.getChildren()) {
3093 refreshData(itemMap
, item
, level
+ 1, child
);
3098 public void updateExpandedItems() {
3099 for (Item item
: fItems
) {
3100 item
.fExpandedIndex
= -1;
3102 List
<Item
> expandedItemList
= new ArrayList
<>();
3103 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3104 ITimeGraphEntry entry
= fRootEntries
[i
];
3105 Item item
= findItem(entry
);
3106 refreshExpanded(expandedItemList
, item
);
3108 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3109 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3112 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3113 // Check for filters
3114 boolean display
= true;
3115 for (ViewerFilter filter
: fFilters
) {
3116 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3122 item
.fExpandedIndex
= expandedItemList
.size();
3123 expandedItemList
.add(item
);
3124 if (item
.fHasChildren
&& item
.fExpanded
) {
3125 for (Item child
: item
.fChildren
) {
3126 refreshExpanded(expandedItemList
, child
);
3132 public void refreshData(ITimeGraphEntry
[] entries
) {
3133 if (entries
== null) {
3134 fRootEntries
= null;
3136 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3142 public void refreshArrows(List
<ILinkEvent
> events
) {
3143 /* If links are null, reset the list */
3144 if (events
!= null) {
3147 fLinks
= new ArrayList
<>();
3151 public ITimeGraphEntry
[] getEntries() {
3152 return fRootEntries
;
3156 private class Item
{
3157 private boolean fExpanded
;
3158 private int fExpandedIndex
;
3159 private boolean fSelected
;
3160 private boolean fHasChildren
;
3161 private int fItemHeight
;
3162 private final int fLevel
;
3163 private final List
<Item
> fChildren
;
3164 private final String fName
;
3165 private final ITimeGraphEntry fEntry
;
3167 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3168 this.fEntry
= entry
;
3170 this.fLevel
= level
;
3171 this.fChildren
= new ArrayList
<>();
3175 public String
toString() {
3181 public void menuDetected(MenuDetectEvent e
) {
3182 if (null == fTimeProvider
) {
3185 if (e
.detail
== SWT
.MENU_MOUSE
) {
3186 if (fPendingMenuDetectEvent
== null) {
3187 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3188 * Store the event and trigger it later just before handling mouseUp.
3189 * This allows for the method to detect if mouse is used to drag zoom.
3191 fPendingMenuDetectEvent
= e
;
3194 fPendingMenuDetectEvent
= null;
3195 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
3199 if (fDragState
!= DRAG_NONE
) {
3203 Point p
= toControl(e
.x
, e
.y
);
3204 int idx
= getItemIndexAtY(p
.y
);
3205 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3206 Item item
= fItemData
.fExpandedItems
[idx
];
3207 ITimeGraphEntry entry
= item
.fEntry
;
3208 if (entry
.hasTimeEvents()) {
3209 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3210 if (event
!= null) {
3212 fireMenuEventOnTimeEvent(e
);
3217 fireMenuEventOnTimeGraphEntry(e
);
3222 * Perform the alignment operation.
3225 * the alignment offset
3227 * @see ITmfTimeAligned
3231 public void performAlign(int offset
) {
3232 fTimeProvider
.setNameSpace(offset
);
3236 * Return the time alignment information
3238 * @return the time alignment information
3240 * @see ITmfTimeAligned
3244 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3245 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());