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