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