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