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