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