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