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