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