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