1 /*****************************************************************************
2 * Copyright (c) 2007, 2015 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 * Add display of links between items
17 * Xavier Raynaud, Kalray - Code optimization
18 * Generoso Pagano, Inria - Support for drag selection listeners
19 *****************************************************************************/
21 package org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Arrays
;
25 import java
.util
.Collections
;
26 import java
.util
.HashMap
;
27 import java
.util
.Iterator
;
28 import java
.util
.LinkedHashMap
;
29 import java
.util
.List
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jface
.action
.IStatusLineManager
;
34 import org
.eclipse
.jface
.resource
.JFaceResources
;
35 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
36 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
37 import org
.eclipse
.jface
.viewers
.ISelection
;
38 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
39 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
40 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
41 import org
.eclipse
.osgi
.util
.NLS
;
42 import org
.eclipse
.swt
.SWT
;
43 import org
.eclipse
.swt
.events
.FocusEvent
;
44 import org
.eclipse
.swt
.events
.FocusListener
;
45 import org
.eclipse
.swt
.events
.KeyEvent
;
46 import org
.eclipse
.swt
.events
.KeyListener
;
47 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
48 import org
.eclipse
.swt
.events
.MenuDetectListener
;
49 import org
.eclipse
.swt
.events
.MouseEvent
;
50 import org
.eclipse
.swt
.events
.MouseListener
;
51 import org
.eclipse
.swt
.events
.MouseMoveListener
;
52 import org
.eclipse
.swt
.events
.MouseTrackListener
;
53 import org
.eclipse
.swt
.events
.MouseWheelListener
;
54 import org
.eclipse
.swt
.events
.PaintEvent
;
55 import org
.eclipse
.swt
.events
.SelectionListener
;
56 import org
.eclipse
.swt
.events
.TraverseEvent
;
57 import org
.eclipse
.swt
.events
.TraverseListener
;
58 import org
.eclipse
.swt
.events
.TypedEvent
;
59 import org
.eclipse
.swt
.graphics
.Color
;
60 import org
.eclipse
.swt
.graphics
.Cursor
;
61 import org
.eclipse
.swt
.graphics
.Font
;
62 import org
.eclipse
.swt
.graphics
.FontData
;
63 import org
.eclipse
.swt
.graphics
.GC
;
64 import org
.eclipse
.swt
.graphics
.Image
;
65 import org
.eclipse
.swt
.graphics
.Point
;
66 import org
.eclipse
.swt
.graphics
.Rectangle
;
67 import org
.eclipse
.swt
.widgets
.Composite
;
68 import org
.eclipse
.swt
.widgets
.Display
;
69 import org
.eclipse
.swt
.widgets
.Event
;
70 import org
.eclipse
.swt
.widgets
.Listener
;
71 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
72 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentInfo
;
73 import org
.eclipse
.tracecompass
.tmf
.ui
.signal
.TmfTimeViewAlignmentSignal
;
74 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.ITmfTimeAligned
;
75 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphColorListener
;
76 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
77 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
78 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTimeListener
;
79 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
80 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.StateItem
;
81 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTimeEvent
;
82 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
83 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
84 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.IMarkerEvent
;
85 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
86 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
87 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
88 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
90 import com
.google
.common
.collect
.Iterables
;
93 * Time graph control implementation
95 * @author Alvaro Sanchez-Leon
96 * @author Patrick Tasse
98 public class TimeGraphControl
extends TimeGraphBaseControl
99 implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
,
100 MouseWheelListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
,
101 MenuDetectListener
, ITmfTimeGraphDrawingHelper
, ITimeGraphColorListener
, Listener
{
103 /** Constant indicating that all levels of the time graph should be expanded */
104 public static final int ALL_LEVELS
= AbstractTreeViewer
.ALL_LEVELS
;
106 private static final int DRAG_NONE
= 0;
107 private static final int DRAG_TRACE_ITEM
= 1;
108 private static final int DRAG_SPLIT_LINE
= 2;
109 private static final int DRAG_ZOOM
= 3;
110 private static final int DRAG_SELECTION
= 4;
112 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
114 private static final double ZOOM_FACTOR
= 1.5;
115 private static final double ZOOM_IN_FACTOR
= 0.8;
116 private static final double ZOOM_OUT_FACTOR
= 1.25;
118 private static final int SNAP_WIDTH
= 2;
119 private static final int ARROW_HOVER_MAX_DIST
= 5;
121 private static final int NO_STATUS
= -1;
122 private static final int STATUS_WITHOUT_CURSOR_TIME
= -2;
124 private static final int MAX_LABEL_LENGTH
= 256;
126 private static final int PPI
= 72; // points per inch
127 private static final int DPI
= Display
.getDefault().getDPI().y
;
129 /** Resource manager */
130 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
132 /** Color map for event types */
133 private Color
[] fEventColorMap
= null;
135 private ITimeDataProvider fTimeProvider
;
136 private IStatusLineManager fStatusLineManager
= null;
137 private TimeGraphScale fTimeGraphScale
= null;
139 private boolean fIsInFocus
= false;
140 private boolean fMouseOverSplitLine
= false;
141 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
142 private int fHeightAdjustment
= 0;
143 private Map
<Integer
, Font
> fFonts
= new HashMap
<>();
144 private boolean fBlendSubPixelEvents
= false;
145 private int fMinimumItemWidth
= 0;
146 private int fTopIndex
= 0;
147 private int fDragState
= DRAG_NONE
;
148 private boolean fDragBeginMarker
= false;
149 private int fDragButton
;
150 private int fDragX0
= 0;
151 private int fDragX
= 0;
152 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
153 private int fIdealNameSpace
= 0;
154 private long fTime0bak
;
155 private long fTime1bak
;
156 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
157 private ItemData fItemData
= null;
158 private List
<IMarkerEvent
> fMarkers
= null;
159 private boolean fMarkersVisible
= true;
160 private List
<SelectionListener
> fSelectionListeners
;
161 private List
<ITimeGraphTimeListener
> fDragSelectionListeners
;
162 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<>();
163 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<>();
164 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<>();
165 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<>();
166 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
167 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
168 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
169 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
170 private final List
<@NonNull ViewerFilter
> fFilters
= new ArrayList
<>();
171 private MenuDetectEvent fPendingMenuDetectEvent
= null;
172 private boolean fGridLinesVisible
= true;
173 private Color fGridLineColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_GRAY
);
174 private boolean fHideArrows
= false;
175 private int fAutoExpandLevel
= ALL_LEVELS
;
177 private int fBorderWidth
= 0;
178 private int fHeaderHeight
= 0;
181 * Standard constructor
184 * The parent composite object
186 * The color scheme to use
188 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
190 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.DOUBLE_BUFFERED
);
192 fItemData
= new ItemData();
194 addFocusListener(this);
195 addMouseListener(this);
196 addMouseMoveListener(this);
197 addMouseTrackListener(this);
198 addMouseWheelListener(this);
199 addTraverseListener(this);
200 addKeyListener(this);
201 addMenuDetectListener(this);
202 addListener(SWT
.MouseWheel
, this);
206 public void dispose() {
208 fResourceManager
.dispose();
209 for (Font font
: fFonts
.values()) {
215 * Sets the timegraph provider used by this timegraph viewer.
217 * @param timeGraphProvider the timegraph provider
219 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
220 fTimeGraphProvider
= timeGraphProvider
;
222 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
223 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
224 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).addColorListener(this);
227 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
228 colorSettingsChanged(stateItems
);
232 * Gets the timegraph provider used by this timegraph viewer.
234 * @return the timegraph provider, or <code>null</code> if not set.
236 public ITimeGraphPresentationProvider
getTimeGraphProvider() {
237 return fTimeGraphProvider
;
241 * Gets the color map used by this timegraph viewer.
243 * @return a color map, or <code>null</code> if not set.
245 public Color
[] getEventColorMap() {
246 return fEventColorMap
;
250 * Assign the given time provider
252 * @param timeProvider
255 public void setTimeProvider(ITimeDataProvider timeProvider
) {
256 fTimeProvider
= timeProvider
;
261 * Assign the status line manager
263 * @param statusLineManager
264 * The status line manager, or null to disable status line messages
266 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
267 if (fStatusLineManager
!= null && statusLineManager
== null) {
268 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
270 fStatusLineManager
= statusLineManager
;
274 * Assign the time graph scale
276 * @param timeGraphScale
277 * The time graph scale
279 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
280 fTimeGraphScale
= timeGraphScale
;
284 * Add a selection listener
287 * The listener to add
289 public void addSelectionListener(SelectionListener listener
) {
290 if (listener
== null) {
291 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
293 if (null == fSelectionListeners
) {
294 fSelectionListeners
= new ArrayList
<>();
296 fSelectionListeners
.add(listener
);
300 * Remove a selection listener
303 * The listener to remove
305 public void removeSelectionListener(SelectionListener listener
) {
306 if (null != fSelectionListeners
) {
307 fSelectionListeners
.remove(listener
);
312 * Selection changed callback
314 public void fireSelectionChanged() {
315 if (null != fSelectionListeners
) {
316 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
317 while (it
.hasNext()) {
318 SelectionListener listener
= it
.next();
319 listener
.widgetSelected(null);
325 * Default selection callback
327 public void fireDefaultSelection() {
328 if (null != fSelectionListeners
) {
329 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
330 while (it
.hasNext()) {
331 SelectionListener listener
= it
.next();
332 listener
.widgetDefaultSelected(null);
338 * Add a drag selection listener
341 * The listener to add
343 public void addDragSelectionListener(ITimeGraphTimeListener listener
) {
344 if (listener
== null) {
345 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
347 if (null == fDragSelectionListeners
) {
348 fDragSelectionListeners
= new ArrayList
<>();
350 fDragSelectionListeners
.add(listener
);
354 * Remove a drag selection listener
357 * The listener to remove
359 public void removeDragSelectionListener(ITimeGraphTimeListener listener
) {
360 if (null != fDragSelectionListeners
) {
361 fDragSelectionListeners
.remove(listener
);
366 * Drag Selection changed callback
369 * Time interval start
373 public void fireDragSelectionChanged(long start
, long end
) {
374 // check for backward intervals
375 long beginTime
, endTime
;
383 // call the listeners
384 if (null != fDragSelectionListeners
) {
385 Iterator
<ITimeGraphTimeListener
> it
= fDragSelectionListeners
.iterator();
386 while (it
.hasNext()) {
387 ITimeGraphTimeListener listener
= it
.next();
388 listener
.timeSelected(new TimeGraphTimeEvent(this, beginTime
, endTime
));
394 * Get the traces in the model
396 * @return The array of traces
398 public ITimeGraphEntry
[] getTraces() {
399 return fItemData
.getEntries();
403 * Refresh the data for the thing
405 public void refreshData() {
406 fItemData
.refreshData();
411 * Refresh data for the given traces
414 * The traces to refresh
416 public void refreshData(ITimeGraphEntry
[] traces
) {
417 fItemData
.refreshData(traces
);
422 * Refresh the links (arrows) of this widget
424 * @param events The link event list
426 public void refreshArrows(List
<ILinkEvent
> events
) {
427 fItemData
.refreshArrows(events
);
431 * Get the links (arrows) of this widget
433 * @return The unmodifiable link event list
437 public List
<ILinkEvent
> getArrows() {
438 return Collections
.unmodifiableList(fItemData
.fLinks
);
441 boolean ensureVisibleItem(int idx
, boolean redraw
) {
442 boolean changed
= false;
445 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
446 if (fItemData
.fExpandedItems
[index
].fSelected
) {
451 if (index
>= fItemData
.fExpandedItems
.length
) {
454 if (index
< fTopIndex
) {
461 int page
= countPerPage();
462 if (index
>= fTopIndex
+ page
) {
463 setTopIndex(index
- page
+ 1);
474 * Assign the given index as the top one
479 public void setTopIndex(int idx
) {
480 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
481 index
= Math
.max(0, index
);
487 * Sets the auto-expand level to be used for new entries discovered when
488 * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}
489 * . The value 0 means that there is no auto-expand; 1 means that top-level
490 * entries are expanded, but not their children; 2 means that top-level
491 * entries are expanded, and their children, but not grand-children; and so
494 * The value {@link #ALL_LEVELS} means that all subtrees should be expanded.
498 * non-negative level, or <code>ALL_LEVELS</code> to expand all
501 public void setAutoExpandLevel(int level
) {
502 fAutoExpandLevel
= level
;
506 * Returns the auto-expand level.
508 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of
509 * the tree are expanded automatically
510 * @see #setAutoExpandLevel
512 public int getAutoExpandLevel() {
513 return fAutoExpandLevel
;
517 * Get the expanded state of a given entry.
521 * @return true if the entry is expanded, false if collapsed
524 public boolean getExpandedState(ITimeGraphEntry entry
) {
525 Item item
= fItemData
.fItemMap
.get(entry
);
526 return (item
!= null ? item
.fExpanded
: false);
530 * Set the expanded state of a given entry
535 * True if expanded, false if collapsed
537 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
538 Item item
= fItemData
.findItem(entry
);
539 if (item
!= null && item
.fExpanded
!= expanded
) {
540 item
.fExpanded
= expanded
;
541 fItemData
.updateExpandedItems();
547 * Collapses all nodes of the viewer's tree, starting with the root.
549 public void collapseAll() {
550 for (Item item
: fItemData
.fItems
) {
551 item
.fExpanded
= false;
553 fItemData
.updateExpandedItems();
558 * Expands all nodes of the viewer's tree, starting with the root.
560 public void expandAll() {
561 for (Item item
: fItemData
.fItems
) {
562 item
.fExpanded
= true;
564 fItemData
.updateExpandedItems();
569 * Add a tree listener
572 * The listener to add
574 public void addTreeListener(ITimeGraphTreeListener listener
) {
575 if (!fTreeListeners
.contains(listener
)) {
576 fTreeListeners
.add(listener
);
581 * Remove a tree listener
584 * The listener to remove
586 public void removeTreeListener(ITimeGraphTreeListener listener
) {
587 if (fTreeListeners
.contains(listener
)) {
588 fTreeListeners
.remove(listener
);
593 * Tree event callback
598 * The expanded state (true for expanded, false for collapsed)
600 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
601 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
602 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
604 listener
.treeExpanded(event
);
606 listener
.treeCollapsed(event
);
612 * Add a menu listener on {@link ITimeGraphEntry}s
615 * The listener to add
617 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
618 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
619 fTimeGraphEntryMenuListeners
.add(listener
);
624 * Remove a menu listener on {@link ITimeGraphEntry}s
627 * The listener to remove
629 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
630 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
631 fTimeGraphEntryMenuListeners
.remove(listener
);
636 * Menu event callback on {@link ITimeGraphEntry}s
639 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
641 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
642 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
643 listener
.menuDetected(event
);
648 * Add a menu listener on {@link ITimeEvent}s
651 * The listener to add
653 public void addTimeEventMenuListener(MenuDetectListener listener
) {
654 if (!fTimeEventMenuListeners
.contains(listener
)) {
655 fTimeEventMenuListeners
.add(listener
);
660 * Remove a menu listener on {@link ITimeEvent}s
663 * The listener to remove
665 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
666 if (fTimeEventMenuListeners
.contains(listener
)) {
667 fTimeEventMenuListeners
.remove(listener
);
672 * Menu event callback on {@link ITimeEvent}s
675 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
677 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
678 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
679 listener
.menuDetected(event
);
684 public ISelection
getSelection() {
685 TimeGraphSelection sel
= new TimeGraphSelection();
686 ITimeGraphEntry trace
= getSelectedTrace();
687 if (null != trace
&& null != fTimeProvider
) {
688 long selectedTime
= fTimeProvider
.getSelectionBegin();
689 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
700 * Get the selection object
702 * @return The selection
704 public ISelection
getSelectionTrace() {
705 TimeGraphSelection sel
= new TimeGraphSelection();
706 ITimeGraphEntry trace
= getSelectedTrace();
714 * Enable/disable one of the traces in the model
717 * 1 to enable it, -1 to disable. The method returns immediately
718 * if another value is used.
720 public void selectTrace(int n
) {
721 if ((n
!= 1) && (n
!= -1)) {
725 boolean changed
= false;
726 int lastSelection
= -1;
727 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
728 Item item
= fItemData
.fExpandedItems
[i
];
729 if (item
.fSelected
) {
731 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
732 item
.fSelected
= false;
733 item
= fItemData
.fExpandedItems
[i
+ 1];
734 item
.fSelected
= true;
736 } else if ((-1 == n
) && (i
> 0)) {
737 item
.fSelected
= false;
738 item
= fItemData
.fExpandedItems
[i
- 1];
739 item
.fSelected
= true;
746 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
747 Item item
= fItemData
.fExpandedItems
[0];
748 item
.fSelected
= true;
753 ensureVisibleItem(-1, false);
755 fireSelectionChanged();
763 * 1 for next event, -1 for previous event
765 * true to extend selection range, false for single selection
768 public void selectEvent(int n
, boolean extend
) {
769 if (null == fTimeProvider
) {
772 ITimeGraphEntry trace
= getSelectedTrace();
776 long selectedTime
= fTimeProvider
.getSelectionEnd();
777 long endTime
= fTimeProvider
.getMaxTime();
778 ITimeEvent nextEvent
;
779 if (n
== -1 && selectedTime
> endTime
) {
780 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
782 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
784 if (null == nextEvent
&& n
== -1) {
785 nextEvent
= Utils
.getFirstEvent(trace
);
787 if (null != nextEvent
) {
788 long nextTime
= nextEvent
.getTime();
789 // If last event detected e.g. going back or not moving to a next
791 if (nextTime
<= selectedTime
&& n
== 1) {
792 // Select to the end of this last event
793 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
794 // but not beyond the end of the trace
795 if (nextTime
> endTime
) {
798 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
799 // for previous event go to its end time unless we were already there
800 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
803 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), nextTime
, true);
805 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
807 fireSelectionChanged();
810 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), endTime
, true);
812 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
814 fireSelectionChanged();
816 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
820 * Select the next event
823 * true to extend selection range, false for single selection
826 public void selectNextEvent(boolean extend
) {
827 selectEvent(1, extend
);
828 // Notify if visible time window has been adjusted
829 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
833 * Select the previous event
836 * true to extend selection range, false for single selection
839 public void selectPrevEvent(boolean extend
) {
840 selectEvent(-1, extend
);
841 // Notify if visible time window has been adjusted
842 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
846 * Select the next trace
848 public void selectNextTrace() {
853 * Select the previous trace
855 public void selectPrevTrace() {
860 * Scroll left or right by one half window size
863 * true to scroll left, false to scroll right
865 public void horizontalScroll(boolean left
) {
866 long time0
= fTimeProvider
.getTime0();
867 long time1
= fTimeProvider
.getTime1();
868 long timeMin
= fTimeProvider
.getMinTime();
869 long timeMax
= fTimeProvider
.getMaxTime();
870 long range
= time1
- time0
;
874 long increment
= Math
.max(1, range
/ 2);
876 time0
= Math
.max(time0
- increment
, timeMin
);
877 time1
= time0
+ range
;
879 time1
= Math
.min(time1
+ increment
, timeMax
);
880 time0
= time1
- range
;
882 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
886 * Zoom based on mouse cursor location with mouse scrolling
888 * @param zoomIn true to zoom in, false to zoom out
890 public void zoom(boolean zoomIn
) {
891 int globalX
= getDisplay().getCursorLocation().x
;
892 Point p
= toControl(globalX
, 0);
893 int nameSpace
= fTimeProvider
.getNameSpace();
894 int timeSpace
= fTimeProvider
.getTimeSpace();
895 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
896 long time0
= fTimeProvider
.getTime0();
897 long time1
= fTimeProvider
.getTime1();
898 long interval
= time1
- time0
;
901 } // to allow getting out of single point interval
904 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
906 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
908 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
909 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
910 long newTime1
= newTime0
+ newInterval
;
911 fTimeProvider
.setStartFinishTimeNotify(newTime0
, newTime1
);
915 * zoom in using single click
917 public void zoomIn() {
918 long prevTime0
= fTimeProvider
.getTime0();
919 long prevTime1
= fTimeProvider
.getTime1();
920 long prevRange
= prevTime1
- prevTime0
;
921 if (prevRange
== 0) {
924 ITimeDataProvider provider
= fTimeProvider
;
925 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
926 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
927 selTime
= (prevTime0
+ prevTime1
) / 2;
929 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
930 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
932 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
934 if (inaccuracy
> 0 && inaccuracy
< 100) {
935 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
939 long min
= fTimeProvider
.getMinTimeInterval();
940 if ((time1
- time0
) < min
) {
941 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
945 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
949 * zoom out using single click
951 public void zoomOut() {
952 long prevTime0
= fTimeProvider
.getTime0();
953 long prevTime1
= fTimeProvider
.getTime1();
954 ITimeDataProvider provider
= fTimeProvider
;
955 long selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
956 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
957 selTime
= (prevTime0
+ prevTime1
) / 2;
959 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
960 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
962 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
963 if (inaccuracy
> 0 && inaccuracy
< 100) {
964 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
968 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
975 * true to zoom in, false to zoom out
978 public void verticalZoom(boolean zoomIn
) {
984 fItemData
.refreshData();
989 * Reset the vertical zoom to default.
993 public void resetVerticalZoom() {
994 fHeightAdjustment
= 0;
995 fItemData
.refreshData();
1000 * Set the grid lines visibility. The default is true.
1003 * true to show the grid lines, false otherwise
1006 public void setGridLinesVisible(boolean visible
) {
1007 fGridLinesVisible
= visible
;
1011 * Get the grid lines visibility.
1013 * @return true if the grid lines are visible, false otherwise
1016 public boolean getGridLinesVisible() {
1017 return fGridLinesVisible
;
1021 * Set the grid line color. The default is SWT.COLOR_GRAY.
1024 * the grid line color
1027 public void setGridLineColor(Color color
) {
1028 fGridLineColor
= color
;
1032 * Get the grid line color.
1034 * @return the grid line color
1037 public Color
getGridLineColor() {
1038 return fGridLineColor
;
1042 * Set the markers list.
1045 * The markers list, or null
1048 public void setMarkers(List
<IMarkerEvent
> markers
) {
1050 fTimeGraphScale
.setMarkers(markers
);
1054 * Get the markers list.
1056 * @return The markers list, or null
1059 public List
<IMarkerEvent
> getMarkers() {
1064 * Set the markers visibility. The default is true.
1067 * true to show the markers, false otherwise
1070 public void setMarkersVisible(boolean visible
) {
1071 fMarkersVisible
= visible
;
1072 fTimeGraphScale
.setMarkersVisible(visible
);
1076 * Get the markers visibility.
1078 * @return true if the markers are visible, false otherwise
1081 public boolean getMarkersVisible() {
1082 return fMarkersVisible
;
1088 * @param hideArrows true to hide arrows
1090 public void hideArrows(boolean hideArrows
) {
1091 fHideArrows
= hideArrows
;
1095 * Follow the arrow forward
1098 * true to extend selection range, false for single selection
1101 public void followArrowFwd(boolean extend
) {
1102 ITimeGraphEntry trace
= getSelectedTrace();
1103 if (trace
== null) {
1106 long selectedTime
= fTimeProvider
.getSelectionEnd();
1107 for (ILinkEvent link
: fItemData
.fLinks
) {
1108 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
1109 selectItem(link
.getDestinationEntry(), false);
1110 if (link
.getDuration() != 0) {
1112 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime() + link
.getDuration(), true);
1114 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
1116 // Notify if visible time window has been adjusted
1117 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
1119 fireSelectionChanged();
1120 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
1124 selectNextEvent(extend
);
1128 * Follow the arrow backward
1131 * true to extend selection range, false for single selection
1134 public void followArrowBwd(boolean extend
) {
1135 ITimeGraphEntry trace
= getSelectedTrace();
1136 if (trace
== null) {
1139 long selectedTime
= fTimeProvider
.getSelectionEnd();
1140 for (ILinkEvent link
: fItemData
.fLinks
) {
1141 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
1142 selectItem(link
.getEntry(), false);
1143 if (link
.getDuration() != 0) {
1145 fTimeProvider
.setSelectionRangeNotify(fTimeProvider
.getSelectionBegin(), link
.getTime(), true);
1147 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), 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 selectPrevEvent(extend
);
1161 * Return the currently selected trace
1163 * @return The entry matching the trace
1165 public ITimeGraphEntry
getSelectedTrace() {
1166 ITimeGraphEntry trace
= null;
1167 int idx
= getSelectedIndex();
1169 trace
= fItemData
.fExpandedItems
[idx
].fEntry
;
1175 * Retrieve the index of the currently selected item
1179 public int getSelectedIndex() {
1181 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1182 Item item
= fItemData
.fExpandedItems
[i
];
1183 if (item
.fSelected
) {
1191 boolean toggle(int idx
) {
1192 boolean toggled
= false;
1193 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1194 Item item
= fItemData
.fExpandedItems
[idx
];
1195 if (item
.fHasChildren
) {
1196 item
.fExpanded
= !item
.fExpanded
;
1197 fItemData
.updateExpandedItems();
1200 fireTreeEvent(item
.fEntry
, item
.fExpanded
);
1207 * Gets the index of the item at the given location.
1211 * @return the index of the item at the given location, of -1 if none.
1213 protected int getItemIndexAtY(int y
) {
1218 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1219 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1227 boolean isOverSplitLine(int x
) {
1228 if (x
< 0 || null == fTimeProvider
) {
1231 int nameWidth
= fTimeProvider
.getNameSpace();
1232 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1236 * Gets the {@link ITimeGraphEntry} at the given location.
1239 * a point in the widget
1240 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1243 protected ITimeGraphEntry
getEntry(Point pt
) {
1244 int idx
= getItemIndexAtY(pt
.y
);
1245 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fEntry
: null;
1249 * Return the arrow event closest to the given point that is no further than
1250 * a maximum distance.
1253 * a point in the widget
1254 * @return The closest arrow event, or null if there is none close enough.
1256 protected ILinkEvent
getArrow(Point pt
) {
1260 ILinkEvent linkEvent
= null;
1261 double minDistance
= Double
.MAX_VALUE
;
1262 for (ILinkEvent event
: fItemData
.fLinks
) {
1263 Rectangle rect
= getArrowRectangle(new Rectangle(0, 0, 0, 0), event
);
1267 int x2
= x1
+ rect
.width
;
1268 int y2
= y1
+ rect
.height
;
1269 double d
= Utils
.distance(pt
.x
, pt
.y
, x1
, y1
, x2
, y2
);
1270 if (minDistance
> d
) {
1276 if (minDistance
<= ARROW_HOVER_MAX_DIST
) {
1283 public int getXForTime(long time
) {
1284 if (null == fTimeProvider
) {
1287 long time0
= fTimeProvider
.getTime0();
1288 long time1
= fTimeProvider
.getTime1();
1289 int width
= getSize().x
;
1290 int nameSpace
= fTimeProvider
.getNameSpace();
1291 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1292 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1297 public long getTimeAtX(int coord
) {
1298 if (null == fTimeProvider
) {
1302 Point size
= getSize();
1303 long time0
= fTimeProvider
.getTime0();
1304 long time1
= fTimeProvider
.getTime1();
1305 int nameWidth
= fTimeProvider
.getNameSpace();
1306 final int x
= coord
- nameWidth
;
1307 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1308 if (x
>= 0 && size
.x
>= nameWidth
) {
1309 if (time1
- time0
> timeWidth
) {
1310 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1311 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1313 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1314 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1320 void selectItem(int idx
, boolean addSelection
) {
1321 boolean changed
= false;
1323 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1324 Item item
= fItemData
.fExpandedItems
[idx
];
1325 changed
= !item
.fSelected
;
1326 item
.fSelected
= true;
1329 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1330 Item item
= fItemData
.fExpandedItems
[i
];
1331 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1334 item
.fSelected
= i
== idx
;
1337 changed
|= ensureVisibleItem(idx
, true);
1344 * Callback for item selection
1347 * The entry matching the trace
1348 * @param addSelection
1349 * If the selection is added or removed
1351 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1352 int idx
= fItemData
.findItemIndex(trace
);
1353 selectItem(idx
, addSelection
);
1357 * Retrieve the number of entries shown per page.
1361 public int countPerPage() {
1362 int height
= getSize().y
;
1365 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1366 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1367 if (ySum
>= height
) {
1372 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1373 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1374 if (ySum
>= height
) {
1383 * Get the index of the top element
1387 public int getTopIndex() {
1392 * Get the number of expanded (visible) items
1394 * @return The count of expanded (visible) items
1396 public int getExpandedElementCount() {
1397 return fItemData
.fExpandedItems
.length
;
1401 * Get an array of all expanded (visible) elements
1403 * @return The expanded (visible) elements
1405 public ITimeGraphEntry
[] getExpandedElements() {
1406 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<>();
1407 for (Item item
: fItemData
.fExpandedItems
) {
1408 elements
.add(item
.fEntry
);
1410 return elements
.toArray(new ITimeGraphEntry
[0]);
1413 Rectangle
getNameRect(Rectangle bounds
, int idx
, int nameWidth
) {
1414 Rectangle rect
= getItemRect(bounds
, idx
);
1415 rect
.width
= nameWidth
;
1419 Rectangle
getStatesRect(Rectangle bounds
, int idx
, int nameWidth
) {
1420 Rectangle rect
= getItemRect(bounds
, idx
);
1421 rect
.x
+= nameWidth
;
1422 rect
.width
-= nameWidth
;
1426 Rectangle
getItemRect(Rectangle bounds
, int idx
) {
1428 if (idx
>= fTopIndex
) {
1429 for (int i
= fTopIndex
; i
< idx
; i
++) {
1430 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1433 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1434 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1437 int y
= bounds
.y
+ ySum
;
1438 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1439 return new Rectangle(bounds
.x
, y
, bounds
.width
, height
);
1443 void paint(Rectangle bounds
, PaintEvent e
) {
1446 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1450 fIdealNameSpace
= 0;
1451 int nameSpace
= fTimeProvider
.getNameSpace();
1453 // draw the background layer
1454 drawBackground(bounds
, nameSpace
, gc
);
1456 // draw the grid lines
1457 drawGridLines(bounds
, gc
);
1459 // draw the background markers
1460 drawMarkers(bounds
, fTimeProvider
, fMarkers
, false, nameSpace
, gc
);
1463 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1465 // draw the foreground markers
1466 drawMarkers(bounds
, fTimeProvider
, fMarkers
, true, nameSpace
, gc
);
1468 // draw the links (arrows)
1469 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1471 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1473 int alpha
= gc
.getAlpha();
1476 long time0
= fTimeProvider
.getTime0();
1477 long time1
= fTimeProvider
.getTime1();
1478 long selectionBegin
= fTimeProvider
.getSelectionBegin();
1479 long selectionEnd
= fTimeProvider
.getSelectionEnd();
1480 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1481 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1482 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1484 // draw selection lines
1485 if (fDragState
!= DRAG_SELECTION
) {
1486 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1487 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1488 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1491 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1492 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1497 // draw selection background
1498 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1499 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1500 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1501 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1503 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1504 } else if (x0
- x1
> 1) {
1505 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1509 // draw drag selection background
1510 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1511 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1512 if (fDragX0
< fDragX
) {
1513 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1514 } else if (fDragX0
> fDragX
) {
1515 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1520 if (DRAG_SPLIT_LINE
== fDragState
) {
1521 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1522 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1523 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1524 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1525 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1526 if (fDragX
!= fDragX0
) {
1527 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1529 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1530 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1531 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1532 if (fDragX
!= fDragX0
) {
1533 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1535 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1536 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1537 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1544 * Draw the background layer. Fills the background of the control's name
1545 * space and states space, updates the background of items if necessary,
1546 * and draws the item's name text and middle line.
1549 * The bounds of the control
1551 * The name space width
1556 protected void drawBackground(Rectangle bounds
, int nameSpace
, GC gc
) {
1557 // draw empty name space background
1558 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1559 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1561 // draw empty states space background
1562 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1563 drawBackground(gc
, bounds
.x
+ nameSpace
, bounds
.y
, bounds
.width
- nameSpace
, bounds
.height
);
1565 for (int i
= fTopIndex
; i
< fItemData
.fExpandedItems
.length
; i
++) {
1566 Rectangle itemRect
= getItemRect(bounds
, i
);
1567 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1570 Item item
= fItemData
.fExpandedItems
[i
];
1571 // draw the background of selected item and items with no time events
1572 if (! item
.fEntry
.hasTimeEvents()) {
1573 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1574 gc
.fillRectangle(itemRect
);
1575 } else if (item
.fSelected
) {
1576 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, true));
1577 gc
.fillRectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1578 gc
.setBackground(getColorScheme().getBkColor(true, fIsInFocus
, false));
1579 gc
.fillRectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1581 // draw the name and middle line
1582 if (! item
.fEntry
.hasTimeEvents()) {
1583 drawName(item
, itemRect
, gc
);
1585 Rectangle nameRect
= new Rectangle(itemRect
.x
, itemRect
.y
, nameSpace
, itemRect
.height
);
1586 drawName(item
, nameRect
, gc
);
1587 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1588 drawMidLine(rect
, gc
);
1594 * Draw the grid lines
1597 * The bounds of the control
1602 public void drawGridLines(Rectangle bounds
, GC gc
) {
1603 if (!fGridLinesVisible
) {
1606 gc
.setForeground(fGridLineColor
);
1607 gc
.setAlpha(fGridLineColor
.getAlpha());
1608 for (int x
: fTimeGraphScale
.getTickList()) {
1609 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1618 * The rectangle of the area
1619 * @param timeProvider
1622 * The list of markers
1624 * true to draw the foreground markers, false otherwise
1626 * The width reserved for the names
1628 * Reference to the SWT GC object
1631 protected void drawMarkers(Rectangle bounds
, ITimeDataProvider timeProvider
, List
<IMarkerEvent
> markers
, boolean foreground
, int nameSpace
, GC gc
) {
1632 if (!fMarkersVisible
|| markers
== null || markers
.isEmpty()) {
1635 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1636 /* the list can grow concurrently but cannot shrink */
1637 for (int i
= 0; i
< markers
.size(); i
++) {
1638 IMarkerEvent marker
= markers
.get(i
);
1639 if (marker
.isForeground() == foreground
) {
1640 drawMarker(marker
, bounds
, timeProvider
, nameSpace
, gc
);
1643 gc
.setClipping((Rectangle
) null);
1647 * Draw a single marker
1652 * The bounds of the control
1653 * @param timeProvider
1656 * The width reserved for the name
1658 * Reference to the SWT GC object
1661 protected void drawMarker(IMarkerEvent marker
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1662 Rectangle rect
= Utils
.clone(bounds
);
1663 if (marker
.getEntry() != null) {
1664 int index
= fItemData
.findItemIndex(marker
.getEntry());
1668 rect
= getStatesRect(bounds
, index
, nameSpace
);
1669 if (rect
.y
< 0 || rect
.y
> bounds
.height
) {
1673 int x0
= getXForTime(marker
.getTime());
1674 int x1
= getXForTime(marker
.getTime() + marker
.getDuration());
1675 if (x0
> bounds
.width
|| x1
< nameSpace
) {
1678 rect
.x
= Math
.max(nameSpace
, Math
.min(bounds
.width
, x0
));
1679 rect
.width
= Math
.max(1, Math
.min(bounds
.width
, x1
) - rect
.x
);
1681 gc
.setBackground(marker
.getColor());
1682 gc
.setAlpha(marker
.getColor().getAlpha());
1683 gc
.fillRectangle(rect
);
1685 String label
= marker
.getLabel();
1686 if (label
!= null && marker
.getEntry() != null) {
1687 label
= label
.substring(0, Math
.min(label
.indexOf('\n') != -1 ? label
.indexOf('\n') : label
.length(), MAX_LABEL_LENGTH
));
1688 gc
.setForeground(marker
.getColor());
1689 Utils
.drawText(gc
, label
, rect
.x
- gc
.textExtent(label
).x
, rect
.y
, true);
1694 * Draw many items at once
1697 * The bounds of the control
1698 * @param timeProvider
1701 * The array items to draw
1703 * The index of the first element to draw
1705 * The name space width
1709 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1710 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1711 for (int i
= topIndex
; i
< items
.length
; i
++) {
1712 Item item
= items
[i
];
1713 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1723 * The bounds of the control
1724 * @param timeProvider
1727 * The expanded item index
1729 * The name space width
1733 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1734 Rectangle itemRect
= getItemRect(bounds
, i
);
1735 if (itemRect
.y
>= bounds
.y
+ bounds
.height
) {
1739 ITimeGraphEntry entry
= item
.fEntry
;
1740 long time0
= timeProvider
.getTime0();
1741 long time1
= timeProvider
.getTime1();
1742 long selectedTime
= fTimeProvider
.getSelectionEnd();
1744 Rectangle rect
= new Rectangle(nameSpace
, itemRect
.y
, itemRect
.width
- nameSpace
, itemRect
.height
);
1745 if (rect
.isEmpty() || (time1
<= time0
)) {
1746 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1750 boolean selected
= item
.fSelected
;
1751 // K pixels per second
1752 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1754 if (item
.fEntry
.hasTimeEvents()) {
1755 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1756 fillSpace(rect
, gc
, selected
);
1758 int margins
= getMarginForHeight(rect
.height
);
1759 int height
= rect
.height
- margins
;
1760 int topMargin
= (margins
+ 1) / 2;
1761 Rectangle stateRect
= new Rectangle(rect
.x
, rect
.y
+ topMargin
, rect
.width
, height
);
1763 /* Set the font for this item */
1764 setFontForHeight(height
, gc
);
1766 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1767 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1770 while (iterator
.hasNext()) {
1771 ITimeEvent event
= iterator
.next();
1772 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1773 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1774 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1775 // event is out of bounds
1778 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1779 stateRect
.x
= Math
.max(rect
.x
, x
);
1780 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1781 if (stateRect
.x
== lastX
) {
1782 stateRect
.width
-= 1;
1783 if (stateRect
.width
> 0) {
1784 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1785 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1789 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1790 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1791 lastX
= stateRect
.x
;
1794 gc
.setClipping((Rectangle
) null);
1796 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1803 * The bounds of the control
1804 * @param timeProvider
1807 * The list of link events
1809 * The name space width
1813 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1814 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1818 gc
.setClipping(new Rectangle(nameSpace
, 0, bounds
.width
- nameSpace
, bounds
.height
));
1819 /* the list can grow concurrently but cannot shrink */
1820 for (int i
= 0; i
< links
.size(); i
++) {
1821 drawLink(links
.get(i
), bounds
, timeProvider
, nameSpace
, gc
);
1823 gc
.setClipping((Rectangle
) null);
1827 * Draws a link type event
1830 * The link event to draw
1832 * The bounds of the control
1833 * @param timeProvider
1836 * The name space width
1840 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1841 drawArrow(getColorScheme(), event
, getArrowRectangle(bounds
, event
), gc
);
1844 private Rectangle
getArrowRectangle(Rectangle bounds
, ILinkEvent event
) {
1845 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1846 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1848 if ((srcIndex
== -1) || (destIndex
== -1)) {
1852 Rectangle src
= getStatesRect(bounds
, srcIndex
, fTimeProvider
.getNameSpace());
1853 Rectangle dst
= getStatesRect(bounds
, destIndex
, fTimeProvider
.getNameSpace());
1855 int x0
= getXForTime(event
.getTime());
1856 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1858 // limit the x-coordinates to prevent integer overflow in calculations
1859 // and also GC.drawLine doesn't draw properly with large coordinates
1860 final int limit
= Integer
.MAX_VALUE
/ 1024;
1861 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1862 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1864 int y0
= src
.y
+ src
.height
/ 2;
1865 int y1
= dst
.y
+ dst
.height
/ 2;
1866 return new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
);
1875 * Time event for which we're drawing the arrow
1877 * The arrow rectangle
1880 * @return true if the arrow was drawn
1882 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1883 Rectangle rect
, GC gc
) {
1888 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1892 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1895 Color stateColor
= null;
1896 if (colorIdx
< fEventColorMap
.length
) {
1897 stateColor
= fEventColorMap
[colorIdx
];
1899 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1902 gc
.setForeground(stateColor
);
1903 gc
.setBackground(stateColor
);
1905 /* Draw the arrow */
1906 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1907 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1910 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1915 * @author Francis Giraldeau
1918 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1920 * The algorithm was taken from this site, not the code itself
1922 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1925 double cos
= 0.9510;
1926 double sin
= 0.3090;
1927 long lenx
= x1
- x0
;
1928 long leny
= y1
- y0
;
1929 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1931 double dx
= factor
* lenx
/ len
;
1932 double dy
= factor
* leny
/ len
;
1933 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1934 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1935 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1936 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1937 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1938 gc
.fillPolygon(arrow
);
1942 * Draw the name of an item.
1947 * The bounds of the item's name space
1951 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1952 boolean hasTimeEvents
= item
.fEntry
.hasTimeEvents();
1954 // No name to be drawn
1955 if (fTimeProvider
.getNameSpace() == 0) {
1959 int height
= bounds
.height
- getMarginForHeight(bounds
.height
);
1960 setFontForHeight(height
, gc
);
1962 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1963 if (item
.fHasChildren
) {
1964 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1965 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1966 Rectangle rect
= Utils
.clone(bounds
);
1967 rect
.x
+= leftMargin
;
1968 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1969 rect
.width
= EXPAND_SIZE
;
1970 rect
.height
= EXPAND_SIZE
;
1971 gc
.fillRectangle(rect
);
1972 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1973 int midy
= rect
.y
+ rect
.height
/ 2;
1974 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1975 if (!item
.fExpanded
) {
1976 int midx
= rect
.x
+ rect
.width
/ 2;
1977 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1980 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1982 Image img
= fTimeGraphProvider
.getItemImage(item
.fEntry
);
1985 int imgHeight
= img
.getImageData().height
;
1986 int imgWidth
= img
.getImageData().width
;
1988 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1989 gc
.drawImage(img
, x
, y
);
1990 leftMargin
+= imgWidth
+ MARGIN
;
1992 String name
= item
.fName
;
1993 Point size
= gc
.stringExtent(name
);
1994 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1995 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1997 if (hasTimeEvents
) {
1998 // cut long string with "..."
1999 int width
= bounds
.width
- leftMargin
;
2001 while (size
.x
> width
&& name
.length() > 1) {
2003 name
= name
.substring(0, name
.length() - 1);
2004 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
2007 name
+= "..."; //$NON-NLS-1$
2010 Rectangle rect
= Utils
.clone(bounds
);
2011 rect
.x
+= leftMargin
;
2012 rect
.width
-= leftMargin
;
2014 if (rect
.width
> 0) {
2015 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
2016 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
2017 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
2018 leftMargin
+= textWidth
+ MARGIN
;
2020 if (hasTimeEvents
) {
2022 rect
.x
= bounds
.x
+ leftMargin
;
2024 rect
.width
= bounds
.width
- rect
.x
;
2025 drawMidLine(rect
, gc
);
2031 * Draw the state (color fill)
2036 * Time event for which we're drawing the state
2038 * The state rectangle
2042 * Is this time event currently selected (so it appears
2044 * @param timeSelected
2045 * Is the timestamp currently selected
2046 * @return true if the state was drawn
2048 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
2049 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
2051 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
2052 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
2055 boolean visible
= rect
.width
== 0 ?
false : true;
2056 rect
.width
= Math
.max(1, rect
.width
);
2057 Color black
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
2058 gc
.setForeground(black
);
2060 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
2062 // Only draw the top and bottom borders
2063 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
2064 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
2065 if (rect
.width
== 1) {
2066 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2069 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2072 Color stateColor
= null;
2073 if (colorIdx
< fEventColorMap
.length
) {
2074 stateColor
= fEventColorMap
[colorIdx
];
2079 boolean reallySelected
= timeSelected
&& selected
;
2080 // fill all rect area
2081 gc
.setBackground(stateColor
);
2083 gc
.fillRectangle(rect
);
2084 } else if (fBlendSubPixelEvents
) {
2086 gc
.fillRectangle(rect
);
2090 if (reallySelected
) {
2091 gc
.drawLine(rect
.x
, rect
.y
- 1, rect
.x
+ rect
.width
- 1, rect
.y
- 1);
2092 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
);
2095 gc
.drawPoint(rect
.x
, rect
.y
- 2);
2097 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
2102 * Fill an item's states rectangle
2105 * The states rectangle
2109 * true if the item is selected
2111 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
2112 /* Nothing to draw */
2116 * Draw a line at the middle height of a rectangle
2123 private void drawMidLine(Rectangle rect
, GC gc
) {
2124 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
2125 int midy
= rect
.y
+ rect
.height
/ 2;
2126 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
2129 private static int getMarginForHeight(int height
) {
2131 * State rectangle is smaller than the item bounds when height is > 4.
2132 * Don't use any margin if the height is below or equal that threshold.
2133 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2134 * pixels for the state height, but with a minimum margin of 1.
2136 final int MARGIN_THRESHOLD
= 4;
2137 final int PREFERRED_HEIGHT
= 13;
2138 final int MIN_MARGIN
= 1;
2139 final int MAX_MARGIN
= 6;
2140 return height
<= MARGIN_THRESHOLD ?
0 :
2141 Math
.max(Math
.min(height
- PREFERRED_HEIGHT
, MAX_MARGIN
), MIN_MARGIN
);
2144 private void setFontForHeight(int pixels
, GC gc
) {
2145 /* convert font height from pixels to points */
2146 int height
= Math
.max(pixels
* PPI
/ DPI
, 1);
2147 Font font
= fFonts
.get(height
);
2149 FontData fontData
= gc
.getFont().getFontData()[0];
2150 fontData
.setHeight(height
);
2151 font
= new Font(gc
.getDevice(), fontData
);
2152 fFonts
.put(height
, font
);
2158 public void keyTraversed(TraverseEvent e
) {
2159 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
2165 public void keyPressed(KeyEvent e
) {
2167 if (fItemData
.fExpandedItems
.length
== 0) {
2170 if (SWT
.HOME
== e
.keyCode
) {
2172 } else if (SWT
.END
== e
.keyCode
) {
2173 idx
= fItemData
.fExpandedItems
.length
- 1;
2174 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
2175 idx
= getSelectedIndex();
2178 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
2181 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
2182 idx
= getSelectedIndex();
2185 } else if (idx
> 0) {
2188 } else if (SWT
.ARROW_LEFT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2189 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2190 selectPrevEvent(extend
);
2191 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
&& fDragState
== DRAG_NONE
) {
2192 boolean extend
= (e
.stateMask
& SWT
.SHIFT
) != 0;
2193 selectNextEvent(extend
);
2194 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
2195 int page
= countPerPage();
2196 idx
= getSelectedIndex();
2201 if (idx
>= fItemData
.fExpandedItems
.length
) {
2202 idx
= fItemData
.fExpandedItems
.length
- 1;
2204 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
2205 int page
= countPerPage();
2206 idx
= getSelectedIndex();
2214 } else if (SWT
.CR
== e
.keyCode
) {
2215 idx
= getSelectedIndex();
2217 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
2220 fireDefaultSelection();
2224 } else if ((e
.character
== '+' || e
.character
== '=') && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2226 } else if (e
.character
== '-' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2227 verticalZoom(false);
2228 } else if (e
.character
== '0' && ((e
.stateMask
& SWT
.CTRL
) != 0)) {
2229 resetVerticalZoom();
2232 selectItem(idx
, false);
2233 fireSelectionChanged();
2235 int x
= toControl(e
.display
.getCursorLocation()).x
;
2236 updateCursor(x
, e
.stateMask
| e
.keyCode
);
2240 public void keyReleased(KeyEvent e
) {
2241 int x
= toControl(e
.display
.getCursorLocation()).x
;
2242 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
2246 public void focusGained(FocusEvent e
) {
2249 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2253 public void focusLost(FocusEvent e
) {
2255 if (DRAG_NONE
!= fDragState
) {
2257 fDragState
= DRAG_NONE
;
2260 updateStatusLine(NO_STATUS
);
2264 * @return If the current view is focused
2266 public boolean isInFocus() {
2271 * Provide the possibility to control the wait cursor externally e.g. data
2272 * requests in progress
2274 * @param waitInd Should we wait indefinitely?
2276 public void waitCursor(boolean waitInd
) {
2277 // Update cursor as indicated
2279 setCursor(fWaitCursor
);
2285 private void updateCursor(int x
, int stateMask
) {
2286 // if Wait cursor not active, check for the need to change the cursor
2287 if (getCursor() == fWaitCursor
) {
2290 Cursor cursor
= null;
2291 if (fDragState
== DRAG_SPLIT_LINE
) {
2292 } else if (fDragState
== DRAG_SELECTION
) {
2293 cursor
= fResizeCursor
;
2294 } else if (fDragState
== DRAG_TRACE_ITEM
) {
2295 cursor
= fDragCursor
;
2296 } else if (fDragState
== DRAG_ZOOM
) {
2297 cursor
= fZoomCursor
;
2298 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2299 cursor
= fDragCursor
;
2300 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2301 cursor
= fResizeCursor
;
2302 } else if (!isOverSplitLine(x
)) {
2303 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2304 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2305 int xBegin
= getXForTime(selectionBegin
);
2306 int xEnd
= getXForTime(selectionEnd
);
2307 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
2308 cursor
= fResizeCursor
;
2311 if (getCursor() != cursor
) {
2317 * Update the status line following a change of selection.
2321 public void updateStatusLine() {
2322 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2325 private void updateStatusLine(int x
) {
2326 // use the time provider of the time graph scale for the status line
2327 ITimeDataProvider tdp
= fTimeGraphScale
.getTimeProvider();
2328 if (fStatusLineManager
== null || null == tdp
||
2329 tdp
.getTime0() == tdp
.getTime1()) {
2332 TimeFormat tf
= tdp
.getTimeFormat();
2333 Resolution res
= Resolution
.NANOSEC
;
2334 StringBuilder message
= new StringBuilder();
2335 if ((x
>= 0 || x
== STATUS_WITHOUT_CURSOR_TIME
) && fDragState
== DRAG_NONE
) {
2336 if (x
!= STATUS_WITHOUT_CURSOR_TIME
) {
2337 long time
= getTimeAtX(x
);
2339 if (tdp
instanceof ITimeDataProviderConverter
) {
2340 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2342 message
.append(NLS
.bind("T: {0}{1} ", //$NON-NLS-1$
2344 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2345 Utils
.formatTime(time
, tf
, res
)
2349 long selectionBegin
= tdp
.getSelectionBegin();
2350 long selectionEnd
= tdp
.getSelectionEnd();
2351 message
.append(NLS
.bind("T1: {0}{1}", //$NON-NLS-1$
2353 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionBegin
) + ' ' : "", //$NON-NLS-1$
2354 Utils
.formatTime(selectionBegin
, tf
, res
)
2356 if (selectionBegin
!= selectionEnd
) {
2357 message
.append(NLS
.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2359 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(selectionEnd
) + ' ' : "", //$NON-NLS-1$
2360 Utils
.formatTime(selectionEnd
, tf
, res
),
2361 Utils
.formatDelta(selectionEnd
- selectionBegin
, tf
, res
)
2364 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
2365 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2366 long time
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2367 if (tdp
instanceof ITimeDataProviderConverter
) {
2368 time0
= ((ITimeDataProviderConverter
) tdp
).convertTime(time0
);
2369 time
= ((ITimeDataProviderConverter
) tdp
).convertTime(time
);
2371 message
.append(NLS
.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2373 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time0
) + ' ' : "", //$NON-NLS-1$
2374 Utils
.formatTime(time0
, tf
, res
),
2375 tf
== TimeFormat
.CALENDAR ? Utils
.formatDate(time
) + ' ' : "", //$NON-NLS-1$
2376 Utils
.formatTime(time
, tf
, res
),
2377 Utils
.formatDelta(time
- time0
, tf
, res
)
2380 fStatusLineManager
.setMessage(message
.toString());
2384 public void mouseMove(MouseEvent e
) {
2385 if (null == fTimeProvider
) {
2388 Point size
= getSize();
2389 if (DRAG_TRACE_ITEM
== fDragState
) {
2390 int nameWidth
= fTimeProvider
.getNameSpace();
2391 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
2393 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
2394 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
2395 long time1
= fTime1bak
- timeDelta
;
2396 long maxTime
= fTimeProvider
.getMaxTime();
2397 if (time1
> maxTime
) {
2400 long time0
= time1
- (fTime1bak
- fTime0bak
);
2401 if (time0
< fTimeProvider
.getMinTime()) {
2402 time0
= fTimeProvider
.getMinTime();
2403 time1
= time0
+ (fTime1bak
- fTime0bak
);
2405 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2407 } else if (DRAG_SPLIT_LINE
== fDragState
) {
2409 fTimeProvider
.setNameSpace(e
.x
);
2410 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2411 } else if (DRAG_SELECTION
== fDragState
) {
2412 if (fDragBeginMarker
) {
2413 fDragX0
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2415 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2418 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2419 fireDragSelectionChanged(getTimeAtX(fDragX0
), getTimeAtX(fDragX
));
2420 } else if (DRAG_ZOOM
== fDragState
) {
2421 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2423 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2424 } else if (DRAG_NONE
== fDragState
) {
2425 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2426 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2429 fMouseOverSplitLine
= mouseOverSplitLine
;
2431 updateCursor(e
.x
, e
.stateMask
);
2432 updateStatusLine(e
.x
);
2436 public void mouseDoubleClick(MouseEvent e
) {
2437 if (null == fTimeProvider
) {
2440 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2441 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2442 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2443 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2444 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2447 fMouseOverSplitLine
= mouseOverSplitLine
;
2448 TmfSignalManager
.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2451 int idx
= getItemIndexAtY(e
.y
);
2453 selectItem(idx
, false);
2454 fireDefaultSelection();
2460 public void mouseDown(MouseEvent e
) {
2461 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2462 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2463 getSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2467 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2468 int nameSpace
= fTimeProvider
.getNameSpace();
2469 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2470 fDragState
= DRAG_SPLIT_LINE
;
2471 fDragButton
= e
.button
;
2474 fTime0bak
= fTimeProvider
.getTime0();
2475 fTime1bak
= fTimeProvider
.getTime1();
2477 updateCursor(e
.x
, e
.stateMask
);
2481 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2482 int nameSpace
= fTimeProvider
.getNameSpace();
2483 idx
= getItemIndexAtY(e
.y
);
2485 Item item
= fItemData
.fExpandedItems
[idx
];
2486 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2490 selectItem(idx
, false);
2491 fireSelectionChanged();
2493 selectItem(idx
, false); // clear selection
2494 fireSelectionChanged();
2496 long hitTime
= getTimeAtX(e
.x
);
2500 fDragState
= DRAG_SELECTION
;
2501 fDragBeginMarker
= false;
2502 fDragButton
= e
.button
;
2505 fDragTime0
= getTimeAtX(fDragX0
);
2506 long selectionBegin
= fTimeProvider
.getSelectionBegin();
2507 long selectionEnd
= fTimeProvider
.getSelectionEnd();
2508 int xBegin
= getXForTime(selectionBegin
);
2509 int xEnd
= getXForTime(selectionEnd
);
2510 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2511 long time
= getTimeAtX(e
.x
);
2512 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2513 fDragBeginMarker
= true;
2516 fDragTime0
= selectionEnd
;
2519 fDragTime0
= selectionBegin
;
2522 long time
= getTimeAtX(e
.x
);
2523 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2524 fDragBeginMarker
= true;
2527 fDragTime0
= selectionEnd
;
2528 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2530 fDragTime0
= selectionBegin
;
2533 fTime0bak
= fTimeProvider
.getTime0();
2534 fTime1bak
= fTimeProvider
.getTime1();
2536 updateCursor(e
.x
, e
.stateMask
);
2537 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2539 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2540 long hitTime
= getTimeAtX(e
.x
);
2543 fDragState
= DRAG_TRACE_ITEM
;
2544 fDragButton
= e
.button
;
2547 fTime0bak
= fTimeProvider
.getTime0();
2548 fTime1bak
= fTimeProvider
.getTime1();
2549 updateCursor(e
.x
, e
.stateMask
);
2551 } else if (3 == e
.button
) {
2553 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getSize().x
- RIGHT_MARGIN
);
2555 fDragTime0
= getTimeAtX(fDragX0
);
2556 fDragState
= DRAG_ZOOM
;
2557 fDragButton
= e
.button
;
2559 updateCursor(e
.x
, e
.stateMask
);
2560 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2565 public void mouseUp(MouseEvent e
) {
2566 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2567 menuDetected(fPendingMenuDetectEvent
);
2569 if (DRAG_NONE
!= fDragState
) {
2571 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2572 if (fDragX
!= fDragX0
) {
2573 fTimeProvider
.notifyStartFinishTime();
2575 fDragState
= DRAG_NONE
;
2576 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2577 fDragState
= DRAG_NONE
;
2579 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2580 if (fDragX
== fDragX0
) { // click without selecting anything
2581 long time
= getTimeAtX(e
.x
);
2582 fTimeProvider
.setSelectedTimeNotify(time
, false);
2584 long time0
= fDragBeginMarker ?
getTimeAtX(fDragX0
) : fDragTime0
;
2585 long time1
= fDragBeginMarker ? fDragTime0
: getTimeAtX(fDragX
);
2586 fTimeProvider
.setSelectionRangeNotify(time0
, time1
, false);
2588 fDragState
= DRAG_NONE
;
2590 fTimeGraphScale
.setDragRange(-1, -1);
2591 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2592 int nameWidth
= fTimeProvider
.getNameSpace();
2593 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2594 long time0
= getTimeAtX(fDragX0
);
2595 long time1
= getTimeAtX(fDragX
);
2596 if (time0
< time1
) {
2597 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2599 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2604 fDragState
= DRAG_NONE
;
2605 fTimeGraphScale
.setDragRange(-1, -1);
2608 updateCursor(e
.x
, e
.stateMask
);
2609 updateStatusLine(e
.x
);
2613 public void mouseEnter(MouseEvent e
) {
2617 public void mouseExit(MouseEvent e
) {
2618 if (fMouseOverSplitLine
) {
2619 fMouseOverSplitLine
= false;
2622 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME
);
2626 public void mouseHover(MouseEvent e
) {
2630 public void mouseScrolled(MouseEvent e
) {
2631 if (fDragState
!= DRAG_NONE
) {
2634 boolean horizontalZoom
= false;
2635 boolean horizontalScroll
= false;
2636 boolean verticalZoom
= false;
2637 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2638 Point parentSize
= getParent().getSize();
2639 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2640 // over the parent control
2641 if (e
.x
> getSize().x
) {
2642 // over the vertical scroll bar
2643 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2644 verticalZoom
= true;
2646 } else if (e
.y
< 0) {
2647 // over the time scale
2648 horizontalZoom
= true;
2649 } else if (e
.y
>= getSize().y
) {
2650 // over the horizontal scroll bar
2651 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2652 horizontalZoom
= true;
2654 horizontalScroll
= true;
2657 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == (SWT
.SHIFT
| SWT
.CTRL
)) {
2658 verticalZoom
= true;
2659 } else if (e
.x
< fTimeProvider
.getNameSpace()) {
2660 // over the name space
2661 horizontalZoom
= false;
2663 // over the state area
2664 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
2665 // over the state area, CTRL pressed
2666 horizontalZoom
= true;
2668 // over the state area, CTRL not pressed
2669 horizontalZoom
= false;
2677 } else if (e
.count
< 0) {
2678 verticalZoom(false);
2680 } else if (horizontalZoom
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2683 } else if (e
.count
< 0) {
2686 } else if (horizontalScroll
) {
2687 horizontalScroll(e
.count
> 0);
2689 setTopIndex(getTopIndex() - e
.count
);
2694 public void handleEvent(Event event
) {
2695 if (event
.type
== SWT
.MouseWheel
) {
2696 // prevent horizontal scrolling when the mouse wheel is used to
2697 // scroll vertically or zoom
2703 public int getBorderWidth() {
2704 return fBorderWidth
;
2708 * Set the border width
2710 * @param borderWidth
2713 public void setBorderWidth(int borderWidth
) {
2714 this.fBorderWidth
= borderWidth
;
2718 * @return The current height of the header row
2720 public int getHeaderHeight() {
2721 return fHeaderHeight
;
2725 * Set the height of the header row
2727 * @param headerHeight
2730 public void setHeaderHeight(int headerHeight
) {
2731 this.fHeaderHeight
= headerHeight
;
2735 * @return The default height of regular item rows
2737 public int getItemHeight() {
2738 return fGlobalItemHeight
;
2742 * Set the default height of regular item rows.
2747 public void setItemHeight(int rowHeight
) {
2748 this.fGlobalItemHeight
= rowHeight
;
2749 for (Item item
: fItemData
.fItems
) {
2750 item
.fItemHeight
= rowHeight
;
2755 * Set the height of a specific item. Overrides the default item height.
2758 * A time graph entry
2761 * @return true if the height is successfully stored, false otherwise
2763 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2764 Item item
= fItemData
.findItem(entry
);
2766 item
.fItemHeight
= rowHeight
;
2773 * Set the minimum item width
2775 * @param width The minimum width
2777 public void setMinimumItemWidth(int width
) {
2778 this.fMinimumItemWidth
= width
;
2782 * @return The minimum item width
2784 public int getMinimumItemWidth() {
2785 return fMinimumItemWidth
;
2789 * Set whether all time events with a duration shorter than one pixel should
2790 * be blended in. If false, only the first such time event will be drawn and
2791 * the subsequent time events in the same pixel will be discarded. The
2792 * default value is false.
2795 * true if sub-pixel events should be blended, false otherwise.
2798 public void setBlendSubPixelEvents(boolean blend
) {
2799 fBlendSubPixelEvents
= blend
;
2803 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2804 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2805 fSelectionChangedListeners
.add(listener
);
2810 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2811 if (listener
!= null) {
2812 fSelectionChangedListeners
.remove(listener
);
2817 public void setSelection(ISelection selection
) {
2818 if (selection
instanceof TimeGraphSelection
) {
2819 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2820 Object ob
= sel
.getFirstElement();
2821 if (ob
instanceof ITimeGraphEntry
) {
2822 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2823 selectItem(trace
, false);
2830 * @param filter The filter object to be attached to the view
2832 public void addFilter(@NonNull ViewerFilter filter
) {
2833 if (!fFilters
.contains(filter
)) {
2834 fFilters
.add(filter
);
2839 * @param filter The filter object to be attached to the view
2841 public void removeFilter(@NonNull ViewerFilter filter
) {
2842 fFilters
.remove(filter
);
2846 * Returns this control's filters.
2848 * @return an array of viewer filters
2851 public @NonNull ViewerFilter
[] getFilters() {
2852 return Iterables
.toArray(fFilters
, ViewerFilter
.class);
2856 * Sets the filters, replacing any previous filters.
2859 * an array of viewer filters, or null
2862 public void setFilters(@NonNull ViewerFilter
[] filters
) {
2864 if (filters
!= null) {
2865 fFilters
.addAll(Arrays
.asList(filters
));
2870 public void colorSettingsChanged(StateItem
[] stateItems
) {
2871 /* Destroy previous colors from the resource manager */
2872 if (fEventColorMap
!= null) {
2873 for (Color color
: fEventColorMap
) {
2874 fResourceManager
.destroyColor(color
.getRGB());
2877 if (stateItems
!= null) {
2878 fEventColorMap
= new Color
[stateItems
.length
];
2879 for (int i
= 0; i
< stateItems
.length
; i
++) {
2880 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
2883 fEventColorMap
= new Color
[] { };
2888 private class ItemData
{
2889 private Map
<ITimeGraphEntry
, Item
> fItemMap
= new LinkedHashMap
<>();
2890 private Item
[] fExpandedItems
= new Item
[0];
2891 private Item
[] fItems
= new Item
[0];
2892 private ITimeGraphEntry fRootEntries
[] = new ITimeGraphEntry
[0];
2893 private List
<ILinkEvent
> fLinks
= new ArrayList
<>();
2898 public Item
findItem(ITimeGraphEntry entry
) {
2899 return fItemMap
.get(entry
);
2902 public int findItemIndex(ITimeGraphEntry entry
) {
2903 Item item
= fItemMap
.get(entry
);
2907 return item
.fExpandedIndex
;
2910 public void refreshData() {
2911 ITimeGraphEntry selection
= getSelectedTrace();
2912 Map
<ITimeGraphEntry
, Item
> itemMap
= new LinkedHashMap
<>();
2913 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2914 ITimeGraphEntry entry
= fRootEntries
[i
];
2915 refreshData(itemMap
, null, 0, entry
);
2918 fItems
= fItemMap
.values().toArray(new Item
[0]);
2919 updateExpandedItems();
2920 if (selection
!= null) {
2921 for (Item item
: fExpandedItems
) {
2922 if (item
.fEntry
== selection
) {
2923 item
.fSelected
= true;
2930 private void refreshData(Map
<ITimeGraphEntry
, Item
> itemMap
, Item parent
, int level
, ITimeGraphEntry entry
) {
2931 Item item
= new Item(entry
, entry
.getName(), level
);
2932 if (parent
!= null) {
2933 parent
.fChildren
.add(item
);
2935 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2936 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2938 item
.fItemHeight
= fGlobalItemHeight
;
2940 item
.fItemHeight
= Math
.max(1, item
.fItemHeight
+ fHeightAdjustment
);
2941 itemMap
.put(entry
, item
);
2942 if (entry
.hasChildren()) {
2943 Item oldItem
= fItemMap
.get(entry
);
2944 if (oldItem
!= null && oldItem
.fHasChildren
&& level
== oldItem
.fLevel
&& entry
.getParent() == oldItem
.fEntry
.getParent()) {
2945 /* existing items keep their old expanded state */
2946 item
.fExpanded
= oldItem
.fExpanded
;
2948 /* new items set the expanded state according to auto-expand level */
2949 item
.fExpanded
= fAutoExpandLevel
== ALL_LEVELS
|| level
< fAutoExpandLevel
;
2951 item
.fHasChildren
= true;
2952 for (ITimeGraphEntry child
: entry
.getChildren()) {
2953 refreshData(itemMap
, item
, level
+ 1, child
);
2958 public void updateExpandedItems() {
2959 for (Item item
: fItems
) {
2960 item
.fExpandedIndex
= -1;
2962 List
<Item
> expandedItemList
= new ArrayList
<>();
2963 for (int i
= 0; i
< fRootEntries
.length
; i
++) {
2964 ITimeGraphEntry entry
= fRootEntries
[i
];
2965 Item item
= findItem(entry
);
2966 refreshExpanded(expandedItemList
, item
);
2968 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2969 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2972 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2973 // Check for filters
2974 boolean display
= true;
2975 for (ViewerFilter filter
: fFilters
) {
2976 if (!filter
.select(null, item
.fEntry
.getParent(), item
.fEntry
)) {
2982 item
.fExpandedIndex
= expandedItemList
.size();
2983 expandedItemList
.add(item
);
2984 if (item
.fHasChildren
&& item
.fExpanded
) {
2985 for (Item child
: item
.fChildren
) {
2986 refreshExpanded(expandedItemList
, child
);
2992 public void refreshData(ITimeGraphEntry
[] entries
) {
2993 if (entries
== null) {
2994 fRootEntries
= null;
2996 fRootEntries
= Arrays
.copyOf(entries
, entries
.length
);
3002 public void refreshArrows(List
<ILinkEvent
> events
) {
3003 /* If links are null, reset the list */
3004 if (events
!= null) {
3007 fLinks
= new ArrayList
<>();
3011 public ITimeGraphEntry
[] getEntries() {
3012 return fRootEntries
;
3016 private class Item
{
3017 private boolean fExpanded
;
3018 private int fExpandedIndex
;
3019 private boolean fSelected
;
3020 private boolean fHasChildren
;
3021 private int fItemHeight
;
3022 private final int fLevel
;
3023 private final List
<Item
> fChildren
;
3024 private final String fName
;
3025 private final ITimeGraphEntry fEntry
;
3027 public Item(ITimeGraphEntry entry
, String name
, int level
) {
3028 this.fEntry
= entry
;
3030 this.fLevel
= level
;
3031 this.fChildren
= new ArrayList
<>();
3035 public String
toString() {
3041 public void menuDetected(MenuDetectEvent e
) {
3042 if (null == fTimeProvider
) {
3045 if (e
.detail
== SWT
.MENU_MOUSE
) {
3046 if (fPendingMenuDetectEvent
== null) {
3047 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3048 * Store the event and trigger it later just before handling mouseUp.
3049 * This allows for the method to detect if mouse is used to drag zoom.
3051 fPendingMenuDetectEvent
= e
;
3054 fPendingMenuDetectEvent
= null;
3055 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
3059 if (fDragState
!= DRAG_NONE
) {
3063 Point p
= toControl(e
.x
, e
.y
);
3064 int idx
= getItemIndexAtY(p
.y
);
3065 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
3066 Item item
= fItemData
.fExpandedItems
[idx
];
3067 ITimeGraphEntry entry
= item
.fEntry
;
3068 if (entry
.hasTimeEvents()) {
3069 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
3070 if (event
!= null) {
3072 fireMenuEventOnTimeEvent(e
);
3077 fireMenuEventOnTimeGraphEntry(e
);
3082 * Perform the alignment operation.
3085 * the alignment offset
3087 * @see ITmfTimeAligned
3091 public void performAlign(int offset
) {
3092 fTimeProvider
.setNameSpace(offset
);
3096 * Return the time alignment information
3098 * @return the time alignment information
3100 * @see ITmfTimeAligned
3104 public TmfTimeViewAlignmentInfo
getTimeViewAlignmentInfo() {
3105 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider
.getNameSpace());