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