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