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