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