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