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