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