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