tmf: Improve SS Explorer filter button description
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / TimeGraphControl.java
CommitLineData
837a2f8c 1/*****************************************************************************
351a2391 2 * Copyright (c) 2007, 2014 Intel Corporation and others
4999a196 3 *
837a2f8c
PT
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
4999a196
GB
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
bec1f1ac 16 * Add display of links between items
351a2391 17 * Xavier Raynaud, Kalray - Code optimization
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;
0fcf3b09 38import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
f566d40a 39import org.eclipse.linuxtools.tmf.core.timestamp.TmfNanoTimestamp;
0fcf3b09 40import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampDelta;
496f76d3 41import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphColorListener;
837a2f8c 42import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;
4999a196 43import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
f2ca0f69 44import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphTimeListener;
837a2f8c
PT
45import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphTreeListener;
46import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.StateItem;
f2ca0f69 47import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphTimeEvent;
837a2f8c 48import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.TimeGraphTreeExpansionEvent;
bec1f1ac 49import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ILinkEvent;
837a2f8c
PT
50import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
51import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
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.
1156 * @since 3.1
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
1644 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
1645 if (colorIdx < 0) {
1646 return false;
1647 }
1648 boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true;
1649
1650 if (visible) {
1651 Color stateColor = null;
1652 if (colorIdx < fEventColorMap.length) {
1653 stateColor = fEventColorMap[colorIdx];
1654 } else {
1655 stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
1656 }
1657
1658 gc.setForeground(stateColor);
1659 gc.setBackground(stateColor);
1660
1661 /* Draw the arrow */
1662 gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
1663 drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc);
1664
1665 }
1666 fTimeGraphProvider.postDrawEvent(event, rect, gc);
1667 return visible;
1668 }
1669
1670 /*
1671 * @author Francis Giraldeau
1672 *
1673 * Inspiration:
1674 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1675 *
1676 * The algorithm was taken from this site, not the code itself
1677 */
1678 private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc)
1679 {
1680 int factor = 10;
1681 double cos = 0.9510;
1682 double sin = 0.3090;
03ab8eeb
PT
1683 long lenx = x1 - x0;
1684 long leny = y1 - y0;
bec1f1ac
GB
1685 double len = Math.sqrt(lenx * lenx + leny * leny);
1686
1687 double dx = factor * lenx / len;
1688 double dy = factor * leny / len;
1689 int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin)));
1690 int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos)));
1691 int end2X = (int) Math.round((x1 - (dx * cos + dy * sin)));
1692 int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos)));
1693 int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 };
1694 gc.fillPolygon(arrow);
1695 }
1696
a0a88f65
AM
1697 /**
1698 * Draw the name of an item.
1699 *
1700 * @param item
1701 * Item object
1702 * @param bounds
1703 * Where to draw the name
1704 * @param gc
1705 * Graphics context
1706 */
837a2f8c 1707 protected void drawName(Item item, Rectangle bounds, GC gc) {
70e10acc 1708 boolean hasTimeEvents = item.fEntry.hasTimeEvents();
837a2f8c 1709 if (! hasTimeEvents) {
f1fae91f 1710 gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus));
837a2f8c 1711 gc.fillRectangle(bounds);
f1fae91f
PT
1712 if (item.fSelected && fIsInFocus) {
1713 gc.setForeground(getColorScheme().getBkColor(item.fSelected, fIsInFocus, false));
837a2f8c
PT
1714 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1, bounds.height - 1);
1715 }
1716 } else {
f1fae91f
PT
1717 gc.setBackground(getColorScheme().getBkColor(item.fSelected, fIsInFocus, true));
1718 gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus));
837a2f8c
PT
1719 gc.fillRectangle(bounds);
1720 }
1721
1722 // No name to be drawn
f1fae91f 1723 if (fTimeProvider.getNameSpace() == 0) {
837a2f8c
PT
1724 return;
1725 }
1726
f1fae91f
PT
1727 int leftMargin = MARGIN + item.fLevel * EXPAND_SIZE;
1728 if (item.fHasChildren) {
1729 gc.setForeground(getColorScheme().getFgColorGroup(false, false));
1730 gc.setBackground(getColorScheme().getBkColor(false, false, false));
837a2f8c
PT
1731 Rectangle rect = Utils.clone(bounds);
1732 rect.x += leftMargin;
1733 rect.y += (bounds.height - EXPAND_SIZE) / 2;
1734 rect.width = EXPAND_SIZE;
1735 rect.height = EXPAND_SIZE;
1736 gc.fillRectangle(rect);
1737 gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
1738 int midy = rect.y + rect.height / 2;
1739 gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy);
f1fae91f 1740 if (!item.fExpanded) {
837a2f8c
PT
1741 int midx = rect.x + rect.width / 2;
1742 gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3);
1743 }
1744 }
1745 leftMargin += EXPAND_SIZE + MARGIN;
1746
70e10acc 1747 Image img = fTimeGraphProvider.getItemImage(item.fEntry);
837a2f8c
PT
1748 if (img != null) {
1749 // draw icon
1750 int imgHeight = img.getImageData().height;
1751 int imgWidth = img.getImageData().width;
1752 int x = leftMargin;
1753 int y = bounds.y + (bounds.height - imgHeight) / 2;
1754 gc.drawImage(img, x, y);
1755 leftMargin += imgWidth + MARGIN;
1756 }
f1fae91f 1757 String name = item.fName;
837a2f8c 1758 Point size = gc.stringExtent(name);
f1fae91f
PT
1759 if (fIdealNameSpace < leftMargin + size.x + MARGIN) {
1760 fIdealNameSpace = leftMargin + size.x + MARGIN;
837a2f8c
PT
1761 }
1762 if (hasTimeEvents) {
1763 // cut long string with "..."
1764 int width = bounds.width - leftMargin;
1765 int cuts = 0;
1766 while (size.x > width && name.length() > 1) {
1767 cuts++;
1768 name = name.substring(0, name.length() - 1);
1769 size = gc.stringExtent(name + "..."); //$NON-NLS-1$
1770 }
1771 if (cuts > 0) {
1772 name += "..."; //$NON-NLS-1$
1773 }
1774 }
1775 Rectangle rect = Utils.clone(bounds);
1776 rect.x += leftMargin;
1777 rect.width -= leftMargin;
1778 // draw text
1779 if (rect.width > 0) {
1780 rect.y += (bounds.height - gc.stringExtent(name).y) / 2;
f1fae91f 1781 gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus));
837a2f8c
PT
1782 int textWidth = Utils.drawText(gc, name, rect, true);
1783 leftMargin += textWidth + MARGIN;
1784 rect.y -= 2;
1785
1786 if (hasTimeEvents) {
1787 // draw middle line
1788 int x = bounds.x + leftMargin;
1789 int width = bounds.width - x;
1790 int midy = bounds.y + bounds.height / 2;
f1fae91f 1791 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
837a2f8c
PT
1792 gc.drawLine(x, midy, x + width, midy);
1793 }
1794 }
1795 }
1796
a0a88f65
AM
1797 /**
1798 * Draw the state (color fill)
1799 *
1800 * @param colors
1801 * Color scheme
1802 * @param event
1803 * Time event for which we're drawing the state
1804 * @param rect
1805 * Where to draw
1806 * @param gc
1807 * Graphics context
1808 * @param selected
1809 * Is this time event currently selected (so it appears
1810 * highlighted)
1811 * @param timeSelected
1812 * Is the timestamp currently selected
ad128fd8
PT
1813 * @return true if the state was drawn
1814 * @since 2.0
a0a88f65 1815 */
ad128fd8 1816 protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event,
837a2f8c
PT
1817 Rectangle rect, GC gc, boolean selected, boolean timeSelected) {
1818
1819 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
ad128fd8
PT
1820 if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) {
1821 return false;
837a2f8c
PT
1822 }
1823 boolean visible = rect.width == 0 ? false : true;
351a2391
XR
1824 Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
1825 gc.setForeground(black);
837a2f8c
PT
1826
1827 if (visible) {
ad128fd8
PT
1828 if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) {
1829 // Only draw the top and bottom borders
ad128fd8
PT
1830 gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y);
1831 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
1832 if (rect.width == 1) {
ad128fd8
PT
1833 gc.drawPoint(rect.x, rect.y - 2);
1834 }
1835 return false;
1836 }
837a2f8c
PT
1837 Color stateColor = null;
1838 if (colorIdx < fEventColorMap.length) {
1839 stateColor = fEventColorMap[colorIdx];
1840 } else {
351a2391 1841 stateColor = black;
837a2f8c
PT
1842 }
1843
41b5c37f 1844 boolean reallySelected = timeSelected && selected;
837a2f8c
PT
1845 // fill all rect area
1846 gc.setBackground(stateColor);
1847 gc.fillRectangle(rect);
837a2f8c 1848
351a2391
XR
1849 if (reallySelected) {
1850 gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1);
1851 gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height);
837a2f8c
PT
1852 }
1853 } else {
837a2f8c 1854 gc.drawPoint(rect.x, rect.y - 2);
837a2f8c
PT
1855 }
1856 fTimeGraphProvider.postDrawEvent(event, rect, gc);
ad128fd8 1857 return visible;
837a2f8c
PT
1858 }
1859
a0a88f65
AM
1860 /**
1861 * Fill the space between two contiguous time events
1862 *
1863 * @param rect
1864 * Rectangle to fill
1865 * @param gc
1866 * Graphics context
1867 * @param selected
1868 * Is this time event selected or not
1869 */
837a2f8c 1870 protected void fillSpace(Rectangle rect, GC gc, boolean selected) {
f1fae91f 1871 gc.setBackground(getColorScheme().getBkColor(selected, fIsInFocus, false));
837a2f8c 1872 gc.fillRectangle(rect);
f1fae91f
PT
1873 if (fDragState == DRAG_ZOOM) {
1874 gc.setBackground(getColorScheme().getBkColor(selected, fIsInFocus, true));
1875 if (fDragX0 < fDragX) {
1876 gc.fillRectangle(new Rectangle(fDragX0, rect.y, fDragX - fDragX0, rect.height));
1877 } else if (fDragX0 > fDragX) {
1878 gc.fillRectangle(new Rectangle(fDragX, rect.y, fDragX0 - fDragX, rect.height));
5b2b9bd7
PT
1879 }
1880 }
837a2f8c 1881 // draw middle line
f1fae91f 1882 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
837a2f8c
PT
1883 int midy = rect.y + rect.height / 2;
1884 gc.drawLine(rect.x, midy, rect.x + rect.width, midy);
1885 }
1886
1887 @Override
1888 public void keyTraversed(TraverseEvent e) {
1889 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
1890 e.doit = true;
1891 }
1892 }
1893
1894 @Override
1895 public void keyPressed(KeyEvent e) {
1896 int idx = -1;
f1fae91f 1897 if (fItemData.fExpandedItems.length == 0) {
837a2f8c
PT
1898 return;
1899 }
1900 if (SWT.HOME == e.keyCode) {
1901 idx = 0;
1902 } else if (SWT.END == e.keyCode) {
f1fae91f 1903 idx = fItemData.fExpandedItems.length - 1;
837a2f8c
PT
1904 } else if (SWT.ARROW_DOWN == e.keyCode) {
1905 idx = getSelectedIndex();
1906 if (idx < 0) {
1907 idx = 0;
f1fae91f 1908 } else if (idx < fItemData.fExpandedItems.length - 1) {
837a2f8c
PT
1909 idx++;
1910 }
1911 } else if (SWT.ARROW_UP == e.keyCode) {
1912 idx = getSelectedIndex();
1913 if (idx < 0) {
1914 idx = 0;
1915 } else if (idx > 0) {
1916 idx--;
1917 }
1918 } else if (SWT.ARROW_LEFT == e.keyCode) {
1919 selectPrevEvent();
1920 } else if (SWT.ARROW_RIGHT == e.keyCode) {
1921 selectNextEvent();
1922 } else if (SWT.PAGE_DOWN == e.keyCode) {
1923 int page = countPerPage();
1924 idx = getSelectedIndex();
1925 if (idx < 0) {
1926 idx = 0;
1927 }
1928 idx += page;
f1fae91f
PT
1929 if (idx >= fItemData.fExpandedItems.length) {
1930 idx = fItemData.fExpandedItems.length - 1;
837a2f8c
PT
1931 }
1932 } else if (SWT.PAGE_UP == e.keyCode) {
1933 int page = countPerPage();
1934 idx = getSelectedIndex();
1935 if (idx < 0) {
1936 idx = 0;
1937 }
1938 idx -= page;
1939 if (idx < 0) {
1940 idx = 0;
1941 }
1942 } else if (SWT.CR == e.keyCode) {
1943 idx = getSelectedIndex();
1944 if (idx >= 0) {
f1fae91f 1945 if (fItemData.fExpandedItems[idx].fHasChildren) {
837a2f8c
PT
1946 toggle(idx);
1947 } else {
1948 fireDefaultSelection();
1949 }
1950 }
1951 idx = -1;
1952 }
1953 if (idx >= 0) {
1954 selectItem(idx, false);
1955 fireSelectionChanged();
1956 }
0fcf3b09
PT
1957 int x = toControl(e.display.getCursorLocation()).x;
1958 updateCursor(x, e.stateMask | e.keyCode);
837a2f8c
PT
1959 }
1960
1961 @Override
1962 public void keyReleased(KeyEvent e) {
0fcf3b09
PT
1963 int x = toControl(e.display.getCursorLocation()).x;
1964 updateCursor(x, e.stateMask & ~e.keyCode);
837a2f8c
PT
1965 }
1966
1967 @Override
1968 public void focusGained(FocusEvent e) {
f1fae91f
PT
1969 fIsInFocus = true;
1970 if (fMouseScrollFilterListener == null) {
1971 fMouseScrollFilterListener = new Listener() {
837a2f8c
PT
1972 // This filter is used to prevent horizontal scrolling of the view
1973 // when the mouse wheel is used to zoom
1974 @Override
1975 public void handleEvent(Event event) {
1976 event.doit = false;
1977 }
1978 };
f1fae91f 1979 getDisplay().addFilter(SWT.MouseWheel, fMouseScrollFilterListener);
837a2f8c
PT
1980 }
1981 redraw();
0fcf3b09 1982 updateStatusLine(NO_STATUS);
837a2f8c
PT
1983 }
1984
1985 @Override
1986 public void focusLost(FocusEvent e) {
f1fae91f
PT
1987 fIsInFocus = false;
1988 if (fMouseScrollFilterListener != null) {
1989 getDisplay().removeFilter(SWT.MouseWheel, fMouseScrollFilterListener);
1990 fMouseScrollFilterListener = null;
837a2f8c 1991 }
f1fae91f 1992 if (DRAG_NONE != fDragState) {
837a2f8c 1993 setCapture(false);
f1fae91f 1994 fDragState = DRAG_NONE;
837a2f8c
PT
1995 }
1996 redraw();
0fcf3b09 1997 updateStatusLine(NO_STATUS);
837a2f8c
PT
1998 }
1999
2000 /**
2001 * @return If the current view is focused
2002 */
2003 public boolean isInFocus() {
f1fae91f 2004 return fIsInFocus;
837a2f8c
PT
2005 }
2006
2007 /**
2008 * Provide the possibility to control the wait cursor externally e.g. data
2009 * requests in progress
2010 *
2011 * @param waitInd Should we wait indefinitely?
2012 */
2013 public void waitCursor(boolean waitInd) {
2014 // Update cursor as indicated
2015 if (waitInd) {
f1fae91f 2016 setCursor(fWaitCursor);
837a2f8c
PT
2017 } else {
2018 setCursor(null);
837a2f8c 2019 }
0fcf3b09 2020 }
837a2f8c 2021
0fcf3b09
PT
2022 private void updateCursor(int x, int stateMask) {
2023 // if Wait cursor not active, check for the need to change the cursor
2024 if (getCursor() == fWaitCursor) {
2025 return;
2026 }
2027 Cursor cursor = null;
2028 if (fDragState == DRAG_SPLIT_LINE) {
2029 } else if (fDragState == DRAG_SELECTION) {
2030 cursor = fResizeCursor;
2031 } else if (fDragState == DRAG_TRACE_ITEM) {
2032 cursor = fDragCursor;
2033 } else if (fDragState == DRAG_ZOOM) {
2034 cursor = fZoomCursor;
2035 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
2036 cursor = fDragCursor;
2037 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2038 cursor = fResizeCursor;
baf92cac
AM
2039 } else if (!isOverSplitLine(x)) {
2040 long selectionBegin = fTimeProvider.getSelectionBegin();
2041 long selectionEnd = fTimeProvider.getSelectionEnd();
2042 int xBegin = getXForTime(selectionBegin);
2043 int xEnd = getXForTime(selectionEnd);
2044 if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) {
2045 cursor = fResizeCursor;
0fcf3b09
PT
2046 }
2047 }
2048 if (getCursor() != cursor) {
2049 setCursor(cursor);
2050 }
837a2f8c
PT
2051 }
2052
0fcf3b09 2053 private void updateStatusLine(int x) {
407bfdd5
PT
2054 if (fStatusLineManager == null || null == fTimeProvider ||
2055 fTimeProvider.getTime0() == fTimeProvider.getTime1()) {
0fcf3b09
PT
2056 return;
2057 }
2058 StringBuilder message = new StringBuilder();
2059 if (x >= 0 && fDragState == DRAG_NONE) {
2060 long time = getTimeAtX(x);
2061 if (time >= 0) {
2062 message.append("T: "); //$NON-NLS-1$
f566d40a 2063 message.append(new TmfNanoTimestamp(time).toString());
0fcf3b09 2064 message.append(" T1: "); //$NON-NLS-1$
baf92cac
AM
2065 long selectionBegin = fTimeProvider.getSelectionBegin();
2066 long selectionEnd = fTimeProvider.getSelectionEnd();
2067 message.append(new TmfNanoTimestamp(Math.min(selectionBegin, selectionEnd)).toString());
2068 if (selectionBegin != selectionEnd) {
2069 message.append(" T2: "); //$NON-NLS-1$
2070 message.append(new TmfNanoTimestamp(Math.max(selectionBegin, selectionEnd)).toString());
2071 message.append(" \u0394: "); //$NON-NLS-1$
2072 message.append(new TmfTimestampDelta(Math.abs(selectionBegin - selectionEnd), ITmfTimestamp.NANOSECOND_SCALE));
0fcf3b09
PT
2073 }
2074 }
2075 } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) {
2076 long time0 = fDragTime0;
2077 long time = getTimeAtX(fDragX);
2078 message.append("T1: "); //$NON-NLS-1$
f566d40a 2079 message.append(new TmfNanoTimestamp(Math.min(time, time0)).toString());
0fcf3b09
PT
2080 if (time != time0) {
2081 message.append(" T2: "); //$NON-NLS-1$
f566d40a 2082 message.append(new TmfNanoTimestamp(Math.max(time, time0)).toString());
0fcf3b09
PT
2083 message.append(" \u0394: "); //$NON-NLS-1$
2084 message.append(new TmfTimestampDelta(Math.abs(time - time0), ITmfTimestamp.NANOSECOND_SCALE));
837a2f8c
PT
2085 }
2086 }
0fcf3b09 2087 fStatusLineManager.setMessage(message.toString());
837a2f8c
PT
2088 }
2089
2090 @Override
2091 public void mouseMove(MouseEvent e) {
f1fae91f 2092 if (null == fTimeProvider) {
837a2f8c
PT
2093 return;
2094 }
2095 Point size = getCtrlSize();
f1fae91f
PT
2096 if (DRAG_TRACE_ITEM == fDragState) {
2097 int nameWidth = fTimeProvider.getNameSpace();
0fcf3b09
PT
2098 if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) {
2099 fDragX = e.x;
f1fae91f
PT
2100 double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak);
2101 long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec));
2102 long time1 = fTime1bak - timeDelta;
2103 long maxTime = fTimeProvider.getMaxTime();
837a2f8c
PT
2104 if (time1 > maxTime) {
2105 time1 = maxTime;
2106 }
f1fae91f
PT
2107 long time0 = time1 - (fTime1bak - fTime0bak);
2108 if (time0 < fTimeProvider.getMinTime()) {
2109 time0 = fTimeProvider.getMinTime();
2110 time1 = time0 + (fTime1bak - fTime0bak);
837a2f8c 2111 }
f1fae91f 2112 fTimeProvider.setStartFinishTime(time0, time1);
837a2f8c 2113 }
f1fae91f
PT
2114 } else if (DRAG_SPLIT_LINE == fDragState) {
2115 fDragX = e.x;
2116 fTimeProvider.setNameSpace(e.x);
0fcf3b09 2117 } else if (DRAG_SELECTION == fDragState) {
baf92cac
AM
2118 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2119 redraw();
2120 fTimeGraphScale.setDragRange(fDragX0, fDragX);
f2ca0f69 2121 fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX));
f1fae91f
PT
2122 } else if (DRAG_ZOOM == fDragState) {
2123 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
5b2b9bd7 2124 redraw();
0fcf3b09 2125 fTimeGraphScale.setDragRange(fDragX0, fDragX);
f1fae91f 2126 } else if (DRAG_NONE == fDragState) {
837a2f8c 2127 boolean mouseOverSplitLine = isOverSplitLine(e.x);
f1fae91f 2128 if (fMouseOverSplitLine != mouseOverSplitLine) {
837a2f8c
PT
2129 redraw();
2130 }
f1fae91f 2131 fMouseOverSplitLine = mouseOverSplitLine;
837a2f8c 2132 }
0fcf3b09
PT
2133 updateCursor(e.x, e.stateMask);
2134 updateStatusLine(e.x);
837a2f8c
PT
2135 }
2136
2137 @Override
2138 public void mouseDoubleClick(MouseEvent e) {
f1fae91f 2139 if (null == fTimeProvider) {
837a2f8c
PT
2140 return;
2141 }
5b2b9bd7 2142 if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) {
f1fae91f
PT
2143 if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) {
2144 fTimeProvider.setNameSpace(fIdealNameSpace);
837a2f8c 2145 boolean mouseOverSplitLine = isOverSplitLine(e.x);
f1fae91f 2146 if (fMouseOverSplitLine != mouseOverSplitLine) {
837a2f8c
PT
2147 redraw();
2148 }
f1fae91f 2149 fMouseOverSplitLine = mouseOverSplitLine;
837a2f8c
PT
2150 return;
2151 }
2152 int idx = getItemIndexAtY(e.y);
2153 if (idx >= 0) {
2154 selectItem(idx, false);
2155 fireDefaultSelection();
2156 }
2157 }
2158 }
2159
2160 @Override
2161 public void mouseDown(MouseEvent e) {
407bfdd5
PT
2162 if (fDragState != DRAG_NONE || null == fTimeProvider ||
2163 fTimeProvider.getTime0() == fTimeProvider.getTime1() ||
2164 getCtrlSize().x - fTimeProvider.getNameSpace() <= 0) {
837a2f8c
PT
2165 return;
2166 }
2167 int idx;
0fcf3b09 2168 if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) {
f1fae91f
PT
2169 int nameSpace = fTimeProvider.getNameSpace();
2170 if (nameSpace != 0 && isOverSplitLine(e.x)) {
2171 fDragState = DRAG_SPLIT_LINE;
0fcf3b09 2172 fDragButton = e.button;
f1fae91f
PT
2173 fDragX = e.x;
2174 fDragX0 = fDragX;
2175 fTime0bak = fTimeProvider.getTime0();
2176 fTime1bak = fTimeProvider.getTime1();
2177 redraw();
0fcf3b09 2178 updateCursor(e.x, e.stateMask);
f1fae91f 2179 return;
837a2f8c 2180 }
0fcf3b09
PT
2181 }
2182 if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) {
2183 int nameSpace = fTimeProvider.getNameSpace();
837a2f8c
PT
2184 idx = getItemIndexAtY(e.y);
2185 if (idx >= 0) {
f1fae91f
PT
2186 Item item = fItemData.fExpandedItems[idx];
2187 if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) {
837a2f8c 2188 toggle(idx);
0fcf3b09 2189 return;
837a2f8c
PT
2190 }
2191 selectItem(idx, false);
2192 fireSelectionChanged();
2193 } else {
2194 selectItem(idx, false); // clear selection
837a2f8c
PT
2195 fireSelectionChanged();
2196 }
0fcf3b09
PT
2197 long hitTime = getTimeAtX(e.x);
2198 if (hitTime >= 0) {
2199 setCapture(true);
2200
2201 fDragState = DRAG_SELECTION;
2202 fDragButton = e.button;
2203 fDragX = e.x;
2204 fDragX0 = fDragX;
2205 fDragTime0 = getTimeAtX(fDragX0);
baf92cac
AM
2206 long selectionBegin = fTimeProvider.getSelectionBegin();
2207 long selectionEnd = fTimeProvider.getSelectionEnd();
2208 int xBegin = getXForTime(selectionBegin);
2209 int xEnd = getXForTime(selectionEnd);
2210 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2211 long time = getTimeAtX(e.x);
2212 if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) {
2213 fDragX0 = xEnd;
2214 fDragTime0 = selectionEnd;
0fcf3b09 2215 } else {
baf92cac
AM
2216 fDragX0 = xBegin;
2217 fDragTime0 = selectionBegin;
2218 }
2219 } else {
2220 long time = getTimeAtX(e.x);
2221 if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) {
2222 fDragX0 = xEnd;
2223 fDragTime0 = selectionEnd;
2224 } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) {
2225 fDragX0 = xBegin;
2226 fDragTime0 = selectionBegin;
0fcf3b09
PT
2227 }
2228 }
2229 fTime0bak = fTimeProvider.getTime0();
2230 fTime1bak = fTimeProvider.getTime1();
2231 redraw();
2232 updateCursor(e.x, e.stateMask);
2233 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2234 }
2235 } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) {
2236 long hitTime = getTimeAtX(e.x);
2237 if (hitTime > 0) {
2238 setCapture(true);
2239 fDragState = DRAG_TRACE_ITEM;
2240 fDragButton = e.button;
2241 fDragX = e.x;
2242 fDragX0 = fDragX;
2243 fTime0bak = fTimeProvider.getTime0();
2244 fTime1bak = fTimeProvider.getTime1();
2245 updateCursor(e.x, e.stateMask);
2246 }
5b2b9bd7 2247 } else if (3 == e.button) {
5b2b9bd7 2248 setCapture(true);
f1fae91f
PT
2249 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getCtrlSize().x - RIGHT_MARGIN);
2250 fDragX0 = fDragX;
2251 fDragState = DRAG_ZOOM;
0fcf3b09
PT
2252 fDragButton = e.button;
2253 redraw();
2254 updateCursor(e.x, e.stateMask);
2255 fTimeGraphScale.setDragRange(fDragX0, fDragX);
837a2f8c
PT
2256 }
2257 }
2258
2259 @Override
2260 public void mouseUp(MouseEvent e) {
0b5a90a0
PT
2261 if (fPendingMenuDetectEvent != null && e.button == 3) {
2262 menuDetected(fPendingMenuDetectEvent);
2263 }
f1fae91f 2264 if (DRAG_NONE != fDragState) {
837a2f8c 2265 setCapture(false);
0fcf3b09
PT
2266 if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) {
2267 if (fDragX != fDragX0) {
2268 fTimeProvider.notifyStartFinishTime();
2269 }
2270 fDragState = DRAG_NONE;
2271 } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) {
2272 fDragState = DRAG_NONE;
2273 redraw();
2274 } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) {
2275 if (fDragX == fDragX0) { // click without selecting anything
837a2f8c 2276 long time = getTimeAtX(e.x);
f1fae91f 2277 fTimeProvider.setSelectedTimeNotify(time, false);
e1725a05 2278 } else {
0fcf3b09
PT
2279 long time0 = fDragTime0;
2280 long time1 = getTimeAtX(fDragX);
2281 if (time0 <= time1) {
baf92cac 2282 fTimeProvider.setSelectionRangeNotify(time0, time1);
0fcf3b09 2283 } else {
baf92cac 2284 fTimeProvider.setSelectionRangeNotify(time1, time0);
0fcf3b09 2285 }
837a2f8c 2286 }
f1fae91f 2287 fDragState = DRAG_NONE;
837a2f8c 2288 redraw();
0fcf3b09
PT
2289 fTimeGraphScale.setDragRange(-1, -1);
2290 } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) {
f1fae91f
PT
2291 int nameWidth = fTimeProvider.getNameSpace();
2292 if (Math.max(fDragX, fDragX0) > nameWidth && fDragX != fDragX0) {
2293 long time0 = getTimeAtX(fDragX0);
2294 long time1 = getTimeAtX(fDragX);
5b2b9bd7 2295 if (time0 < time1) {
f1fae91f 2296 fTimeProvider.setStartFinishTimeNotify(time0, time1);
5b2b9bd7 2297 } else {
f1fae91f 2298 fTimeProvider.setStartFinishTimeNotify(time1, time0);
5b2b9bd7
PT
2299 }
2300 } else {
2301 redraw();
2302 }
f1fae91f 2303 fDragState = DRAG_NONE;
0fcf3b09 2304 fTimeGraphScale.setDragRange(-1, -1);
837a2f8c 2305 }
837a2f8c 2306 }
0fcf3b09
PT
2307 updateCursor(e.x, e.stateMask);
2308 updateStatusLine(e.x);
837a2f8c
PT
2309 }
2310
2311 @Override
2312 public void mouseEnter(MouseEvent e) {
2313 }
2314
2315 @Override
2316 public void mouseExit(MouseEvent e) {
f1fae91f
PT
2317 if (fMouseOverSplitLine) {
2318 fMouseOverSplitLine = false;
837a2f8c
PT
2319 redraw();
2320 }
0fcf3b09 2321 updateStatusLine(NO_STATUS);
837a2f8c
PT
2322 }
2323
2324 @Override
2325 public void mouseHover(MouseEvent e) {
2326 }
2327
2328 @Override
2329 public void mouseScrolled(MouseEvent e) {
f1fae91f 2330 if ((fMouseScrollFilterListener == null) || fDragState != DRAG_NONE) {
837a2f8c
PT
2331 return;
2332 }
2333 boolean zoomScroll = false;
2334 Point p = getParent().toControl(getDisplay().getCursorLocation());
2335 Point parentSize = getParent().getSize();
2336 if (p.x >= 0 && p.x < parentSize.x && p.y >= 0 && p.y < parentSize.y) {
2337 // over the parent control
2338 if (e.x > getCtrlSize().x) {
2339 // over the horizontal scroll bar
2340 zoomScroll = false;
f1fae91f 2341 } else if (e.y >= 0 && e.y < getCtrlSize().y && e.x < fTimeProvider.getNameSpace()) {
837a2f8c
PT
2342 // over the name space
2343 zoomScroll = false;
2344 } else {
2345 zoomScroll = true;
2346 }
2347 }
f1fae91f 2348 if (zoomScroll && fTimeProvider.getTime0() != fTimeProvider.getTime1()) {
837a2f8c
PT
2349 if (e.count > 0) {
2350 zoom(true);
2351 } else if (e.count < 0) {
2352 zoom(false);
2353 }
2354 } else {
2355 setTopIndex(getTopIndex() - e.count);
2356 }
2357 }
2358
2359 @Override
2360 public void controlMoved(ControlEvent e) {
2361 }
2362
2363 @Override
2364 public void controlResized(ControlEvent e) {
2365 adjustScrolls();
2366 }
2367
2368 @Override
2369 public void widgetDefaultSelected(SelectionEvent e) {
2370 }
2371
2372 @Override
2373 public void widgetSelected(SelectionEvent e) {
2374 if (e.widget == getVerticalBar()) {
2375 setTopIndex(getVerticalBar().getSelection());
f1fae91f 2376 } else if (e.widget == getHorizontalBar() && null != fTimeProvider) {
837a2f8c 2377 int start = getHorizontalBar().getSelection();
f1fae91f
PT
2378 long time0 = fTimeProvider.getTime0();
2379 long time1 = fTimeProvider.getTime1();
2380 long timeMin = fTimeProvider.getMinTime();
2381 long timeMax = fTimeProvider.getMaxTime();
837a2f8c
PT
2382 long delta = timeMax - timeMin;
2383
2384 long range = time1 - time0;
837a2f8c
PT
2385 time0 = timeMin + Math.round(delta * ((double) start / H_SCROLLBAR_MAX));
2386 time1 = time0 + range;
2387
2388 // TODO: Follow-up with Bug 310310
2389 // In Linux SWT.DRAG is the only value received
2390 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2391 if (e.detail == SWT.DRAG) {
f1fae91f 2392 fTimeProvider.setStartFinishTime(time0, time1);
837a2f8c 2393 } else {
f1fae91f 2394 fTimeProvider.setStartFinishTimeNotify(time0, time1);
837a2f8c
PT
2395 }
2396 }
2397 }
2398
837a2f8c
PT
2399 @Override
2400 public int getBorderWidth() {
f1fae91f 2401 return fBorderWidth;
837a2f8c
PT
2402 }
2403
2404 /**
2405 * Set the border width
2406 *
2407 * @param borderWidth
2408 * The width
2409 */
2410 public void setBorderWidth(int borderWidth) {
f1fae91f 2411 this.fBorderWidth = borderWidth;
837a2f8c
PT
2412 }
2413
2414 /**
2415 * @return The current height of the header row
2416 */
2417 public int getHeaderHeight() {
f1fae91f 2418 return fHeaderHeight;
837a2f8c
PT
2419 }
2420
2421 /**
2422 * Set the height of the header row
2423 *
2424 * @param headerHeight
2425 * The height
2426 */
2427 public void setHeaderHeight(int headerHeight) {
f1fae91f 2428 this.fHeaderHeight = headerHeight;
837a2f8c
PT
2429 }
2430
2431 /**
c004295c 2432 * @return The default height of regular item rows
837a2f8c
PT
2433 */
2434 public int getItemHeight() {
f1fae91f 2435 return fGlobalItemHeight;
837a2f8c
PT
2436 }
2437
2438 /**
c004295c 2439 * Set the default height of regular item rows.
837a2f8c
PT
2440 *
2441 * @param rowHeight
2442 * The height
2443 */
2444 public void setItemHeight(int rowHeight) {
f1fae91f 2445 this.fGlobalItemHeight = rowHeight;
837a2f8c
PT
2446 }
2447
c004295c
PT
2448 /**
2449 * Set the height of a specific item. Overrides the default item height.
2450 *
2451 * @param entry
2452 * A time graph entry
2453 * @param rowHeight
2454 * The height
2455 * @return true if the height is successfully stored, false otherwise
2456 *
2457 * @since 2.1
2458 */
2459 public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) {
2460 Item item = fItemData.findItem(entry);
2461 if (item != null) {
2462 item.fItemHeight = rowHeight;
2463 return true;
2464 }
2465 return false;
2466 }
2467
837a2f8c
PT
2468 /**
2469 * Set the minimum item width
2470 *
2471 * @param width The minimum width
2472 */
2473 public void setMinimumItemWidth(int width) {
f1fae91f 2474 this.fMinimumItemWidth = width;
837a2f8c
PT
2475 }
2476
2477 /**
2478 * @return The minimum item width
2479 */
2480 public int getMinimumItemWidth() {
f1fae91f 2481 return fMinimumItemWidth;
837a2f8c
PT
2482 }
2483
2484 /**
2485 * @return The entries that are currently filtered out
f1fae91f
PT
2486 *
2487 * @since 2.0
837a2f8c 2488 */
f1fae91f
PT
2489 public List<ITimeGraphEntry> getFilteredOut() {
2490 return fItemData.getFilteredOut();
837a2f8c
PT
2491 }
2492
837a2f8c
PT
2493 @Override
2494 public void addSelectionChangedListener(ISelectionChangedListener listener) {
f1fae91f
PT
2495 if (listener != null && !fSelectionChangedListeners.contains(listener)) {
2496 fSelectionChangedListeners.add(listener);
837a2f8c
PT
2497 }
2498 }
2499
837a2f8c
PT
2500 @Override
2501 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
2502 if (listener != null) {
f1fae91f 2503 fSelectionChangedListeners.remove(listener);
837a2f8c
PT
2504 }
2505 }
2506
837a2f8c
PT
2507 @Override
2508 public void setSelection(ISelection selection) {
2509 if (selection instanceof TimeGraphSelection) {
2510 TimeGraphSelection sel = (TimeGraphSelection) selection;
2511 Object ob = sel.getFirstElement();
2512 if (ob instanceof ITimeGraphEntry) {
2513 ITimeGraphEntry trace = (ITimeGraphEntry) ob;
2514 selectItem(trace, false);
2515 }
2516 }
2517
2518 }
2519
6ac5a950
AM
2520 /**
2521 * @param filter The filter object to be attached to the view
2522 * @since 2.0
2523 */
2524 public void addFilter(ViewerFilter filter) {
f1fae91f
PT
2525 if (!fFilters.contains(filter)) {
2526 fFilters.add(filter);
6ac5a950
AM
2527 }
2528 }
2529
2530 /**
2531 * @param filter The filter object to be attached to the view
2532 * @since 2.0
2533 */
2534 public void removeFilter(ViewerFilter filter) {
f1fae91f 2535 fFilters.remove(filter);
6ac5a950
AM
2536 }
2537
c4767854
AM
2538 /**
2539 * @since 3.0
2540 */
496f76d3
GB
2541 @Override
2542 public void colorSettingsChanged(StateItem[] stateItems) {
2543 /* Destroy previous colors from the resource manager */
517fe01b
EB
2544 if (fEventColorMap != null) {
2545 for (Color color : fEventColorMap) {
2546 fResourceManager.destroyColor(color.getRGB());
2547 }
496f76d3
GB
2548 }
2549 if (stateItems != null) {
2550 fEventColorMap = new Color[stateItems.length];
2551 for (int i = 0; i < stateItems.length; i++) {
2552 fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor());
2553 }
2554 } else {
2555 fEventColorMap = new Color[] { };
2556 }
2557 redraw();
2558 }
2559
837a2f8c 2560 private class ItemData {
70e10acc 2561 private final Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>();
f1fae91f
PT
2562 private Item[] fExpandedItems = new Item[0];
2563 private Item[] fItems = new Item[0];
70e10acc 2564 private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0];
507b1336 2565 private List<ILinkEvent> fLinks = new ArrayList<>();
70e10acc 2566 private boolean fEntryFilter[] = new boolean[0];
507b1336 2567 private final ArrayList<ITimeGraphEntry> fFilteredOut = new ArrayList<>();
837a2f8c
PT
2568
2569 public ItemData() {
2570 }
2571
70e10acc
PT
2572 public Item findItem(ITimeGraphEntry entry) {
2573 return fItemMap.get(entry);
837a2f8c
PT
2574 }
2575
70e10acc
PT
2576 public int findItemIndex(ITimeGraphEntry entry) {
2577 Item item = fItemMap.get(entry);
2578 if (item == null) {
837a2f8c
PT
2579 return -1;
2580 }
70e10acc 2581 return item.fExpandedIndex;
837a2f8c
PT
2582 }
2583
2584 public void refreshData() {
70e10acc 2585 fItemMap.clear();
f1fae91f 2586 fFilteredOut.clear();
3e9a3685 2587 ITimeGraphEntry selection = getSelectedTrace();
70e10acc
PT
2588 for (int i = 0; i < fRootEntries.length; i++) {
2589 ITimeGraphEntry entry = fRootEntries[i];
2590 refreshData(fItemMap, null, 0, entry);
837a2f8c 2591 }
70e10acc 2592 fItems = fItemMap.values().toArray(new Item[0]);
837a2f8c 2593 updateExpandedItems();
3e9a3685 2594 if (selection != null) {
f1fae91f 2595 for (Item item : fExpandedItems) {
70e10acc 2596 if (item.fEntry == selection) {
f1fae91f 2597 item.fSelected = true;
3e9a3685
PT
2598 break;
2599 }
2600 }
2601 }
837a2f8c
PT
2602 }
2603
70e10acc 2604 private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) {
837a2f8c
PT
2605 Item item = new Item(entry, entry.getName(), level);
2606 if (parent != null) {
f1fae91f 2607 parent.fChildren.add(item);
837a2f8c 2608 }
c004295c
PT
2609 if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) {
2610 item.fItemHeight = fTimeGraphProvider.getItemHeight(entry);
2611 } else {
2612 item.fItemHeight = fGlobalItemHeight;
2613 }
70e10acc 2614 itemMap.put(entry, item);
837a2f8c 2615 if (entry.hasChildren()) {
f4617471 2616 item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel;
f1fae91f 2617 item.fHasChildren = true;
837a2f8c 2618 for (ITimeGraphEntry child : entry.getChildren()) {
70e10acc 2619 refreshData(itemMap, item, level + 1, child);
837a2f8c
PT
2620 }
2621 }
2622 }
2623
2624 public void updateExpandedItems() {
70e10acc
PT
2625 for (Item item : fItems) {
2626 item.fExpandedIndex = -1;
2627 }
507b1336 2628 List<Item> expandedItemList = new ArrayList<>();
70e10acc
PT
2629 for (int i = 0; i < fRootEntries.length; i++) {
2630 ITimeGraphEntry entry = fRootEntries[i];
837a2f8c
PT
2631 Item item = findItem(entry);
2632 refreshExpanded(expandedItemList, item);
2633 }
f1fae91f 2634 fExpandedItems = expandedItemList.toArray(new Item[0]);
19ed1845 2635 fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1));
837a2f8c
PT
2636 }
2637
2638 private void refreshExpanded(List<Item> expandedItemList, Item item) {
6ac5a950
AM
2639 // Check for filters
2640 boolean display = true;
f1fae91f 2641 for (ViewerFilter filter : fFilters) {
70e10acc 2642 if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) {
6ac5a950
AM
2643 display = false;
2644 break;
2645 }
2646 }
2647 if (display) {
70e10acc 2648 item.fExpandedIndex = expandedItemList.size();
6ac5a950 2649 expandedItemList.add(item);
f1fae91f
PT
2650 if (item.fHasChildren && item.fExpanded) {
2651 for (Item child : item.fChildren) {
6ac5a950
AM
2652 refreshExpanded(expandedItemList, child);
2653 }
837a2f8c
PT
2654 }
2655 }
2656 }
2657
70e10acc
PT
2658 public void refreshData(ITimeGraphEntry[] entries) {
2659 if (entries == null) {
2660 fEntryFilter = null;
2661 fRootEntries = null;
f1fae91f 2662 } else {
70e10acc
PT
2663 if (entries.length == 0) {
2664 fEntryFilter = null;
2665 } else if (fEntryFilter == null || entries.length != fEntryFilter.length) {
2666 fEntryFilter = new boolean[entries.length];
2667 java.util.Arrays.fill(fEntryFilter, true);
f1fae91f 2668 }
70e10acc 2669 fRootEntries = Arrays.copyOf(entries, entries.length);
837a2f8c
PT
2670 }
2671
837a2f8c
PT
2672 refreshData();
2673 }
2674
bec1f1ac
GB
2675 public void refreshArrows(List<ILinkEvent> events) {
2676 /* If links are null, reset the list */
2677 if (events != null) {
2678 fLinks = events;
2679 } else {
507b1336 2680 fLinks = new ArrayList<>();
bec1f1ac
GB
2681 }
2682 }
2683
70e10acc
PT
2684 public ITimeGraphEntry[] getEntries() {
2685 return fRootEntries;
837a2f8c
PT
2686 }
2687
70e10acc
PT
2688 public boolean[] getEntryFilter() {
2689 return fEntryFilter;
837a2f8c
PT
2690 }
2691
f1fae91f
PT
2692 public List<ITimeGraphEntry> getFilteredOut() {
2693 return fFilteredOut;
837a2f8c
PT
2694 }
2695 }
2696
2697 private class Item {
f1fae91f 2698 private boolean fExpanded;
70e10acc 2699 private int fExpandedIndex;
f1fae91f
PT
2700 private boolean fSelected;
2701 private boolean fHasChildren;
2702 private int fItemHeight;
70e10acc
PT
2703 private final int fLevel;
2704 private final List<Item> fChildren;
2705 private final String fName;
2706 private final ITimeGraphEntry fEntry;
837a2f8c 2707
70e10acc
PT
2708 public Item(ITimeGraphEntry entry, String name, int level) {
2709 this.fEntry = entry;
f1fae91f
PT
2710 this.fName = name;
2711 this.fLevel = level;
507b1336 2712 this.fChildren = new ArrayList<>();
837a2f8c
PT
2713 }
2714
2715 @Override
2716 public String toString() {
f1fae91f 2717 return fName;
837a2f8c
PT
2718 }
2719 }
2720
27df1564 2721 /**
77c4a6df 2722 * @since 1.2
27df1564
XR
2723 */
2724 @Override
2725 public void menuDetected(MenuDetectEvent e) {
f1fae91f 2726 if (null == fTimeProvider) {
27df1564
XR
2727 return;
2728 }
0b5a90a0
PT
2729 if (e.detail == SWT.MENU_MOUSE) {
2730 if (fPendingMenuDetectEvent == null) {
2731 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2732 * Store the event and trigger it later just before handling mouseUp.
2733 * This allows for the method to detect if mouse is used to drag zoom.
2734 */
2735 fPendingMenuDetectEvent = e;
2736 return;
2737 }
2738 fPendingMenuDetectEvent = null;
f1fae91f 2739 if (fDragState != DRAG_ZOOM || fDragX != fDragX0) {
0b5a90a0
PT
2740 return;
2741 }
2742 } else {
f1fae91f 2743 if (fDragState != DRAG_NONE) {
0b5a90a0
PT
2744 return;
2745 }
2746 }
27df1564
XR
2747 Point p = toControl(e.x, e.y);
2748 int idx = getItemIndexAtY(p.y);
f1fae91f
PT
2749 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
2750 Item item = fItemData.fExpandedItems[idx];
70e10acc 2751 ITimeGraphEntry entry = item.fEntry;
27df1564
XR
2752 if (entry.hasTimeEvents()) {
2753 ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2);
2754 if (event != null) {
2755 e.data = event;
2756 fireMenuEventOnTimeEvent(e);
2757 return;
2758 }
2759 }
2760 e.data = entry;
2761 fireMenuEventOnTimeGraphEntry(e);
2762 }
2763 }
2764
837a2f8c
PT
2765}
2766
27df1564 2767
This page took 0.285388 seconds and 5 git commands to generate.