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