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