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