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 | |
837a2f8c PT |
16 | *****************************************************************************/ |
17 | ||
18 | package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets; | |
19 | ||
20 | import java.util.ArrayList; | |
f1fae91f | 21 | import java.util.Arrays; |
837a2f8c PT |
22 | import java.util.Iterator; |
23 | import java.util.List; | |
837a2f8c PT |
24 | |
25 | import org.eclipse.jface.resource.JFaceResources; | |
26 | import org.eclipse.jface.resource.LocalResourceManager; | |
27 | import org.eclipse.jface.viewers.ISelection; | |
28 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
29 | import org.eclipse.jface.viewers.ISelectionProvider; | |
6ac5a950 | 30 | import org.eclipse.jface.viewers.ViewerFilter; |
837a2f8c | 31 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider; |
4999a196 | 32 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2; |
837a2f8c PT |
33 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphTreeListener; |
34 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem; | |
35 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent; | |
36 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent; | |
37 | import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry; | |
38 | import org.eclipse.swt.SWT; | |
39 | import org.eclipse.swt.events.ControlEvent; | |
40 | import org.eclipse.swt.events.ControlListener; | |
41 | import org.eclipse.swt.events.FocusEvent; | |
42 | import org.eclipse.swt.events.FocusListener; | |
43 | import org.eclipse.swt.events.KeyEvent; | |
44 | import org.eclipse.swt.events.KeyListener; | |
27df1564 XR |
45 | import org.eclipse.swt.events.MenuDetectEvent; |
46 | import org.eclipse.swt.events.MenuDetectListener; | |
837a2f8c PT |
47 | import org.eclipse.swt.events.MouseEvent; |
48 | import org.eclipse.swt.events.MouseListener; | |
49 | import org.eclipse.swt.events.MouseMoveListener; | |
50 | import org.eclipse.swt.events.MouseTrackListener; | |
51 | import org.eclipse.swt.events.MouseWheelListener; | |
52 | import org.eclipse.swt.events.PaintEvent; | |
53 | import org.eclipse.swt.events.SelectionEvent; | |
54 | import org.eclipse.swt.events.SelectionListener; | |
55 | import org.eclipse.swt.events.TraverseEvent; | |
56 | import org.eclipse.swt.events.TraverseListener; | |
27df1564 | 57 | import org.eclipse.swt.events.TypedEvent; |
837a2f8c PT |
58 | import org.eclipse.swt.graphics.Color; |
59 | import org.eclipse.swt.graphics.Cursor; | |
60 | import org.eclipse.swt.graphics.GC; | |
61 | import org.eclipse.swt.graphics.Image; | |
62 | import org.eclipse.swt.graphics.Point; | |
63 | import org.eclipse.swt.graphics.Rectangle; | |
64 | import org.eclipse.swt.widgets.Composite; | |
65 | import org.eclipse.swt.widgets.Display; | |
66 | import org.eclipse.swt.widgets.Event; | |
67 | import org.eclipse.swt.widgets.Listener; | |
68 | import org.eclipse.swt.widgets.ScrollBar; | |
69 | ||
70 | /** | |
71 | * Time graph control implementation | |
72 | * | |
73 | * @version 1.0 | |
74 | * @author Alvaro Sanchez-Leon | |
75 | * @author Patrick Tasse | |
76 | */ | |
4999a196 | 77 | public class TimeGraphControl extends TimeGraphBaseControl implements FocusListener, KeyListener, MouseMoveListener, MouseListener, MouseWheelListener, ControlListener, SelectionListener, MouseTrackListener, TraverseListener, ISelectionProvider, MenuDetectListener, ITmfTimeGraphDrawingHelper { |
837a2f8c | 78 | |
f1fae91f | 79 | |
a0a88f65 AM |
80 | /** Max scrollbar size */ |
81 | public static final int H_SCROLLBAR_MAX = Integer.MAX_VALUE - 1; | |
82 | ||
837a2f8c PT |
83 | private static final int DRAG_NONE = 0; |
84 | private static final int DRAG_TRACE_ITEM = 1; | |
85 | private static final int DRAG_SPLIT_LINE = 2; | |
5b2b9bd7 | 86 | private static final int DRAG_ZOOM = 3; |
a0a88f65 | 87 | |
837a2f8c PT |
88 | private static final int CUSTOM_ITEM_HEIGHT = -1; // get item height from provider |
89 | ||
f1fae91f PT |
90 | private static final double ZOOM_FACTOR = 1.5; |
91 | private static final double ZOOM_IN_FACTOR = 0.8; | |
92 | private static final double ZOOM_OUT_FACTOR = 1.25; | |
93 | ||
94 | /** Resource manager */ | |
95 | private LocalResourceManager fResourceManager = new LocalResourceManager(JFaceResources.getResources()); | |
96 | ||
97 | /** Color map for event types */ | |
98 | private Color[] fEventColorMap = null; | |
99 | ||
100 | private ITimeDataProvider fTimeProvider; | |
101 | private boolean fIsInFocus = false; | |
102 | private boolean fIsDragCursor = false; | |
103 | private boolean fIsWaitCursor = true; | |
104 | private boolean fMouseOverSplitLine = false; | |
105 | private int fGlobalItemHeight = CUSTOM_ITEM_HEIGHT; | |
106 | private int fMinimumItemWidth = 0; | |
107 | private int fTopIndex = 0; | |
108 | private int fDragState = DRAG_NONE; | |
109 | private int fDragX0 = 0; | |
110 | private int fDragX = 0; | |
111 | private int fIdealNameSpace = 0; | |
112 | private long fTime0bak; | |
113 | private long fTime1bak; | |
837a2f8c | 114 | private ITimeGraphPresentationProvider fTimeGraphProvider = null; |
f1fae91f PT |
115 | private ItemData fItemData = null; |
116 | private List<SelectionListener> fSelectionListeners; | |
117 | private final List<ISelectionChangedListener> fSelectionChangedListeners = new ArrayList<ISelectionChangedListener>(); | |
118 | private final List<ITimeGraphTreeListener> fTreeListeners = new ArrayList<ITimeGraphTreeListener>(); | |
119 | private final List<MenuDetectListener> fTimeGraphEntryMenuListeners = new ArrayList<MenuDetectListener>(); | |
120 | private final List<MenuDetectListener> fTimeEventMenuListeners = new ArrayList<MenuDetectListener>(); | |
121 | private final Cursor fDragCursor; | |
122 | private final Cursor fWaitCursor; | |
123 | private final List<ViewerFilter> fFilters = new ArrayList<ViewerFilter>(); | |
0b5a90a0 | 124 | private MenuDetectEvent fPendingMenuDetectEvent = null; |
837a2f8c | 125 | |
f1fae91f PT |
126 | private int fBorderWidth = 0; |
127 | private int fHeaderHeight = 0; | |
837a2f8c | 128 | |
f1fae91f | 129 | private Listener fMouseScrollFilterListener; |
837a2f8c | 130 | |
837a2f8c PT |
131 | private MouseScrollNotifier fMouseScrollNotifier; |
132 | private final Object fMouseScrollNotifierLock = new Object(); | |
f1fae91f | 133 | |
837a2f8c | 134 | private class MouseScrollNotifier extends Thread { |
f1fae91f PT |
135 | private static final long DELAY = 400L; |
136 | private static final long POLLING_INTERVAL = 10L; | |
837a2f8c PT |
137 | private long fLastScrollTime = Long.MAX_VALUE; |
138 | ||
139 | @Override | |
140 | public void run() { | |
141 | while ((System.currentTimeMillis() - fLastScrollTime) < DELAY) { | |
142 | try { | |
143 | Thread.sleep(POLLING_INTERVAL); | |
144 | } catch (Exception e) { | |
145 | return; | |
146 | } | |
147 | } | |
148 | if (!isInterrupted()) { | |
149 | Display.getDefault().asyncExec(new Runnable() { | |
150 | @Override | |
151 | public void run() { | |
152 | if (isDisposed()) { | |
153 | return; | |
154 | } | |
f1fae91f | 155 | fTimeProvider.notifyStartFinishTime(); |
837a2f8c PT |
156 | } |
157 | }); | |
158 | } | |
159 | synchronized (fMouseScrollNotifierLock) { | |
160 | fMouseScrollNotifier = null; | |
161 | } | |
162 | } | |
163 | ||
164 | public void mouseScrolled() { | |
165 | fLastScrollTime = System.currentTimeMillis(); | |
166 | } | |
167 | } | |
168 | ||
169 | /** | |
170 | * Standard constructor | |
171 | * | |
172 | * @param parent | |
173 | * The parent composite object | |
174 | * @param colors | |
175 | * The color scheme to use | |
176 | */ | |
177 | public TimeGraphControl(Composite parent, TimeGraphColorScheme colors) { | |
178 | ||
179 | super(parent, colors, SWT.NO_BACKGROUND | SWT.H_SCROLL | SWT.DOUBLE_BUFFERED); | |
180 | ||
f1fae91f | 181 | fItemData = new ItemData(); |
837a2f8c PT |
182 | |
183 | addFocusListener(this); | |
184 | addMouseListener(this); | |
185 | addMouseMoveListener(this); | |
186 | addMouseTrackListener(this); | |
187 | addMouseWheelListener(this); | |
188 | addTraverseListener(this); | |
189 | addKeyListener(this); | |
190 | addControlListener(this); | |
27df1564 | 191 | addMenuDetectListener(this); |
837a2f8c PT |
192 | ScrollBar scrollHor = getHorizontalBar(); |
193 | ||
194 | if (scrollHor != null) { | |
195 | scrollHor.addSelectionListener(this); | |
196 | } | |
197 | ||
f1fae91f PT |
198 | fDragCursor = new Cursor(super.getDisplay(), SWT.CURSOR_SIZEWE); |
199 | fWaitCursor = new Cursor(super.getDisplay(), SWT.CURSOR_WAIT); | |
837a2f8c PT |
200 | } |
201 | ||
202 | @Override | |
203 | public void dispose() { | |
204 | super.dispose(); | |
f1fae91f PT |
205 | fDragCursor.dispose(); |
206 | fWaitCursor.dispose(); | |
837a2f8c PT |
207 | fResourceManager.dispose(); |
208 | } | |
209 | ||
210 | /** | |
211 | * Sets the timegraph provider used by this timegraph viewer. | |
212 | * | |
213 | * @param timeGraphProvider the timegraph provider | |
214 | */ | |
215 | public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider) { | |
216 | fTimeGraphProvider = timeGraphProvider; | |
837a2f8c | 217 | |
4999a196 GB |
218 | if (timeGraphProvider instanceof ITimeGraphPresentationProvider2) { |
219 | ((ITimeGraphPresentationProvider2) timeGraphProvider).setDrawingHelper(this); | |
220 | } | |
221 | ||
837a2f8c PT |
222 | if (fEventColorMap != null) { |
223 | for (Color color : fEventColorMap) { | |
224 | fResourceManager.destroyColor(color.getRGB()); | |
225 | } | |
226 | } | |
227 | StateItem[] stateItems = fTimeGraphProvider.getStateTable(); | |
228 | if (stateItems != null) { | |
229 | fEventColorMap = new Color[stateItems.length]; | |
230 | for (int i = 0; i < stateItems.length; i++) { | |
231 | fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor()); | |
232 | } | |
233 | } else { | |
234 | fEventColorMap = new Color[] { }; | |
235 | } | |
236 | } | |
237 | ||
238 | /** | |
239 | * Assign the given time provider | |
240 | * | |
241 | * @param timeProvider | |
242 | * The time provider | |
243 | */ | |
244 | public void setTimeProvider(ITimeDataProvider timeProvider) { | |
f1fae91f | 245 | fTimeProvider = timeProvider; |
837a2f8c PT |
246 | adjustScrolls(); |
247 | redraw(); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Add a selection listener | |
252 | * | |
253 | * @param listener | |
254 | * The listener to add | |
255 | */ | |
256 | public void addSelectionListener(SelectionListener listener) { | |
257 | if (listener == null) { | |
258 | SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
259 | } | |
f1fae91f PT |
260 | if (null == fSelectionListeners) { |
261 | fSelectionListeners = new ArrayList<SelectionListener>(); | |
837a2f8c | 262 | } |
f1fae91f | 263 | fSelectionListeners.add(listener); |
837a2f8c PT |
264 | } |
265 | ||
266 | /** | |
267 | * Remove a selection listener | |
268 | * | |
269 | * @param listener | |
270 | * The listener to remove | |
271 | */ | |
272 | public void removeSelectionListener(SelectionListener listener) { | |
f1fae91f PT |
273 | if (null != fSelectionListeners) { |
274 | fSelectionListeners.remove(listener); | |
837a2f8c PT |
275 | } |
276 | } | |
277 | ||
278 | /** | |
279 | * Selection changed callback | |
280 | */ | |
281 | public void fireSelectionChanged() { | |
f1fae91f PT |
282 | if (null != fSelectionListeners) { |
283 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
284 | while (it.hasNext()) { |
285 | SelectionListener listener = it.next(); | |
286 | listener.widgetSelected(null); | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | /** | |
292 | * Default selection callback | |
293 | */ | |
294 | public void fireDefaultSelection() { | |
f1fae91f PT |
295 | if (null != fSelectionListeners) { |
296 | Iterator<SelectionListener> it = fSelectionListeners.iterator(); | |
837a2f8c PT |
297 | while (it.hasNext()) { |
298 | SelectionListener listener = it.next(); | |
299 | listener.widgetDefaultSelected(null); | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
304 | /** | |
305 | * Get the traces in the model | |
306 | * | |
307 | * @return The array of traces | |
308 | */ | |
309 | public ITimeGraphEntry[] getTraces() { | |
f1fae91f | 310 | return fItemData.getTraces(); |
837a2f8c PT |
311 | } |
312 | ||
313 | /** | |
314 | * Get the on/off trace filters | |
315 | * | |
316 | * @return The array of filters | |
317 | */ | |
318 | public boolean[] getTraceFilter() { | |
f1fae91f | 319 | return fItemData.getTraceFilter(); |
837a2f8c PT |
320 | } |
321 | ||
322 | /** | |
323 | * Refresh the data for the thing | |
324 | */ | |
325 | public void refreshData() { | |
f1fae91f | 326 | fItemData.refreshData(); |
837a2f8c PT |
327 | adjustScrolls(); |
328 | redraw(); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Refresh data for the given traces | |
333 | * | |
334 | * @param traces | |
335 | * The traces to refresh | |
336 | */ | |
337 | public void refreshData(ITimeGraphEntry[] traces) { | |
f1fae91f | 338 | fItemData.refreshData(traces); |
837a2f8c PT |
339 | adjustScrolls(); |
340 | redraw(); | |
341 | } | |
342 | ||
343 | /** | |
344 | * Adjust the scoll bars | |
345 | */ | |
346 | public void adjustScrolls() { | |
f1fae91f | 347 | if (null == fTimeProvider) { |
837a2f8c PT |
348 | getHorizontalBar().setValues(0, 1, 1, 1, 1, 1); |
349 | return; | |
350 | } | |
351 | ||
352 | // HORIZONTAL BAR | |
353 | // Visible window | |
f1fae91f PT |
354 | long time0 = fTimeProvider.getTime0(); |
355 | long time1 = fTimeProvider.getTime1(); | |
837a2f8c | 356 | // Time boundaries |
f1fae91f PT |
357 | long timeMin = fTimeProvider.getMinTime(); |
358 | long timeMax = fTimeProvider.getMaxTime(); | |
837a2f8c PT |
359 | |
360 | long delta = timeMax - timeMin; | |
361 | ||
362 | int timePos = 0; | |
363 | int thumb = H_SCROLLBAR_MAX; | |
364 | ||
365 | if (delta != 0) { | |
366 | // Thumb size (page size) | |
367 | thumb = Math.max(1, (int) (H_SCROLLBAR_MAX * ((double) (time1 - time0) / delta))); | |
368 | // At the beginning of visible window | |
369 | timePos = (int) (H_SCROLLBAR_MAX * ((double) (time0 - timeMin) / delta)); | |
370 | } | |
371 | ||
372 | // position, minimum, maximum, thumb size, increment (half page)t, page | |
373 | // increment size (full page) | |
374 | getHorizontalBar().setValues(timePos, 0, H_SCROLLBAR_MAX, thumb, Math.max(1, thumb / 2), Math.max(2, thumb)); | |
375 | } | |
376 | ||
377 | boolean ensureVisibleItem(int idx, boolean redraw) { | |
378 | boolean changed = false; | |
41b5c37f AM |
379 | int index = idx; |
380 | if (index < 0) { | |
f1fae91f PT |
381 | for (index = 0; index < fItemData.fExpandedItems.length; index++) { |
382 | if (fItemData.fExpandedItems[index].fSelected) { | |
837a2f8c PT |
383 | break; |
384 | } | |
385 | } | |
386 | } | |
f1fae91f | 387 | if (index >= fItemData.fExpandedItems.length) { |
837a2f8c PT |
388 | return changed; |
389 | } | |
f1fae91f | 390 | if (index < fTopIndex) { |
41b5c37f | 391 | setTopIndex(index); |
837a2f8c PT |
392 | if (redraw) { |
393 | redraw(); | |
394 | } | |
395 | changed = true; | |
396 | } else { | |
397 | int page = countPerPage(); | |
f1fae91f | 398 | if (index >= fTopIndex + page) { |
41b5c37f | 399 | setTopIndex(index - page + 1); |
837a2f8c PT |
400 | if (redraw) { |
401 | redraw(); | |
402 | } | |
403 | changed = true; | |
404 | } | |
405 | } | |
406 | return changed; | |
407 | } | |
408 | ||
409 | /** | |
410 | * Assign the given index as the top one | |
411 | * | |
412 | * @param idx | |
413 | * The index | |
414 | */ | |
415 | public void setTopIndex(int idx) { | |
f1fae91f | 416 | int index = Math.min(idx, fItemData.fExpandedItems.length - countPerPage()); |
41b5c37f | 417 | index = Math.max(0, index); |
f1fae91f | 418 | fTopIndex = index; |
837a2f8c PT |
419 | redraw(); |
420 | } | |
421 | ||
422 | /** | |
423 | * Set the expanded state of a given entry | |
424 | * | |
425 | * @param entry | |
426 | * The entry | |
427 | * @param expanded | |
428 | * True if expanded, false if collapsed | |
429 | */ | |
430 | public void setExpandedState(ITimeGraphEntry entry, boolean expanded) { | |
f1fae91f PT |
431 | Item item = fItemData.findItem(entry); |
432 | if (item != null && item.fExpanded != expanded) { | |
433 | item.fExpanded = expanded; | |
434 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
435 | redraw(); |
436 | } | |
437 | } | |
438 | ||
439 | /** | |
440 | * Collapses all nodes of the viewer's tree, starting with the root. | |
441 | * | |
442 | * @since 2.0 | |
443 | */ | |
444 | public void collapseAll() { | |
f1fae91f PT |
445 | for (Item item : fItemData.fItems) { |
446 | item.fExpanded = false; | |
837a2f8c | 447 | } |
f1fae91f | 448 | fItemData.updateExpandedItems(); |
837a2f8c PT |
449 | redraw(); |
450 | } | |
451 | ||
452 | /** | |
453 | * Expands all nodes of the viewer's tree, starting with the root. | |
454 | * | |
455 | * @since 2.0 | |
456 | */ | |
457 | public void expandAll() { | |
f1fae91f PT |
458 | for (Item item : fItemData.fItems) { |
459 | item.fExpanded = true; | |
837a2f8c | 460 | } |
f1fae91f | 461 | fItemData.updateExpandedItems(); |
837a2f8c PT |
462 | redraw(); |
463 | } | |
464 | ||
465 | /** | |
466 | * Add a tree listener | |
467 | * | |
468 | * @param listener | |
469 | * The listener to add | |
470 | */ | |
471 | public void addTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
472 | if (!fTreeListeners.contains(listener)) { |
473 | fTreeListeners.add(listener); | |
837a2f8c PT |
474 | } |
475 | } | |
476 | ||
477 | /** | |
478 | * Remove a tree listener | |
479 | * | |
480 | * @param listener | |
481 | * The listener to remove | |
482 | */ | |
483 | public void removeTreeListener(ITimeGraphTreeListener listener) { | |
f1fae91f PT |
484 | if (fTreeListeners.contains(listener)) { |
485 | fTreeListeners.remove(listener); | |
837a2f8c PT |
486 | } |
487 | } | |
488 | ||
489 | /** | |
490 | * Tree event callback | |
491 | * | |
492 | * @param entry | |
493 | * The affected entry | |
494 | * @param expanded | |
495 | * The expanded state (true for expanded, false for collapsed) | |
496 | */ | |
497 | public void fireTreeEvent(ITimeGraphEntry entry, boolean expanded) { | |
498 | TimeGraphTreeExpansionEvent event = new TimeGraphTreeExpansionEvent(this, entry); | |
f1fae91f | 499 | for (ITimeGraphTreeListener listener : fTreeListeners) { |
837a2f8c PT |
500 | if (expanded) { |
501 | listener.treeExpanded(event); | |
502 | } else { | |
503 | listener.treeCollapsed(event); | |
504 | } | |
505 | } | |
506 | } | |
507 | ||
27df1564 XR |
508 | /** |
509 | * Add a menu listener on {@link ITimeGraphEntry}s | |
510 | * @param listener | |
511 | * The listener to add | |
77c4a6df | 512 | * @since 1.2 |
27df1564 XR |
513 | */ |
514 | public void addTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
515 | if (!fTimeGraphEntryMenuListeners.contains(listener)) { |
516 | fTimeGraphEntryMenuListeners.add(listener); | |
27df1564 XR |
517 | } |
518 | } | |
519 | ||
520 | /** | |
521 | * Remove a menu listener on {@link ITimeGraphEntry}s | |
522 | * | |
523 | * @param listener | |
524 | * The listener to remove | |
77c4a6df | 525 | * @since 1.2 |
27df1564 XR |
526 | */ |
527 | public void removeTimeGraphEntryMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
528 | if (fTimeGraphEntryMenuListeners.contains(listener)) { |
529 | fTimeGraphEntryMenuListeners.remove(listener); | |
27df1564 XR |
530 | } |
531 | } | |
532 | ||
533 | /** | |
534 | * Menu event callback on {@link ITimeGraphEntry}s | |
535 | * | |
536 | * @param event | |
537 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry} | |
538 | */ | |
539 | private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event) { | |
f1fae91f | 540 | for (MenuDetectListener listener : fTimeGraphEntryMenuListeners) { |
27df1564 XR |
541 | listener.menuDetected(event); |
542 | } | |
543 | } | |
544 | ||
545 | /** | |
546 | * Add a menu listener on {@link ITimeEvent}s | |
547 | * | |
548 | * @param listener | |
549 | * The listener to add | |
77c4a6df | 550 | * @since 1.2 |
27df1564 XR |
551 | */ |
552 | public void addTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
553 | if (!fTimeEventMenuListeners.contains(listener)) { |
554 | fTimeEventMenuListeners.add(listener); | |
27df1564 XR |
555 | } |
556 | } | |
557 | ||
558 | /** | |
559 | * Remove a menu listener on {@link ITimeEvent}s | |
560 | * | |
561 | * @param listener | |
562 | * The listener to remove | |
77c4a6df | 563 | * @since 1.2 |
27df1564 XR |
564 | */ |
565 | public void removeTimeEventMenuListener(MenuDetectListener listener) { | |
f1fae91f PT |
566 | if (fTimeEventMenuListeners.contains(listener)) { |
567 | fTimeEventMenuListeners.remove(listener); | |
27df1564 XR |
568 | } |
569 | } | |
570 | ||
571 | /** | |
572 | * Menu event callback on {@link ITimeEvent}s | |
573 | * | |
574 | * @param event | |
575 | * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent} | |
576 | */ | |
577 | private void fireMenuEventOnTimeEvent(MenuDetectEvent event) { | |
f1fae91f | 578 | for (MenuDetectListener listener : fTimeEventMenuListeners) { |
27df1564 XR |
579 | listener.menuDetected(event); |
580 | } | |
581 | } | |
582 | ||
837a2f8c PT |
583 | @Override |
584 | public ISelection getSelection() { | |
585 | TimeGraphSelection sel = new TimeGraphSelection(); | |
586 | ITimeGraphEntry trace = getSelectedTrace(); | |
f1fae91f PT |
587 | if (null != trace && null != fTimeProvider) { |
588 | long selectedTime = fTimeProvider.getSelectedTime(); | |
837a2f8c PT |
589 | ITimeEvent event = Utils.findEvent(trace, selectedTime, 0); |
590 | if (event != null) { | |
591 | sel.add(event); | |
592 | } else { | |
593 | sel.add(trace); | |
594 | } | |
595 | } | |
596 | return sel; | |
597 | } | |
598 | ||
599 | /** | |
600 | * Get the selection object | |
601 | * | |
602 | * @return The selection | |
603 | */ | |
604 | public ISelection getSelectionTrace() { | |
605 | TimeGraphSelection sel = new TimeGraphSelection(); | |
606 | ITimeGraphEntry trace = getSelectedTrace(); | |
607 | if (null != trace) { | |
608 | sel.add(trace); | |
609 | } | |
610 | return sel; | |
611 | } | |
612 | ||
613 | /** | |
614 | * Enable/disable one of the traces in the model | |
615 | * | |
616 | * @param n | |
617 | * 1 to enable it, -1 to disable. The method returns immediately | |
618 | * if another value is used. | |
619 | */ | |
620 | public void selectTrace(int n) { | |
621 | if ((n != 1) && (n != -1)) { | |
622 | return; | |
623 | } | |
624 | ||
625 | boolean changed = false; | |
626 | int lastSelection = -1; | |
f1fae91f PT |
627 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
628 | Item item = fItemData.fExpandedItems[i]; | |
629 | if (item.fSelected) { | |
837a2f8c | 630 | lastSelection = i; |
f1fae91f PT |
631 | if ((1 == n) && (i < fItemData.fExpandedItems.length - 1)) { |
632 | item.fSelected = false; | |
633 | item = fItemData.fExpandedItems[i + 1]; | |
634 | item.fSelected = true; | |
837a2f8c PT |
635 | changed = true; |
636 | } else if ((-1 == n) && (i > 0)) { | |
f1fae91f PT |
637 | item.fSelected = false; |
638 | item = fItemData.fExpandedItems[i - 1]; | |
639 | item.fSelected = true; | |
837a2f8c PT |
640 | changed = true; |
641 | } | |
642 | break; | |
643 | } | |
644 | } | |
645 | ||
f1fae91f PT |
646 | if (lastSelection < 0 && fItemData.fExpandedItems.length > 0) { |
647 | Item item = fItemData.fExpandedItems[0]; | |
648 | item.fSelected = true; | |
837a2f8c PT |
649 | changed = true; |
650 | } | |
651 | ||
652 | if (changed) { | |
653 | ensureVisibleItem(-1, false); | |
654 | redraw(); | |
655 | fireSelectionChanged(); | |
656 | } | |
657 | } | |
658 | ||
659 | /** | |
660 | * Select an event | |
661 | * | |
662 | * @param n | |
663 | * 1 for next event, -1 for previous event | |
664 | */ | |
665 | public void selectEvent(int n) { | |
f1fae91f | 666 | if (null == fTimeProvider) { |
837a2f8c PT |
667 | return; |
668 | } | |
669 | ITimeGraphEntry trace = getSelectedTrace(); | |
670 | if (trace == null) { | |
671 | return; | |
672 | } | |
f1fae91f PT |
673 | long selectedTime = fTimeProvider.getSelectedTime(); |
674 | long endTime = fTimeProvider.getEndTime(); | |
837a2f8c PT |
675 | ITimeEvent nextEvent; |
676 | if (-1 == n && selectedTime > endTime) { | |
677 | nextEvent = Utils.findEvent(trace, selectedTime, 0); | |
678 | } else { | |
679 | nextEvent = Utils.findEvent(trace, selectedTime, n); | |
680 | } | |
681 | if (null == nextEvent && -1 == n) { | |
682 | nextEvent = Utils.getFirstEvent(trace); | |
683 | } | |
684 | if (null != nextEvent) { | |
685 | long nextTime = nextEvent.getTime(); | |
686 | // If last event detected e.g. going back or not moving to a next | |
687 | // event | |
688 | if (nextTime <= selectedTime && n == 1) { | |
689 | // Select to the end of this last event | |
690 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); | |
691 | // but not beyond the end of the trace | |
692 | if (nextTime > endTime) { | |
693 | nextTime = endTime; | |
694 | } | |
f1fae91f | 695 | } else if (n == -1 && nextEvent.getTime() + nextEvent.getDuration() < selectedTime) { |
837a2f8c | 696 | // for previous event go to its end time unless we were already there |
f1fae91f | 697 | nextTime = nextEvent.getTime() + nextEvent.getDuration(); |
837a2f8c | 698 | } |
f1fae91f | 699 | fTimeProvider.setSelectedTimeNotify(nextTime, true); |
837a2f8c PT |
700 | fireSelectionChanged(); |
701 | } else if (1 == n) { | |
f1fae91f | 702 | fTimeProvider.setSelectedTimeNotify(endTime, true); |
837a2f8c PT |
703 | fireSelectionChanged(); |
704 | } | |
705 | } | |
706 | ||
707 | /** | |
708 | * Select the next event | |
709 | */ | |
710 | public void selectNextEvent() { | |
711 | selectEvent(1); | |
712 | // Notify if visible time window has been adjusted | |
f1fae91f | 713 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
714 | } |
715 | ||
716 | /** | |
717 | * Select the previous event | |
718 | */ | |
719 | public void selectPrevEvent() { | |
720 | selectEvent(-1); | |
721 | // Notify if visible time window has been adjusted | |
f1fae91f | 722 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getTime0(), fTimeProvider.getTime1()); |
837a2f8c PT |
723 | } |
724 | ||
725 | /** | |
726 | * Select the next trace | |
727 | */ | |
728 | public void selectNextTrace() { | |
729 | selectTrace(1); | |
730 | } | |
731 | ||
732 | /** | |
733 | * Select the previous trace | |
734 | */ | |
735 | public void selectPrevTrace() { | |
736 | selectTrace(-1); | |
737 | } | |
738 | ||
739 | /** | |
740 | * Zoom based on mouse cursor location with mouse scrolling | |
741 | * | |
742 | * @param zoomIn true to zoom in, false to zoom out | |
743 | */ | |
744 | public void zoom(boolean zoomIn) { | |
745 | int globalX = getDisplay().getCursorLocation().x; | |
746 | Point p = toControl(globalX, 0); | |
f1fae91f PT |
747 | int nameSpace = fTimeProvider.getNameSpace(); |
748 | int timeSpace = fTimeProvider.getTimeSpace(); | |
837a2f8c | 749 | int xPos = Math.max(nameSpace, Math.min(nameSpace + timeSpace, p.x)); |
f1fae91f PT |
750 | long time0 = fTimeProvider.getTime0(); |
751 | long time1 = fTimeProvider.getTime1(); | |
837a2f8c PT |
752 | long interval = time1 - time0; |
753 | if (interval == 0) { | |
754 | interval = 1; | |
755 | } // to allow getting out of single point interval | |
756 | long newInterval; | |
757 | if (zoomIn) { | |
f1fae91f | 758 | newInterval = Math.max(Math.round(interval * ZOOM_IN_FACTOR), fTimeProvider.getMinTimeInterval()); |
837a2f8c | 759 | } else { |
f1fae91f | 760 | newInterval = (long) Math.ceil(interval * ZOOM_OUT_FACTOR); |
837a2f8c PT |
761 | } |
762 | long center = time0 + Math.round(((double) (xPos - nameSpace) / timeSpace * interval)); | |
763 | long newTime0 = center - Math.round((double) newInterval * (center - time0) / interval); | |
764 | long newTime1 = newTime0 + newInterval; | |
f1fae91f | 765 | fTimeProvider.setStartFinishTime(newTime0, newTime1); |
837a2f8c PT |
766 | synchronized (fMouseScrollNotifierLock) { |
767 | if (fMouseScrollNotifier == null) { | |
768 | fMouseScrollNotifier = new MouseScrollNotifier(); | |
769 | fMouseScrollNotifier.start(); | |
770 | } | |
771 | fMouseScrollNotifier.mouseScrolled(); | |
772 | } | |
773 | } | |
774 | ||
775 | /** | |
776 | * zoom in using single click | |
777 | */ | |
778 | public void zoomIn() { | |
f1fae91f PT |
779 | long prevTime0 = fTimeProvider.getTime0(); |
780 | long prevTime1 = fTimeProvider.getTime1(); | |
781 | long prevRange = prevTime1 - prevTime0; | |
782 | if (prevRange == 0) { | |
3e9a3685 PT |
783 | return; |
784 | } | |
f1fae91f PT |
785 | long selTime = fTimeProvider.getSelectedTime(); |
786 | if (selTime <= prevTime0 || selTime >= prevTime1) { | |
787 | selTime = (prevTime0 + prevTime1) / 2; | |
837a2f8c | 788 | } |
f1fae91f PT |
789 | long time0 = selTime - (long) ((selTime - prevTime0) / ZOOM_FACTOR); |
790 | long time1 = selTime + (long) ((prevTime1 - selTime) / ZOOM_FACTOR); | |
837a2f8c | 791 | |
f1fae91f | 792 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
837a2f8c PT |
793 | |
794 | if (inaccuracy > 0 && inaccuracy < 100) { | |
f1fae91f | 795 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
837a2f8c PT |
796 | return; |
797 | } | |
798 | ||
f1fae91f PT |
799 | long min = fTimeProvider.getMinTimeInterval(); |
800 | if ((time1 - time0) < min) { | |
801 | time0 = selTime - (selTime - prevTime0) * min / prevRange; | |
802 | time1 = time0 + min; | |
837a2f8c PT |
803 | } |
804 | ||
f1fae91f | 805 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
806 | } |
807 | ||
808 | /** | |
809 | * zoom out using single click | |
810 | */ | |
811 | public void zoomOut() { | |
f1fae91f PT |
812 | long prevTime0 = fTimeProvider.getTime0(); |
813 | long prevTime1 = fTimeProvider.getTime1(); | |
814 | long selTime = fTimeProvider.getSelectedTime(); | |
815 | if (selTime <= prevTime0 || selTime >= prevTime1) { | |
816 | selTime = (prevTime0 + prevTime1) / 2; | |
837a2f8c | 817 | } |
f1fae91f PT |
818 | long time0 = (long) (selTime - (selTime - prevTime0) * ZOOM_FACTOR); |
819 | long time1 = (long) (selTime + (prevTime1 - selTime) * ZOOM_FACTOR); | |
837a2f8c | 820 | |
f1fae91f | 821 | long inaccuracy = (fTimeProvider.getMaxTime() - fTimeProvider.getMinTime()) - (time1 - time0); |
837a2f8c | 822 | if (inaccuracy > 0 && inaccuracy < 100) { |
f1fae91f | 823 | fTimeProvider.setStartFinishTimeNotify(fTimeProvider.getMinTime(), fTimeProvider.getMaxTime()); |
837a2f8c PT |
824 | return; |
825 | } | |
826 | ||
f1fae91f | 827 | fTimeProvider.setStartFinishTimeNotify(time0, time1); |
837a2f8c PT |
828 | } |
829 | ||
830 | /** | |
831 | * Return the currently selected trace | |
832 | * | |
833 | * @return The entry matching the trace | |
834 | */ | |
835 | public ITimeGraphEntry getSelectedTrace() { | |
836 | ITimeGraphEntry trace = null; | |
837 | int idx = getSelectedIndex(); | |
838 | if (idx >= 0) { | |
f1fae91f | 839 | trace = fItemData.fExpandedItems[idx].fTrace; |
837a2f8c PT |
840 | } |
841 | return trace; | |
842 | } | |
843 | ||
844 | /** | |
845 | * Retrieve the index of the currently selected item | |
846 | * | |
847 | * @return The index | |
848 | */ | |
849 | public int getSelectedIndex() { | |
850 | int idx = -1; | |
f1fae91f PT |
851 | for (int i = 0; i < fItemData.fExpandedItems.length; i++) { |
852 | Item item = fItemData.fExpandedItems[i]; | |
853 | if (item.fSelected) { | |
837a2f8c PT |
854 | idx = i; |
855 | break; | |
856 | } | |
857 | } | |
858 | return idx; | |
859 | } | |
860 | ||
861 | boolean toggle(int idx) { | |
862 | boolean toggled = false; | |
f1fae91f PT |
863 | if (idx >= 0 && idx < fItemData.fExpandedItems.length) { |
864 | Item item = fItemData.fExpandedItems[idx]; | |
865 | if (item.fHasChildren) { | |
866 | item.fExpanded = !item.fExpanded; | |
867 | fItemData.updateExpandedItems(); | |
837a2f8c PT |
868 | adjustScrolls(); |
869 | redraw(); | |
870 | toggled = true; | |
f1fae91f | 871 | fireTreeEvent(item.fTrace, item.fExpanded); |
837a2f8c PT |
872 | } |
873 | } | |
874 | return toggled; | |
875 | } | |
876 | ||
877 | int getItemIndexAtY(int y) { | |
878 | if (y < 0) { | |
879 | return -1; | |
880 | } | |
f1fae91f | 881 | if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) { |
837a2f8c | 882 | int ySum = 0; |
f1fae91f PT |
883 | for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) { |
884 | ySum += fItemData.fExpandedItems[idx].fItemHeight; | |
837a2f8c PT |
885 | if (y < ySum) { |
886 | return idx; | |
887 | } | |
888 | } | |
889 | return -1; | |
890 | } | |
f1fae91f PT |
891 | int idx = y / fGlobalItemHeight; |
892 | idx += fTopIndex; | |
893 | if (idx < fItemData.fExpandedItems.length) { | |
837a2f8c PT |
894 | return idx; |
895 | } | |
896 | return -1; | |
897 | } | |
898 | ||
899 | boolean isOverSplitLine(int x) { | |
f1fae91f | 900 | if (x < 0 || null == fTimeProvider) { |
837a2f8c PT |
901 | return false; |
902 | } | |
903 | int w = 4; | |
f1fae91f | 904 | int nameWidth = fTimeProvider.getNameSpace(); |
837a2f8c PT |
905 | if (x > nameWidth - w && x < nameWidth + w) { |
906 | return true; | |
907 | } | |
908 | return false; | |
909 | } | |
910 | ||
911 | ITimeGraphEntry getEntry(Point pt) { | |
912 | int idx = getItemIndexAtY(pt.y); | |
f1fae91f | 913 | return idx >= 0 ? fItemData.fExpandedItems[idx].fTrace : null; |
837a2f8c PT |
914 | } |
915 | ||
713a70ae | 916 | /** |
713a70ae PT |
917 | * @since 2.0 |
918 | */ | |
4999a196 | 919 | @Override |
713a70ae | 920 | public int getXForTime(long time) { |
f1fae91f | 921 | if (null == fTimeProvider) { |
713a70ae PT |
922 | return -1; |
923 | } | |
f1fae91f PT |
924 | long time0 = fTimeProvider.getTime0(); |
925 | long time1 = fTimeProvider.getTime1(); | |
713a70ae | 926 | int width = getCtrlSize().x; |
f1fae91f | 927 | int nameSpace = fTimeProvider.getNameSpace(); |
713a70ae PT |
928 | double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0); |
929 | int x = getBounds().x + nameSpace + (int) ((time - time0) * pixelsPerNanoSec); | |
930 | return x; | |
931 | } | |
932 | ||
933 | /** | |
713a70ae PT |
934 | * @since 2.0 |
935 | */ | |
4999a196 | 936 | @Override |
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 |