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