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