Commit | Line | Data |
---|---|---|
837a2f8c | 1 | /***************************************************************************** |
95d225e0 | 2 | * Copyright (c) 2007, 2017 Intel Corporation and others |
4999a196 | 3 | * |
837a2f8c PT |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Intel Corporation - Initial API and implementation | |
11 | * Ruslan A. Scherbakov, Intel - Initial API and implementation | |
4999a196 GB |
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 | |
bec1f1ac | 16 | * Add display of links between items |
351a2391 | 17 | * Xavier Raynaud, Kalray - Code optimization |
f2ca0f69 | 18 | * Generoso Pagano, Inria - Support for drag selection listeners |
837a2f8c PT |
19 | *****************************************************************************/ |
20 | ||
2bdf0193 | 21 | package org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets; |
837a2f8c | 22 | |
48072ae3 | 23 | import java.util.AbstractMap.SimpleEntry; |
837a2f8c | 24 | import java.util.ArrayList; |
f1fae91f | 25 | import java.util.Arrays; |
2fe6a9ea | 26 | import java.util.Collections; |
83d0971d | 27 | import java.util.HashMap; |
837a2f8c | 28 | import java.util.Iterator; |
70e10acc | 29 | import java.util.LinkedHashMap; |
8b516722 | 30 | import java.util.LinkedList; |
837a2f8c | 31 | import java.util.List; |
70e10acc | 32 | import java.util.Map; |
48072ae3 | 33 | import java.util.Map.Entry; |
8b516722 | 34 | import java.util.Queue; |
837a2f8c | 35 | |
367e2932 | 36 | import org.eclipse.jdt.annotation.NonNull; |
0fcf3b09 | 37 | import org.eclipse.jface.action.IStatusLineManager; |
837a2f8c PT |
38 | import org.eclipse.jface.resource.JFaceResources; |
39 | import org.eclipse.jface.resource.LocalResourceManager; | |
f4617471 | 40 | import org.eclipse.jface.viewers.AbstractTreeViewer; |
837a2f8c PT |
41 | import org.eclipse.jface.viewers.ISelection; |
42 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
43 | import org.eclipse.jface.viewers.ISelectionProvider; | |
07cd9368 | 44 | import org.eclipse.jface.viewers.IStructuredSelection; |
26cf0ea8 | 45 | import org.eclipse.jface.viewers.ITableLabelProvider; |
ad4415e1 | 46 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
07cd9368 | 47 | import org.eclipse.jface.viewers.StructuredSelection; |
6ac5a950 | 48 | import org.eclipse.jface.viewers.ViewerFilter; |
0fab12b0 | 49 | import org.eclipse.osgi.util.NLS; |
837a2f8c | 50 | import org.eclipse.swt.SWT; |
26cf0ea8 PT |
51 | import org.eclipse.swt.events.ControlEvent; |
52 | import org.eclipse.swt.events.ControlListener; | |
837a2f8c PT |
53 | import org.eclipse.swt.events.FocusEvent; |
54 | import org.eclipse.swt.events.FocusListener; | |
55 | import org.eclipse.swt.events.KeyEvent; | |
56 | import org.eclipse.swt.events.KeyListener; | |
27df1564 XR |
57 | import org.eclipse.swt.events.MenuDetectEvent; |
58 | import org.eclipse.swt.events.MenuDetectListener; | |
837a2f8c PT |
59 | import org.eclipse.swt.events.MouseEvent; |
60 | import org.eclipse.swt.events.MouseListener; | |
61 | import org.eclipse.swt.events.MouseMoveListener; | |
62 | import org.eclipse.swt.events.MouseTrackListener; | |
63 | import org.eclipse.swt.events.MouseWheelListener; | |
64 | import org.eclipse.swt.events.PaintEvent; | |
837a2f8c PT |
65 | import org.eclipse.swt.events.SelectionListener; |
66 | import org.eclipse.swt.events.TraverseEvent; | |
67 | import org.eclipse.swt.events.TraverseListener; | |
27df1564 | 68 | import org.eclipse.swt.events.TypedEvent; |
837a2f8c PT |
69 | import org.eclipse.swt.graphics.Color; |
70 | import org.eclipse.swt.graphics.Cursor; | |
3bd20aa6 PT |
71 | import org.eclipse.swt.graphics.Font; |
72 | import org.eclipse.swt.graphics.FontData; | |
837a2f8c PT |
73 | import org.eclipse.swt.graphics.GC; |
74 | import org.eclipse.swt.graphics.Image; | |
75 | import org.eclipse.swt.graphics.Point; | |
76 | import org.eclipse.swt.graphics.Rectangle; | |
77 | import org.eclipse.swt.widgets.Composite; | |
78 | import org.eclipse.swt.widgets.Display; | |
79 | import org.eclipse.swt.widgets.Event; | |
80 | import org.eclipse.swt.widgets.Listener; | |
024658d1 | 81 | import org.eclipse.swt.widgets.Menu; |
26cf0ea8 PT |
82 | import org.eclipse.swt.widgets.Tree; |
83 | import org.eclipse.swt.widgets.TreeColumn; | |
a23cd365 | 84 | import org.eclipse.tracecompass.common.core.log.TraceCompassLog; |
1d46761c | 85 | import org.eclipse.tracecompass.common.core.math.SaturatedArithmetic; |
d2e4afa7 MAL |
86 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
87 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; | |
88 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; | |
89 | import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned; | |
2bdf0193 AM |
90 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphColorListener; |
91 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; | |
92 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2; | |
93 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener; | |
94 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTreeListener; | |
95 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; | |
96 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent; | |
97 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent; | |
98 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent; | |
56b43ab2 | 99 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.IMarkerEvent; |
2bdf0193 | 100 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
a23cd365 | 101 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEventStyleStrings; |
2bdf0193 AM |
102 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; |
103 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; | |
104 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; | |
837a2f8c | 105 | |
4923d7b9 PT |
106 | import com.google.common.collect.Iterables; |
107 | ||
837a2f8c PT |
108 | /** |
109 | * Time graph control implementation | |
110 | * | |
837a2f8c PT |
111 | * @author Alvaro Sanchez-Leon |
112 | * @author Patrick Tasse | |
113 | */ | |
496f76d3 | 114 | public class TimeGraphControl extends TimeGraphBaseControl |
b698ec63 PT |
115 | implements FocusListener, KeyListener, MouseMoveListener, MouseListener, |
116 | MouseWheelListener, MouseTrackListener, TraverseListener, ISelectionProvider, | |
6b11be52 | 117 | MenuDetectListener, ITmfTimeGraphDrawingHelper, ITimeGraphColorListener, Listener { |
f1fae91f | 118 | |
a23cd365 MK |
119 | /** |
120 | * Constant indicating that all levels of the time graph should be expanded | |
121 | */ | |
f4617471 PT |
122 | public static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS; |
123 | ||
e6436e5b BH |
124 | private static final int DRAG_MARGIN = 5; |
125 | ||
837a2f8c PT |
126 | private static final int DRAG_NONE = 0; |
127 | private static final int DRAG_TRACE_ITEM = 1; | |
128 | private static final int DRAG_SPLIT_LINE = 2; | |
5b2b9bd7 | 129 | private static final int DRAG_ZOOM = 3; |
0fcf3b09 | 130 | private static final int DRAG_SELECTION = 4; |
a0a88f65 | 131 | |
a23cd365 MK |
132 | /** |
133 | * Get item height from provider | |
134 | */ | |
135 | private static final int CUSTOM_ITEM_HEIGHT = -1; | |
837a2f8c | 136 | |
f1fae91f PT |
137 | private static final double ZOOM_FACTOR = 1.5; |
138 | private static final double ZOOM_IN_FACTOR = 0.8; | |
139 | private static final double ZOOM_OUT_FACTOR = 1.25; | |
140 | ||
26cf0ea8 | 141 | private static final int SNAP_WIDTH = 3; |
1053eaa6 | 142 | private static final int ARROW_HOVER_MAX_DIST = 5; |
0fcf3b09 | 143 | |
a23cd365 MK |
144 | /** |
145 | * base to height ratio | |
146 | */ | |
147 | private static final double ARROW_RATIO = Math.sqrt(3) / 2; | |
0fcf3b09 | 148 | private static final int NO_STATUS = -1; |
33fa1fc7 | 149 | private static final int STATUS_WITHOUT_CURSOR_TIME = -2; |
0fcf3b09 | 150 | |
1d012443 PT |
151 | private static final int MAX_LABEL_LENGTH = 256; |
152 | ||
a23cd365 MK |
153 | /** points per inch */ |
154 | private static final int PPI = 72; | |
83d0971d PT |
155 | private static final int DPI = Display.getDefault().getDPI().y; |
156 | ||
48072ae3 PT |
157 | private static final int VERTICAL_ZOOM_DELAY = 400; |
158 | ||
26cf0ea8 PT |
159 | private static final String PREFERRED_WIDTH = "width"; //$NON-NLS-1$ |
160 | ||
f1fae91f PT |
161 | /** Resource manager */ |
162 | private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); | |
163 | ||
164 | /** Color map for event types */ | |
165 | private Color[] fEventColorMap = null; | |
166 | ||
167 | private ITimeDataProvider fTimeProvider; | |
26cf0ea8 | 168 | private ITableLabelProvider fLabelProvider; |
0fcf3b09 | 169 | private IStatusLineManager fStatusLineManager = null; |
26cf0ea8 | 170 | private Tree fTree = null; |
0fcf3b09 PT |
171 | private TimeGraphScale fTimeGraphScale = null; |
172 | ||
f1fae91f | 173 | private boolean fIsInFocus = false; |
f1fae91f PT |
174 | private boolean fMouseOverSplitLine = false; |
175 | private int fGlobalItemHeight = CUSTOM_ITEM_HEIGHT; | |
3bd20aa6 | 176 | private int fHeightAdjustment = 0; |
95d225e0 | 177 | private int fMaxItemHeight = 0; |
83d0971d | 178 | private Map<Integer, Font> fFonts = new HashMap<>(); |
b052914f | 179 | private boolean fBlendSubPixelEvents = false; |
f1fae91f PT |
180 | private int fMinimumItemWidth = 0; |
181 | private int fTopIndex = 0; | |
182 | private int fDragState = DRAG_NONE; | |
85203d74 | 183 | private boolean fDragBeginMarker = false; |
0fcf3b09 | 184 | private int fDragButton; |
f1fae91f PT |
185 | private int fDragX0 = 0; |
186 | private int fDragX = 0; | |
8b516722 | 187 | private boolean fHasNamespaceFocus = false; |
a23cd365 MK |
188 | /** |
189 | * Used to preserve accuracy of modified selection | |
190 | */ | |
191 | private long fDragTime0 = 0; | |
f1fae91f | 192 | private int fIdealNameSpace = 0; |
26cf0ea8 | 193 | private boolean fAutoResizeColumns = true; |
f1fae91f PT |
194 | private long fTime0bak; |
195 | private long fTime1bak; | |
837a2f8c | 196 | private ITimeGraphPresentationProvider fTimeGraphProvider = null; |
f1fae91f | 197 | private ItemData fItemData = null; |
56b43ab2 PT |
198 | private List<IMarkerEvent> fMarkers = null; |
199 | private boolean fMarkersVisible = true; | |
f1fae91f | 200 | private List<SelectionListener> fSelectionListeners; |
f2ca0f69 | 201 | private List<ITimeGraphTimeListener> fDragSelectionListeners; |
507b1336 AM |
202 | private final List<ISelectionChangedListener> fSelectionChangedListeners = new ArrayList<>(); |
203 | private final List<ITimeGraphTreeListener> fTreeListeners = new ArrayList<>(); | |
204 | private final List<MenuDetectListener> fTimeGraphEntryMenuListeners = new ArrayList<>(); | |
205 | private final List<MenuDetectListener> fTimeEventMenuListeners = new ArrayList<>(); | |
0fcf3b09 PT |
206 | private final Cursor fDragCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND); |
207 | private final Cursor fResizeCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_IBEAM); | |
208 | private final Cursor fWaitCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT); | |
209 | private final Cursor fZoomCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEWE); | |
367e2932 | 210 | private final List<@NonNull ViewerFilter> fFilters = new ArrayList<>(); |
0b5a90a0 | 211 | private MenuDetectEvent fPendingMenuDetectEvent = null; |
55d1fc96 PT |
212 | private boolean fGridLinesVisible = true; |
213 | private Color fGridLineColor = Display.getDefault().getSystemColor(SWT.COLOR_GRAY); | |
79ec0b89 | 214 | private boolean fHideArrows = false; |
f4617471 | 215 | private int fAutoExpandLevel = ALL_LEVELS; |
48072ae3 PT |
216 | private Entry<ITimeGraphEntry, Integer> fVerticalZoomAlignEntry = null; |
217 | private long fVerticalZoomAlignTime = 0; | |
f1fae91f PT |
218 | private int fBorderWidth = 0; |
219 | private int fHeaderHeight = 0; | |
837a2f8c | 220 | |
a23cd365 MK |
221 | private boolean fFirstHeightWarning = true; |
222 | ||
837a2f8c PT |
223 | /** |
224 | * Standard constructor | |
225 | * | |
226 | * @param parent | |
227 | * The parent composite object | |
228 | * @param colors | |
229 | * The color scheme to use | |
230 | */ | |
231 | public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) { | |
232 | ||
b698ec63 | 233 | super(parent, colors, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED); |
837a2f8c | 234 | |
f1fae91f | 235 | fItemData = new ItemData(); |
837a2f8c PT |
236 | |
237 | addFocusListener(this); | |
238 | addMouseListener(this); | |
239 | addMouseMoveListener(this); | |
240 | addMouseTrackListener(this); | |
241 | addMouseWheelListener(this); | |
242 | addTraverseListener(this); | |
243 | addKeyListener(this); | |
27df1564 | 244 | addMenuDetectListener(this); |
6b11be52 | 245 | addListener(SWT.MouseWheel, this); |
25033fef PT |
246 | addDisposeListener((e) -> { |
247 | fResourceManager.dispose(); | |
248 | for (Font font : fFonts.values()) { | |
249 | font.dispose(); | |
250 | } | |
251 | }); | |
837a2f8c PT |
252 | } |
253 | ||
254 | /** | |
255 | * Sets the timegraph provider used by this timegraph viewer. | |
256 | * | |
a23cd365 MK |
257 | * @param timeGraphProvider |
258 | * the timegraph provider | |
837a2f8c PT |
259 | */ |
260 | public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) { | |
261 | fTimeGraphProvider = timeGraphProvider; | |
837a2f8c | 262 | |
4999a196 GB |
263 | if (timeGraphProvider instanceof ITimeGraphPresentationProvider2) { |
264 | ((ITimeGraphPresentationProvider2) timeGraphProvider).setDrawingHelper(this); | |
496f76d3 | 265 | ((ITimeGraphPresentationProvider2) timeGraphProvider).addColorListener(this); |
4999a196 GB |
266 | } |
267 | ||
837a2f8c | 268 | StateItem[] stateItems = fTimeGraphProvider.getStateTable(); |
496f76d3 | 269 | colorSettingsChanged(stateItems); |
837a2f8c PT |
270 | } |
271 | ||
5a66cf9c XR |
272 | /** |
273 | * Gets the timegraph provider used by this timegraph viewer. | |
274 | * | |
275 | * @return the timegraph provider, or <code>null</code> if not set. | |
5a66cf9c XR |
276 | */ |
277 | public ITimeGraphPresentationProvider getTimeGraphProvider() { | |
278 | return fTimeGraphProvider; | |
279 | } | |
280 | ||
fb023ce6 AM |
281 | /** |
282 | * Gets the time data provider used by this viewer. | |
283 | * | |
284 | * @return The time data provider, or <code>null</code> if not set | |
285 | * @since 2.1 | |
286 | */ | |
287 | public ITimeDataProvider getTimeDataProvider() { | |
288 | return fTimeProvider; | |
289 | } | |
290 | ||
5a66cf9c XR |
291 | /** |
292 | * Gets the color map used by this timegraph viewer. | |
293 | * | |
294 | * @return a color map, or <code>null</code> if not set. | |
5a66cf9c XR |
295 | */ |
296 | public Color[] getEventColorMap() { | |
297 | return fEventColorMap; | |
298 | } | |
299 | ||
837a2f8c PT |
300 | /** |
301 | * Assign the given time provider | |
302 | * | |
303 | * @param timeProvider | |
304 | * The time provider | |
305 | */ | |
306 | public void setTimeProvider(ITimeDataProvider timeProvider) { | |
f1fae91f | 307 | fTimeProvider = timeProvider; |
837a2f8c PT |
308 | redraw(); |
309 | } | |
310 | ||
26cf0ea8 PT |
311 | /** |
312 | * Set the label provider for the name space | |
313 | * | |
314 | * @param labelProvider | |
315 | * The label provider | |
316 | * @since 2.3 | |
317 | */ | |
318 | public void setLabelProvider(ITableLabelProvider labelProvider) { | |
319 | fLabelProvider = labelProvider; | |
320 | redraw(); | |
321 | } | |
322 | ||
323 | /** | |
324 | * Get the label provider for the name space | |
325 | * | |
326 | * @return The label provider | |
327 | * @since 2.3 | |
328 | */ | |
329 | public ITableLabelProvider getLabelProvider() { | |
330 | return fLabelProvider; | |
331 | } | |
332 | ||
0fcf3b09 PT |
333 | /** |
334 | * Assign the status line manager | |
335 | * | |
336 | * @param statusLineManager | |
a23cd365 MK |
337 | * The status line manager, or null to disable status line |
338 | * messages | |
0fcf3b09 PT |
339 | */ |
340 | public void setStatusLineManager(IStatusLineManager statusLineManager) { | |
341 | if (fStatusLineManager != null && statusLineManager == null) { | |
342 | fStatusLineManager.setMessage(""); //$NON-NLS-1$ | |
343 | } | |
344 | fStatusLineManager = statusLineManager; | |
345 | } | |
346 | ||
26cf0ea8 PT |
347 | /** |
348 | * Assign the tree that represents the name space header | |
349 | * | |
350 | * @param tree | |
351 | * The tree | |
352 | * @since 2.3 | |
353 | */ | |
354 | public void setTree(Tree tree) { | |
355 | fTree = tree; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Returns the tree control associated with this time graph control. The | |
360 | * tree is only used for column handling of the name space and contains no | |
361 | * tree items. | |
362 | * | |
363 | * @return the tree control | |
364 | * @since 2.3 | |
365 | */ | |
366 | public Tree getTree() { | |
367 | return fTree; | |
368 | } | |
369 | ||
370 | /** | |
371 | * Sets the columns for this time graph control's name space. | |
372 | * | |
373 | * @param columnNames | |
374 | * the column names | |
375 | * @since 2.3 | |
376 | */ | |
377 | public void setColumns(String[] columnNames) { | |
378 | for (TreeColumn column : fTree.getColumns()) { | |
379 | column.dispose(); | |
380 | } | |
381 | ControlListener controlListener = new ControlListener() { | |
382 | @Override | |
383 | public void controlResized(ControlEvent e) { | |
384 | if (fAutoResizeColumns && ((TreeColumn) e.widget).getWidth() < (Integer) e.widget.getData(PREFERRED_WIDTH)) { | |
385 | fAutoResizeColumns = false; | |
386 | } | |
387 | redraw(); | |
388 | } | |
a23cd365 | 389 | |
26cf0ea8 PT |
390 | @Override |
391 | public void controlMoved(ControlEvent e) { | |
392 | redraw(); | |
393 | } | |
394 | }; | |
395 | for (String columnName : columnNames) { | |
396 | TreeColumn column = new TreeColumn(fTree, SWT.LEFT); | |
397 | column.setMoveable(true); | |
398 | column.setText(columnName); | |
399 | column.pack(); | |
400 | column.setData(PREFERRED_WIDTH, column.getWidth()); | |
401 | column.addControlListener(controlListener); | |
402 | } | |
403 | } | |
404 | ||
0fcf3b09 PT |
405 | /** |
406 | * Assign the time graph scale | |
407 | * | |
408 | * @param timeGraphScale | |
409 | * The time graph scale | |
0fcf3b09 PT |
410 | */ |
411 | public void setTimeGraphScale(TimeGraphScale timeGraphScale) { | |
412 | fTimeGraphScale = timeGraphScale; | |
413 | } | |
414 | ||
837a2f8c PT |
415 | /** |
416 | * Add a selection listener | |
417 | * | |
418 | * @param listener | |
419 | * The listener to add | |
420 | */ | |
421 | public void addSelectionListener(SelectionListener listener) { | |
422 | if (listener == null) { | |
423 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
424 | } | |
f1fae91f | 425 | if (null == fSelectionListeners) { |
507b1336 | 426 | fSelectionListeners = new ArrayList<>(); |
837a2f8c | 427 | } |
f1fae91f | 428 | fSelectionListeners.add(listener); |
837a2f8c PT |
429 | } |
430 | ||
431 | /** | |
432 | * Remove a selection listener | |
433 | * | |
434 | * @param listener | |
435 | * The listener to remove | |
436 | */ | |
437 | public void removeSelectionListener(SelectionListener listener) { | |
f1fae91f PT |
438 | if (null != fSelectionListeners) { |
439 | fSelectionListeners.remove(listener); | |
837a2f8c PT |
440 | } |
441 | } | |
442 | ||
443 | /** | |
444 | * Selection changed callback | |
445 | */ | |
446 | public void fireSelectionChanged() { | |
f1fae91f PT |
447 | if (null != fSelectionListeners) { |
448 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
449 | while (it.hasNext()) { |
450 | SelectionListener listener = it.next(); | |
451 | listener.widgetSelected(null); | |
452 | } | |
453 | } | |
ad4415e1 BH |
454 | |
455 | if (null != fSelectionChangedListeners) { | |
456 | for (ISelectionChangedListener listener : fSelectionChangedListeners) { | |
457 | listener.selectionChanged(new SelectionChangedEvent(this, getSelection())); | |
458 | } | |
459 | } | |
837a2f8c PT |
460 | } |
461 | ||
462 | /** | |
463 | * Default selection callback | |
464 | */ | |
465 | public void fireDefaultSelection() { | |
f1fae91f PT |
466 | if (null != fSelectionListeners) { |
467 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
468 | while (it.hasNext()) { |
469 | SelectionListener listener = it.next(); | |
470 | listener.widgetDefaultSelected(null); | |
471 | } | |
472 | } | |
473 | } | |
474 | ||
f2ca0f69 GP |
475 | /** |
476 | * Add a drag selection listener | |
477 | * | |
478 | * @param listener | |
479 | * The listener to add | |
f2ca0f69 GP |
480 | */ |
481 | public void addDragSelectionListener(ITimeGraphTimeListener listener) { | |
482 | if (listener == null) { | |
483 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
484 | } | |
485 | if (null == fDragSelectionListeners) { | |
486 | fDragSelectionListeners = new ArrayList<>(); | |
487 | } | |
488 | fDragSelectionListeners.add(listener); | |
489 | } | |
490 | ||
491 | /** | |
492 | * Remove a drag selection listener | |
493 | * | |
494 | * @param listener | |
495 | * The listener to remove | |
f2ca0f69 GP |
496 | */ |
497 | public void removeDragSelectionListener(ITimeGraphTimeListener listener) { | |
498 | if (null != fDragSelectionListeners) { | |
499 | fDragSelectionListeners.remove(listener); | |
500 | } | |
501 | } | |
502 | ||
503 | /** | |
504 | * Drag Selection changed callback | |
505 | * | |
506 | * @param start | |
507 | * Time interval start | |
508 | * @param end | |
509 | * Time interval end | |
f2ca0f69 GP |
510 | */ |
511 | public void fireDragSelectionChanged(long start, long end) { | |
512 | // check for backward intervals | |
513 | long beginTime, endTime; | |
514 | if (start > end) { | |
515 | beginTime = end; | |
516 | endTime = start; | |
517 | } else { | |
518 | beginTime = start; | |
519 | endTime = end; | |
520 | } | |
521 | // call the listeners | |
522 | if (null != fDragSelectionListeners) { | |
523 | Iterator<ITimeGraphTimeListener> it = fDragSelectionListeners.iterator(); | |
524 | while (it.hasNext()) { | |
525 | ITimeGraphTimeListener listener = it.next(); | |
526 | listener.timeSelected(new TimeGraphTimeEvent(this, beginTime, endTime)); | |
527 | } | |
528 | } | |
529 | } | |
530 | ||
837a2f8c PT |
531 | /** |
532 | * Get the traces in the model | |
533 | * | |
534 | * @return The array of traces | |
535 | */ | |
536 | public ITimeGraphEntry[] getTraces() { | |
70e10acc | 537 | return fItemData.getEntries(); |
837a2f8c PT |
538 | } |
539 | ||
837a2f8c PT |
540 | /** |
541 | * Refresh the data for the thing | |
542 | */ | |
543 | public void refreshData() { | |
f1fae91f | 544 | fItemData.refreshData(); |
837a2f8c PT |
545 | redraw(); |
546 | } | |
547 | ||
548 | /** | |
549 | * Refresh data for the given traces | |
550 | * | |
551 | * @param traces | |
552 | * The traces to refresh | |
553 | */ | |
554 | public void refreshData(ITimeGraphEntry[] traces) { | |
f1fae91f | 555 | fItemData.refreshData(traces); |
837a2f8c PT |
556 | redraw(); |
557 | } | |
558 | ||
bec1f1ac GB |
559 | /** |
560 | * Refresh the links (arrows) of this widget | |
561 | * | |
a23cd365 MK |
562 | * @param events |
563 | * The link event list | |
bec1f1ac GB |
564 | */ |
565 | public void refreshArrows(List<ILinkEvent> events) { | |
566 | fItemData.refreshArrows(events); | |
567 | } | |
568 | ||
2fe6a9ea PT |
569 | /** |
570 | * Get the links (arrows) of this widget | |
571 | * | |
572 | * @return The unmodifiable link event list | |
573 | * | |
0336f981 | 574 | * @since 1.1 |
2fe6a9ea PT |
575 | */ |
576 | public List<ILinkEvent> getArrows() { | |
577 | return Collections.unmodifiableList(fItemData.fLinks); | |
578 | } | |
579 | ||
837a2f8c PT |
580 | boolean ensureVisibleItem(int idx, boolean redraw) { |
581 | boolean changed = false; | |
41b5c37f AM |
582 | int index = idx; |
583 | if (index < 0) { | |
f1fae91f PT |
584 | for (index = 0; index < fItemData.fExpandedItems.length; index++) { |
585 | if (fItemData.fExpandedItems[index].fSelected) { | |
837a2f8c PT |
586 | break; |
587 | } | |
588 | } | |
589 | } | |
f1fae91f | 590 | if (index >= fItemData.fExpandedItems.length) { |
837a2f8c PT |
591 | return changed; |
592 | } | |
f1fae91f | 593 | if (index < fTopIndex) { |
41b5c37f | 594 | setTopIndex(index); |
837a2f8c PT |
595 | if (redraw) { |
596 | redraw(); | |
597 | } | |
598 | changed = true; | |
599 | } else { | |
600 | int page = countPerPage(); | |
f1fae91f | 601 | if (index >= fTopIndex + page) { |
41b5c37f | 602 | setTopIndex(index - page + 1); |
837a2f8c PT |
603 | if (redraw) { |
604 | redraw(); | |
605 | } | |
606 | changed = true; | |
607 | } | |
608 | } | |
609 | return changed; | |
610 | } | |
611 | ||
612 | /** | |
613 | * Assign the given index as the top one | |
614 | * | |
615 | * @param idx | |
616 | * The index | |
617 | */ | |
618 | public void setTopIndex(int idx) { | |
f1fae91f | 619 | int index = Math.min(idx, fItemData.fExpandedItems.length - countPerPage()); |
a23cd365 | 620 | index = Math.max(0, index); |
f1fae91f | 621 | fTopIndex = index; |
837a2f8c PT |
622 | redraw(); |
623 | } | |
624 | ||
48072ae3 PT |
625 | /** |
626 | * Set the top index so that the requested element is at the specified | |
627 | * position. | |
628 | * | |
629 | * @param entry | |
630 | * the time graph entry to be positioned | |
631 | * @param y | |
632 | * the requested y-coordinate | |
633 | * @since 2.0 | |
634 | */ | |
635 | public void setElementPosition(ITimeGraphEntry entry, int y) { | |
636 | Item item = fItemData.fItemMap.get(entry); | |
637 | if (item == null || item.fExpandedIndex == -1) { | |
638 | return; | |
639 | } | |
640 | int index = item.fExpandedIndex; | |
641 | Rectangle itemRect = getItemRect(getClientArea(), index); | |
642 | int delta = itemRect.y + itemRect.height - y; | |
643 | int topIndex = getItemIndexAtY(delta); | |
644 | if (topIndex != -1) { | |
645 | setTopIndex(topIndex); | |
646 | } else { | |
647 | if (delta < 0) { | |
648 | setTopIndex(0); | |
649 | } else { | |
650 | setTopIndex(getExpandedElementCount()); | |
651 | } | |
652 | } | |
653 | } | |
654 | ||
f4617471 | 655 | /** |
df0e3d5f PT |
656 | * Sets the auto-expand level to be used for new entries discovered when |
657 | * calling {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])} | |
658 | * . The value 0 means that there is no auto-expand; 1 means that top-level | |
f4617471 PT |
659 | * entries are expanded, but not their children; 2 means that top-level |
660 | * entries are expanded, and their children, but not grand-children; and so | |
661 | * on. | |
662 | * <p> | |
663 | * The value {@link #ALL_LEVELS} means that all subtrees should be expanded. | |
664 | * </p> | |
df0e3d5f | 665 | * |
f4617471 PT |
666 | * @param level |
667 | * non-negative level, or <code>ALL_LEVELS</code> to expand all | |
668 | * levels of the tree | |
f4617471 PT |
669 | */ |
670 | public void setAutoExpandLevel(int level) { | |
671 | fAutoExpandLevel = level; | |
672 | } | |
673 | ||
674 | /** | |
675 | * Returns the auto-expand level. | |
676 | * | |
677 | * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of | |
678 | * the tree are expanded automatically | |
679 | * @see #setAutoExpandLevel | |
f4617471 PT |
680 | */ |
681 | public int getAutoExpandLevel() { | |
682 | return fAutoExpandLevel; | |
683 | } | |
684 | ||
df0e3d5f PT |
685 | /** |
686 | * Get the expanded state of a given entry. | |
687 | * | |
688 | * @param entry | |
689 | * The entry | |
690 | * @return true if the entry is expanded, false if collapsed | |
0336f981 | 691 | * @since 1.1 |
df0e3d5f PT |
692 | */ |
693 | public boolean getExpandedState(ITimeGraphEntry entry) { | |
694 | Item item = fItemData.fItemMap.get(entry); | |
695 | return (item != null ? item.fExpanded : false); | |
696 | } | |
697 | ||
837a2f8c PT |
698 | /** |
699 | * Set the expanded state of a given entry | |
700 | * | |
701 | * @param entry | |
702 | * The entry | |
703 | * @param expanded | |
704 | * True if expanded, false if collapsed | |
705 | */ | |
706 | public void setExpandedState(ITimeGraphEntry entry, boolean expanded) { | |
f1fae91f PT |
707 | Item item = fItemData.findItem(entry); |
708 | if (item != null && item.fExpanded != expanded) { | |
709 | item.fExpanded = expanded; | |
710 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
711 | redraw(); |
712 | } | |
713 | } | |
714 | ||
8b516722 | 715 | /** |
a23cd365 MK |
716 | * Set the expanded state of a given entry to certain relative level. It |
717 | * will call fireTreeEvent() for each changed entry. At the end it will call | |
718 | * redraw(). | |
8b516722 BH |
719 | * |
720 | * @param entry | |
721 | * The entry | |
722 | * @param level | |
723 | * level to expand to or negative for all levels | |
724 | * @param expanded | |
725 | * True if expanded, false if collapsed | |
726 | */ | |
727 | private void setExpandedState(ITimeGraphEntry entry, int level, boolean expanded) { | |
728 | setExpandedStateInt(entry, level, expanded); | |
729 | redraw(); | |
730 | } | |
731 | ||
732 | /** | |
733 | * Set the expanded state of a given entry and its children to the first | |
734 | * level that has one collapsed entry. | |
735 | * | |
736 | * @param entry | |
737 | * The entry | |
738 | */ | |
739 | private void setExpandedStateLevel(ITimeGraphEntry entry) { | |
740 | int level = findExpandedLevel(entry); | |
741 | if (level >= 0) { | |
742 | setExpandedStateInt(entry, level, true); | |
743 | redraw(); | |
744 | } | |
745 | } | |
746 | ||
747 | /* | |
a23cd365 | 748 | * Inner class for finding relative level with at least one collapsed entry. |
8b516722 BH |
749 | */ |
750 | private class SearchNode { | |
751 | SearchNode(ITimeGraphEntry e, int l) { | |
752 | entry = e; | |
753 | level = l; | |
754 | } | |
a23cd365 | 755 | |
8b516722 BH |
756 | ITimeGraphEntry entry; |
757 | int level; | |
758 | } | |
759 | ||
760 | /** | |
761 | * Finds the relative level with at least one collapsed entry. | |
762 | * | |
763 | * @param entry | |
764 | * the start entry | |
765 | * @return the found level or -1 if all levels are already expanded. | |
766 | */ | |
767 | private int findExpandedLevel(ITimeGraphEntry entry) { | |
768 | Queue<SearchNode> queue = new LinkedList<>(); | |
769 | SearchNode root = new SearchNode(entry, 0); | |
770 | SearchNode node = root; | |
771 | queue.add(root); | |
772 | ||
773 | while (!queue.isEmpty()) { | |
774 | node = queue.remove(); | |
775 | if (node.entry.hasChildren() && !getExpandedState(node.entry)) { | |
776 | return node.level; | |
777 | } | |
778 | for (ITimeGraphEntry e : node.entry.getChildren()) { | |
779 | if (e.hasChildren()) { | |
780 | SearchNode n = new SearchNode(e, node.level + 1); | |
781 | queue.add(n); | |
782 | } | |
783 | } | |
784 | } | |
785 | return -1; | |
786 | } | |
787 | ||
788 | /** | |
a23cd365 MK |
789 | * Set the expanded state of a given entry to certain relative level. It |
790 | * will call fireTreeEvent() for each changed entry. No redraw is done. | |
8b516722 BH |
791 | * |
792 | * @param entry | |
793 | * The entry | |
794 | * @param level | |
795 | * level to expand to or negative for all levels | |
796 | * @param expanded | |
797 | * True if expanded, false if collapsed | |
798 | */ | |
799 | private void setExpandedStateInt(ITimeGraphEntry entry, int aLevel, boolean expanded) { | |
800 | int level = aLevel; | |
801 | if ((level > 0) || (level < 0)) { | |
802 | level--; | |
803 | if (entry.hasChildren()) { | |
804 | for (ITimeGraphEntry e : entry.getChildren()) { | |
805 | setExpandedStateInt(e, level, expanded); | |
806 | } | |
807 | } | |
808 | } | |
809 | Item item = fItemData.findItem(entry); | |
810 | if (item != null && item.fExpanded != expanded) { | |
811 | item.fExpanded = expanded; | |
812 | fItemData.updateExpandedItems(); | |
813 | fireTreeEvent(item.fEntry, item.fExpanded); | |
814 | } | |
815 | } | |
816 | ||
837a2f8c PT |
817 | /** |
818 | * Collapses all nodes of the viewer's tree, starting with the root. | |
837a2f8c PT |
819 | */ |
820 | public void collapseAll() { | |
f1fae91f PT |
821 | for (Item item : fItemData.fItems) { |
822 | item.fExpanded = false; | |
837a2f8c | 823 | } |
f1fae91f | 824 | fItemData.updateExpandedItems(); |
837a2f8c PT |
825 | redraw(); |
826 | } | |
827 | ||
828 | /** | |
829 | * Expands all nodes of the viewer's tree, starting with the root. | |
837a2f8c PT |
830 | */ |
831 | public void expandAll() { | |
f1fae91f PT |
832 | for (Item item : fItemData.fItems) { |
833 | item.fExpanded = true; | |
837a2f8c | 834 | } |
f1fae91f | 835 | fItemData.updateExpandedItems(); |
837a2f8c PT |
836 | redraw(); |
837 | } | |
838 | ||
839 | /** | |
840 | * Add a tree listener | |
841 | * | |
842 | * @param listener | |
843 | * The listener to add | |
844 | */ | |
845 | public void addTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
846 | if (!fTreeListeners.contains(listener)) { |
847 | fTreeListeners.add(listener); | |
837a2f8c PT |
848 | } |
849 | } | |
850 | ||
851 | /** | |
852 | * Remove a tree listener | |
853 | * | |
854 | * @param listener | |
855 | * The listener to remove | |
856 | */ | |
857 | public void removeTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
858 | if (fTreeListeners.contains(listener)) { |
859 | fTreeListeners.remove(listener); | |
837a2f8c PT |
860 | } |
861 | } | |
862 | ||
863 | /** | |
864 | * Tree event callback | |
865 | * | |
866 | * @param entry | |
867 | * The affected entry | |
868 | * @param expanded | |
869 | * The expanded state (true for expanded, false for collapsed) | |
870 | */ | |
871 | public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) { | |
872 | TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry); | |
f1fae91f | 873 | for (ITimeGraphTreeListener listener : fTreeListeners) { |
837a2f8c PT |
874 | if (expanded) { |
875 | listener.treeExpanded(event); | |
876 | } else { | |
877 | listener.treeCollapsed(event); | |
878 | } | |
879 | } | |
880 | } | |
881 | ||
27df1564 XR |
882 | /** |
883 | * Add a menu listener on {@link ITimeGraphEntry}s | |
ae09c4ad | 884 | * |
27df1564 XR |
885 | * @param listener |
886 | * The listener to add | |
27df1564 XR |
887 | */ |
888 | public void addTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
889 | if (!fTimeGraphEntryMenuListeners.contains(listener)) { |
890 | fTimeGraphEntryMenuListeners.add(listener); | |
27df1564 XR |
891 | } |
892 | } | |
893 | ||
894 | /** | |
895 | * Remove a menu listener on {@link ITimeGraphEntry}s | |
896 | * | |
897 | * @param listener | |
898 | * The listener to remove | |
27df1564 XR |
899 | */ |
900 | public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
901 | if (fTimeGraphEntryMenuListeners.contains(listener)) { |
902 | fTimeGraphEntryMenuListeners.remove(listener); | |
27df1564 XR |
903 | } |
904 | } | |
905 | ||
906 | /** | |
907 | * Menu event callback on {@link ITimeGraphEntry}s | |
908 | * | |
909 | * @param event | |
a23cd365 MK |
910 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to |
911 | * the selected {@link ITimeGraphEntry} | |
27df1564 XR |
912 | */ |
913 | private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) { | |
f1fae91f | 914 | for (MenuDetectListener listener : fTimeGraphEntryMenuListeners) { |
27df1564 XR |
915 | listener.menuDetected(event); |
916 | } | |
917 | } | |
918 | ||
919 | /** | |
920 | * Add a menu listener on {@link ITimeEvent}s | |
921 | * | |
922 | * @param listener | |
923 | * The listener to add | |
27df1564 XR |
924 | */ |
925 | public void addTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
926 | if (!fTimeEventMenuListeners.contains(listener)) { |
927 | fTimeEventMenuListeners.add(listener); | |
27df1564 XR |
928 | } |
929 | } | |
930 | ||
931 | /** | |
932 | * Remove a menu listener on {@link ITimeEvent}s | |
933 | * | |
934 | * @param listener | |
935 | * The listener to remove | |
27df1564 XR |
936 | */ |
937 | public void removeTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
938 | if (fTimeEventMenuListeners.contains(listener)) { |
939 | fTimeEventMenuListeners.remove(listener); | |
27df1564 XR |
940 | } |
941 | } | |
942 | ||
943 | /** | |
944 | * Menu event callback on {@link ITimeEvent}s | |
945 | * | |
946 | * @param event | |
a23cd365 MK |
947 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to |
948 | * the selected {@link ITimeEvent} | |
27df1564 XR |
949 | */ |
950 | private void fireMenuEventOnTimeEvent(MenuDetectEvent event) { | |
f1fae91f | 951 | for (MenuDetectListener listener : fTimeEventMenuListeners) { |
27df1564 XR |
952 | listener.menuDetected(event); |
953 | } | |
954 | } | |
955 | ||
8b516722 BH |
956 | @Override |
957 | public boolean setFocus() { | |
958 | if ((fTimeProvider != null) && fTimeProvider.getNameSpace() > 0) { | |
959 | fHasNamespaceFocus = true; | |
960 | } | |
961 | return super.setFocus(); | |
962 | } | |
963 | ||
07cd9368 PT |
964 | /** |
965 | * Returns the current selection for this time graph. If a time graph entry | |
966 | * is selected, it will be the first element in the selection. If a time | |
967 | * event is selected, it will be the second element in the selection. | |
968 | * | |
969 | * @return the current selection | |
970 | */ | |
837a2f8c PT |
971 | @Override |
972 | public ISelection getSelection() { | |
07cd9368 PT |
973 | ITimeGraphEntry entry = getSelectedTrace(); |
974 | if (null != entry && null != fTimeProvider) { | |
baf92cac | 975 | long selectedTime = fTimeProvider.getSelectionBegin(); |
07cd9368 PT |
976 | ITimeEvent event = Utils.findEvent(entry, selectedTime, 0); |
977 | if (event == null) { | |
978 | return new StructuredSelection(entry); | |
837a2f8c | 979 | } |
07cd9368 | 980 | return new StructuredSelection(new Object[] { entry, event }); |
837a2f8c | 981 | } |
07cd9368 | 982 | return StructuredSelection.EMPTY; |
837a2f8c PT |
983 | } |
984 | ||
985 | /** | |
986 | * Get the selection object | |
987 | * | |
988 | * @return The selection | |
989 | */ | |
990 | public ISelection getSelectionTrace() { | |
07cd9368 PT |
991 | ITimeGraphEntry entry = getSelectedTrace(); |
992 | if (null != entry) { | |
993 | return new StructuredSelection(entry); | |
837a2f8c | 994 | } |
07cd9368 | 995 | return StructuredSelection.EMPTY; |
837a2f8c PT |
996 | } |
997 | ||
998 | /** | |
999 | * Enable/disable one of the traces in the model | |
1000 | * | |
1001 | * @param n | |
1002 | * 1 to enable it, -1 to disable. The method returns immediately | |
1003 | * if another value is used. | |
1004 | */ | |
1005 | public void selectTrace(int n) { | |
1006 | if ((n != 1) && (n != -1)) { | |
1007 | return; | |
1008 | } | |
1009 | ||
1010 | boolean changed = false; | |
1011 | int lastSelection = -1; | |
f1fae91f PT |
1012 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1013 | Item item = fItemData.fExpandedItems[i]; | |
1014 | if (item.fSelected) { | |
837a2f8c | 1015 | lastSelection = i; |
f1fae91f PT |
1016 | if ((1 == n) && (i < fItemData.fExpandedItems.length - 1)) { |
1017 | item.fSelected = false; | |
1018 | item = fItemData.fExpandedItems[i + 1]; | |
1019 | item.fSelected = true; | |
837a2f8c PT |
1020 | changed = true; |
1021 | } else if ((-1 == n) && (i > 0)) { | |
f1fae91f PT |
1022 | item.fSelected = false; |
1023 | item = fItemData.fExpandedItems[i - 1]; | |
1024 | item.fSelected = true; | |
837a2f8c PT |
1025 | changed = true; |
1026 | } | |
1027 | break; | |
1028 | } | |
1029 | } | |
1030 | ||
f1fae91f PT |
1031 | if (lastSelection < 0 && fItemData.fExpandedItems.length > 0) { |
1032 | Item item = fItemData.fExpandedItems[0]; | |
1033 | item.fSelected = true; | |
837a2f8c PT |
1034 | changed = true; |
1035 | } | |
1036 | ||
1037 | if (changed) { | |
1038 | ensureVisibleItem(-1, false); | |
1039 | redraw(); | |
1040 | fireSelectionChanged(); | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | /** | |
1045 | * Select an event | |
1046 | * | |
1047 | * @param n | |
1048 | * 1 for next event, -1 for previous event | |
33fa1fc7 PT |
1049 | * @param extend |
1050 | * true to extend selection range, false for single selection | |
1051 | * @since 1.0 | |
837a2f8c | 1052 | */ |
33fa1fc7 | 1053 | public void selectEvent(int n, boolean extend) { |
f1fae91f | 1054 | if (null == fTimeProvider) { |
837a2f8c PT |
1055 | return; |
1056 | } | |
1057 | ITimeGraphEntry trace = getSelectedTrace(); | |
1058 | if (trace == null) { | |
1059 | return; | |
1060 | } | |
33fa1fc7 | 1061 | long selectedTime = fTimeProvider.getSelectionEnd(); |
50d36521 | 1062 | long endTime = fTimeProvider.getMaxTime(); |
837a2f8c | 1063 | ITimeEvent nextEvent; |
33fa1fc7 | 1064 | if (n == -1 && selectedTime > endTime) { |
837a2f8c PT |
1065 | nextEvent = Utils.findEvent(trace, selectedTime, 0); |
1066 | } else { | |
1067 | nextEvent = Utils.findEvent(trace, selectedTime, n); | |
1068 | } | |
33fa1fc7 | 1069 | if (null == nextEvent && n == -1) { |
837a2f8c PT |
1070 | nextEvent = Utils.getFirstEvent(trace); |
1071 | } | |
1072 | if (null != nextEvent) { | |
1073 | long nextTime = nextEvent.getTime(); | |
1074 | // If last event detected e.g. going back or not moving to a next | |
1075 | // event | |
1076 | if (nextTime <= selectedTime && n == 1) { | |
1077 | // Select to the end of this last event | |
1078 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); | |
1079 | // but not beyond the end of the trace | |
1080 | if (nextTime > endTime) { | |
1081 | nextTime = endTime; | |
1082 | } | |
f1fae91f | 1083 | } else if (n == -1 && nextEvent.getTime() + nextEvent.getDuration() < selectedTime) { |
a23cd365 MK |
1084 | // for previous event go to its end time unless we were already |
1085 | // there | |
f1fae91f | 1086 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
837a2f8c | 1087 | } |
33fa1fc7 | 1088 | if (extend) { |
84c8aef7 | 1089 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), nextTime, true); |
33fa1fc7 PT |
1090 | } else { |
1091 | fTimeProvider.setSelectedTimeNotify(nextTime, true); | |
1092 | } | |
837a2f8c | 1093 | fireSelectionChanged(); |
33fa1fc7 PT |
1094 | } else if (n == 1) { |
1095 | if (extend) { | |
84c8aef7 | 1096 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), endTime, true); |
33fa1fc7 PT |
1097 | } else { |
1098 | fTimeProvider.setSelectedTimeNotify(endTime, true); | |
1099 | } | |
837a2f8c PT |
1100 | fireSelectionChanged(); |
1101 | } | |
33fa1fc7 | 1102 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
1103 | } |
1104 | ||
1105 | /** | |
1106 | * Select the next event | |
33fa1fc7 PT |
1107 | * |
1108 | * @param extend | |
1109 | * true to extend selection range, false for single selection | |
1110 | * @since 1.0 | |
837a2f8c | 1111 | */ |
33fa1fc7 PT |
1112 | public void selectNextEvent(boolean extend) { |
1113 | selectEvent(1, extend); | |
837a2f8c | 1114 | // Notify if visible time window has been adjusted |
f1fae91f | 1115 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
1116 | } |
1117 | ||
1118 | /** | |
1119 | * Select the previous event | |
33fa1fc7 PT |
1120 | * |
1121 | * @param extend | |
1122 | * true to extend selection range, false for single selection | |
1123 | * @since 1.0 | |
837a2f8c | 1124 | */ |
33fa1fc7 PT |
1125 | public void selectPrevEvent(boolean extend) { |
1126 | selectEvent(-1, extend); | |
837a2f8c | 1127 | // Notify if visible time window has been adjusted |
f1fae91f | 1128 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
1129 | } |
1130 | ||
1131 | /** | |
1132 | * Select the next trace | |
1133 | */ | |
1134 | public void selectNextTrace() { | |
1135 | selectTrace(1); | |
1136 | } | |
1137 | ||
1138 | /** | |
1139 | * Select the previous trace | |
1140 | */ | |
1141 | public void selectPrevTrace() { | |
1142 | selectTrace(-1); | |
1143 | } | |
1144 | ||
b698ec63 PT |
1145 | /** |
1146 | * Scroll left or right by one half window size | |
1147 | * | |
1148 | * @param left | |
1149 | * true to scroll left, false to scroll right | |
b698ec63 PT |
1150 | */ |
1151 | public void horizontalScroll(boolean left) { | |
1152 | long time0 = fTimeProvider.getTime0(); | |
1153 | long time1 = fTimeProvider.getTime1(); | |
1154 | long timeMin = fTimeProvider.getMinTime(); | |
1155 | long timeMax = fTimeProvider.getMaxTime(); | |
1156 | long range = time1 - time0; | |
1157 | if (range <= 0) { | |
1158 | return; | |
1159 | } | |
1160 | long increment = Math.max(1, range / 2); | |
1161 | if (left) { | |
1162 | time0 = Math.max(time0 - increment, timeMin); | |
1163 | time1 = time0 + range; | |
1164 | } else { | |
1165 | time1 = Math.min(time1 + increment, timeMax); | |
1166 | time0 = time1 - range; | |
1167 | } | |
1168 | fTimeProvider.setStartFinishTimeNotify(time0, time1); | |
1169 | } | |
1170 | ||
837a2f8c PT |
1171 | /** |
1172 | * Zoom based on mouse cursor location with mouse scrolling | |
1173 | * | |
a23cd365 MK |
1174 | * @param zoomIn |
1175 | * true to zoom in, false to zoom out | |
837a2f8c PT |
1176 | */ |
1177 | public void zoom(boolean zoomIn) { | |
1178 | int globalX = getDisplay().getCursorLocation().x; | |
1179 | Point p = toControl(globalX, 0); | |
f1fae91f PT |
1180 | int nameSpace = fTimeProvider.getNameSpace(); |
1181 | int timeSpace = fTimeProvider.getTimeSpace(); | |
837a2f8c | 1182 | int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x)); |
f1fae91f PT |
1183 | long time0 = fTimeProvider.getTime0(); |
1184 | long time1 = fTimeProvider.getTime1(); | |
837a2f8c PT |
1185 | long interval = time1 - time0; |
1186 | if (interval == 0) { | |
1187 | interval = 1; | |
1188 | } // to allow getting out of single point interval | |
1189 | long newInterval; | |
1190 | if (zoomIn) { | |
f1fae91f | 1191 | newInterval = Math.max(Math.round(interval * ZOOM_IN_FACTOR), fTimeProvider.getMinTimeInterval()); |
837a2f8c | 1192 | } else { |
f1fae91f | 1193 | newInterval = (long) Math.ceil(interval * ZOOM_OUT_FACTOR); |
837a2f8c PT |
1194 | } |
1195 | long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval)); | |
1196 | long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval); | |
5a868cfe PT |
1197 | /* snap to bounds if zooming out of range */ |
1198 | newTime0 = Math.max(fTimeProvider.getMinTime(), Math.min(newTime0, fTimeProvider.getMaxTime() - newInterval)); | |
837a2f8c | 1199 | long newTime1 = newTime0 + newInterval; |
10ad9fa6 | 1200 | fTimeProvider.setStartFinishTimeNotify(newTime0, newTime1); |
837a2f8c PT |
1201 | } |
1202 | ||
1203 | /** | |
1204 | * zoom in using single click | |
1205 | */ | |
1206 | public void zoomIn() { | |
f1fae91f PT |
1207 | long prevTime0 = fTimeProvider.getTime0(); |
1208 | long prevTime1 = fTimeProvider.getTime1(); | |
1209 | long prevRange = prevTime1 - prevTime0; | |
1210 | if (prevRange == 0) { | |
3e9a3685 PT |
1211 | return; |
1212 | } | |
baf92cac AM |
1213 | ITimeDataProvider provider = fTimeProvider; |
1214 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; | |
5a868cfe | 1215 | if (selTime < prevTime0 || selTime > prevTime1) { |
f1fae91f | 1216 | selTime = (prevTime0 + prevTime1) / 2; |
837a2f8c | 1217 | } |
f1fae91f PT |
1218 | long time0 = selTime - (long) ((selTime - prevTime0) / ZOOM_FACTOR); |
1219 | long time1 = selTime + (long) ((prevTime1 - selTime) / ZOOM_FACTOR); | |
837a2f8c | 1220 | |
f1fae91f PT |
1221 | long min = fTimeProvider.getMinTimeInterval(); |
1222 | if ((time1 - time0) < min) { | |
1223 | time0 = selTime - (selTime - prevTime0) * min / prevRange; | |
1224 | time1 = time0 + min; | |
837a2f8c PT |
1225 | } |
1226 | ||
f1fae91f | 1227 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
1228 | } |
1229 | ||
1230 | /** | |
1231 | * zoom out using single click | |
1232 | */ | |
1233 | public void zoomOut() { | |
f1fae91f PT |
1234 | long prevTime0 = fTimeProvider.getTime0(); |
1235 | long prevTime1 = fTimeProvider.getTime1(); | |
baf92cac AM |
1236 | ITimeDataProvider provider = fTimeProvider; |
1237 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; | |
5a868cfe | 1238 | if (selTime < prevTime0 || selTime > prevTime1) { |
f1fae91f | 1239 | selTime = (prevTime0 + prevTime1) / 2; |
837a2f8c | 1240 | } |
5a868cfe PT |
1241 | long newInterval; |
1242 | long time0; | |
1243 | if (prevTime1 - prevTime0 <= 1) { | |
1244 | newInterval = 2; | |
1245 | time0 = selTime - 1; | |
1246 | } else { | |
1247 | newInterval = (long) Math.ceil((prevTime1 - prevTime0) * ZOOM_FACTOR); | |
1248 | time0 = selTime - (long) Math.ceil((selTime - prevTime0) * ZOOM_FACTOR); | |
837a2f8c | 1249 | } |
5a868cfe PT |
1250 | /* snap to bounds if zooming out of range */ |
1251 | time0 = Math.max(fTimeProvider.getMinTime(), Math.min(time0, fTimeProvider.getMaxTime() - newInterval)); | |
1252 | long time1 = time0 + newInterval; | |
837a2f8c | 1253 | |
f1fae91f | 1254 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
1255 | } |
1256 | ||
3bd20aa6 PT |
1257 | /** |
1258 | * Zoom vertically. | |
1259 | * | |
1260 | * @param zoomIn | |
1261 | * true to zoom in, false to zoom out | |
3bd20aa6 PT |
1262 | * @since 2.0 |
1263 | */ | |
83d0971d | 1264 | public void verticalZoom(boolean zoomIn) { |
3bd20aa6 PT |
1265 | if (zoomIn) { |
1266 | fHeightAdjustment++; | |
1267 | } else { | |
1268 | fHeightAdjustment--; | |
95d225e0 | 1269 | fHeightAdjustment = Math.max(fHeightAdjustment, 1 - fMaxItemHeight); |
3bd20aa6 | 1270 | } |
83d0971d | 1271 | fItemData.refreshData(); |
3bd20aa6 PT |
1272 | redraw(); |
1273 | } | |
1274 | ||
1275 | /** | |
1276 | * Reset the vertical zoom to default. | |
1277 | * | |
3bd20aa6 PT |
1278 | * @since 2.0 |
1279 | */ | |
83d0971d | 1280 | public void resetVerticalZoom() { |
3bd20aa6 | 1281 | fHeightAdjustment = 0; |
83d0971d | 1282 | fItemData.refreshData(); |
3bd20aa6 PT |
1283 | redraw(); |
1284 | } | |
1285 | ||
55d1fc96 PT |
1286 | /** |
1287 | * Set the grid lines visibility. The default is true. | |
1288 | * | |
1289 | * @param visible | |
1290 | * true to show the grid lines, false otherwise | |
1291 | * @since 2.0 | |
1292 | */ | |
1293 | public void setGridLinesVisible(boolean visible) { | |
1294 | fGridLinesVisible = visible; | |
1295 | } | |
1296 | ||
1297 | /** | |
1298 | * Get the grid lines visibility. | |
1299 | * | |
1300 | * @return true if the grid lines are visible, false otherwise | |
1301 | * @since 2.0 | |
1302 | */ | |
1303 | public boolean getGridLinesVisible() { | |
1304 | return fGridLinesVisible; | |
1305 | } | |
1306 | ||
1307 | /** | |
1308 | * Set the grid line color. The default is SWT.COLOR_GRAY. | |
1309 | * | |
1310 | * @param color | |
1311 | * the grid line color | |
1312 | * @since 2.0 | |
1313 | */ | |
1314 | public void setGridLineColor(Color color) { | |
1315 | fGridLineColor = color; | |
1316 | } | |
1317 | ||
1318 | /** | |
1319 | * Get the grid line color. | |
1320 | * | |
1321 | * @return the grid line color | |
1322 | * @since 2.0 | |
1323 | */ | |
1324 | public Color getGridLineColor() { | |
1325 | return fGridLineColor; | |
1326 | } | |
1327 | ||
56b43ab2 PT |
1328 | /** |
1329 | * Set the markers list. | |
1330 | * | |
1331 | * @param markers | |
1332 | * The markers list, or null | |
1333 | * @since 2.0 | |
1334 | */ | |
1335 | public void setMarkers(List<IMarkerEvent> markers) { | |
1336 | fMarkers = markers; | |
56b43ab2 PT |
1337 | } |
1338 | ||
1339 | /** | |
1340 | * Get the markers list. | |
1341 | * | |
1342 | * @return The markers list, or null | |
1343 | * @since 2.0 | |
1344 | */ | |
1345 | public List<IMarkerEvent> getMarkers() { | |
1346 | return fMarkers; | |
1347 | } | |
1348 | ||
1349 | /** | |
1350 | * Set the markers visibility. The default is true. | |
1351 | * | |
1352 | * @param visible | |
1353 | * true to show the markers, false otherwise | |
1354 | * @since 2.0 | |
1355 | */ | |
1356 | public void setMarkersVisible(boolean visible) { | |
1357 | fMarkersVisible = visible; | |
56b43ab2 PT |
1358 | } |
1359 | ||
1360 | /** | |
1361 | * Get the markers visibility. | |
1362 | * | |
1363 | * @return true if the markers are visible, false otherwise | |
1364 | * @since 2.0 | |
1365 | */ | |
1366 | public boolean getMarkersVisible() { | |
1367 | return fMarkersVisible; | |
1368 | } | |
1369 | ||
79ec0b89 PT |
1370 | /** |
1371 | * Hide arrows | |
1372 | * | |
a23cd365 MK |
1373 | * @param hideArrows |
1374 | * true to hide arrows | |
79ec0b89 PT |
1375 | */ |
1376 | public void hideArrows(boolean hideArrows) { | |
1377 | fHideArrows = hideArrows; | |
1378 | } | |
1379 | ||
086f21ae PT |
1380 | /** |
1381 | * Follow the arrow forward | |
33fa1fc7 PT |
1382 | * |
1383 | * @param extend | |
1384 | * true to extend selection range, false for single selection | |
1385 | * @since 1.0 | |
086f21ae | 1386 | */ |
33fa1fc7 | 1387 | public void followArrowFwd(boolean extend) { |
086f21ae PT |
1388 | ITimeGraphEntry trace = getSelectedTrace(); |
1389 | if (trace == null) { | |
1390 | return; | |
1391 | } | |
33fa1fc7 | 1392 | long selectedTime = fTimeProvider.getSelectionEnd(); |
086f21ae PT |
1393 | for (ILinkEvent link : fItemData.fLinks) { |
1394 | if (link.getEntry() == trace && link.getTime() == selectedTime) { | |
1395 | selectItem(link.getDestinationEntry(), false); | |
1396 | if (link.getDuration() != 0) { | |
33fa1fc7 | 1397 | if (extend) { |
84c8aef7 | 1398 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime() + link.getDuration(), true); |
33fa1fc7 PT |
1399 | } else { |
1400 | fTimeProvider.setSelectedTimeNotify(link.getTime() + link.getDuration(), true); | |
1401 | } | |
086f21ae PT |
1402 | // Notify if visible time window has been adjusted |
1403 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); | |
1404 | } | |
1405 | fireSelectionChanged(); | |
33fa1fc7 | 1406 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
086f21ae PT |
1407 | return; |
1408 | } | |
1409 | } | |
33fa1fc7 | 1410 | selectNextEvent(extend); |
086f21ae PT |
1411 | } |
1412 | ||
1413 | /** | |
1414 | * Follow the arrow backward | |
33fa1fc7 PT |
1415 | * |
1416 | * @param extend | |
1417 | * true to extend selection range, false for single selection | |
1418 | * @since 1.0 | |
086f21ae | 1419 | */ |
33fa1fc7 | 1420 | public void followArrowBwd(boolean extend) { |
086f21ae PT |
1421 | ITimeGraphEntry trace = getSelectedTrace(); |
1422 | if (trace == null) { | |
1423 | return; | |
1424 | } | |
33fa1fc7 | 1425 | long selectedTime = fTimeProvider.getSelectionEnd(); |
086f21ae PT |
1426 | for (ILinkEvent link : fItemData.fLinks) { |
1427 | if (link.getDestinationEntry() == trace && link.getTime() + link.getDuration() == selectedTime) { | |
1428 | selectItem(link.getEntry(), false); | |
1429 | if (link.getDuration() != 0) { | |
33fa1fc7 | 1430 | if (extend) { |
84c8aef7 | 1431 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime(), true); |
33fa1fc7 PT |
1432 | } else { |
1433 | fTimeProvider.setSelectedTimeNotify(link.getTime(), true); | |
1434 | } | |
086f21ae PT |
1435 | // Notify if visible time window has been adjusted |
1436 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); | |
1437 | } | |
1438 | fireSelectionChanged(); | |
33fa1fc7 | 1439 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
086f21ae PT |
1440 | return; |
1441 | } | |
1442 | } | |
33fa1fc7 | 1443 | selectPrevEvent(extend); |
086f21ae PT |
1444 | } |
1445 | ||
837a2f8c PT |
1446 | /** |
1447 | * Return the currently selected trace | |
1448 | * | |
1449 | * @return The entry matching the trace | |
1450 | */ | |
1451 | public ITimeGraphEntry getSelectedTrace() { | |
1452 | ITimeGraphEntry trace = null; | |
1453 | int idx = getSelectedIndex(); | |
1454 | if (idx >= 0) { | |
70e10acc | 1455 | trace = fItemData.fExpandedItems[idx].fEntry; |
837a2f8c PT |
1456 | } |
1457 | return trace; | |
1458 | } | |
1459 | ||
1460 | /** | |
1461 | * Retrieve the index of the currently selected item | |
1462 | * | |
1463 | * @return The index | |
1464 | */ | |
1465 | public int getSelectedIndex() { | |
1466 | int idx = -1; | |
f1fae91f PT |
1467 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1468 | Item item = fItemData.fExpandedItems[i]; | |
1469 | if (item.fSelected) { | |
837a2f8c PT |
1470 | idx = i; |
1471 | break; | |
1472 | } | |
1473 | } | |
1474 | return idx; | |
1475 | } | |
1476 | ||
1477 | boolean toggle(int idx) { | |
1478 | boolean toggled = false; | |
f1fae91f PT |
1479 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
1480 | Item item = fItemData.fExpandedItems[idx]; | |
1481 | if (item.fHasChildren) { | |
1482 | item.fExpanded = !item.fExpanded; | |
1483 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
1484 | redraw(); |
1485 | toggled = true; | |
70e10acc | 1486 | fireTreeEvent(item.fEntry, item.fExpanded); |
837a2f8c PT |
1487 | } |
1488 | } | |
1489 | return toggled; | |
1490 | } | |
1491 | ||
bedfbbb5 XR |
1492 | /** |
1493 | * Gets the index of the item at the given location. | |
1494 | * | |
1495 | * @param y | |
1496 | * the y coordinate | |
1497 | * @return the index of the item at the given location, of -1 if none. | |
bedfbbb5 XR |
1498 | */ |
1499 | protected int getItemIndexAtY(int y) { | |
c004295c | 1500 | int ySum = 0; |
48072ae3 PT |
1501 | if (y < 0) { |
1502 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { | |
1503 | ySum -= fItemData.fExpandedItems[idx].fItemHeight; | |
1504 | if (y >= ySum) { | |
1505 | return idx; | |
1506 | } | |
1507 | } | |
1508 | } else { | |
1509 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { | |
1510 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1511 | if (y < ySum) { | |
1512 | return idx; | |
1513 | } | |
837a2f8c | 1514 | } |
837a2f8c PT |
1515 | } |
1516 | return -1; | |
1517 | } | |
1518 | ||
1519 | boolean isOverSplitLine(int x) { | |
f1fae91f | 1520 | if (x < 0 || null == fTimeProvider) { |
837a2f8c PT |
1521 | return false; |
1522 | } | |
f1fae91f | 1523 | int nameWidth = fTimeProvider.getNameSpace(); |
26cf0ea8 | 1524 | return Math.abs(x - nameWidth) <= SNAP_WIDTH; |
837a2f8c PT |
1525 | } |
1526 | ||
a974fd46 PT |
1527 | boolean isOverTimeSpace(int x, int y) { |
1528 | Point size = getSize(); | |
1529 | return x >= fTimeProvider.getNameSpace() && x < size.x && y >= 0 && y < size.y; | |
1530 | } | |
a23cd365 | 1531 | |
bedfbbb5 XR |
1532 | /** |
1533 | * Gets the {@link ITimeGraphEntry} at the given location. | |
1534 | * | |
1535 | * @param pt | |
1536 | * a point in the widget | |
1537 | * @return the {@link ITimeGraphEntry} at this point, or <code>null</code> | |
1538 | * if none. | |
8c684b48 | 1539 | * @since 2.0 |
bedfbbb5 | 1540 | */ |
8c684b48 | 1541 | public ITimeGraphEntry getEntry(Point pt) { |
837a2f8c | 1542 | int idx = getItemIndexAtY(pt.y); |
70e10acc | 1543 | return idx >= 0 ? fItemData.fExpandedItems[idx].fEntry : null; |
837a2f8c PT |
1544 | } |
1545 | ||
1053eaa6 PT |
1546 | /** |
1547 | * Return the arrow event closest to the given point that is no further than | |
1548 | * a maximum distance. | |
1549 | * | |
1550 | * @param pt | |
1551 | * a point in the widget | |
1552 | * @return The closest arrow event, or null if there is none close enough. | |
1053eaa6 PT |
1553 | */ |
1554 | protected ILinkEvent getArrow(Point pt) { | |
1555 | if (fHideArrows) { | |
1556 | return null; | |
1557 | } | |
1558 | ILinkEvent linkEvent = null; | |
1559 | double minDistance = Double.MAX_VALUE; | |
1560 | for (ILinkEvent event : fItemData.fLinks) { | |
1561 | Rectangle rect = getArrowRectangle(new Rectangle(0, 0, 0, 0), event); | |
1562 | if (rect != null) { | |
1563 | int x1 = rect.x; | |
1564 | int y1 = rect.y; | |
1565 | int x2 = x1 + rect.width; | |
1566 | int y2 = y1 + rect.height; | |
1567 | double d = Utils.distance(pt.x, pt.y, x1, y1, x2, y2); | |
1568 | if (minDistance > d) { | |
1569 | minDistance = d; | |
1570 | linkEvent = event; | |
1571 | } | |
1572 | } | |
1573 | } | |
1574 | if (minDistance <= ARROW_HOVER_MAX_DIST) { | |
1575 | return linkEvent; | |
1576 | } | |
1577 | return null; | |
1578 | } | |
1579 | ||
4999a196 | 1580 | @Override |
713a70ae | 1581 | public int getXForTime(long time) { |
f1fae91f | 1582 | if (null == fTimeProvider) { |
713a70ae PT |
1583 | return -1; |
1584 | } | |
f1fae91f PT |
1585 | long time0 = fTimeProvider.getTime0(); |
1586 | long time1 = fTimeProvider.getTime1(); | |
b698ec63 | 1587 | int width = getSize().x; |
f1fae91f | 1588 | int nameSpace = fTimeProvider.getNameSpace(); |
713a70ae | 1589 | double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
1d46761c | 1590 | int x = SaturatedArithmetic.add(getBounds().x + nameSpace, (int) ((time - time0) * pixelsPerNanoSec)); |
713a70ae PT |
1591 | return x; |
1592 | } | |
1593 | ||
4999a196 | 1594 | @Override |
41b5c37f | 1595 | public long getTimeAtX(int coord) { |
f1fae91f | 1596 | if (null == fTimeProvider) { |
837a2f8c PT |
1597 | return -1; |
1598 | } | |
1599 | long hitTime = -1; | |
b698ec63 | 1600 | Point size = getSize(); |
f1fae91f PT |
1601 | long time0 = fTimeProvider.getTime0(); |
1602 | long time1 = fTimeProvider.getTime1(); | |
1603 | int nameWidth = fTimeProvider.getNameSpace(); | |
41b5c37f | 1604 | final int x = coord - nameWidth; |
837a2f8c PT |
1605 | int timeWidth = size.x - nameWidth - RIGHT_MARGIN; |
1606 | if (x >= 0 && size.x >= nameWidth) { | |
1607 | if (time1 - time0 > timeWidth) { | |
a23cd365 MK |
1608 | // nanosecond smaller than one pixel: use the first integer |
1609 | // nanosecond of this pixel's time range | |
837a2f8c PT |
1610 | hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth)); |
1611 | } else { | |
a23cd365 MK |
1612 | // nanosecond greater than one pixel: use the nanosecond that |
1613 | // covers this pixel start position | |
837a2f8c PT |
1614 | hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth)); |
1615 | } | |
1616 | } | |
1617 | return hitTime; | |
1618 | } | |
1619 | ||
1620 | void selectItem(int idx, boolean addSelection) { | |
26cf0ea8 PT |
1621 | selectItem(idx, addSelection, true); |
1622 | } | |
1623 | ||
1624 | void selectItem(int idx, boolean addSelection, boolean reveal) { | |
837a2f8c PT |
1625 | boolean changed = false; |
1626 | if (addSelection) { | |
f1fae91f PT |
1627 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
1628 | Item item = fItemData.fExpandedItems[idx]; | |
1629 | changed = !item.fSelected; | |
1630 | item.fSelected = true; | |
837a2f8c PT |
1631 | } |
1632 | } else { | |
f1fae91f PT |
1633 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1634 | Item item = fItemData.fExpandedItems[i]; | |
1635 | if ((i == idx && !item.fSelected) || (idx == -1 && item.fSelected)) { | |
837a2f8c PT |
1636 | changed = true; |
1637 | } | |
f1fae91f | 1638 | item.fSelected = i == idx; |
837a2f8c PT |
1639 | } |
1640 | } | |
26cf0ea8 PT |
1641 | if (reveal) { |
1642 | changed |= ensureVisibleItem(idx, true); | |
1643 | } | |
837a2f8c PT |
1644 | if (changed) { |
1645 | redraw(); | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | /** | |
26cf0ea8 | 1650 | * Select an entry and make it visible |
837a2f8c | 1651 | * |
26cf0ea8 PT |
1652 | * @param entry |
1653 | * The entry to select | |
837a2f8c | 1654 | * @param addSelection |
26cf0ea8 PT |
1655 | * <code>true</code> to add the entry to the current selection, |
1656 | * or <code>false</code> to set a new selection | |
837a2f8c | 1657 | */ |
26cf0ea8 PT |
1658 | public void selectItem(ITimeGraphEntry entry, boolean addSelection) { |
1659 | int idx = fItemData.findItemIndex(entry); | |
837a2f8c PT |
1660 | selectItem(idx, addSelection); |
1661 | } | |
1662 | ||
26cf0ea8 PT |
1663 | /** |
1664 | * Select an entry | |
1665 | * | |
1666 | * @param entry | |
1667 | * The entry to select | |
1668 | * @param addSelection | |
1669 | * <code>true</code> to add the entry to the current selection, | |
1670 | * or <code>false</code> to set a new selection | |
1671 | * @param reveal | |
1672 | * <code>true</code> if the selection is to be made visible, and | |
1673 | * <code>false</code> otherwise | |
1674 | * @since 2.3 | |
1675 | */ | |
1676 | public void selectItem(ITimeGraphEntry entry, boolean addSelection, boolean reveal) { | |
1677 | int idx = fItemData.findItemIndex(entry); | |
1678 | selectItem(idx, addSelection, reveal); | |
1679 | } | |
1680 | ||
837a2f8c PT |
1681 | /** |
1682 | * Retrieve the number of entries shown per page. | |
1683 | * | |
1684 | * @return The count | |
1685 | */ | |
1686 | public int countPerPage() { | |
b698ec63 | 1687 | int height = getSize().y; |
837a2f8c | 1688 | int count = 0; |
c004295c PT |
1689 | int ySum = 0; |
1690 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { | |
1691 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1692 | if (ySum >= height) { | |
1693 | return count; | |
837a2f8c | 1694 | } |
c004295c | 1695 | count++; |
837a2f8c | 1696 | } |
c004295c PT |
1697 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { |
1698 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1699 | if (ySum >= height) { | |
1700 | return count; | |
1701 | } | |
1702 | count++; | |
837a2f8c PT |
1703 | } |
1704 | return count; | |
1705 | } | |
1706 | ||
1707 | /** | |
1708 | * Get the index of the top element | |
1709 | * | |
1710 | * @return The index | |
1711 | */ | |
1712 | public int getTopIndex() { | |
f1fae91f | 1713 | return fTopIndex; |
837a2f8c PT |
1714 | } |
1715 | ||
1716 | /** | |
df0e3d5f | 1717 | * Get the number of expanded (visible) items |
837a2f8c | 1718 | * |
df0e3d5f | 1719 | * @return The count of expanded (visible) items |
837a2f8c PT |
1720 | */ |
1721 | public int getExpandedElementCount() { | |
f1fae91f | 1722 | return fItemData.fExpandedItems.length; |
837a2f8c PT |
1723 | } |
1724 | ||
1725 | /** | |
df0e3d5f | 1726 | * Get an array of all expanded (visible) elements |
837a2f8c | 1727 | * |
df0e3d5f | 1728 | * @return The expanded (visible) elements |
837a2f8c PT |
1729 | */ |
1730 | public ITimeGraphEntry[] getExpandedElements() { | |
507b1336 | 1731 | ArrayList<ITimeGraphEntry> elements = new ArrayList<>(); |
f1fae91f | 1732 | for (Item item : fItemData.fExpandedItems) { |
70e10acc | 1733 | elements.add(item.fEntry); |
837a2f8c PT |
1734 | } |
1735 | return elements.toArray(new ITimeGraphEntry[0]); | |
1736 | } | |
1737 | ||
48072ae3 PT |
1738 | /** |
1739 | * Get the expanded (visible) element at the specified index. | |
1740 | * | |
1741 | * @param index | |
1742 | * the element index | |
1743 | * @return The expanded (visible) element or null if out of range | |
1744 | * @since 2.0 | |
1745 | */ | |
1746 | public ITimeGraphEntry getExpandedElement(int index) { | |
1747 | if (index < 0 || index >= fItemData.fExpandedItems.length) { | |
1748 | return null; | |
1749 | } | |
1750 | return fItemData.fExpandedItems[index].fEntry; | |
1751 | } | |
1752 | ||
2d594808 | 1753 | /** |
b52c0cb3 | 1754 | * Get the bounds of the specified entry relative to its parent time graph. |
2d594808 | 1755 | * |
a23cd365 MK |
1756 | * @param entry |
1757 | * the time graph entry | |
2d594808 | 1758 | * @return the bounds of the entry, or null if the entry is not visible |
5e479c4f | 1759 | * @since 2.3 |
2d594808 PT |
1760 | */ |
1761 | public Rectangle getItemBounds(ITimeGraphEntry entry) { | |
1762 | int idx = fItemData.findItemIndex(entry); | |
1763 | if (idx >= 0) { | |
b52c0cb3 | 1764 | return getItemRect(getClientArea(), idx); |
2d594808 PT |
1765 | } |
1766 | return null; | |
1767 | } | |
1768 | ||
83b36bb4 PT |
1769 | Rectangle getNameRect(Rectangle bounds, int idx, int nameWidth) { |
1770 | Rectangle rect = getItemRect(bounds, idx); | |
c004295c PT |
1771 | rect.width = nameWidth; |
1772 | return rect; | |
837a2f8c PT |
1773 | } |
1774 | ||
83b36bb4 PT |
1775 | Rectangle getStatesRect(Rectangle bounds, int idx, int nameWidth) { |
1776 | Rectangle rect = getItemRect(bounds, idx); | |
1777 | rect.x += nameWidth; | |
1778 | rect.width -= nameWidth; | |
1779 | return rect; | |
1780 | } | |
1781 | ||
1782 | Rectangle getItemRect(Rectangle bounds, int idx) { | |
c004295c PT |
1783 | int ySum = 0; |
1784 | if (idx >= fTopIndex) { | |
f1fae91f PT |
1785 | for (int i = fTopIndex; i < idx; i++) { |
1786 | ySum += fItemData.fExpandedItems[i].fItemHeight; | |
837a2f8c | 1787 | } |
c004295c PT |
1788 | } else { |
1789 | for (int i = fTopIndex - 1; i >= idx; i--) { | |
1790 | ySum -= fItemData.fExpandedItems[i].fItemHeight; | |
1791 | } | |
837a2f8c | 1792 | } |
83b36bb4 | 1793 | int y = bounds.y + ySum; |
c004295c | 1794 | int height = fItemData.fExpandedItems[idx].fItemHeight; |
83b36bb4 | 1795 | return new Rectangle(bounds.x, y, bounds.width, height); |
837a2f8c PT |
1796 | } |
1797 | ||
1798 | @Override | |
1799 | void paint(Rectangle bounds, PaintEvent e) { | |
1800 | GC gc = e.gc; | |
837a2f8c | 1801 | |
f1fae91f | 1802 | if (bounds.width < 2 || bounds.height < 2 || null == fTimeProvider) { |
837a2f8c PT |
1803 | return; |
1804 | } | |
1805 | ||
f1fae91f PT |
1806 | fIdealNameSpace = 0; |
1807 | int nameSpace = fTimeProvider.getNameSpace(); | |
837a2f8c | 1808 | |
1c67fff3 | 1809 | // draw the background layer |
83b36bb4 | 1810 | drawBackground(bounds, nameSpace, gc); |
837a2f8c | 1811 | |
55d1fc96 PT |
1812 | // draw the grid lines |
1813 | drawGridLines(bounds, gc); | |
1814 | ||
56b43ab2 PT |
1815 | // draw the background markers |
1816 | drawMarkers(bounds, fTimeProvider, fMarkers, false, nameSpace, gc); | |
1817 | ||
1c67fff3 | 1818 | // draw the items |
f1fae91f | 1819 | drawItems(bounds, fTimeProvider, fItemData.fExpandedItems, fTopIndex, nameSpace, gc); |
1c67fff3 | 1820 | |
56b43ab2 PT |
1821 | // draw the foreground markers |
1822 | drawMarkers(bounds, fTimeProvider, fMarkers, true, nameSpace, gc); | |
1823 | ||
1c67fff3 | 1824 | // draw the links (arrows) |
bec1f1ac | 1825 | drawLinks(bounds, fTimeProvider, fItemData.fLinks, nameSpace, gc); |
1c67fff3 | 1826 | |
bec1f1ac | 1827 | fTimeGraphProvider.postDrawControl(bounds, gc); |
837a2f8c | 1828 | |
0fcf3b09 PT |
1829 | int alpha = gc.getAlpha(); |
1830 | gc.setAlpha(100); | |
1831 | ||
f1fae91f PT |
1832 | long time0 = fTimeProvider.getTime0(); |
1833 | long time1 = fTimeProvider.getTime1(); | |
baf92cac AM |
1834 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
1835 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
837a2f8c | 1836 | double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
1d46761c PT |
1837 | int x0 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionBegin - time0) * pixelsPerNanoSec)); |
1838 | int x1 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionEnd - time0) * pixelsPerNanoSec)); | |
0fcf3b09 PT |
1839 | |
1840 | // draw selection lines | |
1841 | if (fDragState != DRAG_SELECTION) { | |
f1fae91f | 1842 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); |
0fcf3b09 PT |
1843 | if (x0 >= nameSpace && x0 < bounds.x + bounds.width) { |
1844 | gc.drawLine(x0, bounds.y, x0, bounds.y + bounds.height); | |
1845 | } | |
1846 | if (x1 != x0) { | |
1847 | if (x1 >= nameSpace && x1 < bounds.x + bounds.width) { | |
1848 | gc.drawLine(x1, bounds.y, x1, bounds.y + bounds.height); | |
1849 | } | |
1850 | } | |
837a2f8c PT |
1851 | } |
1852 | ||
0fcf3b09 PT |
1853 | // draw selection background |
1854 | if (selectionBegin != 0 && selectionEnd != 0 && fDragState != DRAG_SELECTION) { | |
1855 | x0 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x0)); | |
1856 | x1 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x1)); | |
1857 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1858 | if (x1 - x0 > 1) { | |
1859 | gc.fillRectangle(new Rectangle(x0 + 1, bounds.y, x1 - x0 - 1, bounds.height)); | |
1860 | } else if (x0 - x1 > 1) { | |
1861 | gc.fillRectangle(new Rectangle(x1 + 1, bounds.y, x0 - x1 - 1, bounds.height)); | |
1862 | } | |
1863 | } | |
1864 | ||
1865 | // draw drag selection background | |
1866 | if (fDragState == DRAG_ZOOM || fDragState == DRAG_SELECTION) { | |
1867 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1868 | if (fDragX0 < fDragX) { | |
1869 | gc.fillRectangle(new Rectangle(fDragX0, bounds.y, fDragX - fDragX0, bounds.height)); | |
1870 | } else if (fDragX0 > fDragX) { | |
1871 | gc.fillRectangle(new Rectangle(fDragX, bounds.y, fDragX0 - fDragX, bounds.height)); | |
1872 | } | |
1873 | } | |
1874 | ||
26cf0ea8 PT |
1875 | // draw split line |
1876 | if (DRAG_SPLIT_LINE == fDragState || | |
1877 | (DRAG_NONE == fDragState && fMouseOverSplitLine && fTimeProvider.getNameSpace() > 0)) { | |
1878 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.DARK_GRAY)); | |
1879 | } else { | |
1880 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.GRAY)); | |
1881 | } | |
1882 | gc.fillRectangle(bounds.x + nameSpace - SNAP_WIDTH, bounds.y, SNAP_WIDTH, bounds.height); | |
1883 | ||
1884 | if (DRAG_ZOOM == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { | |
f1fae91f PT |
1885 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); |
1886 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); | |
0fcf3b09 PT |
1887 | if (fDragX != fDragX0) { |
1888 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); | |
1889 | } | |
1890 | } else if (DRAG_SELECTION == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { | |
1891 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); | |
1892 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); | |
1893 | if (fDragX != fDragX0) { | |
1894 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); | |
1895 | } | |
837a2f8c | 1896 | } |
0fcf3b09 PT |
1897 | |
1898 | gc.setAlpha(alpha); | |
837a2f8c PT |
1899 | } |
1900 | ||
83b36bb4 | 1901 | /** |
1c67fff3 | 1902 | * Draw the background layer. Fills the background of the control's name |
a23cd365 MK |
1903 | * space and states space, updates the background of items if necessary, and |
1904 | * draws the item's name text and middle line. | |
83b36bb4 PT |
1905 | * |
1906 | * @param bounds | |
1c67fff3 | 1907 | * The bounds of the control |
83b36bb4 | 1908 | * @param nameSpace |
1c67fff3 | 1909 | * The name space width |
83b36bb4 | 1910 | * @param gc |
1c67fff3 | 1911 | * Graphics context |
83b36bb4 PT |
1912 | * @since 2.0 |
1913 | */ | |
1914 | protected void drawBackground(Rectangle bounds, int nameSpace, GC gc) { | |
1915 | // draw empty name space background | |
1916 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1917 | drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height); | |
1918 | ||
1919 | // draw empty states space background | |
1920 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.BACKGROUND)); | |
1921 | drawBackground(gc, bounds.x + nameSpace, bounds.y, bounds.width - nameSpace, bounds.height); | |
1922 | ||
83b36bb4 PT |
1923 | for (int i = fTopIndex; i < fItemData.fExpandedItems.length; i++) { |
1924 | Rectangle itemRect = getItemRect(bounds, i); | |
1925 | if (itemRect.y >= bounds.y + bounds.height) { | |
1926 | break; | |
1927 | } | |
1928 | Item item = fItemData.fExpandedItems[i]; | |
a23cd365 MK |
1929 | // draw the background of selected item and items with no time |
1930 | // events | |
1931 | if (!item.fEntry.hasTimeEvents()) { | |
83b36bb4 PT |
1932 | gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus)); |
1933 | gc.fillRectangle(itemRect); | |
1934 | } else if (item.fSelected) { | |
1935 | gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, true)); | |
1936 | gc.fillRectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height); | |
1937 | gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, false)); | |
1938 | gc.fillRectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); | |
1939 | } | |
26cf0ea8 PT |
1940 | // draw the name space |
1941 | Rectangle nameRect = new Rectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height); | |
1942 | drawName(item, nameRect, gc); | |
1943 | if (item.fEntry.hasTimeEvents()) { | |
1c67fff3 PT |
1944 | Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); |
1945 | drawMidLine(rect, gc); | |
1946 | } | |
83b36bb4 PT |
1947 | } |
1948 | } | |
1949 | ||
55d1fc96 PT |
1950 | /** |
1951 | * Draw the grid lines | |
1952 | * | |
1953 | * @param bounds | |
1c67fff3 | 1954 | * The bounds of the control |
55d1fc96 | 1955 | * @param gc |
1c67fff3 | 1956 | * Graphics context |
55d1fc96 PT |
1957 | * @since 2.0 |
1958 | */ | |
1959 | public void drawGridLines(Rectangle bounds, GC gc) { | |
1960 | if (!fGridLinesVisible) { | |
1961 | return; | |
1962 | } | |
1963 | gc.setForeground(fGridLineColor); | |
56b43ab2 | 1964 | gc.setAlpha(fGridLineColor.getAlpha()); |
55d1fc96 PT |
1965 | for (int x : fTimeGraphScale.getTickList()) { |
1966 | gc.drawLine(x, bounds.y, x, bounds.y + bounds.height); | |
1967 | } | |
56b43ab2 PT |
1968 | gc.setAlpha(255); |
1969 | } | |
1970 | ||
1971 | /** | |
1972 | * Draw the markers | |
1973 | * | |
1974 | * @param bounds | |
1975 | * The rectangle of the area | |
1976 | * @param timeProvider | |
1977 | * The time provider | |
1978 | * @param markers | |
1979 | * The list of markers | |
1980 | * @param foreground | |
1981 | * true to draw the foreground markers, false otherwise | |
1982 | * @param nameSpace | |
1983 | * The width reserved for the names | |
1984 | * @param gc | |
1985 | * Reference to the SWT GC object | |
1986 | * @since 2.0 | |
1987 | */ | |
1988 | protected void drawMarkers(Rectangle bounds, ITimeDataProvider timeProvider, List<IMarkerEvent> markers, boolean foreground, int nameSpace, GC gc) { | |
1989 | if (!fMarkersVisible || markers == null || markers.isEmpty()) { | |
1990 | return; | |
1991 | } | |
1992 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); | |
1993 | /* the list can grow concurrently but cannot shrink */ | |
1994 | for (int i = 0; i < markers.size(); i++) { | |
1995 | IMarkerEvent marker = markers.get(i); | |
1996 | if (marker.isForeground() == foreground) { | |
1997 | drawMarker(marker, bounds, timeProvider, nameSpace, gc); | |
1998 | } | |
1999 | } | |
2000 | gc.setClipping((Rectangle) null); | |
2001 | } | |
2002 | ||
2003 | /** | |
2004 | * Draw a single marker | |
2005 | * | |
2006 | * @param marker | |
2007 | * The marker event | |
2008 | * @param bounds | |
2009 | * The bounds of the control | |
2010 | * @param timeProvider | |
2011 | * The time provider | |
2012 | * @param nameSpace | |
2013 | * The width reserved for the name | |
2014 | * @param gc | |
2015 | * Reference to the SWT GC object | |
2016 | * @since 2.0 | |
2017 | */ | |
2018 | protected void drawMarker(IMarkerEvent marker, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { | |
2019 | Rectangle rect = Utils.clone(bounds); | |
2020 | if (marker.getEntry() != null) { | |
2021 | int index = fItemData.findItemIndex(marker.getEntry()); | |
2022 | if (index == -1) { | |
2023 | return; | |
2024 | } | |
2025 | rect = getStatesRect(bounds, index, nameSpace); | |
2026 | if (rect.y < 0 || rect.y > bounds.height) { | |
2027 | return; | |
2028 | } | |
2029 | } | |
2030 | int x0 = getXForTime(marker.getTime()); | |
2031 | int x1 = getXForTime(marker.getTime() + marker.getDuration()); | |
2032 | if (x0 > bounds.width || x1 < nameSpace) { | |
2033 | return; | |
2034 | } | |
2035 | rect.x = Math.max(nameSpace, Math.min(bounds.width, x0)); | |
2036 | rect.width = Math.max(1, Math.min(bounds.width, x1) - rect.x); | |
2037 | ||
8910dea2 PT |
2038 | Color color = getColorScheme().getColor(marker.getColor()); |
2039 | gc.setBackground(color); | |
2040 | gc.setAlpha(color.getAlpha()); | |
56b43ab2 PT |
2041 | gc.fillRectangle(rect); |
2042 | gc.setAlpha(255); | |
2043 | String label = marker.getLabel(); | |
2044 | if (label != null && marker.getEntry() != null) { | |
1d012443 | 2045 | label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH)); |
8910dea2 | 2046 | gc.setForeground(color); |
1d012443 | 2047 | Utils.drawText(gc, label, rect.x - gc.textExtent(label).x, rect.y, true); |
56b43ab2 | 2048 | } |
55d1fc96 PT |
2049 | } |
2050 | ||
837a2f8c PT |
2051 | /** |
2052 | * Draw many items at once | |
2053 | * | |
2054 | * @param bounds | |
1c67fff3 | 2055 | * The bounds of the control |
837a2f8c PT |
2056 | * @param timeProvider |
2057 | * The time provider | |
2058 | * @param items | |
2059 | * The array items to draw | |
2060 | * @param topIndex | |
2061 | * The index of the first element to draw | |
2062 | * @param nameSpace | |
1c67fff3 | 2063 | * The name space width |
837a2f8c | 2064 | * @param gc |
1c67fff3 | 2065 | * Graphics context |
837a2f8c PT |
2066 | */ |
2067 | public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider, | |
2068 | Item[] items, int topIndex, int nameSpace, GC gc) { | |
2069 | for (int i = topIndex; i < items.length; i++) { | |
2070 | Item item = items[i]; | |
2071 | drawItem(item, bounds, timeProvider, i, nameSpace, gc); | |
2072 | } | |
837a2f8c PT |
2073 | } |
2074 | ||
2075 | /** | |
2076 | * Draws the item | |
2077 | * | |
1c67fff3 PT |
2078 | * @param item |
2079 | * The item to draw | |
2080 | * @param bounds | |
2081 | * The bounds of the control | |
2082 | * @param timeProvider | |
2083 | * The time provider | |
2084 | * @param i | |
2085 | * The expanded item index | |
2086 | * @param nameSpace | |
2087 | * The name space width | |
2088 | * @param gc | |
2089 | * Graphics context | |
837a2f8c PT |
2090 | */ |
2091 | protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) { | |
83b36bb4 PT |
2092 | Rectangle itemRect = getItemRect(bounds, i); |
2093 | if (itemRect.y >= bounds.y + bounds.height) { | |
2094 | return; | |
2095 | } | |
2096 | ||
70e10acc | 2097 | ITimeGraphEntry entry = item.fEntry; |
837a2f8c PT |
2098 | long time0 = timeProvider.getTime0(); |
2099 | long time1 = timeProvider.getTime1(); | |
33fa1fc7 | 2100 | long selectedTime = fTimeProvider.getSelectionEnd(); |
837a2f8c | 2101 | |
83b36bb4 | 2102 | Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height); |
1c67fff3 | 2103 | if (rect.isEmpty() || (time1 <= time0)) { |
837a2f8c PT |
2104 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); |
2105 | return; | |
2106 | } | |
2107 | ||
f1fae91f | 2108 | boolean selected = item.fSelected; |
837a2f8c PT |
2109 | // K pixels per second |
2110 | double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0); | |
2111 | ||
70e10acc | 2112 | if (item.fEntry.hasTimeEvents()) { |
ab0a54d5 | 2113 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
837a2f8c | 2114 | fillSpace(rect, gc, selected); |
83d0971d PT |
2115 | |
2116 | int margins = getMarginForHeight(rect.height); | |
2117 | int height = rect.height - margins; | |
2118 | int topMargin = (margins + 1) / 2; | |
2119 | Rectangle stateRect = new Rectangle(rect.x, rect.y + topMargin, rect.width, height); | |
2120 | ||
2121 | /* Set the font for this item */ | |
2122 | setFontForHeight(height, gc); | |
837a2f8c PT |
2123 | |
2124 | long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace(); | |
2125 | Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration); | |
2126 | ||
2127 | int lastX = -1; | |
2128 | while (iterator.hasNext()) { | |
2129 | ITimeEvent event = iterator.next(); | |
1d46761c PT |
2130 | int x = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() - time0) * pixelsPerNanoSec)); |
2131 | int xEnd = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec)); | |
837a2f8c PT |
2132 | if (x >= rect.x + rect.width || xEnd < rect.x) { |
2133 | // event is out of bounds | |
2134 | continue; | |
2135 | } | |
2136 | xEnd = Math.min(rect.x + rect.width, xEnd); | |
2137 | stateRect.x = Math.max(rect.x, x); | |
2138 | stateRect.width = Math.max(0, xEnd - stateRect.x + 1); | |
2139 | if (stateRect.x == lastX) { | |
2140 | stateRect.width -= 1; | |
2141 | if (stateRect.width > 0) { | |
2142 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); | |
2143 | gc.drawPoint(stateRect.x, stateRect.y - 2); | |
2144 | stateRect.x += 1; | |
2145 | } | |
837a2f8c PT |
2146 | } |
2147 | boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration(); | |
f1fae91f | 2148 | if (drawState(getColorScheme(), event, stateRect, gc, selected, timeSelected)) { |
b052914f | 2149 | lastX = stateRect.x; |
ad128fd8 | 2150 | } |
837a2f8c | 2151 | } |
ab0a54d5 | 2152 | gc.setClipping((Rectangle) null); |
837a2f8c PT |
2153 | } |
2154 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); | |
2155 | } | |
2156 | ||
bec1f1ac GB |
2157 | /** |
2158 | * Draw the links | |
2159 | * | |
2160 | * @param bounds | |
1c67fff3 | 2161 | * The bounds of the control |
bec1f1ac GB |
2162 | * @param timeProvider |
2163 | * The time provider | |
2164 | * @param links | |
6ae6c5bd | 2165 | * The list of link events |
bec1f1ac | 2166 | * @param nameSpace |
1c67fff3 | 2167 | * The name space width |
bec1f1ac | 2168 | * @param gc |
1c67fff3 | 2169 | * Graphics context |
bec1f1ac GB |
2170 | */ |
2171 | public void drawLinks(Rectangle bounds, ITimeDataProvider timeProvider, | |
2172 | List<ILinkEvent> links, int nameSpace, GC gc) { | |
79ec0b89 PT |
2173 | if (fHideArrows) { |
2174 | return; | |
2175 | } | |
ab0a54d5 | 2176 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
6ae6c5bd PT |
2177 | /* the list can grow concurrently but cannot shrink */ |
2178 | for (int i = 0; i < links.size(); i++) { | |
2179 | drawLink(links.get(i), bounds, timeProvider, nameSpace, gc); | |
bec1f1ac | 2180 | } |
ab0a54d5 | 2181 | gc.setClipping((Rectangle) null); |
bec1f1ac GB |
2182 | } |
2183 | ||
2184 | /** | |
1c67fff3 | 2185 | * Draws a link type event |
bec1f1ac GB |
2186 | * |
2187 | * @param event | |
1c67fff3 | 2188 | * The link event to draw |
bec1f1ac | 2189 | * @param bounds |
1c67fff3 | 2190 | * The bounds of the control |
bec1f1ac | 2191 | * @param timeProvider |
1c67fff3 | 2192 | * The time provider |
bec1f1ac | 2193 | * @param nameSpace |
1c67fff3 | 2194 | * The name space width |
bec1f1ac GB |
2195 | * @param gc |
2196 | * Graphics context | |
bec1f1ac GB |
2197 | */ |
2198 | protected void drawLink(ILinkEvent event, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { | |
1053eaa6 PT |
2199 | drawArrow(getColorScheme(), event, getArrowRectangle(bounds, event), gc); |
2200 | } | |
2201 | ||
2202 | private Rectangle getArrowRectangle(Rectangle bounds, ILinkEvent event) { | |
bec1f1ac GB |
2203 | int srcIndex = fItemData.findItemIndex(event.getEntry()); |
2204 | int destIndex = fItemData.findItemIndex(event.getDestinationEntry()); | |
2205 | ||
2206 | if ((srcIndex == -1) || (destIndex == -1)) { | |
1053eaa6 | 2207 | return null; |
bec1f1ac GB |
2208 | } |
2209 | ||
1053eaa6 PT |
2210 | Rectangle src = getStatesRect(bounds, srcIndex, fTimeProvider.getNameSpace()); |
2211 | Rectangle dst = getStatesRect(bounds, destIndex, fTimeProvider.getNameSpace()); | |
bec1f1ac GB |
2212 | |
2213 | int x0 = getXForTime(event.getTime()); | |
2214 | int x1 = getXForTime(event.getTime() + event.getDuration()); | |
03ab8eeb PT |
2215 | |
2216 | // limit the x-coordinates to prevent integer overflow in calculations | |
2217 | // and also GC.drawLine doesn't draw properly with large coordinates | |
2218 | final int limit = Integer.MAX_VALUE / 1024; | |
2219 | x0 = Math.max(-limit, Math.min(x0, limit)); | |
2220 | x1 = Math.max(-limit, Math.min(x1, limit)); | |
2221 | ||
bec1f1ac GB |
2222 | int y0 = src.y + src.height / 2; |
2223 | int y1 = dst.y + dst.height / 2; | |
1053eaa6 | 2224 | return new Rectangle(x0, y0, x1 - x0, y1 - y0); |
bec1f1ac GB |
2225 | } |
2226 | ||
2227 | /** | |
1c67fff3 | 2228 | * Draw an arrow |
bec1f1ac GB |
2229 | * |
2230 | * @param colors | |
2231 | * Color scheme | |
2232 | * @param event | |
1c67fff3 | 2233 | * Time event for which we're drawing the arrow |
bec1f1ac | 2234 | * @param rect |
1c67fff3 | 2235 | * The arrow rectangle |
bec1f1ac GB |
2236 | * @param gc |
2237 | * Graphics context | |
1c67fff3 | 2238 | * @return true if the arrow was drawn |
bec1f1ac GB |
2239 | */ |
2240 | protected boolean drawArrow(TimeGraphColorScheme colors, ITimeEvent event, | |
2241 | Rectangle rect, GC gc) { | |
2242 | ||
a5e66312 PT |
2243 | if (rect == null) { |
2244 | return false; | |
2245 | } | |
bec1f1ac GB |
2246 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); |
2247 | if (colorIdx < 0) { | |
2248 | return false; | |
2249 | } | |
2250 | boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true; | |
2251 | ||
2252 | if (visible) { | |
2253 | Color stateColor = null; | |
2254 | if (colorIdx < fEventColorMap.length) { | |
2255 | stateColor = fEventColorMap[colorIdx]; | |
2256 | } else { | |
2257 | stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); | |
2258 | } | |
2259 | ||
2260 | gc.setForeground(stateColor); | |
2261 | gc.setBackground(stateColor); | |
2262 | ||
2263 | /* Draw the arrow */ | |
2264 | gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); | |
2265 | drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc); | |
2266 | ||
2267 | } | |
2268 | fTimeGraphProvider.postDrawEvent(event, rect, gc); | |
2269 | return visible; | |
2270 | } | |
2271 | ||
2272 | /* | |
2273 | * @author Francis Giraldeau | |
2274 | * | |
2275 | * Inspiration: | |
2276 | * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm | |
2277 | * | |
2278 | * The algorithm was taken from this site, not the code itself | |
2279 | */ | |
a23cd365 | 2280 | private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc) { |
bec1f1ac GB |
2281 | int factor = 10; |
2282 | double cos = 0.9510; | |
2283 | double sin = 0.3090; | |
03ab8eeb PT |
2284 | long lenx = x1 - x0; |
2285 | long leny = y1 - y0; | |
bec1f1ac GB |
2286 | double len = Math.sqrt(lenx * lenx + leny * leny); |
2287 | ||
2288 | double dx = factor * lenx / len; | |
2289 | double dy = factor * leny / len; | |
2290 | int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin))); | |
2291 | int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos))); | |
2292 | int end2X = (int) Math.round((x1 - (dx * cos + dy * sin))); | |
2293 | int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos))); | |
2294 | int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 }; | |
2295 | gc.fillPolygon(arrow); | |
2296 | } | |
2297 | ||
a0a88f65 | 2298 | /** |
26cf0ea8 | 2299 | * Draw the name space of an item. |
a0a88f65 AM |
2300 | * |
2301 | * @param item | |
2302 | * Item object | |
2303 | * @param bounds | |
1c67fff3 | 2304 | * The bounds of the item's name space |
a0a88f65 AM |
2305 | * @param gc |
2306 | * Graphics context | |
2307 | */ | |
837a2f8c | 2308 | protected void drawName(Item item, Rectangle bounds, GC gc) { |
26cf0ea8 | 2309 | // No name space to be drawn |
f1fae91f | 2310 | if (fTimeProvider.getNameSpace() == 0) { |
837a2f8c PT |
2311 | return; |
2312 | } | |
2313 | ||
26cf0ea8 PT |
2314 | boolean hasTimeEvents = item.fEntry.hasTimeEvents(); |
2315 | if (hasTimeEvents) { | |
2316 | gc.setClipping(bounds); | |
2317 | } | |
2318 | ||
83d0971d PT |
2319 | int height = bounds.height - getMarginForHeight(bounds.height); |
2320 | setFontForHeight(height, gc); | |
2321 | ||
26cf0ea8 | 2322 | String name = fLabelProvider == null ? item.fName : fLabelProvider.getColumnText(item.fEntry, 0); |
837a2f8c | 2323 | Rectangle rect = Utils.clone(bounds); |
26cf0ea8 PT |
2324 | rect.y += (bounds.height - gc.stringExtent(name).y) / 2; |
2325 | TreeColumn[] columns = fTree.getColumns(); | |
2326 | int idealNameSpace = 0; | |
2327 | for (int i = 0; i < columns.length; i++) { | |
2328 | int columnIndex = fTree.getColumnOrder()[i]; | |
2329 | TreeColumn column = columns[columnIndex]; | |
2330 | rect.width = column.getWidth(); | |
2331 | gc.setClipping(rect.x, bounds.y, Math.min(rect.width, bounds.x + bounds.width - rect.x - SNAP_WIDTH), bounds.height); | |
2332 | int width = MARGIN; | |
2333 | if (i == 0) { | |
2334 | // first visible column | |
2335 | width += item.fLevel * EXPAND_SIZE; | |
2336 | if (item.fHasChildren) { | |
2337 | // draw expand/collapse arrow | |
2338 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.DARK_GRAY)); | |
2339 | int arrowHeightHint = (height < 4) ? height : (height < 6) ? height - 1 : height - 2; | |
2340 | int arrowHalfHeight = Math.max(1, Math.min(arrowHeightHint, (int) Math.round((EXPAND_SIZE - 2) / ARROW_RATIO))) / 2; | |
2341 | int arrowHalfWidth = (Math.max(1, Math.min(EXPAND_SIZE - 2, (int) Math.round(arrowHeightHint * ARROW_RATIO))) + 1) / 2; | |
2342 | int x1 = bounds.x + width + 1; | |
2343 | int x2 = x1 + 2 * arrowHalfWidth; | |
2344 | int midy = bounds.y + bounds.height / 2; | |
2345 | int y1 = midy - arrowHalfHeight; | |
2346 | int y2 = midy + arrowHalfHeight; | |
2347 | if (!item.fExpanded) { // > | |
2348 | gc.fillPolygon(new int[] { x1, y1, x2, midy, x1, y2 }); | |
2349 | } else { // v | |
2350 | int midx = x1 + arrowHalfWidth; | |
2351 | gc.fillPolygon(new int[] { x1, y1, x2, y1, midx, y2 }); | |
2352 | } | |
2353 | } | |
2354 | width += EXPAND_SIZE + MARGIN; | |
2355 | ||
2356 | Image img = fLabelProvider != null ? fLabelProvider.getColumnImage(item.fEntry, columnIndex) | |
2357 | : columnIndex == 0 ? fTimeGraphProvider.getItemImage(item.fEntry) : null; | |
2358 | if (img != null) { | |
2359 | // draw icon | |
2360 | int imgHeight = img.getImageData().height; | |
2361 | int imgWidth = img.getImageData().width; | |
c44be4c0 PT |
2362 | int dstHeight = Math.min(bounds.height, imgHeight); |
2363 | int dstWidth = dstHeight * imgWidth / imgHeight; | |
26cf0ea8 | 2364 | int x = width; |
c44be4c0 PT |
2365 | int y = bounds.y + (bounds.height - dstHeight) / 2; |
2366 | gc.drawImage(img, 0, 0, imgWidth, imgHeight, x, y, dstWidth, dstHeight); | |
26cf0ea8 PT |
2367 | width += imgWidth + MARGIN; |
2368 | } | |
2369 | } else { | |
2370 | if (fLabelProvider == null) { | |
2371 | break; | |
2372 | } | |
2373 | } | |
2374 | String label = fLabelProvider != null ? fLabelProvider.getColumnText(item.fEntry, columnIndex) | |
2375 | : columnIndex == 0 ? item.fName : ""; //$NON-NLS-1$ | |
f1fae91f | 2376 | gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus)); |
26cf0ea8 PT |
2377 | Rectangle textRect = new Rectangle(rect.x + width, rect.y, rect.width - width, rect.height); |
2378 | int textWidth = Utils.drawText(gc, label, textRect, true); | |
2379 | width += textWidth + MARGIN; | |
2380 | if (textWidth > 0) { | |
2381 | idealNameSpace = rect.x + width; | |
2382 | } | |
2383 | if (columns.length == 1) { | |
2384 | drawMidLine(new Rectangle(bounds.x + width, bounds.y, bounds.x + bounds.width, bounds.height), gc); | |
2385 | } | |
2386 | if (fAutoResizeColumns && width > column.getWidth()) { | |
2387 | column.setData(PREFERRED_WIDTH, width); | |
2388 | column.setWidth(width); | |
837a2f8c | 2389 | } |
26cf0ea8 PT |
2390 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE)); |
2391 | if (i < columns.length - 1) { | |
2392 | // not the last visible column: draw the vertical cell border | |
2393 | int x = rect.x + rect.width - 1; | |
2394 | gc.drawLine(x, bounds.y, x, bounds.y + bounds.height); | |
2395 | } | |
2396 | rect.x += rect.width; | |
837a2f8c | 2397 | } |
26cf0ea8 PT |
2398 | fIdealNameSpace = Math.max(fIdealNameSpace, idealNameSpace); |
2399 | ||
2400 | gc.setClipping((Rectangle) null); | |
837a2f8c PT |
2401 | } |
2402 | ||
a0a88f65 AM |
2403 | /** |
2404 | * Draw the state (color fill) | |
2405 | * | |
2406 | * @param colors | |
2407 | * Color scheme | |
2408 | * @param event | |
2409 | * Time event for which we're drawing the state | |
2410 | * @param rect | |
1c67fff3 | 2411 | * The state rectangle |
a0a88f65 AM |
2412 | * @param gc |
2413 | * Graphics context | |
2414 | * @param selected | |
2415 | * Is this time event currently selected (so it appears | |
2416 | * highlighted) | |
2417 | * @param timeSelected | |
2418 | * Is the timestamp currently selected | |
ad128fd8 | 2419 | * @return true if the state was drawn |
a0a88f65 | 2420 | */ |
ad128fd8 | 2421 | protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event, |
837a2f8c PT |
2422 | Rectangle rect, GC gc, boolean selected, boolean timeSelected) { |
2423 | ||
2424 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); | |
ad128fd8 PT |
2425 | if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) { |
2426 | return false; | |
837a2f8c PT |
2427 | } |
2428 | boolean visible = rect.width == 0 ? false : true; | |
b052914f | 2429 | rect.width = Math.max(1, rect.width); |
a23cd365 | 2430 | Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
351a2391 | 2431 | gc.setForeground(black); |
a23cd365 MK |
2432 | Map<String, Object> styleMap = fTimeGraphProvider.getEventStyle(event); |
2433 | float heightFactor = (float) styleMap.getOrDefault(ITimeEventStyleStrings.heightFactor(), 1.0f); | |
2434 | if (heightFactor > 1.0 || heightFactor < 0) { | |
2435 | if (fFirstHeightWarning) { | |
2436 | TraceCompassLog.getLogger(this.getClass()).warning("Heightfactor out of range : " + heightFactor + " for event " + event.toString() + " - clamping results"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ | |
2437 | fFirstHeightWarning = false; | |
2438 | } | |
2439 | heightFactor = Math.max(0.0f, Math.min(1.0f, heightFactor)); | |
2440 | } | |
2441 | ||
2442 | int height = (int) (rect.height * heightFactor); | |
2443 | Rectangle drawRect = new Rectangle(rect.x, rect.y + ((rect.height - height) / 2), rect.width, height); | |
837a2f8c | 2444 | |
b052914f PT |
2445 | if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) { |
2446 | if (visible) { | |
ad128fd8 | 2447 | // Only draw the top and bottom borders |
a23cd365 MK |
2448 | gc.drawLine(drawRect.x, drawRect.y, drawRect.x + drawRect.width - 1, drawRect.y); |
2449 | gc.drawLine(drawRect.x, drawRect.y + drawRect.height - 1, drawRect.x + drawRect.width - 1, drawRect.y + drawRect.height - 1); | |
2450 | if (drawRect.width == 1) { | |
2451 | gc.drawPoint(drawRect.x, drawRect.y - 2); | |
ad128fd8 | 2452 | } |
837a2f8c | 2453 | } |
a23cd365 | 2454 | fTimeGraphProvider.postDrawEvent(event, drawRect, gc); |
b052914f PT |
2455 | return false; |
2456 | } | |
2457 | Color stateColor = null; | |
2458 | if (colorIdx < fEventColorMap.length) { | |
2459 | stateColor = fEventColorMap[colorIdx]; | |
2460 | } else { | |
2461 | stateColor = black; | |
2462 | } | |
837a2f8c | 2463 | |
b052914f PT |
2464 | boolean reallySelected = timeSelected && selected; |
2465 | // fill all rect area | |
2466 | gc.setBackground(stateColor); | |
2467 | if (visible) { | |
e77f2402 MK |
2468 | int prevAlpha = gc.getAlpha(); |
2469 | int alpha = ((int) styleMap.getOrDefault(ITimeEventStyleStrings.fillColor(), 0xff)) & 0xff; | |
2470 | gc.setAlpha(alpha); | |
a23cd365 | 2471 | gc.fillRectangle(drawRect); |
e77f2402 | 2472 | gc.setAlpha(prevAlpha); |
b052914f PT |
2473 | } else if (fBlendSubPixelEvents) { |
2474 | gc.setAlpha(128); | |
a23cd365 | 2475 | gc.fillRectangle(drawRect); |
b052914f PT |
2476 | gc.setAlpha(255); |
2477 | } | |
837a2f8c | 2478 | |
b052914f | 2479 | if (reallySelected) { |
a23cd365 MK |
2480 | gc.drawLine(drawRect.x, drawRect.y - 1, drawRect.x + drawRect.width - 1, drawRect.y - 1); |
2481 | gc.drawLine(drawRect.x, drawRect.y + drawRect.height, drawRect.x + drawRect.width - 1, drawRect.y + drawRect.height); | |
b052914f PT |
2482 | } |
2483 | if (!visible) { | |
a23cd365 | 2484 | gc.drawPoint(drawRect.x, drawRect.y - 2); |
837a2f8c | 2485 | } |
a23cd365 | 2486 | fTimeGraphProvider.postDrawEvent(event, drawRect, gc); |
ad128fd8 | 2487 | return visible; |
837a2f8c PT |
2488 | } |
2489 | ||
a0a88f65 | 2490 | /** |
83b36bb4 | 2491 | * Fill an item's states rectangle |
a0a88f65 AM |
2492 | * |
2493 | * @param rect | |
1c67fff3 | 2494 | * The states rectangle |
a0a88f65 AM |
2495 | * @param gc |
2496 | * Graphics context | |
2497 | * @param selected | |
83b36bb4 | 2498 | * true if the item is selected |
a0a88f65 | 2499 | */ |
837a2f8c | 2500 | protected void fillSpace(Rectangle rect, GC gc, boolean selected) { |
1c67fff3 PT |
2501 | /* Nothing to draw */ |
2502 | } | |
2503 | ||
2504 | /** | |
2505 | * Draw a line at the middle height of a rectangle | |
2506 | * | |
2507 | * @param rect | |
2508 | * The rectangle | |
2509 | * @param gc | |
2510 | * Graphics context | |
2511 | */ | |
2512 | private void drawMidLine(Rectangle rect, GC gc) { | |
f1fae91f | 2513 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE)); |
837a2f8c PT |
2514 | int midy = rect.y + rect.height / 2; |
2515 | gc.drawLine(rect.x, midy, rect.x + rect.width, midy); | |
2516 | } | |
2517 | ||
83d0971d PT |
2518 | private static int getMarginForHeight(int height) { |
2519 | /* | |
2520 | * State rectangle is smaller than the item bounds when height is > 4. | |
2521 | * Don't use any margin if the height is below or equal that threshold. | |
2522 | * Use a maximum of 6 pixels for both margins, otherwise try to use 13 | |
2523 | * pixels for the state height, but with a minimum margin of 1. | |
2524 | */ | |
2525 | final int MARGIN_THRESHOLD = 4; | |
2526 | final int PREFERRED_HEIGHT = 13; | |
2527 | final int MIN_MARGIN = 1; | |
2528 | final int MAX_MARGIN = 6; | |
a23cd365 | 2529 | return height <= MARGIN_THRESHOLD ? 0 : Math.max(Math.min(height - PREFERRED_HEIGHT, MAX_MARGIN), MIN_MARGIN); |
83d0971d PT |
2530 | } |
2531 | ||
2532 | private void setFontForHeight(int pixels, GC gc) { | |
2533 | /* convert font height from pixels to points */ | |
2534 | int height = Math.max(pixels * PPI / DPI, 1); | |
2535 | Font font = fFonts.get(height); | |
2536 | if (font == null) { | |
2537 | FontData fontData = gc.getFont().getFontData()[0]; | |
2538 | fontData.setHeight(height); | |
2539 | font = new Font(gc.getDevice(), fontData); | |
2540 | fFonts.put(height, font); | |
2541 | } | |
2542 | gc.setFont(font); | |
2543 | } | |
2544 | ||
837a2f8c PT |
2545 | @Override |
2546 | public void keyTraversed(TraverseEvent e) { | |
2547 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { | |
2548 | e.doit = true; | |
2549 | } | |
2550 | } | |
2551 | ||
2552 | @Override | |
2553 | public void keyPressed(KeyEvent e) { | |
2554 | int idx = -1; | |
f1fae91f | 2555 | if (fItemData.fExpandedItems.length == 0) { |
837a2f8c PT |
2556 | return; |
2557 | } | |
2558 | if (SWT.HOME == e.keyCode) { | |
2559 | idx = 0; | |
2560 | } else if (SWT.END == e.keyCode) { | |
f1fae91f | 2561 | idx = fItemData.fExpandedItems.length - 1; |
837a2f8c PT |
2562 | } else if (SWT.ARROW_DOWN == e.keyCode) { |
2563 | idx = getSelectedIndex(); | |
2564 | if (idx < 0) { | |
2565 | idx = 0; | |
f1fae91f | 2566 | } else if (idx < fItemData.fExpandedItems.length - 1) { |
837a2f8c PT |
2567 | idx++; |
2568 | } | |
2569 | } else if (SWT.ARROW_UP == e.keyCode) { | |
2570 | idx = getSelectedIndex(); | |
2571 | if (idx < 0) { | |
2572 | idx = 0; | |
2573 | } else if (idx > 0) { | |
2574 | idx--; | |
2575 | } | |
33fa1fc7 PT |
2576 | } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) { |
2577 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; | |
2578 | selectPrevEvent(extend); | |
2579 | } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) { | |
2580 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; | |
2581 | selectNextEvent(extend); | |
837a2f8c PT |
2582 | } else if (SWT.PAGE_DOWN == e.keyCode) { |
2583 | int page = countPerPage(); | |
2584 | idx = getSelectedIndex(); | |
2585 | if (idx < 0) { | |
2586 | idx = 0; | |
2587 | } | |
2588 | idx += page; | |
f1fae91f PT |
2589 | if (idx >= fItemData.fExpandedItems.length) { |
2590 | idx = fItemData.fExpandedItems.length - 1; | |
837a2f8c PT |
2591 | } |
2592 | } else if (SWT.PAGE_UP == e.keyCode) { | |
2593 | int page = countPerPage(); | |
2594 | idx = getSelectedIndex(); | |
2595 | if (idx < 0) { | |
2596 | idx = 0; | |
2597 | } | |
2598 | idx -= page; | |
2599 | if (idx < 0) { | |
2600 | idx = 0; | |
2601 | } | |
2602 | } else if (SWT.CR == e.keyCode) { | |
2603 | idx = getSelectedIndex(); | |
2604 | if (idx >= 0) { | |
f1fae91f | 2605 | if (fItemData.fExpandedItems[idx].fHasChildren) { |
837a2f8c PT |
2606 | toggle(idx); |
2607 | } else { | |
2608 | fireDefaultSelection(); | |
2609 | } | |
2610 | } | |
2611 | idx = -1; | |
3bd20aa6 | 2612 | } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) != 0)) { |
48072ae3 | 2613 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
83d0971d | 2614 | verticalZoom(true); |
48072ae3 PT |
2615 | if (fVerticalZoomAlignEntry != null) { |
2616 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2617 | } | |
3bd20aa6 | 2618 | } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) != 0)) { |
48072ae3 | 2619 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
83d0971d | 2620 | verticalZoom(false); |
48072ae3 PT |
2621 | if (fVerticalZoomAlignEntry != null) { |
2622 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2623 | } | |
3bd20aa6 | 2624 | } else if (e.character == '0' && ((e.stateMask & SWT.CTRL) != 0)) { |
48072ae3 | 2625 | fVerticalZoomAlignEntry = getVerticalZoomAlignSelection(); |
83d0971d | 2626 | resetVerticalZoom(); |
48072ae3 PT |
2627 | if (fVerticalZoomAlignEntry != null) { |
2628 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
2629 | } | |
8b516722 BH |
2630 | } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) { |
2631 | if (fHasNamespaceFocus) { | |
2632 | ITimeGraphEntry entry = getSelectedTrace(); | |
2633 | setExpandedState(entry, 0, true); | |
2634 | } else { | |
2635 | zoomIn(); | |
2636 | } | |
2637 | } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) { | |
2638 | if (fHasNamespaceFocus) { | |
2639 | ITimeGraphEntry entry = getSelectedTrace(); | |
2640 | if ((entry != null) && entry.hasChildren()) { | |
2641 | setExpandedState(entry, -1, false); | |
2642 | } | |
2643 | } else { | |
2644 | zoomOut(); | |
2645 | } | |
2646 | } else if ((e.character == '*') && ((e.stateMask & SWT.CTRL) == 0)) { | |
2647 | if (fHasNamespaceFocus) { | |
2648 | ITimeGraphEntry entry = getSelectedTrace(); | |
2649 | if ((entry != null) && entry.hasChildren()) { | |
2650 | setExpandedStateLevel(entry); | |
2651 | } | |
2652 | } | |
837a2f8c PT |
2653 | } |
2654 | if (idx >= 0) { | |
2655 | selectItem(idx, false); | |
2656 | fireSelectionChanged(); | |
2657 | } | |
0fcf3b09 PT |
2658 | int x = toControl(e.display.getCursorLocation()).x; |
2659 | updateCursor(x, e.stateMask | e.keyCode); | |
837a2f8c PT |
2660 | } |
2661 | ||
2662 | @Override | |
2663 | public void keyReleased(KeyEvent e) { | |
0fcf3b09 PT |
2664 | int x = toControl(e.display.getCursorLocation()).x; |
2665 | updateCursor(x, e.stateMask & ~e.keyCode); | |
837a2f8c PT |
2666 | } |
2667 | ||
2668 | @Override | |
2669 | public void focusGained(FocusEvent e) { | |
f1fae91f | 2670 | fIsInFocus = true; |
837a2f8c | 2671 | redraw(); |
33fa1fc7 | 2672 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
2673 | } |
2674 | ||
2675 | @Override | |
2676 | public void focusLost(FocusEvent e) { | |
f1fae91f | 2677 | fIsInFocus = false; |
f1fae91f | 2678 | if (DRAG_NONE != fDragState) { |
837a2f8c | 2679 | setCapture(false); |
f1fae91f | 2680 | fDragState = DRAG_NONE; |
837a2f8c PT |
2681 | } |
2682 | redraw(); | |
0fcf3b09 | 2683 | updateStatusLine(NO_STATUS); |
837a2f8c PT |
2684 | } |
2685 | ||
2686 | /** | |
2687 | * @return If the current view is focused | |
2688 | */ | |
2689 | public boolean isInFocus() { | |
f1fae91f | 2690 | return fIsInFocus; |
837a2f8c PT |
2691 | } |
2692 | ||
2693 | /** | |
2694 | * Provide the possibility to control the wait cursor externally e.g. data | |
2695 | * requests in progress | |
2696 | * | |
a23cd365 MK |
2697 | * @param waitInd |
2698 | * Should we wait indefinitely? | |
837a2f8c PT |
2699 | */ |
2700 | public void waitCursor(boolean waitInd) { | |
2701 | // Update cursor as indicated | |
2702 | if (waitInd) { | |
f1fae91f | 2703 | setCursor(fWaitCursor); |
837a2f8c PT |
2704 | } else { |
2705 | setCursor(null); | |
837a2f8c | 2706 | } |
0fcf3b09 | 2707 | } |
837a2f8c | 2708 | |
0fcf3b09 PT |
2709 | private void updateCursor(int x, int stateMask) { |
2710 | // if Wait cursor not active, check for the need to change the cursor | |
2711 | if (getCursor() == fWaitCursor) { | |
2712 | return; | |
2713 | } | |
2714 | Cursor cursor = null; | |
2715 | if (fDragState == DRAG_SPLIT_LINE) { | |
2716 | } else if (fDragState == DRAG_SELECTION) { | |
2717 | cursor = fResizeCursor; | |
2718 | } else if (fDragState == DRAG_TRACE_ITEM) { | |
2719 | cursor = fDragCursor; | |
2720 | } else if (fDragState == DRAG_ZOOM) { | |
2721 | cursor = fZoomCursor; | |
2722 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
2723 | cursor = fDragCursor; | |
2724 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { | |
2725 | cursor = fResizeCursor; | |
baf92cac AM |
2726 | } else if (!isOverSplitLine(x)) { |
2727 | long selectionBegin = fTimeProvider.getSelectionBegin(); | |
2728 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
2729 | int xBegin = getXForTime(selectionBegin); | |
2730 | int xEnd = getXForTime(selectionEnd); | |
2731 | if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) { | |
2732 | cursor = fResizeCursor; | |
0fcf3b09 PT |
2733 | } |
2734 | } | |
2735 | if (getCursor() != cursor) { | |
2736 | setCursor(cursor); | |
2737 | } | |
837a2f8c PT |
2738 | } |
2739 | ||
4aa2593c PT |
2740 | /** |
2741 | * Update the status line following a change of selection. | |
2742 | * | |
2743 | * @since 2.0 | |
2744 | */ | |
2745 | public void updateStatusLine() { | |
2746 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); | |
2747 | } | |
2748 | ||
0fcf3b09 | 2749 | private void updateStatusLine(int x) { |
0fab12b0 PT |
2750 | // use the time provider of the time graph scale for the status line |
2751 | ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider(); | |
2752 | if (fStatusLineManager == null || null == tdp || | |
2753 | tdp.getTime0() == tdp.getTime1()) { | |
0fcf3b09 PT |
2754 | return; |
2755 | } | |
0fab12b0 PT |
2756 | TimeFormat tf = tdp.getTimeFormat(); |
2757 | Resolution res = Resolution.NANOSEC; | |
0fcf3b09 | 2758 | StringBuilder message = new StringBuilder(); |
33fa1fc7 PT |
2759 | if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) { |
2760 | if (x != STATUS_WITHOUT_CURSOR_TIME) { | |
2761 | long time = getTimeAtX(x); | |
2762 | if (time >= 0) { | |
2763 | if (tdp instanceof ITimeDataProviderConverter) { | |
2764 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); | |
2765 | } | |
2766 | message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$ | |
0fab12b0 | 2767 | new Object[] { |
33fa1fc7 PT |
2768 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ |
2769 | Utils.formatTime(time, tf, res) | |
0fab12b0 | 2770 | })); |
0fcf3b09 PT |
2771 | } |
2772 | } | |
33fa1fc7 PT |
2773 | long selectionBegin = tdp.getSelectionBegin(); |
2774 | long selectionEnd = tdp.getSelectionEnd(); | |
2775 | message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$ | |
2776 | new Object[] { | |
2777 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$ | |
2778 | Utils.formatTime(selectionBegin, tf, res) | |
2779 | })); | |
2780 | if (selectionBegin != selectionEnd) { | |
2781 | message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$ | |
2782 | new Object[] { | |
2783 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$ | |
2784 | Utils.formatTime(selectionEnd, tf, res), | |
2785 | Utils.formatDelta(selectionEnd - selectionBegin, tf, res) | |
2786 | })); | |
2787 | } | |
0fcf3b09 | 2788 | } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) { |
85203d74 PT |
2789 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
2790 | long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); | |
0fab12b0 PT |
2791 | if (tdp instanceof ITimeDataProviderConverter) { |
2792 | time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0); | |
2793 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); | |
837a2f8c | 2794 | } |
0fab12b0 PT |
2795 | message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$ |
2796 | new Object[] { | |
85203d74 PT |
2797 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$ |
2798 | Utils.formatTime(time0, tf, res), | |
2799 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ | |
2800 | Utils.formatTime(time, tf, res), | |
2801 | Utils.formatDelta(time - time0, tf, res) | |
0fab12b0 | 2802 | })); |
837a2f8c | 2803 | } |
0fcf3b09 | 2804 | fStatusLineManager.setMessage(message.toString()); |
837a2f8c PT |
2805 | } |
2806 | ||
2807 | @Override | |
2808 | public void mouseMove(MouseEvent e) { | |
f1fae91f | 2809 | if (null == fTimeProvider) { |
837a2f8c PT |
2810 | return; |
2811 | } | |
b698ec63 | 2812 | Point size = getSize(); |
f1fae91f PT |
2813 | if (DRAG_TRACE_ITEM == fDragState) { |
2814 | int nameWidth = fTimeProvider.getNameSpace(); | |
0fcf3b09 PT |
2815 | if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) { |
2816 | fDragX = e.x; | |
f1fae91f PT |
2817 | double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak); |
2818 | long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec)); | |
2819 | long time1 = fTime1bak - timeDelta; | |
2820 | long maxTime = fTimeProvider.getMaxTime(); | |
837a2f8c PT |
2821 | if (time1 > maxTime) { |
2822 | time1 = maxTime; | |
2823 | } | |
f1fae91f PT |
2824 | long time0 = time1 - (fTime1bak - fTime0bak); |
2825 | if (time0 < fTimeProvider.getMinTime()) { | |
2826 | time0 = fTimeProvider.getMinTime(); | |
2827 | time1 = time0 + (fTime1bak - fTime0bak); | |
837a2f8c | 2828 | } |
88de10c6 | 2829 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c | 2830 | } |
f1fae91f PT |
2831 | } else if (DRAG_SPLIT_LINE == fDragState) { |
2832 | fDragX = e.x; | |
2833 | fTimeProvider.setNameSpace(e.x); | |
d2e4afa7 | 2834 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
0fcf3b09 | 2835 | } else if (DRAG_SELECTION == fDragState) { |
85203d74 PT |
2836 | if (fDragBeginMarker) { |
2837 | fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
2838 | } else { | |
2839 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
2840 | } | |
baf92cac AM |
2841 | redraw(); |
2842 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
f2ca0f69 | 2843 | fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX)); |
f1fae91f PT |
2844 | } else if (DRAG_ZOOM == fDragState) { |
2845 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
5b2b9bd7 | 2846 | redraw(); |
0fcf3b09 | 2847 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
f1fae91f | 2848 | } else if (DRAG_NONE == fDragState) { |
837a2f8c | 2849 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
f1fae91f | 2850 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
837a2f8c PT |
2851 | redraw(); |
2852 | } | |
f1fae91f | 2853 | fMouseOverSplitLine = mouseOverSplitLine; |
837a2f8c | 2854 | } |
8b516722 BH |
2855 | |
2856 | if (e.x >= fTimeProvider.getNameSpace()) { | |
2857 | fHasNamespaceFocus = false; | |
2858 | } else { | |
2859 | fHasNamespaceFocus = true; | |
2860 | } | |
0fcf3b09 PT |
2861 | updateCursor(e.x, e.stateMask); |
2862 | updateStatusLine(e.x); | |
837a2f8c PT |
2863 | } |
2864 | ||
2865 | @Override | |
2866 | public void mouseDoubleClick(MouseEvent e) { | |
f1fae91f | 2867 | if (null == fTimeProvider) { |
837a2f8c PT |
2868 | return; |
2869 | } | |
5b2b9bd7 | 2870 | if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
f1fae91f PT |
2871 | if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) { |
2872 | fTimeProvider.setNameSpace(fIdealNameSpace); | |
837a2f8c | 2873 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
f1fae91f | 2874 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
837a2f8c PT |
2875 | redraw(); |
2876 | } | |
f1fae91f | 2877 | fMouseOverSplitLine = mouseOverSplitLine; |
d2e4afa7 | 2878 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
837a2f8c PT |
2879 | return; |
2880 | } | |
2881 | int idx = getItemIndexAtY(e.y); | |
2882 | if (idx >= 0) { | |
2883 | selectItem(idx, false); | |
2884 | fireDefaultSelection(); | |
2885 | } | |
2886 | } | |
2887 | } | |
2888 | ||
2889 | @Override | |
2890 | public void mouseDown(MouseEvent e) { | |
26cf0ea8 | 2891 | if (fDragState != DRAG_NONE) { |
837a2f8c PT |
2892 | return; |
2893 | } | |
0fcf3b09 | 2894 | if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) { |
f1fae91f PT |
2895 | int nameSpace = fTimeProvider.getNameSpace(); |
2896 | if (nameSpace != 0 && isOverSplitLine(e.x)) { | |
2897 | fDragState = DRAG_SPLIT_LINE; | |
0fcf3b09 | 2898 | fDragButton = e.button; |
f1fae91f PT |
2899 | fDragX = e.x; |
2900 | fDragX0 = fDragX; | |
f1fae91f | 2901 | redraw(); |
0fcf3b09 | 2902 | updateCursor(e.x, e.stateMask); |
f1fae91f | 2903 | return; |
837a2f8c | 2904 | } |
0fcf3b09 | 2905 | } |
26cf0ea8 PT |
2906 | if (fTimeProvider == null || |
2907 | fTimeProvider.getTime0() == fTimeProvider.getTime1() || | |
2908 | getSize().x - fTimeProvider.getNameSpace() <= 0) { | |
2909 | return; | |
2910 | } | |
2911 | int idx; | |
0fcf3b09 PT |
2912 | if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) { |
2913 | int nameSpace = fTimeProvider.getNameSpace(); | |
837a2f8c PT |
2914 | idx = getItemIndexAtY(e.y); |
2915 | if (idx >= 0) { | |
f1fae91f PT |
2916 | Item item = fItemData.fExpandedItems[idx]; |
2917 | if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) { | |
837a2f8c | 2918 | toggle(idx); |
0fcf3b09 | 2919 | return; |
837a2f8c PT |
2920 | } |
2921 | selectItem(idx, false); | |
2922 | fireSelectionChanged(); | |
2923 | } else { | |
2924 | selectItem(idx, false); // clear selection | |
837a2f8c PT |
2925 | fireSelectionChanged(); |
2926 | } | |
0fcf3b09 PT |
2927 | long hitTime = getTimeAtX(e.x); |
2928 | if (hitTime >= 0) { | |
2929 | setCapture(true); | |
2930 | ||
2931 | fDragState = DRAG_SELECTION; | |
85203d74 | 2932 | fDragBeginMarker = false; |
0fcf3b09 PT |
2933 | fDragButton = e.button; |
2934 | fDragX = e.x; | |
2935 | fDragX0 = fDragX; | |
2936 | fDragTime0 = getTimeAtX(fDragX0); | |
baf92cac AM |
2937 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
2938 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
2939 | int xBegin = getXForTime(selectionBegin); | |
2940 | int xEnd = getXForTime(selectionEnd); | |
2941 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { | |
2942 | long time = getTimeAtX(e.x); | |
2943 | if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) { | |
85203d74 PT |
2944 | fDragBeginMarker = true; |
2945 | fDragX = xEnd; | |
2946 | fDragX0 = e.x; | |
baf92cac | 2947 | fDragTime0 = selectionEnd; |
0fcf3b09 | 2948 | } else { |
baf92cac AM |
2949 | fDragX0 = xBegin; |
2950 | fDragTime0 = selectionBegin; | |
2951 | } | |
2952 | } else { | |
2953 | long time = getTimeAtX(e.x); | |
2954 | if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) { | |
85203d74 PT |
2955 | fDragBeginMarker = true; |
2956 | fDragX = xEnd; | |
2957 | fDragX0 = e.x; | |
baf92cac AM |
2958 | fDragTime0 = selectionEnd; |
2959 | } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) { | |
2960 | fDragX0 = xBegin; | |
2961 | fDragTime0 = selectionBegin; | |
0fcf3b09 PT |
2962 | } |
2963 | } | |
2964 | fTime0bak = fTimeProvider.getTime0(); | |
2965 | fTime1bak = fTimeProvider.getTime1(); | |
2966 | redraw(); | |
2967 | updateCursor(e.x, e.stateMask); | |
2968 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
2969 | } | |
2970 | } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) { | |
2971 | long hitTime = getTimeAtX(e.x); | |
2972 | if (hitTime > 0) { | |
2973 | setCapture(true); | |
2974 | fDragState = DRAG_TRACE_ITEM; | |
2975 | fDragButton = e.button; | |
2976 | fDragX = e.x; | |
2977 | fDragX0 = fDragX; | |
2978 | fTime0bak = fTimeProvider.getTime0(); | |
2979 | fTime1bak = fTimeProvider.getTime1(); | |
2980 | updateCursor(e.x, e.stateMask); | |
2981 | } | |
5b2b9bd7 | 2982 | } else if (3 == e.button) { |
14b7dd60 MK |
2983 | if (e.x >= fTimeProvider.getNameSpace()) { |
2984 | setCapture(true); | |
2985 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN); | |
2986 | fDragX0 = fDragX; | |
2987 | fDragTime0 = getTimeAtX(fDragX0); | |
2988 | fDragState = DRAG_ZOOM; | |
2989 | fDragButton = e.button; | |
2990 | redraw(); | |
2991 | updateCursor(e.x, e.stateMask); | |
2992 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
2989c909 | 2993 | } else { |
a23cd365 MK |
2994 | idx = getItemIndexAtY(e.y); |
2995 | selectItem(idx, false); | |
2996 | fireSelectionChanged(); | |
14b7dd60 | 2997 | } |
837a2f8c PT |
2998 | } |
2999 | } | |
3000 | ||
3001 | @Override | |
3002 | public void mouseUp(MouseEvent e) { | |
0b5a90a0 | 3003 | if (fPendingMenuDetectEvent != null && e.button == 3) { |
e6436e5b | 3004 | if ((fDragState == DRAG_ZOOM) && isInDragZoomMargin()) { |
1dc4aa0c | 3005 | // Select entry and time event for single click |
e6436e5b | 3006 | long time = getTimeAtX(fDragX0); |
1dc4aa0c BH |
3007 | fTimeProvider.setSelectionRangeNotify(time, time, false); |
3008 | int idx = getItemIndexAtY(e.y); | |
3009 | selectItem(idx, false); | |
3010 | fireSelectionChanged(); | |
3011 | } | |
0b5a90a0 PT |
3012 | menuDetected(fPendingMenuDetectEvent); |
3013 | } | |
f1fae91f | 3014 | if (DRAG_NONE != fDragState) { |
837a2f8c | 3015 | setCapture(false); |
0fcf3b09 | 3016 | if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) { |
c089de78 | 3017 | fDragState = DRAG_NONE; |
0fcf3b09 PT |
3018 | if (fDragX != fDragX0) { |
3019 | fTimeProvider.notifyStartFinishTime(); | |
3020 | } | |
0fcf3b09 PT |
3021 | } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) { |
3022 | fDragState = DRAG_NONE; | |
3023 | redraw(); | |
a23cd365 | 3024 | } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) { |
c089de78 | 3025 | fDragState = DRAG_NONE; |
0fcf3b09 | 3026 | if (fDragX == fDragX0) { // click without selecting anything |
837a2f8c | 3027 | long time = getTimeAtX(e.x); |
f1fae91f | 3028 | fTimeProvider.setSelectedTimeNotify(time, false); |
e1725a05 | 3029 | } else { |
85203d74 PT |
3030 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
3031 | long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); | |
84c8aef7 | 3032 | fTimeProvider.setSelectionRangeNotify(time0, time1, false); |
837a2f8c | 3033 | } |
837a2f8c | 3034 | redraw(); |
0fcf3b09 PT |
3035 | fTimeGraphScale.setDragRange(-1, -1); |
3036 | } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) { | |
c089de78 | 3037 | fDragState = DRAG_NONE; |
f1fae91f | 3038 | int nameWidth = fTimeProvider.getNameSpace(); |
e6436e5b | 3039 | if ((Math.max(fDragX, fDragX0) > nameWidth) && !isInDragZoomMargin()) { |
f1fae91f PT |
3040 | long time0 = getTimeAtX(fDragX0); |
3041 | long time1 = getTimeAtX(fDragX); | |
5b2b9bd7 | 3042 | if (time0 < time1) { |
f1fae91f | 3043 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
5b2b9bd7 | 3044 | } else { |
f1fae91f | 3045 | fTimeProvider.setStartFinishTimeNotify(time1, time0); |
5b2b9bd7 PT |
3046 | } |
3047 | } else { | |
3048 | redraw(); | |
3049 | } | |
0fcf3b09 | 3050 | fTimeGraphScale.setDragRange(-1, -1); |
837a2f8c | 3051 | } |
837a2f8c | 3052 | } |
0fcf3b09 PT |
3053 | updateCursor(e.x, e.stateMask); |
3054 | updateStatusLine(e.x); | |
837a2f8c PT |
3055 | } |
3056 | ||
3057 | @Override | |
3058 | public void mouseEnter(MouseEvent e) { | |
3059 | } | |
3060 | ||
3061 | @Override | |
3062 | public void mouseExit(MouseEvent e) { | |
f1fae91f PT |
3063 | if (fMouseOverSplitLine) { |
3064 | fMouseOverSplitLine = false; | |
837a2f8c PT |
3065 | redraw(); |
3066 | } | |
33fa1fc7 | 3067 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
3068 | } |
3069 | ||
3070 | @Override | |
3071 | public void mouseHover(MouseEvent e) { | |
3072 | } | |
3073 | ||
3074 | @Override | |
3075 | public void mouseScrolled(MouseEvent e) { | |
93cfc823 | 3076 | if (fDragState != DRAG_NONE || e.count == 0) { |
837a2f8c PT |
3077 | return; |
3078 | } | |
494c9b22 PT |
3079 | |
3080 | /* | |
a23cd365 MK |
3081 | * On some platforms the mouse scroll event is sent to the control that |
3082 | * has focus even if it is not under the cursor. Handle the event only | |
3083 | * if over the time graph control. | |
494c9b22 PT |
3084 | */ |
3085 | Point size = getSize(); | |
3086 | Rectangle bounds = new Rectangle(0, 0, size.x, size.y); | |
3087 | if (!bounds.contains(e.x, e.y)) { | |
3088 | return; | |
3089 | } | |
3090 | ||
494d213d | 3091 | boolean horizontalZoom = false; |
b698ec63 | 3092 | boolean horizontalScroll = false; |
494d213d | 3093 | boolean verticalZoom = false; |
494c9b22 PT |
3094 | boolean verticalScroll = false; |
3095 | ||
3096 | // over the time graph control | |
3097 | if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) { | |
3098 | verticalZoom = true; | |
3099 | } else if (e.x < fTimeProvider.getNameSpace()) { | |
3100 | // over the name space | |
3101 | verticalScroll = true; | |
3102 | } else { | |
3103 | // over the state area | |
3104 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
3105 | // over the state area, CTRL pressed | |
dc4fa715 | 3106 | horizontalZoom = true; |
494c9b22 PT |
3107 | } else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { |
3108 | // over the state area, SHIFT pressed | |
3109 | horizontalScroll = true; | |
e5552166 | 3110 | } else { |
494c9b22 PT |
3111 | // over the state area, no modifier pressed |
3112 | verticalScroll = true; | |
837a2f8c PT |
3113 | } |
3114 | } | |
494d213d | 3115 | if (verticalZoom) { |
48072ae3 | 3116 | fVerticalZoomAlignEntry = getVerticalZoomAlignCursor(e.y); |
494c9b22 | 3117 | verticalZoom(e.count > 0); |
48072ae3 PT |
3118 | if (fVerticalZoomAlignEntry != null) { |
3119 | setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue()); | |
3120 | } | |
494d213d | 3121 | } else if (horizontalZoom && fTimeProvider.getTime0() != fTimeProvider.getTime1()) { |
494c9b22 | 3122 | zoom(e.count > 0); |
b698ec63 PT |
3123 | } else if (horizontalScroll) { |
3124 | horizontalScroll(e.count > 0); | |
a23cd365 | 3125 | } else if (verticalScroll) { |
837a2f8c PT |
3126 | setTopIndex(getTopIndex() - e.count); |
3127 | } | |
3128 | } | |
3129 | ||
48072ae3 PT |
3130 | /** |
3131 | * Get the vertical zoom alignment entry and position based on the current | |
3132 | * selection. If there is no selection or if the selection is not visible, | |
3133 | * return an alignment entry with a null time graph entry. | |
3134 | * | |
3135 | * @return a map entry where the key is the selection's time graph entry and | |
3136 | * the value is the center y-coordinate of that entry, or null | |
3137 | */ | |
3138 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignSelection() { | |
3139 | Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing(); | |
3140 | if (alignEntry != null) { | |
3141 | return alignEntry; | |
3142 | } | |
3143 | int index = getSelectedIndex(); | |
3144 | if (index == -1 || index >= getExpandedElementCount()) { | |
3145 | return new SimpleEntry<>(null, 0); | |
3146 | } | |
3147 | Rectangle bounds = getClientArea(); | |
3148 | Rectangle itemRect = getItemRect(bounds, index); | |
3149 | if (itemRect.y < bounds.y || itemRect.y > bounds.y + bounds.height) { | |
3150 | /* selection is not visible */ | |
3151 | return new SimpleEntry<>(null, 0); | |
3152 | } | |
3153 | ITimeGraphEntry entry = getExpandedElement(index); | |
3154 | int y = itemRect.y + itemRect.height / 2; | |
3155 | return new SimpleEntry<>(entry, y); | |
3156 | } | |
3157 | ||
3158 | /** | |
3159 | * Get the vertical zoom alignment entry and position at the specified | |
3160 | * cursor position. | |
3161 | * | |
3162 | * @param y | |
3163 | * the cursor y-coordinate | |
3164 | * @return a map entry where the key is the time graph entry under the | |
3165 | * cursor and the value is the cursor y-coordinate | |
3166 | */ | |
3167 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignCursor(int y) { | |
3168 | Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing(); | |
3169 | if (alignEntry != null) { | |
3170 | return alignEntry; | |
3171 | } | |
3172 | int index = getItemIndexAtY(y); | |
3173 | if (index == -1) { | |
3174 | index = getExpandedElementCount() - 1; | |
3175 | } | |
3176 | ITimeGraphEntry entry = getExpandedElement(index); | |
3177 | return new SimpleEntry<>(entry, y); | |
3178 | } | |
3179 | ||
3180 | /** | |
3181 | * Get the vertical zoom alignment entry and position if there is an ongoing | |
3182 | * one and we are within the vertical zoom delay, or otherwise return null. | |
3183 | * | |
3184 | * @return a map entry where the key is a time graph entry and the value is | |
3185 | * a y-coordinate, or null | |
3186 | */ | |
3187 | private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignOngoing() { | |
3188 | long currentTimeMillis = System.currentTimeMillis(); | |
3189 | if (currentTimeMillis < fVerticalZoomAlignTime + VERTICAL_ZOOM_DELAY) { | |
3190 | /* | |
3191 | * If the vertical zoom is triggered repeatedly in a short amount of | |
3192 | * time, use the initial event's entry and position. | |
3193 | */ | |
3194 | fVerticalZoomAlignTime = currentTimeMillis; | |
3195 | return fVerticalZoomAlignEntry; | |
3196 | } | |
3197 | fVerticalZoomAlignTime = currentTimeMillis; | |
3198 | return null; | |
3199 | } | |
3200 | ||
6b11be52 PT |
3201 | @Override |
3202 | public void handleEvent(Event event) { | |
3203 | if (event.type == SWT.MouseWheel) { | |
3204 | // prevent horizontal scrolling when the mouse wheel is used to | |
3205 | // scroll vertically or zoom | |
3206 | event.doit = false; | |
3207 | } | |
3208 | } | |
3209 | ||
837a2f8c PT |
3210 | @Override |
3211 | public int getBorderWidth() { | |
f1fae91f | 3212 | return fBorderWidth; |
837a2f8c PT |
3213 | } |
3214 | ||
3215 | /** | |
3216 | * Set the border width | |
3217 | * | |
3218 | * @param borderWidth | |
3219 | * The width | |
3220 | */ | |
3221 | public void setBorderWidth(int borderWidth) { | |
f1fae91f | 3222 | this.fBorderWidth = borderWidth; |
837a2f8c PT |
3223 | } |
3224 | ||
3225 | /** | |
3226 | * @return The current height of the header row | |
3227 | */ | |
3228 | public int getHeaderHeight() { | |
f1fae91f | 3229 | return fHeaderHeight; |
837a2f8c PT |
3230 | } |
3231 | ||
3232 | /** | |
3233 | * Set the height of the header row | |
3234 | * | |
3235 | * @param headerHeight | |
3236 | * The height | |
3237 | */ | |
3238 | public void setHeaderHeight(int headerHeight) { | |
f1fae91f | 3239 | this.fHeaderHeight = headerHeight; |
837a2f8c PT |
3240 | } |
3241 | ||
3242 | /** | |
c004295c | 3243 | * @return The default height of regular item rows |
837a2f8c PT |
3244 | */ |
3245 | public int getItemHeight() { | |
f1fae91f | 3246 | return fGlobalItemHeight; |
837a2f8c PT |
3247 | } |
3248 | ||
3249 | /** | |
c004295c | 3250 | * Set the default height of regular item rows. |
837a2f8c PT |
3251 | * |
3252 | * @param rowHeight | |
3253 | * The height | |
3254 | */ | |
3255 | public void setItemHeight(int rowHeight) { | |
f1fae91f | 3256 | this.fGlobalItemHeight = rowHeight; |
63b93cd6 PT |
3257 | for (Item item : fItemData.fItems) { |
3258 | item.fItemHeight = rowHeight; | |
3259 | } | |
837a2f8c PT |
3260 | } |
3261 | ||
c004295c PT |
3262 | /** |
3263 | * Set the height of a specific item. Overrides the default item height. | |
3264 | * | |
3265 | * @param entry | |
3266 | * A time graph entry | |
3267 | * @param rowHeight | |
3268 | * The height | |
3269 | * @return true if the height is successfully stored, false otherwise | |
c004295c PT |
3270 | */ |
3271 | public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) { | |
3272 | Item item = fItemData.findItem(entry); | |
3273 | if (item != null) { | |
3274 | item.fItemHeight = rowHeight; | |
3275 | return true; | |
3276 | } | |
3277 | return false; | |
3278 | } | |
3279 | ||
837a2f8c PT |
3280 | /** |
3281 | * Set the minimum item width | |
3282 | * | |
a23cd365 MK |
3283 | * @param width |
3284 | * The minimum width | |
837a2f8c PT |
3285 | */ |
3286 | public void setMinimumItemWidth(int width) { | |
f1fae91f | 3287 | this.fMinimumItemWidth = width; |
837a2f8c PT |
3288 | } |
3289 | ||
3290 | /** | |
3291 | * @return The minimum item width | |
3292 | */ | |
3293 | public int getMinimumItemWidth() { | |
f1fae91f | 3294 | return fMinimumItemWidth; |
837a2f8c PT |
3295 | } |
3296 | ||
b052914f PT |
3297 | /** |
3298 | * Set whether all time events with a duration shorter than one pixel should | |
3299 | * be blended in. If false, only the first such time event will be drawn and | |
3300 | * the subsequent time events in the same pixel will be discarded. The | |
3301 | * default value is false. | |
3302 | * | |
3303 | * @param blend | |
3304 | * true if sub-pixel events should be blended, false otherwise. | |
0336f981 | 3305 | * @since 1.1 |
b052914f PT |
3306 | */ |
3307 | public void setBlendSubPixelEvents(boolean blend) { | |
3308 | fBlendSubPixelEvents = blend; | |
3309 | } | |
3310 | ||
837a2f8c PT |
3311 | @Override |
3312 | public void addSelectionChangedListener(ISelectionChangedListener listener) { | |
f1fae91f PT |
3313 | if (listener != null && !fSelectionChangedListeners.contains(listener)) { |
3314 | fSelectionChangedListeners.add(listener); | |
837a2f8c PT |
3315 | } |
3316 | } | |
3317 | ||
837a2f8c PT |
3318 | @Override |
3319 | public void removeSelectionChangedListener(ISelectionChangedListener listener) { | |
3320 | if (listener != null) { | |
f1fae91f | 3321 | fSelectionChangedListeners.remove(listener); |
837a2f8c PT |
3322 | } |
3323 | } | |
3324 | ||
837a2f8c PT |
3325 | @Override |
3326 | public void setSelection(ISelection selection) { | |
07cd9368 PT |
3327 | if (selection instanceof IStructuredSelection) { |
3328 | Object ob = ((IStructuredSelection) selection).getFirstElement(); | |
837a2f8c | 3329 | if (ob instanceof ITimeGraphEntry) { |
07cd9368 | 3330 | selectItem((ITimeGraphEntry) ob, false); |
837a2f8c PT |
3331 | } |
3332 | } | |
3333 | ||
3334 | } | |
3335 | ||
6ac5a950 | 3336 | /** |
a23cd365 MK |
3337 | * @param filter |
3338 | * The filter object to be attached to the view | |
6ac5a950 | 3339 | */ |
367e2932 | 3340 | public void addFilter(@NonNull ViewerFilter filter) { |
f1fae91f PT |
3341 | if (!fFilters.contains(filter)) { |
3342 | fFilters.add(filter); | |
6ac5a950 AM |
3343 | } |
3344 | } | |
3345 | ||
3346 | /** | |
a23cd365 MK |
3347 | * @param filter |
3348 | * The filter object to be attached to the view | |
6ac5a950 | 3349 | */ |
367e2932 | 3350 | public void removeFilter(@NonNull ViewerFilter filter) { |
f1fae91f | 3351 | fFilters.remove(filter); |
6ac5a950 AM |
3352 | } |
3353 | ||
4923d7b9 PT |
3354 | /** |
3355 | * Returns this control's filters. | |
3356 | * | |
3357 | * @return an array of viewer filters | |
066b02aa | 3358 | * @since 1.2 |
4923d7b9 | 3359 | */ |
367e2932 | 3360 | public @NonNull ViewerFilter[] getFilters() { |
4923d7b9 PT |
3361 | return Iterables.toArray(fFilters, ViewerFilter.class); |
3362 | } | |
3363 | ||
3364 | /** | |
3365 | * Sets the filters, replacing any previous filters. | |
3366 | * | |
3367 | * @param filters | |
3368 | * an array of viewer filters, or null | |
066b02aa | 3369 | * @since 1.2 |
4923d7b9 | 3370 | */ |
367e2932 | 3371 | public void setFilters(@NonNull ViewerFilter[] filters) { |
4923d7b9 PT |
3372 | fFilters.clear(); |
3373 | if (filters != null) { | |
3374 | fFilters.addAll(Arrays.asList(filters)); | |
3375 | } | |
3376 | } | |
3377 | ||
496f76d3 GB |
3378 | @Override |
3379 | public void colorSettingsChanged(StateItem[] stateItems) { | |
3380 | /* Destroy previous colors from the resource manager */ | |
517fe01b EB |
3381 | if (fEventColorMap != null) { |
3382 | for (Color color : fEventColorMap) { | |
3383 | fResourceManager.destroyColor(color.getRGB()); | |
3384 | } | |
496f76d3 GB |
3385 | } |
3386 | if (stateItems != null) { | |
3387 | fEventColorMap = new Color[stateItems.length]; | |
3388 | for (int i = 0; i < stateItems.length; i++) { | |
3389 | fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor()); | |
3390 | } | |
3391 | } else { | |
a23cd365 | 3392 | fEventColorMap = new Color[] {}; |
496f76d3 GB |
3393 | } |
3394 | redraw(); | |
3395 | } | |
3396 | ||
837a2f8c | 3397 | private class ItemData { |
df0e3d5f | 3398 | private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>(); |
f1fae91f PT |
3399 | private Item[] fExpandedItems = new Item[0]; |
3400 | private Item[] fItems = new Item[0]; | |
70e10acc | 3401 | private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0]; |
507b1336 | 3402 | private List<ILinkEvent> fLinks = new ArrayList<>(); |
837a2f8c PT |
3403 | |
3404 | public ItemData() { | |
3405 | } | |
3406 | ||
70e10acc PT |
3407 | public Item findItem(ITimeGraphEntry entry) { |
3408 | return fItemMap.get(entry); | |
837a2f8c PT |
3409 | } |
3410 | ||
70e10acc PT |
3411 | public int findItemIndex(ITimeGraphEntry entry) { |
3412 | Item item = fItemMap.get(entry); | |
3413 | if (item == null) { | |
837a2f8c PT |
3414 | return -1; |
3415 | } | |
70e10acc | 3416 | return item.fExpandedIndex; |
837a2f8c PT |
3417 | } |
3418 | ||
3419 | public void refreshData() { | |
3e9a3685 | 3420 | ITimeGraphEntry selection = getSelectedTrace(); |
df0e3d5f | 3421 | Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>(); |
95d225e0 | 3422 | fMaxItemHeight = 0; |
70e10acc PT |
3423 | for (int i = 0; i < fRootEntries.length; i++) { |
3424 | ITimeGraphEntry entry = fRootEntries[i]; | |
df0e3d5f | 3425 | refreshData(itemMap, null, 0, entry); |
837a2f8c | 3426 | } |
df0e3d5f | 3427 | fItemMap = itemMap; |
70e10acc | 3428 | fItems = fItemMap.values().toArray(new Item[0]); |
837a2f8c | 3429 | updateExpandedItems(); |
3e9a3685 | 3430 | if (selection != null) { |
f1fae91f | 3431 | for (Item item : fExpandedItems) { |
70e10acc | 3432 | if (item.fEntry == selection) { |
f1fae91f | 3433 | item.fSelected = true; |
3e9a3685 PT |
3434 | break; |
3435 | } | |
3436 | } | |
3437 | } | |
837a2f8c PT |
3438 | } |
3439 | ||
70e10acc | 3440 | private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) { |
837a2f8c PT |
3441 | Item item = new Item(entry, entry.getName(), level); |
3442 | if (parent != null) { | |
f1fae91f | 3443 | parent.fChildren.add(item); |
837a2f8c | 3444 | } |
c004295c PT |
3445 | if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) { |
3446 | item.fItemHeight = fTimeGraphProvider.getItemHeight(entry); | |
3447 | } else { | |
3448 | item.fItemHeight = fGlobalItemHeight; | |
3449 | } | |
95d225e0 | 3450 | fMaxItemHeight = Math.max(fMaxItemHeight, item.fItemHeight); |
3bd20aa6 | 3451 | item.fItemHeight = Math.max(1, item.fItemHeight + fHeightAdjustment); |
70e10acc | 3452 | itemMap.put(entry, item); |
837a2f8c | 3453 | if (entry.hasChildren()) { |
df0e3d5f PT |
3454 | Item oldItem = fItemMap.get(entry); |
3455 | if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) { | |
3456 | /* existing items keep their old expanded state */ | |
3457 | item.fExpanded = oldItem.fExpanded; | |
3458 | } else { | |
a23cd365 MK |
3459 | /* |
3460 | * new items set the expanded state according to auto-expand | |
3461 | * level | |
3462 | */ | |
df0e3d5f PT |
3463 | item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel; |
3464 | } | |
f1fae91f | 3465 | item.fHasChildren = true; |
837a2f8c | 3466 | for (ITimeGraphEntry child : entry.getChildren()) { |
70e10acc | 3467 | refreshData(itemMap, item, level + 1, child); |
837a2f8c PT |
3468 | } |
3469 | } | |
3470 | } | |
3471 | ||
3472 | public void updateExpandedItems() { | |
70e10acc PT |
3473 | for (Item item : fItems) { |
3474 | item.fExpandedIndex = -1; | |
3475 | } | |
507b1336 | 3476 | List<Item> expandedItemList = new ArrayList<>(); |
70e10acc PT |
3477 | for (int i = 0; i < fRootEntries.length; i++) { |
3478 | ITimeGraphEntry entry = fRootEntries[i]; | |
837a2f8c PT |
3479 | Item item = findItem(entry); |
3480 | refreshExpanded(expandedItemList, item); | |
3481 | } | |
f1fae91f | 3482 | fExpandedItems = expandedItemList.toArray(new Item[0]); |
19ed1845 | 3483 | fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1)); |
837a2f8c PT |
3484 | } |
3485 | ||
3486 | private void refreshExpanded(List<Item> expandedItemList, Item item) { | |
6ac5a950 AM |
3487 | // Check for filters |
3488 | boolean display = true; | |
f1fae91f | 3489 | for (ViewerFilter filter : fFilters) { |
70e10acc | 3490 | if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) { |
6ac5a950 AM |
3491 | display = false; |
3492 | break; | |
3493 | } | |
3494 | } | |
3495 | if (display) { | |
70e10acc | 3496 | item.fExpandedIndex = expandedItemList.size(); |
6ac5a950 | 3497 | expandedItemList.add(item); |
f1fae91f PT |
3498 | if (item.fHasChildren && item.fExpanded) { |
3499 | for (Item child : item.fChildren) { | |
6ac5a950 AM |
3500 | refreshExpanded(expandedItemList, child); |
3501 | } | |
837a2f8c PT |
3502 | } |
3503 | } | |
3504 | } | |
3505 | ||
70e10acc PT |
3506 | public void refreshData(ITimeGraphEntry[] entries) { |
3507 | if (entries == null) { | |
70e10acc | 3508 | fRootEntries = null; |
f1fae91f | 3509 | } else { |
70e10acc | 3510 | fRootEntries = Arrays.copyOf(entries, entries.length); |
837a2f8c PT |
3511 | } |
3512 | ||
837a2f8c PT |
3513 | refreshData(); |
3514 | } | |
3515 | ||
bec1f1ac GB |
3516 | public void refreshArrows(List<ILinkEvent> events) { |
3517 | /* If links are null, reset the list */ | |
3518 | if (events != null) { | |
3519 | fLinks = events; | |
3520 | } else { | |
507b1336 | 3521 | fLinks = new ArrayList<>(); |
bec1f1ac GB |
3522 | } |
3523 | } | |
3524 | ||
70e10acc PT |
3525 | public ITimeGraphEntry[] getEntries() { |
3526 | return fRootEntries; | |
837a2f8c | 3527 | } |
837a2f8c PT |
3528 | } |
3529 | ||
3530 | private class Item { | |
f1fae91f | 3531 | private boolean fExpanded; |
70e10acc | 3532 | private int fExpandedIndex; |
f1fae91f PT |
3533 | private boolean fSelected; |
3534 | private boolean fHasChildren; | |
3535 | private int fItemHeight; | |
70e10acc PT |
3536 | private final int fLevel; |
3537 | private final List<Item> fChildren; | |
3538 | private final String fName; | |
3539 | private final ITimeGraphEntry fEntry; | |
837a2f8c | 3540 | |
70e10acc PT |
3541 | public Item(ITimeGraphEntry entry, String name, int level) { |
3542 | this.fEntry = entry; | |
f1fae91f PT |
3543 | this.fName = name; |
3544 | this.fLevel = level; | |
507b1336 | 3545 | this.fChildren = new ArrayList<>(); |
837a2f8c PT |
3546 | } |
3547 | ||
3548 | @Override | |
3549 | public String toString() { | |
f1fae91f | 3550 | return fName; |
837a2f8c PT |
3551 | } |
3552 | } | |
3553 | ||
27df1564 XR |
3554 | @Override |
3555 | public void menuDetected(MenuDetectEvent e) { | |
f1fae91f | 3556 | if (null == fTimeProvider) { |
27df1564 XR |
3557 | return; |
3558 | } | |
a974fd46 PT |
3559 | /* |
3560 | * This flag indicates if menu was prevented from being shown below and | |
3561 | * therefore must be made visible on callback from mouseUp(). | |
3562 | */ | |
3563 | boolean pendingEventCallback = fPendingMenuDetectEvent != null; | |
024658d1 | 3564 | Point p = toControl(e.x, e.y); |
a974fd46 | 3565 | if (e.detail == SWT.MENU_MOUSE && isOverTimeSpace(p.x, p.y)) { |
0b5a90a0 | 3566 | if (fPendingMenuDetectEvent == null) { |
a23cd365 MK |
3567 | /* |
3568 | * Feature in Linux. The MenuDetectEvent is received before | |
3569 | * mouseDown. Store the event and trigger it later just before | |
3570 | * handling mouseUp. This allows for the method to detect if | |
3571 | * mouse is used to drag zoom. | |
0b5a90a0 PT |
3572 | */ |
3573 | fPendingMenuDetectEvent = e; | |
024658d1 | 3574 | /* |
a974fd46 PT |
3575 | * Prevent the platform to show the menu when returning. The |
3576 | * menu will be shown (see below) when this method is called | |
3577 | * again during mouseUp(). | |
024658d1 BH |
3578 | */ |
3579 | e.doit = false; | |
0b5a90a0 PT |
3580 | return; |
3581 | } | |
3582 | fPendingMenuDetectEvent = null; | |
a974fd46 PT |
3583 | if (fDragState != DRAG_ZOOM || !isInDragZoomMargin()) { |
3584 | /* | |
3585 | * Don't show the menu on mouseUp() if a drag zoom is in | |
3586 | * progress with a drag range outside of the drag zoom margin, | |
3587 | * or if any other drag operation, or none, is in progress. | |
3588 | */ | |
3589 | e.doit = false; | |
0b5a90a0 PT |
3590 | return; |
3591 | } | |
3592 | } else { | |
f1fae91f | 3593 | if (fDragState != DRAG_NONE) { |
a974fd46 PT |
3594 | /* |
3595 | * Don't show the menu on keyboard menu or mouse menu outside of | |
3596 | * the time space if any drag operation is in progress. | |
3597 | */ | |
3598 | e.doit = false; | |
0b5a90a0 PT |
3599 | return; |
3600 | } | |
3601 | } | |
27df1564 | 3602 | int idx = getItemIndexAtY(p.y); |
f1fae91f PT |
3603 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
3604 | Item item = fItemData.fExpandedItems[idx]; | |
70e10acc | 3605 | ITimeGraphEntry entry = item.fEntry; |
9bdf1671 BH |
3606 | |
3607 | /* Send menu event for the time graph entry */ | |
3608 | e.doit = true; | |
3609 | e.data = entry; | |
3610 | fireMenuEventOnTimeGraphEntry(e); | |
3611 | Menu menu = getMenu(); | |
a974fd46 | 3612 | if (pendingEventCallback && e.doit && (menu != null)) { |
9bdf1671 BH |
3613 | menu.setVisible(true); |
3614 | } | |
3615 | ||
3616 | /* Send menu event for time event */ | |
27df1564 XR |
3617 | if (entry.hasTimeEvents()) { |
3618 | ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2); | |
3619 | if (event != null) { | |
9bdf1671 | 3620 | e.doit = true; |
27df1564 XR |
3621 | e.data = event; |
3622 | fireMenuEventOnTimeEvent(e); | |
9bdf1671 | 3623 | menu = getMenu(); |
a974fd46 | 3624 | if (pendingEventCallback && e.doit && (menu != null)) { |
024658d1 BH |
3625 | menu.setVisible(true); |
3626 | } | |
27df1564 XR |
3627 | } |
3628 | } | |
27df1564 XR |
3629 | } |
3630 | } | |
3631 | ||
d2e4afa7 MAL |
3632 | /** |
3633 | * Perform the alignment operation. | |
3634 | * | |
3635 | * @param offset | |
3636 | * the alignment offset | |
3637 | * | |
3638 | * @see ITmfTimeAligned | |
3639 | * | |
3640 | * @since 1.0 | |
3641 | */ | |
3642 | public void performAlign(int offset) { | |
3643 | fTimeProvider.setNameSpace(offset); | |
3644 | } | |
3645 | ||
3646 | /** | |
3647 | * Return the time alignment information | |
3648 | * | |
3649 | * @return the time alignment information | |
3650 | * | |
3651 | * @see ITmfTimeAligned | |
3652 | * | |
3653 | * @since 1.0 | |
3654 | */ | |
3655 | public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() { | |
3656 | return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace()); | |
3657 | } | |
e6436e5b BH |
3658 | |
3659 | private boolean isInDragZoomMargin() { | |
3660 | return (Math.abs(fDragX - fDragX0) < DRAG_MARGIN); | |
3661 | } | |
837a2f8c | 3662 | } |