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