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