Commit | Line | Data |
---|---|---|
837a2f8c | 1 | /***************************************************************************** |
10ad9fa6 | 2 | * Copyright (c) 2007, 2015 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 PT |
22 | |
23 | import java.util.ArrayList; | |
f1fae91f | 24 | import java.util.Arrays; |
2fe6a9ea | 25 | import java.util.Collections; |
837a2f8c | 26 | import java.util.Iterator; |
70e10acc | 27 | import java.util.LinkedHashMap; |
837a2f8c | 28 | import java.util.List; |
70e10acc | 29 | import java.util.Map; |
837a2f8c | 30 | |
0fcf3b09 | 31 | import org.eclipse.jface.action.IStatusLineManager; |
837a2f8c PT |
32 | import org.eclipse.jface.resource.JFaceResources; |
33 | import org.eclipse.jface.resource.LocalResourceManager; | |
f4617471 | 34 | import org.eclipse.jface.viewers.AbstractTreeViewer; |
837a2f8c PT |
35 | import org.eclipse.jface.viewers.ISelection; |
36 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
37 | import org.eclipse.jface.viewers.ISelectionProvider; | |
6ac5a950 | 38 | import org.eclipse.jface.viewers.ViewerFilter; |
0fab12b0 | 39 | import org.eclipse.osgi.util.NLS; |
837a2f8c | 40 | import org.eclipse.swt.SWT; |
837a2f8c PT |
41 | import org.eclipse.swt.events.FocusEvent; |
42 | import org.eclipse.swt.events.FocusListener; | |
43 | import org.eclipse.swt.events.KeyEvent; | |
44 | import org.eclipse.swt.events.KeyListener; | |
27df1564 XR |
45 | import org.eclipse.swt.events.MenuDetectEvent; |
46 | import org.eclipse.swt.events.MenuDetectListener; | |
837a2f8c PT |
47 | import org.eclipse.swt.events.MouseEvent; |
48 | import org.eclipse.swt.events.MouseListener; | |
49 | import org.eclipse.swt.events.MouseMoveListener; | |
50 | import org.eclipse.swt.events.MouseTrackListener; | |
51 | import org.eclipse.swt.events.MouseWheelListener; | |
52 | import org.eclipse.swt.events.PaintEvent; | |
837a2f8c PT |
53 | import org.eclipse.swt.events.SelectionListener; |
54 | import org.eclipse.swt.events.TraverseEvent; | |
55 | import org.eclipse.swt.events.TraverseListener; | |
27df1564 | 56 | import org.eclipse.swt.events.TypedEvent; |
837a2f8c PT |
57 | import org.eclipse.swt.graphics.Color; |
58 | import org.eclipse.swt.graphics.Cursor; | |
59 | import org.eclipse.swt.graphics.GC; | |
60 | import org.eclipse.swt.graphics.Image; | |
61 | import org.eclipse.swt.graphics.Point; | |
62 | import org.eclipse.swt.graphics.Rectangle; | |
63 | import org.eclipse.swt.widgets.Composite; | |
64 | import org.eclipse.swt.widgets.Display; | |
65 | import org.eclipse.swt.widgets.Event; | |
66 | import org.eclipse.swt.widgets.Listener; | |
d2e4afa7 MAL |
67 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; |
68 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo; | |
69 | import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal; | |
70 | import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned; | |
2bdf0193 AM |
71 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphColorListener; |
72 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; | |
73 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2; | |
74 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTimeListener; | |
75 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.ITimeGraphTreeListener; | |
76 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.StateItem; | |
77 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTimeEvent; | |
78 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent; | |
79 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent; | |
80 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
81 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
82 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution; | |
83 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat; | |
837a2f8c PT |
84 | |
85 | /** | |
86 | * Time graph control implementation | |
87 | * | |
837a2f8c PT |
88 | * @author Alvaro Sanchez-Leon |
89 | * @author Patrick Tasse | |
90 | */ | |
496f76d3 | 91 | public class TimeGraphControl extends TimeGraphBaseControl |
b698ec63 PT |
92 | implements FocusListener, KeyListener, MouseMoveListener, MouseListener, |
93 | MouseWheelListener, MouseTrackListener, TraverseListener, ISelectionProvider, | |
6b11be52 | 94 | MenuDetectListener, ITmfTimeGraphDrawingHelper, ITimeGraphColorListener, Listener { |
f1fae91f | 95 | |
ae09c4ad | 96 | /** Constant indicating that all levels of the time graph should be expanded */ |
f4617471 PT |
97 | public static final int ALL_LEVELS = AbstractTreeViewer.ALL_LEVELS; |
98 | ||
837a2f8c PT |
99 | private static final int DRAG_NONE = 0; |
100 | private static final int DRAG_TRACE_ITEM = 1; | |
101 | private static final int DRAG_SPLIT_LINE = 2; | |
5b2b9bd7 | 102 | private static final int DRAG_ZOOM = 3; |
0fcf3b09 | 103 | private static final int DRAG_SELECTION = 4; |
a0a88f65 | 104 | |
837a2f8c PT |
105 | private static final int CUSTOM_ITEM_HEIGHT = -1; // get item height from provider |
106 | ||
f1fae91f PT |
107 | private static final double ZOOM_FACTOR = 1.5; |
108 | private static final double ZOOM_IN_FACTOR = 0.8; | |
109 | private static final double ZOOM_OUT_FACTOR = 1.25; | |
110 | ||
0fcf3b09 | 111 | private static final int SNAP_WIDTH = 2; |
1053eaa6 | 112 | private static final int ARROW_HOVER_MAX_DIST = 5; |
0fcf3b09 PT |
113 | |
114 | private static final int NO_STATUS = -1; | |
33fa1fc7 | 115 | private static final int STATUS_WITHOUT_CURSOR_TIME = -2; |
0fcf3b09 | 116 | |
f1fae91f PT |
117 | /** Resource manager */ |
118 | private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); | |
119 | ||
120 | /** Color map for event types */ | |
121 | private Color[] fEventColorMap = null; | |
122 | ||
123 | private ITimeDataProvider fTimeProvider; | |
0fcf3b09 PT |
124 | private IStatusLineManager fStatusLineManager = null; |
125 | private TimeGraphScale fTimeGraphScale = null; | |
126 | ||
f1fae91f | 127 | private boolean fIsInFocus = false; |
f1fae91f PT |
128 | private boolean fMouseOverSplitLine = false; |
129 | private int fGlobalItemHeight = CUSTOM_ITEM_HEIGHT; | |
130 | private int fMinimumItemWidth = 0; | |
131 | private int fTopIndex = 0; | |
132 | private int fDragState = DRAG_NONE; | |
85203d74 | 133 | private boolean fDragBeginMarker = false; |
0fcf3b09 | 134 | private int fDragButton; |
f1fae91f PT |
135 | private int fDragX0 = 0; |
136 | private int fDragX = 0; | |
0fcf3b09 | 137 | private long fDragTime0 = 0; // used to preserve accuracy of modified selection |
f1fae91f PT |
138 | private int fIdealNameSpace = 0; |
139 | private long fTime0bak; | |
140 | private long fTime1bak; | |
837a2f8c | 141 | private ITimeGraphPresentationProvider fTimeGraphProvider = null; |
f1fae91f PT |
142 | private ItemData fItemData = null; |
143 | private List<SelectionListener> fSelectionListeners; | |
f2ca0f69 | 144 | private List<ITimeGraphTimeListener> fDragSelectionListeners; |
507b1336 AM |
145 | private final List<ISelectionChangedListener> fSelectionChangedListeners = new ArrayList<>(); |
146 | private final List<ITimeGraphTreeListener> fTreeListeners = new ArrayList<>(); | |
147 | private final List<MenuDetectListener> fTimeGraphEntryMenuListeners = new ArrayList<>(); | |
148 | private final List<MenuDetectListener> fTimeEventMenuListeners = new ArrayList<>(); | |
0fcf3b09 PT |
149 | private final Cursor fDragCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND); |
150 | private final Cursor fResizeCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_IBEAM); | |
151 | private final Cursor fWaitCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_WAIT); | |
152 | private final Cursor fZoomCursor = Display.getDefault().getSystemCursor(SWT.CURSOR_SIZEWE); | |
507b1336 | 153 | private final List<ViewerFilter> fFilters = new ArrayList<>(); |
0b5a90a0 | 154 | private MenuDetectEvent fPendingMenuDetectEvent = null; |
79ec0b89 | 155 | private boolean fHideArrows = false; |
f4617471 | 156 | private int fAutoExpandLevel = ALL_LEVELS; |
837a2f8c | 157 | |
f1fae91f PT |
158 | private int fBorderWidth = 0; |
159 | private int fHeaderHeight = 0; | |
837a2f8c | 160 | |
837a2f8c PT |
161 | /** |
162 | * Standard constructor | |
163 | * | |
164 | * @param parent | |
165 | * The parent composite object | |
166 | * @param colors | |
167 | * The color scheme to use | |
168 | */ | |
169 | public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) { | |
170 | ||
b698ec63 | 171 | super(parent, colors, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED); |
837a2f8c | 172 | |
f1fae91f | 173 | fItemData = new ItemData(); |
837a2f8c PT |
174 | |
175 | addFocusListener(this); | |
176 | addMouseListener(this); | |
177 | addMouseMoveListener(this); | |
178 | addMouseTrackListener(this); | |
179 | addMouseWheelListener(this); | |
180 | addTraverseListener(this); | |
181 | addKeyListener(this); | |
27df1564 | 182 | addMenuDetectListener(this); |
6b11be52 | 183 | addListener(SWT.MouseWheel, this); |
837a2f8c PT |
184 | } |
185 | ||
186 | @Override | |
187 | public void dispose() { | |
188 | super.dispose(); | |
837a2f8c PT |
189 | fResourceManager.dispose(); |
190 | } | |
191 | ||
192 | /** | |
193 | * Sets the timegraph provider used by this timegraph viewer. | |
194 | * | |
195 | * @param timeGraphProvider the timegraph provider | |
196 | */ | |
197 | public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) { | |
198 | fTimeGraphProvider = timeGraphProvider; | |
837a2f8c | 199 | |
4999a196 GB |
200 | if (timeGraphProvider instanceof ITimeGraphPresentationProvider2) { |
201 | ((ITimeGraphPresentationProvider2) timeGraphProvider).setDrawingHelper(this); | |
496f76d3 | 202 | ((ITimeGraphPresentationProvider2) timeGraphProvider).addColorListener(this); |
4999a196 GB |
203 | } |
204 | ||
837a2f8c | 205 | StateItem[] stateItems = fTimeGraphProvider.getStateTable(); |
496f76d3 | 206 | colorSettingsChanged(stateItems); |
837a2f8c PT |
207 | } |
208 | ||
5a66cf9c XR |
209 | /** |
210 | * Gets the timegraph provider used by this timegraph viewer. | |
211 | * | |
212 | * @return the timegraph provider, or <code>null</code> if not set. | |
5a66cf9c XR |
213 | */ |
214 | public ITimeGraphPresentationProvider getTimeGraphProvider() { | |
215 | return fTimeGraphProvider; | |
216 | } | |
217 | ||
218 | /** | |
219 | * Gets the color map used by this timegraph viewer. | |
220 | * | |
221 | * @return a color map, or <code>null</code> if not set. | |
5a66cf9c XR |
222 | */ |
223 | public Color[] getEventColorMap() { | |
224 | return fEventColorMap; | |
225 | } | |
226 | ||
837a2f8c PT |
227 | /** |
228 | * Assign the given time provider | |
229 | * | |
230 | * @param timeProvider | |
231 | * The time provider | |
232 | */ | |
233 | public void setTimeProvider(ITimeDataProvider timeProvider) { | |
f1fae91f | 234 | fTimeProvider = timeProvider; |
837a2f8c PT |
235 | redraw(); |
236 | } | |
237 | ||
0fcf3b09 PT |
238 | /** |
239 | * Assign the status line manager | |
240 | * | |
241 | * @param statusLineManager | |
242 | * The status line manager, or null to disable status line messages | |
0fcf3b09 PT |
243 | */ |
244 | public void setStatusLineManager(IStatusLineManager statusLineManager) { | |
245 | if (fStatusLineManager != null && statusLineManager == null) { | |
246 | fStatusLineManager.setMessage(""); //$NON-NLS-1$ | |
247 | } | |
248 | fStatusLineManager = statusLineManager; | |
249 | } | |
250 | ||
251 | /** | |
252 | * Assign the time graph scale | |
253 | * | |
254 | * @param timeGraphScale | |
255 | * The time graph scale | |
0fcf3b09 PT |
256 | */ |
257 | public void setTimeGraphScale(TimeGraphScale timeGraphScale) { | |
258 | fTimeGraphScale = timeGraphScale; | |
259 | } | |
260 | ||
837a2f8c PT |
261 | /** |
262 | * Add a selection listener | |
263 | * | |
264 | * @param listener | |
265 | * The listener to add | |
266 | */ | |
267 | public void addSelectionListener(SelectionListener listener) { | |
268 | if (listener == null) { | |
269 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
270 | } | |
f1fae91f | 271 | if (null == fSelectionListeners) { |
507b1336 | 272 | fSelectionListeners = new ArrayList<>(); |
837a2f8c | 273 | } |
f1fae91f | 274 | fSelectionListeners.add(listener); |
837a2f8c PT |
275 | } |
276 | ||
277 | /** | |
278 | * Remove a selection listener | |
279 | * | |
280 | * @param listener | |
281 | * The listener to remove | |
282 | */ | |
283 | public void removeSelectionListener(SelectionListener listener) { | |
f1fae91f PT |
284 | if (null != fSelectionListeners) { |
285 | fSelectionListeners.remove(listener); | |
837a2f8c PT |
286 | } |
287 | } | |
288 | ||
289 | /** | |
290 | * Selection changed callback | |
291 | */ | |
292 | public void fireSelectionChanged() { | |
f1fae91f PT |
293 | if (null != fSelectionListeners) { |
294 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
295 | while (it.hasNext()) { |
296 | SelectionListener listener = it.next(); | |
297 | listener.widgetSelected(null); | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | /** | |
303 | * Default selection callback | |
304 | */ | |
305 | public void fireDefaultSelection() { | |
f1fae91f PT |
306 | if (null != fSelectionListeners) { |
307 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
308 | while (it.hasNext()) { |
309 | SelectionListener listener = it.next(); | |
310 | listener.widgetDefaultSelected(null); | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
f2ca0f69 GP |
315 | /** |
316 | * Add a drag selection listener | |
317 | * | |
318 | * @param listener | |
319 | * The listener to add | |
f2ca0f69 GP |
320 | */ |
321 | public void addDragSelectionListener(ITimeGraphTimeListener listener) { | |
322 | if (listener == null) { | |
323 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
324 | } | |
325 | if (null == fDragSelectionListeners) { | |
326 | fDragSelectionListeners = new ArrayList<>(); | |
327 | } | |
328 | fDragSelectionListeners.add(listener); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Remove a drag selection listener | |
333 | * | |
334 | * @param listener | |
335 | * The listener to remove | |
f2ca0f69 GP |
336 | */ |
337 | public void removeDragSelectionListener(ITimeGraphTimeListener listener) { | |
338 | if (null != fDragSelectionListeners) { | |
339 | fDragSelectionListeners.remove(listener); | |
340 | } | |
341 | } | |
342 | ||
343 | /** | |
344 | * Drag Selection changed callback | |
345 | * | |
346 | * @param start | |
347 | * Time interval start | |
348 | * @param end | |
349 | * Time interval end | |
f2ca0f69 GP |
350 | */ |
351 | public void fireDragSelectionChanged(long start, long end) { | |
352 | // check for backward intervals | |
353 | long beginTime, endTime; | |
354 | if (start > end) { | |
355 | beginTime = end; | |
356 | endTime = start; | |
357 | } else { | |
358 | beginTime = start; | |
359 | endTime = end; | |
360 | } | |
361 | // call the listeners | |
362 | if (null != fDragSelectionListeners) { | |
363 | Iterator<ITimeGraphTimeListener> it = fDragSelectionListeners.iterator(); | |
364 | while (it.hasNext()) { | |
365 | ITimeGraphTimeListener listener = it.next(); | |
366 | listener.timeSelected(new TimeGraphTimeEvent(this, beginTime, endTime)); | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
837a2f8c PT |
371 | /** |
372 | * Get the traces in the model | |
373 | * | |
374 | * @return The array of traces | |
375 | */ | |
376 | public ITimeGraphEntry[] getTraces() { | |
70e10acc | 377 | return fItemData.getEntries(); |
837a2f8c PT |
378 | } |
379 | ||
380 | /** | |
381 | * Get the on/off trace filters | |
382 | * | |
383 | * @return The array of filters | |
384 | */ | |
385 | public boolean[] getTraceFilter() { | |
70e10acc | 386 | return fItemData.getEntryFilter(); |
837a2f8c PT |
387 | } |
388 | ||
389 | /** | |
390 | * Refresh the data for the thing | |
391 | */ | |
392 | public void refreshData() { | |
f1fae91f | 393 | fItemData.refreshData(); |
837a2f8c PT |
394 | redraw(); |
395 | } | |
396 | ||
397 | /** | |
398 | * Refresh data for the given traces | |
399 | * | |
400 | * @param traces | |
401 | * The traces to refresh | |
402 | */ | |
403 | public void refreshData(ITimeGraphEntry[] traces) { | |
f1fae91f | 404 | fItemData.refreshData(traces); |
837a2f8c PT |
405 | redraw(); |
406 | } | |
407 | ||
bec1f1ac GB |
408 | /** |
409 | * Refresh the links (arrows) of this widget | |
410 | * | |
2fe6a9ea | 411 | * @param events The link event list |
bec1f1ac GB |
412 | */ |
413 | public void refreshArrows(List<ILinkEvent> events) { | |
414 | fItemData.refreshArrows(events); | |
415 | } | |
416 | ||
2fe6a9ea PT |
417 | /** |
418 | * Get the links (arrows) of this widget | |
419 | * | |
420 | * @return The unmodifiable link event list | |
421 | * | |
422 | * @since 1.0 | |
423 | */ | |
424 | public List<ILinkEvent> getArrows() { | |
425 | return Collections.unmodifiableList(fItemData.fLinks); | |
426 | } | |
427 | ||
837a2f8c PT |
428 | boolean ensureVisibleItem(int idx, boolean redraw) { |
429 | boolean changed = false; | |
41b5c37f AM |
430 | int index = idx; |
431 | if (index < 0) { | |
f1fae91f PT |
432 | for (index = 0; index < fItemData.fExpandedItems.length; index++) { |
433 | if (fItemData.fExpandedItems[index].fSelected) { | |
837a2f8c PT |
434 | break; |
435 | } | |
436 | } | |
437 | } | |
f1fae91f | 438 | if (index >= fItemData.fExpandedItems.length) { |
837a2f8c PT |
439 | return changed; |
440 | } | |
f1fae91f | 441 | if (index < fTopIndex) { |
41b5c37f | 442 | setTopIndex(index); |
837a2f8c PT |
443 | if (redraw) { |
444 | redraw(); | |
445 | } | |
446 | changed = true; | |
447 | } else { | |
448 | int page = countPerPage(); | |
f1fae91f | 449 | if (index >= fTopIndex + page) { |
41b5c37f | 450 | setTopIndex(index - page + 1); |
837a2f8c PT |
451 | if (redraw) { |
452 | redraw(); | |
453 | } | |
454 | changed = true; | |
455 | } | |
456 | } | |
457 | return changed; | |
458 | } | |
459 | ||
460 | /** | |
461 | * Assign the given index as the top one | |
462 | * | |
463 | * @param idx | |
464 | * The index | |
465 | */ | |
466 | public void setTopIndex(int idx) { | |
f1fae91f | 467 | int index = Math.min(idx, fItemData.fExpandedItems.length - countPerPage()); |
41b5c37f | 468 | index = Math.max(0, index); |
f1fae91f | 469 | fTopIndex = index; |
837a2f8c PT |
470 | redraw(); |
471 | } | |
472 | ||
f4617471 PT |
473 | /** |
474 | * Sets the auto-expand level to be used when the entries are refreshed | |
475 | * using {@link #refreshData()} or {@link #refreshData(ITimeGraphEntry[])}. | |
476 | * The value 0 means that there is no auto-expand; 1 means that top-level | |
477 | * entries are expanded, but not their children; 2 means that top-level | |
478 | * entries are expanded, and their children, but not grand-children; and so | |
479 | * on. | |
480 | * <p> | |
481 | * The value {@link #ALL_LEVELS} means that all subtrees should be expanded. | |
482 | * </p> | |
483 | * @param level | |
484 | * non-negative level, or <code>ALL_LEVELS</code> to expand all | |
485 | * levels of the tree | |
f4617471 PT |
486 | */ |
487 | public void setAutoExpandLevel(int level) { | |
488 | fAutoExpandLevel = level; | |
489 | } | |
490 | ||
491 | /** | |
492 | * Returns the auto-expand level. | |
493 | * | |
494 | * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of | |
495 | * the tree are expanded automatically | |
496 | * @see #setAutoExpandLevel | |
f4617471 PT |
497 | */ |
498 | public int getAutoExpandLevel() { | |
499 | return fAutoExpandLevel; | |
500 | } | |
501 | ||
837a2f8c PT |
502 | /** |
503 | * Set the expanded state of a given entry | |
504 | * | |
505 | * @param entry | |
506 | * The entry | |
507 | * @param expanded | |
508 | * True if expanded, false if collapsed | |
509 | */ | |
510 | public void setExpandedState(ITimeGraphEntry entry, boolean expanded) { | |
f1fae91f PT |
511 | Item item = fItemData.findItem(entry); |
512 | if (item != null && item.fExpanded != expanded) { | |
513 | item.fExpanded = expanded; | |
514 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
515 | redraw(); |
516 | } | |
517 | } | |
518 | ||
519 | /** | |
520 | * Collapses all nodes of the viewer's tree, starting with the root. | |
837a2f8c PT |
521 | */ |
522 | public void collapseAll() { | |
f1fae91f PT |
523 | for (Item item : fItemData.fItems) { |
524 | item.fExpanded = false; | |
837a2f8c | 525 | } |
f1fae91f | 526 | fItemData.updateExpandedItems(); |
837a2f8c PT |
527 | redraw(); |
528 | } | |
529 | ||
530 | /** | |
531 | * Expands all nodes of the viewer's tree, starting with the root. | |
837a2f8c PT |
532 | */ |
533 | public void expandAll() { | |
f1fae91f PT |
534 | for (Item item : fItemData.fItems) { |
535 | item.fExpanded = true; | |
837a2f8c | 536 | } |
f1fae91f | 537 | fItemData.updateExpandedItems(); |
837a2f8c PT |
538 | redraw(); |
539 | } | |
540 | ||
541 | /** | |
542 | * Add a tree listener | |
543 | * | |
544 | * @param listener | |
545 | * The listener to add | |
546 | */ | |
547 | public void addTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
548 | if (!fTreeListeners.contains(listener)) { |
549 | fTreeListeners.add(listener); | |
837a2f8c PT |
550 | } |
551 | } | |
552 | ||
553 | /** | |
554 | * Remove a tree listener | |
555 | * | |
556 | * @param listener | |
557 | * The listener to remove | |
558 | */ | |
559 | public void removeTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
560 | if (fTreeListeners.contains(listener)) { |
561 | fTreeListeners.remove(listener); | |
837a2f8c PT |
562 | } |
563 | } | |
564 | ||
565 | /** | |
566 | * Tree event callback | |
567 | * | |
568 | * @param entry | |
569 | * The affected entry | |
570 | * @param expanded | |
571 | * The expanded state (true for expanded, false for collapsed) | |
572 | */ | |
573 | public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) { | |
574 | TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry); | |
f1fae91f | 575 | for (ITimeGraphTreeListener listener : fTreeListeners) { |
837a2f8c PT |
576 | if (expanded) { |
577 | listener.treeExpanded(event); | |
578 | } else { | |
579 | listener.treeCollapsed(event); | |
580 | } | |
581 | } | |
582 | } | |
583 | ||
27df1564 XR |
584 | /** |
585 | * Add a menu listener on {@link ITimeGraphEntry}s | |
ae09c4ad | 586 | * |
27df1564 XR |
587 | * @param listener |
588 | * The listener to add | |
27df1564 XR |
589 | */ |
590 | public void addTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
591 | if (!fTimeGraphEntryMenuListeners.contains(listener)) { |
592 | fTimeGraphEntryMenuListeners.add(listener); | |
27df1564 XR |
593 | } |
594 | } | |
595 | ||
596 | /** | |
597 | * Remove a menu listener on {@link ITimeGraphEntry}s | |
598 | * | |
599 | * @param listener | |
600 | * The listener to remove | |
27df1564 XR |
601 | */ |
602 | public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
603 | if (fTimeGraphEntryMenuListeners.contains(listener)) { |
604 | fTimeGraphEntryMenuListeners.remove(listener); | |
27df1564 XR |
605 | } |
606 | } | |
607 | ||
608 | /** | |
609 | * Menu event callback on {@link ITimeGraphEntry}s | |
610 | * | |
611 | * @param event | |
612 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry} | |
613 | */ | |
614 | private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) { | |
f1fae91f | 615 | for (MenuDetectListener listener : fTimeGraphEntryMenuListeners) { |
27df1564 XR |
616 | listener.menuDetected(event); |
617 | } | |
618 | } | |
619 | ||
620 | /** | |
621 | * Add a menu listener on {@link ITimeEvent}s | |
622 | * | |
623 | * @param listener | |
624 | * The listener to add | |
27df1564 XR |
625 | */ |
626 | public void addTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
627 | if (!fTimeEventMenuListeners.contains(listener)) { |
628 | fTimeEventMenuListeners.add(listener); | |
27df1564 XR |
629 | } |
630 | } | |
631 | ||
632 | /** | |
633 | * Remove a menu listener on {@link ITimeEvent}s | |
634 | * | |
635 | * @param listener | |
636 | * The listener to remove | |
27df1564 XR |
637 | */ |
638 | public void removeTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
639 | if (fTimeEventMenuListeners.contains(listener)) { |
640 | fTimeEventMenuListeners.remove(listener); | |
27df1564 XR |
641 | } |
642 | } | |
643 | ||
644 | /** | |
645 | * Menu event callback on {@link ITimeEvent}s | |
646 | * | |
647 | * @param event | |
648 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent} | |
649 | */ | |
650 | private void fireMenuEventOnTimeEvent(MenuDetectEvent event) { | |
f1fae91f | 651 | for (MenuDetectListener listener : fTimeEventMenuListeners) { |
27df1564 XR |
652 | listener.menuDetected(event); |
653 | } | |
654 | } | |
655 | ||
837a2f8c PT |
656 | @Override |
657 | public ISelection getSelection() { | |
658 | TimeGraphSelection sel = new TimeGraphSelection(); | |
659 | ITimeGraphEntry trace = getSelectedTrace(); | |
f1fae91f | 660 | if (null != trace && null != fTimeProvider) { |
baf92cac | 661 | long selectedTime = fTimeProvider.getSelectionBegin(); |
837a2f8c PT |
662 | ITimeEvent event = Utils.findEvent(trace, selectedTime, 0); |
663 | if (event != null) { | |
664 | sel.add(event); | |
665 | } else { | |
666 | sel.add(trace); | |
667 | } | |
668 | } | |
669 | return sel; | |
670 | } | |
671 | ||
672 | /** | |
673 | * Get the selection object | |
674 | * | |
675 | * @return The selection | |
676 | */ | |
677 | public ISelection getSelectionTrace() { | |
678 | TimeGraphSelection sel = new TimeGraphSelection(); | |
679 | ITimeGraphEntry trace = getSelectedTrace(); | |
680 | if (null != trace) { | |
681 | sel.add(trace); | |
682 | } | |
683 | return sel; | |
684 | } | |
685 | ||
686 | /** | |
687 | * Enable/disable one of the traces in the model | |
688 | * | |
689 | * @param n | |
690 | * 1 to enable it, -1 to disable. The method returns immediately | |
691 | * if another value is used. | |
692 | */ | |
693 | public void selectTrace(int n) { | |
694 | if ((n != 1) && (n != -1)) { | |
695 | return; | |
696 | } | |
697 | ||
698 | boolean changed = false; | |
699 | int lastSelection = -1; | |
f1fae91f PT |
700 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
701 | Item item = fItemData.fExpandedItems[i]; | |
702 | if (item.fSelected) { | |
837a2f8c | 703 | lastSelection = i; |
f1fae91f PT |
704 | if ((1 == n) && (i < fItemData.fExpandedItems.length - 1)) { |
705 | item.fSelected = false; | |
706 | item = fItemData.fExpandedItems[i + 1]; | |
707 | item.fSelected = true; | |
837a2f8c PT |
708 | changed = true; |
709 | } else if ((-1 == n) && (i > 0)) { | |
f1fae91f PT |
710 | item.fSelected = false; |
711 | item = fItemData.fExpandedItems[i - 1]; | |
712 | item.fSelected = true; | |
837a2f8c PT |
713 | changed = true; |
714 | } | |
715 | break; | |
716 | } | |
717 | } | |
718 | ||
f1fae91f PT |
719 | if (lastSelection < 0 && fItemData.fExpandedItems.length > 0) { |
720 | Item item = fItemData.fExpandedItems[0]; | |
721 | item.fSelected = true; | |
837a2f8c PT |
722 | changed = true; |
723 | } | |
724 | ||
725 | if (changed) { | |
726 | ensureVisibleItem(-1, false); | |
727 | redraw(); | |
728 | fireSelectionChanged(); | |
729 | } | |
730 | } | |
731 | ||
732 | /** | |
733 | * Select an event | |
734 | * | |
735 | * @param n | |
736 | * 1 for next event, -1 for previous event | |
33fa1fc7 PT |
737 | * @param extend |
738 | * true to extend selection range, false for single selection | |
739 | * @since 1.0 | |
837a2f8c | 740 | */ |
33fa1fc7 | 741 | public void selectEvent(int n, boolean extend) { |
f1fae91f | 742 | if (null == fTimeProvider) { |
837a2f8c PT |
743 | return; |
744 | } | |
745 | ITimeGraphEntry trace = getSelectedTrace(); | |
746 | if (trace == null) { | |
747 | return; | |
748 | } | |
33fa1fc7 | 749 | long selectedTime = fTimeProvider.getSelectionEnd(); |
50d36521 | 750 | long endTime = fTimeProvider.getMaxTime(); |
837a2f8c | 751 | ITimeEvent nextEvent; |
33fa1fc7 | 752 | if (n == -1 && selectedTime > endTime) { |
837a2f8c PT |
753 | nextEvent = Utils.findEvent(trace, selectedTime, 0); |
754 | } else { | |
755 | nextEvent = Utils.findEvent(trace, selectedTime, n); | |
756 | } | |
33fa1fc7 | 757 | if (null == nextEvent && n == -1) { |
837a2f8c PT |
758 | nextEvent = Utils.getFirstEvent(trace); |
759 | } | |
760 | if (null != nextEvent) { | |
761 | long nextTime = nextEvent.getTime(); | |
762 | // If last event detected e.g. going back or not moving to a next | |
763 | // event | |
764 | if (nextTime <= selectedTime && n == 1) { | |
765 | // Select to the end of this last event | |
766 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); | |
767 | // but not beyond the end of the trace | |
768 | if (nextTime > endTime) { | |
769 | nextTime = endTime; | |
770 | } | |
f1fae91f | 771 | } else if (n == -1 && nextEvent.getTime() + nextEvent.getDuration() < selectedTime) { |
837a2f8c | 772 | // for previous event go to its end time unless we were already there |
f1fae91f | 773 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
837a2f8c | 774 | } |
33fa1fc7 PT |
775 | if (extend) { |
776 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), nextTime); | |
777 | } else { | |
778 | fTimeProvider.setSelectedTimeNotify(nextTime, true); | |
779 | } | |
837a2f8c | 780 | fireSelectionChanged(); |
33fa1fc7 PT |
781 | } else if (n == 1) { |
782 | if (extend) { | |
783 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), endTime); | |
784 | } else { | |
785 | fTimeProvider.setSelectedTimeNotify(endTime, true); | |
786 | } | |
837a2f8c PT |
787 | fireSelectionChanged(); |
788 | } | |
33fa1fc7 | 789 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
790 | } |
791 | ||
792 | /** | |
793 | * Select the next event | |
33fa1fc7 PT |
794 | * |
795 | * @param extend | |
796 | * true to extend selection range, false for single selection | |
797 | * @since 1.0 | |
837a2f8c | 798 | */ |
33fa1fc7 PT |
799 | public void selectNextEvent(boolean extend) { |
800 | selectEvent(1, extend); | |
837a2f8c | 801 | // Notify if visible time window has been adjusted |
f1fae91f | 802 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
803 | } |
804 | ||
805 | /** | |
806 | * Select the previous event | |
33fa1fc7 PT |
807 | * |
808 | * @param extend | |
809 | * true to extend selection range, false for single selection | |
810 | * @since 1.0 | |
837a2f8c | 811 | */ |
33fa1fc7 PT |
812 | public void selectPrevEvent(boolean extend) { |
813 | selectEvent(-1, extend); | |
837a2f8c | 814 | // Notify if visible time window has been adjusted |
f1fae91f | 815 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
816 | } |
817 | ||
818 | /** | |
819 | * Select the next trace | |
820 | */ | |
821 | public void selectNextTrace() { | |
822 | selectTrace(1); | |
823 | } | |
824 | ||
825 | /** | |
826 | * Select the previous trace | |
827 | */ | |
828 | public void selectPrevTrace() { | |
829 | selectTrace(-1); | |
830 | } | |
831 | ||
b698ec63 PT |
832 | /** |
833 | * Scroll left or right by one half window size | |
834 | * | |
835 | * @param left | |
836 | * true to scroll left, false to scroll right | |
b698ec63 PT |
837 | */ |
838 | public void horizontalScroll(boolean left) { | |
839 | long time0 = fTimeProvider.getTime0(); | |
840 | long time1 = fTimeProvider.getTime1(); | |
841 | long timeMin = fTimeProvider.getMinTime(); | |
842 | long timeMax = fTimeProvider.getMaxTime(); | |
843 | long range = time1 - time0; | |
844 | if (range <= 0) { | |
845 | return; | |
846 | } | |
847 | long increment = Math.max(1, range / 2); | |
848 | if (left) { | |
849 | time0 = Math.max(time0 - increment, timeMin); | |
850 | time1 = time0 + range; | |
851 | } else { | |
852 | time1 = Math.min(time1 + increment, timeMax); | |
853 | time0 = time1 - range; | |
854 | } | |
855 | fTimeProvider.setStartFinishTimeNotify(time0, time1); | |
856 | } | |
857 | ||
837a2f8c PT |
858 | /** |
859 | * Zoom based on mouse cursor location with mouse scrolling | |
860 | * | |
861 | * @param zoomIn true to zoom in, false to zoom out | |
862 | */ | |
863 | public void zoom(boolean zoomIn) { | |
864 | int globalX = getDisplay().getCursorLocation().x; | |
865 | Point p = toControl(globalX, 0); | |
f1fae91f PT |
866 | int nameSpace = fTimeProvider.getNameSpace(); |
867 | int timeSpace = fTimeProvider.getTimeSpace(); | |
837a2f8c | 868 | int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x)); |
f1fae91f PT |
869 | long time0 = fTimeProvider.getTime0(); |
870 | long time1 = fTimeProvider.getTime1(); | |
837a2f8c PT |
871 | long interval = time1 - time0; |
872 | if (interval == 0) { | |
873 | interval = 1; | |
874 | } // to allow getting out of single point interval | |
875 | long newInterval; | |
876 | if (zoomIn) { | |
f1fae91f | 877 | newInterval = Math.max(Math.round(interval * ZOOM_IN_FACTOR), fTimeProvider.getMinTimeInterval()); |
837a2f8c | 878 | } else { |
f1fae91f | 879 | newInterval = (long) Math.ceil(interval * ZOOM_OUT_FACTOR); |
837a2f8c PT |
880 | } |
881 | long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval)); | |
882 | long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval); | |
883 | long newTime1 = newTime0 + newInterval; | |
10ad9fa6 | 884 | fTimeProvider.setStartFinishTimeNotify(newTime0, newTime1); |
837a2f8c PT |
885 | } |
886 | ||
887 | /** | |
888 | * zoom in using single click | |
889 | */ | |
890 | public void zoomIn() { | |
f1fae91f PT |
891 | long prevTime0 = fTimeProvider.getTime0(); |
892 | long prevTime1 = fTimeProvider.getTime1(); | |
893 | long prevRange = prevTime1 - prevTime0; | |
894 | if (prevRange == 0) { | |
3e9a3685 PT |
895 | return; |
896 | } | |
baf92cac AM |
897 | ITimeDataProvider provider = fTimeProvider; |
898 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; | |
f1fae91f PT |
899 | if (selTime <= prevTime0 || selTime >= prevTime1) { |
900 | selTime = (prevTime0 + prevTime1) / 2; | |
837a2f8c | 901 | } |
f1fae91f PT |
902 | long time0 = selTime - (long) ((selTime - prevTime0) / ZOOM_FACTOR); |
903 | long time1 = selTime + (long) ((prevTime1 - selTime) / ZOOM_FACTOR); | |
837a2f8c | 904 | |
f1fae91f | 905 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
837a2f8c PT |
906 | |
907 | if (inaccuracy > 0 && inaccuracy < 100) { | |
f1fae91f | 908 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
837a2f8c PT |
909 | return; |
910 | } | |
911 | ||
f1fae91f PT |
912 | long min = fTimeProvider.getMinTimeInterval(); |
913 | if ((time1 - time0) < min) { | |
914 | time0 = selTime - (selTime - prevTime0) * min / prevRange; | |
915 | time1 = time0 + min; | |
837a2f8c PT |
916 | } |
917 | ||
f1fae91f | 918 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
919 | } |
920 | ||
921 | /** | |
922 | * zoom out using single click | |
923 | */ | |
924 | public void zoomOut() { | |
f1fae91f PT |
925 | long prevTime0 = fTimeProvider.getTime0(); |
926 | long prevTime1 = fTimeProvider.getTime1(); | |
baf92cac AM |
927 | ITimeDataProvider provider = fTimeProvider; |
928 | long selTime = (provider.getSelectionEnd() + provider.getSelectionBegin()) / 2; | |
f1fae91f PT |
929 | if (selTime <= prevTime0 || selTime >= prevTime1) { |
930 | selTime = (prevTime0 + prevTime1) / 2; | |
837a2f8c | 931 | } |
f1fae91f PT |
932 | long time0 = (long) (selTime - (selTime - prevTime0) * ZOOM_FACTOR); |
933 | long time1 = (long) (selTime + (prevTime1 - selTime) * ZOOM_FACTOR); | |
837a2f8c | 934 | |
f1fae91f | 935 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
837a2f8c | 936 | if (inaccuracy > 0 && inaccuracy < 100) { |
f1fae91f | 937 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
837a2f8c PT |
938 | return; |
939 | } | |
940 | ||
f1fae91f | 941 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
942 | } |
943 | ||
79ec0b89 PT |
944 | /** |
945 | * Hide arrows | |
946 | * | |
947 | * @param hideArrows true to hide arrows | |
79ec0b89 PT |
948 | */ |
949 | public void hideArrows(boolean hideArrows) { | |
950 | fHideArrows = hideArrows; | |
951 | } | |
952 | ||
086f21ae PT |
953 | /** |
954 | * Follow the arrow forward | |
33fa1fc7 PT |
955 | * |
956 | * @param extend | |
957 | * true to extend selection range, false for single selection | |
958 | * @since 1.0 | |
086f21ae | 959 | */ |
33fa1fc7 | 960 | public void followArrowFwd(boolean extend) { |
086f21ae PT |
961 | ITimeGraphEntry trace = getSelectedTrace(); |
962 | if (trace == null) { | |
963 | return; | |
964 | } | |
33fa1fc7 | 965 | long selectedTime = fTimeProvider.getSelectionEnd(); |
086f21ae PT |
966 | for (ILinkEvent link : fItemData.fLinks) { |
967 | if (link.getEntry() == trace && link.getTime() == selectedTime) { | |
968 | selectItem(link.getDestinationEntry(), false); | |
969 | if (link.getDuration() != 0) { | |
33fa1fc7 PT |
970 | if (extend) { |
971 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime() + link.getDuration()); | |
972 | } else { | |
973 | fTimeProvider.setSelectedTimeNotify(link.getTime() + link.getDuration(), true); | |
974 | } | |
086f21ae PT |
975 | // Notify if visible time window has been adjusted |
976 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); | |
977 | } | |
978 | fireSelectionChanged(); | |
33fa1fc7 | 979 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
086f21ae PT |
980 | return; |
981 | } | |
982 | } | |
33fa1fc7 | 983 | selectNextEvent(extend); |
086f21ae PT |
984 | } |
985 | ||
986 | /** | |
987 | * Follow the arrow backward | |
33fa1fc7 PT |
988 | * |
989 | * @param extend | |
990 | * true to extend selection range, false for single selection | |
991 | * @since 1.0 | |
086f21ae | 992 | */ |
33fa1fc7 | 993 | public void followArrowBwd(boolean extend) { |
086f21ae PT |
994 | ITimeGraphEntry trace = getSelectedTrace(); |
995 | if (trace == null) { | |
996 | return; | |
997 | } | |
33fa1fc7 | 998 | long selectedTime = fTimeProvider.getSelectionEnd(); |
086f21ae PT |
999 | for (ILinkEvent link : fItemData.fLinks) { |
1000 | if (link.getDestinationEntry() == trace && link.getTime() + link.getDuration() == selectedTime) { | |
1001 | selectItem(link.getEntry(), false); | |
1002 | if (link.getDuration() != 0) { | |
33fa1fc7 PT |
1003 | if (extend) { |
1004 | fTimeProvider.setSelectionRangeNotify(fTimeProvider.getSelectionBegin(), link.getTime()); | |
1005 | } else { | |
1006 | fTimeProvider.setSelectedTimeNotify(link.getTime(), true); | |
1007 | } | |
086f21ae PT |
1008 | // Notify if visible time window has been adjusted |
1009 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); | |
1010 | } | |
1011 | fireSelectionChanged(); | |
33fa1fc7 | 1012 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
086f21ae PT |
1013 | return; |
1014 | } | |
1015 | } | |
33fa1fc7 | 1016 | selectPrevEvent(extend); |
086f21ae PT |
1017 | } |
1018 | ||
837a2f8c PT |
1019 | /** |
1020 | * Return the currently selected trace | |
1021 | * | |
1022 | * @return The entry matching the trace | |
1023 | */ | |
1024 | public ITimeGraphEntry getSelectedTrace() { | |
1025 | ITimeGraphEntry trace = null; | |
1026 | int idx = getSelectedIndex(); | |
1027 | if (idx >= 0) { | |
70e10acc | 1028 | trace = fItemData.fExpandedItems[idx].fEntry; |
837a2f8c PT |
1029 | } |
1030 | return trace; | |
1031 | } | |
1032 | ||
1033 | /** | |
1034 | * Retrieve the index of the currently selected item | |
1035 | * | |
1036 | * @return The index | |
1037 | */ | |
1038 | public int getSelectedIndex() { | |
1039 | int idx = -1; | |
f1fae91f PT |
1040 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1041 | Item item = fItemData.fExpandedItems[i]; | |
1042 | if (item.fSelected) { | |
837a2f8c PT |
1043 | idx = i; |
1044 | break; | |
1045 | } | |
1046 | } | |
1047 | return idx; | |
1048 | } | |
1049 | ||
1050 | boolean toggle(int idx) { | |
1051 | boolean toggled = false; | |
f1fae91f PT |
1052 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
1053 | Item item = fItemData.fExpandedItems[idx]; | |
1054 | if (item.fHasChildren) { | |
1055 | item.fExpanded = !item.fExpanded; | |
1056 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
1057 | redraw(); |
1058 | toggled = true; | |
70e10acc | 1059 | fireTreeEvent(item.fEntry, item.fExpanded); |
837a2f8c PT |
1060 | } |
1061 | } | |
1062 | return toggled; | |
1063 | } | |
1064 | ||
bedfbbb5 XR |
1065 | /** |
1066 | * Gets the index of the item at the given location. | |
1067 | * | |
1068 | * @param y | |
1069 | * the y coordinate | |
1070 | * @return the index of the item at the given location, of -1 if none. | |
bedfbbb5 XR |
1071 | */ |
1072 | protected int getItemIndexAtY(int y) { | |
837a2f8c PT |
1073 | if (y < 0) { |
1074 | return -1; | |
1075 | } | |
c004295c PT |
1076 | int ySum = 0; |
1077 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { | |
1078 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1079 | if (y < ySum) { | |
1080 | return idx; | |
837a2f8c | 1081 | } |
837a2f8c PT |
1082 | } |
1083 | return -1; | |
1084 | } | |
1085 | ||
1086 | boolean isOverSplitLine(int x) { | |
f1fae91f | 1087 | if (x < 0 || null == fTimeProvider) { |
837a2f8c PT |
1088 | return false; |
1089 | } | |
f1fae91f | 1090 | int nameWidth = fTimeProvider.getNameSpace(); |
0fcf3b09 | 1091 | return Math.abs(x - nameWidth) < SNAP_WIDTH; |
837a2f8c PT |
1092 | } |
1093 | ||
bedfbbb5 XR |
1094 | /** |
1095 | * Gets the {@link ITimeGraphEntry} at the given location. | |
1096 | * | |
1097 | * @param pt | |
1098 | * a point in the widget | |
1099 | * @return the {@link ITimeGraphEntry} at this point, or <code>null</code> | |
1100 | * if none. | |
bedfbbb5 XR |
1101 | */ |
1102 | protected ITimeGraphEntry getEntry(Point pt) { | |
837a2f8c | 1103 | int idx = getItemIndexAtY(pt.y); |
70e10acc | 1104 | return idx >= 0 ? fItemData.fExpandedItems[idx].fEntry : null; |
837a2f8c PT |
1105 | } |
1106 | ||
1053eaa6 PT |
1107 | /** |
1108 | * Return the arrow event closest to the given point that is no further than | |
1109 | * a maximum distance. | |
1110 | * | |
1111 | * @param pt | |
1112 | * a point in the widget | |
1113 | * @return The closest arrow event, or null if there is none close enough. | |
1053eaa6 PT |
1114 | */ |
1115 | protected ILinkEvent getArrow(Point pt) { | |
1116 | if (fHideArrows) { | |
1117 | return null; | |
1118 | } | |
1119 | ILinkEvent linkEvent = null; | |
1120 | double minDistance = Double.MAX_VALUE; | |
1121 | for (ILinkEvent event : fItemData.fLinks) { | |
1122 | Rectangle rect = getArrowRectangle(new Rectangle(0, 0, 0, 0), event); | |
1123 | if (rect != null) { | |
1124 | int x1 = rect.x; | |
1125 | int y1 = rect.y; | |
1126 | int x2 = x1 + rect.width; | |
1127 | int y2 = y1 + rect.height; | |
1128 | double d = Utils.distance(pt.x, pt.y, x1, y1, x2, y2); | |
1129 | if (minDistance > d) { | |
1130 | minDistance = d; | |
1131 | linkEvent = event; | |
1132 | } | |
1133 | } | |
1134 | } | |
1135 | if (minDistance <= ARROW_HOVER_MAX_DIST) { | |
1136 | return linkEvent; | |
1137 | } | |
1138 | return null; | |
1139 | } | |
1140 | ||
4999a196 | 1141 | @Override |
713a70ae | 1142 | public int getXForTime(long time) { |
f1fae91f | 1143 | if (null == fTimeProvider) { |
713a70ae PT |
1144 | return -1; |
1145 | } | |
f1fae91f PT |
1146 | long time0 = fTimeProvider.getTime0(); |
1147 | long time1 = fTimeProvider.getTime1(); | |
b698ec63 | 1148 | int width = getSize().x; |
f1fae91f | 1149 | int nameSpace = fTimeProvider.getNameSpace(); |
713a70ae PT |
1150 | double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
1151 | int x = getBounds().x + nameSpace + (int) ((time - time0) * pixelsPerNanoSec); | |
1152 | return x; | |
1153 | } | |
1154 | ||
4999a196 | 1155 | @Override |
41b5c37f | 1156 | public long getTimeAtX(int coord) { |
f1fae91f | 1157 | if (null == fTimeProvider) { |
837a2f8c PT |
1158 | return -1; |
1159 | } | |
1160 | long hitTime = -1; | |
b698ec63 | 1161 | Point size = getSize(); |
f1fae91f PT |
1162 | long time0 = fTimeProvider.getTime0(); |
1163 | long time1 = fTimeProvider.getTime1(); | |
1164 | int nameWidth = fTimeProvider.getNameSpace(); | |
41b5c37f | 1165 | final int x = coord - nameWidth; |
837a2f8c PT |
1166 | int timeWidth = size.x - nameWidth - RIGHT_MARGIN; |
1167 | if (x >= 0 && size.x >= nameWidth) { | |
1168 | if (time1 - time0 > timeWidth) { | |
1169 | // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range | |
1170 | hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth)); | |
1171 | } else { | |
1172 | // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position | |
1173 | hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth)); | |
1174 | } | |
1175 | } | |
1176 | return hitTime; | |
1177 | } | |
1178 | ||
1179 | void selectItem(int idx, boolean addSelection) { | |
1180 | boolean changed = false; | |
1181 | if (addSelection) { | |
f1fae91f PT |
1182 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
1183 | Item item = fItemData.fExpandedItems[idx]; | |
1184 | changed = !item.fSelected; | |
1185 | item.fSelected = true; | |
837a2f8c PT |
1186 | } |
1187 | } else { | |
f1fae91f PT |
1188 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
1189 | Item item = fItemData.fExpandedItems[i]; | |
1190 | if ((i == idx && !item.fSelected) || (idx == -1 && item.fSelected)) { | |
837a2f8c PT |
1191 | changed = true; |
1192 | } | |
f1fae91f | 1193 | item.fSelected = i == idx; |
837a2f8c PT |
1194 | } |
1195 | } | |
1196 | changed |= ensureVisibleItem(idx, true); | |
1197 | if (changed) { | |
1198 | redraw(); | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | /** | |
1203 | * Callback for item selection | |
1204 | * | |
1205 | * @param trace | |
1206 | * The entry matching the trace | |
1207 | * @param addSelection | |
1208 | * If the selection is added or removed | |
1209 | */ | |
1210 | public void selectItem(ITimeGraphEntry trace, boolean addSelection) { | |
f1fae91f | 1211 | int idx = fItemData.findItemIndex(trace); |
837a2f8c PT |
1212 | selectItem(idx, addSelection); |
1213 | } | |
1214 | ||
1215 | /** | |
1216 | * Retrieve the number of entries shown per page. | |
1217 | * | |
1218 | * @return The count | |
1219 | */ | |
1220 | public int countPerPage() { | |
b698ec63 | 1221 | int height = getSize().y; |
837a2f8c | 1222 | int count = 0; |
c004295c PT |
1223 | int ySum = 0; |
1224 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { | |
1225 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1226 | if (ySum >= height) { | |
1227 | return count; | |
837a2f8c | 1228 | } |
c004295c | 1229 | count++; |
837a2f8c | 1230 | } |
c004295c PT |
1231 | for (int idx = fTopIndex - 1; idx >= 0; idx--) { |
1232 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
1233 | if (ySum >= height) { | |
1234 | return count; | |
1235 | } | |
1236 | count++; | |
837a2f8c PT |
1237 | } |
1238 | return count; | |
1239 | } | |
1240 | ||
1241 | /** | |
1242 | * Get the index of the top element | |
1243 | * | |
1244 | * @return The index | |
1245 | */ | |
1246 | public int getTopIndex() { | |
f1fae91f | 1247 | return fTopIndex; |
837a2f8c PT |
1248 | } |
1249 | ||
1250 | /** | |
1251 | * Get the number of expanded items | |
1252 | * | |
1253 | * @return The count of expanded items | |
1254 | */ | |
1255 | public int getExpandedElementCount() { | |
f1fae91f | 1256 | return fItemData.fExpandedItems.length; |
837a2f8c PT |
1257 | } |
1258 | ||
1259 | /** | |
1260 | * Get an array of all expanded elements | |
1261 | * | |
1262 | * @return The expanded elements | |
1263 | */ | |
1264 | public ITimeGraphEntry[] getExpandedElements() { | |
507b1336 | 1265 | ArrayList<ITimeGraphEntry> elements = new ArrayList<>(); |
f1fae91f | 1266 | for (Item item : fItemData.fExpandedItems) { |
70e10acc | 1267 | elements.add(item.fEntry); |
837a2f8c PT |
1268 | } |
1269 | return elements.toArray(new ITimeGraphEntry[0]); | |
1270 | } | |
1271 | ||
837a2f8c | 1272 | Rectangle getNameRect(Rectangle bound, int idx, int nameWidth) { |
c004295c PT |
1273 | Rectangle rect = getStatesRect(bound, idx, nameWidth); |
1274 | rect.x = bound.x; | |
1275 | rect.width = nameWidth; | |
1276 | return rect; | |
837a2f8c PT |
1277 | } |
1278 | ||
1279 | Rectangle getStatesRect(Rectangle bound, int idx, int nameWidth) { | |
1280 | int x = bound.x + nameWidth; | |
837a2f8c | 1281 | int width = bound.width - x; |
c004295c PT |
1282 | int ySum = 0; |
1283 | if (idx >= fTopIndex) { | |
f1fae91f PT |
1284 | for (int i = fTopIndex; i < idx; i++) { |
1285 | ySum += fItemData.fExpandedItems[i].fItemHeight; | |
837a2f8c | 1286 | } |
c004295c PT |
1287 | } else { |
1288 | for (int i = fTopIndex - 1; i >= idx; i--) { | |
1289 | ySum -= fItemData.fExpandedItems[i].fItemHeight; | |
1290 | } | |
837a2f8c | 1291 | } |
c004295c PT |
1292 | int y = bound.y + ySum; |
1293 | int height = fItemData.fExpandedItems[idx].fItemHeight; | |
837a2f8c PT |
1294 | return new Rectangle(x, y, width, height); |
1295 | } | |
1296 | ||
1297 | @Override | |
1298 | void paint(Rectangle bounds, PaintEvent e) { | |
1299 | GC gc = e.gc; | |
f1fae91f | 1300 | gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.BACKGROUND)); |
837a2f8c PT |
1301 | drawBackground(gc, bounds.x, bounds.y, bounds.width, bounds.height); |
1302 | ||
f1fae91f | 1303 | if (bounds.width < 2 || bounds.height < 2 || null == fTimeProvider) { |
837a2f8c PT |
1304 | return; |
1305 | } | |
1306 | ||
f1fae91f PT |
1307 | fIdealNameSpace = 0; |
1308 | int nameSpace = fTimeProvider.getNameSpace(); | |
837a2f8c PT |
1309 | |
1310 | // draw empty name space background | |
f1fae91f | 1311 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); |
837a2f8c PT |
1312 | drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height); |
1313 | ||
0fcf3b09 | 1314 | // draw items |
f1fae91f | 1315 | drawItems(bounds, fTimeProvider, fItemData.fExpandedItems, fTopIndex, nameSpace, gc); |
bec1f1ac GB |
1316 | drawLinks(bounds, fTimeProvider, fItemData.fLinks, nameSpace, gc); |
1317 | fTimeGraphProvider.postDrawControl(bounds, gc); | |
837a2f8c | 1318 | |
0fcf3b09 PT |
1319 | int alpha = gc.getAlpha(); |
1320 | gc.setAlpha(100); | |
1321 | ||
f1fae91f PT |
1322 | long time0 = fTimeProvider.getTime0(); |
1323 | long time1 = fTimeProvider.getTime1(); | |
baf92cac AM |
1324 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
1325 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
837a2f8c | 1326 | double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
0fcf3b09 PT |
1327 | int x0 = bounds.x + nameSpace + (int) ((selectionBegin - time0) * pixelsPerNanoSec); |
1328 | int x1 = bounds.x + nameSpace + (int) ((selectionEnd - time0) * pixelsPerNanoSec); | |
1329 | ||
1330 | // draw selection lines | |
1331 | if (fDragState != DRAG_SELECTION) { | |
f1fae91f | 1332 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); |
0fcf3b09 PT |
1333 | if (x0 >= nameSpace && x0 < bounds.x + bounds.width) { |
1334 | gc.drawLine(x0, bounds.y, x0, bounds.y + bounds.height); | |
1335 | } | |
1336 | if (x1 != x0) { | |
1337 | if (x1 >= nameSpace && x1 < bounds.x + bounds.width) { | |
1338 | gc.drawLine(x1, bounds.y, x1, bounds.y + bounds.height); | |
1339 | } | |
1340 | } | |
837a2f8c PT |
1341 | } |
1342 | ||
0fcf3b09 PT |
1343 | // draw selection background |
1344 | if (selectionBegin != 0 && selectionEnd != 0 && fDragState != DRAG_SELECTION) { | |
1345 | x0 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x0)); | |
1346 | x1 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x1)); | |
1347 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1348 | if (x1 - x0 > 1) { | |
1349 | gc.fillRectangle(new Rectangle(x0 + 1, bounds.y, x1 - x0 - 1, bounds.height)); | |
1350 | } else if (x0 - x1 > 1) { | |
1351 | gc.fillRectangle(new Rectangle(x1 + 1, bounds.y, x0 - x1 - 1, bounds.height)); | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | // draw drag selection background | |
1356 | if (fDragState == DRAG_ZOOM || fDragState == DRAG_SELECTION) { | |
1357 | gc.setBackground(getColorScheme().getBkColor(false, false, true)); | |
1358 | if (fDragX0 < fDragX) { | |
1359 | gc.fillRectangle(new Rectangle(fDragX0, bounds.y, fDragX - fDragX0, bounds.height)); | |
1360 | } else if (fDragX0 > fDragX) { | |
1361 | gc.fillRectangle(new Rectangle(fDragX, bounds.y, fDragX0 - fDragX, bounds.height)); | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | // draw drag line | |
f1fae91f PT |
1366 | if (DRAG_SPLIT_LINE == fDragState) { |
1367 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.BLACK)); | |
837a2f8c | 1368 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
0fcf3b09 | 1369 | } else if (DRAG_ZOOM == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { |
f1fae91f PT |
1370 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND)); |
1371 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); | |
0fcf3b09 PT |
1372 | if (fDragX != fDragX0) { |
1373 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); | |
1374 | } | |
1375 | } else if (DRAG_SELECTION == fDragState && Math.max(fDragX, fDragX0) > nameSpace) { | |
1376 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME)); | |
1377 | gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1); | |
1378 | if (fDragX != fDragX0) { | |
1379 | gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1); | |
1380 | } | |
f1fae91f PT |
1381 | } else if (DRAG_NONE == fDragState && fMouseOverSplitLine && fTimeProvider.getNameSpace() > 0) { |
1382 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.RED)); | |
837a2f8c PT |
1383 | gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1); |
1384 | } | |
0fcf3b09 PT |
1385 | |
1386 | gc.setAlpha(alpha); | |
837a2f8c PT |
1387 | } |
1388 | ||
1389 | /** | |
1390 | * Draw many items at once | |
1391 | * | |
1392 | * @param bounds | |
1393 | * The rectangle of the area | |
1394 | * @param timeProvider | |
1395 | * The time provider | |
1396 | * @param items | |
1397 | * The array items to draw | |
1398 | * @param topIndex | |
1399 | * The index of the first element to draw | |
1400 | * @param nameSpace | |
1401 | * The width reserved for the names | |
1402 | * @param gc | |
1403 | * Reference to the SWT GC object | |
1404 | */ | |
1405 | public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider, | |
1406 | Item[] items, int topIndex, int nameSpace, GC gc) { | |
1407 | for (int i = topIndex; i < items.length; i++) { | |
1408 | Item item = items[i]; | |
1409 | drawItem(item, bounds, timeProvider, i, nameSpace, gc); | |
1410 | } | |
837a2f8c PT |
1411 | } |
1412 | ||
1413 | /** | |
1414 | * Draws the item | |
1415 | * | |
1416 | * @param item the item to draw | |
1417 | * @param bounds the container rectangle | |
a0a88f65 | 1418 | * @param timeProvider Time provider |
837a2f8c PT |
1419 | * @param i the item index |
1420 | * @param nameSpace the name space | |
a0a88f65 | 1421 | * @param gc Graphics context |
837a2f8c PT |
1422 | */ |
1423 | protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) { | |
70e10acc | 1424 | ITimeGraphEntry entry = item.fEntry; |
837a2f8c PT |
1425 | long time0 = timeProvider.getTime0(); |
1426 | long time1 = timeProvider.getTime1(); | |
33fa1fc7 | 1427 | long selectedTime = fTimeProvider.getSelectionEnd(); |
837a2f8c PT |
1428 | |
1429 | Rectangle nameRect = getNameRect(bounds, i, nameSpace); | |
1430 | if (nameRect.y >= bounds.y + bounds.height) { | |
1431 | return; | |
1432 | } | |
1433 | ||
70e10acc | 1434 | if (! item.fEntry.hasTimeEvents()) { |
837a2f8c PT |
1435 | Rectangle statesRect = getStatesRect(bounds, i, nameSpace); |
1436 | nameRect.width += statesRect.width; | |
1437 | drawName(item, nameRect, gc); | |
1438 | } else { | |
1439 | drawName(item, nameRect, gc); | |
1440 | } | |
1441 | Rectangle rect = getStatesRect(bounds, i, nameSpace); | |
1442 | if (rect.isEmpty()) { | |
1443 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); | |
1444 | return; | |
1445 | } | |
1446 | if (time1 <= time0) { | |
f1fae91f | 1447 | gc.setBackground(getColorScheme().getBkColor(false, false, false)); |
837a2f8c PT |
1448 | gc.fillRectangle(rect); |
1449 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); | |
1450 | return; | |
1451 | } | |
1452 | ||
1453 | // Initialize _rect1 to same values as enclosing rectangle rect | |
1454 | Rectangle stateRect = Utils.clone(rect); | |
f1fae91f | 1455 | boolean selected = item.fSelected; |
837a2f8c PT |
1456 | // K pixels per second |
1457 | double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0); | |
1458 | ||
70e10acc | 1459 | if (item.fEntry.hasTimeEvents()) { |
ab0a54d5 | 1460 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
837a2f8c PT |
1461 | fillSpace(rect, gc, selected); |
1462 | // Drawing rectangle is smaller than reserved space | |
1463 | stateRect.y += 3; | |
1464 | stateRect.height -= 6; | |
1465 | ||
1466 | long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace(); | |
1467 | Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration); | |
1468 | ||
1469 | int lastX = -1; | |
1470 | while (iterator.hasNext()) { | |
1471 | ITimeEvent event = iterator.next(); | |
1472 | int x = rect.x + (int) ((event.getTime() - time0) * pixelsPerNanoSec); | |
1473 | int xEnd = rect.x + (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec); | |
1474 | if (x >= rect.x + rect.width || xEnd < rect.x) { | |
1475 | // event is out of bounds | |
1476 | continue; | |
1477 | } | |
1478 | xEnd = Math.min(rect.x + rect.width, xEnd); | |
1479 | stateRect.x = Math.max(rect.x, x); | |
1480 | stateRect.width = Math.max(0, xEnd - stateRect.x + 1); | |
1481 | if (stateRect.x == lastX) { | |
1482 | stateRect.width -= 1; | |
1483 | if (stateRect.width > 0) { | |
1484 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); | |
1485 | gc.drawPoint(stateRect.x, stateRect.y - 2); | |
1486 | stateRect.x += 1; | |
1487 | } | |
837a2f8c PT |
1488 | } |
1489 | boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration(); | |
f1fae91f | 1490 | if (drawState(getColorScheme(), event, stateRect, gc, selected, timeSelected)) { |
ad128fd8 PT |
1491 | lastX = x; |
1492 | } | |
837a2f8c | 1493 | } |
ab0a54d5 | 1494 | gc.setClipping((Rectangle) null); |
837a2f8c PT |
1495 | } |
1496 | fTimeGraphProvider.postDrawEntry(entry, rect, gc); | |
1497 | } | |
1498 | ||
bec1f1ac GB |
1499 | /** |
1500 | * Draw the links | |
1501 | * | |
1502 | * @param bounds | |
1503 | * The rectangle of the area | |
1504 | * @param timeProvider | |
1505 | * The time provider | |
1506 | * @param links | |
1507 | * The array items to draw | |
1508 | * @param nameSpace | |
1509 | * The width reserved for the names | |
1510 | * @param gc | |
1511 | * Reference to the SWT GC object | |
bec1f1ac GB |
1512 | */ |
1513 | public void drawLinks(Rectangle bounds, ITimeDataProvider timeProvider, | |
1514 | List<ILinkEvent> links, int nameSpace, GC gc) { | |
79ec0b89 PT |
1515 | if (fHideArrows) { |
1516 | return; | |
1517 | } | |
ab0a54d5 | 1518 | gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height)); |
bec1f1ac GB |
1519 | for (ILinkEvent event : links) { |
1520 | drawLink(event, bounds, timeProvider, nameSpace, gc); | |
1521 | } | |
ab0a54d5 | 1522 | gc.setClipping((Rectangle) null); |
bec1f1ac GB |
1523 | } |
1524 | ||
1525 | /** | |
1526 | * Draws the link type events of this item | |
1527 | * | |
1528 | * @param event | |
1529 | * the item to draw | |
1530 | * @param bounds | |
1531 | * the container rectangle | |
1532 | * @param timeProvider | |
1533 | * Time provider | |
1534 | * @param nameSpace | |
1535 | * the name space | |
1536 | * @param gc | |
1537 | * Graphics context | |
bec1f1ac GB |
1538 | */ |
1539 | protected void drawLink(ILinkEvent event, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) { | |
1053eaa6 PT |
1540 | drawArrow(getColorScheme(), event, getArrowRectangle(bounds, event), gc); |
1541 | } | |
1542 | ||
1543 | private Rectangle getArrowRectangle(Rectangle bounds, ILinkEvent event) { | |
bec1f1ac GB |
1544 | int srcIndex = fItemData.findItemIndex(event.getEntry()); |
1545 | int destIndex = fItemData.findItemIndex(event.getDestinationEntry()); | |
1546 | ||
1547 | if ((srcIndex == -1) || (destIndex == -1)) { | |
1053eaa6 | 1548 | return null; |
bec1f1ac GB |
1549 | } |
1550 | ||
1053eaa6 PT |
1551 | Rectangle src = getStatesRect(bounds, srcIndex, fTimeProvider.getNameSpace()); |
1552 | Rectangle dst = getStatesRect(bounds, destIndex, fTimeProvider.getNameSpace()); | |
bec1f1ac GB |
1553 | |
1554 | int x0 = getXForTime(event.getTime()); | |
1555 | int x1 = getXForTime(event.getTime() + event.getDuration()); | |
03ab8eeb PT |
1556 | |
1557 | // limit the x-coordinates to prevent integer overflow in calculations | |
1558 | // and also GC.drawLine doesn't draw properly with large coordinates | |
1559 | final int limit = Integer.MAX_VALUE / 1024; | |
1560 | x0 = Math.max(-limit, Math.min(x0, limit)); | |
1561 | x1 = Math.max(-limit, Math.min(x1, limit)); | |
1562 | ||
bec1f1ac GB |
1563 | int y0 = src.y + src.height / 2; |
1564 | int y1 = dst.y + dst.height / 2; | |
1053eaa6 | 1565 | return new Rectangle(x0, y0, x1 - x0, y1 - y0); |
bec1f1ac GB |
1566 | } |
1567 | ||
1568 | /** | |
1569 | * Draw the state (color fill) | |
1570 | * | |
1571 | * @param colors | |
1572 | * Color scheme | |
1573 | * @param event | |
1574 | * Time event for which we're drawing the state | |
1575 | * @param rect | |
1576 | * Where to draw | |
1577 | * @param gc | |
1578 | * Graphics context | |
1579 | * @return true if the state was drawn | |
bec1f1ac GB |
1580 | */ |
1581 | protected boolean drawArrow(TimeGraphColorScheme colors, ITimeEvent event, | |
1582 | Rectangle rect, GC gc) { | |
1583 | ||
a5e66312 PT |
1584 | if (rect == null) { |
1585 | return false; | |
1586 | } | |
bec1f1ac GB |
1587 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); |
1588 | if (colorIdx < 0) { | |
1589 | return false; | |
1590 | } | |
1591 | boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true; | |
1592 | ||
1593 | if (visible) { | |
1594 | Color stateColor = null; | |
1595 | if (colorIdx < fEventColorMap.length) { | |
1596 | stateColor = fEventColorMap[colorIdx]; | |
1597 | } else { | |
1598 | stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); | |
1599 | } | |
1600 | ||
1601 | gc.setForeground(stateColor); | |
1602 | gc.setBackground(stateColor); | |
1603 | ||
1604 | /* Draw the arrow */ | |
1605 | gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); | |
1606 | drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc); | |
1607 | ||
1608 | } | |
1609 | fTimeGraphProvider.postDrawEvent(event, rect, gc); | |
1610 | return visible; | |
1611 | } | |
1612 | ||
1613 | /* | |
1614 | * @author Francis Giraldeau | |
1615 | * | |
1616 | * Inspiration: | |
1617 | * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm | |
1618 | * | |
1619 | * The algorithm was taken from this site, not the code itself | |
1620 | */ | |
1621 | private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc) | |
1622 | { | |
1623 | int factor = 10; | |
1624 | double cos = 0.9510; | |
1625 | double sin = 0.3090; | |
03ab8eeb PT |
1626 | long lenx = x1 - x0; |
1627 | long leny = y1 - y0; | |
bec1f1ac GB |
1628 | double len = Math.sqrt(lenx * lenx + leny * leny); |
1629 | ||
1630 | double dx = factor * lenx / len; | |
1631 | double dy = factor * leny / len; | |
1632 | int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin))); | |
1633 | int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos))); | |
1634 | int end2X = (int) Math.round((x1 - (dx * cos + dy * sin))); | |
1635 | int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos))); | |
1636 | int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 }; | |
1637 | gc.fillPolygon(arrow); | |
1638 | } | |
1639 | ||
a0a88f65 AM |
1640 | /** |
1641 | * Draw the name of an item. | |
1642 | * | |
1643 | * @param item | |
1644 | * Item object | |
1645 | * @param bounds | |
1646 | * Where to draw the name | |
1647 | * @param gc | |
1648 | * Graphics context | |
1649 | */ | |
837a2f8c | 1650 | protected void drawName(Item item, Rectangle bounds, GC gc) { |
70e10acc | 1651 | boolean hasTimeEvents = item.fEntry.hasTimeEvents(); |
837a2f8c | 1652 | if (! hasTimeEvents) { |
f1fae91f | 1653 | gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus)); |
837a2f8c | 1654 | gc.fillRectangle(bounds); |
f1fae91f PT |
1655 | if (item.fSelected && fIsInFocus) { |
1656 | gc.setForeground(getColorScheme().getBkColor(item.fSelected, fIsInFocus, false)); | |
837a2f8c PT |
1657 | gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1); |
1658 | } | |
1659 | } else { | |
f1fae91f PT |
1660 | gc.setBackground(getColorScheme().getBkColor(item.fSelected, fIsInFocus, true)); |
1661 | gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus)); | |
837a2f8c PT |
1662 | gc.fillRectangle(bounds); |
1663 | } | |
1664 | ||
1665 | // No name to be drawn | |
f1fae91f | 1666 | if (fTimeProvider.getNameSpace() == 0) { |
837a2f8c PT |
1667 | return; |
1668 | } | |
1669 | ||
f1fae91f PT |
1670 | int leftMargin = MARGIN + item.fLevel * EXPAND_SIZE; |
1671 | if (item.fHasChildren) { | |
1672 | gc.setForeground(getColorScheme().getFgColorGroup(false, false)); | |
1673 | gc.setBackground(getColorScheme().getBkColor(false, false, false)); | |
837a2f8c PT |
1674 | Rectangle rect = Utils.clone(bounds); |
1675 | rect.x += leftMargin; | |
1676 | rect.y += (bounds.height - EXPAND_SIZE) / 2; | |
1677 | rect.width = EXPAND_SIZE; | |
1678 | rect.height = EXPAND_SIZE; | |
1679 | gc.fillRectangle(rect); | |
1680 | gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1); | |
1681 | int midy = rect.y + rect.height / 2; | |
1682 | gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy); | |
f1fae91f | 1683 | if (!item.fExpanded) { |
837a2f8c PT |
1684 | int midx = rect.x + rect.width / 2; |
1685 | gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3); | |
1686 | } | |
1687 | } | |
1688 | leftMargin += EXPAND_SIZE + MARGIN; | |
1689 | ||
70e10acc | 1690 | Image img = fTimeGraphProvider.getItemImage(item.fEntry); |
837a2f8c PT |
1691 | if (img != null) { |
1692 | // draw icon | |
1693 | int imgHeight = img.getImageData().height; | |
1694 | int imgWidth = img.getImageData().width; | |
1695 | int x = leftMargin; | |
1696 | int y = bounds.y + (bounds.height - imgHeight) / 2; | |
1697 | gc.drawImage(img, x, y); | |
1698 | leftMargin += imgWidth + MARGIN; | |
1699 | } | |
f1fae91f | 1700 | String name = item.fName; |
837a2f8c | 1701 | Point size = gc.stringExtent(name); |
f1fae91f PT |
1702 | if (fIdealNameSpace < leftMargin + size.x + MARGIN) { |
1703 | fIdealNameSpace = leftMargin + size.x + MARGIN; | |
837a2f8c PT |
1704 | } |
1705 | if (hasTimeEvents) { | |
1706 | // cut long string with "..." | |
1707 | int width = bounds.width - leftMargin; | |
1708 | int cuts = 0; | |
1709 | while (size.x > width && name.length() > 1) { | |
1710 | cuts++; | |
1711 | name = name.substring(0, name.length() - 1); | |
1712 | size = gc.stringExtent(name + "..."); //$NON-NLS-1$ | |
1713 | } | |
1714 | if (cuts > 0) { | |
1715 | name += "..."; //$NON-NLS-1$ | |
1716 | } | |
1717 | } | |
1718 | Rectangle rect = Utils.clone(bounds); | |
1719 | rect.x += leftMargin; | |
1720 | rect.width -= leftMargin; | |
1721 | // draw text | |
1722 | if (rect.width > 0) { | |
1723 | rect.y += (bounds.height - gc.stringExtent(name).y) / 2; | |
f1fae91f | 1724 | gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus)); |
837a2f8c PT |
1725 | int textWidth = Utils.drawText(gc, name, rect, true); |
1726 | leftMargin += textWidth + MARGIN; | |
1727 | rect.y -= 2; | |
1728 | ||
1729 | if (hasTimeEvents) { | |
1730 | // draw middle line | |
1731 | int x = bounds.x + leftMargin; | |
1732 | int width = bounds.width - x; | |
1733 | int midy = bounds.y + bounds.height / 2; | |
f1fae91f | 1734 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE)); |
837a2f8c PT |
1735 | gc.drawLine(x, midy, x + width, midy); |
1736 | } | |
1737 | } | |
1738 | } | |
1739 | ||
a0a88f65 AM |
1740 | /** |
1741 | * Draw the state (color fill) | |
1742 | * | |
1743 | * @param colors | |
1744 | * Color scheme | |
1745 | * @param event | |
1746 | * Time event for which we're drawing the state | |
1747 | * @param rect | |
1748 | * Where to draw | |
1749 | * @param gc | |
1750 | * Graphics context | |
1751 | * @param selected | |
1752 | * Is this time event currently selected (so it appears | |
1753 | * highlighted) | |
1754 | * @param timeSelected | |
1755 | * Is the timestamp currently selected | |
ad128fd8 | 1756 | * @return true if the state was drawn |
a0a88f65 | 1757 | */ |
ad128fd8 | 1758 | protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event, |
837a2f8c PT |
1759 | Rectangle rect, GC gc, boolean selected, boolean timeSelected) { |
1760 | ||
1761 | int colorIdx = fTimeGraphProvider.getStateTableIndex(event); | |
ad128fd8 PT |
1762 | if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) { |
1763 | return false; | |
837a2f8c PT |
1764 | } |
1765 | boolean visible = rect.width == 0 ? false : true; | |
351a2391 XR |
1766 | Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK); |
1767 | gc.setForeground(black); | |
837a2f8c PT |
1768 | |
1769 | if (visible) { | |
ad128fd8 PT |
1770 | if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) { |
1771 | // Only draw the top and bottom borders | |
ad128fd8 PT |
1772 | gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y); |
1773 | gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1); | |
1774 | if (rect.width == 1) { | |
ad128fd8 PT |
1775 | gc.drawPoint(rect.x, rect.y - 2); |
1776 | } | |
8b4293b5 | 1777 | fTimeGraphProvider.postDrawEvent(event, rect, gc); |
ad128fd8 PT |
1778 | return false; |
1779 | } | |
837a2f8c PT |
1780 | Color stateColor = null; |
1781 | if (colorIdx < fEventColorMap.length) { | |
1782 | stateColor = fEventColorMap[colorIdx]; | |
1783 | } else { | |
351a2391 | 1784 | stateColor = black; |
837a2f8c PT |
1785 | } |
1786 | ||
41b5c37f | 1787 | boolean reallySelected = timeSelected && selected; |
837a2f8c PT |
1788 | // fill all rect area |
1789 | gc.setBackground(stateColor); | |
1790 | gc.fillRectangle(rect); | |
837a2f8c | 1791 | |
351a2391 XR |
1792 | if (reallySelected) { |
1793 | gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1); | |
1794 | gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height); | |
837a2f8c PT |
1795 | } |
1796 | } else { | |
837a2f8c | 1797 | gc.drawPoint(rect.x, rect.y - 2); |
837a2f8c PT |
1798 | } |
1799 | fTimeGraphProvider.postDrawEvent(event, rect, gc); | |
ad128fd8 | 1800 | return visible; |
837a2f8c PT |
1801 | } |
1802 | ||
a0a88f65 AM |
1803 | /** |
1804 | * Fill the space between two contiguous time events | |
1805 | * | |
1806 | * @param rect | |
1807 | * Rectangle to fill | |
1808 | * @param gc | |
1809 | * Graphics context | |
1810 | * @param selected | |
1811 | * Is this time event selected or not | |
1812 | */ | |
837a2f8c | 1813 | protected void fillSpace(Rectangle rect, GC gc, boolean selected) { |
f1fae91f | 1814 | gc.setBackground(getColorScheme().getBkColor(selected, fIsInFocus, false)); |
837a2f8c | 1815 | gc.fillRectangle(rect); |
f1fae91f PT |
1816 | if (fDragState == DRAG_ZOOM) { |
1817 | gc.setBackground(getColorScheme().getBkColor(selected, fIsInFocus, true)); | |
1818 | if (fDragX0 < fDragX) { | |
1819 | gc.fillRectangle(new Rectangle(fDragX0, rect.y, fDragX - fDragX0, rect.height)); | |
1820 | } else if (fDragX0 > fDragX) { | |
1821 | gc.fillRectangle(new Rectangle(fDragX, rect.y, fDragX0 - fDragX, rect.height)); | |
5b2b9bd7 PT |
1822 | } |
1823 | } | |
837a2f8c | 1824 | // draw middle line |
f1fae91f | 1825 | gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE)); |
837a2f8c PT |
1826 | int midy = rect.y + rect.height / 2; |
1827 | gc.drawLine(rect.x, midy, rect.x + rect.width, midy); | |
1828 | } | |
1829 | ||
1830 | @Override | |
1831 | public void keyTraversed(TraverseEvent e) { | |
1832 | if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) { | |
1833 | e.doit = true; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | @Override | |
1838 | public void keyPressed(KeyEvent e) { | |
1839 | int idx = -1; | |
f1fae91f | 1840 | if (fItemData.fExpandedItems.length == 0) { |
837a2f8c PT |
1841 | return; |
1842 | } | |
1843 | if (SWT.HOME == e.keyCode) { | |
1844 | idx = 0; | |
1845 | } else if (SWT.END == e.keyCode) { | |
f1fae91f | 1846 | idx = fItemData.fExpandedItems.length - 1; |
837a2f8c PT |
1847 | } else if (SWT.ARROW_DOWN == e.keyCode) { |
1848 | idx = getSelectedIndex(); | |
1849 | if (idx < 0) { | |
1850 | idx = 0; | |
f1fae91f | 1851 | } else if (idx < fItemData.fExpandedItems.length - 1) { |
837a2f8c PT |
1852 | idx++; |
1853 | } | |
1854 | } else if (SWT.ARROW_UP == e.keyCode) { | |
1855 | idx = getSelectedIndex(); | |
1856 | if (idx < 0) { | |
1857 | idx = 0; | |
1858 | } else if (idx > 0) { | |
1859 | idx--; | |
1860 | } | |
33fa1fc7 PT |
1861 | } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) { |
1862 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; | |
1863 | selectPrevEvent(extend); | |
1864 | } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) { | |
1865 | boolean extend = (e.stateMask & SWT.SHIFT) != 0; | |
1866 | selectNextEvent(extend); | |
837a2f8c PT |
1867 | } else if (SWT.PAGE_DOWN == e.keyCode) { |
1868 | int page = countPerPage(); | |
1869 | idx = getSelectedIndex(); | |
1870 | if (idx < 0) { | |
1871 | idx = 0; | |
1872 | } | |
1873 | idx += page; | |
f1fae91f PT |
1874 | if (idx >= fItemData.fExpandedItems.length) { |
1875 | idx = fItemData.fExpandedItems.length - 1; | |
837a2f8c PT |
1876 | } |
1877 | } else if (SWT.PAGE_UP == e.keyCode) { | |
1878 | int page = countPerPage(); | |
1879 | idx = getSelectedIndex(); | |
1880 | if (idx < 0) { | |
1881 | idx = 0; | |
1882 | } | |
1883 | idx -= page; | |
1884 | if (idx < 0) { | |
1885 | idx = 0; | |
1886 | } | |
1887 | } else if (SWT.CR == e.keyCode) { | |
1888 | idx = getSelectedIndex(); | |
1889 | if (idx >= 0) { | |
f1fae91f | 1890 | if (fItemData.fExpandedItems[idx].fHasChildren) { |
837a2f8c PT |
1891 | toggle(idx); |
1892 | } else { | |
1893 | fireDefaultSelection(); | |
1894 | } | |
1895 | } | |
1896 | idx = -1; | |
1897 | } | |
1898 | if (idx >= 0) { | |
1899 | selectItem(idx, false); | |
1900 | fireSelectionChanged(); | |
1901 | } | |
0fcf3b09 PT |
1902 | int x = toControl(e.display.getCursorLocation()).x; |
1903 | updateCursor(x, e.stateMask | e.keyCode); | |
837a2f8c PT |
1904 | } |
1905 | ||
1906 | @Override | |
1907 | public void keyReleased(KeyEvent e) { | |
0fcf3b09 PT |
1908 | int x = toControl(e.display.getCursorLocation()).x; |
1909 | updateCursor(x, e.stateMask & ~e.keyCode); | |
837a2f8c PT |
1910 | } |
1911 | ||
1912 | @Override | |
1913 | public void focusGained(FocusEvent e) { | |
f1fae91f | 1914 | fIsInFocus = true; |
837a2f8c | 1915 | redraw(); |
33fa1fc7 | 1916 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
1917 | } |
1918 | ||
1919 | @Override | |
1920 | public void focusLost(FocusEvent e) { | |
f1fae91f | 1921 | fIsInFocus = false; |
f1fae91f | 1922 | if (DRAG_NONE != fDragState) { |
837a2f8c | 1923 | setCapture(false); |
f1fae91f | 1924 | fDragState = DRAG_NONE; |
837a2f8c PT |
1925 | } |
1926 | redraw(); | |
0fcf3b09 | 1927 | updateStatusLine(NO_STATUS); |
837a2f8c PT |
1928 | } |
1929 | ||
1930 | /** | |
1931 | * @return If the current view is focused | |
1932 | */ | |
1933 | public boolean isInFocus() { | |
f1fae91f | 1934 | return fIsInFocus; |
837a2f8c PT |
1935 | } |
1936 | ||
1937 | /** | |
1938 | * Provide the possibility to control the wait cursor externally e.g. data | |
1939 | * requests in progress | |
1940 | * | |
1941 | * @param waitInd Should we wait indefinitely? | |
1942 | */ | |
1943 | public void waitCursor(boolean waitInd) { | |
1944 | // Update cursor as indicated | |
1945 | if (waitInd) { | |
f1fae91f | 1946 | setCursor(fWaitCursor); |
837a2f8c PT |
1947 | } else { |
1948 | setCursor(null); | |
837a2f8c | 1949 | } |
0fcf3b09 | 1950 | } |
837a2f8c | 1951 | |
0fcf3b09 PT |
1952 | private void updateCursor(int x, int stateMask) { |
1953 | // if Wait cursor not active, check for the need to change the cursor | |
1954 | if (getCursor() == fWaitCursor) { | |
1955 | return; | |
1956 | } | |
1957 | Cursor cursor = null; | |
1958 | if (fDragState == DRAG_SPLIT_LINE) { | |
1959 | } else if (fDragState == DRAG_SELECTION) { | |
1960 | cursor = fResizeCursor; | |
1961 | } else if (fDragState == DRAG_TRACE_ITEM) { | |
1962 | cursor = fDragCursor; | |
1963 | } else if (fDragState == DRAG_ZOOM) { | |
1964 | cursor = fZoomCursor; | |
1965 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
1966 | cursor = fDragCursor; | |
1967 | } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { | |
1968 | cursor = fResizeCursor; | |
baf92cac AM |
1969 | } else if (!isOverSplitLine(x)) { |
1970 | long selectionBegin = fTimeProvider.getSelectionBegin(); | |
1971 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
1972 | int xBegin = getXForTime(selectionBegin); | |
1973 | int xEnd = getXForTime(selectionEnd); | |
1974 | if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) { | |
1975 | cursor = fResizeCursor; | |
0fcf3b09 PT |
1976 | } |
1977 | } | |
1978 | if (getCursor() != cursor) { | |
1979 | setCursor(cursor); | |
1980 | } | |
837a2f8c PT |
1981 | } |
1982 | ||
0fcf3b09 | 1983 | private void updateStatusLine(int x) { |
0fab12b0 PT |
1984 | // use the time provider of the time graph scale for the status line |
1985 | ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider(); | |
1986 | if (fStatusLineManager == null || null == tdp || | |
1987 | tdp.getTime0() == tdp.getTime1()) { | |
0fcf3b09 PT |
1988 | return; |
1989 | } | |
0fab12b0 PT |
1990 | TimeFormat tf = tdp.getTimeFormat(); |
1991 | Resolution res = Resolution.NANOSEC; | |
0fcf3b09 | 1992 | StringBuilder message = new StringBuilder(); |
33fa1fc7 PT |
1993 | if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) { |
1994 | if (x != STATUS_WITHOUT_CURSOR_TIME) { | |
1995 | long time = getTimeAtX(x); | |
1996 | if (time >= 0) { | |
1997 | if (tdp instanceof ITimeDataProviderConverter) { | |
1998 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); | |
1999 | } | |
2000 | message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$ | |
0fab12b0 | 2001 | new Object[] { |
33fa1fc7 PT |
2002 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ |
2003 | Utils.formatTime(time, tf, res) | |
0fab12b0 | 2004 | })); |
0fcf3b09 PT |
2005 | } |
2006 | } | |
33fa1fc7 PT |
2007 | long selectionBegin = tdp.getSelectionBegin(); |
2008 | long selectionEnd = tdp.getSelectionEnd(); | |
2009 | message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$ | |
2010 | new Object[] { | |
2011 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$ | |
2012 | Utils.formatTime(selectionBegin, tf, res) | |
2013 | })); | |
2014 | if (selectionBegin != selectionEnd) { | |
2015 | message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$ | |
2016 | new Object[] { | |
2017 | tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$ | |
2018 | Utils.formatTime(selectionEnd, tf, res), | |
2019 | Utils.formatDelta(selectionEnd - selectionBegin, tf, res) | |
2020 | })); | |
2021 | } | |
0fcf3b09 | 2022 | } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) { |
85203d74 PT |
2023 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
2024 | long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); | |
0fab12b0 PT |
2025 | if (tdp instanceof ITimeDataProviderConverter) { |
2026 | time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0); | |
2027 | time = ((ITimeDataProviderConverter) tdp).convertTime(time); | |
837a2f8c | 2028 | } |
0fab12b0 PT |
2029 | message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$ |
2030 | new Object[] { | |
85203d74 PT |
2031 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$ |
2032 | Utils.formatTime(time0, tf, res), | |
2033 | tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$ | |
2034 | Utils.formatTime(time, tf, res), | |
2035 | Utils.formatDelta(time - time0, tf, res) | |
0fab12b0 | 2036 | })); |
837a2f8c | 2037 | } |
0fcf3b09 | 2038 | fStatusLineManager.setMessage(message.toString()); |
837a2f8c PT |
2039 | } |
2040 | ||
2041 | @Override | |
2042 | public void mouseMove(MouseEvent e) { | |
f1fae91f | 2043 | if (null == fTimeProvider) { |
837a2f8c PT |
2044 | return; |
2045 | } | |
b698ec63 | 2046 | Point size = getSize(); |
f1fae91f PT |
2047 | if (DRAG_TRACE_ITEM == fDragState) { |
2048 | int nameWidth = fTimeProvider.getNameSpace(); | |
0fcf3b09 PT |
2049 | if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) { |
2050 | fDragX = e.x; | |
f1fae91f PT |
2051 | double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak); |
2052 | long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec)); | |
2053 | long time1 = fTime1bak - timeDelta; | |
2054 | long maxTime = fTimeProvider.getMaxTime(); | |
837a2f8c PT |
2055 | if (time1 > maxTime) { |
2056 | time1 = maxTime; | |
2057 | } | |
f1fae91f PT |
2058 | long time0 = time1 - (fTime1bak - fTime0bak); |
2059 | if (time0 < fTimeProvider.getMinTime()) { | |
2060 | time0 = fTimeProvider.getMinTime(); | |
2061 | time1 = time0 + (fTime1bak - fTime0bak); | |
837a2f8c | 2062 | } |
f1fae91f | 2063 | fTimeProvider.setStartFinishTime(time0, time1); |
837a2f8c | 2064 | } |
f1fae91f PT |
2065 | } else if (DRAG_SPLIT_LINE == fDragState) { |
2066 | fDragX = e.x; | |
2067 | fTimeProvider.setNameSpace(e.x); | |
d2e4afa7 | 2068 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
0fcf3b09 | 2069 | } else if (DRAG_SELECTION == fDragState) { |
85203d74 PT |
2070 | if (fDragBeginMarker) { |
2071 | fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
2072 | } else { | |
2073 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
2074 | } | |
baf92cac AM |
2075 | redraw(); |
2076 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
f2ca0f69 | 2077 | fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX)); |
f1fae91f PT |
2078 | } else if (DRAG_ZOOM == fDragState) { |
2079 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN); | |
5b2b9bd7 | 2080 | redraw(); |
0fcf3b09 | 2081 | fTimeGraphScale.setDragRange(fDragX0, fDragX); |
f1fae91f | 2082 | } else if (DRAG_NONE == fDragState) { |
837a2f8c | 2083 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
f1fae91f | 2084 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
837a2f8c PT |
2085 | redraw(); |
2086 | } | |
f1fae91f | 2087 | fMouseOverSplitLine = mouseOverSplitLine; |
837a2f8c | 2088 | } |
0fcf3b09 PT |
2089 | updateCursor(e.x, e.stateMask); |
2090 | updateStatusLine(e.x); | |
837a2f8c PT |
2091 | } |
2092 | ||
2093 | @Override | |
2094 | public void mouseDoubleClick(MouseEvent e) { | |
f1fae91f | 2095 | if (null == fTimeProvider) { |
837a2f8c PT |
2096 | return; |
2097 | } | |
5b2b9bd7 | 2098 | if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) { |
f1fae91f PT |
2099 | if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) { |
2100 | fTimeProvider.setNameSpace(fIdealNameSpace); | |
837a2f8c | 2101 | boolean mouseOverSplitLine = isOverSplitLine(e.x); |
f1fae91f | 2102 | if (fMouseOverSplitLine != mouseOverSplitLine) { |
837a2f8c PT |
2103 | redraw(); |
2104 | } | |
f1fae91f | 2105 | fMouseOverSplitLine = mouseOverSplitLine; |
d2e4afa7 | 2106 | TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo())); |
837a2f8c PT |
2107 | return; |
2108 | } | |
2109 | int idx = getItemIndexAtY(e.y); | |
2110 | if (idx >= 0) { | |
2111 | selectItem(idx, false); | |
2112 | fireDefaultSelection(); | |
2113 | } | |
2114 | } | |
2115 | } | |
2116 | ||
2117 | @Override | |
2118 | public void mouseDown(MouseEvent e) { | |
407bfdd5 PT |
2119 | if (fDragState != DRAG_NONE || null == fTimeProvider || |
2120 | fTimeProvider.getTime0() == fTimeProvider.getTime1() || | |
b698ec63 | 2121 | getSize().x - fTimeProvider.getNameSpace() <= 0) { |
837a2f8c PT |
2122 | return; |
2123 | } | |
2124 | int idx; | |
0fcf3b09 | 2125 | if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) { |
f1fae91f PT |
2126 | int nameSpace = fTimeProvider.getNameSpace(); |
2127 | if (nameSpace != 0 && isOverSplitLine(e.x)) { | |
2128 | fDragState = DRAG_SPLIT_LINE; | |
0fcf3b09 | 2129 | fDragButton = e.button; |
f1fae91f PT |
2130 | fDragX = e.x; |
2131 | fDragX0 = fDragX; | |
2132 | fTime0bak = fTimeProvider.getTime0(); | |
2133 | fTime1bak = fTimeProvider.getTime1(); | |
2134 | redraw(); | |
0fcf3b09 | 2135 | updateCursor(e.x, e.stateMask); |
f1fae91f | 2136 | return; |
837a2f8c | 2137 | } |
0fcf3b09 PT |
2138 | } |
2139 | if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) { | |
2140 | int nameSpace = fTimeProvider.getNameSpace(); | |
837a2f8c PT |
2141 | idx = getItemIndexAtY(e.y); |
2142 | if (idx >= 0) { | |
f1fae91f PT |
2143 | Item item = fItemData.fExpandedItems[idx]; |
2144 | if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) { | |
837a2f8c | 2145 | toggle(idx); |
0fcf3b09 | 2146 | return; |
837a2f8c PT |
2147 | } |
2148 | selectItem(idx, false); | |
2149 | fireSelectionChanged(); | |
2150 | } else { | |
2151 | selectItem(idx, false); // clear selection | |
837a2f8c PT |
2152 | fireSelectionChanged(); |
2153 | } | |
0fcf3b09 PT |
2154 | long hitTime = getTimeAtX(e.x); |
2155 | if (hitTime >= 0) { | |
2156 | setCapture(true); | |
2157 | ||
2158 | fDragState = DRAG_SELECTION; | |
85203d74 | 2159 | fDragBeginMarker = false; |
0fcf3b09 PT |
2160 | fDragButton = e.button; |
2161 | fDragX = e.x; | |
2162 | fDragX0 = fDragX; | |
2163 | fDragTime0 = getTimeAtX(fDragX0); | |
baf92cac AM |
2164 | long selectionBegin = fTimeProvider.getSelectionBegin(); |
2165 | long selectionEnd = fTimeProvider.getSelectionEnd(); | |
2166 | int xBegin = getXForTime(selectionBegin); | |
2167 | int xEnd = getXForTime(selectionEnd); | |
2168 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) { | |
2169 | long time = getTimeAtX(e.x); | |
2170 | if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) { | |
85203d74 PT |
2171 | fDragBeginMarker = true; |
2172 | fDragX = xEnd; | |
2173 | fDragX0 = e.x; | |
baf92cac | 2174 | fDragTime0 = selectionEnd; |
0fcf3b09 | 2175 | } else { |
baf92cac AM |
2176 | fDragX0 = xBegin; |
2177 | fDragTime0 = selectionBegin; | |
2178 | } | |
2179 | } else { | |
2180 | long time = getTimeAtX(e.x); | |
2181 | if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) { | |
85203d74 PT |
2182 | fDragBeginMarker = true; |
2183 | fDragX = xEnd; | |
2184 | fDragX0 = e.x; | |
baf92cac AM |
2185 | fDragTime0 = selectionEnd; |
2186 | } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) { | |
2187 | fDragX0 = xBegin; | |
2188 | fDragTime0 = selectionBegin; | |
0fcf3b09 PT |
2189 | } |
2190 | } | |
2191 | fTime0bak = fTimeProvider.getTime0(); | |
2192 | fTime1bak = fTimeProvider.getTime1(); | |
2193 | redraw(); | |
2194 | updateCursor(e.x, e.stateMask); | |
2195 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
2196 | } | |
2197 | } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) { | |
2198 | long hitTime = getTimeAtX(e.x); | |
2199 | if (hitTime > 0) { | |
2200 | setCapture(true); | |
2201 | fDragState = DRAG_TRACE_ITEM; | |
2202 | fDragButton = e.button; | |
2203 | fDragX = e.x; | |
2204 | fDragX0 = fDragX; | |
2205 | fTime0bak = fTimeProvider.getTime0(); | |
2206 | fTime1bak = fTimeProvider.getTime1(); | |
2207 | updateCursor(e.x, e.stateMask); | |
2208 | } | |
5b2b9bd7 | 2209 | } else if (3 == e.button) { |
5b2b9bd7 | 2210 | setCapture(true); |
b698ec63 | 2211 | fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN); |
f1fae91f | 2212 | fDragX0 = fDragX; |
0fab12b0 | 2213 | fDragTime0 = getTimeAtX(fDragX0); |
f1fae91f | 2214 | fDragState = DRAG_ZOOM; |
0fcf3b09 PT |
2215 | fDragButton = e.button; |
2216 | redraw(); | |
2217 | updateCursor(e.x, e.stateMask); | |
2218 | fTimeGraphScale.setDragRange(fDragX0, fDragX); | |
837a2f8c PT |
2219 | } |
2220 | } | |
2221 | ||
2222 | @Override | |
2223 | public void mouseUp(MouseEvent e) { | |
0b5a90a0 PT |
2224 | if (fPendingMenuDetectEvent != null && e.button == 3) { |
2225 | menuDetected(fPendingMenuDetectEvent); | |
2226 | } | |
f1fae91f | 2227 | if (DRAG_NONE != fDragState) { |
837a2f8c | 2228 | setCapture(false); |
0fcf3b09 PT |
2229 | if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) { |
2230 | if (fDragX != fDragX0) { | |
2231 | fTimeProvider.notifyStartFinishTime(); | |
2232 | } | |
2233 | fDragState = DRAG_NONE; | |
2234 | } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) { | |
2235 | fDragState = DRAG_NONE; | |
2236 | redraw(); | |
2237 | } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) { | |
2238 | if (fDragX == fDragX0) { // click without selecting anything | |
837a2f8c | 2239 | long time = getTimeAtX(e.x); |
f1fae91f | 2240 | fTimeProvider.setSelectedTimeNotify(time, false); |
e1725a05 | 2241 | } else { |
85203d74 PT |
2242 | long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0; |
2243 | long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX); | |
2244 | fTimeProvider.setSelectionRangeNotify(time0, time1); | |
837a2f8c | 2245 | } |
f1fae91f | 2246 | fDragState = DRAG_NONE; |
837a2f8c | 2247 | redraw(); |
0fcf3b09 PT |
2248 | fTimeGraphScale.setDragRange(-1, -1); |
2249 | } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) { | |
f1fae91f PT |
2250 | int nameWidth = fTimeProvider.getNameSpace(); |
2251 | if (Math.max(fDragX, fDragX0) > nameWidth && fDragX != fDragX0) { | |
2252 | long time0 = getTimeAtX(fDragX0); | |
2253 | long time1 = getTimeAtX(fDragX); | |
5b2b9bd7 | 2254 | if (time0 < time1) { |
f1fae91f | 2255 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
5b2b9bd7 | 2256 | } else { |
f1fae91f | 2257 | fTimeProvider.setStartFinishTimeNotify(time1, time0); |
5b2b9bd7 PT |
2258 | } |
2259 | } else { | |
2260 | redraw(); | |
2261 | } | |
f1fae91f | 2262 | fDragState = DRAG_NONE; |
0fcf3b09 | 2263 | fTimeGraphScale.setDragRange(-1, -1); |
837a2f8c | 2264 | } |
837a2f8c | 2265 | } |
0fcf3b09 PT |
2266 | updateCursor(e.x, e.stateMask); |
2267 | updateStatusLine(e.x); | |
837a2f8c PT |
2268 | } |
2269 | ||
2270 | @Override | |
2271 | public void mouseEnter(MouseEvent e) { | |
2272 | } | |
2273 | ||
2274 | @Override | |
2275 | public void mouseExit(MouseEvent e) { | |
f1fae91f PT |
2276 | if (fMouseOverSplitLine) { |
2277 | fMouseOverSplitLine = false; | |
837a2f8c PT |
2278 | redraw(); |
2279 | } | |
33fa1fc7 | 2280 | updateStatusLine(STATUS_WITHOUT_CURSOR_TIME); |
837a2f8c PT |
2281 | } |
2282 | ||
2283 | @Override | |
2284 | public void mouseHover(MouseEvent e) { | |
2285 | } | |
2286 | ||
2287 | @Override | |
2288 | public void mouseScrolled(MouseEvent e) { | |
6b11be52 | 2289 | if (fDragState != DRAG_NONE) { |
837a2f8c PT |
2290 | return; |
2291 | } | |
2292 | boolean zoomScroll = false; | |
b698ec63 | 2293 | boolean horizontalScroll = false; |
837a2f8c PT |
2294 | Point p = getParent().toControl(getDisplay().getCursorLocation()); |
2295 | Point parentSize = getParent().getSize(); | |
2296 | if (p.x >= 0 && p.x < parentSize.x && p.y >= 0 && p.y < parentSize.y) { | |
2297 | // over the parent control | |
b698ec63 | 2298 | if (e.x > getSize().x) { |
e5552166 | 2299 | // over the vertical scroll bar |
837a2f8c | 2300 | zoomScroll = false; |
b698ec63 PT |
2301 | } else if (e.y < 0) { |
2302 | // over the time scale | |
837a2f8c | 2303 | zoomScroll = true; |
b698ec63 PT |
2304 | } else if (e.y >= getSize().y) { |
2305 | // over the horizontal scroll bar | |
2306 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
2307 | zoomScroll = true; | |
2308 | } else { | |
2309 | horizontalScroll = true; | |
2310 | } | |
e5552166 PT |
2311 | } else { |
2312 | if (e.x < fTimeProvider.getNameSpace()) { | |
2313 | // over the name space | |
2314 | zoomScroll = false; | |
2315 | } else { | |
2316 | // over the state area | |
2317 | if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) { | |
2318 | // over the state area, CTRL pressed | |
2319 | zoomScroll = true; | |
2320 | } else { | |
2321 | // over the state area, CTRL not pressed | |
2322 | zoomScroll = false; | |
2323 | } | |
2324 | } | |
837a2f8c PT |
2325 | } |
2326 | } | |
f1fae91f | 2327 | if (zoomScroll && fTimeProvider.getTime0() != fTimeProvider.getTime1()) { |
837a2f8c PT |
2328 | if (e.count > 0) { |
2329 | zoom(true); | |
2330 | } else if (e.count < 0) { | |
2331 | zoom(false); | |
2332 | } | |
b698ec63 PT |
2333 | } else if (horizontalScroll) { |
2334 | horizontalScroll(e.count > 0); | |
837a2f8c PT |
2335 | } else { |
2336 | setTopIndex(getTopIndex() - e.count); | |
2337 | } | |
2338 | } | |
2339 | ||
6b11be52 PT |
2340 | @Override |
2341 | public void handleEvent(Event event) { | |
2342 | if (event.type == SWT.MouseWheel) { | |
2343 | // prevent horizontal scrolling when the mouse wheel is used to | |
2344 | // scroll vertically or zoom | |
2345 | event.doit = false; | |
2346 | } | |
2347 | } | |
2348 | ||
837a2f8c PT |
2349 | @Override |
2350 | public int getBorderWidth() { | |
f1fae91f | 2351 | return fBorderWidth; |
837a2f8c PT |
2352 | } |
2353 | ||
2354 | /** | |
2355 | * Set the border width | |
2356 | * | |
2357 | * @param borderWidth | |
2358 | * The width | |
2359 | */ | |
2360 | public void setBorderWidth(int borderWidth) { | |
f1fae91f | 2361 | this.fBorderWidth = borderWidth; |
837a2f8c PT |
2362 | } |
2363 | ||
2364 | /** | |
2365 | * @return The current height of the header row | |
2366 | */ | |
2367 | public int getHeaderHeight() { | |
f1fae91f | 2368 | return fHeaderHeight; |
837a2f8c PT |
2369 | } |
2370 | ||
2371 | /** | |
2372 | * Set the height of the header row | |
2373 | * | |
2374 | * @param headerHeight | |
2375 | * The height | |
2376 | */ | |
2377 | public void setHeaderHeight(int headerHeight) { | |
f1fae91f | 2378 | this.fHeaderHeight = headerHeight; |
837a2f8c PT |
2379 | } |
2380 | ||
2381 | /** | |
c004295c | 2382 | * @return The default height of regular item rows |
837a2f8c PT |
2383 | */ |
2384 | public int getItemHeight() { | |
f1fae91f | 2385 | return fGlobalItemHeight; |
837a2f8c PT |
2386 | } |
2387 | ||
2388 | /** | |
c004295c | 2389 | * Set the default height of regular item rows. |
837a2f8c PT |
2390 | * |
2391 | * @param rowHeight | |
2392 | * The height | |
2393 | */ | |
2394 | public void setItemHeight(int rowHeight) { | |
f1fae91f | 2395 | this.fGlobalItemHeight = rowHeight; |
837a2f8c PT |
2396 | } |
2397 | ||
c004295c PT |
2398 | /** |
2399 | * Set the height of a specific item. Overrides the default item height. | |
2400 | * | |
2401 | * @param entry | |
2402 | * A time graph entry | |
2403 | * @param rowHeight | |
2404 | * The height | |
2405 | * @return true if the height is successfully stored, false otherwise | |
c004295c PT |
2406 | */ |
2407 | public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) { | |
2408 | Item item = fItemData.findItem(entry); | |
2409 | if (item != null) { | |
2410 | item.fItemHeight = rowHeight; | |
2411 | return true; | |
2412 | } | |
2413 | return false; | |
2414 | } | |
2415 | ||
837a2f8c PT |
2416 | /** |
2417 | * Set the minimum item width | |
2418 | * | |
2419 | * @param width The minimum width | |
2420 | */ | |
2421 | public void setMinimumItemWidth(int width) { | |
f1fae91f | 2422 | this.fMinimumItemWidth = width; |
837a2f8c PT |
2423 | } |
2424 | ||
2425 | /** | |
2426 | * @return The minimum item width | |
2427 | */ | |
2428 | public int getMinimumItemWidth() { | |
f1fae91f | 2429 | return fMinimumItemWidth; |
837a2f8c PT |
2430 | } |
2431 | ||
2432 | /** | |
2433 | * @return The entries that are currently filtered out | |
2434 | */ | |
f1fae91f PT |
2435 | public List<ITimeGraphEntry> getFilteredOut() { |
2436 | return fItemData.getFilteredOut(); | |
837a2f8c PT |
2437 | } |
2438 | ||
837a2f8c PT |
2439 | @Override |
2440 | public void addSelectionChangedListener(ISelectionChangedListener listener) { | |
f1fae91f PT |
2441 | if (listener != null && !fSelectionChangedListeners.contains(listener)) { |
2442 | fSelectionChangedListeners.add(listener); | |
837a2f8c PT |
2443 | } |
2444 | } | |
2445 | ||
837a2f8c PT |
2446 | @Override |
2447 | public void removeSelectionChangedListener(ISelectionChangedListener listener) { | |
2448 | if (listener != null) { | |
f1fae91f | 2449 | fSelectionChangedListeners.remove(listener); |
837a2f8c PT |
2450 | } |
2451 | } | |
2452 | ||
837a2f8c PT |
2453 | @Override |
2454 | public void setSelection(ISelection selection) { | |
2455 | if (selection instanceof TimeGraphSelection) { | |
2456 | TimeGraphSelection sel = (TimeGraphSelection) selection; | |
2457 | Object ob = sel.getFirstElement(); | |
2458 | if (ob instanceof ITimeGraphEntry) { | |
2459 | ITimeGraphEntry trace = (ITimeGraphEntry) ob; | |
2460 | selectItem(trace, false); | |
2461 | } | |
2462 | } | |
2463 | ||
2464 | } | |
2465 | ||
6ac5a950 AM |
2466 | /** |
2467 | * @param filter The filter object to be attached to the view | |
6ac5a950 AM |
2468 | */ |
2469 | public void addFilter(ViewerFilter filter) { | |
f1fae91f PT |
2470 | if (!fFilters.contains(filter)) { |
2471 | fFilters.add(filter); | |
6ac5a950 AM |
2472 | } |
2473 | } | |
2474 | ||
2475 | /** | |
2476 | * @param filter The filter object to be attached to the view | |
6ac5a950 AM |
2477 | */ |
2478 | public void removeFilter(ViewerFilter filter) { | |
f1fae91f | 2479 | fFilters.remove(filter); |
6ac5a950 AM |
2480 | } |
2481 | ||
496f76d3 GB |
2482 | @Override |
2483 | public void colorSettingsChanged(StateItem[] stateItems) { | |
2484 | /* Destroy previous colors from the resource manager */ | |
517fe01b EB |
2485 | if (fEventColorMap != null) { |
2486 | for (Color color : fEventColorMap) { | |
2487 | fResourceManager.destroyColor(color.getRGB()); | |
2488 | } | |
496f76d3 GB |
2489 | } |
2490 | if (stateItems != null) { | |
2491 | fEventColorMap = new Color[stateItems.length]; | |
2492 | for (int i = 0; i < stateItems.length; i++) { | |
2493 | fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor()); | |
2494 | } | |
2495 | } else { | |
2496 | fEventColorMap = new Color[] { }; | |
2497 | } | |
2498 | redraw(); | |
2499 | } | |
2500 | ||
837a2f8c | 2501 | private class ItemData { |
70e10acc | 2502 | private final Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>(); |
f1fae91f PT |
2503 | private Item[] fExpandedItems = new Item[0]; |
2504 | private Item[] fItems = new Item[0]; | |
70e10acc | 2505 | private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0]; |
507b1336 | 2506 | private List<ILinkEvent> fLinks = new ArrayList<>(); |
70e10acc | 2507 | private boolean fEntryFilter[] = new boolean[0]; |
507b1336 | 2508 | private final ArrayList<ITimeGraphEntry> fFilteredOut = new ArrayList<>(); |
837a2f8c PT |
2509 | |
2510 | public ItemData() { | |
2511 | } | |
2512 | ||
70e10acc PT |
2513 | public Item findItem(ITimeGraphEntry entry) { |
2514 | return fItemMap.get(entry); | |
837a2f8c PT |
2515 | } |
2516 | ||
70e10acc PT |
2517 | public int findItemIndex(ITimeGraphEntry entry) { |
2518 | Item item = fItemMap.get(entry); | |
2519 | if (item == null) { | |
837a2f8c PT |
2520 | return -1; |
2521 | } | |
70e10acc | 2522 | return item.fExpandedIndex; |
837a2f8c PT |
2523 | } |
2524 | ||
2525 | public void refreshData() { | |
70e10acc | 2526 | fItemMap.clear(); |
f1fae91f | 2527 | fFilteredOut.clear(); |
3e9a3685 | 2528 | ITimeGraphEntry selection = getSelectedTrace(); |
70e10acc PT |
2529 | for (int i = 0; i < fRootEntries.length; i++) { |
2530 | ITimeGraphEntry entry = fRootEntries[i]; | |
2531 | refreshData(fItemMap, null, 0, entry); | |
837a2f8c | 2532 | } |
70e10acc | 2533 | fItems = fItemMap.values().toArray(new Item[0]); |
837a2f8c | 2534 | updateExpandedItems(); |
3e9a3685 | 2535 | if (selection != null) { |
f1fae91f | 2536 | for (Item item : fExpandedItems) { |
70e10acc | 2537 | if (item.fEntry == selection) { |
f1fae91f | 2538 | item.fSelected = true; |
3e9a3685 PT |
2539 | break; |
2540 | } | |
2541 | } | |
2542 | } | |
837a2f8c PT |
2543 | } |
2544 | ||
70e10acc | 2545 | private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) { |
837a2f8c PT |
2546 | Item item = new Item(entry, entry.getName(), level); |
2547 | if (parent != null) { | |
f1fae91f | 2548 | parent.fChildren.add(item); |
837a2f8c | 2549 | } |
c004295c PT |
2550 | if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) { |
2551 | item.fItemHeight = fTimeGraphProvider.getItemHeight(entry); | |
2552 | } else { | |
2553 | item.fItemHeight = fGlobalItemHeight; | |
2554 | } | |
70e10acc | 2555 | itemMap.put(entry, item); |
837a2f8c | 2556 | if (entry.hasChildren()) { |
f4617471 | 2557 | item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel; |
f1fae91f | 2558 | item.fHasChildren = true; |
837a2f8c | 2559 | for (ITimeGraphEntry child : entry.getChildren()) { |
70e10acc | 2560 | refreshData(itemMap, item, level + 1, child); |
837a2f8c PT |
2561 | } |
2562 | } | |
2563 | } | |
2564 | ||
2565 | public void updateExpandedItems() { | |
70e10acc PT |
2566 | for (Item item : fItems) { |
2567 | item.fExpandedIndex = -1; | |
2568 | } | |
507b1336 | 2569 | List<Item> expandedItemList = new ArrayList<>(); |
70e10acc PT |
2570 | for (int i = 0; i < fRootEntries.length; i++) { |
2571 | ITimeGraphEntry entry = fRootEntries[i]; | |
837a2f8c PT |
2572 | Item item = findItem(entry); |
2573 | refreshExpanded(expandedItemList, item); | |
2574 | } | |
f1fae91f | 2575 | fExpandedItems = expandedItemList.toArray(new Item[0]); |
19ed1845 | 2576 | fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1)); |
837a2f8c PT |
2577 | } |
2578 | ||
2579 | private void refreshExpanded(List<Item> expandedItemList, Item item) { | |
6ac5a950 AM |
2580 | // Check for filters |
2581 | boolean display = true; | |
f1fae91f | 2582 | for (ViewerFilter filter : fFilters) { |
70e10acc | 2583 | if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) { |
6ac5a950 AM |
2584 | display = false; |
2585 | break; | |
2586 | } | |
2587 | } | |
2588 | if (display) { | |
70e10acc | 2589 | item.fExpandedIndex = expandedItemList.size(); |
6ac5a950 | 2590 | expandedItemList.add(item); |
f1fae91f PT |
2591 | if (item.fHasChildren && item.fExpanded) { |
2592 | for (Item child : item.fChildren) { | |
6ac5a950 AM |
2593 | refreshExpanded(expandedItemList, child); |
2594 | } | |
837a2f8c PT |
2595 | } |
2596 | } | |
2597 | } | |
2598 | ||
70e10acc PT |
2599 | public void refreshData(ITimeGraphEntry[] entries) { |
2600 | if (entries == null) { | |
2601 | fEntryFilter = null; | |
2602 | fRootEntries = null; | |
f1fae91f | 2603 | } else { |
70e10acc PT |
2604 | if (entries.length == 0) { |
2605 | fEntryFilter = null; | |
2606 | } else if (fEntryFilter == null || entries.length != fEntryFilter.length) { | |
2607 | fEntryFilter = new boolean[entries.length]; | |
2608 | java.util.Arrays.fill(fEntryFilter, true); | |
f1fae91f | 2609 | } |
70e10acc | 2610 | fRootEntries = Arrays.copyOf(entries, entries.length); |
837a2f8c PT |
2611 | } |
2612 | ||
837a2f8c PT |
2613 | refreshData(); |
2614 | } | |
2615 | ||
bec1f1ac GB |
2616 | public void refreshArrows(List<ILinkEvent> events) { |
2617 | /* If links are null, reset the list */ | |
2618 | if (events != null) { | |
2619 | fLinks = events; | |
2620 | } else { | |
507b1336 | 2621 | fLinks = new ArrayList<>(); |
bec1f1ac GB |
2622 | } |
2623 | } | |
2624 | ||
70e10acc PT |
2625 | public ITimeGraphEntry[] getEntries() { |
2626 | return fRootEntries; | |
837a2f8c PT |
2627 | } |
2628 | ||
70e10acc PT |
2629 | public boolean[] getEntryFilter() { |
2630 | return fEntryFilter; | |
837a2f8c PT |
2631 | } |
2632 | ||
f1fae91f PT |
2633 | public List<ITimeGraphEntry> getFilteredOut() { |
2634 | return fFilteredOut; | |
837a2f8c PT |
2635 | } |
2636 | } | |
2637 | ||
2638 | private class Item { | |
f1fae91f | 2639 | private boolean fExpanded; |
70e10acc | 2640 | private int fExpandedIndex; |
f1fae91f PT |
2641 | private boolean fSelected; |
2642 | private boolean fHasChildren; | |
2643 | private int fItemHeight; | |
70e10acc PT |
2644 | private final int fLevel; |
2645 | private final List<Item> fChildren; | |
2646 | private final String fName; | |
2647 | private final ITimeGraphEntry fEntry; | |
837a2f8c | 2648 | |
70e10acc PT |
2649 | public Item(ITimeGraphEntry entry, String name, int level) { |
2650 | this.fEntry = entry; | |
f1fae91f PT |
2651 | this.fName = name; |
2652 | this.fLevel = level; | |
507b1336 | 2653 | this.fChildren = new ArrayList<>(); |
837a2f8c PT |
2654 | } |
2655 | ||
2656 | @Override | |
2657 | public String toString() { | |
f1fae91f | 2658 | return fName; |
837a2f8c PT |
2659 | } |
2660 | } | |
2661 | ||
27df1564 XR |
2662 | @Override |
2663 | public void menuDetected(MenuDetectEvent e) { | |
f1fae91f | 2664 | if (null == fTimeProvider) { |
27df1564 XR |
2665 | return; |
2666 | } | |
0b5a90a0 PT |
2667 | if (e.detail == SWT.MENU_MOUSE) { |
2668 | if (fPendingMenuDetectEvent == null) { | |
2669 | /* Feature in Linux. The MenuDetectEvent is received before mouseDown. | |
2670 | * Store the event and trigger it later just before handling mouseUp. | |
2671 | * This allows for the method to detect if mouse is used to drag zoom. | |
2672 | */ | |
2673 | fPendingMenuDetectEvent = e; | |
2674 | return; | |
2675 | } | |
2676 | fPendingMenuDetectEvent = null; | |
f1fae91f | 2677 | if (fDragState != DRAG_ZOOM || fDragX != fDragX0) { |
0b5a90a0 PT |
2678 | return; |
2679 | } | |
2680 | } else { | |
f1fae91f | 2681 | if (fDragState != DRAG_NONE) { |
0b5a90a0 PT |
2682 | return; |
2683 | } | |
2684 | } | |
27df1564 XR |
2685 | Point p = toControl(e.x, e.y); |
2686 | int idx = getItemIndexAtY(p.y); | |
f1fae91f PT |
2687 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
2688 | Item item = fItemData.fExpandedItems[idx]; | |
70e10acc | 2689 | ITimeGraphEntry entry = item.fEntry; |
27df1564 XR |
2690 | if (entry.hasTimeEvents()) { |
2691 | ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2); | |
2692 | if (event != null) { | |
2693 | e.data = event; | |
2694 | fireMenuEventOnTimeEvent(e); | |
2695 | return; | |
2696 | } | |
2697 | } | |
2698 | e.data = entry; | |
2699 | fireMenuEventOnTimeGraphEntry(e); | |
2700 | } | |
2701 | } | |
2702 | ||
d2e4afa7 MAL |
2703 | /** |
2704 | * Perform the alignment operation. | |
2705 | * | |
2706 | * @param offset | |
2707 | * the alignment offset | |
2708 | * | |
2709 | * @see ITmfTimeAligned | |
2710 | * | |
2711 | * @since 1.0 | |
2712 | */ | |
2713 | public void performAlign(int offset) { | |
2714 | fTimeProvider.setNameSpace(offset); | |
2715 | } | |
2716 | ||
2717 | /** | |
2718 | * Return the time alignment information | |
2719 | * | |
2720 | * @return the time alignment information | |
2721 | * | |
2722 | * @see ITmfTimeAligned | |
2723 | * | |
2724 | * @since 1.0 | |
2725 | */ | |
2726 | public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() { | |
2727 | return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace()); | |
2728 | } | |
837a2f8c PT |
2729 | } |
2730 | ||
27df1564 | 2731 |