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