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