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