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