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