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