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