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