1 /*****************************************************************************
2 * Copyright (c) 2007, 2013 Intel Corporation and others
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
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 *****************************************************************************/
19 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
26 import org
.eclipse
.jface
.action
.IStatusLineManager
;
27 import org
.eclipse
.jface
.resource
.JFaceResources
;
28 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
32 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.ControlEvent
;
46 import org
.eclipse
.swt
.events
.ControlListener
;
47 import org
.eclipse
.swt
.events
.FocusEvent
;
48 import org
.eclipse
.swt
.events
.FocusListener
;
49 import org
.eclipse
.swt
.events
.KeyEvent
;
50 import org
.eclipse
.swt
.events
.KeyListener
;
51 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
52 import org
.eclipse
.swt
.events
.MenuDetectListener
;
53 import org
.eclipse
.swt
.events
.MouseEvent
;
54 import org
.eclipse
.swt
.events
.MouseListener
;
55 import org
.eclipse
.swt
.events
.MouseMoveListener
;
56 import org
.eclipse
.swt
.events
.MouseTrackListener
;
57 import org
.eclipse
.swt
.events
.MouseWheelListener
;
58 import org
.eclipse
.swt
.events
.PaintEvent
;
59 import org
.eclipse
.swt
.events
.SelectionEvent
;
60 import org
.eclipse
.swt
.events
.SelectionListener
;
61 import org
.eclipse
.swt
.events
.TraverseEvent
;
62 import org
.eclipse
.swt
.events
.TraverseListener
;
63 import org
.eclipse
.swt
.events
.TypedEvent
;
64 import org
.eclipse
.swt
.graphics
.Color
;
65 import org
.eclipse
.swt
.graphics
.Cursor
;
66 import org
.eclipse
.swt
.graphics
.GC
;
67 import org
.eclipse
.swt
.graphics
.Image
;
68 import org
.eclipse
.swt
.graphics
.Point
;
69 import org
.eclipse
.swt
.graphics
.Rectangle
;
70 import org
.eclipse
.swt
.widgets
.Composite
;
71 import org
.eclipse
.swt
.widgets
.Display
;
72 import org
.eclipse
.swt
.widgets
.Event
;
73 import org
.eclipse
.swt
.widgets
.Listener
;
74 import org
.eclipse
.swt
.widgets
.ScrollBar
;
77 * Time graph control implementation
80 * @author Alvaro Sanchez-Leon
81 * @author Patrick Tasse
83 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
, MenuDetectListener
, ITmfTimeGraphDrawingHelper
{
86 /** Max scrollbar size */
87 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
89 private static final int DRAG_NONE
= 0;
90 private static final int DRAG_TRACE_ITEM
= 1;
91 private static final int DRAG_SPLIT_LINE
= 2;
92 private static final int DRAG_ZOOM
= 3;
93 private static final int DRAG_SELECTION
= 4;
95 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
97 private static final double ZOOM_FACTOR
= 1.5;
98 private static final double ZOOM_IN_FACTOR
= 0.8;
99 private static final double ZOOM_OUT_FACTOR
= 1.25;
101 private static final int SNAP_WIDTH
= 2;
103 private static final int NO_STATUS
= -1;
105 /** Resource manager */
106 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
108 /** Color map for event types */
109 private Color
[] fEventColorMap
= null;
111 private ITimeDataProvider fTimeProvider
;
112 private IStatusLineManager fStatusLineManager
= null;
113 private TimeGraphScale fTimeGraphScale
= null;
115 private boolean fIsInFocus
= false;
116 private boolean fMouseOverSplitLine
= false;
117 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
118 private int fMinimumItemWidth
= 0;
119 private int fTopIndex
= 0;
120 private int fDragState
= DRAG_NONE
;
121 private int fDragButton
;
122 private int fDragX0
= 0;
123 private int fDragX
= 0;
124 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
125 private int fIdealNameSpace
= 0;
126 private long fTime0bak
;
127 private long fTime1bak
;
128 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
129 private ItemData fItemData
= null;
130 private List
<SelectionListener
> fSelectionListeners
;
131 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
132 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
133 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
134 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
135 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
136 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
137 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
138 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
139 private final List
<ViewerFilter
> fFilters
= new ArrayList
<ViewerFilter
>();
140 private MenuDetectEvent fPendingMenuDetectEvent
= null;
141 private boolean fHideArrows
= false;
143 private int fBorderWidth
= 0;
144 private int fHeaderHeight
= 0;
146 private Listener fMouseScrollFilterListener
;
148 private MouseScrollNotifier fMouseScrollNotifier
;
149 private final Object fMouseScrollNotifierLock
= new Object();
151 private class MouseScrollNotifier
extends Thread
{
152 private static final long DELAY
= 400L;
153 private static final long POLLING_INTERVAL
= 10L;
154 private long fLastScrollTime
= Long
.MAX_VALUE
;
158 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
160 Thread
.sleep(POLLING_INTERVAL
);
161 } catch (Exception e
) {
165 if (!isInterrupted()) {
166 Display
.getDefault().asyncExec(new Runnable() {
172 fTimeProvider
.notifyStartFinishTime();
176 synchronized (fMouseScrollNotifierLock
) {
177 fMouseScrollNotifier
= null;
181 public void mouseScrolled() {
182 fLastScrollTime
= System
.currentTimeMillis();
187 * Standard constructor
190 * The parent composite object
192 * The color scheme to use
194 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
196 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
198 fItemData
= new ItemData();
200 addFocusListener(this);
201 addMouseListener(this);
202 addMouseMoveListener(this);
203 addMouseTrackListener(this);
204 addMouseWheelListener(this);
205 addTraverseListener(this);
206 addKeyListener(this);
207 addControlListener(this);
208 addMenuDetectListener(this);
209 ScrollBar scrollHor
= getHorizontalBar();
211 if (scrollHor
!= null) {
212 scrollHor
.addSelectionListener(this);
217 public void dispose() {
219 fResourceManager
.dispose();
223 * Sets the timegraph provider used by this timegraph viewer.
225 * @param timeGraphProvider the timegraph provider
227 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
228 fTimeGraphProvider
= timeGraphProvider
;
230 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
231 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
234 if (fEventColorMap
!= null) {
235 for (Color color
: fEventColorMap
) {
236 fResourceManager
.destroyColor(color
.getRGB());
239 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
240 if (stateItems
!= null) {
241 fEventColorMap
= new Color
[stateItems
.length
];
242 for (int i
= 0; i
< stateItems
.length
; i
++) {
243 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
246 fEventColorMap
= new Color
[] { };
251 * Assign the given time provider
253 * @param timeProvider
256 public void setTimeProvider(ITimeDataProvider timeProvider
) {
257 fTimeProvider
= timeProvider
;
263 * Assign the status line manager
265 * @param statusLineManager
266 * The status line manager, or null to disable status line messages
269 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
270 if (fStatusLineManager
!= null && statusLineManager
== null) {
271 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
273 fStatusLineManager
= statusLineManager
;
277 * Assign the time graph scale
279 * @param timeGraphScale
280 * The time graph scale
283 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
284 fTimeGraphScale
= timeGraphScale
;
288 * Add a selection listener
291 * The listener to add
293 public void addSelectionListener(SelectionListener listener
) {
294 if (listener
== null) {
295 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
297 if (null == fSelectionListeners
) {
298 fSelectionListeners
= new ArrayList
<SelectionListener
>();
300 fSelectionListeners
.add(listener
);
304 * Remove a selection listener
307 * The listener to remove
309 public void removeSelectionListener(SelectionListener listener
) {
310 if (null != fSelectionListeners
) {
311 fSelectionListeners
.remove(listener
);
316 * Selection changed callback
318 public void fireSelectionChanged() {
319 if (null != fSelectionListeners
) {
320 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
321 while (it
.hasNext()) {
322 SelectionListener listener
= it
.next();
323 listener
.widgetSelected(null);
329 * Default selection callback
331 public void fireDefaultSelection() {
332 if (null != fSelectionListeners
) {
333 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
334 while (it
.hasNext()) {
335 SelectionListener listener
= it
.next();
336 listener
.widgetDefaultSelected(null);
342 * Get the traces in the model
344 * @return The array of traces
346 public ITimeGraphEntry
[] getTraces() {
347 return fItemData
.getTraces();
351 * Get the on/off trace filters
353 * @return The array of filters
355 public boolean[] getTraceFilter() {
356 return fItemData
.getTraceFilter();
360 * Refresh the data for the thing
362 public void refreshData() {
363 fItemData
.refreshData();
369 * Refresh data for the given traces
372 * The traces to refresh
374 public void refreshData(ITimeGraphEntry
[] traces
) {
375 fItemData
.refreshData(traces
);
381 * Refresh the links (arrows) of this widget
383 * @param events The link events to refresh
386 public void refreshArrows(List
<ILinkEvent
> events
) {
387 fItemData
.refreshArrows(events
);
391 * Adjust the scoll bars
393 public void adjustScrolls() {
394 if (null == fTimeProvider
) {
395 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
401 long time0
= fTimeProvider
.getTime0();
402 long time1
= fTimeProvider
.getTime1();
404 long timeMin
= fTimeProvider
.getMinTime();
405 long timeMax
= fTimeProvider
.getMaxTime();
407 long delta
= timeMax
- timeMin
;
410 int thumb
= H_SCROLLBAR_MAX
;
413 // Thumb size (page size)
414 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
415 // At the beginning of visible window
416 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
419 // position, minimum, maximum, thumb size, increment (half page)t, page
420 // increment size (full page)
421 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
424 boolean ensureVisibleItem(int idx
, boolean redraw
) {
425 boolean changed
= false;
428 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
429 if (fItemData
.fExpandedItems
[index
].fSelected
) {
434 if (index
>= fItemData
.fExpandedItems
.length
) {
437 if (index
< fTopIndex
) {
444 int page
= countPerPage();
445 if (index
>= fTopIndex
+ page
) {
446 setTopIndex(index
- page
+ 1);
457 * Assign the given index as the top one
462 public void setTopIndex(int idx
) {
463 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
464 index
= Math
.max(0, index
);
470 * Set the expanded state of a given entry
475 * True if expanded, false if collapsed
477 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
478 Item item
= fItemData
.findItem(entry
);
479 if (item
!= null && item
.fExpanded
!= expanded
) {
480 item
.fExpanded
= expanded
;
481 fItemData
.updateExpandedItems();
487 * Collapses all nodes of the viewer's tree, starting with the root.
491 public void collapseAll() {
492 for (Item item
: fItemData
.fItems
) {
493 item
.fExpanded
= false;
495 fItemData
.updateExpandedItems();
500 * Expands all nodes of the viewer's tree, starting with the root.
504 public void expandAll() {
505 for (Item item
: fItemData
.fItems
) {
506 item
.fExpanded
= true;
508 fItemData
.updateExpandedItems();
513 * Add a tree listener
516 * The listener to add
518 public void addTreeListener(ITimeGraphTreeListener listener
) {
519 if (!fTreeListeners
.contains(listener
)) {
520 fTreeListeners
.add(listener
);
525 * Remove a tree listener
528 * The listener to remove
530 public void removeTreeListener(ITimeGraphTreeListener listener
) {
531 if (fTreeListeners
.contains(listener
)) {
532 fTreeListeners
.remove(listener
);
537 * Tree event callback
542 * The expanded state (true for expanded, false for collapsed)
544 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
545 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
546 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
548 listener
.treeExpanded(event
);
550 listener
.treeCollapsed(event
);
556 * Add a menu listener on {@link ITimeGraphEntry}s
558 * The listener to add
561 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
562 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
563 fTimeGraphEntryMenuListeners
.add(listener
);
568 * Remove a menu listener on {@link ITimeGraphEntry}s
571 * The listener to remove
574 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
575 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
576 fTimeGraphEntryMenuListeners
.remove(listener
);
581 * Menu event callback on {@link ITimeGraphEntry}s
584 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
586 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
587 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
588 listener
.menuDetected(event
);
593 * Add a menu listener on {@link ITimeEvent}s
596 * The listener to add
599 public void addTimeEventMenuListener(MenuDetectListener listener
) {
600 if (!fTimeEventMenuListeners
.contains(listener
)) {
601 fTimeEventMenuListeners
.add(listener
);
606 * Remove a menu listener on {@link ITimeEvent}s
609 * The listener to remove
612 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
613 if (fTimeEventMenuListeners
.contains(listener
)) {
614 fTimeEventMenuListeners
.remove(listener
);
619 * Menu event callback on {@link ITimeEvent}s
622 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
624 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
625 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
626 listener
.menuDetected(event
);
630 @SuppressWarnings("deprecation")
632 public ISelection
getSelection() {
633 TimeGraphSelection sel
= new TimeGraphSelection();
634 ITimeGraphEntry trace
= getSelectedTrace();
635 if (null != trace
&& null != fTimeProvider
) {
637 if (fTimeProvider
instanceof ITimeDataProvider2
) {
638 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
640 selectedTime
= fTimeProvider
.getSelectedTime();
642 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
653 * Get the selection object
655 * @return The selection
657 public ISelection
getSelectionTrace() {
658 TimeGraphSelection sel
= new TimeGraphSelection();
659 ITimeGraphEntry trace
= getSelectedTrace();
667 * Enable/disable one of the traces in the model
670 * 1 to enable it, -1 to disable. The method returns immediately
671 * if another value is used.
673 public void selectTrace(int n
) {
674 if ((n
!= 1) && (n
!= -1)) {
678 boolean changed
= false;
679 int lastSelection
= -1;
680 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
681 Item item
= fItemData
.fExpandedItems
[i
];
682 if (item
.fSelected
) {
684 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
685 item
.fSelected
= false;
686 item
= fItemData
.fExpandedItems
[i
+ 1];
687 item
.fSelected
= true;
689 } else if ((-1 == n
) && (i
> 0)) {
690 item
.fSelected
= false;
691 item
= fItemData
.fExpandedItems
[i
- 1];
692 item
.fSelected
= true;
699 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
700 Item item
= fItemData
.fExpandedItems
[0];
701 item
.fSelected
= true;
706 ensureVisibleItem(-1, false);
708 fireSelectionChanged();
716 * 1 for next event, -1 for previous event
718 public void selectEvent(int n
) {
719 if (null == fTimeProvider
) {
722 ITimeGraphEntry trace
= getSelectedTrace();
727 if (fTimeProvider
instanceof ITimeDataProvider2
) {
728 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
730 @SuppressWarnings("deprecation")
731 long time
= fTimeProvider
.getSelectedTime();
734 long endTime
= fTimeProvider
.getEndTime();
735 ITimeEvent nextEvent
;
736 if (-1 == n
&& selectedTime
> endTime
) {
737 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
739 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
741 if (null == nextEvent
&& -1 == n
) {
742 nextEvent
= Utils
.getFirstEvent(trace
);
744 if (null != nextEvent
) {
745 long nextTime
= nextEvent
.getTime();
746 // If last event detected e.g. going back or not moving to a next
748 if (nextTime
<= selectedTime
&& n
== 1) {
749 // Select to the end of this last event
750 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
751 // but not beyond the end of the trace
752 if (nextTime
> endTime
) {
755 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
756 // for previous event go to its end time unless we were already there
757 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
759 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
760 fireSelectionChanged();
762 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
763 fireSelectionChanged();
768 * Select the next event
770 public void selectNextEvent() {
772 // Notify if visible time window has been adjusted
773 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
777 * Select the previous event
779 public void selectPrevEvent() {
781 // Notify if visible time window has been adjusted
782 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
786 * Select the next trace
788 public void selectNextTrace() {
793 * Select the previous trace
795 public void selectPrevTrace() {
800 * Zoom based on mouse cursor location with mouse scrolling
802 * @param zoomIn true to zoom in, false to zoom out
804 public void zoom(boolean zoomIn
) {
805 int globalX
= getDisplay().getCursorLocation().x
;
806 Point p
= toControl(globalX
, 0);
807 int nameSpace
= fTimeProvider
.getNameSpace();
808 int timeSpace
= fTimeProvider
.getTimeSpace();
809 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
810 long time0
= fTimeProvider
.getTime0();
811 long time1
= fTimeProvider
.getTime1();
812 long interval
= time1
- time0
;
815 } // to allow getting out of single point interval
818 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
820 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
822 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
823 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
824 long newTime1
= newTime0
+ newInterval
;
825 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
826 synchronized (fMouseScrollNotifierLock
) {
827 if (fMouseScrollNotifier
== null) {
828 fMouseScrollNotifier
= new MouseScrollNotifier();
829 fMouseScrollNotifier
.start();
831 fMouseScrollNotifier
.mouseScrolled();
836 * zoom in using single click
838 public void zoomIn() {
839 long prevTime0
= fTimeProvider
.getTime0();
840 long prevTime1
= fTimeProvider
.getTime1();
841 long prevRange
= prevTime1
- prevTime0
;
842 if (prevRange
== 0) {
846 if (fTimeProvider
instanceof ITimeDataProvider2
) {
847 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
848 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
850 @SuppressWarnings("deprecation")
851 long selectedTime
= fTimeProvider
.getSelectedTime();
852 selTime
= selectedTime
;
854 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
855 selTime
= (prevTime0
+ prevTime1
) / 2;
857 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
858 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
860 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
862 if (inaccuracy
> 0 && inaccuracy
< 100) {
863 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
867 long min
= fTimeProvider
.getMinTimeInterval();
868 if ((time1
- time0
) < min
) {
869 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
873 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
877 * zoom out using single click
879 public void zoomOut() {
880 long prevTime0
= fTimeProvider
.getTime0();
881 long prevTime1
= fTimeProvider
.getTime1();
883 if (fTimeProvider
instanceof ITimeDataProvider2
) {
884 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
885 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
887 @SuppressWarnings("deprecation")
888 long selectedTime
= fTimeProvider
.getSelectedTime();
889 selTime
= selectedTime
;
891 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
892 selTime
= (prevTime0
+ prevTime1
) / 2;
894 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
895 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
897 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
898 if (inaccuracy
> 0 && inaccuracy
< 100) {
899 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
903 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
909 * @param hideArrows true to hide arrows
913 public void hideArrows(boolean hideArrows
) {
914 fHideArrows
= hideArrows
;
918 * Follow the arrow forward
922 public void followArrowFwd() {
923 ITimeGraphEntry trace
= getSelectedTrace();
927 long selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
928 for (ILinkEvent link
: fItemData
.fLinks
) {
929 if (link
.getEntry() == trace
&& link
.getTime() == selectedTime
) {
930 selectItem(link
.getDestinationEntry(), false);
931 if (link
.getDuration() != 0) {
932 fTimeProvider
.setSelectedTimeNotify(link
.getTime() + link
.getDuration(), true);
933 // Notify if visible time window has been adjusted
934 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
936 fireSelectionChanged();
944 * Follow the arrow backward
948 public void followArrowBwd() {
949 ITimeGraphEntry trace
= getSelectedTrace();
953 long selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
954 for (ILinkEvent link
: fItemData
.fLinks
) {
955 if (link
.getDestinationEntry() == trace
&& link
.getTime() + link
.getDuration() == selectedTime
) {
956 selectItem(link
.getEntry(), false);
957 if (link
.getDuration() != 0) {
958 fTimeProvider
.setSelectedTimeNotify(link
.getTime(), true);
959 // Notify if visible time window has been adjusted
960 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
962 fireSelectionChanged();
970 * Return the currently selected trace
972 * @return The entry matching the trace
974 public ITimeGraphEntry
getSelectedTrace() {
975 ITimeGraphEntry trace
= null;
976 int idx
= getSelectedIndex();
978 trace
= fItemData
.fExpandedItems
[idx
].fTrace
;
984 * Retrieve the index of the currently selected item
988 public int getSelectedIndex() {
990 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
991 Item item
= fItemData
.fExpandedItems
[i
];
992 if (item
.fSelected
) {
1000 boolean toggle(int idx
) {
1001 boolean toggled
= false;
1002 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1003 Item item
= fItemData
.fExpandedItems
[idx
];
1004 if (item
.fHasChildren
) {
1005 item
.fExpanded
= !item
.fExpanded
;
1006 fItemData
.updateExpandedItems();
1010 fireTreeEvent(item
.fTrace
, item
.fExpanded
);
1016 int getItemIndexAtY(int y
) {
1021 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1022 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1030 boolean isOverSplitLine(int x
) {
1031 if (x
< 0 || null == fTimeProvider
) {
1034 int nameWidth
= fTimeProvider
.getNameSpace();
1035 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
1038 ITimeGraphEntry
getEntry(Point pt
) {
1039 int idx
= getItemIndexAtY(pt
.y
);
1040 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fTrace
: null;
1047 public int getXForTime(long time
) {
1048 if (null == fTimeProvider
) {
1051 long time0
= fTimeProvider
.getTime0();
1052 long time1
= fTimeProvider
.getTime1();
1053 int width
= getCtrlSize().x
;
1054 int nameSpace
= fTimeProvider
.getNameSpace();
1055 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1056 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1064 public long getTimeAtX(int coord
) {
1065 if (null == fTimeProvider
) {
1069 Point size
= getCtrlSize();
1070 long time0
= fTimeProvider
.getTime0();
1071 long time1
= fTimeProvider
.getTime1();
1072 int nameWidth
= fTimeProvider
.getNameSpace();
1073 final int x
= coord
- nameWidth
;
1074 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1075 if (x
>= 0 && size
.x
>= nameWidth
) {
1076 if (time1
- time0
> timeWidth
) {
1077 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1078 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1080 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1081 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1087 void selectItem(int idx
, boolean addSelection
) {
1088 boolean changed
= false;
1090 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1091 Item item
= fItemData
.fExpandedItems
[idx
];
1092 changed
= !item
.fSelected
;
1093 item
.fSelected
= true;
1096 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1097 Item item
= fItemData
.fExpandedItems
[i
];
1098 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1101 item
.fSelected
= i
== idx
;
1104 changed
|= ensureVisibleItem(idx
, true);
1111 * Callback for item selection
1114 * The entry matching the trace
1115 * @param addSelection
1116 * If the selection is added or removed
1118 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1119 int idx
= fItemData
.findItemIndex(trace
);
1120 selectItem(idx
, addSelection
);
1124 * Retrieve the number of entries shown per page.
1128 public int countPerPage() {
1129 int height
= getCtrlSize().y
;
1132 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1133 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1134 if (ySum
>= height
) {
1139 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1140 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1141 if (ySum
>= height
) {
1150 * Get the index of the top element
1154 public int getTopIndex() {
1159 * Get the number of expanded items
1161 * @return The count of expanded items
1163 public int getExpandedElementCount() {
1164 return fItemData
.fExpandedItems
.length
;
1168 * Get an array of all expanded elements
1170 * @return The expanded elements
1172 public ITimeGraphEntry
[] getExpandedElements() {
1173 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1174 for (Item item
: fItemData
.fExpandedItems
) {
1175 elements
.add(item
.fTrace
);
1177 return elements
.toArray(new ITimeGraphEntry
[0]);
1180 Point
getCtrlSize() {
1181 Point size
= getSize();
1182 if (getHorizontalBar().isVisible()) {
1183 size
.y
-= getHorizontalBar().getSize().y
;
1188 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1189 Rectangle rect
= getStatesRect(bound
, idx
, nameWidth
);
1191 rect
.width
= nameWidth
;
1195 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1196 int x
= bound
.x
+ nameWidth
;
1197 int width
= bound
.width
- x
;
1199 if (idx
>= fTopIndex
) {
1200 for (int i
= fTopIndex
; i
< idx
; i
++) {
1201 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1204 for (int i
= fTopIndex
- 1; i
>= idx
; i
--) {
1205 ySum
-= fItemData
.fExpandedItems
[i
].fItemHeight
;
1208 int y
= bound
.y
+ ySum
;
1209 int height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1210 return new Rectangle(x
, y
, width
, height
);
1214 void paint(Rectangle bounds
, PaintEvent e
) {
1216 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1217 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1219 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1223 fIdealNameSpace
= 0;
1224 int nameSpace
= fTimeProvider
.getNameSpace();
1226 // draw empty name space background
1227 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1228 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1231 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1232 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1233 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1235 int alpha
= gc
.getAlpha();
1238 long time0
= fTimeProvider
.getTime0();
1239 long time1
= fTimeProvider
.getTime1();
1240 long selectionBegin
;
1242 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1243 selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1244 selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1246 @SuppressWarnings("deprecation")
1247 long selectedTime
= fTimeProvider
.getSelectedTime();
1248 selectionBegin
= selectedTime
;
1249 selectionEnd
= selectedTime
;
1251 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1252 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1253 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1255 // draw selection lines
1256 if (fDragState
!= DRAG_SELECTION
) {
1257 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1258 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1259 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1262 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1263 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1268 // draw selection background
1269 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1270 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1271 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1272 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1274 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1275 } else if (x0
- x1
> 1) {
1276 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1280 // draw drag selection background
1281 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1282 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1283 if (fDragX0
< fDragX
) {
1284 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1285 } else if (fDragX0
> fDragX
) {
1286 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1291 if (DRAG_SPLIT_LINE
== fDragState
) {
1292 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1293 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1294 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1295 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1296 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1297 if (fDragX
!= fDragX0
) {
1298 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1300 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1301 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1302 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1303 if (fDragX
!= fDragX0
) {
1304 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1306 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1307 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1308 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1315 * Draw many items at once
1318 * The rectangle of the area
1319 * @param timeProvider
1322 * The array items to draw
1324 * The index of the first element to draw
1326 * The width reserved for the names
1328 * Reference to the SWT GC object
1330 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1331 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1332 for (int i
= topIndex
; i
< items
.length
; i
++) {
1333 Item item
= items
[i
];
1334 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1341 * @param item the item to draw
1342 * @param bounds the container rectangle
1343 * @param timeProvider Time provider
1344 * @param i the item index
1345 * @param nameSpace the name space
1346 * @param gc Graphics context
1348 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1349 ITimeGraphEntry entry
= item
.fTrace
;
1350 long time0
= timeProvider
.getTime0();
1351 long time1
= timeProvider
.getTime1();
1353 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1354 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1356 @SuppressWarnings("deprecation")
1357 long time
= fTimeProvider
.getSelectedTime();
1358 selectedTime
= time
;
1361 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1362 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1366 if (! item
.fTrace
.hasTimeEvents()) {
1367 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1368 nameRect
.width
+= statesRect
.width
;
1369 drawName(item
, nameRect
, gc
);
1371 drawName(item
, nameRect
, gc
);
1373 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1374 if (rect
.isEmpty()) {
1375 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1378 if (time1
<= time0
) {
1379 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1380 gc
.fillRectangle(rect
);
1381 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1385 // Initialize _rect1 to same values as enclosing rectangle rect
1386 Rectangle stateRect
= Utils
.clone(rect
);
1387 boolean selected
= item
.fSelected
;
1388 // K pixels per second
1389 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1391 if (item
.fTrace
.hasTimeEvents()) {
1392 fillSpace(rect
, gc
, selected
);
1393 // Drawing rectangle is smaller than reserved space
1395 stateRect
.height
-= 6;
1397 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1398 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1401 while (iterator
.hasNext()) {
1402 ITimeEvent event
= iterator
.next();
1403 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1404 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1405 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1406 // event is out of bounds
1409 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1410 stateRect
.x
= Math
.max(rect
.x
, x
);
1411 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1412 if (stateRect
.x
== lastX
) {
1413 stateRect
.width
-= 1;
1414 if (stateRect
.width
> 0) {
1415 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1416 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1420 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1421 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1426 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1433 * The rectangle of the area
1434 * @param timeProvider
1437 * The array items to draw
1439 * The width reserved for the names
1441 * Reference to the SWT GC object
1444 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1445 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1449 for (ILinkEvent event
: links
) {
1450 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1455 * Draws the link type events of this item
1460 * the container rectangle
1461 * @param timeProvider
1469 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1470 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1471 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1473 if ((srcIndex
== -1) || (destIndex
== -1)) {
1477 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1478 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1480 int x0
= getXForTime(event
.getTime());
1481 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1483 // limit the x-coordinates to prevent integer overflow in calculations
1484 // and also GC.drawLine doesn't draw properly with large coordinates
1485 final int limit
= Integer
.MAX_VALUE
/ 1024;
1486 x0
= Math
.max(-limit
, Math
.min(x0
, limit
));
1487 x1
= Math
.max(-limit
, Math
.min(x1
, limit
));
1489 int y0
= src
.y
+ src
.height
/ 2;
1490 int y1
= dst
.y
+ dst
.height
/ 2;
1491 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1495 * Draw the state (color fill)
1500 * Time event for which we're drawing the state
1505 * @return true if the state was drawn
1508 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1509 Rectangle rect
, GC gc
) {
1511 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1515 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1518 Color stateColor
= null;
1519 if (colorIdx
< fEventColorMap
.length
) {
1520 stateColor
= fEventColorMap
[colorIdx
];
1522 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1525 gc
.setForeground(stateColor
);
1526 gc
.setBackground(stateColor
);
1528 /* Draw the arrow */
1529 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1530 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1533 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1538 * @author Francis Giraldeau
1541 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1543 * The algorithm was taken from this site, not the code itself
1545 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1548 double cos
= 0.9510;
1549 double sin
= 0.3090;
1550 long lenx
= x1
- x0
;
1551 long leny
= y1
- y0
;
1552 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1554 double dx
= factor
* lenx
/ len
;
1555 double dy
= factor
* leny
/ len
;
1556 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1557 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1558 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1559 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1560 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1561 gc
.fillPolygon(arrow
);
1565 * Draw the name of an item.
1570 * Where to draw the name
1574 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1575 boolean hasTimeEvents
= item
.fTrace
.hasTimeEvents();
1576 if (! hasTimeEvents
) {
1577 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1578 gc
.fillRectangle(bounds
);
1579 if (item
.fSelected
&& fIsInFocus
) {
1580 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1581 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1584 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1585 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1586 gc
.fillRectangle(bounds
);
1589 // No name to be drawn
1590 if (fTimeProvider
.getNameSpace() == 0) {
1594 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1595 if (item
.fHasChildren
) {
1596 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1597 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1598 Rectangle rect
= Utils
.clone(bounds
);
1599 rect
.x
+= leftMargin
;
1600 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1601 rect
.width
= EXPAND_SIZE
;
1602 rect
.height
= EXPAND_SIZE
;
1603 gc
.fillRectangle(rect
);
1604 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1605 int midy
= rect
.y
+ rect
.height
/ 2;
1606 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1607 if (!item
.fExpanded
) {
1608 int midx
= rect
.x
+ rect
.width
/ 2;
1609 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1612 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1614 Image img
= fTimeGraphProvider
.getItemImage(item
.fTrace
);
1617 int imgHeight
= img
.getImageData().height
;
1618 int imgWidth
= img
.getImageData().width
;
1620 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1621 gc
.drawImage(img
, x
, y
);
1622 leftMargin
+= imgWidth
+ MARGIN
;
1624 String name
= item
.fName
;
1625 Point size
= gc
.stringExtent(name
);
1626 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1627 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1629 if (hasTimeEvents
) {
1630 // cut long string with "..."
1631 int width
= bounds
.width
- leftMargin
;
1633 while (size
.x
> width
&& name
.length() > 1) {
1635 name
= name
.substring(0, name
.length() - 1);
1636 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1639 name
+= "..."; //$NON-NLS-1$
1642 Rectangle rect
= Utils
.clone(bounds
);
1643 rect
.x
+= leftMargin
;
1644 rect
.width
-= leftMargin
;
1646 if (rect
.width
> 0) {
1647 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1648 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1649 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1650 leftMargin
+= textWidth
+ MARGIN
;
1653 if (hasTimeEvents
) {
1655 int x
= bounds
.x
+ leftMargin
;
1656 int width
= bounds
.width
- x
;
1657 int midy
= bounds
.y
+ bounds
.height
/ 2;
1658 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1659 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1665 * Draw the state (color fill)
1670 * Time event for which we're drawing the state
1676 * Is this time event currently selected (so it appears
1678 * @param timeSelected
1679 * Is the timestamp currently selected
1680 * @return true if the state was drawn
1683 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1684 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1686 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1687 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1690 boolean visible
= rect
.width
== 0 ?
false : true;
1693 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1694 // Only draw the top and bottom borders
1695 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1696 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1697 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1698 if (rect
.width
== 1) {
1699 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1700 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1704 Color stateColor
= null;
1705 if (colorIdx
< fEventColorMap
.length
) {
1706 stateColor
= fEventColorMap
[colorIdx
];
1708 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1711 boolean reallySelected
= timeSelected
&& selected
;
1712 // fill all rect area
1713 gc
.setBackground(stateColor
);
1714 gc
.fillRectangle(rect
);
1715 // get the border color?
1716 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1719 if (!reallySelected
) {
1720 // Draw the top and bottom borders i.e. no side borders
1721 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1722 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1725 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1726 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1728 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1733 * Fill the space between two contiguous time events
1740 * Is this time event selected or not
1742 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1743 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1744 gc
.fillRectangle(rect
);
1745 if (fDragState
== DRAG_ZOOM
) {
1746 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1747 if (fDragX0
< fDragX
) {
1748 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1749 } else if (fDragX0
> fDragX
) {
1750 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1754 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1755 int midy
= rect
.y
+ rect
.height
/ 2;
1756 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1760 public void keyTraversed(TraverseEvent e
) {
1761 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1767 public void keyPressed(KeyEvent e
) {
1769 if (fItemData
.fExpandedItems
.length
== 0) {
1772 if (SWT
.HOME
== e
.keyCode
) {
1774 } else if (SWT
.END
== e
.keyCode
) {
1775 idx
= fItemData
.fExpandedItems
.length
- 1;
1776 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1777 idx
= getSelectedIndex();
1780 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1783 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1784 idx
= getSelectedIndex();
1787 } else if (idx
> 0) {
1790 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1792 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1794 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1795 int page
= countPerPage();
1796 idx
= getSelectedIndex();
1801 if (idx
>= fItemData
.fExpandedItems
.length
) {
1802 idx
= fItemData
.fExpandedItems
.length
- 1;
1804 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1805 int page
= countPerPage();
1806 idx
= getSelectedIndex();
1814 } else if (SWT
.CR
== e
.keyCode
) {
1815 idx
= getSelectedIndex();
1817 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1820 fireDefaultSelection();
1826 selectItem(idx
, false);
1827 fireSelectionChanged();
1829 int x
= toControl(e
.display
.getCursorLocation()).x
;
1830 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1834 public void keyReleased(KeyEvent e
) {
1835 int x
= toControl(e
.display
.getCursorLocation()).x
;
1836 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1840 public void focusGained(FocusEvent e
) {
1842 if (fMouseScrollFilterListener
== null) {
1843 fMouseScrollFilterListener
= new Listener() {
1844 // This filter is used to prevent horizontal scrolling of the view
1845 // when the mouse wheel is used to zoom
1847 public void handleEvent(Event event
) {
1851 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1854 updateStatusLine(NO_STATUS
);
1858 public void focusLost(FocusEvent e
) {
1860 if (fMouseScrollFilterListener
!= null) {
1861 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1862 fMouseScrollFilterListener
= null;
1864 if (DRAG_NONE
!= fDragState
) {
1866 fDragState
= DRAG_NONE
;
1869 updateStatusLine(NO_STATUS
);
1873 * @return If the current view is focused
1875 public boolean isInFocus() {
1880 * Provide the possibility to control the wait cursor externally e.g. data
1881 * requests in progress
1883 * @param waitInd Should we wait indefinitely?
1885 public void waitCursor(boolean waitInd
) {
1886 // Update cursor as indicated
1888 setCursor(fWaitCursor
);
1894 private void updateCursor(int x
, int stateMask
) {
1895 // if Wait cursor not active, check for the need to change the cursor
1896 if (getCursor() == fWaitCursor
) {
1899 Cursor cursor
= null;
1900 if (fDragState
== DRAG_SPLIT_LINE
) {
1901 } else if (fDragState
== DRAG_SELECTION
) {
1902 cursor
= fResizeCursor
;
1903 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1904 cursor
= fDragCursor
;
1905 } else if (fDragState
== DRAG_ZOOM
) {
1906 cursor
= fZoomCursor
;
1907 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1908 cursor
= fDragCursor
;
1909 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1910 cursor
= fResizeCursor
;
1912 if (!isOverSplitLine(x
) &&fTimeProvider
instanceof ITimeDataProvider2
) {
1913 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1914 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1915 int xBegin
= getXForTime(selectionBegin
);
1916 int xEnd
= getXForTime(selectionEnd
);
1917 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1918 cursor
= fResizeCursor
;
1922 if (getCursor() != cursor
) {
1927 private void updateStatusLine(int x
) {
1928 if (fStatusLineManager
== null || null == fTimeProvider
||
1929 fTimeProvider
.getTime0() == fTimeProvider
.getTime1()) {
1932 StringBuilder message
= new StringBuilder();
1933 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1934 long time
= getTimeAtX(x
);
1936 message
.append("T: "); //$NON-NLS-1$
1937 message
.append(new TmfNanoTimestamp(time
).toString());
1938 message
.append(" T1: "); //$NON-NLS-1$
1939 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1940 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1941 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1942 message
.append(new TmfNanoTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1943 if (selectionBegin
!= selectionEnd
) {
1944 message
.append(" T2: "); //$NON-NLS-1$
1945 message
.append(new TmfNanoTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1946 message
.append(" \u0394: "); //$NON-NLS-1$
1947 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1950 @SuppressWarnings("deprecation")
1951 long selectedTime
= fTimeProvider
.getSelectedTime();
1952 message
.append(new TmfNanoTimestamp(selectedTime
));
1955 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1956 long time0
= fDragTime0
;
1957 long time
= getTimeAtX(fDragX
);
1958 message
.append("T1: "); //$NON-NLS-1$
1959 message
.append(new TmfNanoTimestamp(Math
.min(time
, time0
)).toString());
1960 if (time
!= time0
) {
1961 message
.append(" T2: "); //$NON-NLS-1$
1962 message
.append(new TmfNanoTimestamp(Math
.max(time
, time0
)).toString());
1963 message
.append(" \u0394: "); //$NON-NLS-1$
1964 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1967 fStatusLineManager
.setMessage(message
.toString());
1971 public void mouseMove(MouseEvent e
) {
1972 if (null == fTimeProvider
) {
1975 Point size
= getCtrlSize();
1976 if (DRAG_TRACE_ITEM
== fDragState
) {
1977 int nameWidth
= fTimeProvider
.getNameSpace();
1978 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1980 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1981 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1982 long time1
= fTime1bak
- timeDelta
;
1983 long maxTime
= fTimeProvider
.getMaxTime();
1984 if (time1
> maxTime
) {
1987 long time0
= time1
- (fTime1bak
- fTime0bak
);
1988 if (time0
< fTimeProvider
.getMinTime()) {
1989 time0
= fTimeProvider
.getMinTime();
1990 time1
= time0
+ (fTime1bak
- fTime0bak
);
1992 fTimeProvider
.setStartFinishTime(time0
, time1
);
1994 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1996 fTimeProvider
.setNameSpace(e
.x
);
1997 } else if (DRAG_SELECTION
== fDragState
) {
1998 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1999 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2001 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2003 } else if (DRAG_ZOOM
== fDragState
) {
2004 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
2006 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2007 } else if (DRAG_NONE
== fDragState
) {
2008 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2009 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2012 fMouseOverSplitLine
= mouseOverSplitLine
;
2014 updateCursor(e
.x
, e
.stateMask
);
2015 updateStatusLine(e
.x
);
2019 public void mouseDoubleClick(MouseEvent e
) {
2020 if (null == fTimeProvider
) {
2023 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
2024 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
2025 fTimeProvider
.setNameSpace(fIdealNameSpace
);
2026 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
2027 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
2030 fMouseOverSplitLine
= mouseOverSplitLine
;
2033 int idx
= getItemIndexAtY(e
.y
);
2035 selectItem(idx
, false);
2036 fireDefaultSelection();
2042 public void mouseDown(MouseEvent e
) {
2043 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
||
2044 fTimeProvider
.getTime0() == fTimeProvider
.getTime1() ||
2045 getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2049 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
2050 int nameSpace
= fTimeProvider
.getNameSpace();
2051 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
2052 fDragState
= DRAG_SPLIT_LINE
;
2053 fDragButton
= e
.button
;
2056 fTime0bak
= fTimeProvider
.getTime0();
2057 fTime1bak
= fTimeProvider
.getTime1();
2059 updateCursor(e
.x
, e
.stateMask
);
2063 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2064 int nameSpace
= fTimeProvider
.getNameSpace();
2065 idx
= getItemIndexAtY(e
.y
);
2067 Item item
= fItemData
.fExpandedItems
[idx
];
2068 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2072 selectItem(idx
, false);
2073 fireSelectionChanged();
2075 selectItem(idx
, false); // clear selection
2076 fireSelectionChanged();
2078 long hitTime
= getTimeAtX(e
.x
);
2082 fDragState
= DRAG_SELECTION
;
2083 fDragButton
= e
.button
;
2086 fDragTime0
= getTimeAtX(fDragX0
);
2087 if (fTimeProvider
instanceof ITimeDataProvider2
) {
2088 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
2089 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
2090 int xBegin
= getXForTime(selectionBegin
);
2091 int xEnd
= getXForTime(selectionEnd
);
2092 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2093 long time
= getTimeAtX(e
.x
);
2094 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2096 fDragTime0
= selectionEnd
;
2099 fDragTime0
= selectionBegin
;
2102 long time
= getTimeAtX(e
.x
);
2103 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2105 fDragTime0
= selectionEnd
;
2106 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2108 fDragTime0
= selectionBegin
;
2112 fTime0bak
= fTimeProvider
.getTime0();
2113 fTime1bak
= fTimeProvider
.getTime1();
2115 updateCursor(e
.x
, e
.stateMask
);
2116 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2118 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2119 long hitTime
= getTimeAtX(e
.x
);
2122 fDragState
= DRAG_TRACE_ITEM
;
2123 fDragButton
= e
.button
;
2126 fTime0bak
= fTimeProvider
.getTime0();
2127 fTime1bak
= fTimeProvider
.getTime1();
2128 updateCursor(e
.x
, e
.stateMask
);
2130 } else if (3 == e
.button
) {
2132 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2134 fDragState
= DRAG_ZOOM
;
2135 fDragButton
= e
.button
;
2137 updateCursor(e
.x
, e
.stateMask
);
2138 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2143 public void mouseUp(MouseEvent e
) {
2144 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2145 menuDetected(fPendingMenuDetectEvent
);
2147 if (DRAG_NONE
!= fDragState
) {
2149 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2150 if (fDragX
!= fDragX0
) {
2151 fTimeProvider
.notifyStartFinishTime();
2153 fDragState
= DRAG_NONE
;
2154 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2155 fDragState
= DRAG_NONE
;
2157 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2158 if (fDragX
== fDragX0
) { // click without selecting anything
2159 long time
= getTimeAtX(e
.x
);
2160 fTimeProvider
.setSelectedTimeNotify(time
, false);
2162 long time0
= fDragTime0
;
2163 long time1
= getTimeAtX(fDragX
);
2164 if (time0
<= time1
) {
2165 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time0
, time1
);
2167 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time1
, time0
);
2170 fDragState
= DRAG_NONE
;
2172 fTimeGraphScale
.setDragRange(-1, -1);
2173 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2174 int nameWidth
= fTimeProvider
.getNameSpace();
2175 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2176 long time0
= getTimeAtX(fDragX0
);
2177 long time1
= getTimeAtX(fDragX
);
2178 if (time0
< time1
) {
2179 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2181 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2186 fDragState
= DRAG_NONE
;
2187 fTimeGraphScale
.setDragRange(-1, -1);
2190 updateCursor(e
.x
, e
.stateMask
);
2191 updateStatusLine(e
.x
);
2195 public void mouseEnter(MouseEvent e
) {
2199 public void mouseExit(MouseEvent e
) {
2200 if (fMouseOverSplitLine
) {
2201 fMouseOverSplitLine
= false;
2204 updateStatusLine(NO_STATUS
);
2208 public void mouseHover(MouseEvent e
) {
2212 public void mouseScrolled(MouseEvent e
) {
2213 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2216 boolean zoomScroll
= false;
2217 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2218 Point parentSize
= getParent().getSize();
2219 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2220 // over the parent control
2221 if (e
.x
> getCtrlSize().x
) {
2222 // over the horizontal scroll bar
2224 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2225 // over the name space
2231 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2234 } else if (e
.count
< 0) {
2238 setTopIndex(getTopIndex() - e
.count
);
2243 public void controlMoved(ControlEvent e
) {
2247 public void controlResized(ControlEvent e
) {
2252 public void widgetDefaultSelected(SelectionEvent e
) {
2256 public void widgetSelected(SelectionEvent e
) {
2257 if (e
.widget
== getVerticalBar()) {
2258 setTopIndex(getVerticalBar().getSelection());
2259 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2260 int start
= getHorizontalBar().getSelection();
2261 long time0
= fTimeProvider
.getTime0();
2262 long time1
= fTimeProvider
.getTime1();
2263 long timeMin
= fTimeProvider
.getMinTime();
2264 long timeMax
= fTimeProvider
.getMaxTime();
2265 long delta
= timeMax
- timeMin
;
2267 long range
= time1
- time0
;
2268 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2269 time1
= time0
+ range
;
2271 // TODO: Follow-up with Bug 310310
2272 // In Linux SWT.DRAG is the only value received
2273 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2274 if (e
.detail
== SWT
.DRAG
) {
2275 fTimeProvider
.setStartFinishTime(time0
, time1
);
2277 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2283 public int getBorderWidth() {
2284 return fBorderWidth
;
2288 * Set the border width
2290 * @param borderWidth
2293 public void setBorderWidth(int borderWidth
) {
2294 this.fBorderWidth
= borderWidth
;
2298 * @return The current height of the header row
2300 public int getHeaderHeight() {
2301 return fHeaderHeight
;
2305 * Set the height of the header row
2307 * @param headerHeight
2310 public void setHeaderHeight(int headerHeight
) {
2311 this.fHeaderHeight
= headerHeight
;
2315 * @return The default height of regular item rows
2317 public int getItemHeight() {
2318 return fGlobalItemHeight
;
2322 * Set the default height of regular item rows.
2327 public void setItemHeight(int rowHeight
) {
2328 this.fGlobalItemHeight
= rowHeight
;
2332 * Set the height of a specific item. Overrides the default item height.
2335 * A time graph entry
2338 * @return true if the height is successfully stored, false otherwise
2342 public boolean setItemHeight(ITimeGraphEntry entry
, int rowHeight
) {
2343 Item item
= fItemData
.findItem(entry
);
2345 item
.fItemHeight
= rowHeight
;
2352 * Set the minimum item width
2354 * @param width The minimum width
2356 public void setMinimumItemWidth(int width
) {
2357 this.fMinimumItemWidth
= width
;
2361 * @return The minimum item width
2363 public int getMinimumItemWidth() {
2364 return fMinimumItemWidth
;
2368 * @return The entries that are currently filtered out
2372 public List
<ITimeGraphEntry
> getFilteredOut() {
2373 return fItemData
.getFilteredOut();
2377 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2378 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2379 fSelectionChangedListeners
.add(listener
);
2384 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2385 if (listener
!= null) {
2386 fSelectionChangedListeners
.remove(listener
);
2391 public void setSelection(ISelection selection
) {
2392 if (selection
instanceof TimeGraphSelection
) {
2393 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2394 Object ob
= sel
.getFirstElement();
2395 if (ob
instanceof ITimeGraphEntry
) {
2396 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2397 selectItem(trace
, false);
2404 * @param filter The filter object to be attached to the view
2407 public void addFilter(ViewerFilter filter
) {
2408 if (!fFilters
.contains(filter
)) {
2409 fFilters
.add(filter
);
2414 * @param filter The filter object to be attached to the view
2417 public void removeFilter(ViewerFilter filter
) {
2418 fFilters
.remove(filter
);
2421 private class ItemData
{
2422 private Item
[] fExpandedItems
= new Item
[0];
2423 private Item
[] fItems
= new Item
[0];
2424 private ITimeGraphEntry fTraces
[] = new ITimeGraphEntry
[0];
2425 private List
<ILinkEvent
> fLinks
= new ArrayList
<ILinkEvent
>();
2426 private boolean fTraceFilter
[] = new boolean[0];
2427 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<ITimeGraphEntry
>();
2432 Item
findItem(ITimeGraphEntry entry
) {
2433 if (entry
== null) {
2437 for (int i
= 0; i
< fItems
.length
; i
++) {
2438 Item item
= fItems
[i
];
2439 if (item
.fTrace
== entry
) {
2447 int findItemIndex(ITimeGraphEntry trace
) {
2448 if (trace
== null) {
2452 for (int i
= 0; i
< fExpandedItems
.length
; i
++) {
2453 Item item
= fExpandedItems
[i
];
2454 if (item
.fTrace
== trace
) {
2462 public void refreshData() {
2463 List
<Item
> itemList
= new ArrayList
<Item
>();
2464 fFilteredOut
.clear();
2465 ITimeGraphEntry selection
= getSelectedTrace();
2466 for (int i
= 0; i
< fTraces
.length
; i
++) {
2467 ITimeGraphEntry entry
= fTraces
[i
];
2468 refreshData(itemList
, null, 0, entry
);
2470 fItems
= itemList
.toArray(new Item
[0]);
2471 updateExpandedItems();
2472 if (selection
!= null) {
2473 for (Item item
: fExpandedItems
) {
2474 if (item
.fTrace
== selection
) {
2475 item
.fSelected
= true;
2482 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2483 Item item
= new Item(entry
, entry
.getName(), level
);
2484 if (parent
!= null) {
2485 parent
.fChildren
.add(item
);
2487 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
2488 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2490 item
.fItemHeight
= fGlobalItemHeight
;
2493 if (entry
.hasChildren()) {
2494 item
.fExpanded
= true;
2495 item
.fHasChildren
= true;
2496 for (ITimeGraphEntry child
: entry
.getChildren()) {
2497 refreshData(itemList
, item
, level
+ 1, child
);
2502 public void updateExpandedItems() {
2503 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2504 for (int i
= 0; i
< fTraces
.length
; i
++) {
2505 ITimeGraphEntry entry
= fTraces
[i
];
2506 Item item
= findItem(entry
);
2507 refreshExpanded(expandedItemList
, item
);
2509 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2510 fTopIndex
= Math
.min(fTopIndex
, Math
.max(0, fExpandedItems
.length
- 1));
2513 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2514 // Check for filters
2515 boolean display
= true;
2516 for (ViewerFilter filter
: fFilters
) {
2517 if (!filter
.select(null, item
.fTrace
.getParent(), item
.fTrace
)) {
2523 expandedItemList
.add(item
);
2524 if (item
.fHasChildren
&& item
.fExpanded
) {
2525 for (Item child
: item
.fChildren
) {
2526 refreshExpanded(expandedItemList
, child
);
2532 public void refreshData(ITimeGraphEntry traces
[]) {
2533 if (traces
== null) {
2534 fTraceFilter
= null;
2537 if (traces
.length
== 0) {
2538 fTraceFilter
= null;
2539 } else if (fTraceFilter
== null || traces
.length
!= fTraceFilter
.length
) {
2540 fTraceFilter
= new boolean[traces
.length
];
2541 java
.util
.Arrays
.fill(fTraceFilter
, true);
2543 fTraces
= Arrays
.copyOf(traces
, traces
.length
);
2549 public void refreshArrows(List
<ILinkEvent
> events
) {
2550 /* If links are null, reset the list */
2551 if (events
!= null) {
2554 fLinks
= new ArrayList
<ILinkEvent
>();
2558 public ITimeGraphEntry
[] getTraces() {
2562 public boolean[] getTraceFilter() {
2563 return fTraceFilter
;
2566 public List
<ITimeGraphEntry
> getFilteredOut() {
2567 return fFilteredOut
;
2571 private class Item
{
2572 private boolean fExpanded
;
2573 private boolean fSelected
;
2574 private boolean fHasChildren
;
2575 private int fItemHeight
;
2577 private List
<Item
> fChildren
;
2578 private String fName
;
2579 private ITimeGraphEntry fTrace
;
2581 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2582 this.fTrace
= trace
;
2584 this.fLevel
= level
;
2585 this.fChildren
= new ArrayList
<Item
>();
2589 public String
toString() {
2598 public void menuDetected(MenuDetectEvent e
) {
2599 if (null == fTimeProvider
) {
2602 if (e
.detail
== SWT
.MENU_MOUSE
) {
2603 if (fPendingMenuDetectEvent
== null) {
2604 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2605 * Store the event and trigger it later just before handling mouseUp.
2606 * This allows for the method to detect if mouse is used to drag zoom.
2608 fPendingMenuDetectEvent
= e
;
2611 fPendingMenuDetectEvent
= null;
2612 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2616 if (fDragState
!= DRAG_NONE
) {
2620 Point p
= toControl(e
.x
, e
.y
);
2621 int idx
= getItemIndexAtY(p
.y
);
2622 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2623 Item item
= fItemData
.fExpandedItems
[idx
];
2624 ITimeGraphEntry entry
= item
.fTrace
;
2625 if (entry
.hasTimeEvents()) {
2626 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2627 if (event
!= null) {
2629 fireMenuEventOnTimeEvent(e
);
2634 fireMenuEventOnTimeGraphEntry(e
);