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