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