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