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