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
>();
118 private MenuDetectEvent fPendingMenuDetectEvent
= null;
120 // Vertical formatting formatting for the state control view
121 private final boolean _visibleVerticalScroll
= true;
122 private int _borderWidth
= 0;
123 private int _headerHeight
= 0;
125 private Listener mouseScrollFilterListener
;
127 private MouseScrollNotifier fMouseScrollNotifier
;
128 private final Object fMouseScrollNotifierLock
= new Object();
129 private class MouseScrollNotifier
extends Thread
{
130 private final static long DELAY
= 400L;
131 private final static long POLLING_INTERVAL
= 10L;
132 private long fLastScrollTime
= Long
.MAX_VALUE
;
136 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
138 Thread
.sleep(POLLING_INTERVAL
);
139 } catch (Exception e
) {
143 if (!isInterrupted()) {
144 Display
.getDefault().asyncExec(new Runnable() {
150 _timeProvider
.notifyStartFinishTime();
154 synchronized (fMouseScrollNotifierLock
) {
155 fMouseScrollNotifier
= null;
159 public void mouseScrolled() {
160 fLastScrollTime
= System
.currentTimeMillis();
165 * Standard constructor
168 * The parent composite object
170 * The color scheme to use
172 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
174 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
176 _data
= new ItemData();
178 addFocusListener(this);
179 addMouseListener(this);
180 addMouseMoveListener(this);
181 addMouseTrackListener(this);
182 addMouseWheelListener(this);
183 addTraverseListener(this);
184 addKeyListener(this);
185 addControlListener(this);
186 addMenuDetectListener(this);
187 ScrollBar scrollHor
= getHorizontalBar();
189 if (scrollHor
!= null) {
190 scrollHor
.addSelectionListener(this);
193 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
194 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
198 public void dispose() {
200 _dragCursor3
.dispose();
201 _WaitCursor
.dispose();
202 fResourceManager
.dispose();
206 * Sets the timegraph provider used by this timegraph viewer.
208 * @param timeGraphProvider the timegraph provider
210 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
211 fTimeGraphProvider
= timeGraphProvider
;
212 _data
.provider
= timeGraphProvider
;
214 if (fEventColorMap
!= null) {
215 for (Color color
: fEventColorMap
) {
216 fResourceManager
.destroyColor(color
.getRGB());
219 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
220 if (stateItems
!= null) {
221 fEventColorMap
= new Color
[stateItems
.length
];
222 for (int i
= 0; i
< stateItems
.length
; i
++) {
223 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
226 fEventColorMap
= new Color
[] { };
231 * Assign the given time provider
233 * @param timeProvider
236 public void setTimeProvider(ITimeDataProvider timeProvider
) {
237 _timeProvider
= timeProvider
;
243 * Add a selection listener
246 * The listener to add
248 public void addSelectionListener(SelectionListener listener
) {
249 if (listener
== null) {
250 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
252 if (null == _selectionListeners
) {
253 _selectionListeners
= new ArrayList
<SelectionListener
>();
255 _selectionListeners
.add(listener
);
259 * Remove a selection listener
262 * The listener to remove
264 public void removeSelectionListener(SelectionListener listener
) {
265 if (null != _selectionListeners
) {
266 _selectionListeners
.remove(listener
);
271 * Selection changed callback
273 public void fireSelectionChanged() {
274 if (null != _selectionListeners
) {
275 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
276 while (it
.hasNext()) {
277 SelectionListener listener
= it
.next();
278 listener
.widgetSelected(null);
284 * Default selection callback
286 public void fireDefaultSelection() {
287 if (null != _selectionListeners
) {
288 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
289 while (it
.hasNext()) {
290 SelectionListener listener
= it
.next();
291 listener
.widgetDefaultSelected(null);
297 * Get the traces in the model
299 * @return The array of traces
301 public ITimeGraphEntry
[] getTraces() {
302 return _data
.getTraces();
306 * Get the on/off trace filters
308 * @return The array of filters
310 public boolean[] getTraceFilter() {
311 return _data
.getTraceFilter();
315 * Refresh the data for the thing
317 public void refreshData() {
324 * Refresh data for the given traces
327 * The traces to refresh
329 public void refreshData(ITimeGraphEntry
[] traces
) {
330 _data
.refreshData(traces
);
336 * Adjust the scoll bars
338 public void adjustScrolls() {
339 if (null == _timeProvider
) {
340 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
346 long time0
= _timeProvider
.getTime0();
347 long time1
= _timeProvider
.getTime1();
349 long timeMin
= _timeProvider
.getMinTime();
350 long timeMax
= _timeProvider
.getMaxTime();
352 long delta
= timeMax
- timeMin
;
355 int thumb
= H_SCROLLBAR_MAX
;
358 // Thumb size (page size)
359 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
360 // At the beginning of visible window
361 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
364 // position, minimum, maximum, thumb size, increment (half page)t, page
365 // increment size (full page)
366 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
369 boolean ensureVisibleItem(int idx
, boolean redraw
) {
370 boolean changed
= false;
373 for (index
= 0; index
< _data
._expandedItems
.length
; index
++) {
374 if (_data
._expandedItems
[index
]._selected
) {
379 if (index
>= _data
._expandedItems
.length
) {
382 if (index
< _topIndex
) {
384 //FIXME:getVerticalBar().setSelection(_topItem);
390 int page
= countPerPage();
391 if (index
>= _topIndex
+ page
) {
392 setTopIndex(index
- page
+ 1);
393 //FIXME:getVerticalBar().setSelection(_topItem);
404 * Assign the given index as the top one
409 public void setTopIndex(int idx
) {
410 int index
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
411 index
= Math
.max(0, index
);
417 * Set the expanded state of a given entry
422 * True if expanded, false if collapsed
424 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
425 Item item
= _data
.findItem(entry
);
426 if (item
!= null && item
._expanded
!= expanded
) {
427 item
._expanded
= expanded
;
428 _data
.updateExpandedItems();
434 * Collapses all nodes of the viewer's tree, starting with the root.
438 public void collapseAll() {
439 for (Item item
: _data
._items
) {
440 item
._expanded
= false;
442 _data
.updateExpandedItems();
447 * Expands all nodes of the viewer's tree, starting with the root.
451 public void expandAll() {
452 for (Item item
: _data
._items
) {
453 item
._expanded
= true;
455 _data
.updateExpandedItems();
460 * Add a tree listener
463 * The listener to add
465 public void addTreeListener(ITimeGraphTreeListener listener
) {
466 if (!_treeListeners
.contains(listener
)) {
467 _treeListeners
.add(listener
);
472 * Remove a tree listener
475 * The listener to remove
477 public void removeTreeListener(ITimeGraphTreeListener listener
) {
478 if (_treeListeners
.contains(listener
)) {
479 _treeListeners
.remove(listener
);
484 * Tree event callback
489 * The expanded state (true for expanded, false for collapsed)
491 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
492 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
493 for (ITimeGraphTreeListener listener
: _treeListeners
) {
495 listener
.treeExpanded(event
);
497 listener
.treeCollapsed(event
);
503 * Add a menu listener on {@link ITimeGraphEntry}s
505 * The listener to add
508 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
509 if (!_timeGraphEntryMenuListeners
.contains(listener
)) {
510 _timeGraphEntryMenuListeners
.add(listener
);
515 * Remove a menu listener on {@link ITimeGraphEntry}s
518 * The listener to remove
521 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
522 if (_timeGraphEntryMenuListeners
.contains(listener
)) {
523 _timeGraphEntryMenuListeners
.remove(listener
);
528 * Menu event callback on {@link ITimeGraphEntry}s
531 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
533 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
534 for (MenuDetectListener listener
: _timeGraphEntryMenuListeners
) {
535 listener
.menuDetected(event
);
540 * Add a menu listener on {@link ITimeEvent}s
543 * The listener to add
546 public void addTimeEventMenuListener(MenuDetectListener listener
) {
547 if (!_timeEventMenuListeners
.contains(listener
)) {
548 _timeEventMenuListeners
.add(listener
);
553 * Remove a menu listener on {@link ITimeEvent}s
556 * The listener to remove
559 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
560 if (_timeEventMenuListeners
.contains(listener
)) {
561 _timeEventMenuListeners
.remove(listener
);
566 * Menu event callback on {@link ITimeEvent}s
569 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
571 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
572 for (MenuDetectListener listener
: _timeEventMenuListeners
) {
573 listener
.menuDetected(event
);
578 public ISelection
getSelection() {
579 TimeGraphSelection sel
= new TimeGraphSelection();
580 ITimeGraphEntry trace
= getSelectedTrace();
581 if (null != trace
&& null != _timeProvider
) {
582 long selectedTime
= _timeProvider
.getSelectedTime();
583 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
594 * Get the selection object
596 * @return The selection
598 public ISelection
getSelectionTrace() {
599 TimeGraphSelection sel
= new TimeGraphSelection();
600 ITimeGraphEntry trace
= getSelectedTrace();
608 * Enable/disable one of the traces in the model
611 * 1 to enable it, -1 to disable. The method returns immediately
612 * if another value is used.
614 public void selectTrace(int n
) {
615 if ((n
!= 1) && (n
!= -1)) {
619 boolean changed
= false;
620 int lastSelection
= -1;
621 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
622 Item item
= _data
._expandedItems
[i
];
623 if (item
._selected
) {
625 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
626 item
._selected
= false;
627 item
= _data
._expandedItems
[i
+ 1];
628 item
._selected
= true;
630 } else if ((-1 == n
) && (i
> 0)) {
631 item
._selected
= false;
632 item
= _data
._expandedItems
[i
- 1];
633 item
._selected
= true;
640 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
641 Item item
= _data
._expandedItems
[0];
642 item
._selected
= true;
647 ensureVisibleItem(-1, false);
649 fireSelectionChanged();
657 * 1 for next event, -1 for previous event
659 public void selectEvent(int n
) {
660 if (null == _timeProvider
) {
663 ITimeGraphEntry trace
= getSelectedTrace();
667 long selectedTime
= _timeProvider
.getSelectedTime();
668 long endTime
= _timeProvider
.getEndTime();
669 ITimeEvent nextEvent
;
670 if (-1 == n
&& selectedTime
> endTime
) {
671 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
673 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
675 if (null == nextEvent
&& -1 == n
) {
676 nextEvent
= Utils
.getFirstEvent(trace
);
678 if (null != nextEvent
) {
679 long nextTime
= nextEvent
.getTime();
680 // If last event detected e.g. going back or not moving to a next
682 if (nextTime
<= selectedTime
&& n
== 1) {
683 // Select to the end of this last event
684 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
685 // but not beyond the end of the trace
686 if (nextTime
> endTime
) {
689 } else if (n
== -1) {
690 // for previous event go to its end time unless we were already there
691 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
692 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
695 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
696 fireSelectionChanged();
698 _timeProvider
.setSelectedTimeNotify(endTime
, true);
699 fireSelectionChanged();
704 * Select the next event
706 public void selectNextEvent() {
708 // Notify if visible time window has been adjusted
709 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
713 * Select the previous event
715 public void selectPrevEvent() {
717 // Notify if visible time window has been adjusted
718 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
722 * Select the next trace
724 public void selectNextTrace() {
729 * Select the previous trace
731 public void selectPrevTrace() {
736 * Zoom based on mouse cursor location with mouse scrolling
738 * @param zoomIn true to zoom in, false to zoom out
740 public void zoom(boolean zoomIn
) {
741 int globalX
= getDisplay().getCursorLocation().x
;
742 Point p
= toControl(globalX
, 0);
743 int nameSpace
= _timeProvider
.getNameSpace();
744 int timeSpace
= _timeProvider
.getTimeSpace();
745 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
746 long time0
= _timeProvider
.getTime0();
747 long time1
= _timeProvider
.getTime1();
748 long interval
= time1
- time0
;
751 } // to allow getting out of single point interval
754 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
756 newInterval
= (long) Math
.ceil(interval
* 1.25);
758 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
759 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
760 long newTime1
= newTime0
+ newInterval
;
761 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
762 synchronized (fMouseScrollNotifierLock
) {
763 if (fMouseScrollNotifier
== null) {
764 fMouseScrollNotifier
= new MouseScrollNotifier();
765 fMouseScrollNotifier
.start();
767 fMouseScrollNotifier
.mouseScrolled();
772 * zoom in using single click
774 public void zoomIn() {
775 long _time0
= _timeProvider
.getTime0();
776 long _time1
= _timeProvider
.getTime1();
777 long _range
= _time1
- _time0
;
778 long selTime
= _timeProvider
.getSelectedTime();
779 if (selTime
<= _time0
|| selTime
>= _time1
) {
780 selTime
= (_time0
+ _time1
) / 2;
782 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
783 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
785 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
787 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
788 // + time1 + " inaccuracy:" + inaccuracy);
790 if (inaccuracy
> 0 && inaccuracy
< 100) {
791 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
795 long m
= _timeProvider
.getMinTimeInterval();
796 if ((time1
- time0
) < m
) {
797 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
801 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
805 * zoom out using single click
807 public void zoomOut() {
808 long _time0
= _timeProvider
.getTime0();
809 long _time1
= _timeProvider
.getTime1();
810 long selTime
= _timeProvider
.getSelectedTime();
811 if (selTime
<= _time0
|| selTime
>= _time1
) {
812 selTime
= (_time0
+ _time1
) / 2;
814 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
815 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
817 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
818 if (inaccuracy
> 0 && inaccuracy
< 100) {
819 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
823 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
827 * Return the currently selected trace
829 * @return The entry matching the trace
831 public ITimeGraphEntry
getSelectedTrace() {
832 ITimeGraphEntry trace
= null;
833 int idx
= getSelectedIndex();
835 trace
= _data
._expandedItems
[idx
]._trace
;
841 * Retrieve the index of the currently selected item
845 public int getSelectedIndex() {
847 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
848 Item item
= _data
._expandedItems
[i
];
849 if (item
._selected
) {
857 boolean toggle(int idx
) {
858 boolean toggled
= false;
859 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
860 Item item
= _data
._expandedItems
[idx
];
861 if (item
._hasChildren
) {
862 item
._expanded
= !item
._expanded
;
863 _data
.updateExpandedItems();
867 fireTreeEvent(item
._trace
, item
._expanded
);
873 int getItemIndexAtY(int y
) {
877 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
879 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
880 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
887 int idx
= y
/ _itemHeight
;
889 if (idx
< _data
._expandedItems
.length
) {
895 boolean isOverSplitLine(int x
) {
896 if (x
< 0 || null == _timeProvider
) {
900 int nameWidth
= _timeProvider
.getNameSpace();
901 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
907 ITimeGraphEntry
getEntry(Point pt
) {
908 int idx
= getItemIndexAtY(pt
.y
);
909 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
913 * Return the x coordinate corresponding to a time
915 * @param time the time
916 * @return the x coordinate corresponding to the time
920 public int getXForTime(long time
) {
921 if (null == _timeProvider
) {
924 long time0
= _timeProvider
.getTime0();
925 long time1
= _timeProvider
.getTime1();
926 int width
= getCtrlSize().x
;
927 int nameSpace
= _timeProvider
.getNameSpace();
928 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
929 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
934 * Return the time corresponding to an x coordinate
936 * @param coord The X coordinate
937 * @return The time corresponding to the x coordinate
941 public long getTimeAtX(int coord
) {
942 if (null == _timeProvider
) {
946 Point size
= getCtrlSize();
947 long time0
= _timeProvider
.getTime0();
948 long time1
= _timeProvider
.getTime1();
949 int nameWidth
= _timeProvider
.getNameSpace();
950 final int x
= coord
- nameWidth
;
951 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
952 if (x
>= 0 && size
.x
>= nameWidth
) {
953 if (time1
- time0
> timeWidth
) {
954 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
955 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
957 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
958 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
964 void selectItem(int idx
, boolean addSelection
) {
965 boolean changed
= false;
967 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
968 Item item
= _data
._expandedItems
[idx
];
969 changed
= (item
._selected
== false);
970 item
._selected
= true;
973 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
974 Item item
= _data
._expandedItems
[i
];
975 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
978 item
._selected
= i
== idx
;
981 changed
|= ensureVisibleItem(idx
, true);
988 * Callback for item selection
991 * The entry matching the trace
992 * @param addSelection
993 * If the selection is added or removed
995 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
996 int idx
= _data
.findItemIndex(trace
);
997 selectItem(idx
, addSelection
);
1001 * Retrieve the number of entries shown per page.
1005 public int countPerPage() {
1006 int height
= getCtrlSize().y
;
1008 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1010 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
1011 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1012 if (ySum
>= height
) {
1017 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
1018 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1019 if (ySum
>= height
) {
1027 count
= height
/ _itemHeight
;
1033 * Get the index of the top element
1037 public int getTopIndex() {
1042 * Get the number of expanded items
1044 * @return The count of expanded items
1046 public int getExpandedElementCount() {
1047 return _data
._expandedItems
.length
;
1051 * Get an array of all expanded elements
1053 * @return The expanded elements
1055 public ITimeGraphEntry
[] getExpandedElements() {
1056 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1057 for (Item item
: _data
._expandedItems
) {
1058 elements
.add(item
._trace
);
1060 return elements
.toArray(new ITimeGraphEntry
[0]);
1063 Point
getCtrlSize() {
1064 Point size
= getSize();
1065 if (getHorizontalBar().isVisible()) {
1066 size
.y
-= getHorizontalBar().getSize().y
;
1071 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1073 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1074 int width
= nameWidth
;
1075 int height
= _itemHeight
;
1076 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1078 for (int i
= _topIndex
; i
< idx
; i
++) {
1079 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1082 height
= _data
._expandedItems
[idx
].itemHeight
;
1084 return new Rectangle(x
, y
, width
, height
);
1087 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1088 int x
= bound
.x
+ nameWidth
;
1089 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1090 int width
= bound
.width
- x
;
1091 int height
= _itemHeight
;
1092 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1094 for (int i
= _topIndex
; i
< idx
; i
++) {
1095 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1098 height
= _data
._expandedItems
[idx
].itemHeight
;
1100 return new Rectangle(x
, y
, width
, height
);
1104 void paint(Rectangle bounds
, PaintEvent e
) {
1106 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
1107 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1109 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
1113 _idealNameSpace
= 0;
1114 int nameSpace
= _timeProvider
.getNameSpace();
1116 // draw empty name space background
1117 gc
.setBackground(_colors
.getBkColor(false, false, true));
1118 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1120 if (_dragState
== DRAG_ZOOM
) {
1121 // draw selected zoom region background
1122 gc
.setBackground(_colors
.getBkColor(false, false, true));
1123 if (_dragX0
< _dragX
) {
1124 gc
.fillRectangle(new Rectangle(_dragX0
, bounds
.y
, _dragX
- _dragX0
, bounds
.height
));
1125 } else if (_dragX0
> _dragX
) {
1126 gc
.fillRectangle(new Rectangle(_dragX
, bounds
.y
, _dragX0
- _dragX
, bounds
.height
));
1130 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1132 // draw selected time
1133 long time0
= _timeProvider
.getTime0();
1134 long time1
= _timeProvider
.getTime1();
1135 long selectedTime
= _timeProvider
.getSelectedTime();
1136 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1137 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1138 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1139 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1140 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1143 // draw drag line, no line if name space is 0.
1144 if (DRAG_SPLIT_LINE
== _dragState
) {
1145 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1146 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1147 } else if (DRAG_ZOOM
== _dragState
&& Math
.max(_dragX
, _dragX0
) > nameSpace
&& _dragX
!= _dragX0
) {
1148 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1149 gc
.drawLine(_dragX0
, bounds
.y
, _dragX0
, bounds
.y
+ bounds
.height
- 1);
1150 gc
.drawLine(_dragX
, bounds
.y
, _dragX
, bounds
.y
+ bounds
.height
- 1);
1151 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1152 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1153 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1158 * Draw many items at once
1161 * The rectangle of the area
1162 * @param timeProvider
1165 * The array items to draw
1167 * The index of the first element to draw
1169 * The width reserved for the names
1171 * Reference to the SWT GC object
1173 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1174 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1175 for (int i
= topIndex
; i
< items
.length
; i
++) {
1176 Item item
= items
[i
];
1177 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1179 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1185 * @param item the item to draw
1186 * @param bounds the container rectangle
1187 * @param timeProvider Time provider
1188 * @param i the item index
1189 * @param nameSpace the name space
1190 * @param gc Graphics context
1192 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1193 ITimeGraphEntry entry
= item
._trace
;
1194 long time0
= timeProvider
.getTime0();
1195 long time1
= timeProvider
.getTime1();
1196 long selectedTime
= timeProvider
.getSelectedTime();
1198 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1199 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1203 if (! item
._trace
.hasTimeEvents()) {
1204 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1205 nameRect
.width
+= statesRect
.width
;
1206 drawName(item
, nameRect
, gc
);
1208 drawName(item
, nameRect
, gc
);
1210 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1211 if (rect
.isEmpty()) {
1212 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1215 if (time1
<= time0
) {
1216 gc
.setBackground(_colors
.getBkColor(false, false, false));
1217 gc
.fillRectangle(rect
);
1218 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1222 // Initialize _rect1 to same values as enclosing rectangle rect
1223 Rectangle stateRect
= Utils
.clone(rect
);
1224 boolean selected
= item
._selected
;
1225 // K pixels per second
1226 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1228 if (item
._trace
.hasTimeEvents()) {
1229 fillSpace(rect
, gc
, selected
);
1230 // Drawing rectangle is smaller than reserved space
1232 stateRect
.height
-= 6;
1234 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1235 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1238 while (iterator
.hasNext()) {
1239 ITimeEvent event
= iterator
.next();
1240 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1241 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1242 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1243 // event is out of bounds
1246 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1247 stateRect
.x
= Math
.max(rect
.x
, x
);
1248 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1249 if (stateRect
.x
== lastX
) {
1250 stateRect
.width
-= 1;
1251 if (stateRect
.width
> 0) {
1252 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1253 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1257 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1258 if (drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
)) {
1263 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1267 * Draw the name of an item.
1272 * Where to draw the name
1276 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1277 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1278 if (! hasTimeEvents
) {
1279 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1280 gc
.fillRectangle(bounds
);
1281 if (item
._selected
&& _isInFocus
) {
1282 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1283 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1286 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1287 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1288 gc
.fillRectangle(bounds
);
1291 // No name to be drawn
1292 if (_timeProvider
.getNameSpace() == 0) {
1296 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1297 if (item
._hasChildren
) {
1298 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1299 gc
.setBackground(_colors
.getBkColor(false, false, false));
1300 Rectangle rect
= Utils
.clone(bounds
);
1301 rect
.x
+= leftMargin
;
1302 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1303 rect
.width
= EXPAND_SIZE
;
1304 rect
.height
= EXPAND_SIZE
;
1305 gc
.fillRectangle(rect
);
1306 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1307 int midy
= rect
.y
+ rect
.height
/ 2;
1308 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1309 if (!item
._expanded
) {
1310 int midx
= rect
.x
+ rect
.width
/ 2;
1311 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1314 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1316 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1319 int imgHeight
= img
.getImageData().height
;
1320 int imgWidth
= img
.getImageData().width
;
1322 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1323 gc
.drawImage(img
, x
, y
);
1324 leftMargin
+= imgWidth
+ MARGIN
;
1326 String name
= item
._name
;
1327 Point size
= gc
.stringExtent(name
);
1328 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1329 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1331 if (hasTimeEvents
) {
1332 // cut long string with "..."
1333 int width
= bounds
.width
- leftMargin
;
1335 while (size
.x
> width
&& name
.length() > 1) {
1337 name
= name
.substring(0, name
.length() - 1);
1338 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1341 name
+= "..."; //$NON-NLS-1$
1344 Rectangle rect
= Utils
.clone(bounds
);
1345 rect
.x
+= leftMargin
;
1346 rect
.width
-= leftMargin
;
1348 if (rect
.width
> 0) {
1349 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1350 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1351 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1352 leftMargin
+= textWidth
+ MARGIN
;
1355 if (hasTimeEvents
) {
1357 int x
= bounds
.x
+ leftMargin
;
1358 int width
= bounds
.width
- x
;
1359 int midy
= bounds
.y
+ bounds
.height
/ 2;
1360 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1361 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1367 * Draw the state (color fill)
1372 * Time event for which we're drawing the state
1378 * Is this time event currently selected (so it appears
1380 * @param timeSelected
1381 * Is the timestamp currently selected
1382 * @return true if the state was drawn
1385 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1386 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1388 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1389 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1392 boolean visible
= rect
.width
== 0 ?
false : true;
1395 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1396 // Only draw the top and bottom borders
1397 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1398 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1399 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1400 if (rect
.width
== 1) {
1401 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1402 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1406 Color stateColor
= null;
1407 if (colorIdx
< fEventColorMap
.length
) {
1408 stateColor
= fEventColorMap
[colorIdx
];
1410 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1413 boolean reallySelected
= timeSelected
&& selected
;
1414 if (reallySelected
) {
1415 // modify the color?
1417 // fill all rect area
1418 gc
.setBackground(stateColor
);
1419 gc
.fillRectangle(rect
);
1420 // get the border color?
1421 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1424 if (!reallySelected
) {
1425 // Draw the top and bottom borders i.e. no side borders
1426 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1427 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1430 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1431 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1433 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1438 * Fill the space between two contiguous time events
1445 * Is this time event selected or not
1447 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1448 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1449 gc
.fillRectangle(rect
);
1450 if (_dragState
== DRAG_ZOOM
) {
1451 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, true));
1452 if (_dragX0
< _dragX
) {
1453 gc
.fillRectangle(new Rectangle(_dragX0
, rect
.y
, _dragX
- _dragX0
, rect
.height
));
1454 } else if (_dragX0
> _dragX
) {
1455 gc
.fillRectangle(new Rectangle(_dragX
, rect
.y
, _dragX0
- _dragX
, rect
.height
));
1459 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1460 int midy
= rect
.y
+ rect
.height
/ 2;
1461 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1465 public void keyTraversed(TraverseEvent e
) {
1466 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1472 public void keyPressed(KeyEvent e
) {
1474 if (_data
._expandedItems
.length
== 0) {
1477 if (SWT
.HOME
== e
.keyCode
) {
1479 } else if (SWT
.END
== e
.keyCode
) {
1480 idx
= _data
._expandedItems
.length
- 1;
1481 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1482 idx
= getSelectedIndex();
1485 } else if (idx
< _data
._expandedItems
.length
- 1) {
1488 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1489 idx
= getSelectedIndex();
1492 } else if (idx
> 0) {
1495 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1497 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1499 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1500 int page
= countPerPage();
1501 idx
= getSelectedIndex();
1506 if (idx
>= _data
._expandedItems
.length
) {
1507 idx
= _data
._expandedItems
.length
- 1;
1509 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1510 int page
= countPerPage();
1511 idx
= getSelectedIndex();
1519 } else if (SWT
.CR
== e
.keyCode
) {
1520 idx
= getSelectedIndex();
1522 if (_data
._expandedItems
[idx
]._hasChildren
) {
1525 fireDefaultSelection();
1531 selectItem(idx
, false);
1532 fireSelectionChanged();
1537 public void keyReleased(KeyEvent e
) {
1541 public void focusGained(FocusEvent e
) {
1543 if (mouseScrollFilterListener
== null) {
1544 mouseScrollFilterListener
= new Listener() {
1545 // This filter is used to prevent horizontal scrolling of the view
1546 // when the mouse wheel is used to zoom
1548 public void handleEvent(Event event
) {
1552 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1558 public void focusLost(FocusEvent e
) {
1560 if (mouseScrollFilterListener
!= null) {
1561 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1562 mouseScrollFilterListener
= null;
1564 if (DRAG_NONE
!= _dragState
) {
1566 _dragState
= DRAG_NONE
;
1572 * @return If the current view is focused
1574 public boolean isInFocus() {
1579 * Provide the possibility to control the wait cursor externally e.g. data
1580 * requests in progress
1582 * @param waitInd Should we wait indefinitely?
1584 public void waitCursor(boolean waitInd
) {
1585 // Update cursor as indicated
1587 setCursor(_WaitCursor
);
1588 _isWaitCursor
= true;
1591 _isWaitCursor
= false;
1594 // Get ready for next mouse move
1595 _isDragCursor3
= false;
1600 * If the x, y position is over the vertical split line (name to time
1601 * ranges), then change the cursor to a drag cursor to indicate the user the
1602 * possibility of resizing
1608 void updateCursor(int x
, int y
) {
1609 // if Wait cursor not active, check for the need to change to a drag
1611 if (_isWaitCursor
== false) {
1612 boolean isSplitLine
= isOverSplitLine(x
);
1613 // No dragcursor is name space is fixed to zero
1614 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1615 setCursor(_dragCursor3
);
1616 _isDragCursor3
= true;
1617 } else if (!isSplitLine
&& _isDragCursor3
) {
1619 _isDragCursor3
= false;
1625 public void mouseMove(MouseEvent e
) {
1626 if (null == _timeProvider
) {
1629 Point size
= getCtrlSize();
1630 if (DRAG_TRACE_ITEM
== _dragState
) {
1631 int nameWidth
= _timeProvider
.getNameSpace();
1632 int x
= e
.x
- nameWidth
;
1633 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1635 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1636 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1637 long time1
= _time1bak
- timeDelta
;
1638 long maxTime
= _timeProvider
.getMaxTime();
1639 if (time1
> maxTime
) {
1642 long time0
= time1
- (_time1bak
- _time0bak
);
1643 if (time0
< _timeProvider
.getMinTime()) {
1644 time0
= _timeProvider
.getMinTime();
1645 time1
= time0
+ (_time1bak
- _time0bak
);
1647 _timeProvider
.setStartFinishTime(time0
, time1
);
1649 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1651 _timeProvider
.setNameSpace(e
.x
);
1652 } else if (DRAG_ZOOM
== _dragState
) {
1653 _dragX
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1655 } else if (DRAG_NONE
== _dragState
) {
1656 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1657 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1660 _mouseOverSplitLine
= mouseOverSplitLine
;
1662 updateCursor(e
.x
, e
.y
);
1666 public void mouseDoubleClick(MouseEvent e
) {
1667 if (null == _timeProvider
) {
1670 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1671 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1672 _timeProvider
.setNameSpace(_idealNameSpace
);
1673 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1674 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1677 _mouseOverSplitLine
= mouseOverSplitLine
;
1680 int idx
= getItemIndexAtY(e
.y
);
1682 selectItem(idx
, false);
1683 fireDefaultSelection();
1689 public void mouseDown(MouseEvent e
) {
1690 if (_dragState
!= DRAG_NONE
|| null == _timeProvider
) {
1694 if (1 == e
.button
) {
1695 int nameSpace
= _timeProvider
.getNameSpace();
1696 if (nameSpace
!= 0) {
1697 if (isOverSplitLine(e
.x
)) {
1698 _dragState
= DRAG_SPLIT_LINE
;
1699 _dragX
= _dragX0
= e
.x
;
1700 _time0bak
= _timeProvider
.getTime0();
1701 _time1bak
= _timeProvider
.getTime1();
1707 idx
= getItemIndexAtY(e
.y
);
1709 Item item
= _data
._expandedItems
[idx
];
1710 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1713 long hitTime
= getTimeAtX(e
.x
);
1715 // _timeProvider.setSelectedTimeInt(hitTime, false);
1717 _dragState
= DRAG_TRACE_ITEM
;
1718 _dragX
= _dragX0
= e
.x
- nameSpace
;
1719 _time0bak
= _timeProvider
.getTime0();
1720 _time1bak
= _timeProvider
.getTime1();
1723 selectItem(idx
, false);
1724 fireSelectionChanged();
1726 selectItem(idx
, false); // clear selection
1728 fireSelectionChanged();
1730 } else if (3 == e
.button
) {
1731 if (_timeProvider
.getTime0() == _timeProvider
.getTime1() || getCtrlSize().x
- _timeProvider
.getNameSpace() <= 0) {
1735 _dragX
= _dragX0
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1736 _dragState
= DRAG_ZOOM
;
1741 public void mouseUp(MouseEvent e
) {
1742 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
1743 menuDetected(fPendingMenuDetectEvent
);
1745 if (DRAG_NONE
!= _dragState
) {
1747 if (e
.button
== 1 && DRAG_TRACE_ITEM
== _dragState
) {
1748 if (_dragX
== _dragX0
) { // click without drag
1749 long time
= getTimeAtX(e
.x
);
1750 _timeProvider
.setSelectedTimeNotify(time
, false);
1752 // Notify time provider to check the need for listener
1754 _timeProvider
.notifyStartFinishTime();
1756 _dragState
= DRAG_NONE
;
1757 } else if (e
.button
== 1 && DRAG_SPLIT_LINE
== _dragState
) {
1759 _dragState
= DRAG_NONE
;
1760 } else if (e
.button
== 3 && DRAG_ZOOM
== _dragState
) {
1761 int nameWidth
= _timeProvider
.getNameSpace();
1762 if (Math
.max(_dragX
, _dragX0
) > nameWidth
&& _dragX
!= _dragX0
) {
1763 long time0
= getTimeAtX(_dragX0
);
1764 long time1
= getTimeAtX(_dragX
);
1765 if (time0
< time1
) {
1766 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1768 _timeProvider
.setStartFinishTimeNotify(time1
, time0
);
1773 _dragState
= DRAG_NONE
;
1779 public void mouseEnter(MouseEvent e
) {
1783 public void mouseExit(MouseEvent e
) {
1784 if (_mouseOverSplitLine
) {
1785 _mouseOverSplitLine
= false;
1791 public void mouseHover(MouseEvent e
) {
1795 public void mouseScrolled(MouseEvent e
) {
1796 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1799 boolean zoomScroll
= false;
1800 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1801 Point parentSize
= getParent().getSize();
1802 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1803 // over the parent control
1804 if (e
.x
> getCtrlSize().x
) {
1805 // over the horizontal scroll bar
1807 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1808 // over the name space
1814 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1817 } else if (e
.count
< 0) {
1821 setTopIndex(getTopIndex() - e
.count
);
1826 public void controlMoved(ControlEvent e
) {
1830 public void controlResized(ControlEvent e
) {
1835 public void widgetDefaultSelected(SelectionEvent e
) {
1839 public void widgetSelected(SelectionEvent e
) {
1840 if (e
.widget
== getVerticalBar()) {
1841 setTopIndex(getVerticalBar().getSelection());
1842 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1843 int start
= getHorizontalBar().getSelection();
1844 long time0
= _timeProvider
.getTime0();
1845 long time1
= _timeProvider
.getTime1();
1846 long timeMin
= _timeProvider
.getMinTime();
1847 long timeMax
= _timeProvider
.getMaxTime();
1848 long delta
= timeMax
- timeMin
;
1850 long range
= time1
- time0
;
1851 // _timeRangeFixed = true;
1852 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1853 time1
= time0
+ range
;
1855 // TODO: Follow-up with Bug 310310
1856 // In Linux SWT.DRAG is the only value received
1857 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1858 if (e
.detail
== SWT
.DRAG
) {
1859 _timeProvider
.setStartFinishTime(time0
, time1
);
1861 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1867 * @return The current visibility of the vertical scroll bar
1869 public boolean isVisibleVerticalScroll() {
1870 return _visibleVerticalScroll
;
1874 public int getBorderWidth() {
1875 return _borderWidth
;
1879 * Set the border width
1881 * @param borderWidth
1884 public void setBorderWidth(int borderWidth
) {
1885 this._borderWidth
= borderWidth
;
1889 * @return The current height of the header row
1891 public int getHeaderHeight() {
1892 return _headerHeight
;
1896 * Set the height of the header row
1898 * @param headerHeight
1901 public void setHeaderHeight(int headerHeight
) {
1902 this._headerHeight
= headerHeight
;
1906 * @return The height of regular item rows
1908 public int getItemHeight() {
1913 * Set the height of regular itew rows
1918 public void setItemHeight(int rowHeight
) {
1919 this._itemHeight
= rowHeight
;
1923 * Set the minimum item width
1925 * @param width The minimum width
1927 public void setMinimumItemWidth(int width
) {
1928 this._minimumItemWidth
= width
;
1932 * @return The minimum item width
1934 public int getMinimumItemWidth() {
1935 return _minimumItemWidth
;
1939 * @return The entries that are currently filtered out
1941 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1942 return _data
.getFilteredOut();
1947 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1948 if (listener
!= null) {
1949 if (!_selectionChangedListeners
.contains(listener
)) {
1950 _selectionChangedListeners
.add(listener
);
1957 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1958 if (listener
!= null) {
1959 _selectionChangedListeners
.remove(listener
);
1965 public void setSelection(ISelection selection
) {
1966 if (selection
instanceof TimeGraphSelection
) {
1967 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1968 Object ob
= sel
.getFirstElement();
1969 if (ob
instanceof ITimeGraphEntry
) {
1970 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1971 selectItem(trace
, false);
1978 * @param filter The filter object to be attached to the view
1981 public void addFilter(ViewerFilter filter
) {
1982 if (!_filters
.contains(filter
)) {
1983 _filters
.add(filter
);
1988 * @param filter The filter object to be attached to the view
1991 public void removeFilter(ViewerFilter filter
) {
1992 _filters
.remove(filter
);
1995 private class ItemData
{
1996 public Item
[] _expandedItems
= new Item
[0];
1997 public Item
[] _items
= new Item
[0];
1998 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1999 private boolean traceFilter
[] = new boolean[0];
2000 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
2001 public ITimeGraphPresentationProvider provider
;
2006 Item
findItem(ITimeGraphEntry entry
) {
2007 if (entry
== null) {
2011 for (int i
= 0; i
< _items
.length
; i
++) {
2012 Item item
= _items
[i
];
2013 if (item
._trace
== entry
) {
2021 int findItemIndex(ITimeGraphEntry trace
) {
2022 if (trace
== null) {
2026 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
2027 Item item
= _expandedItems
[i
];
2028 if (item
._trace
== trace
) {
2036 public void refreshData() {
2037 List
<Item
> itemList
= new ArrayList
<Item
>();
2038 filteredOut
.clear();
2039 for (int i
= 0; i
< _traces
.length
; i
++) {
2040 ITimeGraphEntry entry
= _traces
[i
];
2041 refreshData(itemList
, null, 0, entry
);
2043 _items
= itemList
.toArray(new Item
[0]);
2044 updateExpandedItems();
2047 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2048 Item item
= new Item(entry
, entry
.getName(), level
);
2049 if (parent
!= null) {
2050 parent
.children
.add(item
);
2052 item
.itemHeight
= provider
.getItemHeight(entry
);
2054 if (entry
.hasChildren()) {
2055 item
._expanded
= true;
2056 item
._hasChildren
= true;
2057 for (ITimeGraphEntry child
: entry
.getChildren()) {
2058 refreshData(itemList
, item
, level
+ 1, child
);
2063 public void updateExpandedItems() {
2064 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2065 for (int i
= 0; i
< _traces
.length
; i
++) {
2066 ITimeGraphEntry entry
= _traces
[i
];
2067 Item item
= findItem(entry
);
2068 refreshExpanded(expandedItemList
, item
);
2070 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
2073 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2074 // Check for filters
2075 boolean display
= true;
2076 for (ViewerFilter filter
: _filters
) {
2077 if (!filter
.select(null, item
._trace
.getParent(), item
._trace
)) {
2083 expandedItemList
.add(item
);
2084 if (item
._hasChildren
&& item
._expanded
) {
2085 for (Item child
: item
.children
) {
2086 refreshExpanded(expandedItemList
, child
);
2092 public void refreshData(ITimeGraphEntry traces
[]) {
2093 if (traces
== null || traces
.length
== 0) {
2095 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
2096 traceFilter
= new boolean[traces
.length
];
2097 java
.util
.Arrays
.fill(traceFilter
, true);
2104 public ITimeGraphEntry
[] getTraces() {
2108 public boolean[] getTraceFilter() {
2112 public Vector
<ITimeGraphEntry
> getFilteredOut() {
2117 private class Item
{
2118 public boolean _expanded
;
2119 public boolean _selected
;
2120 public boolean _hasChildren
;
2121 public int itemHeight
;
2123 public List
<Item
> children
;
2124 public String _name
;
2125 public ITimeGraphEntry _trace
;
2127 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2128 this._trace
= trace
;
2131 this.children
= new ArrayList
<Item
>();
2135 public String
toString() {
2144 public void menuDetected(MenuDetectEvent e
) {
2145 if (null == _timeProvider
) {
2148 if (e
.detail
== SWT
.MENU_MOUSE
) {
2149 if (fPendingMenuDetectEvent
== null) {
2150 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2151 * Store the event and trigger it later just before handling mouseUp.
2152 * This allows for the method to detect if mouse is used to drag zoom.
2154 fPendingMenuDetectEvent
= e
;
2157 fPendingMenuDetectEvent
= null;
2158 if (_dragState
!= DRAG_ZOOM
|| _dragX
!= _dragX0
) {
2162 if (_dragState
!= DRAG_NONE
) {
2166 Point p
= toControl(e
.x
, e
.y
);
2167 int idx
= getItemIndexAtY(p
.y
);
2168 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
2169 Item item
= _data
._expandedItems
[idx
];
2170 ITimeGraphEntry entry
= item
._trace
;
2171 if (entry
.hasTimeEvents()) {
2172 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2173 if (event
!= null) {
2175 fireMenuEventOnTimeEvent(e
);
2180 fireMenuEventOnTimeGraphEntry(e
);