1 /*****************************************************************************
2 * Copyright (c) 2007, 2008 Intel Corporation, 2009, 2010, 2011, 2012 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
14 *****************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
21 import java
.util
.Vector
;
23 import org
.eclipse
.jface
.resource
.JFaceResources
;
24 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
27 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
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
.MouseEvent
;
42 import org
.eclipse
.swt
.events
.MouseListener
;
43 import org
.eclipse
.swt
.events
.MouseMoveListener
;
44 import org
.eclipse
.swt
.events
.MouseTrackListener
;
45 import org
.eclipse
.swt
.events
.MouseWheelListener
;
46 import org
.eclipse
.swt
.events
.PaintEvent
;
47 import org
.eclipse
.swt
.events
.SelectionEvent
;
48 import org
.eclipse
.swt
.events
.SelectionListener
;
49 import org
.eclipse
.swt
.events
.TraverseEvent
;
50 import org
.eclipse
.swt
.events
.TraverseListener
;
51 import org
.eclipse
.swt
.graphics
.Color
;
52 import org
.eclipse
.swt
.graphics
.Cursor
;
53 import org
.eclipse
.swt
.graphics
.GC
;
54 import org
.eclipse
.swt
.graphics
.Image
;
55 import org
.eclipse
.swt
.graphics
.Point
;
56 import org
.eclipse
.swt
.graphics
.Rectangle
;
57 import org
.eclipse
.swt
.widgets
.Composite
;
58 import org
.eclipse
.swt
.widgets
.Display
;
59 import org
.eclipse
.swt
.widgets
.Event
;
60 import org
.eclipse
.swt
.widgets
.Listener
;
61 import org
.eclipse
.swt
.widgets
.ScrollBar
;
64 * Time graph control implementation
67 * @author Alvaro Sanchez-Leon
68 * @author Patrick Tasse
70 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
{
72 private static final int DRAG_NONE
= 0;
73 private static final int DRAG_TRACE_ITEM
= 1;
74 private static final int DRAG_SPLIT_LINE
= 2;
75 private static final int DRAG_ZOOM
= 3;
76 public static final boolean DEFAULT_DRAW_THREAD_JOIN
= true;
77 public static final boolean DEFAULT_DRAW_THREAD_WAIT
= true;
78 public static final boolean DEFAULT_DRAW_THREAD_RELEASE
= true;
79 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
80 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
82 private static final double zoomCoeff
= 1.5;
84 private ITimeDataProvider _timeProvider
;
85 private boolean _isInFocus
= false;
86 private boolean _isDragCursor3
= false;
87 private boolean _isWaitCursor
= true;
88 private boolean _mouseOverSplitLine
= false;
89 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
90 private int _minimumItemWidth
= 0;
91 private int _topIndex
= 0;
92 private int _dragState
= DRAG_NONE
;
93 private int _dragX0
= 0;
94 private int _dragX
= 0;
95 private int _idealNameSpace
= 0;
96 // private double _timeStep = 10000000;
97 private long _time0bak
;
98 private long _time1bak
;
99 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
100 private ItemData _data
= null;
101 private List
<SelectionListener
> _selectionListeners
;
102 private final List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
103 private final List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
104 private final Cursor _dragCursor3
;
105 private final Cursor _WaitCursor
;
107 // Vertical formatting formatting for the state control view
108 private final boolean _visibleVerticalScroll
= true;
109 private int _borderWidth
= 0;
110 private int _headerHeight
= 0;
112 private Listener mouseScrollFilterListener
;
114 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
115 protected Color
[] fEventColorMap
= null;
117 private MouseScrollNotifier fMouseScrollNotifier
;
118 private final Object fMouseScrollNotifierLock
= new Object();
119 private class MouseScrollNotifier
extends Thread
{
120 private final static long DELAY
= 400L;
121 private final static long POLLING_INTERVAL
= 10L;
122 private long fLastScrollTime
= Long
.MAX_VALUE
;
126 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
128 Thread
.sleep(POLLING_INTERVAL
);
129 } catch (Exception e
) {
133 if (!isInterrupted()) {
134 Display
.getDefault().asyncExec(new Runnable() {
140 _timeProvider
.notifyStartFinishTime();
144 synchronized (fMouseScrollNotifierLock
) {
145 fMouseScrollNotifier
= null;
149 public void mouseScrolled() {
150 fLastScrollTime
= System
.currentTimeMillis();
155 * Standard constructor
158 * The parent composite object
160 * The color scheme to use
162 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
164 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
166 _data
= new ItemData();
168 addFocusListener(this);
169 addMouseListener(this);
170 addMouseMoveListener(this);
171 addMouseTrackListener(this);
172 addMouseWheelListener(this);
173 addTraverseListener(this);
174 addKeyListener(this);
175 addControlListener(this);
176 ScrollBar scrollHor
= getHorizontalBar();
178 if (scrollHor
!= null) {
179 scrollHor
.addSelectionListener(this);
182 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
183 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
187 public void dispose() {
189 _dragCursor3
.dispose();
190 _WaitCursor
.dispose();
191 fResourceManager
.dispose();
195 * Sets the timegraph provider used by this timegraph viewer.
197 * @param timeGraphProvider the timegraph provider
199 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
200 fTimeGraphProvider
= timeGraphProvider
;
201 _data
.provider
= timeGraphProvider
;
203 if (fEventColorMap
!= null) {
204 for (Color color
: fEventColorMap
) {
205 fResourceManager
.destroyColor(color
.getRGB());
208 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
209 if (stateItems
!= null) {
210 fEventColorMap
= new Color
[stateItems
.length
];
211 for (int i
= 0; i
< stateItems
.length
; i
++) {
212 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
215 fEventColorMap
= new Color
[] { };
220 * Assign the given time provider
222 * @param timeProvider
225 public void setTimeProvider(ITimeDataProvider timeProvider
) {
226 _timeProvider
= timeProvider
;
232 * Add a selection listener
235 * The listener to add
237 public void addSelectionListener(SelectionListener listener
) {
238 if (listener
== null) {
239 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
241 if (null == _selectionListeners
) {
242 _selectionListeners
= new ArrayList
<SelectionListener
>();
244 _selectionListeners
.add(listener
);
248 * Remove a selection listener
251 * The listener to remove
253 public void removeSelectionListener(SelectionListener listener
) {
254 if (null != _selectionListeners
) {
255 _selectionListeners
.remove(listener
);
260 * Selection changed callback
262 public void fireSelectionChanged() {
263 if (null != _selectionListeners
) {
264 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
265 while (it
.hasNext()) {
266 SelectionListener listener
= it
.next();
267 listener
.widgetSelected(null);
273 * Default selection callback
275 public void fireDefaultSelection() {
276 if (null != _selectionListeners
) {
277 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
278 while (it
.hasNext()) {
279 SelectionListener listener
= it
.next();
280 listener
.widgetDefaultSelected(null);
286 * Get the traces in the model
288 * @return The array of traces
290 public ITimeGraphEntry
[] getTraces() {
291 return _data
.getTraces();
295 * Get the on/off trace filters
297 * @return The array of filters
299 public boolean[] getTraceFilter() {
300 return _data
.getTraceFilter();
304 * Refresh the data for the thing
306 public void refreshData() {
313 * Refresh data for the given traces
316 * The traces to refresh
318 public void refreshData(ITimeGraphEntry
[] traces
) {
319 _data
.refreshData(traces
);
325 * Adjust the scoll bars
327 public void adjustScrolls() {
328 if (null == _timeProvider
) {
329 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
335 long time0
= _timeProvider
.getTime0();
336 long time1
= _timeProvider
.getTime1();
338 long timeMin
= _timeProvider
.getMinTime();
339 long timeMax
= _timeProvider
.getMaxTime();
341 long delta
= timeMax
- timeMin
;
344 int thumb
= H_SCROLLBAR_MAX
;
347 // Thumb size (page size)
348 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
349 // At the beginning of visible window
350 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
353 // position, minimum, maximum, thumb size, increment (half page)t, page
354 // increment size (full page)
355 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
358 boolean ensureVisibleItem(int idx
, boolean redraw
) {
359 boolean changed
= false;
361 for (idx
= 0; idx
< _data
._expandedItems
.length
; idx
++) {
362 if (_data
._expandedItems
[idx
]._selected
) {
367 if (idx
>= _data
._expandedItems
.length
) {
370 if (idx
< _topIndex
) {
372 //FIXME:getVerticalBar().setSelection(_topItem);
378 int page
= countPerPage();
379 if (idx
>= _topIndex
+ page
) {
380 setTopIndex(idx
- page
+ 1);
381 //FIXME:getVerticalBar().setSelection(_topItem);
392 * Assign the given index as the top one
397 public void setTopIndex(int idx
) {
398 idx
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
399 idx
= Math
.max(0, idx
);
405 * Set the expanded state of a given entry
410 * True if expanded, false if collapsed
412 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
413 Item item
= _data
.findItem(entry
);
414 if (item
!= null && item
._expanded
!= expanded
) {
415 item
._expanded
= expanded
;
416 _data
.updateExpandedItems();
422 * Add a tree listener
425 * The listener to add
427 public void addTreeListener(ITimeGraphTreeListener listener
) {
428 if (!_treeListeners
.contains(listener
)) {
429 _treeListeners
.add(listener
);
434 * Remove a tree listener
437 * The listener to remove
439 public void removeTreeListener(ITimeGraphTreeListener listener
) {
440 if (_treeListeners
.contains(listener
)) {
441 _treeListeners
.remove(listener
);
446 * Tree event callback
451 * The expanded state (true for expanded, false for collapsed)
453 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
454 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
455 for (ITimeGraphTreeListener listener
: _treeListeners
) {
457 listener
.treeExpanded(event
);
459 listener
.treeCollapsed(event
);
465 public ISelection
getSelection() {
466 TimeGraphSelection sel
= new TimeGraphSelection();
467 ITimeGraphEntry trace
= getSelectedTrace();
468 if (null != trace
&& null != _timeProvider
) {
469 long selectedTime
= _timeProvider
.getSelectedTime();
470 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
481 * Get the selection object
483 * @return The selection
485 public ISelection
getSelectionTrace() {
486 TimeGraphSelection sel
= new TimeGraphSelection();
487 ITimeGraphEntry trace
= getSelectedTrace();
495 * Enable/disable one of the traces in the model
498 * 1 to enable it, -1 to disable. The method returns immediately
499 * if another value is used.
501 public void selectTrace(int n
) {
502 if ((n
!= 1) && (n
!= -1)) {
506 boolean changed
= false;
507 int lastSelection
= -1;
508 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
509 Item item
= _data
._expandedItems
[i
];
510 if (item
._selected
) {
512 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
513 item
._selected
= false;
514 item
= _data
._expandedItems
[i
+ 1];
515 item
._selected
= true;
517 } else if ((-1 == n
) && (i
> 0)) {
518 item
._selected
= false;
519 item
= _data
._expandedItems
[i
- 1];
520 item
._selected
= true;
527 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
528 Item item
= _data
._expandedItems
[0];
529 item
._selected
= true;
534 ensureVisibleItem(-1, false);
536 fireSelectionChanged();
544 * 1 for next event, -1 for previous event
546 public void selectEvent(int n
) {
547 if (null == _timeProvider
) {
550 ITimeGraphEntry trace
= getSelectedTrace();
554 long selectedTime
= _timeProvider
.getSelectedTime();
555 long endTime
= _timeProvider
.getEndTime();
556 ITimeEvent nextEvent
;
557 if (-1 == n
&& selectedTime
> endTime
) {
558 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
560 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
562 if (null == nextEvent
&& -1 == n
) {
563 nextEvent
= Utils
.getFirstEvent(trace
);
565 if (null != nextEvent
) {
566 long nextTime
= nextEvent
.getTime();
567 // If last event detected e.g. going back or not moving to a next
569 if (nextTime
<= selectedTime
&& n
== 1) {
570 // Select to the end of this last event
571 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
572 // but not beyond the end of the trace
573 if (nextTime
> endTime
) {
576 } else if (n
== -1) {
577 // for previous event go to its end time unless we were already there
578 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
579 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
582 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
583 fireSelectionChanged();
585 _timeProvider
.setSelectedTimeNotify(endTime
, true);
586 fireSelectionChanged();
591 * Select the next event
593 public void selectNextEvent() {
595 // Notify if visible time window has been adjusted
596 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
600 * Select the previous event
602 public void selectPrevEvent() {
604 // Notify if visible time window has been adjusted
605 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
609 * Select the next trace
611 public void selectNextTrace() {
616 * Select the previous trace
618 public void selectPrevTrace() {
623 * Zoom based on mouse cursor location with mouse scrolling
625 * @param zoomIn true to zoom in, false to zoom out
627 public void zoom(boolean zoomIn
) {
628 int globalX
= getDisplay().getCursorLocation().x
;
629 Point p
= toControl(globalX
, 0);
630 int nameSpace
= _timeProvider
.getNameSpace();
631 int timeSpace
= _timeProvider
.getTimeSpace();
632 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
633 long time0
= _timeProvider
.getTime0();
634 long time1
= _timeProvider
.getTime1();
635 long interval
= time1
- time0
;
638 } // to allow getting out of single point interval
641 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
643 newInterval
= (long) Math
.ceil(interval
* 1.25);
645 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
646 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
647 long newTime1
= newTime0
+ newInterval
;
648 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
649 synchronized (fMouseScrollNotifierLock
) {
650 if (fMouseScrollNotifier
== null) {
651 fMouseScrollNotifier
= new MouseScrollNotifier();
652 fMouseScrollNotifier
.start();
654 fMouseScrollNotifier
.mouseScrolled();
659 * zoom in using single click
661 public void zoomIn() {
662 long _time0
= _timeProvider
.getTime0();
663 long _time1
= _timeProvider
.getTime1();
664 long _range
= _time1
- _time0
;
665 long selTime
= _timeProvider
.getSelectedTime();
666 if (selTime
<= _time0
|| selTime
>= _time1
) {
667 selTime
= (_time0
+ _time1
) / 2;
669 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
670 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
672 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
674 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
675 // + time1 + " inaccuracy:" + inaccuracy);
677 if (inaccuracy
> 0 && inaccuracy
< 100) {
678 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
682 long m
= _timeProvider
.getMinTimeInterval();
683 if ((time1
- time0
) < m
) {
684 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
688 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
692 * zoom out using single click
694 public void zoomOut() {
695 long _time0
= _timeProvider
.getTime0();
696 long _time1
= _timeProvider
.getTime1();
697 long selTime
= _timeProvider
.getSelectedTime();
698 if (selTime
<= _time0
|| selTime
>= _time1
) {
699 selTime
= (_time0
+ _time1
) / 2;
701 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
702 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
704 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
705 if (inaccuracy
> 0 && inaccuracy
< 100) {
706 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
710 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
714 * Return the currently selected trace
716 * @return The entry matching the trace
718 public ITimeGraphEntry
getSelectedTrace() {
719 ITimeGraphEntry trace
= null;
720 int idx
= getSelectedIndex();
722 trace
= _data
._expandedItems
[idx
]._trace
;
728 * Retrieve the index of the currently selected item
732 public int getSelectedIndex() {
734 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
735 Item item
= _data
._expandedItems
[i
];
736 if (item
._selected
) {
744 boolean toggle(int idx
) {
745 boolean toggled
= false;
746 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
747 Item item
= _data
._expandedItems
[idx
];
748 if (item
._hasChildren
) {
749 item
._expanded
= !item
._expanded
;
750 _data
.updateExpandedItems();
754 fireTreeEvent(item
._trace
, item
._expanded
);
760 int getItemIndexAtY(int y
) {
764 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
766 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
767 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
774 int idx
= y
/ _itemHeight
;
776 if (idx
< _data
._expandedItems
.length
) {
782 boolean isOverSplitLine(int x
) {
783 if (x
< 0 || null == _timeProvider
) {
787 int nameWidth
= _timeProvider
.getNameSpace();
788 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
794 ITimeGraphEntry
getEntry(Point pt
) {
795 int idx
= getItemIndexAtY(pt
.y
);
796 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
799 long getTimeAtX(int x
) {
800 if (null == _timeProvider
) {
804 Point size
= getCtrlSize();
805 long time0
= _timeProvider
.getTime0();
806 long time1
= _timeProvider
.getTime1();
807 int nameWidth
= _timeProvider
.getNameSpace();
809 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
810 if (x
>= 0 && size
.x
>= nameWidth
) {
811 if (time1
- time0
> timeWidth
) {
812 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
813 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
815 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
816 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
822 void selectItem(int idx
, boolean addSelection
) {
823 boolean changed
= false;
825 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
826 Item item
= _data
._expandedItems
[idx
];
827 changed
= (item
._selected
== false);
828 item
._selected
= true;
831 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
832 Item item
= _data
._expandedItems
[i
];
833 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
836 item
._selected
= i
== idx
;
839 changed
|= ensureVisibleItem(idx
, true);
846 * Callback for item selection
849 * The entry matching the trace
850 * @param addSelection
851 * If the selection is added or removed
853 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
854 int idx
= _data
.findItemIndex(trace
);
855 selectItem(idx
, addSelection
);
859 * Retrieve the number of entries shown per page.
863 public int countPerPage() {
864 int height
= getCtrlSize().y
;
866 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
868 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
869 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
870 if (ySum
>= height
) {
875 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
876 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
877 if (ySum
>= height
) {
885 count
= height
/ _itemHeight
;
891 * Get the index of the top element
895 public int getTopIndex() {
900 * Get the number of expanded items
902 * @return The count of expanded items
904 public int getExpandedElementCount() {
905 return _data
._expandedItems
.length
;
909 * Get an array of all expanded elements
911 * @return The expanded elements
913 public ITimeGraphEntry
[] getExpandedElements() {
914 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
915 for (Item item
: _data
._expandedItems
) {
916 elements
.add(item
._trace
);
918 return elements
.toArray(new ITimeGraphEntry
[0]);
921 Point
getCtrlSize() {
922 Point size
= getSize();
923 if (getHorizontalBar().isVisible()) {
924 size
.y
-= getHorizontalBar().getSize().y
;
929 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
931 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
932 int width
= nameWidth
;
933 int height
= _itemHeight
;
934 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
936 for (int i
= _topIndex
; i
< idx
; i
++) {
937 ySum
+= _data
._expandedItems
[i
].itemHeight
;
940 height
= _data
._expandedItems
[idx
].itemHeight
;
942 return new Rectangle(x
, y
, width
, height
);
945 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
946 int x
= bound
.x
+ nameWidth
;
947 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
948 int width
= bound
.width
- x
;
949 int height
= _itemHeight
;
950 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
952 for (int i
= _topIndex
; i
< idx
; i
++) {
953 ySum
+= _data
._expandedItems
[i
].itemHeight
;
956 height
= _data
._expandedItems
[idx
].itemHeight
;
958 return new Rectangle(x
, y
, width
, height
);
962 void paint(Rectangle bounds
, PaintEvent e
) {
964 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
965 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
967 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
972 int nameSpace
= _timeProvider
.getNameSpace();
974 // draw empty name space background
975 gc
.setBackground(_colors
.getBkColor(false, false, true));
976 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
978 if (_dragState
== DRAG_ZOOM
) {
979 // draw selected zoom region background
980 gc
.setBackground(_colors
.getBkColor(false, false, true));
981 if (_dragX0
< _dragX
) {
982 gc
.fillRectangle(new Rectangle(_dragX0
, bounds
.y
, _dragX
- _dragX0
, bounds
.height
));
983 } else if (_dragX0
> _dragX
) {
984 gc
.fillRectangle(new Rectangle(_dragX
, bounds
.y
, _dragX0
- _dragX
, bounds
.height
));
988 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
990 // draw selected time
991 long time0
= _timeProvider
.getTime0();
992 long time1
= _timeProvider
.getTime1();
993 long selectedTime
= _timeProvider
.getSelectedTime();
994 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
995 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
996 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
997 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
998 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1001 // draw drag line, no line if name space is 0.
1002 if (DRAG_SPLIT_LINE
== _dragState
) {
1003 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1004 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1005 } else if (DRAG_ZOOM
== _dragState
&& Math
.max(_dragX
, _dragX0
) > nameSpace
) {
1006 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1007 gc
.drawLine(_dragX0
, bounds
.y
, _dragX0
, bounds
.y
+ bounds
.height
- 1);
1008 gc
.drawLine(_dragX
, bounds
.y
, _dragX
, bounds
.y
+ bounds
.height
- 1);
1009 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1010 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1011 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1016 * Draw many items at once
1019 * The rectangle of the area
1020 * @param timeProvider
1023 * The array items to draw
1025 * The index of the first element to draw
1027 * The width reserved for the names
1029 * Reference to the SWT GC object
1031 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1032 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1033 for (int i
= topIndex
; i
< items
.length
; i
++) {
1034 Item item
= items
[i
];
1035 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1037 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1043 * @param item the item to draw
1044 * @param bounds the container rectangle
1045 * @param i the item index
1046 * @param nameSpace the name space
1049 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1050 ITimeGraphEntry entry
= item
._trace
;
1051 long time0
= timeProvider
.getTime0();
1052 long time1
= timeProvider
.getTime1();
1053 long selectedTime
= timeProvider
.getSelectedTime();
1055 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1056 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1060 if (! item
._trace
.hasTimeEvents()) {
1061 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1062 nameRect
.width
+= statesRect
.width
;
1063 drawName(item
, nameRect
, gc
);
1065 drawName(item
, nameRect
, gc
);
1067 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1068 if (rect
.isEmpty()) {
1069 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1072 if (time1
<= time0
) {
1073 gc
.setBackground(_colors
.getBkColor(false, false, false));
1074 gc
.fillRectangle(rect
);
1075 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1079 // Initialize _rect1 to same values as enclosing rectangle rect
1080 Rectangle stateRect
= Utils
.clone(rect
);
1081 boolean selected
= item
._selected
;
1082 // K pixels per second
1083 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1085 if (item
._trace
.hasTimeEvents()) {
1086 fillSpace(rect
, gc
, selected
);
1087 // Drawing rectangle is smaller than reserved space
1089 stateRect
.height
-= 6;
1091 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1092 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1095 while (iterator
.hasNext()) {
1096 ITimeEvent event
= iterator
.next();
1097 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1098 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1099 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1100 // event is out of bounds
1103 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1104 stateRect
.x
= Math
.max(rect
.x
, x
);
1105 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1106 if (stateRect
.x
== lastX
) {
1107 stateRect
.width
-= 1;
1108 if (stateRect
.width
> 0) {
1109 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1110 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1116 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1117 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
1120 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1123 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1124 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1125 if (! hasTimeEvents
) {
1126 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1127 gc
.fillRectangle(bounds
);
1128 if (item
._selected
&& _isInFocus
) {
1129 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1130 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1133 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1134 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1135 gc
.fillRectangle(bounds
);
1138 // No name to be drawn
1139 if (_timeProvider
.getNameSpace() == 0) {
1143 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1144 if (item
._hasChildren
) {
1145 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1146 gc
.setBackground(_colors
.getBkColor(false, false, false));
1147 Rectangle rect
= Utils
.clone(bounds
);
1148 rect
.x
+= leftMargin
;
1149 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1150 rect
.width
= EXPAND_SIZE
;
1151 rect
.height
= EXPAND_SIZE
;
1152 gc
.fillRectangle(rect
);
1153 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1154 int midy
= rect
.y
+ rect
.height
/ 2;
1155 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1156 if (!item
._expanded
) {
1157 int midx
= rect
.x
+ rect
.width
/ 2;
1158 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1161 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1163 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1166 int imgHeight
= img
.getImageData().height
;
1167 int imgWidth
= img
.getImageData().width
;
1169 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1170 gc
.drawImage(img
, x
, y
);
1171 leftMargin
+= imgWidth
+ MARGIN
;
1173 String name
= item
._name
;
1174 Point size
= gc
.stringExtent(name
);
1175 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1176 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1178 if (hasTimeEvents
) {
1179 // cut long string with "..."
1180 int width
= bounds
.width
- leftMargin
;
1182 while (size
.x
> width
&& name
.length() > 1) {
1184 name
= name
.substring(0, name
.length() - 1);
1185 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1188 name
+= "..."; //$NON-NLS-1$
1191 Rectangle rect
= Utils
.clone(bounds
);
1192 rect
.x
+= leftMargin
;
1193 rect
.width
-= leftMargin
;
1195 if (rect
.width
> 0) {
1196 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1197 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1198 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1199 leftMargin
+= textWidth
+ MARGIN
;
1202 if (hasTimeEvents
) {
1204 int x
= bounds
.x
+ leftMargin
;
1205 int width
= bounds
.width
- x
;
1206 int midy
= bounds
.y
+ bounds
.height
/ 2;
1207 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1208 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1213 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1214 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1216 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1220 boolean visible
= rect
.width
== 0 ?
false : true;
1223 Color stateColor
= null;
1224 if (colorIdx
< fEventColorMap
.length
) {
1225 stateColor
= fEventColorMap
[colorIdx
];
1227 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1230 timeSelected
= timeSelected
&& selected
;
1232 // modify the color?
1234 // fill all rect area
1235 gc
.setBackground(stateColor
);
1236 gc
.fillRectangle(rect
);
1237 // get the border color?
1238 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1241 if (!timeSelected
) {
1242 // Draw the top and bottom borders i.e. no side borders
1244 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1246 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1249 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1250 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1252 // selected rectangle area is not visible but can be represented
1253 // with a broken vertical line of specified width.
1256 gc.setForeground(stateColor);
1257 int s = gc.getLineStyle();
1258 int w = gc.getLineWidth();
1259 gc.setLineStyle(SWT.LINE_DOT);
1260 gc.setLineWidth(width);
1261 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1262 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1264 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1267 if (!timeSelected) {
1268 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1269 gc.drawPoint(rect.x, rect.y);
1270 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1274 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1277 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1278 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1279 gc
.fillRectangle(rect
);
1280 if (_dragState
== DRAG_ZOOM
) {
1281 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, true));
1282 if (_dragX0
< _dragX
) {
1283 gc
.fillRectangle(new Rectangle(_dragX0
, rect
.y
, _dragX
- _dragX0
, rect
.height
));
1284 } else if (_dragX0
> _dragX
) {
1285 gc
.fillRectangle(new Rectangle(_dragX
, rect
.y
, _dragX0
- _dragX
, rect
.height
));
1289 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1290 int midy
= rect
.y
+ rect
.height
/ 2;
1291 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1295 public void keyTraversed(TraverseEvent e
) {
1296 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1302 public void keyPressed(KeyEvent e
) {
1304 if (_data
._expandedItems
.length
== 0) {
1307 if (SWT
.HOME
== e
.keyCode
) {
1309 } else if (SWT
.END
== e
.keyCode
) {
1310 idx
= _data
._expandedItems
.length
- 1;
1311 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1312 idx
= getSelectedIndex();
1315 } else if (idx
< _data
._expandedItems
.length
- 1) {
1318 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1319 idx
= getSelectedIndex();
1322 } else if (idx
> 0) {
1325 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1327 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1329 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1330 int page
= countPerPage();
1331 idx
= getSelectedIndex();
1336 if (idx
>= _data
._expandedItems
.length
) {
1337 idx
= _data
._expandedItems
.length
- 1;
1339 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1340 int page
= countPerPage();
1341 idx
= getSelectedIndex();
1349 } else if (SWT
.CR
== e
.keyCode
) {
1350 idx
= getSelectedIndex();
1352 if (_data
._expandedItems
[idx
]._hasChildren
) {
1355 fireDefaultSelection();
1361 selectItem(idx
, false);
1362 fireSelectionChanged();
1367 public void keyReleased(KeyEvent e
) {
1371 public void focusGained(FocusEvent e
) {
1373 if (mouseScrollFilterListener
== null) {
1374 mouseScrollFilterListener
= new Listener() {
1375 // This filter is used to prevent horizontal scrolling of the view
1376 // when the mouse wheel is used to zoom
1378 public void handleEvent(Event event
) {
1382 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1388 public void focusLost(FocusEvent e
) {
1390 if (mouseScrollFilterListener
!= null) {
1391 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1392 mouseScrollFilterListener
= null;
1394 if (DRAG_NONE
!= _dragState
) {
1396 _dragState
= DRAG_NONE
;
1402 * @return If the current view is focused
1404 public boolean isInFocus() {
1409 * Provide the possibility to control the wait cursor externally e.g. data
1410 * requests in progress
1412 * @param waitInd Should we wait indefinitely?
1414 public void waitCursor(boolean waitInd
) {
1415 // Update cursor as indicated
1417 setCursor(_WaitCursor
);
1418 _isWaitCursor
= true;
1421 _isWaitCursor
= false;
1424 // Get ready for next mouse move
1425 _isDragCursor3
= false;
1430 * If the x, y position is over the vertical split line (name to time
1431 * ranges), then change the cursor to a drag cursor to indicate the user the
1432 * possibility of resizing
1438 void updateCursor(int x
, int y
) {
1439 // if Wait cursor not active, check for the need to change to a drag
1441 if (_isWaitCursor
== false) {
1442 boolean isSplitLine
= isOverSplitLine(x
);
1443 // No dragcursor is name space is fixed to zero
1444 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1445 setCursor(_dragCursor3
);
1446 _isDragCursor3
= true;
1447 } else if (!isSplitLine
&& _isDragCursor3
) {
1449 _isDragCursor3
= false;
1455 public void mouseMove(MouseEvent e
) {
1456 if (null == _timeProvider
) {
1459 Point size
= getCtrlSize();
1460 if (DRAG_TRACE_ITEM
== _dragState
) {
1461 int nameWidth
= _timeProvider
.getNameSpace();
1462 int x
= e
.x
- nameWidth
;
1463 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1465 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1466 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1467 long time1
= _time1bak
- timeDelta
;
1468 long maxTime
= _timeProvider
.getMaxTime();
1469 if (time1
> maxTime
) {
1472 long time0
= time1
- (_time1bak
- _time0bak
);
1473 if (time0
< _timeProvider
.getMinTime()) {
1474 time0
= _timeProvider
.getMinTime();
1475 time1
= time0
+ (_time1bak
- _time0bak
);
1477 _timeProvider
.setStartFinishTime(time0
, time1
);
1479 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1481 _timeProvider
.setNameSpace(e
.x
);
1482 } else if (DRAG_ZOOM
== _dragState
) {
1483 _dragX
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1485 } else if (DRAG_NONE
== _dragState
) {
1486 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1487 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1490 _mouseOverSplitLine
= mouseOverSplitLine
;
1492 updateCursor(e
.x
, e
.y
);
1496 public void mouseDoubleClick(MouseEvent e
) {
1497 if (null == _timeProvider
) {
1500 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1501 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1502 _timeProvider
.setNameSpace(_idealNameSpace
);
1503 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1504 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1507 _mouseOverSplitLine
= mouseOverSplitLine
;
1510 int idx
= getItemIndexAtY(e
.y
);
1512 selectItem(idx
, false);
1513 fireDefaultSelection();
1519 public void mouseDown(MouseEvent e
) {
1520 if (_dragState
!= DRAG_NONE
|| null == _timeProvider
) {
1524 if (1 == e
.button
) {
1525 int nameSpace
= _timeProvider
.getNameSpace();
1526 if (nameSpace
!= 0) {
1527 if (isOverSplitLine(e
.x
)) {
1528 _dragState
= DRAG_SPLIT_LINE
;
1529 _dragX
= _dragX0
= e
.x
;
1530 _time0bak
= _timeProvider
.getTime0();
1531 _time1bak
= _timeProvider
.getTime1();
1537 idx
= getItemIndexAtY(e
.y
);
1539 Item item
= _data
._expandedItems
[idx
];
1540 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1543 long hitTime
= getTimeAtX(e
.x
);
1545 // _timeProvider.setSelectedTimeInt(hitTime, false);
1547 _dragState
= DRAG_TRACE_ITEM
;
1548 _dragX
= _dragX0
= e
.x
- nameSpace
;
1549 _time0bak
= _timeProvider
.getTime0();
1550 _time1bak
= _timeProvider
.getTime1();
1553 selectItem(idx
, false);
1554 fireSelectionChanged();
1556 selectItem(idx
, false); // clear selection
1558 fireSelectionChanged();
1560 } else if (3 == e
.button
) {
1561 if (_timeProvider
.getTime0() == _timeProvider
.getTime1() || getCtrlSize().x
- _timeProvider
.getNameSpace() <= 0) {
1565 _dragX
= _dragX0
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1566 _dragState
= DRAG_ZOOM
;
1571 public void mouseUp(MouseEvent e
) {
1572 if (DRAG_NONE
!= _dragState
) {
1574 if (e
.button
== 1 && DRAG_TRACE_ITEM
== _dragState
) {
1575 // Notify time provider to check the need for listener
1577 _timeProvider
.notifyStartFinishTime();
1578 if (_dragX
== _dragX0
) { // click without drag
1579 long time
= getTimeAtX(e
.x
);
1580 _timeProvider
.setSelectedTimeNotify(time
, false);
1582 _dragState
= DRAG_NONE
;
1583 } else if (e
.button
== 1 && DRAG_SPLIT_LINE
== _dragState
) {
1585 _dragState
= DRAG_NONE
;
1586 } else if (e
.button
== 3 && DRAG_ZOOM
== _dragState
) {
1587 Point size
= getCtrlSize();
1588 int nameWidth
= _timeProvider
.getNameSpace();
1589 int x
= e
.x
- nameWidth
;
1590 if (Math
.max(_dragX
, _dragX0
) > nameWidth
&& _dragX
!= _dragX0
) {
1591 long time0
= getTimeAtX(_dragX0
);
1592 long time1
= getTimeAtX(_dragX
);
1593 if (time0
< time1
) {
1594 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1596 _timeProvider
.setStartFinishTimeNotify(time1
, time0
);
1601 _dragState
= DRAG_NONE
;
1607 public void mouseEnter(MouseEvent e
) {
1611 public void mouseExit(MouseEvent e
) {
1612 if (_mouseOverSplitLine
) {
1613 _mouseOverSplitLine
= false;
1619 public void mouseHover(MouseEvent e
) {
1623 public void mouseScrolled(MouseEvent e
) {
1624 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1627 boolean zoomScroll
= false;
1628 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1629 Point parentSize
= getParent().getSize();
1630 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1631 // over the parent control
1632 if (e
.x
> getCtrlSize().x
) {
1633 // over the horizontal scroll bar
1635 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1636 // over the name space
1642 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1645 } else if (e
.count
< 0) {
1649 setTopIndex(getTopIndex() - e
.count
);
1654 public void controlMoved(ControlEvent e
) {
1658 public void controlResized(ControlEvent e
) {
1663 public void widgetDefaultSelected(SelectionEvent e
) {
1667 public void widgetSelected(SelectionEvent e
) {
1668 if (e
.widget
== getVerticalBar()) {
1669 setTopIndex(getVerticalBar().getSelection());
1670 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1671 int start
= getHorizontalBar().getSelection();
1672 long time0
= _timeProvider
.getTime0();
1673 long time1
= _timeProvider
.getTime1();
1674 long timeMin
= _timeProvider
.getMinTime();
1675 long timeMax
= _timeProvider
.getMaxTime();
1676 long delta
= timeMax
- timeMin
;
1678 long range
= time1
- time0
;
1679 // _timeRangeFixed = true;
1680 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1681 time1
= time0
+ range
;
1683 // TODO: Follow-up with Bug 310310
1684 // In Linux SWT.DRAG is the only value received
1685 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1686 if (e
.detail
== SWT
.DRAG
) {
1687 _timeProvider
.setStartFinishTime(time0
, time1
);
1689 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1695 * @return The current visibility of the vertical scroll bar
1697 public boolean isVisibleVerticalScroll() {
1698 return _visibleVerticalScroll
;
1702 public int getBorderWidth() {
1703 return _borderWidth
;
1707 * Set the border width
1709 * @param borderWidth
1712 public void setBorderWidth(int borderWidth
) {
1713 this._borderWidth
= borderWidth
;
1717 * @return The current height of the header row
1719 public int getHeaderHeight() {
1720 return _headerHeight
;
1724 * Set the height of the header row
1726 * @param headerHeight
1729 public void setHeaderHeight(int headerHeight
) {
1730 this._headerHeight
= headerHeight
;
1734 * @return The height of regular item rows
1736 public int getItemHeight() {
1741 * Set the height of regular itew rows
1746 public void setItemHeight(int rowHeight
) {
1747 this._itemHeight
= rowHeight
;
1751 * Set the minimum item width
1753 * @param width The minimum width
1755 public void setMinimumItemWidth(int width
) {
1756 this._minimumItemWidth
= width
;
1760 * @return The minimum item width
1762 public int getMinimumItemWidth() {
1763 return _minimumItemWidth
;
1767 * @return The entries that are currently filtered out
1769 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1770 return _data
.getFilteredOut();
1775 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1776 if (listener
!= null) {
1777 if (!_selectionChangedListeners
.contains(listener
)) {
1778 _selectionChangedListeners
.add(listener
);
1785 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1786 if (listener
!= null) {
1787 _selectionChangedListeners
.remove(listener
);
1793 public void setSelection(ISelection selection
) {
1794 if (selection
instanceof TimeGraphSelection
) {
1795 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1796 Object ob
= sel
.getFirstElement();
1797 if (ob
instanceof ITimeGraphEntry
) {
1798 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1799 selectItem(trace
, false);
1805 private class ItemData
{
1806 public Item
[] _expandedItems
= new Item
[0];
1807 public Item
[] _items
= new Item
[0];
1808 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1809 private boolean traceFilter
[] = new boolean[0];
1810 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1811 public ITimeGraphPresentationProvider provider
;
1816 Item
findItem(ITimeGraphEntry entry
) {
1817 if (entry
== null) {
1821 for (int i
= 0; i
< _items
.length
; i
++) {
1822 Item item
= _items
[i
];
1823 if (item
._trace
== entry
) {
1831 int findItemIndex(ITimeGraphEntry trace
) {
1832 if (trace
== null) {
1836 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
1837 Item item
= _expandedItems
[i
];
1838 if (item
._trace
== trace
) {
1846 public void refreshData() {
1847 List
<Item
> itemList
= new ArrayList
<Item
>();
1848 filteredOut
.clear();
1849 for (int i
= 0; i
< _traces
.length
; i
++) {
1850 ITimeGraphEntry entry
= _traces
[i
];
1851 refreshData(itemList
, null, 0, entry
);
1853 _items
= itemList
.toArray(new Item
[0]);
1854 updateExpandedItems();
1857 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
1858 Item item
= new Item(entry
, entry
.getName(), level
);
1859 if (parent
!= null) {
1860 parent
.children
.add(item
);
1862 item
.itemHeight
= provider
.getItemHeight(entry
);
1864 if (entry
.hasChildren()) {
1865 item
._expanded
= true;
1866 item
._hasChildren
= true;
1867 for (ITimeGraphEntry child
: entry
.getChildren()) {
1868 refreshData(itemList
, item
, level
+ 1, child
);
1873 public void updateExpandedItems() {
1874 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
1875 for (int i
= 0; i
< _traces
.length
; i
++) {
1876 ITimeGraphEntry entry
= _traces
[i
];
1877 Item item
= findItem(entry
);
1878 refreshExpanded(expandedItemList
, item
);
1880 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
1883 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
1884 expandedItemList
.add(item
);
1885 if (item
._hasChildren
&& item
._expanded
) {
1886 for (Item child
: item
.children
) {
1887 refreshExpanded(expandedItemList
, child
);
1892 public void refreshData(ITimeGraphEntry traces
[]) {
1893 if (traces
== null || traces
.length
== 0) {
1895 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
1896 traceFilter
= new boolean[traces
.length
];
1897 java
.util
.Arrays
.fill(traceFilter
, true);
1904 public ITimeGraphEntry
[] getTraces() {
1908 public boolean[] getTraceFilter() {
1912 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1917 private class Item
{
1918 public boolean _expanded
;
1919 public boolean _selected
;
1920 public boolean _hasChildren
;
1921 public int itemHeight
;
1923 public List
<Item
> children
;
1924 public String _name
;
1925 public ITimeGraphEntry _trace
;
1927 public Item(ITimeGraphEntry trace
, String name
, int level
) {
1928 this._trace
= trace
;
1931 this.children
= new ArrayList
<Item
>();
1935 public String
toString() {