swtbot: Add SWTBotTimeGraph and update SWTBotSash
[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 boolean isOverTimeSpace(int x, int y) {
1412 Point size = getSize();
1413 return x >= fTimeProvider.getNameSpace() && x < size.x && y >= 0 && y < size.y;
1414 }
1415 /**
1416 * Gets the {@link ITimeGraphEntry} at the given location.
1417 *
1418 * @param pt
1419 * a point in the widget
1420 * @return the {@link ITimeGraphEntry} at this point, or <code>null</code>
1421 * if none.
1422 * @since 2.0
1423 */
1424 public ITimeGraphEntry getEntry(Point pt) {
1425 int idx = getItemIndexAtY(pt.y);
1426 return idx >= 0 ? fItemData.fExpandedItems[idx].fEntry : null;
1427 }
1428
1429 /**
1430 * Return the arrow event closest to the given point that is no further than
1431 * a maximum distance.
1432 *
1433 * @param pt
1434 * a point in the widget
1435 * @return The closest arrow event, or null if there is none close enough.
1436 */
1437 protected ILinkEvent getArrow(Point pt) {
1438 if (fHideArrows) {
1439 return null;
1440 }
1441 ILinkEvent linkEvent = null;
1442 double minDistance = Double.MAX_VALUE;
1443 for (ILinkEvent event : fItemData.fLinks) {
1444 Rectangle rect = getArrowRectangle(new Rectangle(0, 0, 0, 0), event);
1445 if (rect != null) {
1446 int x1 = rect.x;
1447 int y1 = rect.y;
1448 int x2 = x1 + rect.width;
1449 int y2 = y1 + rect.height;
1450 double d = Utils.distance(pt.x, pt.y, x1, y1, x2, y2);
1451 if (minDistance > d) {
1452 minDistance = d;
1453 linkEvent = event;
1454 }
1455 }
1456 }
1457 if (minDistance <= ARROW_HOVER_MAX_DIST) {
1458 return linkEvent;
1459 }
1460 return null;
1461 }
1462
1463 @Override
1464 public int getXForTime(long time) {
1465 if (null == fTimeProvider) {
1466 return -1;
1467 }
1468 long time0 = fTimeProvider.getTime0();
1469 long time1 = fTimeProvider.getTime1();
1470 int width = getSize().x;
1471 int nameSpace = fTimeProvider.getNameSpace();
1472 double pixelsPerNanoSec = (width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
1473 int x = SaturatedArithmetic.add(getBounds().x + nameSpace, (int) ((time - time0) * pixelsPerNanoSec));
1474 return x;
1475 }
1476
1477 @Override
1478 public long getTimeAtX(int coord) {
1479 if (null == fTimeProvider) {
1480 return -1;
1481 }
1482 long hitTime = -1;
1483 Point size = getSize();
1484 long time0 = fTimeProvider.getTime0();
1485 long time1 = fTimeProvider.getTime1();
1486 int nameWidth = fTimeProvider.getNameSpace();
1487 final int x = coord - nameWidth;
1488 int timeWidth = size.x - nameWidth - RIGHT_MARGIN;
1489 if (x >= 0 && size.x >= nameWidth) {
1490 if (time1 - time0 > timeWidth) {
1491 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1492 hitTime = time0 + (long) Math.ceil((time1 - time0) * ((double) x / timeWidth));
1493 } else {
1494 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1495 hitTime = time0 + (long) Math.floor((time1 - time0) * ((double) x / timeWidth));
1496 }
1497 }
1498 return hitTime;
1499 }
1500
1501 void selectItem(int idx, boolean addSelection) {
1502 boolean changed = false;
1503 if (addSelection) {
1504 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
1505 Item item = fItemData.fExpandedItems[idx];
1506 changed = !item.fSelected;
1507 item.fSelected = true;
1508 }
1509 } else {
1510 for (int i = 0; i < fItemData.fExpandedItems.length; i++) {
1511 Item item = fItemData.fExpandedItems[i];
1512 if ((i == idx && !item.fSelected) || (idx == -1 && item.fSelected)) {
1513 changed = true;
1514 }
1515 item.fSelected = i == idx;
1516 }
1517 }
1518 changed |= ensureVisibleItem(idx, true);
1519 if (changed) {
1520 redraw();
1521 }
1522 }
1523
1524 /**
1525 * Callback for item selection
1526 *
1527 * @param trace
1528 * The entry matching the trace
1529 * @param addSelection
1530 * If the selection is added or removed
1531 */
1532 public void selectItem(ITimeGraphEntry trace, boolean addSelection) {
1533 int idx = fItemData.findItemIndex(trace);
1534 selectItem(idx, addSelection);
1535 }
1536
1537 /**
1538 * Retrieve the number of entries shown per page.
1539 *
1540 * @return The count
1541 */
1542 public int countPerPage() {
1543 int height = getSize().y;
1544 int count = 0;
1545 int ySum = 0;
1546 for (int idx = fTopIndex; idx < fItemData.fExpandedItems.length; idx++) {
1547 ySum += fItemData.fExpandedItems[idx].fItemHeight;
1548 if (ySum >= height) {
1549 return count;
1550 }
1551 count++;
1552 }
1553 for (int idx = fTopIndex - 1; idx >= 0; idx--) {
1554 ySum += fItemData.fExpandedItems[idx].fItemHeight;
1555 if (ySum >= height) {
1556 return count;
1557 }
1558 count++;
1559 }
1560 return count;
1561 }
1562
1563 /**
1564 * Get the index of the top element
1565 *
1566 * @return The index
1567 */
1568 public int getTopIndex() {
1569 return fTopIndex;
1570 }
1571
1572 /**
1573 * Get the number of expanded (visible) items
1574 *
1575 * @return The count of expanded (visible) items
1576 */
1577 public int getExpandedElementCount() {
1578 return fItemData.fExpandedItems.length;
1579 }
1580
1581 /**
1582 * Get an array of all expanded (visible) elements
1583 *
1584 * @return The expanded (visible) elements
1585 */
1586 public ITimeGraphEntry[] getExpandedElements() {
1587 ArrayList<ITimeGraphEntry> elements = new ArrayList<>();
1588 for (Item item : fItemData.fExpandedItems) {
1589 elements.add(item.fEntry);
1590 }
1591 return elements.toArray(new ITimeGraphEntry[0]);
1592 }
1593
1594 /**
1595 * Get the expanded (visible) element at the specified index.
1596 *
1597 * @param index
1598 * the element index
1599 * @return The expanded (visible) element or null if out of range
1600 * @since 2.0
1601 */
1602 public ITimeGraphEntry getExpandedElement(int index) {
1603 if (index < 0 || index >= fItemData.fExpandedItems.length) {
1604 return null;
1605 }
1606 return fItemData.fExpandedItems[index].fEntry;
1607 }
1608
1609 /**
1610 * Get the bounds of the specified entry
1611 *
1612 * @param entry the time graph entry
1613 * @return the bounds of the entry, or null if the entry is not visible
1614 * @since 2.2
1615 */
1616 public Rectangle getItemBounds(ITimeGraphEntry entry) {
1617 int idx = fItemData.findItemIndex(entry);
1618 if (idx >= 0) {
1619 return getItemRect(getBounds(), idx);
1620 }
1621 return null;
1622 }
1623
1624 Rectangle getNameRect(Rectangle bounds, int idx, int nameWidth) {
1625 Rectangle rect = getItemRect(bounds, idx);
1626 rect.width = nameWidth;
1627 return rect;
1628 }
1629
1630 Rectangle getStatesRect(Rectangle bounds, int idx, int nameWidth) {
1631 Rectangle rect = getItemRect(bounds, idx);
1632 rect.x += nameWidth;
1633 rect.width -= nameWidth;
1634 return rect;
1635 }
1636
1637 Rectangle getItemRect(Rectangle bounds, int idx) {
1638 int ySum = 0;
1639 if (idx >= fTopIndex) {
1640 for (int i = fTopIndex; i < idx; i++) {
1641 ySum += fItemData.fExpandedItems[i].fItemHeight;
1642 }
1643 } else {
1644 for (int i = fTopIndex - 1; i >= idx; i--) {
1645 ySum -= fItemData.fExpandedItems[i].fItemHeight;
1646 }
1647 }
1648 int y = bounds.y + ySum;
1649 int height = fItemData.fExpandedItems[idx].fItemHeight;
1650 return new Rectangle(bounds.x, y, bounds.width, height);
1651 }
1652
1653 @Override
1654 void paint(Rectangle bounds, PaintEvent e) {
1655 GC gc = e.gc;
1656
1657 if (bounds.width < 2 || bounds.height < 2 || null == fTimeProvider) {
1658 return;
1659 }
1660
1661 fIdealNameSpace = 0;
1662 int nameSpace = fTimeProvider.getNameSpace();
1663
1664 // draw the background layer
1665 drawBackground(bounds, nameSpace, gc);
1666
1667 // draw the grid lines
1668 drawGridLines(bounds, gc);
1669
1670 // draw the background markers
1671 drawMarkers(bounds, fTimeProvider, fMarkers, false, nameSpace, gc);
1672
1673 // draw the items
1674 drawItems(bounds, fTimeProvider, fItemData.fExpandedItems, fTopIndex, nameSpace, gc);
1675
1676 // draw the foreground markers
1677 drawMarkers(bounds, fTimeProvider, fMarkers, true, nameSpace, gc);
1678
1679 // draw the links (arrows)
1680 drawLinks(bounds, fTimeProvider, fItemData.fLinks, nameSpace, gc);
1681
1682 fTimeGraphProvider.postDrawControl(bounds, gc);
1683
1684 int alpha = gc.getAlpha();
1685 gc.setAlpha(100);
1686
1687 long time0 = fTimeProvider.getTime0();
1688 long time1 = fTimeProvider.getTime1();
1689 long selectionBegin = fTimeProvider.getSelectionBegin();
1690 long selectionEnd = fTimeProvider.getSelectionEnd();
1691 double pixelsPerNanoSec = (bounds.width - nameSpace <= RIGHT_MARGIN) ? 0 : (double) (bounds.width - nameSpace - RIGHT_MARGIN) / (time1 - time0);
1692 int x0 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionBegin - time0) * pixelsPerNanoSec));
1693 int x1 = SaturatedArithmetic.add(bounds.x + nameSpace, (int) ((selectionEnd - time0) * pixelsPerNanoSec));
1694
1695 // draw selection lines
1696 if (fDragState != DRAG_SELECTION) {
1697 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME));
1698 if (x0 >= nameSpace && x0 < bounds.x + bounds.width) {
1699 gc.drawLine(x0, bounds.y, x0, bounds.y + bounds.height);
1700 }
1701 if (x1 != x0) {
1702 if (x1 >= nameSpace && x1 < bounds.x + bounds.width) {
1703 gc.drawLine(x1, bounds.y, x1, bounds.y + bounds.height);
1704 }
1705 }
1706 }
1707
1708 // draw selection background
1709 if (selectionBegin != 0 && selectionEnd != 0 && fDragState != DRAG_SELECTION) {
1710 x0 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x0));
1711 x1 = Math.max(nameSpace, Math.min(bounds.x + bounds.width, x1));
1712 gc.setBackground(getColorScheme().getBkColor(false, false, true));
1713 if (x1 - x0 > 1) {
1714 gc.fillRectangle(new Rectangle(x0 + 1, bounds.y, x1 - x0 - 1, bounds.height));
1715 } else if (x0 - x1 > 1) {
1716 gc.fillRectangle(new Rectangle(x1 + 1, bounds.y, x0 - x1 - 1, bounds.height));
1717 }
1718 }
1719
1720 // draw drag selection background
1721 if (fDragState == DRAG_ZOOM || fDragState == DRAG_SELECTION) {
1722 gc.setBackground(getColorScheme().getBkColor(false, false, true));
1723 if (fDragX0 < fDragX) {
1724 gc.fillRectangle(new Rectangle(fDragX0, bounds.y, fDragX - fDragX0, bounds.height));
1725 } else if (fDragX0 > fDragX) {
1726 gc.fillRectangle(new Rectangle(fDragX, bounds.y, fDragX0 - fDragX, bounds.height));
1727 }
1728 }
1729
1730 // draw drag line
1731 if (DRAG_SPLIT_LINE == fDragState) {
1732 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.BLACK));
1733 gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1);
1734 } else if (DRAG_ZOOM == fDragState && Math.max(fDragX, fDragX0) > nameSpace) {
1735 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.TOOL_FOREGROUND));
1736 gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1);
1737 if (fDragX != fDragX0) {
1738 gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1);
1739 }
1740 } else if (DRAG_SELECTION == fDragState && Math.max(fDragX, fDragX0) > nameSpace) {
1741 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.SELECTED_TIME));
1742 gc.drawLine(fDragX0, bounds.y, fDragX0, bounds.y + bounds.height - 1);
1743 if (fDragX != fDragX0) {
1744 gc.drawLine(fDragX, bounds.y, fDragX, bounds.y + bounds.height - 1);
1745 }
1746 } else if (DRAG_NONE == fDragState && fMouseOverSplitLine && fTimeProvider.getNameSpace() > 0) {
1747 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.RED));
1748 gc.drawLine(bounds.x + nameSpace, bounds.y, bounds.x + nameSpace, bounds.y + bounds.height - 1);
1749 }
1750
1751 gc.setAlpha(alpha);
1752 }
1753
1754 /**
1755 * Draw the background layer. Fills the background of the control's name
1756 * space and states space, updates the background of items if necessary,
1757 * and draws the item's name text and middle line.
1758 *
1759 * @param bounds
1760 * The bounds of the control
1761 * @param nameSpace
1762 * The name space width
1763 * @param gc
1764 * Graphics context
1765 * @since 2.0
1766 */
1767 protected void drawBackground(Rectangle bounds, int nameSpace, GC gc) {
1768 // draw empty name space background
1769 gc.setBackground(getColorScheme().getBkColor(false, false, true));
1770 drawBackground(gc, bounds.x, bounds.y, nameSpace, bounds.height);
1771
1772 // draw empty states space background
1773 gc.setBackground(getColorScheme().getColor(TimeGraphColorScheme.BACKGROUND));
1774 drawBackground(gc, bounds.x + nameSpace, bounds.y, bounds.width - nameSpace, bounds.height);
1775
1776 for (int i = fTopIndex; i < fItemData.fExpandedItems.length; i++) {
1777 Rectangle itemRect = getItemRect(bounds, i);
1778 if (itemRect.y >= bounds.y + bounds.height) {
1779 break;
1780 }
1781 Item item = fItemData.fExpandedItems[i];
1782 // draw the background of selected item and items with no time events
1783 if (! item.fEntry.hasTimeEvents()) {
1784 gc.setBackground(getColorScheme().getBkColorGroup(item.fSelected, fIsInFocus));
1785 gc.fillRectangle(itemRect);
1786 } else if (item.fSelected) {
1787 gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, true));
1788 gc.fillRectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height);
1789 gc.setBackground(getColorScheme().getBkColor(true, fIsInFocus, false));
1790 gc.fillRectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height);
1791 }
1792 // draw the name and middle line
1793 if (! item.fEntry.hasTimeEvents()) {
1794 drawName(item, itemRect, gc);
1795 } else {
1796 Rectangle nameRect = new Rectangle(itemRect.x, itemRect.y, nameSpace, itemRect.height);
1797 drawName(item, nameRect, gc);
1798 Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height);
1799 drawMidLine(rect, gc);
1800 }
1801 }
1802 }
1803
1804 /**
1805 * Draw the grid lines
1806 *
1807 * @param bounds
1808 * The bounds of the control
1809 * @param gc
1810 * Graphics context
1811 * @since 2.0
1812 */
1813 public void drawGridLines(Rectangle bounds, GC gc) {
1814 if (!fGridLinesVisible) {
1815 return;
1816 }
1817 gc.setForeground(fGridLineColor);
1818 gc.setAlpha(fGridLineColor.getAlpha());
1819 for (int x : fTimeGraphScale.getTickList()) {
1820 gc.drawLine(x, bounds.y, x, bounds.y + bounds.height);
1821 }
1822 gc.setAlpha(255);
1823 }
1824
1825 /**
1826 * Draw the markers
1827 *
1828 * @param bounds
1829 * The rectangle of the area
1830 * @param timeProvider
1831 * The time provider
1832 * @param markers
1833 * The list of markers
1834 * @param foreground
1835 * true to draw the foreground markers, false otherwise
1836 * @param nameSpace
1837 * The width reserved for the names
1838 * @param gc
1839 * Reference to the SWT GC object
1840 * @since 2.0
1841 */
1842 protected void drawMarkers(Rectangle bounds, ITimeDataProvider timeProvider, List<IMarkerEvent> markers, boolean foreground, int nameSpace, GC gc) {
1843 if (!fMarkersVisible || markers == null || markers.isEmpty()) {
1844 return;
1845 }
1846 gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height));
1847 /* the list can grow concurrently but cannot shrink */
1848 for (int i = 0; i < markers.size(); i++) {
1849 IMarkerEvent marker = markers.get(i);
1850 if (marker.isForeground() == foreground) {
1851 drawMarker(marker, bounds, timeProvider, nameSpace, gc);
1852 }
1853 }
1854 gc.setClipping((Rectangle) null);
1855 }
1856
1857 /**
1858 * Draw a single marker
1859 *
1860 * @param marker
1861 * The marker event
1862 * @param bounds
1863 * The bounds of the control
1864 * @param timeProvider
1865 * The time provider
1866 * @param nameSpace
1867 * The width reserved for the name
1868 * @param gc
1869 * Reference to the SWT GC object
1870 * @since 2.0
1871 */
1872 protected void drawMarker(IMarkerEvent marker, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) {
1873 Rectangle rect = Utils.clone(bounds);
1874 if (marker.getEntry() != null) {
1875 int index = fItemData.findItemIndex(marker.getEntry());
1876 if (index == -1) {
1877 return;
1878 }
1879 rect = getStatesRect(bounds, index, nameSpace);
1880 if (rect.y < 0 || rect.y > bounds.height) {
1881 return;
1882 }
1883 }
1884 int x0 = getXForTime(marker.getTime());
1885 int x1 = getXForTime(marker.getTime() + marker.getDuration());
1886 if (x0 > bounds.width || x1 < nameSpace) {
1887 return;
1888 }
1889 rect.x = Math.max(nameSpace, Math.min(bounds.width, x0));
1890 rect.width = Math.max(1, Math.min(bounds.width, x1) - rect.x);
1891
1892 Color color = getColorScheme().getColor(marker.getColor());
1893 gc.setBackground(color);
1894 gc.setAlpha(color.getAlpha());
1895 gc.fillRectangle(rect);
1896 gc.setAlpha(255);
1897 String label = marker.getLabel();
1898 if (label != null && marker.getEntry() != null) {
1899 label = label.substring(0, Math.min(label.indexOf('\n') != -1 ? label.indexOf('\n') : label.length(), MAX_LABEL_LENGTH));
1900 gc.setForeground(color);
1901 Utils.drawText(gc, label, rect.x - gc.textExtent(label).x, rect.y, true);
1902 }
1903 }
1904
1905 /**
1906 * Draw many items at once
1907 *
1908 * @param bounds
1909 * The bounds of the control
1910 * @param timeProvider
1911 * The time provider
1912 * @param items
1913 * The array items to draw
1914 * @param topIndex
1915 * The index of the first element to draw
1916 * @param nameSpace
1917 * The name space width
1918 * @param gc
1919 * Graphics context
1920 */
1921 public void drawItems(Rectangle bounds, ITimeDataProvider timeProvider,
1922 Item[] items, int topIndex, int nameSpace, GC gc) {
1923 for (int i = topIndex; i < items.length; i++) {
1924 Item item = items[i];
1925 drawItem(item, bounds, timeProvider, i, nameSpace, gc);
1926 }
1927 }
1928
1929 /**
1930 * Draws the item
1931 *
1932 * @param item
1933 * The item to draw
1934 * @param bounds
1935 * The bounds of the control
1936 * @param timeProvider
1937 * The time provider
1938 * @param i
1939 * The expanded item index
1940 * @param nameSpace
1941 * The name space width
1942 * @param gc
1943 * Graphics context
1944 */
1945 protected void drawItem(Item item, Rectangle bounds, ITimeDataProvider timeProvider, int i, int nameSpace, GC gc) {
1946 Rectangle itemRect = getItemRect(bounds, i);
1947 if (itemRect.y >= bounds.y + bounds.height) {
1948 return;
1949 }
1950
1951 ITimeGraphEntry entry = item.fEntry;
1952 long time0 = timeProvider.getTime0();
1953 long time1 = timeProvider.getTime1();
1954 long selectedTime = fTimeProvider.getSelectionEnd();
1955
1956 Rectangle rect = new Rectangle(nameSpace, itemRect.y, itemRect.width - nameSpace, itemRect.height);
1957 if (rect.isEmpty() || (time1 <= time0)) {
1958 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
1959 return;
1960 }
1961
1962 boolean selected = item.fSelected;
1963 // K pixels per second
1964 double pixelsPerNanoSec = (rect.width <= RIGHT_MARGIN) ? 0 : (double) (rect.width - RIGHT_MARGIN) / (time1 - time0);
1965
1966 if (item.fEntry.hasTimeEvents()) {
1967 gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height));
1968 fillSpace(rect, gc, selected);
1969
1970 int margins = getMarginForHeight(rect.height);
1971 int height = rect.height - margins;
1972 int topMargin = (margins + 1) / 2;
1973 Rectangle stateRect = new Rectangle(rect.x, rect.y + topMargin, rect.width, height);
1974
1975 /* Set the font for this item */
1976 setFontForHeight(height, gc);
1977
1978 long maxDuration = (timeProvider.getTimeSpace() == 0) ? Long.MAX_VALUE : 1 * (time1 - time0) / timeProvider.getTimeSpace();
1979 Iterator<ITimeEvent> iterator = entry.getTimeEventsIterator(time0, time1, maxDuration);
1980
1981 int lastX = -1;
1982 while (iterator.hasNext()) {
1983 ITimeEvent event = iterator.next();
1984 int x = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() - time0) * pixelsPerNanoSec));
1985 int xEnd = SaturatedArithmetic.add(rect.x, (int) ((event.getTime() + event.getDuration() - time0) * pixelsPerNanoSec));
1986 if (x >= rect.x + rect.width || xEnd < rect.x) {
1987 // event is out of bounds
1988 continue;
1989 }
1990 xEnd = Math.min(rect.x + rect.width, xEnd);
1991 stateRect.x = Math.max(rect.x, x);
1992 stateRect.width = Math.max(0, xEnd - stateRect.x + 1);
1993 if (stateRect.x == lastX) {
1994 stateRect.width -= 1;
1995 if (stateRect.width > 0) {
1996 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1997 gc.drawPoint(stateRect.x, stateRect.y - 2);
1998 stateRect.x += 1;
1999 }
2000 }
2001 boolean timeSelected = selectedTime >= event.getTime() && selectedTime < event.getTime() + event.getDuration();
2002 if (drawState(getColorScheme(), event, stateRect, gc, selected, timeSelected)) {
2003 lastX = stateRect.x;
2004 }
2005 }
2006 gc.setClipping((Rectangle) null);
2007 }
2008 fTimeGraphProvider.postDrawEntry(entry, rect, gc);
2009 }
2010
2011 /**
2012 * Draw the links
2013 *
2014 * @param bounds
2015 * The bounds of the control
2016 * @param timeProvider
2017 * The time provider
2018 * @param links
2019 * The list of link events
2020 * @param nameSpace
2021 * The name space width
2022 * @param gc
2023 * Graphics context
2024 */
2025 public void drawLinks(Rectangle bounds, ITimeDataProvider timeProvider,
2026 List<ILinkEvent> links, int nameSpace, GC gc) {
2027 if (fHideArrows) {
2028 return;
2029 }
2030 gc.setClipping(new Rectangle(nameSpace, 0, bounds.width - nameSpace, bounds.height));
2031 /* the list can grow concurrently but cannot shrink */
2032 for (int i = 0; i < links.size(); i++) {
2033 drawLink(links.get(i), bounds, timeProvider, nameSpace, gc);
2034 }
2035 gc.setClipping((Rectangle) null);
2036 }
2037
2038 /**
2039 * Draws a link type event
2040 *
2041 * @param event
2042 * The link event to draw
2043 * @param bounds
2044 * The bounds of the control
2045 * @param timeProvider
2046 * The time provider
2047 * @param nameSpace
2048 * The name space width
2049 * @param gc
2050 * Graphics context
2051 */
2052 protected void drawLink(ILinkEvent event, Rectangle bounds, ITimeDataProvider timeProvider, int nameSpace, GC gc) {
2053 drawArrow(getColorScheme(), event, getArrowRectangle(bounds, event), gc);
2054 }
2055
2056 private Rectangle getArrowRectangle(Rectangle bounds, ILinkEvent event) {
2057 int srcIndex = fItemData.findItemIndex(event.getEntry());
2058 int destIndex = fItemData.findItemIndex(event.getDestinationEntry());
2059
2060 if ((srcIndex == -1) || (destIndex == -1)) {
2061 return null;
2062 }
2063
2064 Rectangle src = getStatesRect(bounds, srcIndex, fTimeProvider.getNameSpace());
2065 Rectangle dst = getStatesRect(bounds, destIndex, fTimeProvider.getNameSpace());
2066
2067 int x0 = getXForTime(event.getTime());
2068 int x1 = getXForTime(event.getTime() + event.getDuration());
2069
2070 // limit the x-coordinates to prevent integer overflow in calculations
2071 // and also GC.drawLine doesn't draw properly with large coordinates
2072 final int limit = Integer.MAX_VALUE / 1024;
2073 x0 = Math.max(-limit, Math.min(x0, limit));
2074 x1 = Math.max(-limit, Math.min(x1, limit));
2075
2076 int y0 = src.y + src.height / 2;
2077 int y1 = dst.y + dst.height / 2;
2078 return new Rectangle(x0, y0, x1 - x0, y1 - y0);
2079 }
2080
2081 /**
2082 * Draw an arrow
2083 *
2084 * @param colors
2085 * Color scheme
2086 * @param event
2087 * Time event for which we're drawing the arrow
2088 * @param rect
2089 * The arrow rectangle
2090 * @param gc
2091 * Graphics context
2092 * @return true if the arrow was drawn
2093 */
2094 protected boolean drawArrow(TimeGraphColorScheme colors, ITimeEvent event,
2095 Rectangle rect, GC gc) {
2096
2097 if (rect == null) {
2098 return false;
2099 }
2100 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
2101 if (colorIdx < 0) {
2102 return false;
2103 }
2104 boolean visible = ((rect.height == 0) && (rect.width == 0)) ? false : true;
2105
2106 if (visible) {
2107 Color stateColor = null;
2108 if (colorIdx < fEventColorMap.length) {
2109 stateColor = fEventColorMap[colorIdx];
2110 } else {
2111 stateColor = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
2112 }
2113
2114 gc.setForeground(stateColor);
2115 gc.setBackground(stateColor);
2116
2117 /* Draw the arrow */
2118 gc.drawLine(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
2119 drawArrowHead(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height, gc);
2120
2121 }
2122 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2123 return visible;
2124 }
2125
2126 /*
2127 * @author Francis Giraldeau
2128 *
2129 * Inspiration:
2130 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
2131 *
2132 * The algorithm was taken from this site, not the code itself
2133 */
2134 private static void drawArrowHead(int x0, int y0, int x1, int y1, GC gc)
2135 {
2136 int factor = 10;
2137 double cos = 0.9510;
2138 double sin = 0.3090;
2139 long lenx = x1 - x0;
2140 long leny = y1 - y0;
2141 double len = Math.sqrt(lenx * lenx + leny * leny);
2142
2143 double dx = factor * lenx / len;
2144 double dy = factor * leny / len;
2145 int end1X = (int) Math.round((x1 - (dx * cos + dy * -sin)));
2146 int end1Y = (int) Math.round((y1 - (dx * sin + dy * cos)));
2147 int end2X = (int) Math.round((x1 - (dx * cos + dy * sin)));
2148 int end2Y = (int) Math.round((y1 - (dx * -sin + dy * cos)));
2149 int[] arrow = new int[] { x1, y1, end1X, end1Y, end2X, end2Y, x1, y1 };
2150 gc.fillPolygon(arrow);
2151 }
2152
2153 /**
2154 * Draw the name of an item.
2155 *
2156 * @param item
2157 * Item object
2158 * @param bounds
2159 * The bounds of the item's name space
2160 * @param gc
2161 * Graphics context
2162 */
2163 protected void drawName(Item item, Rectangle bounds, GC gc) {
2164 boolean hasTimeEvents = item.fEntry.hasTimeEvents();
2165
2166 // No name to be drawn
2167 if (fTimeProvider.getNameSpace() == 0) {
2168 return;
2169 }
2170
2171 int height = bounds.height - getMarginForHeight(bounds.height);
2172 setFontForHeight(height, gc);
2173
2174 int leftMargin = MARGIN + item.fLevel * EXPAND_SIZE;
2175 if (item.fHasChildren) {
2176 gc.setForeground(getColorScheme().getFgColorGroup(false, false));
2177 gc.setBackground(getColorScheme().getBkColor(false, false, false));
2178 Rectangle rect = Utils.clone(bounds);
2179 rect.x += leftMargin;
2180 rect.y += (bounds.height - EXPAND_SIZE) / 2;
2181 rect.width = EXPAND_SIZE;
2182 rect.height = EXPAND_SIZE;
2183 gc.fillRectangle(rect);
2184 gc.drawRectangle(rect.x, rect.y, rect.width - 1, rect.height - 1);
2185 int midy = rect.y + rect.height / 2;
2186 gc.drawLine(rect.x + 2, midy, rect.x + rect.width - 3, midy);
2187 if (!item.fExpanded) {
2188 int midx = rect.x + rect.width / 2;
2189 gc.drawLine(midx, rect.y + 2, midx, rect.y + rect.height - 3);
2190 }
2191 }
2192 leftMargin += EXPAND_SIZE + MARGIN;
2193
2194 Image img = fTimeGraphProvider.getItemImage(item.fEntry);
2195 if (img != null) {
2196 // draw icon
2197 int imgHeight = img.getImageData().height;
2198 int imgWidth = img.getImageData().width;
2199 int x = leftMargin;
2200 int y = bounds.y + (bounds.height - imgHeight) / 2;
2201 gc.drawImage(img, x, y);
2202 leftMargin += imgWidth + MARGIN;
2203 }
2204 String name = item.fName;
2205 Point size = gc.stringExtent(name);
2206 if (fIdealNameSpace < leftMargin + size.x + MARGIN) {
2207 fIdealNameSpace = leftMargin + size.x + MARGIN;
2208 }
2209 if (hasTimeEvents) {
2210 // cut long string with "..."
2211 int width = bounds.width - leftMargin;
2212 int cuts = 0;
2213 while (size.x > width && name.length() > 1) {
2214 cuts++;
2215 name = name.substring(0, name.length() - 1);
2216 size = gc.stringExtent(name + "..."); //$NON-NLS-1$
2217 }
2218 if (cuts > 0) {
2219 name += "..."; //$NON-NLS-1$
2220 }
2221 }
2222 Rectangle rect = Utils.clone(bounds);
2223 rect.x += leftMargin;
2224 rect.width -= leftMargin;
2225 // draw text
2226 if (rect.width > 0) {
2227 rect.y += (bounds.height - gc.stringExtent(name).y) / 2;
2228 gc.setForeground(getColorScheme().getFgColor(item.fSelected, fIsInFocus));
2229 int textWidth = Utils.drawText(gc, name, rect, true);
2230 leftMargin += textWidth + MARGIN;
2231
2232 if (hasTimeEvents) {
2233 // draw middle line
2234 rect.x = bounds.x + leftMargin;
2235 rect.y = bounds.y;
2236 rect.width = bounds.width - rect.x;
2237 drawMidLine(rect, gc);
2238 }
2239 }
2240 }
2241
2242 /**
2243 * Draw the state (color fill)
2244 *
2245 * @param colors
2246 * Color scheme
2247 * @param event
2248 * Time event for which we're drawing the state
2249 * @param rect
2250 * The state rectangle
2251 * @param gc
2252 * Graphics context
2253 * @param selected
2254 * Is this time event currently selected (so it appears
2255 * highlighted)
2256 * @param timeSelected
2257 * Is the timestamp currently selected
2258 * @return true if the state was drawn
2259 */
2260 protected boolean drawState(TimeGraphColorScheme colors, ITimeEvent event,
2261 Rectangle rect, GC gc, boolean selected, boolean timeSelected) {
2262
2263 int colorIdx = fTimeGraphProvider.getStateTableIndex(event);
2264 if (colorIdx < 0 && colorIdx != ITimeGraphPresentationProvider.TRANSPARENT) {
2265 return false;
2266 }
2267 boolean visible = rect.width == 0 ? false : true;
2268 rect.width = Math.max(1, rect.width);
2269 Color black = Display.getDefault().getSystemColor(SWT.COLOR_BLACK);
2270 gc.setForeground(black);
2271
2272 if (colorIdx == ITimeGraphPresentationProvider.TRANSPARENT) {
2273 if (visible) {
2274 // Only draw the top and bottom borders
2275 gc.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y);
2276 gc.drawLine(rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1);
2277 if (rect.width == 1) {
2278 gc.drawPoint(rect.x, rect.y - 2);
2279 }
2280 }
2281 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2282 return false;
2283 }
2284 Color stateColor = null;
2285 if (colorIdx < fEventColorMap.length) {
2286 stateColor = fEventColorMap[colorIdx];
2287 } else {
2288 stateColor = black;
2289 }
2290
2291 boolean reallySelected = timeSelected && selected;
2292 // fill all rect area
2293 gc.setBackground(stateColor);
2294 if (visible) {
2295 gc.fillRectangle(rect);
2296 } else if (fBlendSubPixelEvents) {
2297 gc.setAlpha(128);
2298 gc.fillRectangle(rect);
2299 gc.setAlpha(255);
2300 }
2301
2302 if (reallySelected) {
2303 gc.drawLine(rect.x, rect.y - 1, rect.x + rect.width - 1, rect.y - 1);
2304 gc.drawLine(rect.x, rect.y + rect.height, rect.x + rect.width - 1, rect.y + rect.height);
2305 }
2306 if (!visible) {
2307 gc.drawPoint(rect.x, rect.y - 2);
2308 }
2309 fTimeGraphProvider.postDrawEvent(event, rect, gc);
2310 return visible;
2311 }
2312
2313 /**
2314 * Fill an item's states rectangle
2315 *
2316 * @param rect
2317 * The states rectangle
2318 * @param gc
2319 * Graphics context
2320 * @param selected
2321 * true if the item is selected
2322 */
2323 protected void fillSpace(Rectangle rect, GC gc, boolean selected) {
2324 /* Nothing to draw */
2325 }
2326
2327 /**
2328 * Draw a line at the middle height of a rectangle
2329 *
2330 * @param rect
2331 * The rectangle
2332 * @param gc
2333 * Graphics context
2334 */
2335 private void drawMidLine(Rectangle rect, GC gc) {
2336 gc.setForeground(getColorScheme().getColor(TimeGraphColorScheme.MID_LINE));
2337 int midy = rect.y + rect.height / 2;
2338 gc.drawLine(rect.x, midy, rect.x + rect.width, midy);
2339 }
2340
2341 private static int getMarginForHeight(int height) {
2342 /*
2343 * State rectangle is smaller than the item bounds when height is > 4.
2344 * Don't use any margin if the height is below or equal that threshold.
2345 * Use a maximum of 6 pixels for both margins, otherwise try to use 13
2346 * pixels for the state height, but with a minimum margin of 1.
2347 */
2348 final int MARGIN_THRESHOLD = 4;
2349 final int PREFERRED_HEIGHT = 13;
2350 final int MIN_MARGIN = 1;
2351 final int MAX_MARGIN = 6;
2352 return height <= MARGIN_THRESHOLD ? 0 :
2353 Math.max(Math.min(height - PREFERRED_HEIGHT, MAX_MARGIN), MIN_MARGIN);
2354 }
2355
2356 private void setFontForHeight(int pixels, GC gc) {
2357 /* convert font height from pixels to points */
2358 int height = Math.max(pixels * PPI / DPI, 1);
2359 Font font = fFonts.get(height);
2360 if (font == null) {
2361 FontData fontData = gc.getFont().getFontData()[0];
2362 fontData.setHeight(height);
2363 font = new Font(gc.getDevice(), fontData);
2364 fFonts.put(height, font);
2365 }
2366 gc.setFont(font);
2367 }
2368
2369 @Override
2370 public void keyTraversed(TraverseEvent e) {
2371 if ((e.detail == SWT.TRAVERSE_TAB_NEXT) || (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
2372 e.doit = true;
2373 }
2374 }
2375
2376 @Override
2377 public void keyPressed(KeyEvent e) {
2378 int idx = -1;
2379 if (fItemData.fExpandedItems.length == 0) {
2380 return;
2381 }
2382 if (SWT.HOME == e.keyCode) {
2383 idx = 0;
2384 } else if (SWT.END == e.keyCode) {
2385 idx = fItemData.fExpandedItems.length - 1;
2386 } else if (SWT.ARROW_DOWN == e.keyCode) {
2387 idx = getSelectedIndex();
2388 if (idx < 0) {
2389 idx = 0;
2390 } else if (idx < fItemData.fExpandedItems.length - 1) {
2391 idx++;
2392 }
2393 } else if (SWT.ARROW_UP == e.keyCode) {
2394 idx = getSelectedIndex();
2395 if (idx < 0) {
2396 idx = 0;
2397 } else if (idx > 0) {
2398 idx--;
2399 }
2400 } else if (SWT.ARROW_LEFT == e.keyCode && fDragState == DRAG_NONE) {
2401 boolean extend = (e.stateMask & SWT.SHIFT) != 0;
2402 selectPrevEvent(extend);
2403 } else if (SWT.ARROW_RIGHT == e.keyCode && fDragState == DRAG_NONE) {
2404 boolean extend = (e.stateMask & SWT.SHIFT) != 0;
2405 selectNextEvent(extend);
2406 } else if (SWT.PAGE_DOWN == e.keyCode) {
2407 int page = countPerPage();
2408 idx = getSelectedIndex();
2409 if (idx < 0) {
2410 idx = 0;
2411 }
2412 idx += page;
2413 if (idx >= fItemData.fExpandedItems.length) {
2414 idx = fItemData.fExpandedItems.length - 1;
2415 }
2416 } else if (SWT.PAGE_UP == e.keyCode) {
2417 int page = countPerPage();
2418 idx = getSelectedIndex();
2419 if (idx < 0) {
2420 idx = 0;
2421 }
2422 idx -= page;
2423 if (idx < 0) {
2424 idx = 0;
2425 }
2426 } else if (SWT.CR == e.keyCode) {
2427 idx = getSelectedIndex();
2428 if (idx >= 0) {
2429 if (fItemData.fExpandedItems[idx].fHasChildren) {
2430 toggle(idx);
2431 } else {
2432 fireDefaultSelection();
2433 }
2434 }
2435 idx = -1;
2436 } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) != 0)) {
2437 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2438 verticalZoom(true);
2439 if (fVerticalZoomAlignEntry != null) {
2440 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2441 }
2442 } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) != 0)) {
2443 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2444 verticalZoom(false);
2445 if (fVerticalZoomAlignEntry != null) {
2446 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2447 }
2448 } else if (e.character == '0' && ((e.stateMask & SWT.CTRL) != 0)) {
2449 fVerticalZoomAlignEntry = getVerticalZoomAlignSelection();
2450 resetVerticalZoom();
2451 if (fVerticalZoomAlignEntry != null) {
2452 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2453 }
2454 } else if ((e.character == '+' || e.character == '=') && ((e.stateMask & SWT.CTRL) == 0)) {
2455 if (fHasNamespaceFocus) {
2456 ITimeGraphEntry entry = getSelectedTrace();
2457 setExpandedState(entry, 0, true);
2458 } else {
2459 zoomIn();
2460 }
2461 } else if (e.character == '-' && ((e.stateMask & SWT.CTRL) == 0)) {
2462 if (fHasNamespaceFocus) {
2463 ITimeGraphEntry entry = getSelectedTrace();
2464 if ((entry != null) && entry.hasChildren()) {
2465 setExpandedState(entry, -1, false);
2466 }
2467 } else {
2468 zoomOut();
2469 }
2470 } else if ((e.character == '*') && ((e.stateMask & SWT.CTRL) == 0)) {
2471 if (fHasNamespaceFocus) {
2472 ITimeGraphEntry entry = getSelectedTrace();
2473 if ((entry != null) && entry.hasChildren()) {
2474 setExpandedStateLevel(entry);
2475 }
2476 }
2477 }
2478 if (idx >= 0) {
2479 selectItem(idx, false);
2480 fireSelectionChanged();
2481 }
2482 int x = toControl(e.display.getCursorLocation()).x;
2483 updateCursor(x, e.stateMask | e.keyCode);
2484 }
2485
2486 @Override
2487 public void keyReleased(KeyEvent e) {
2488 int x = toControl(e.display.getCursorLocation()).x;
2489 updateCursor(x, e.stateMask & ~e.keyCode);
2490 }
2491
2492 @Override
2493 public void focusGained(FocusEvent e) {
2494 fIsInFocus = true;
2495 redraw();
2496 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2497 }
2498
2499 @Override
2500 public void focusLost(FocusEvent e) {
2501 fIsInFocus = false;
2502 if (DRAG_NONE != fDragState) {
2503 setCapture(false);
2504 fDragState = DRAG_NONE;
2505 }
2506 redraw();
2507 updateStatusLine(NO_STATUS);
2508 }
2509
2510 /**
2511 * @return If the current view is focused
2512 */
2513 public boolean isInFocus() {
2514 return fIsInFocus;
2515 }
2516
2517 /**
2518 * Provide the possibility to control the wait cursor externally e.g. data
2519 * requests in progress
2520 *
2521 * @param waitInd Should we wait indefinitely?
2522 */
2523 public void waitCursor(boolean waitInd) {
2524 // Update cursor as indicated
2525 if (waitInd) {
2526 setCursor(fWaitCursor);
2527 } else {
2528 setCursor(null);
2529 }
2530 }
2531
2532 private void updateCursor(int x, int stateMask) {
2533 // if Wait cursor not active, check for the need to change the cursor
2534 if (getCursor() == fWaitCursor) {
2535 return;
2536 }
2537 Cursor cursor = null;
2538 if (fDragState == DRAG_SPLIT_LINE) {
2539 } else if (fDragState == DRAG_SELECTION) {
2540 cursor = fResizeCursor;
2541 } else if (fDragState == DRAG_TRACE_ITEM) {
2542 cursor = fDragCursor;
2543 } else if (fDragState == DRAG_ZOOM) {
2544 cursor = fZoomCursor;
2545 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
2546 cursor = fDragCursor;
2547 } else if ((stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2548 cursor = fResizeCursor;
2549 } else if (!isOverSplitLine(x)) {
2550 long selectionBegin = fTimeProvider.getSelectionBegin();
2551 long selectionEnd = fTimeProvider.getSelectionEnd();
2552 int xBegin = getXForTime(selectionBegin);
2553 int xEnd = getXForTime(selectionEnd);
2554 if (Math.abs(x - xBegin) < SNAP_WIDTH || Math.abs(x - xEnd) < SNAP_WIDTH) {
2555 cursor = fResizeCursor;
2556 }
2557 }
2558 if (getCursor() != cursor) {
2559 setCursor(cursor);
2560 }
2561 }
2562
2563 /**
2564 * Update the status line following a change of selection.
2565 *
2566 * @since 2.0
2567 */
2568 public void updateStatusLine() {
2569 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2570 }
2571
2572 private void updateStatusLine(int x) {
2573 // use the time provider of the time graph scale for the status line
2574 ITimeDataProvider tdp = fTimeGraphScale.getTimeProvider();
2575 if (fStatusLineManager == null || null == tdp ||
2576 tdp.getTime0() == tdp.getTime1()) {
2577 return;
2578 }
2579 TimeFormat tf = tdp.getTimeFormat();
2580 Resolution res = Resolution.NANOSEC;
2581 StringBuilder message = new StringBuilder();
2582 if ((x >= 0 || x == STATUS_WITHOUT_CURSOR_TIME) && fDragState == DRAG_NONE) {
2583 if (x != STATUS_WITHOUT_CURSOR_TIME) {
2584 long time = getTimeAtX(x);
2585 if (time >= 0) {
2586 if (tdp instanceof ITimeDataProviderConverter) {
2587 time = ((ITimeDataProviderConverter) tdp).convertTime(time);
2588 }
2589 message.append(NLS.bind("T: {0}{1} ", //$NON-NLS-1$
2590 new Object[] {
2591 tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$
2592 Utils.formatTime(time, tf, res)
2593 }));
2594 }
2595 }
2596 long selectionBegin = tdp.getSelectionBegin();
2597 long selectionEnd = tdp.getSelectionEnd();
2598 message.append(NLS.bind("T1: {0}{1}", //$NON-NLS-1$
2599 new Object[] {
2600 tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionBegin) + ' ' : "", //$NON-NLS-1$
2601 Utils.formatTime(selectionBegin, tf, res)
2602 }));
2603 if (selectionBegin != selectionEnd) {
2604 message.append(NLS.bind(" T2: {0}{1} \u0394: {2}", //$NON-NLS-1$
2605 new Object[] {
2606 tf == TimeFormat.CALENDAR ? Utils.formatDate(selectionEnd) + ' ' : "", //$NON-NLS-1$
2607 Utils.formatTime(selectionEnd, tf, res),
2608 Utils.formatDelta(selectionEnd - selectionBegin, tf, res)
2609 }));
2610 }
2611 } else if (fDragState == DRAG_SELECTION || fDragState == DRAG_ZOOM) {
2612 long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0;
2613 long time = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX);
2614 if (tdp instanceof ITimeDataProviderConverter) {
2615 time0 = ((ITimeDataProviderConverter) tdp).convertTime(time0);
2616 time = ((ITimeDataProviderConverter) tdp).convertTime(time);
2617 }
2618 message.append(NLS.bind("T1: {0}{1} T2: {2}{3} \u0394: {4}", //$NON-NLS-1$
2619 new Object[] {
2620 tf == TimeFormat.CALENDAR ? Utils.formatDate(time0) + ' ' : "", //$NON-NLS-1$
2621 Utils.formatTime(time0, tf, res),
2622 tf == TimeFormat.CALENDAR ? Utils.formatDate(time) + ' ' : "", //$NON-NLS-1$
2623 Utils.formatTime(time, tf, res),
2624 Utils.formatDelta(time - time0, tf, res)
2625 }));
2626 }
2627 fStatusLineManager.setMessage(message.toString());
2628 }
2629
2630 @Override
2631 public void mouseMove(MouseEvent e) {
2632 if (null == fTimeProvider) {
2633 return;
2634 }
2635 Point size = getSize();
2636 if (DRAG_TRACE_ITEM == fDragState) {
2637 int nameWidth = fTimeProvider.getNameSpace();
2638 if (e.x > nameWidth && size.x > nameWidth && fDragX != e.x) {
2639 fDragX = e.x;
2640 double pixelsPerNanoSec = (size.x - nameWidth <= RIGHT_MARGIN) ? 0 : (double) (size.x - nameWidth - RIGHT_MARGIN) / (fTime1bak - fTime0bak);
2641 long timeDelta = (long) ((pixelsPerNanoSec == 0) ? 0 : ((fDragX - fDragX0) / pixelsPerNanoSec));
2642 long time1 = fTime1bak - timeDelta;
2643 long maxTime = fTimeProvider.getMaxTime();
2644 if (time1 > maxTime) {
2645 time1 = maxTime;
2646 }
2647 long time0 = time1 - (fTime1bak - fTime0bak);
2648 if (time0 < fTimeProvider.getMinTime()) {
2649 time0 = fTimeProvider.getMinTime();
2650 time1 = time0 + (fTime1bak - fTime0bak);
2651 }
2652 fTimeProvider.setStartFinishTimeNotify(time0, time1);
2653 }
2654 } else if (DRAG_SPLIT_LINE == fDragState) {
2655 fDragX = e.x;
2656 fTimeProvider.setNameSpace(e.x);
2657 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2658 } else if (DRAG_SELECTION == fDragState) {
2659 if (fDragBeginMarker) {
2660 fDragX0 = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2661 } else {
2662 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2663 }
2664 redraw();
2665 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2666 fireDragSelectionChanged(getTimeAtX(fDragX0), getTimeAtX(fDragX));
2667 } else if (DRAG_ZOOM == fDragState) {
2668 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), size.x - RIGHT_MARGIN);
2669 redraw();
2670 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2671 } else if (DRAG_NONE == fDragState) {
2672 boolean mouseOverSplitLine = isOverSplitLine(e.x);
2673 if (fMouseOverSplitLine != mouseOverSplitLine) {
2674 redraw();
2675 }
2676 fMouseOverSplitLine = mouseOverSplitLine;
2677 }
2678
2679 if (e.x >= fTimeProvider.getNameSpace()) {
2680 fHasNamespaceFocus = false;
2681 } else {
2682 fHasNamespaceFocus = true;
2683 }
2684 updateCursor(e.x, e.stateMask);
2685 updateStatusLine(e.x);
2686 }
2687
2688 @Override
2689 public void mouseDoubleClick(MouseEvent e) {
2690 if (null == fTimeProvider) {
2691 return;
2692 }
2693 if (1 == e.button && (e.stateMask & SWT.BUTTON_MASK) == 0) {
2694 if (isOverSplitLine(e.x) && fTimeProvider.getNameSpace() != 0) {
2695 fTimeProvider.setNameSpace(fIdealNameSpace);
2696 boolean mouseOverSplitLine = isOverSplitLine(e.x);
2697 if (fMouseOverSplitLine != mouseOverSplitLine) {
2698 redraw();
2699 }
2700 fMouseOverSplitLine = mouseOverSplitLine;
2701 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(this, getTimeViewAlignmentInfo()));
2702 return;
2703 }
2704 int idx = getItemIndexAtY(e.y);
2705 if (idx >= 0) {
2706 selectItem(idx, false);
2707 fireDefaultSelection();
2708 }
2709 }
2710 }
2711
2712 @Override
2713 public void mouseDown(MouseEvent e) {
2714 if (fDragState != DRAG_NONE || null == fTimeProvider ||
2715 fTimeProvider.getTime0() == fTimeProvider.getTime1() ||
2716 getSize().x - fTimeProvider.getNameSpace() <= 0) {
2717 return;
2718 }
2719 int idx;
2720 if (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == 0) {
2721 int nameSpace = fTimeProvider.getNameSpace();
2722 if (nameSpace != 0 && isOverSplitLine(e.x)) {
2723 fDragState = DRAG_SPLIT_LINE;
2724 fDragButton = e.button;
2725 fDragX = e.x;
2726 fDragX0 = fDragX;
2727 fTime0bak = fTimeProvider.getTime0();
2728 fTime1bak = fTimeProvider.getTime1();
2729 redraw();
2730 updateCursor(e.x, e.stateMask);
2731 return;
2732 }
2733 }
2734 if (1 == e.button && ((e.stateMask & SWT.MODIFIER_MASK) == 0 || (e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT)) {
2735 int nameSpace = fTimeProvider.getNameSpace();
2736 idx = getItemIndexAtY(e.y);
2737 if (idx >= 0) {
2738 Item item = fItemData.fExpandedItems[idx];
2739 if (item.fHasChildren && e.x < nameSpace && e.x < MARGIN + (item.fLevel + 1) * EXPAND_SIZE) {
2740 toggle(idx);
2741 return;
2742 }
2743 selectItem(idx, false);
2744 fireSelectionChanged();
2745 } else {
2746 selectItem(idx, false); // clear selection
2747 fireSelectionChanged();
2748 }
2749 long hitTime = getTimeAtX(e.x);
2750 if (hitTime >= 0) {
2751 setCapture(true);
2752
2753 fDragState = DRAG_SELECTION;
2754 fDragBeginMarker = false;
2755 fDragButton = e.button;
2756 fDragX = e.x;
2757 fDragX0 = fDragX;
2758 fDragTime0 = getTimeAtX(fDragX0);
2759 long selectionBegin = fTimeProvider.getSelectionBegin();
2760 long selectionEnd = fTimeProvider.getSelectionEnd();
2761 int xBegin = getXForTime(selectionBegin);
2762 int xEnd = getXForTime(selectionEnd);
2763 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2764 long time = getTimeAtX(e.x);
2765 if (Math.abs(time - selectionBegin) < Math.abs(time - selectionEnd)) {
2766 fDragBeginMarker = true;
2767 fDragX = xEnd;
2768 fDragX0 = e.x;
2769 fDragTime0 = selectionEnd;
2770 } else {
2771 fDragX0 = xBegin;
2772 fDragTime0 = selectionBegin;
2773 }
2774 } else {
2775 long time = getTimeAtX(e.x);
2776 if (Math.abs(e.x - xBegin) < SNAP_WIDTH && Math.abs(time - selectionBegin) <= Math.abs(time - selectionEnd)) {
2777 fDragBeginMarker = true;
2778 fDragX = xEnd;
2779 fDragX0 = e.x;
2780 fDragTime0 = selectionEnd;
2781 } else if (Math.abs(e.x - xEnd) < SNAP_WIDTH && Math.abs(time - selectionEnd) <= Math.abs(time - selectionBegin)) {
2782 fDragX0 = xBegin;
2783 fDragTime0 = selectionBegin;
2784 }
2785 }
2786 fTime0bak = fTimeProvider.getTime0();
2787 fTime1bak = fTimeProvider.getTime1();
2788 redraw();
2789 updateCursor(e.x, e.stateMask);
2790 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2791 }
2792 } else if (2 == e.button || (1 == e.button && (e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL)) {
2793 long hitTime = getTimeAtX(e.x);
2794 if (hitTime > 0) {
2795 setCapture(true);
2796 fDragState = DRAG_TRACE_ITEM;
2797 fDragButton = e.button;
2798 fDragX = e.x;
2799 fDragX0 = fDragX;
2800 fTime0bak = fTimeProvider.getTime0();
2801 fTime1bak = fTimeProvider.getTime1();
2802 updateCursor(e.x, e.stateMask);
2803 }
2804 } else if (3 == e.button) {
2805 if (e.x >= fTimeProvider.getNameSpace()) {
2806 setCapture(true);
2807 fDragX = Math.min(Math.max(e.x, fTimeProvider.getNameSpace()), getSize().x - RIGHT_MARGIN);
2808 fDragX0 = fDragX;
2809 fDragTime0 = getTimeAtX(fDragX0);
2810 fDragState = DRAG_ZOOM;
2811 fDragButton = e.button;
2812 redraw();
2813 updateCursor(e.x, e.stateMask);
2814 fTimeGraphScale.setDragRange(fDragX0, fDragX);
2815 } else {
2816 idx = getItemIndexAtY(e.y);
2817 selectItem(idx, false);
2818 fireSelectionChanged();
2819 }
2820 }
2821 }
2822
2823 @Override
2824 public void mouseUp(MouseEvent e) {
2825 if (fPendingMenuDetectEvent != null && e.button == 3) {
2826 if ((fDragState == DRAG_ZOOM) && isInDragZoomMargin()) {
2827 // Select entry and time event for single click
2828 long time = getTimeAtX(fDragX0);
2829 fTimeProvider.setSelectionRangeNotify(time, time, false);
2830 int idx = getItemIndexAtY(e.y);
2831 selectItem(idx, false);
2832 fireSelectionChanged();
2833 }
2834 menuDetected(fPendingMenuDetectEvent);
2835 }
2836 if (DRAG_NONE != fDragState) {
2837 setCapture(false);
2838 if (e.button == fDragButton && DRAG_TRACE_ITEM == fDragState) {
2839 fDragState = DRAG_NONE;
2840 if (fDragX != fDragX0) {
2841 fTimeProvider.notifyStartFinishTime();
2842 }
2843 } else if (e.button == fDragButton && DRAG_SPLIT_LINE == fDragState) {
2844 fDragState = DRAG_NONE;
2845 redraw();
2846 } else if (e.button == fDragButton && DRAG_SELECTION == fDragState) {
2847 fDragState = DRAG_NONE;
2848 if (fDragX == fDragX0) { // click without selecting anything
2849 long time = getTimeAtX(e.x);
2850 fTimeProvider.setSelectedTimeNotify(time, false);
2851 } else {
2852 long time0 = fDragBeginMarker ? getTimeAtX(fDragX0) : fDragTime0;
2853 long time1 = fDragBeginMarker ? fDragTime0 : getTimeAtX(fDragX);
2854 fTimeProvider.setSelectionRangeNotify(time0, time1, false);
2855 }
2856 redraw();
2857 fTimeGraphScale.setDragRange(-1, -1);
2858 } else if (e.button == fDragButton && DRAG_ZOOM == fDragState) {
2859 fDragState = DRAG_NONE;
2860 int nameWidth = fTimeProvider.getNameSpace();
2861 if ((Math.max(fDragX, fDragX0) > nameWidth) && !isInDragZoomMargin()) {
2862 long time0 = getTimeAtX(fDragX0);
2863 long time1 = getTimeAtX(fDragX);
2864 if (time0 < time1) {
2865 fTimeProvider.setStartFinishTimeNotify(time0, time1);
2866 } else {
2867 fTimeProvider.setStartFinishTimeNotify(time1, time0);
2868 }
2869 } else {
2870 redraw();
2871 }
2872 fTimeGraphScale.setDragRange(-1, -1);
2873 }
2874 }
2875 updateCursor(e.x, e.stateMask);
2876 updateStatusLine(e.x);
2877 }
2878
2879 @Override
2880 public void mouseEnter(MouseEvent e) {
2881 }
2882
2883 @Override
2884 public void mouseExit(MouseEvent e) {
2885 if (fMouseOverSplitLine) {
2886 fMouseOverSplitLine = false;
2887 redraw();
2888 }
2889 updateStatusLine(STATUS_WITHOUT_CURSOR_TIME);
2890 }
2891
2892 @Override
2893 public void mouseHover(MouseEvent e) {
2894 }
2895
2896 @Override
2897 public void mouseScrolled(MouseEvent e) {
2898 if (fDragState != DRAG_NONE || e.count == 0) {
2899 return;
2900 }
2901
2902 /*
2903 * On some platforms the mouse scroll event is sent to the
2904 * control that has focus even if it is not under the cursor.
2905 * Handle the event only if over the time graph control.
2906 */
2907 Point size = getSize();
2908 Rectangle bounds = new Rectangle(0, 0, size.x, size.y);
2909 if (!bounds.contains(e.x, e.y)) {
2910 return;
2911 }
2912
2913 boolean horizontalZoom = false;
2914 boolean horizontalScroll = false;
2915 boolean verticalZoom = false;
2916 boolean verticalScroll = false;
2917
2918 // over the time graph control
2919 if ((e.stateMask & SWT.MODIFIER_MASK) == (SWT.SHIFT | SWT.CTRL)) {
2920 verticalZoom = true;
2921 } else if (e.x < fTimeProvider.getNameSpace()) {
2922 // over the name space
2923 verticalScroll = true;
2924 } else {
2925 // over the state area
2926 if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.CTRL) {
2927 // over the state area, CTRL pressed
2928 horizontalZoom = true;
2929 } else if ((e.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
2930 // over the state area, SHIFT pressed
2931 horizontalScroll = true;
2932 } else {
2933 // over the state area, no modifier pressed
2934 verticalScroll = true;
2935 }
2936 }
2937 if (verticalZoom) {
2938 fVerticalZoomAlignEntry = getVerticalZoomAlignCursor(e.y);
2939 verticalZoom(e.count > 0);
2940 if (fVerticalZoomAlignEntry != null) {
2941 setElementPosition(fVerticalZoomAlignEntry.getKey(), fVerticalZoomAlignEntry.getValue());
2942 }
2943 } else if (horizontalZoom && fTimeProvider.getTime0() != fTimeProvider.getTime1()) {
2944 zoom(e.count > 0);
2945 } else if (horizontalScroll) {
2946 horizontalScroll(e.count > 0);
2947 } else if (verticalScroll){
2948 setTopIndex(getTopIndex() - e.count);
2949 }
2950 }
2951
2952 /**
2953 * Get the vertical zoom alignment entry and position based on the current
2954 * selection. If there is no selection or if the selection is not visible,
2955 * return an alignment entry with a null time graph entry.
2956 *
2957 * @return a map entry where the key is the selection's time graph entry and
2958 * the value is the center y-coordinate of that entry, or null
2959 */
2960 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignSelection() {
2961 Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing();
2962 if (alignEntry != null) {
2963 return alignEntry;
2964 }
2965 int index = getSelectedIndex();
2966 if (index == -1 || index >= getExpandedElementCount()) {
2967 return new SimpleEntry<>(null, 0);
2968 }
2969 Rectangle bounds = getClientArea();
2970 Rectangle itemRect = getItemRect(bounds, index);
2971 if (itemRect.y < bounds.y || itemRect.y > bounds.y + bounds.height) {
2972 /* selection is not visible */
2973 return new SimpleEntry<>(null, 0);
2974 }
2975 ITimeGraphEntry entry = getExpandedElement(index);
2976 int y = itemRect.y + itemRect.height / 2;
2977 return new SimpleEntry<>(entry, y);
2978 }
2979
2980 /**
2981 * Get the vertical zoom alignment entry and position at the specified
2982 * cursor position.
2983 *
2984 * @param y
2985 * the cursor y-coordinate
2986 * @return a map entry where the key is the time graph entry under the
2987 * cursor and the value is the cursor y-coordinate
2988 */
2989 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignCursor(int y) {
2990 Entry<ITimeGraphEntry, Integer> alignEntry = getVerticalZoomAlignOngoing();
2991 if (alignEntry != null) {
2992 return alignEntry;
2993 }
2994 int index = getItemIndexAtY(y);
2995 if (index == -1) {
2996 index = getExpandedElementCount() - 1;
2997 }
2998 ITimeGraphEntry entry = getExpandedElement(index);
2999 return new SimpleEntry<>(entry, y);
3000 }
3001
3002 /**
3003 * Get the vertical zoom alignment entry and position if there is an ongoing
3004 * one and we are within the vertical zoom delay, or otherwise return null.
3005 *
3006 * @return a map entry where the key is a time graph entry and the value is
3007 * a y-coordinate, or null
3008 */
3009 private Entry<ITimeGraphEntry, Integer> getVerticalZoomAlignOngoing() {
3010 long currentTimeMillis = System.currentTimeMillis();
3011 if (currentTimeMillis < fVerticalZoomAlignTime + VERTICAL_ZOOM_DELAY) {
3012 /*
3013 * If the vertical zoom is triggered repeatedly in a short amount of
3014 * time, use the initial event's entry and position.
3015 */
3016 fVerticalZoomAlignTime = currentTimeMillis;
3017 return fVerticalZoomAlignEntry;
3018 }
3019 fVerticalZoomAlignTime = currentTimeMillis;
3020 return null;
3021 }
3022
3023 @Override
3024 public void handleEvent(Event event) {
3025 if (event.type == SWT.MouseWheel) {
3026 // prevent horizontal scrolling when the mouse wheel is used to
3027 // scroll vertically or zoom
3028 event.doit = false;
3029 }
3030 }
3031
3032 @Override
3033 public int getBorderWidth() {
3034 return fBorderWidth;
3035 }
3036
3037 /**
3038 * Set the border width
3039 *
3040 * @param borderWidth
3041 * The width
3042 */
3043 public void setBorderWidth(int borderWidth) {
3044 this.fBorderWidth = borderWidth;
3045 }
3046
3047 /**
3048 * @return The current height of the header row
3049 */
3050 public int getHeaderHeight() {
3051 return fHeaderHeight;
3052 }
3053
3054 /**
3055 * Set the height of the header row
3056 *
3057 * @param headerHeight
3058 * The height
3059 */
3060 public void setHeaderHeight(int headerHeight) {
3061 this.fHeaderHeight = headerHeight;
3062 }
3063
3064 /**
3065 * @return The default height of regular item rows
3066 */
3067 public int getItemHeight() {
3068 return fGlobalItemHeight;
3069 }
3070
3071 /**
3072 * Set the default height of regular item rows.
3073 *
3074 * @param rowHeight
3075 * The height
3076 */
3077 public void setItemHeight(int rowHeight) {
3078 this.fGlobalItemHeight = rowHeight;
3079 for (Item item : fItemData.fItems) {
3080 item.fItemHeight = rowHeight;
3081 }
3082 }
3083
3084 /**
3085 * Set the height of a specific item. Overrides the default item height.
3086 *
3087 * @param entry
3088 * A time graph entry
3089 * @param rowHeight
3090 * The height
3091 * @return true if the height is successfully stored, false otherwise
3092 */
3093 public boolean setItemHeight(ITimeGraphEntry entry, int rowHeight) {
3094 Item item = fItemData.findItem(entry);
3095 if (item != null) {
3096 item.fItemHeight = rowHeight;
3097 return true;
3098 }
3099 return false;
3100 }
3101
3102 /**
3103 * Set the minimum item width
3104 *
3105 * @param width The minimum width
3106 */
3107 public void setMinimumItemWidth(int width) {
3108 this.fMinimumItemWidth = width;
3109 }
3110
3111 /**
3112 * @return The minimum item width
3113 */
3114 public int getMinimumItemWidth() {
3115 return fMinimumItemWidth;
3116 }
3117
3118 /**
3119 * Set whether all time events with a duration shorter than one pixel should
3120 * be blended in. If false, only the first such time event will be drawn and
3121 * the subsequent time events in the same pixel will be discarded. The
3122 * default value is false.
3123 *
3124 * @param blend
3125 * true if sub-pixel events should be blended, false otherwise.
3126 * @since 1.1
3127 */
3128 public void setBlendSubPixelEvents(boolean blend) {
3129 fBlendSubPixelEvents = blend;
3130 }
3131
3132 @Override
3133 public void addSelectionChangedListener(ISelectionChangedListener listener) {
3134 if (listener != null && !fSelectionChangedListeners.contains(listener)) {
3135 fSelectionChangedListeners.add(listener);
3136 }
3137 }
3138
3139 @Override
3140 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
3141 if (listener != null) {
3142 fSelectionChangedListeners.remove(listener);
3143 }
3144 }
3145
3146 @Override
3147 public void setSelection(ISelection selection) {
3148 if (selection instanceof IStructuredSelection) {
3149 Object ob = ((IStructuredSelection) selection).getFirstElement();
3150 if (ob instanceof ITimeGraphEntry) {
3151 selectItem((ITimeGraphEntry) ob, false);
3152 }
3153 }
3154
3155 }
3156
3157 /**
3158 * @param filter The filter object to be attached to the view
3159 */
3160 public void addFilter(@NonNull ViewerFilter filter) {
3161 if (!fFilters.contains(filter)) {
3162 fFilters.add(filter);
3163 }
3164 }
3165
3166 /**
3167 * @param filter The filter object to be attached to the view
3168 */
3169 public void removeFilter(@NonNull ViewerFilter filter) {
3170 fFilters.remove(filter);
3171 }
3172
3173 /**
3174 * Returns this control's filters.
3175 *
3176 * @return an array of viewer filters
3177 * @since 1.2
3178 */
3179 public @NonNull ViewerFilter[] getFilters() {
3180 return Iterables.toArray(fFilters, ViewerFilter.class);
3181 }
3182
3183 /**
3184 * Sets the filters, replacing any previous filters.
3185 *
3186 * @param filters
3187 * an array of viewer filters, or null
3188 * @since 1.2
3189 */
3190 public void setFilters(@NonNull ViewerFilter[] filters) {
3191 fFilters.clear();
3192 if (filters != null) {
3193 fFilters.addAll(Arrays.asList(filters));
3194 }
3195 }
3196
3197 @Override
3198 public void colorSettingsChanged(StateItem[] stateItems) {
3199 /* Destroy previous colors from the resource manager */
3200 if (fEventColorMap != null) {
3201 for (Color color : fEventColorMap) {
3202 fResourceManager.destroyColor(color.getRGB());
3203 }
3204 }
3205 if (stateItems != null) {
3206 fEventColorMap = new Color[stateItems.length];
3207 for (int i = 0; i < stateItems.length; i++) {
3208 fEventColorMap[i] = fResourceManager.createColor(stateItems[i].getStateColor());
3209 }
3210 } else {
3211 fEventColorMap = new Color[] { };
3212 }
3213 redraw();
3214 }
3215
3216 private class ItemData {
3217 private Map<ITimeGraphEntry, Item> fItemMap = new LinkedHashMap<>();
3218 private Item[] fExpandedItems = new Item[0];
3219 private Item[] fItems = new Item[0];
3220 private ITimeGraphEntry fRootEntries[] = new ITimeGraphEntry[0];
3221 private List<ILinkEvent> fLinks = new ArrayList<>();
3222
3223 public ItemData() {
3224 }
3225
3226 public Item findItem(ITimeGraphEntry entry) {
3227 return fItemMap.get(entry);
3228 }
3229
3230 public int findItemIndex(ITimeGraphEntry entry) {
3231 Item item = fItemMap.get(entry);
3232 if (item == null) {
3233 return -1;
3234 }
3235 return item.fExpandedIndex;
3236 }
3237
3238 public void refreshData() {
3239 ITimeGraphEntry selection = getSelectedTrace();
3240 Map<ITimeGraphEntry, Item> itemMap = new LinkedHashMap<>();
3241 for (int i = 0; i < fRootEntries.length; i++) {
3242 ITimeGraphEntry entry = fRootEntries[i];
3243 refreshData(itemMap, null, 0, entry);
3244 }
3245 fItemMap = itemMap;
3246 fItems = fItemMap.values().toArray(new Item[0]);
3247 updateExpandedItems();
3248 if (selection != null) {
3249 for (Item item : fExpandedItems) {
3250 if (item.fEntry == selection) {
3251 item.fSelected = true;
3252 break;
3253 }
3254 }
3255 }
3256 }
3257
3258 private void refreshData(Map<ITimeGraphEntry, Item> itemMap, Item parent, int level, ITimeGraphEntry entry) {
3259 Item item = new Item(entry, entry.getName(), level);
3260 if (parent != null) {
3261 parent.fChildren.add(item);
3262 }
3263 if (fGlobalItemHeight == CUSTOM_ITEM_HEIGHT) {
3264 item.fItemHeight = fTimeGraphProvider.getItemHeight(entry);
3265 } else {
3266 item.fItemHeight = fGlobalItemHeight;
3267 }
3268 item.fItemHeight = Math.max(1, item.fItemHeight + fHeightAdjustment);
3269 itemMap.put(entry, item);
3270 if (entry.hasChildren()) {
3271 Item oldItem = fItemMap.get(entry);
3272 if (oldItem != null && oldItem.fHasChildren && level == oldItem.fLevel && entry.getParent() == oldItem.fEntry.getParent()) {
3273 /* existing items keep their old expanded state */
3274 item.fExpanded = oldItem.fExpanded;
3275 } else {
3276 /* new items set the expanded state according to auto-expand level */
3277 item.fExpanded = fAutoExpandLevel == ALL_LEVELS || level < fAutoExpandLevel;
3278 }
3279 item.fHasChildren = true;
3280 for (ITimeGraphEntry child : entry.getChildren()) {
3281 refreshData(itemMap, item, level + 1, child);
3282 }
3283 }
3284 }
3285
3286 public void updateExpandedItems() {
3287 for (Item item : fItems) {
3288 item.fExpandedIndex = -1;
3289 }
3290 List<Item> expandedItemList = new ArrayList<>();
3291 for (int i = 0; i < fRootEntries.length; i++) {
3292 ITimeGraphEntry entry = fRootEntries[i];
3293 Item item = findItem(entry);
3294 refreshExpanded(expandedItemList, item);
3295 }
3296 fExpandedItems = expandedItemList.toArray(new Item[0]);
3297 fTopIndex = Math.min(fTopIndex, Math.max(0, fExpandedItems.length - 1));
3298 }
3299
3300 private void refreshExpanded(List<Item> expandedItemList, Item item) {
3301 // Check for filters
3302 boolean display = true;
3303 for (ViewerFilter filter : fFilters) {
3304 if (!filter.select(null, item.fEntry.getParent(), item.fEntry)) {
3305 display = false;
3306 break;
3307 }
3308 }
3309 if (display) {
3310 item.fExpandedIndex = expandedItemList.size();
3311 expandedItemList.add(item);
3312 if (item.fHasChildren && item.fExpanded) {
3313 for (Item child : item.fChildren) {
3314 refreshExpanded(expandedItemList, child);
3315 }
3316 }
3317 }
3318 }
3319
3320 public void refreshData(ITimeGraphEntry[] entries) {
3321 if (entries == null) {
3322 fRootEntries = null;
3323 } else {
3324 fRootEntries = Arrays.copyOf(entries, entries.length);
3325 }
3326
3327 refreshData();
3328 }
3329
3330 public void refreshArrows(List<ILinkEvent> events) {
3331 /* If links are null, reset the list */
3332 if (events != null) {
3333 fLinks = events;
3334 } else {
3335 fLinks = new ArrayList<>();
3336 }
3337 }
3338
3339 public ITimeGraphEntry[] getEntries() {
3340 return fRootEntries;
3341 }
3342 }
3343
3344 private class Item {
3345 private boolean fExpanded;
3346 private int fExpandedIndex;
3347 private boolean fSelected;
3348 private boolean fHasChildren;
3349 private int fItemHeight;
3350 private final int fLevel;
3351 private final List<Item> fChildren;
3352 private final String fName;
3353 private final ITimeGraphEntry fEntry;
3354
3355 public Item(ITimeGraphEntry entry, String name, int level) {
3356 this.fEntry = entry;
3357 this.fName = name;
3358 this.fLevel = level;
3359 this.fChildren = new ArrayList<>();
3360 }
3361
3362 @Override
3363 public String toString() {
3364 return fName;
3365 }
3366 }
3367
3368 @Override
3369 public void menuDetected(MenuDetectEvent e) {
3370 if (null == fTimeProvider) {
3371 return;
3372 }
3373 /*
3374 * This flag indicates if menu was prevented from being shown below and
3375 * therefore must be made visible on callback from mouseUp().
3376 */
3377 boolean pendingEventCallback = fPendingMenuDetectEvent != null;
3378 Point p = toControl(e.x, e.y);
3379 if (e.detail == SWT.MENU_MOUSE && isOverTimeSpace(p.x, p.y)) {
3380 if (fPendingMenuDetectEvent == null) {
3381 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
3382 * Store the event and trigger it later just before handling mouseUp.
3383 * This allows for the method to detect if mouse is used to drag zoom.
3384 */
3385 fPendingMenuDetectEvent = e;
3386 /*
3387 * Prevent the platform to show the menu when returning. The
3388 * menu will be shown (see below) when this method is called
3389 * again during mouseUp().
3390 */
3391 e.doit = false;
3392 return;
3393 }
3394 fPendingMenuDetectEvent = null;
3395 if (fDragState != DRAG_ZOOM || !isInDragZoomMargin()) {
3396 /*
3397 * Don't show the menu on mouseUp() if a drag zoom is in
3398 * progress with a drag range outside of the drag zoom margin,
3399 * or if any other drag operation, or none, is in progress.
3400 */
3401 e.doit = false;
3402 return;
3403 }
3404 } else {
3405 if (fDragState != DRAG_NONE) {
3406 /*
3407 * Don't show the menu on keyboard menu or mouse menu outside of
3408 * the time space if any drag operation is in progress.
3409 */
3410 e.doit = false;
3411 return;
3412 }
3413 }
3414 int idx = getItemIndexAtY(p.y);
3415 if (idx >= 0 && idx < fItemData.fExpandedItems.length) {
3416 Item item = fItemData.fExpandedItems[idx];
3417 ITimeGraphEntry entry = item.fEntry;
3418
3419 /* Send menu event for the time graph entry */
3420 e.doit = true;
3421 e.data = entry;
3422 fireMenuEventOnTimeGraphEntry(e);
3423 Menu menu = getMenu();
3424 if (pendingEventCallback && e.doit && (menu != null)) {
3425 menu.setVisible(true);
3426 }
3427
3428 /* Send menu event for time event */
3429 if (entry.hasTimeEvents()) {
3430 ITimeEvent event = Utils.findEvent(entry, getTimeAtX(p.x), 2);
3431 if (event != null) {
3432 e.doit = true;
3433 e.data = event;
3434 fireMenuEventOnTimeEvent(e);
3435 menu = getMenu();
3436 if (pendingEventCallback && e.doit && (menu != null)) {
3437 menu.setVisible(true);
3438 }
3439 }
3440 }
3441 }
3442 }
3443
3444 /**
3445 * Perform the alignment operation.
3446 *
3447 * @param offset
3448 * the alignment offset
3449 *
3450 * @see ITmfTimeAligned
3451 *
3452 * @since 1.0
3453 */
3454 public void performAlign(int offset) {
3455 fTimeProvider.setNameSpace(offset);
3456 }
3457
3458 /**
3459 * Return the time alignment information
3460 *
3461 * @return the time alignment information
3462 *
3463 * @see ITmfTimeAligned
3464 *
3465 * @since 1.0
3466 */
3467 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
3468 return new TmfTimeViewAlignmentInfo(getShell(), toDisplay(0, 0), fTimeProvider.getNameSpace());
3469 }
3470
3471 private boolean isInDragZoomMargin() {
3472 return (Math.abs(fDragX - fDragX0) < DRAG_MARGIN);
3473 }
3474 }
This page took 0.11287 seconds and 5 git commands to generate.