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