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