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
) {
1085 fTimeGraphScale
.setMarkers(markers
);
1089 * Get the markers list.
1091 * @return The markers list, or null
1094 public List
<IMarkerEvent
> getMarkers() {
1099 * Set the markers visibility. The default is true.
1102 * true to show the markers, false otherwise
1105 public void setMarkersVisible(boolean visible
) {
1106 fMarkersVisible
= visible
;
1107 fTimeGraphScale
.setMarkersVisible(visible
);
1111 * Get the markers visibility.
1113 * @return true if the markers are visible, false otherwise
1116 public boolean getMarkersVisible() {
1117 return fMarkersVisible
;
1123 * @param hideArrows true to hide arrows
1125 public void hideArrows(boolean hideArrows
) {
1126 fHideArrows
= hideArrows
;
1130 * Follow the arrow forward
1133 * true to extend selection range, false for single selection
1136 public void followArrowFwd(boolean extend
) {
1137 ITimeGraphEntry trace
= getSelectedTrace();
1138 if (trace
== null) {
1141 long selectedTime
= fTimeProvider
.getSelectionEnd();
1142 for (ILinkEvent link
: fItemData
.fLinks
) {
1143 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1144 selectItem(link
.getDestinationEntry(), false);
1145 if (link
.getDuration() != 0) {
1147 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1149 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1151 // Notify if visible time window has been adjusted
1152 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1154 fireSelectionChanged();
1155 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1159 selectNextEvent(extend
);
1163 * Follow the arrow backward
1166 * true to extend selection range, false for single selection
1169 public void followArrowBwd(boolean extend
) {
1170 ITimeGraphEntry trace
= getSelectedTrace();
1171 if (trace
== null) {
1174 long selectedTime
= fTimeProvider
.getSelectionEnd();
1175 for (ILinkEvent link
: fItemData
.fLinks
) {
1176 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1177 selectItem(link
.getEntry(), false);
1178 if (link
.getDuration() != 0) {
1180 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1182 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
1184 // Notify if visible time window has been adjusted
1185 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1187 fireSelectionChanged();
1188 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1192 selectPrevEvent(extend
);
1196 * Return the currently selected trace
1198 * @return The entry matching the trace
1200 public ITimeGraphEntry
getSelectedTrace() {
1201 ITimeGraphEntry trace
= null;
1202 int idx
= getSelectedIndex();
1204 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1210 * Retrieve the index of the currently selected item
1214 public int getSelectedIndex() {
1216 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1217 Item item
= fItemData
.fExpandedItems
[i
];
1218 if (item
.fSelected
) {
1226 boolean toggle(int idx
) {
1227 boolean toggled
= false;
1228 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1229 Item item
= fItemData
.fExpandedItems
[idx
];
1230 if (item
.fHasChildren
) {
1231 item
.fExpanded
= !item
.fExpanded
;
1232 fItemData
.updateExpandedItems();
1235 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1242 * Gets the index of the item at the given location.
1246 * @return the index of the item at the given location, of -1 if none.
1248 protected int getItemIndexAtY(int y
) {
1251 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1252 ySum
-= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1258 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1259 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1268 boolean isOverSplitLine(int x
) {
1269 if (x
< 0 || null == fTimeProvider
) {
1272 int nameWidth
= fTimeProvider
.getNameSpace();
1273 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1277 * Gets the {@link ITimeGraphEntry} at the given location.
1280 * a point in the widget
1281 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1284 protected ITimeGraphEntry
getEntry(Point pt
) {
1285 int idx
= getItemIndexAtY(pt
.y
);
1286 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1290 * Return the arrow event closest to the given point that is no further than
1291 * a maximum distance.
1294 * a point in the widget
1295 * @return The closest arrow event, or null if there is none close enough.
1297 protected ILinkEvent
getArrow(Point pt
) {
1301 ILinkEvent linkEvent
= null;
1302 double minDistance
= Double
.MAX_VALUE
;
1303 for (ILinkEvent event
: fItemData
.fLinks
) {
1304 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1308 int x2
= x1
+ rect
.width
;
1309 int y2
= y1
+ rect
.height
;
1310 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1311 if (minDistance
> d
) {
1317 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1324 public int getXForTime(long time
) {
1325 if (null == fTimeProvider
) {
1328 long time0
= fTimeProvider
.getTime0();
1329 long time1
= fTimeProvider
.getTime1();
1330 int width
= getSize().x
;
1331 int nameSpace
= fTimeProvider
.getNameSpace();
1332 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1333 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1338 public long getTimeAtX(int coord
) {
1339 if (null == fTimeProvider
) {
1343 Point size
= getSize();
1344 long time0
= fTimeProvider
.getTime0();
1345 long time1
= fTimeProvider
.getTime1();
1346 int nameWidth
= fTimeProvider
.getNameSpace();
1347 final int x
= coord
- nameWidth
;
1348 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1349 if (x
>= 0 && size
.x
>= nameWidth
) {
1350 if (time1
- time0
> timeWidth
) {
1351 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1352 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1354 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1355 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1361 void selectItem(int idx
, boolean addSelection
) {
1362 boolean changed
= false;
1364 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1365 Item item
= fItemData
.fExpandedItems
[idx
];
1366 changed
= !item
.fSelected
;
1367 item
.fSelected
= true;
1370 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1371 Item item
= fItemData
.fExpandedItems
[i
];
1372 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1375 item
.fSelected
= i
== idx
;
1378 changed
|= ensureVisibleItem(idx
, true);
1385 * Callback for item selection
1388 * The entry matching the trace
1389 * @param addSelection
1390 * If the selection is added or removed
1392 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1393 int idx
= fItemData
.findItemIndex(trace
);
1394 selectItem(idx
, addSelection
);
1398 * Retrieve the number of entries shown per page.
1402 public int countPerPage() {
1403 int height
= getSize().y
;
1406 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1407 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1408 if (ySum
>= height
) {
1413 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1414 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1415 if (ySum
>= height
) {
1424 * Get the index of the top element
1428 public int getTopIndex() {
1433 * Get the number of expanded (visible) items
1435 * @return The count of expanded (visible) items
1437 public int getExpandedElementCount() {
1438 return fItemData
.fExpandedItems
.length
;
1442 * Get an array of all expanded (visible) elements
1444 * @return The expanded (visible) elements
1446 public ITimeGraphEntry
[] getExpandedElements() {
1447 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1448 for (Item item
: fItemData
.fExpandedItems
) {
1449 elements
.add(item
.fEntry
);
1451 return elements
.toArray(new ITimeGraphEntry
[0]);
1455 * Get the expanded (visible) element at the specified index.
1459 * @return The expanded (visible) element or null if out of range
1462 public ITimeGraphEntry
getExpandedElement(int index
) {
1463 if (index
< 0 || index
>= fItemData
.fExpandedItems
.length
) {
1466 return fItemData
.fExpandedItems
[index
].fEntry
;
1469 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1470 Rectangle rect
= getItemRect(bounds
, idx
);
1471 rect
.width
= nameWidth
;
1475 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1476 Rectangle rect
= getItemRect(bounds
, idx
);
1477 rect
.x
+= nameWidth
;
1478 rect
.width
-= nameWidth
;
1482 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1484 if (idx
>= fTopIndex
) {
1485 for (int i
= fTopIndex
; i
< idx
; i
++) {
1486 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1489 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1490 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1493 int y
= bounds
.y
+ ySum
;
1494 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1495 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1499 void paint(Rectangle bounds
, PaintEvent e
) {
1502 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1506 fIdealNameSpace
= 0;
1507 int nameSpace
= fTimeProvider
.getNameSpace();
1509 // draw the background layer
1510 drawBackground(bounds
, nameSpace
, gc
);
1512 // draw the grid lines
1513 drawGridLines(bounds
, gc
);
1515 // draw the background markers
1516 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1519 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1521 // draw the foreground markers
1522 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1524 // draw the links (arrows)
1525 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1527 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1529 int alpha
= gc
.getAlpha();
1532 long time0
= fTimeProvider
.getTime0();
1533 long time1
= fTimeProvider
.getTime1();
1534 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1535 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1536 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1537 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1538 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1540 // draw selection lines
1541 if (fDragState
!= DRAG_SELECTION
) {
1542 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1543 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1544 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1547 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1548 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1553 // draw selection background
1554 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1555 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1556 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1557 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1559 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1560 } else if (x0
- x1
> 1) {
1561 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1565 // draw drag selection background
1566 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1567 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1568 if (fDragX0
< fDragX
) {
1569 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1570 } else if (fDragX0
> fDragX
) {
1571 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1576 if (DRAG_SPLIT_LINE
== fDragState
) {
1577 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1578 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1579 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1580 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1581 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1582 if (fDragX
!= fDragX0
) {
1583 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1585 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1586 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1587 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1588 if (fDragX
!= fDragX0
) {
1589 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1591 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1592 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1593 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1600 * Draw the background layer. Fills the background of the control's name
1601 * space and states space, updates the background of items if necessary,
1602 * and draws the item's name text and middle line.
1605 * The bounds of the control
1607 * The name space width
1612 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1613 // draw empty name space background
1614 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1615 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1617 // draw empty states space background
1618 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1619 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1621 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1622 Rectangle itemRect
= getItemRect(bounds
, i
);
1623 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1626 Item item
= fItemData
.fExpandedItems
[i
];
1627 // draw the background of selected item and items with no time events
1628 if (! item
.fEntry
.hasTimeEvents()) {
1629 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1630 gc
.fillRectangle(itemRect
);
1631 } else if (item
.fSelected
) {
1632 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1633 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1634 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1635 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1637 // draw the name and middle line
1638 if (! item
.fEntry
.hasTimeEvents()) {
1639 drawName(item
, itemRect
, gc
);
1641 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1642 drawName(item
, nameRect
, gc
);
1643 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1644 drawMidLine(rect
, gc
);
1650 * Draw the grid lines
1653 * The bounds of the control
1658 public void drawGridLines(Rectangle bounds
, GC gc
) {
1659 if (!fGridLinesVisible
) {
1662 gc
.setForeground(fGridLineColor
);
1663 gc
.setAlpha(fGridLineColor
.getAlpha());
1664 for (int x
: fTimeGraphScale
.getTickList()) {
1665 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1674 * The rectangle of the area
1675 * @param timeProvider
1678 * The list of markers
1680 * true to draw the foreground markers, false otherwise
1682 * The width reserved for the names
1684 * Reference to the SWT GC object
1687 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1688 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1691 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1692 /* the list can grow concurrently but cannot shrink */
1693 for (int i
= 0; i
< markers
.size(); i
++) {
1694 IMarkerEvent marker
= markers
.get(i
);
1695 if (marker
.isForeground() == foreground
) {
1696 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1699 gc
.setClipping((Rectangle
) null);
1703 * Draw a single marker
1708 * The bounds of the control
1709 * @param timeProvider
1712 * The width reserved for the name
1714 * Reference to the SWT GC object
1717 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1718 Rectangle rect
= Utils
.clone(bounds
);
1719 if (marker
.getEntry() != null) {
1720 int index
= fItemData
.findItemIndex(marker
.getEntry());
1724 rect
= getStatesRect(bounds
, index
, nameSpace
);
1725 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1729 int x0
= getXForTime(marker
.getTime());
1730 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1731 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1734 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1735 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1737 Color color
= getColorScheme().getColor(marker
.getColor());
1738 gc
.setBackground(color
);
1739 gc
.setAlpha(color
.getAlpha());
1740 gc
.fillRectangle(rect
);
1742 String label
= marker
.getLabel();
1743 if (label
!= null && marker
.getEntry() != null) {
1744 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1745 gc
.setForeground(color
);
1746 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1751 * Draw many items at once
1754 * The bounds of the control
1755 * @param timeProvider
1758 * The array items to draw
1760 * The index of the first element to draw
1762 * The name space width
1766 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1767 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1768 for (int i
= topIndex
; i
< items
.length
; i
++) {
1769 Item item
= items
[i
];
1770 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1780 * The bounds of the control
1781 * @param timeProvider
1784 * The expanded item index
1786 * The name space width
1790 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1791 Rectangle itemRect
= getItemRect(bounds
, i
);
1792 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1796 ITimeGraphEntry entry
= item
.fEntry
;
1797 long time0
= timeProvider
.getTime0();
1798 long time1
= timeProvider
.getTime1();
1799 long selectedTime
= fTimeProvider
.getSelectionEnd();
1801 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1802 if (rect
.isEmpty() || (time1
<= time0
)) {
1803 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1807 boolean selected
= item
.fSelected
;
1808 // K pixels per second
1809 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1811 if (item
.fEntry
.hasTimeEvents()) {
1812 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1813 fillSpace(rect
, gc
, selected
);
1815 int margins
= getMarginForHeight(rect
.height
);
1816 int height
= rect
.height
- margins
;
1817 int topMargin
= (margins
+ 1) / 2;
1818 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1820 /* Set the font for this item */
1821 setFontForHeight(height
, gc
);
1823 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1824 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1827 while (iterator
.hasNext()) {
1828 ITimeEvent event
= iterator
.next();
1829 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1830 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1831 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1832 // event is out of bounds
1835 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1836 stateRect
.x
= Math
.max(rect
.x
, x
);
1837 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1838 if (stateRect
.x
== lastX
) {
1839 stateRect
.width
-= 1;
1840 if (stateRect
.width
> 0) {
1841 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1842 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1846 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1847 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1848 lastX
= stateRect
.x
;
1851 gc
.setClipping((Rectangle
) null);
1853 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1860 * The bounds of the control
1861 * @param timeProvider
1864 * The list of link events
1866 * The name space width
1870 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1871 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1875 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1876 /* the list can grow concurrently but cannot shrink */
1877 for (int i
= 0; i
< links
.size(); i
++) {
1878 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1880 gc
.setClipping((Rectangle
) null);
1884 * Draws a link type event
1887 * The link event to draw
1889 * The bounds of the control
1890 * @param timeProvider
1893 * The name space width
1897 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1898 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1901 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1902 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1903 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1905 if ((srcIndex
== -1) || (destIndex
== -1)) {
1909 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1910 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1912 int x0
= getXForTime(event
.getTime());
1913 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1915 // limit the x-coordinates to prevent integer overflow in calculations
1916 // and also GC.drawLine doesn't draw properly with large coordinates
1917 final int limit
= Integer
.MAX_VALUE
/ 1024;
1918 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1919 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1921 int y0
= src
.y
+ src
.height
/ 2;
1922 int y1
= dst
.y
+ dst
.height
/ 2;
1923 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1932 * Time event for which we're drawing the arrow
1934 * The arrow rectangle
1937 * @return true if the arrow was drawn
1939 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1940 Rectangle rect
, GC gc
) {
1945 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1949 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1952 Color stateColor
= null;
1953 if (colorIdx
< fEventColorMap
.length
) {
1954 stateColor
= fEventColorMap
[colorIdx
];
1956 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1959 gc
.setForeground(stateColor
);
1960 gc
.setBackground(stateColor
);
1962 /* Draw the arrow */
1963 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1964 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1967 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1972 * @author Francis Giraldeau
1975 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1977 * The algorithm was taken from this site, not the code itself
1979 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1982 double cos
= 0.9510;
1983 double sin
= 0.3090;
1984 long lenx
= x1
- x0
;
1985 long leny
= y1
- y0
;
1986 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1988 double dx
= factor
* lenx
/ len
;
1989 double dy
= factor
* leny
/ len
;
1990 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1991 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1992 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1993 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1994 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1995 gc
.fillPolygon(arrow
);
1999 * Draw the name of an item.
2004 * The bounds of the item's name space
2008 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
2009 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
2011 // No name to be drawn
2012 if (fTimeProvider
.getNameSpace() == 0) {
2016 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
2017 setFontForHeight(height
, gc
);
2019 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
2020 if (item
.fHasChildren
) {
2021 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
2022 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
2023 Rectangle rect
= Utils
.clone(bounds
);
2024 rect
.x
+= leftMargin
;
2025 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
2026 rect
.width
= EXPAND_SIZE
;
2027 rect
.height
= EXPAND_SIZE
;
2028 gc
.fillRectangle(rect
);
2029 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
2030 int midy
= rect
.y
+ rect
.height
/ 2;
2031 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
2032 if (!item
.fExpanded
) {
2033 int midx
= rect
.x
+ rect
.width
/ 2;
2034 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
2037 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
2039 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
2042 int imgHeight
= img
.getImageData().height
;
2043 int imgWidth
= img
.getImageData().width
;
2045 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
2046 gc
.drawImage(img
, x
, y
);
2047 leftMargin
+= imgWidth
+ MARGIN
;
2049 String name
= item
.fName
;
2050 Point size
= gc
.stringExtent(name
);
2051 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
2052 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
2054 if (hasTimeEvents
) {
2055 // cut long string with "..."
2056 int width
= bounds
.width
- leftMargin
;
2058 while (size
.x
> width
&& name
.length() > 1) {
2060 name
= name
.substring(0, name
.length() - 1);
2061 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2064 name
+= "..."; //$NON-NLS-1$
2067 Rectangle rect
= Utils
.clone(bounds
);
2068 rect
.x
+= leftMargin
;
2069 rect
.width
-= leftMargin
;
2071 if (rect
.width
> 0) {
2072 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2073 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2074 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2075 leftMargin
+= textWidth
+ MARGIN
;
2077 if (hasTimeEvents
) {
2079 rect
.x
= bounds
.x
+ leftMargin
;
2081 rect
.width
= bounds
.width
- rect
.x
;
2082 drawMidLine(rect
, gc
);
2088 * Draw the state (color fill)
2093 * Time event for which we're drawing the state
2095 * The state rectangle
2099 * Is this time event currently selected (so it appears
2101 * @param timeSelected
2102 * Is the timestamp currently selected
2103 * @return true if the state was drawn
2105 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2106 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2108 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2109 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2112 boolean visible
= rect
.width
== 0 ?
false : true;
2113 rect
.width
= Math
.max(1, rect
.width
);
2114 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2115 gc
.setForeground(black
);
2117 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2119 // Only draw the top and bottom borders
2120 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2121 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2122 if (rect
.width
== 1) {
2123 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2126 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2129 Color stateColor
= null;
2130 if (colorIdx
< fEventColorMap
.length
) {
2131 stateColor
= fEventColorMap
[colorIdx
];
2136 boolean reallySelected
= timeSelected
&& selected
;
2137 // fill all rect area
2138 gc
.setBackground(stateColor
);
2140 gc
.fillRectangle(rect
);
2141 } else if (fBlendSubPixelEvents
) {
2143 gc
.fillRectangle(rect
);
2147 if (reallySelected
) {
2148 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2149 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2152 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2154 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2159 * Fill an item's states rectangle
2162 * The states rectangle
2166 * true if the item is selected
2168 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2169 /* Nothing to draw */
2173 * Draw a line at the middle height of a rectangle
2180 private void drawMidLine(Rectangle rect
, GC gc
) {
2181 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2182 int midy
= rect
.y
+ rect
.height
/ 2;
2183 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2186 private static int getMarginForHeight(int height
) {
2188 * State rectangle is smaller than the item bounds when height is > 4.
2189 * Don't use any margin if the height is below or equal that threshold.
2190 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2191 * pixels for the state height, but with a minimum margin of 1.
2193 final int MARGIN_THRESHOLD
= 4;
2194 final int PREFERRED_HEIGHT
= 13;
2195 final int MIN_MARGIN
= 1;
2196 final int MAX_MARGIN
= 6;
2197 return height
<= MARGIN_THRESHOLD ?
0 :
2198 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2201 private void setFontForHeight(int pixels
, GC gc
) {
2202 /* convert font height from pixels to points */
2203 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2204 Font font
= fFonts
.get(height
);
2206 FontData fontData
= gc
.getFont().getFontData()[0];
2207 fontData
.setHeight(height
);
2208 font
= new Font(gc
.getDevice(), fontData
);
2209 fFonts
.put(height
, font
);
2215 public void keyTraversed(TraverseEvent e
) {
2216 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2222 public void keyPressed(KeyEvent e
) {
2224 if (fItemData
.fExpandedItems
.length
== 0) {
2227 if (SWT
.HOME
== e
.keyCode
) {
2229 } else if (SWT
.END
== e
.keyCode
) {
2230 idx
= fItemData
.fExpandedItems
.length
- 1;
2231 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2232 idx
= getSelectedIndex();
2235 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2238 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2239 idx
= getSelectedIndex();
2242 } else if (idx
> 0) {
2245 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2246 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2247 selectPrevEvent(extend
);
2248 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2249 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2250 selectNextEvent(extend
);
2251 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2252 int page
= countPerPage();
2253 idx
= getSelectedIndex();
2258 if (idx
>= fItemData
.fExpandedItems
.length
) {
2259 idx
= fItemData
.fExpandedItems
.length
- 1;
2261 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2262 int page
= countPerPage();
2263 idx
= getSelectedIndex();
2271 } else if (SWT
.CR
== e
.keyCode
) {
2272 idx
= getSelectedIndex();
2274 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2277 fireDefaultSelection();
2281 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2282 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2284 if (fVerticalZoomAlignEntry
!= null) {
2285 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2287 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2288 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2289 verticalZoom(false);
2290 if (fVerticalZoomAlignEntry
!= null) {
2291 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2293 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2294 fVerticalZoomAlignEntry
= getVerticalZoomAlignSelection();
2295 resetVerticalZoom();
2296 if (fVerticalZoomAlignEntry
!= null) {
2297 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2301 selectItem(idx
, false);
2302 fireSelectionChanged();
2304 int x
= toControl(e
.display
.getCursorLocation()).x
;
2305 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2309 public void keyReleased(KeyEvent e
) {
2310 int x
= toControl(e
.display
.getCursorLocation()).x
;
2311 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2315 public void focusGained(FocusEvent e
) {
2318 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2322 public void focusLost(FocusEvent e
) {
2324 if (DRAG_NONE
!= fDragState
) {
2326 fDragState
= DRAG_NONE
;
2329 updateStatusLine(NO_STATUS
);
2333 * @return If the current view is focused
2335 public boolean isInFocus() {
2340 * Provide the possibility to control the wait cursor externally e.g. data
2341 * requests in progress
2343 * @param waitInd Should we wait indefinitely?
2345 public void waitCursor(boolean waitInd
) {
2346 // Update cursor as indicated
2348 setCursor(fWaitCursor
);
2354 private void updateCursor(int x
, int stateMask
) {
2355 // if Wait cursor not active, check for the need to change the cursor
2356 if (getCursor() == fWaitCursor
) {
2359 Cursor cursor
= null;
2360 if (fDragState
== DRAG_SPLIT_LINE
) {
2361 } else if (fDragState
== DRAG_SELECTION
) {
2362 cursor
= fResizeCursor
;
2363 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2364 cursor
= fDragCursor
;
2365 } else if (fDragState
== DRAG_ZOOM
) {
2366 cursor
= fZoomCursor
;
2367 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2368 cursor
= fDragCursor
;
2369 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2370 cursor
= fResizeCursor
;
2371 } else if (!isOverSplitLine(x
)) {
2372 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2373 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2374 int xBegin
= getXForTime(selectionBegin
);
2375 int xEnd
= getXForTime(selectionEnd
);
2376 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2377 cursor
= fResizeCursor
;
2380 if (getCursor() != cursor
) {
2386 * Update the status line following a change of selection.
2390 public void updateStatusLine() {
2391 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2394 private void updateStatusLine(int x
) {
2395 // use the time provider of the time graph scale for the status line
2396 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2397 if (fStatusLineManager
== null || null == tdp
||
2398 tdp
.getTime0() == tdp
.getTime1()) {
2401 TimeFormat tf
= tdp
.getTimeFormat();
2402 Resolution res
= Resolution
.NANOSEC
;
2403 StringBuilder message
= new StringBuilder();
2404 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2405 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2406 long time
= getTimeAtX(x
);
2408 if (tdp
instanceof ITimeDataProviderConverter
) {
2409 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2411 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2413 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2414 Utils
.formatTime(time
, tf
, res
)
2418 long selectionBegin
= tdp
.getSelectionBegin();
2419 long selectionEnd
= tdp
.getSelectionEnd();
2420 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2422 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2423 Utils
.formatTime(selectionBegin
, tf
, res
)
2425 if (selectionBegin
!= selectionEnd
) {
2426 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2428 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2429 Utils
.formatTime(selectionEnd
, tf
, res
),
2430 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2433 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2434 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2435 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2436 if (tdp
instanceof ITimeDataProviderConverter
) {
2437 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2438 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2440 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2442 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2443 Utils
.formatTime(time0
, tf
, res
),
2444 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2445 Utils
.formatTime(time
, tf
, res
),
2446 Utils
.formatDelta(time
- time0
, tf
, res
)
2449 fStatusLineManager
.setMessage(message
.toString());
2453 public void mouseMove(MouseEvent e
) {
2454 if (null == fTimeProvider
) {
2457 Point size
= getSize();
2458 if (DRAG_TRACE_ITEM
== fDragState
) {
2459 int nameWidth
= fTimeProvider
.getNameSpace();
2460 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2462 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2463 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2464 long time1
= fTime1bak
- timeDelta
;
2465 long maxTime
= fTimeProvider
.getMaxTime();
2466 if (time1
> maxTime
) {
2469 long time0
= time1
- (fTime1bak
- fTime0bak
);
2470 if (time0
< fTimeProvider
.getMinTime()) {
2471 time0
= fTimeProvider
.getMinTime();
2472 time1
= time0
+ (fTime1bak
- fTime0bak
);
2474 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2476 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2478 fTimeProvider
.setNameSpace(e
.x
);
2479 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2480 } else if (DRAG_SELECTION
== fDragState
) {
2481 if (fDragBeginMarker
) {
2482 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2484 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2487 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2488 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2489 } else if (DRAG_ZOOM
== fDragState
) {
2490 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2492 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2493 } else if (DRAG_NONE
== fDragState
) {
2494 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2495 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2498 fMouseOverSplitLine
= mouseOverSplitLine
;
2500 updateCursor(e
.x
, e
.stateMask
);
2501 updateStatusLine(e
.x
);
2505 public void mouseDoubleClick(MouseEvent e
) {
2506 if (null == fTimeProvider
) {
2509 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2510 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2511 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2512 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2513 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2516 fMouseOverSplitLine
= mouseOverSplitLine
;
2517 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2520 int idx
= getItemIndexAtY(e
.y
);
2522 selectItem(idx
, false);
2523 fireDefaultSelection();
2529 public void mouseDown(MouseEvent e
) {
2530 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2531 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2532 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2536 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2537 int nameSpace
= fTimeProvider
.getNameSpace();
2538 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2539 fDragState
= DRAG_SPLIT_LINE
;
2540 fDragButton
= e
.button
;
2543 fTime0bak
= fTimeProvider
.getTime0();
2544 fTime1bak
= fTimeProvider
.getTime1();
2546 updateCursor(e
.x
, e
.stateMask
);
2550 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2551 int nameSpace
= fTimeProvider
.getNameSpace();
2552 idx
= getItemIndexAtY(e
.y
);
2554 Item item
= fItemData
.fExpandedItems
[idx
];
2555 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2559 selectItem(idx
, false);
2560 fireSelectionChanged();
2562 selectItem(idx
, false); // clear selection
2563 fireSelectionChanged();
2565 long hitTime
= getTimeAtX(e
.x
);
2569 fDragState
= DRAG_SELECTION
;
2570 fDragBeginMarker
= false;
2571 fDragButton
= e
.button
;
2574 fDragTime0
= getTimeAtX(fDragX0
);
2575 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2576 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2577 int xBegin
= getXForTime(selectionBegin
);
2578 int xEnd
= getXForTime(selectionEnd
);
2579 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2580 long time
= getTimeAtX(e
.x
);
2581 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2582 fDragBeginMarker
= true;
2585 fDragTime0
= selectionEnd
;
2588 fDragTime0
= selectionBegin
;
2591 long time
= getTimeAtX(e
.x
);
2592 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2593 fDragBeginMarker
= true;
2596 fDragTime0
= selectionEnd
;
2597 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2599 fDragTime0
= selectionBegin
;
2602 fTime0bak
= fTimeProvider
.getTime0();
2603 fTime1bak
= fTimeProvider
.getTime1();
2605 updateCursor(e
.x
, e
.stateMask
);
2606 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2608 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2609 long hitTime
= getTimeAtX(e
.x
);
2612 fDragState
= DRAG_TRACE_ITEM
;
2613 fDragButton
= e
.button
;
2616 fTime0bak
= fTimeProvider
.getTime0();
2617 fTime1bak
= fTimeProvider
.getTime1();
2618 updateCursor(e
.x
, e
.stateMask
);
2620 } else if (3 == e
.button
) {
2621 if (e
.x
>= fTimeProvider
.getNameSpace()) {
2623 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2625 fDragTime0
= getTimeAtX(fDragX0
);
2626 fDragState
= DRAG_ZOOM
;
2627 fDragButton
= e
.button
;
2629 updateCursor(e
.x
, e
.stateMask
);
2630 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2636 public void mouseUp(MouseEvent e
) {
2637 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2638 menuDetected(fPendingMenuDetectEvent
);
2640 if (DRAG_NONE
!= fDragState
) {
2642 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2643 if (fDragX
!= fDragX0
) {
2644 fTimeProvider
.notifyStartFinishTime();
2646 fDragState
= DRAG_NONE
;
2647 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2648 fDragState
= DRAG_NONE
;
2650 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2651 if (fDragX
== fDragX0
) { // click without selecting anything
2652 long time
= getTimeAtX(e
.x
);
2653 fTimeProvider
.setSelectedTimeNotify(time
, false);
2655 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2656 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2657 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2659 fDragState
= DRAG_NONE
;
2661 fTimeGraphScale
.setDragRange(-1, -1);
2662 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2663 int nameWidth
= fTimeProvider
.getNameSpace();
2664 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2665 long time0
= getTimeAtX(fDragX0
);
2666 long time1
= getTimeAtX(fDragX
);
2667 if (time0
< time1
) {
2668 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2670 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2675 fDragState
= DRAG_NONE
;
2676 fTimeGraphScale
.setDragRange(-1, -1);
2679 updateCursor(e
.x
, e
.stateMask
);
2680 updateStatusLine(e
.x
);
2684 public void mouseEnter(MouseEvent e
) {
2688 public void mouseExit(MouseEvent e
) {
2689 if (fMouseOverSplitLine
) {
2690 fMouseOverSplitLine
= false;
2693 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2697 public void mouseHover(MouseEvent e
) {
2701 public void mouseScrolled(MouseEvent e
) {
2702 if (fDragState
!= DRAG_NONE
) {
2705 boolean horizontalZoom
= false;
2706 boolean horizontalScroll
= false;
2707 boolean verticalZoom
= false;
2708 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2709 Point parentSize
= getParent().getSize();
2710 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2711 // over the parent control
2712 if (e
.x
> getSize().x
) {
2713 // over the vertical scroll bar
2714 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2715 verticalZoom
= true;
2717 } else if (e
.y
< 0) {
2718 // over the time scale
2719 horizontalZoom
= true;
2720 } else if (e
.y
>= getSize().y
) {
2721 // over the horizontal scroll bar
2722 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2723 horizontalZoom
= true;
2725 horizontalScroll
= true;
2728 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2729 verticalZoom
= true;
2730 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2731 // over the name space
2732 horizontalZoom
= false;
2734 // over the state area
2735 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2736 // over the state area, CTRL pressed
2737 horizontalZoom
= true;
2739 // over the state area, CTRL not pressed
2740 horizontalZoom
= false;
2746 fVerticalZoomAlignEntry
= getVerticalZoomAlignCursor(e
.y
);
2749 } else if (e
.count
< 0) {
2750 verticalZoom(false);
2752 if (fVerticalZoomAlignEntry
!= null) {
2753 setElementPosition(fVerticalZoomAlignEntry
.getKey(), fVerticalZoomAlignEntry
.getValue());
2755 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2758 } else if (e
.count
< 0) {
2761 } else if (horizontalScroll
) {
2762 horizontalScroll(e
.count
> 0);
2764 setTopIndex(getTopIndex() - e
.count
);
2769 * Get the vertical zoom alignment entry and position based on the current
2770 * selection. If there is no selection or if the selection is not visible,
2771 * return an alignment entry with a null time graph entry.
2773 * @return a map entry where the key is the selection's time graph entry and
2774 * the value is the center y-coordinate of that entry, or null
2776 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignSelection() {
2777 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2778 if (alignEntry
!= null) {
2781 int index
= getSelectedIndex();
2782 if (index
== -1 || index
>= getExpandedElementCount()) {
2783 return new SimpleEntry
<>(null, 0);
2785 Rectangle bounds
= getClientArea();
2786 Rectangle itemRect
= getItemRect(bounds
, index
);
2787 if (itemRect
.y
< bounds
.y
|| itemRect
.y
> bounds
.y
+ bounds
.height
) {
2788 /* selection is not visible */
2789 return new SimpleEntry
<>(null, 0);
2791 ITimeGraphEntry entry
= getExpandedElement(index
);
2792 int y
= itemRect
.y
+ itemRect
.height
/ 2;
2793 return new SimpleEntry
<>(entry
, y
);
2797 * Get the vertical zoom alignment entry and position at the specified
2801 * the cursor y-coordinate
2802 * @return a map entry where the key is the time graph entry under the
2803 * cursor and the value is the cursor y-coordinate
2805 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignCursor(int y
) {
2806 Entry
<ITimeGraphEntry
, Integer
> alignEntry
= getVerticalZoomAlignOngoing();
2807 if (alignEntry
!= null) {
2810 int index
= getItemIndexAtY(y
);
2812 index
= getExpandedElementCount() - 1;
2814 ITimeGraphEntry entry
= getExpandedElement(index
);
2815 return new SimpleEntry
<>(entry
, y
);
2819 * Get the vertical zoom alignment entry and position if there is an ongoing
2820 * one and we are within the vertical zoom delay, or otherwise return null.
2822 * @return a map entry where the key is a time graph entry and the value is
2823 * a y-coordinate, or null
2825 private Entry
<ITimeGraphEntry
, Integer
> getVerticalZoomAlignOngoing() {
2826 long currentTimeMillis
= System
.currentTimeMillis();
2827 if (currentTimeMillis
< fVerticalZoomAlignTime
+ VERTICAL_ZOOM_DELAY
) {
2829 * If the vertical zoom is triggered repeatedly in a short amount of
2830 * time, use the initial event's entry and position.
2832 fVerticalZoomAlignTime
= currentTimeMillis
;
2833 return fVerticalZoomAlignEntry
;
2835 fVerticalZoomAlignTime
= currentTimeMillis
;
2840 public void handleEvent(Event event
) {
2841 if (event
.type
== SWT
.MouseWheel
) {
2842 // prevent horizontal scrolling when the mouse wheel is used to
2843 // scroll vertically or zoom
2849 public int getBorderWidth() {
2850 return fBorderWidth
;
2854 * Set the border width
2856 * @param borderWidth
2859 public void setBorderWidth(int borderWidth
) {
2860 this.fBorderWidth
= borderWidth
;
2864 * @return The current height of the header row
2866 public int getHeaderHeight() {
2867 return fHeaderHeight
;
2871 * Set the height of the header row
2873 * @param headerHeight
2876 public void setHeaderHeight(int headerHeight
) {
2877 this.fHeaderHeight
= headerHeight
;
2881 * @return The default height of regular item rows
2883 public int getItemHeight() {
2884 return fGlobalItemHeight
;
2888 * Set the default height of regular item rows.
2893 public void setItemHeight(int rowHeight
) {
2894 this.fGlobalItemHeight
= rowHeight
;
2895 for (Item item
: fItemData
.fItems
) {
2896 item
.fItemHeight
= rowHeight
;
2901 * Set the height of a specific item. Overrides the default item height.
2904 * A time graph entry
2907 * @return true if the height is successfully stored, false otherwise
2909 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2910 Item item
= fItemData
.findItem(entry
);
2912 item
.fItemHeight
= rowHeight
;
2919 * Set the minimum item width
2921 * @param width The minimum width
2923 public void setMinimumItemWidth(int width
) {
2924 this.fMinimumItemWidth
= width
;
2928 * @return The minimum item width
2930 public int getMinimumItemWidth() {
2931 return fMinimumItemWidth
;
2935 * Set whether all time events with a duration shorter than one pixel should
2936 * be blended in. If false, only the first such time event will be drawn and
2937 * the subsequent time events in the same pixel will be discarded. The
2938 * default value is false.
2941 * true if sub-pixel events should be blended, false otherwise.
2944 public void setBlendSubPixelEvents(boolean blend
) {
2945 fBlendSubPixelEvents
= blend
;
2949 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2950 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2951 fSelectionChangedListeners
.add(listener
);
2956 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2957 if (listener
!= null) {
2958 fSelectionChangedListeners
.remove(listener
);
2963 public void setSelection(ISelection selection
) {
2964 if (selection
instanceof TimeGraphSelection
) {
2965 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2966 Object ob
= sel
.getFirstElement();
2967 if (ob
instanceof ITimeGraphEntry
) {
2968 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2969 selectItem(trace
, false);
2976 * @param filter The filter object to be attached to the view
2978 public void addFilter(@NonNull ViewerFilter filter
) {
2979 if (!fFilters
.contains(filter
)) {
2980 fFilters
.add(filter
);
2985 * @param filter The filter object to be attached to the view
2987 public void removeFilter(@NonNull ViewerFilter filter
) {
2988 fFilters
.remove(filter
);
2992 * Returns this control's filters.
2994 * @return an array of viewer filters
2997 public @NonNull ViewerFilter
[] getFilters() {
2998 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
3002 * Sets the filters, replacing any previous filters.
3005 * an array of viewer filters, or null
3008 public void setFilters(@NonNull ViewerFilter
[] filters
) {
3010 if (filters
!= null) {
3011 fFilters
.addAll(Arrays
.asList(filters
));
3016 public void colorSettingsChanged(StateItem
[] stateItems
) {
3017 /* Destroy previous colors from the resource manager */
3018 if (fEventColorMap
!= null) {
3019 for (Color color
: fEventColorMap
) {
3020 fResourceManager
.destroyColor(color
.getRGB());
3023 if (stateItems
!= null) {
3024 fEventColorMap
= new Color
[stateItems
.length
];
3025 for (int i
= 0; i
< stateItems
.length
; i
++) {
3026 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
3029 fEventColorMap
= new Color
[] { };
3034 private class ItemData
{
3035 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
3036 private Item
[] fExpandedItems
= new Item
[0];
3037 private Item
[] fItems
= new Item
[0];
3038 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
3039 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
3044 public Item
findItem(ITimeGraphEntry entry
) {
3045 return fItemMap
.get(entry
);
3048 public int findItemIndex(ITimeGraphEntry entry
) {
3049 Item item
= fItemMap
.get(entry
);
3053 return item
.fExpandedIndex
;
3056 public void refreshData() {
3057 ITimeGraphEntry selection
= getSelectedTrace();
3058 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
3059 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3060 ITimeGraphEntry entry
= fRootEntries
[i
];
3061 refreshData(itemMap
, null, 0, entry
);
3064 fItems
= fItemMap
.values().toArray(new Item
[0]);
3065 updateExpandedItems();
3066 if (selection
!= null) {
3067 for (Item item
: fExpandedItems
) {
3068 if (item
.fEntry
== selection
) {
3069 item
.fSelected
= true;
3076 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
3077 Item item
= new Item(entry
, entry
.getName(), level
);
3078 if (parent
!= null) {
3079 parent
.fChildren
.add(item
);
3081 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
3082 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
3084 item
.fItemHeight
= fGlobalItemHeight
;
3086 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
3087 itemMap
.put(entry
, item
);
3088 if (entry
.hasChildren()) {
3089 Item oldItem
= fItemMap
.get(entry
);
3090 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
3091 /* existing items keep their old expanded state */
3092 item
.fExpanded
= oldItem
.fExpanded
;
3094 /* new items set the expanded state according to auto-expand level */
3095 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
3097 item
.fHasChildren
= true;
3098 for (ITimeGraphEntry child
: entry
.getChildren()) {
3099 refreshData(itemMap
, item
, level
+ 1, child
);
3104 public void updateExpandedItems() {
3105 for (Item item
: fItems
) {
3106 item
.fExpandedIndex
= -1;
3108 List
<Item
> expandedItemList
= new ArrayList
<>();
3109 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
3110 ITimeGraphEntry entry
= fRootEntries
[i
];
3111 Item item
= findItem(entry
);
3112 refreshExpanded(expandedItemList
, item
);
3114 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
3115 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
3118 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
3119 // Check for filters
3120 boolean display
= true;
3121 for (ViewerFilter filter
: fFilters
) {
3122 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
3128 item
.fExpandedIndex
= expandedItemList
.size();
3129 expandedItemList
.add(item
);
3130 if (item
.fHasChildren
&& item
.fExpanded
) {
3131 for (Item child
: item
.fChildren
) {
3132 refreshExpanded(expandedItemList
, child
);
3138 public void refreshData(ITimeGraphEntry
[] entries
) {
3139 if (entries
== null) {
3140 fRootEntries
= null;
3142 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3148 public void refreshArrows(List
<ILinkEvent
> events
) {
3149 /* If links are null, reset the list */
3150 if (events
!= null) {
3153 fLinks
= new ArrayList
<>();
3157 public ITimeGraphEntry
[] getEntries() {
3158 return fRootEntries
;
3162 private class Item
{
3163 private boolean fExpanded
;
3164 private int fExpandedIndex
;
3165 private boolean fSelected
;
3166 private boolean fHasChildren
;
3167 private int fItemHeight
;
3168 private final int fLevel
;
3169 private final List
<Item
> fChildren
;
3170 private final String fName
;
3171 private final ITimeGraphEntry fEntry
;
3173 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3174 this.fEntry
= entry
;
3176 this.fLevel
= level
;
3177 this.fChildren
= new ArrayList
<>();
3181 public String
toString() {
3187 public void menuDetected(MenuDetectEvent e
) {
3188 if (null == fTimeProvider
) {
3191 if (e
.detail
== SWT
.MENU_MOUSE
) {
3192 if (fPendingMenuDetectEvent
== null) {
3193 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3194 * Store the event and trigger it later just before handling mouseUp.
3195 * This allows for the method to detect if mouse is used to drag zoom.
3197 fPendingMenuDetectEvent
= e
;
3200 fPendingMenuDetectEvent
= null;
3201 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
3205 if (fDragState
!= DRAG_NONE
) {
3209 Point p
= toControl(e
.x
, e
.y
);
3210 int idx
= getItemIndexAtY(p
.y
);
3211 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3212 Item item
= fItemData
.fExpandedItems
[idx
];
3213 ITimeGraphEntry entry
= item
.fEntry
;
3214 if (entry
.hasTimeEvents()) {
3215 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3216 if (event
!= null) {
3218 fireMenuEventOnTimeEvent(e
);
3223 fireMenuEventOnTimeGraphEntry(e
);
3228 * Perform the alignment operation.
3231 * the alignment offset
3233 * @see ITmfTimeAligned
3237 public void performAlign(int offset
) {
3238 fTimeProvider
.setNameSpace(offset
);
3242 * Return the time alignment information
3244 * @return the time alignment information
3246 * @see ITmfTimeAligned
3250 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3251 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());