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