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