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