1 /*****************************************************************************
2 * Copyright (c) 2007, 2013 Intel Corporation, Ericsson.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchez-Leon - Updated for TMF
12 * Patrick Tasse - Refactoring
13 *****************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.Vector
;
22 import org
.eclipse
.jface
.resource
.JFaceResources
;
23 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
24 import org
.eclipse
.jface
.viewers
.ISelection
;
25 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
26 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
27 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.ControlEvent
;
36 import org
.eclipse
.swt
.events
.ControlListener
;
37 import org
.eclipse
.swt
.events
.FocusEvent
;
38 import org
.eclipse
.swt
.events
.FocusListener
;
39 import org
.eclipse
.swt
.events
.KeyEvent
;
40 import org
.eclipse
.swt
.events
.KeyListener
;
41 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
42 import org
.eclipse
.swt
.events
.MenuDetectListener
;
43 import org
.eclipse
.swt
.events
.MouseEvent
;
44 import org
.eclipse
.swt
.events
.MouseListener
;
45 import org
.eclipse
.swt
.events
.MouseMoveListener
;
46 import org
.eclipse
.swt
.events
.MouseTrackListener
;
47 import org
.eclipse
.swt
.events
.MouseWheelListener
;
48 import org
.eclipse
.swt
.events
.PaintEvent
;
49 import org
.eclipse
.swt
.events
.SelectionEvent
;
50 import org
.eclipse
.swt
.events
.SelectionListener
;
51 import org
.eclipse
.swt
.events
.TraverseEvent
;
52 import org
.eclipse
.swt
.events
.TraverseListener
;
53 import org
.eclipse
.swt
.events
.TypedEvent
;
54 import org
.eclipse
.swt
.graphics
.Color
;
55 import org
.eclipse
.swt
.graphics
.Cursor
;
56 import org
.eclipse
.swt
.graphics
.GC
;
57 import org
.eclipse
.swt
.graphics
.Image
;
58 import org
.eclipse
.swt
.graphics
.Point
;
59 import org
.eclipse
.swt
.graphics
.Rectangle
;
60 import org
.eclipse
.swt
.widgets
.Composite
;
61 import org
.eclipse
.swt
.widgets
.Display
;
62 import org
.eclipse
.swt
.widgets
.Event
;
63 import org
.eclipse
.swt
.widgets
.Listener
;
64 import org
.eclipse
.swt
.widgets
.ScrollBar
;
67 * Time graph control implementation
70 * @author Alvaro Sanchez-Leon
71 * @author Patrick Tasse
73 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
, MenuDetectListener
{
75 /** Max scrollbar size */
76 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
78 /** Resource manager */
79 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
81 /** Color map for event types */
82 protected Color
[] fEventColorMap
= null;
84 private static final int DRAG_NONE
= 0;
85 private static final int DRAG_TRACE_ITEM
= 1;
86 private static final int DRAG_SPLIT_LINE
= 2;
87 private static final int DRAG_ZOOM
= 3;
89 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
91 private static final double zoomCoeff
= 1.5;
93 private ITimeDataProvider _timeProvider
;
94 private boolean _isInFocus
= false;
95 private boolean _isDragCursor3
= false;
96 private boolean _isWaitCursor
= true;
97 private boolean _mouseOverSplitLine
= false;
98 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
99 private int _minimumItemWidth
= 0;
100 private int _topIndex
= 0;
101 private int _dragState
= DRAG_NONE
;
102 private int _dragX0
= 0;
103 private int _dragX
= 0;
104 private int _idealNameSpace
= 0;
105 // private double _timeStep = 10000000;
106 private long _time0bak
;
107 private long _time1bak
;
108 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
109 private ItemData _data
= null;
110 private List
<SelectionListener
> _selectionListeners
;
111 private final List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
112 private final List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
113 private final List
<MenuDetectListener
> _timeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
114 private final List
<MenuDetectListener
> _timeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
115 private final Cursor _dragCursor3
;
116 private final Cursor _WaitCursor
;
117 private final List
<ViewerFilter
> _filters
= new ArrayList
<ViewerFilter
>();
119 // Vertical formatting formatting for the state control view
120 private final boolean _visibleVerticalScroll
= true;
121 private int _borderWidth
= 0;
122 private int _headerHeight
= 0;
124 private Listener mouseScrollFilterListener
;
126 private MouseScrollNotifier fMouseScrollNotifier
;
127 private final Object fMouseScrollNotifierLock
= new Object();
128 private class MouseScrollNotifier
extends Thread
{
129 private final static long DELAY
= 400L;
130 private final static long POLLING_INTERVAL
= 10L;
131 private long fLastScrollTime
= Long
.MAX_VALUE
;
135 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
137 Thread
.sleep(POLLING_INTERVAL
);
138 } catch (Exception e
) {
142 if (!isInterrupted()) {
143 Display
.getDefault().asyncExec(new Runnable() {
149 _timeProvider
.notifyStartFinishTime();
153 synchronized (fMouseScrollNotifierLock
) {
154 fMouseScrollNotifier
= null;
158 public void mouseScrolled() {
159 fLastScrollTime
= System
.currentTimeMillis();
164 * Standard constructor
167 * The parent composite object
169 * The color scheme to use
171 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
173 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
175 _data
= new ItemData();
177 addFocusListener(this);
178 addMouseListener(this);
179 addMouseMoveListener(this);
180 addMouseTrackListener(this);
181 addMouseWheelListener(this);
182 addTraverseListener(this);
183 addKeyListener(this);
184 addControlListener(this);
185 addMenuDetectListener(this);
186 ScrollBar scrollHor
= getHorizontalBar();
188 if (scrollHor
!= null) {
189 scrollHor
.addSelectionListener(this);
192 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
193 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
197 public void dispose() {
199 _dragCursor3
.dispose();
200 _WaitCursor
.dispose();
201 fResourceManager
.dispose();
205 * Sets the timegraph provider used by this timegraph viewer.
207 * @param timeGraphProvider the timegraph provider
209 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
210 fTimeGraphProvider
= timeGraphProvider
;
211 _data
.provider
= timeGraphProvider
;
213 if (fEventColorMap
!= null) {
214 for (Color color
: fEventColorMap
) {
215 fResourceManager
.destroyColor(color
.getRGB());
218 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
219 if (stateItems
!= null) {
220 fEventColorMap
= new Color
[stateItems
.length
];
221 for (int i
= 0; i
< stateItems
.length
; i
++) {
222 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
225 fEventColorMap
= new Color
[] { };
230 * Assign the given time provider
232 * @param timeProvider
235 public void setTimeProvider(ITimeDataProvider timeProvider
) {
236 _timeProvider
= timeProvider
;
242 * Add a selection listener
245 * The listener to add
247 public void addSelectionListener(SelectionListener listener
) {
248 if (listener
== null) {
249 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
251 if (null == _selectionListeners
) {
252 _selectionListeners
= new ArrayList
<SelectionListener
>();
254 _selectionListeners
.add(listener
);
258 * Remove a selection listener
261 * The listener to remove
263 public void removeSelectionListener(SelectionListener listener
) {
264 if (null != _selectionListeners
) {
265 _selectionListeners
.remove(listener
);
270 * Selection changed callback
272 public void fireSelectionChanged() {
273 if (null != _selectionListeners
) {
274 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
275 while (it
.hasNext()) {
276 SelectionListener listener
= it
.next();
277 listener
.widgetSelected(null);
283 * Default selection callback
285 public void fireDefaultSelection() {
286 if (null != _selectionListeners
) {
287 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
288 while (it
.hasNext()) {
289 SelectionListener listener
= it
.next();
290 listener
.widgetDefaultSelected(null);
296 * Get the traces in the model
298 * @return The array of traces
300 public ITimeGraphEntry
[] getTraces() {
301 return _data
.getTraces();
305 * Get the on/off trace filters
307 * @return The array of filters
309 public boolean[] getTraceFilter() {
310 return _data
.getTraceFilter();
314 * Refresh the data for the thing
316 public void refreshData() {
323 * Refresh data for the given traces
326 * The traces to refresh
328 public void refreshData(ITimeGraphEntry
[] traces
) {
329 _data
.refreshData(traces
);
335 * Adjust the scoll bars
337 public void adjustScrolls() {
338 if (null == _timeProvider
) {
339 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
345 long time0
= _timeProvider
.getTime0();
346 long time1
= _timeProvider
.getTime1();
348 long timeMin
= _timeProvider
.getMinTime();
349 long timeMax
= _timeProvider
.getMaxTime();
351 long delta
= timeMax
- timeMin
;
354 int thumb
= H_SCROLLBAR_MAX
;
357 // Thumb size (page size)
358 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
359 // At the beginning of visible window
360 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
363 // position, minimum, maximum, thumb size, increment (half page)t, page
364 // increment size (full page)
365 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
368 boolean ensureVisibleItem(int idx
, boolean redraw
) {
369 boolean changed
= false;
372 for (index
= 0; index
< _data
._expandedItems
.length
; index
++) {
373 if (_data
._expandedItems
[index
]._selected
) {
378 if (index
>= _data
._expandedItems
.length
) {
381 if (index
< _topIndex
) {
383 //FIXME:getVerticalBar().setSelection(_topItem);
389 int page
= countPerPage();
390 if (index
>= _topIndex
+ page
) {
391 setTopIndex(index
- page
+ 1);
392 //FIXME:getVerticalBar().setSelection(_topItem);
403 * Assign the given index as the top one
408 public void setTopIndex(int idx
) {
409 int index
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
410 index
= Math
.max(0, index
);
416 * Set the expanded state of a given entry
421 * True if expanded, false if collapsed
423 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
424 Item item
= _data
.findItem(entry
);
425 if (item
!= null && item
._expanded
!= expanded
) {
426 item
._expanded
= expanded
;
427 _data
.updateExpandedItems();
433 * Collapses all nodes of the viewer's tree, starting with the root.
437 public void collapseAll() {
438 for (Item item
: _data
._items
) {
439 item
._expanded
= false;
441 _data
.updateExpandedItems();
446 * Expands all nodes of the viewer's tree, starting with the root.
450 public void expandAll() {
451 for (Item item
: _data
._items
) {
452 item
._expanded
= true;
454 _data
.updateExpandedItems();
459 * Add a tree listener
462 * The listener to add
464 public void addTreeListener(ITimeGraphTreeListener listener
) {
465 if (!_treeListeners
.contains(listener
)) {
466 _treeListeners
.add(listener
);
471 * Remove a tree listener
474 * The listener to remove
476 public void removeTreeListener(ITimeGraphTreeListener listener
) {
477 if (_treeListeners
.contains(listener
)) {
478 _treeListeners
.remove(listener
);
483 * Tree event callback
488 * The expanded state (true for expanded, false for collapsed)
490 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
491 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
492 for (ITimeGraphTreeListener listener
: _treeListeners
) {
494 listener
.treeExpanded(event
);
496 listener
.treeCollapsed(event
);
502 * Add a menu listener on {@link ITimeGraphEntry}s
504 * The listener to add
507 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
508 if (!_timeGraphEntryMenuListeners
.contains(listener
)) {
509 _timeGraphEntryMenuListeners
.add(listener
);
514 * Remove a menu listener on {@link ITimeGraphEntry}s
517 * The listener to remove
520 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
521 if (_timeGraphEntryMenuListeners
.contains(listener
)) {
522 _timeGraphEntryMenuListeners
.remove(listener
);
527 * Menu event callback on {@link ITimeGraphEntry}s
530 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
532 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
533 for (MenuDetectListener listener
: _timeGraphEntryMenuListeners
) {
534 listener
.menuDetected(event
);
539 * Add a menu listener on {@link ITimeEvent}s
542 * The listener to add
545 public void addTimeEventMenuListener(MenuDetectListener listener
) {
546 if (!_timeEventMenuListeners
.contains(listener
)) {
547 _timeEventMenuListeners
.add(listener
);
552 * Remove a menu listener on {@link ITimeEvent}s
555 * The listener to remove
558 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
559 if (_timeEventMenuListeners
.contains(listener
)) {
560 _timeEventMenuListeners
.remove(listener
);
565 * Menu event callback on {@link ITimeEvent}s
568 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
570 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
571 for (MenuDetectListener listener
: _timeEventMenuListeners
) {
572 listener
.menuDetected(event
);
577 public ISelection
getSelection() {
578 TimeGraphSelection sel
= new TimeGraphSelection();
579 ITimeGraphEntry trace
= getSelectedTrace();
580 if (null != trace
&& null != _timeProvider
) {
581 long selectedTime
= _timeProvider
.getSelectedTime();
582 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
593 * Get the selection object
595 * @return The selection
597 public ISelection
getSelectionTrace() {
598 TimeGraphSelection sel
= new TimeGraphSelection();
599 ITimeGraphEntry trace
= getSelectedTrace();
607 * Enable/disable one of the traces in the model
610 * 1 to enable it, -1 to disable. The method returns immediately
611 * if another value is used.
613 public void selectTrace(int n
) {
614 if ((n
!= 1) && (n
!= -1)) {
618 boolean changed
= false;
619 int lastSelection
= -1;
620 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
621 Item item
= _data
._expandedItems
[i
];
622 if (item
._selected
) {
624 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
625 item
._selected
= false;
626 item
= _data
._expandedItems
[i
+ 1];
627 item
._selected
= true;
629 } else if ((-1 == n
) && (i
> 0)) {
630 item
._selected
= false;
631 item
= _data
._expandedItems
[i
- 1];
632 item
._selected
= true;
639 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
640 Item item
= _data
._expandedItems
[0];
641 item
._selected
= true;
646 ensureVisibleItem(-1, false);
648 fireSelectionChanged();
656 * 1 for next event, -1 for previous event
658 public void selectEvent(int n
) {
659 if (null == _timeProvider
) {
662 ITimeGraphEntry trace
= getSelectedTrace();
666 long selectedTime
= _timeProvider
.getSelectedTime();
667 long endTime
= _timeProvider
.getEndTime();
668 ITimeEvent nextEvent
;
669 if (-1 == n
&& selectedTime
> endTime
) {
670 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
672 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
674 if (null == nextEvent
&& -1 == n
) {
675 nextEvent
= Utils
.getFirstEvent(trace
);
677 if (null != nextEvent
) {
678 long nextTime
= nextEvent
.getTime();
679 // If last event detected e.g. going back or not moving to a next
681 if (nextTime
<= selectedTime
&& n
== 1) {
682 // Select to the end of this last event
683 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
684 // but not beyond the end of the trace
685 if (nextTime
> endTime
) {
688 } else if (n
== -1) {
689 // for previous event go to its end time unless we were already there
690 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
691 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
694 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
695 fireSelectionChanged();
697 _timeProvider
.setSelectedTimeNotify(endTime
, true);
698 fireSelectionChanged();
703 * Select the next event
705 public void selectNextEvent() {
707 // Notify if visible time window has been adjusted
708 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
712 * Select the previous event
714 public void selectPrevEvent() {
716 // Notify if visible time window has been adjusted
717 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
721 * Select the next trace
723 public void selectNextTrace() {
728 * Select the previous trace
730 public void selectPrevTrace() {
735 * Zoom based on mouse cursor location with mouse scrolling
737 * @param zoomIn true to zoom in, false to zoom out
739 public void zoom(boolean zoomIn
) {
740 int globalX
= getDisplay().getCursorLocation().x
;
741 Point p
= toControl(globalX
, 0);
742 int nameSpace
= _timeProvider
.getNameSpace();
743 int timeSpace
= _timeProvider
.getTimeSpace();
744 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
745 long time0
= _timeProvider
.getTime0();
746 long time1
= _timeProvider
.getTime1();
747 long interval
= time1
- time0
;
750 } // to allow getting out of single point interval
753 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
755 newInterval
= (long) Math
.ceil(interval
* 1.25);
757 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
758 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
759 long newTime1
= newTime0
+ newInterval
;
760 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
761 synchronized (fMouseScrollNotifierLock
) {
762 if (fMouseScrollNotifier
== null) {
763 fMouseScrollNotifier
= new MouseScrollNotifier();
764 fMouseScrollNotifier
.start();
766 fMouseScrollNotifier
.mouseScrolled();
771 * zoom in using single click
773 public void zoomIn() {
774 long _time0
= _timeProvider
.getTime0();
775 long _time1
= _timeProvider
.getTime1();
776 long _range
= _time1
- _time0
;
777 long selTime
= _timeProvider
.getSelectedTime();
778 if (selTime
<= _time0
|| selTime
>= _time1
) {
779 selTime
= (_time0
+ _time1
) / 2;
781 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
782 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
784 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
786 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
787 // + time1 + " inaccuracy:" + inaccuracy);
789 if (inaccuracy
> 0 && inaccuracy
< 100) {
790 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
794 long m
= _timeProvider
.getMinTimeInterval();
795 if ((time1
- time0
) < m
) {
796 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
800 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
804 * zoom out using single click
806 public void zoomOut() {
807 long _time0
= _timeProvider
.getTime0();
808 long _time1
= _timeProvider
.getTime1();
809 long selTime
= _timeProvider
.getSelectedTime();
810 if (selTime
<= _time0
|| selTime
>= _time1
) {
811 selTime
= (_time0
+ _time1
) / 2;
813 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
814 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
816 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
817 if (inaccuracy
> 0 && inaccuracy
< 100) {
818 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
822 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
826 * Return the currently selected trace
828 * @return The entry matching the trace
830 public ITimeGraphEntry
getSelectedTrace() {
831 ITimeGraphEntry trace
= null;
832 int idx
= getSelectedIndex();
834 trace
= _data
._expandedItems
[idx
]._trace
;
840 * Retrieve the index of the currently selected item
844 public int getSelectedIndex() {
846 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
847 Item item
= _data
._expandedItems
[i
];
848 if (item
._selected
) {
856 boolean toggle(int idx
) {
857 boolean toggled
= false;
858 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
859 Item item
= _data
._expandedItems
[idx
];
860 if (item
._hasChildren
) {
861 item
._expanded
= !item
._expanded
;
862 _data
.updateExpandedItems();
866 fireTreeEvent(item
._trace
, item
._expanded
);
872 int getItemIndexAtY(int y
) {
876 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
878 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
879 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
886 int idx
= y
/ _itemHeight
;
888 if (idx
< _data
._expandedItems
.length
) {
894 boolean isOverSplitLine(int x
) {
895 if (x
< 0 || null == _timeProvider
) {
899 int nameWidth
= _timeProvider
.getNameSpace();
900 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
906 ITimeGraphEntry
getEntry(Point pt
) {
907 int idx
= getItemIndexAtY(pt
.y
);
908 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
912 * Return the x coordinate corresponding to a time
914 * @param time the time
915 * @return the x coordinate corresponding to the time
919 public int getXForTime(long time
) {
920 if (null == _timeProvider
) {
923 long time0
= _timeProvider
.getTime0();
924 long time1
= _timeProvider
.getTime1();
925 int width
= getCtrlSize().x
;
926 int nameSpace
= _timeProvider
.getNameSpace();
927 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
928 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
933 * Return the time corresponding to an x coordinate
935 * @param coord The X coordinate
936 * @return The time corresponding to the x coordinate
940 public long getTimeAtX(int coord
) {
941 if (null == _timeProvider
) {
945 Point size
= getCtrlSize();
946 long time0
= _timeProvider
.getTime0();
947 long time1
= _timeProvider
.getTime1();
948 int nameWidth
= _timeProvider
.getNameSpace();
949 final int x
= coord
- nameWidth
;
950 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
951 if (x
>= 0 && size
.x
>= nameWidth
) {
952 if (time1
- time0
> timeWidth
) {
953 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
954 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
956 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
957 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
963 void selectItem(int idx
, boolean addSelection
) {
964 boolean changed
= false;
966 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
967 Item item
= _data
._expandedItems
[idx
];
968 changed
= (item
._selected
== false);
969 item
._selected
= true;
972 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
973 Item item
= _data
._expandedItems
[i
];
974 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
977 item
._selected
= i
== idx
;
980 changed
|= ensureVisibleItem(idx
, true);
987 * Callback for item selection
990 * The entry matching the trace
991 * @param addSelection
992 * If the selection is added or removed
994 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
995 int idx
= _data
.findItemIndex(trace
);
996 selectItem(idx
, addSelection
);
1000 * Retrieve the number of entries shown per page.
1004 public int countPerPage() {
1005 int height
= getCtrlSize().y
;
1007 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1009 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
1010 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1011 if (ySum
>= height
) {
1016 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
1017 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1018 if (ySum
>= height
) {
1026 count
= height
/ _itemHeight
;
1032 * Get the index of the top element
1036 public int getTopIndex() {
1041 * Get the number of expanded items
1043 * @return The count of expanded items
1045 public int getExpandedElementCount() {
1046 return _data
._expandedItems
.length
;
1050 * Get an array of all expanded elements
1052 * @return The expanded elements
1054 public ITimeGraphEntry
[] getExpandedElements() {
1055 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1056 for (Item item
: _data
._expandedItems
) {
1057 elements
.add(item
._trace
);
1059 return elements
.toArray(new ITimeGraphEntry
[0]);
1062 Point
getCtrlSize() {
1063 Point size
= getSize();
1064 if (getHorizontalBar().isVisible()) {
1065 size
.y
-= getHorizontalBar().getSize().y
;
1070 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1072 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1073 int width
= nameWidth
;
1074 int height
= _itemHeight
;
1075 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1077 for (int i
= _topIndex
; i
< idx
; i
++) {
1078 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1081 height
= _data
._expandedItems
[idx
].itemHeight
;
1083 return new Rectangle(x
, y
, width
, height
);
1086 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1087 int x
= bound
.x
+ nameWidth
;
1088 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1089 int width
= bound
.width
- x
;
1090 int height
= _itemHeight
;
1091 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1093 for (int i
= _topIndex
; i
< idx
; i
++) {
1094 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1097 height
= _data
._expandedItems
[idx
].itemHeight
;
1099 return new Rectangle(x
, y
, width
, height
);
1103 void paint(Rectangle bounds
, PaintEvent e
) {
1105 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
1106 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1108 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
1112 _idealNameSpace
= 0;
1113 int nameSpace
= _timeProvider
.getNameSpace();
1115 // draw empty name space background
1116 gc
.setBackground(_colors
.getBkColor(false, false, true));
1117 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1119 if (_dragState
== DRAG_ZOOM
) {
1120 // draw selected zoom region background
1121 gc
.setBackground(_colors
.getBkColor(false, false, true));
1122 if (_dragX0
< _dragX
) {
1123 gc
.fillRectangle(new Rectangle(_dragX0
, bounds
.y
, _dragX
- _dragX0
, bounds
.height
));
1124 } else if (_dragX0
> _dragX
) {
1125 gc
.fillRectangle(new Rectangle(_dragX
, bounds
.y
, _dragX0
- _dragX
, bounds
.height
));
1129 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1131 // draw selected time
1132 long time0
= _timeProvider
.getTime0();
1133 long time1
= _timeProvider
.getTime1();
1134 long selectedTime
= _timeProvider
.getSelectedTime();
1135 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1136 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1137 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1138 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1139 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1142 // draw drag line, no line if name space is 0.
1143 if (DRAG_SPLIT_LINE
== _dragState
) {
1144 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1145 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1146 } else if (DRAG_ZOOM
== _dragState
&& Math
.max(_dragX
, _dragX0
) > nameSpace
) {
1147 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1148 gc
.drawLine(_dragX0
, bounds
.y
, _dragX0
, bounds
.y
+ bounds
.height
- 1);
1149 gc
.drawLine(_dragX
, bounds
.y
, _dragX
, bounds
.y
+ bounds
.height
- 1);
1150 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1151 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1152 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1157 * Draw many items at once
1160 * The rectangle of the area
1161 * @param timeProvider
1164 * The array items to draw
1166 * The index of the first element to draw
1168 * The width reserved for the names
1170 * Reference to the SWT GC object
1172 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1173 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1174 for (int i
= topIndex
; i
< items
.length
; i
++) {
1175 Item item
= items
[i
];
1176 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1178 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1184 * @param item the item to draw
1185 * @param bounds the container rectangle
1186 * @param timeProvider Time provider
1187 * @param i the item index
1188 * @param nameSpace the name space
1189 * @param gc Graphics context
1191 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1192 ITimeGraphEntry entry
= item
._trace
;
1193 long time0
= timeProvider
.getTime0();
1194 long time1
= timeProvider
.getTime1();
1195 long selectedTime
= timeProvider
.getSelectedTime();
1197 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1198 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1202 if (! item
._trace
.hasTimeEvents()) {
1203 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1204 nameRect
.width
+= statesRect
.width
;
1205 drawName(item
, nameRect
, gc
);
1207 drawName(item
, nameRect
, gc
);
1209 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1210 if (rect
.isEmpty()) {
1211 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1214 if (time1
<= time0
) {
1215 gc
.setBackground(_colors
.getBkColor(false, false, false));
1216 gc
.fillRectangle(rect
);
1217 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1221 // Initialize _rect1 to same values as enclosing rectangle rect
1222 Rectangle stateRect
= Utils
.clone(rect
);
1223 boolean selected
= item
._selected
;
1224 // K pixels per second
1225 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1227 if (item
._trace
.hasTimeEvents()) {
1228 fillSpace(rect
, gc
, selected
);
1229 // Drawing rectangle is smaller than reserved space
1231 stateRect
.height
-= 6;
1233 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1234 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1237 while (iterator
.hasNext()) {
1238 ITimeEvent event
= iterator
.next();
1239 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1240 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1241 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1242 // event is out of bounds
1245 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1246 stateRect
.x
= Math
.max(rect
.x
, x
);
1247 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1248 if (stateRect
.x
== lastX
) {
1249 stateRect
.width
-= 1;
1250 if (stateRect
.width
> 0) {
1251 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1252 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1256 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1257 if (drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
)) {
1262 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1266 * Draw the name of an item.
1271 * Where to draw the name
1275 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1276 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1277 if (! hasTimeEvents
) {
1278 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1279 gc
.fillRectangle(bounds
);
1280 if (item
._selected
&& _isInFocus
) {
1281 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1282 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1285 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1286 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1287 gc
.fillRectangle(bounds
);
1290 // No name to be drawn
1291 if (_timeProvider
.getNameSpace() == 0) {
1295 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1296 if (item
._hasChildren
) {
1297 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1298 gc
.setBackground(_colors
.getBkColor(false, false, false));
1299 Rectangle rect
= Utils
.clone(bounds
);
1300 rect
.x
+= leftMargin
;
1301 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1302 rect
.width
= EXPAND_SIZE
;
1303 rect
.height
= EXPAND_SIZE
;
1304 gc
.fillRectangle(rect
);
1305 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1306 int midy
= rect
.y
+ rect
.height
/ 2;
1307 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1308 if (!item
._expanded
) {
1309 int midx
= rect
.x
+ rect
.width
/ 2;
1310 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1313 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1315 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1318 int imgHeight
= img
.getImageData().height
;
1319 int imgWidth
= img
.getImageData().width
;
1321 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1322 gc
.drawImage(img
, x
, y
);
1323 leftMargin
+= imgWidth
+ MARGIN
;
1325 String name
= item
._name
;
1326 Point size
= gc
.stringExtent(name
);
1327 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1328 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1330 if (hasTimeEvents
) {
1331 // cut long string with "..."
1332 int width
= bounds
.width
- leftMargin
;
1334 while (size
.x
> width
&& name
.length() > 1) {
1336 name
= name
.substring(0, name
.length() - 1);
1337 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1340 name
+= "..."; //$NON-NLS-1$
1343 Rectangle rect
= Utils
.clone(bounds
);
1344 rect
.x
+= leftMargin
;
1345 rect
.width
-= leftMargin
;
1347 if (rect
.width
> 0) {
1348 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1349 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1350 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1351 leftMargin
+= textWidth
+ MARGIN
;
1354 if (hasTimeEvents
) {
1356 int x
= bounds
.x
+ leftMargin
;
1357 int width
= bounds
.width
- x
;
1358 int midy
= bounds
.y
+ bounds
.height
/ 2;
1359 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1360 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1366 * Draw the state (color fill)
1371 * Time event for which we're drawing the state
1377 * Is this time event currently selected (so it appears
1379 * @param timeSelected
1380 * Is the timestamp currently selected
1381 * @return true if the state was drawn
1384 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1385 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1387 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1388 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1391 boolean visible
= rect
.width
== 0 ?
false : true;
1394 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1395 // Only draw the top and bottom borders
1396 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1397 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1398 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1399 if (rect
.width
== 1) {
1400 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1401 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1405 Color stateColor
= null;
1406 if (colorIdx
< fEventColorMap
.length
) {
1407 stateColor
= fEventColorMap
[colorIdx
];
1409 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1412 boolean reallySelected
= timeSelected
&& selected
;
1413 if (reallySelected
) {
1414 // modify the color?
1416 // fill all rect area
1417 gc
.setBackground(stateColor
);
1418 gc
.fillRectangle(rect
);
1419 // get the border color?
1420 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1423 if (!reallySelected
) {
1424 // Draw the top and bottom borders i.e. no side borders
1425 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1426 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1429 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1430 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1432 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1437 * Fill the space between two contiguous time events
1444 * Is this time event selected or not
1446 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1447 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1448 gc
.fillRectangle(rect
);
1449 if (_dragState
== DRAG_ZOOM
) {
1450 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, true));
1451 if (_dragX0
< _dragX
) {
1452 gc
.fillRectangle(new Rectangle(_dragX0
, rect
.y
, _dragX
- _dragX0
, rect
.height
));
1453 } else if (_dragX0
> _dragX
) {
1454 gc
.fillRectangle(new Rectangle(_dragX
, rect
.y
, _dragX0
- _dragX
, rect
.height
));
1458 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1459 int midy
= rect
.y
+ rect
.height
/ 2;
1460 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1464 public void keyTraversed(TraverseEvent e
) {
1465 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1471 public void keyPressed(KeyEvent e
) {
1473 if (_data
._expandedItems
.length
== 0) {
1476 if (SWT
.HOME
== e
.keyCode
) {
1478 } else if (SWT
.END
== e
.keyCode
) {
1479 idx
= _data
._expandedItems
.length
- 1;
1480 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1481 idx
= getSelectedIndex();
1484 } else if (idx
< _data
._expandedItems
.length
- 1) {
1487 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1488 idx
= getSelectedIndex();
1491 } else if (idx
> 0) {
1494 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1496 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1498 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1499 int page
= countPerPage();
1500 idx
= getSelectedIndex();
1505 if (idx
>= _data
._expandedItems
.length
) {
1506 idx
= _data
._expandedItems
.length
- 1;
1508 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1509 int page
= countPerPage();
1510 idx
= getSelectedIndex();
1518 } else if (SWT
.CR
== e
.keyCode
) {
1519 idx
= getSelectedIndex();
1521 if (_data
._expandedItems
[idx
]._hasChildren
) {
1524 fireDefaultSelection();
1530 selectItem(idx
, false);
1531 fireSelectionChanged();
1536 public void keyReleased(KeyEvent e
) {
1540 public void focusGained(FocusEvent e
) {
1542 if (mouseScrollFilterListener
== null) {
1543 mouseScrollFilterListener
= new Listener() {
1544 // This filter is used to prevent horizontal scrolling of the view
1545 // when the mouse wheel is used to zoom
1547 public void handleEvent(Event event
) {
1551 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1557 public void focusLost(FocusEvent e
) {
1559 if (mouseScrollFilterListener
!= null) {
1560 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1561 mouseScrollFilterListener
= null;
1563 if (DRAG_NONE
!= _dragState
) {
1565 _dragState
= DRAG_NONE
;
1571 * @return If the current view is focused
1573 public boolean isInFocus() {
1578 * Provide the possibility to control the wait cursor externally e.g. data
1579 * requests in progress
1581 * @param waitInd Should we wait indefinitely?
1583 public void waitCursor(boolean waitInd
) {
1584 // Update cursor as indicated
1586 setCursor(_WaitCursor
);
1587 _isWaitCursor
= true;
1590 _isWaitCursor
= false;
1593 // Get ready for next mouse move
1594 _isDragCursor3
= false;
1599 * If the x, y position is over the vertical split line (name to time
1600 * ranges), then change the cursor to a drag cursor to indicate the user the
1601 * possibility of resizing
1607 void updateCursor(int x
, int y
) {
1608 // if Wait cursor not active, check for the need to change to a drag
1610 if (_isWaitCursor
== false) {
1611 boolean isSplitLine
= isOverSplitLine(x
);
1612 // No dragcursor is name space is fixed to zero
1613 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1614 setCursor(_dragCursor3
);
1615 _isDragCursor3
= true;
1616 } else if (!isSplitLine
&& _isDragCursor3
) {
1618 _isDragCursor3
= false;
1624 public void mouseMove(MouseEvent e
) {
1625 if (null == _timeProvider
) {
1628 Point size
= getCtrlSize();
1629 if (DRAG_TRACE_ITEM
== _dragState
) {
1630 int nameWidth
= _timeProvider
.getNameSpace();
1631 int x
= e
.x
- nameWidth
;
1632 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1634 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1635 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1636 long time1
= _time1bak
- timeDelta
;
1637 long maxTime
= _timeProvider
.getMaxTime();
1638 if (time1
> maxTime
) {
1641 long time0
= time1
- (_time1bak
- _time0bak
);
1642 if (time0
< _timeProvider
.getMinTime()) {
1643 time0
= _timeProvider
.getMinTime();
1644 time1
= time0
+ (_time1bak
- _time0bak
);
1646 _timeProvider
.setStartFinishTime(time0
, time1
);
1648 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1650 _timeProvider
.setNameSpace(e
.x
);
1651 } else if (DRAG_ZOOM
== _dragState
) {
1652 _dragX
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1654 } else if (DRAG_NONE
== _dragState
) {
1655 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1656 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1659 _mouseOverSplitLine
= mouseOverSplitLine
;
1661 updateCursor(e
.x
, e
.y
);
1665 public void mouseDoubleClick(MouseEvent e
) {
1666 if (null == _timeProvider
) {
1669 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1670 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1671 _timeProvider
.setNameSpace(_idealNameSpace
);
1672 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1673 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1676 _mouseOverSplitLine
= mouseOverSplitLine
;
1679 int idx
= getItemIndexAtY(e
.y
);
1681 selectItem(idx
, false);
1682 fireDefaultSelection();
1688 public void mouseDown(MouseEvent e
) {
1689 if (_dragState
!= DRAG_NONE
|| null == _timeProvider
) {
1693 if (1 == e
.button
) {
1694 int nameSpace
= _timeProvider
.getNameSpace();
1695 if (nameSpace
!= 0) {
1696 if (isOverSplitLine(e
.x
)) {
1697 _dragState
= DRAG_SPLIT_LINE
;
1698 _dragX
= _dragX0
= e
.x
;
1699 _time0bak
= _timeProvider
.getTime0();
1700 _time1bak
= _timeProvider
.getTime1();
1706 idx
= getItemIndexAtY(e
.y
);
1708 Item item
= _data
._expandedItems
[idx
];
1709 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1712 long hitTime
= getTimeAtX(e
.x
);
1714 // _timeProvider.setSelectedTimeInt(hitTime, false);
1716 _dragState
= DRAG_TRACE_ITEM
;
1717 _dragX
= _dragX0
= e
.x
- nameSpace
;
1718 _time0bak
= _timeProvider
.getTime0();
1719 _time1bak
= _timeProvider
.getTime1();
1722 selectItem(idx
, false);
1723 fireSelectionChanged();
1725 selectItem(idx
, false); // clear selection
1727 fireSelectionChanged();
1729 } else if (3 == e
.button
) {
1730 if (_timeProvider
.getTime0() == _timeProvider
.getTime1() || getCtrlSize().x
- _timeProvider
.getNameSpace() <= 0) {
1734 _dragX
= _dragX0
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1735 _dragState
= DRAG_ZOOM
;
1740 public void mouseUp(MouseEvent e
) {
1741 if (DRAG_NONE
!= _dragState
) {
1743 if (e
.button
== 1 && DRAG_TRACE_ITEM
== _dragState
) {
1744 if (_dragX
== _dragX0
) { // click without drag
1745 long time
= getTimeAtX(e
.x
);
1746 _timeProvider
.setSelectedTimeNotify(time
, false);
1748 // Notify time provider to check the need for listener
1750 _timeProvider
.notifyStartFinishTime();
1752 _dragState
= DRAG_NONE
;
1753 } else if (e
.button
== 1 && DRAG_SPLIT_LINE
== _dragState
) {
1755 _dragState
= DRAG_NONE
;
1756 } else if (e
.button
== 3 && DRAG_ZOOM
== _dragState
) {
1757 int nameWidth
= _timeProvider
.getNameSpace();
1758 if (Math
.max(_dragX
, _dragX0
) > nameWidth
&& _dragX
!= _dragX0
) {
1759 long time0
= getTimeAtX(_dragX0
);
1760 long time1
= getTimeAtX(_dragX
);
1761 if (time0
< time1
) {
1762 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1764 _timeProvider
.setStartFinishTimeNotify(time1
, time0
);
1769 _dragState
= DRAG_NONE
;
1775 public void mouseEnter(MouseEvent e
) {
1779 public void mouseExit(MouseEvent e
) {
1780 if (_mouseOverSplitLine
) {
1781 _mouseOverSplitLine
= false;
1787 public void mouseHover(MouseEvent e
) {
1791 public void mouseScrolled(MouseEvent e
) {
1792 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1795 boolean zoomScroll
= false;
1796 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1797 Point parentSize
= getParent().getSize();
1798 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1799 // over the parent control
1800 if (e
.x
> getCtrlSize().x
) {
1801 // over the horizontal scroll bar
1803 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1804 // over the name space
1810 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1813 } else if (e
.count
< 0) {
1817 setTopIndex(getTopIndex() - e
.count
);
1822 public void controlMoved(ControlEvent e
) {
1826 public void controlResized(ControlEvent e
) {
1831 public void widgetDefaultSelected(SelectionEvent e
) {
1835 public void widgetSelected(SelectionEvent e
) {
1836 if (e
.widget
== getVerticalBar()) {
1837 setTopIndex(getVerticalBar().getSelection());
1838 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1839 int start
= getHorizontalBar().getSelection();
1840 long time0
= _timeProvider
.getTime0();
1841 long time1
= _timeProvider
.getTime1();
1842 long timeMin
= _timeProvider
.getMinTime();
1843 long timeMax
= _timeProvider
.getMaxTime();
1844 long delta
= timeMax
- timeMin
;
1846 long range
= time1
- time0
;
1847 // _timeRangeFixed = true;
1848 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1849 time1
= time0
+ range
;
1851 // TODO: Follow-up with Bug 310310
1852 // In Linux SWT.DRAG is the only value received
1853 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1854 if (e
.detail
== SWT
.DRAG
) {
1855 _timeProvider
.setStartFinishTime(time0
, time1
);
1857 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1863 * @return The current visibility of the vertical scroll bar
1865 public boolean isVisibleVerticalScroll() {
1866 return _visibleVerticalScroll
;
1870 public int getBorderWidth() {
1871 return _borderWidth
;
1875 * Set the border width
1877 * @param borderWidth
1880 public void setBorderWidth(int borderWidth
) {
1881 this._borderWidth
= borderWidth
;
1885 * @return The current height of the header row
1887 public int getHeaderHeight() {
1888 return _headerHeight
;
1892 * Set the height of the header row
1894 * @param headerHeight
1897 public void setHeaderHeight(int headerHeight
) {
1898 this._headerHeight
= headerHeight
;
1902 * @return The height of regular item rows
1904 public int getItemHeight() {
1909 * Set the height of regular itew rows
1914 public void setItemHeight(int rowHeight
) {
1915 this._itemHeight
= rowHeight
;
1919 * Set the minimum item width
1921 * @param width The minimum width
1923 public void setMinimumItemWidth(int width
) {
1924 this._minimumItemWidth
= width
;
1928 * @return The minimum item width
1930 public int getMinimumItemWidth() {
1931 return _minimumItemWidth
;
1935 * @return The entries that are currently filtered out
1937 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1938 return _data
.getFilteredOut();
1943 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1944 if (listener
!= null) {
1945 if (!_selectionChangedListeners
.contains(listener
)) {
1946 _selectionChangedListeners
.add(listener
);
1953 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1954 if (listener
!= null) {
1955 _selectionChangedListeners
.remove(listener
);
1961 public void setSelection(ISelection selection
) {
1962 if (selection
instanceof TimeGraphSelection
) {
1963 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1964 Object ob
= sel
.getFirstElement();
1965 if (ob
instanceof ITimeGraphEntry
) {
1966 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1967 selectItem(trace
, false);
1974 * @param filter The filter object to be attached to the view
1977 public void addFilter(ViewerFilter filter
) {
1978 if (!_filters
.contains(filter
)) {
1979 _filters
.add(filter
);
1984 * @param filter The filter object to be attached to the view
1987 public void removeFilter(ViewerFilter filter
) {
1988 _filters
.remove(filter
);
1991 private class ItemData
{
1992 public Item
[] _expandedItems
= new Item
[0];
1993 public Item
[] _items
= new Item
[0];
1994 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1995 private boolean traceFilter
[] = new boolean[0];
1996 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1997 public ITimeGraphPresentationProvider provider
;
2002 Item
findItem(ITimeGraphEntry entry
) {
2003 if (entry
== null) {
2007 for (int i
= 0; i
< _items
.length
; i
++) {
2008 Item item
= _items
[i
];
2009 if (item
._trace
== entry
) {
2017 int findItemIndex(ITimeGraphEntry trace
) {
2018 if (trace
== null) {
2022 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
2023 Item item
= _expandedItems
[i
];
2024 if (item
._trace
== trace
) {
2032 public void refreshData() {
2033 List
<Item
> itemList
= new ArrayList
<Item
>();
2034 filteredOut
.clear();
2035 for (int i
= 0; i
< _traces
.length
; i
++) {
2036 ITimeGraphEntry entry
= _traces
[i
];
2037 refreshData(itemList
, null, 0, entry
);
2039 _items
= itemList
.toArray(new Item
[0]);
2040 updateExpandedItems();
2043 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2044 Item item
= new Item(entry
, entry
.getName(), level
);
2045 if (parent
!= null) {
2046 parent
.children
.add(item
);
2048 item
.itemHeight
= provider
.getItemHeight(entry
);
2050 if (entry
.hasChildren()) {
2051 item
._expanded
= true;
2052 item
._hasChildren
= true;
2053 for (ITimeGraphEntry child
: entry
.getChildren()) {
2054 refreshData(itemList
, item
, level
+ 1, child
);
2059 public void updateExpandedItems() {
2060 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2061 for (int i
= 0; i
< _traces
.length
; i
++) {
2062 ITimeGraphEntry entry
= _traces
[i
];
2063 Item item
= findItem(entry
);
2064 refreshExpanded(expandedItemList
, item
);
2066 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
2069 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2070 // Check for filters
2071 boolean display
= true;
2072 for (ViewerFilter filter
: _filters
) {
2073 if (!filter
.select(null, item
._trace
.getParent(), item
._trace
)) {
2079 expandedItemList
.add(item
);
2080 if (item
._hasChildren
&& item
._expanded
) {
2081 for (Item child
: item
.children
) {
2082 refreshExpanded(expandedItemList
, child
);
2088 public void refreshData(ITimeGraphEntry traces
[]) {
2089 if (traces
== null || traces
.length
== 0) {
2091 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
2092 traceFilter
= new boolean[traces
.length
];
2093 java
.util
.Arrays
.fill(traceFilter
, true);
2100 public ITimeGraphEntry
[] getTraces() {
2104 public boolean[] getTraceFilter() {
2108 public Vector
<ITimeGraphEntry
> getFilteredOut() {
2113 private class Item
{
2114 public boolean _expanded
;
2115 public boolean _selected
;
2116 public boolean _hasChildren
;
2117 public int itemHeight
;
2119 public List
<Item
> children
;
2120 public String _name
;
2121 public ITimeGraphEntry _trace
;
2123 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2124 this._trace
= trace
;
2127 this.children
= new ArrayList
<Item
>();
2131 public String
toString() {
2140 public void menuDetected(MenuDetectEvent e
) {
2141 if (null == _timeProvider
) {
2144 Point p
= toControl(e
.x
, e
.y
);
2145 int idx
= getItemIndexAtY(p
.y
);
2146 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
2147 Item item
= _data
._expandedItems
[idx
];
2148 ITimeGraphEntry entry
= item
._trace
;
2149 if (entry
.hasTimeEvents()) {
2150 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2151 if (event
!= null) {
2153 fireMenuEventOnTimeEvent(e
);
2158 fireMenuEventOnTimeGraphEntry(e
);