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 public static final boolean DEFAULT_DRAW_THREAD_JOIN
= true;
76 public static final boolean DEFAULT_DRAW_THREAD_WAIT
= true;
77 public static final boolean DEFAULT_DRAW_THREAD_RELEASE
= true;
78 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
79 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
81 private static final double zoomCoeff
= 1.5;
83 private ITimeDataProvider _timeProvider
;
84 private boolean _isInFocus
= false;
85 private boolean _isDragCursor3
= false;
86 private boolean _isWaitCursor
= true;
87 private boolean _mouseOverSplitLine
= false;
88 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
89 private int _minimumItemWidth
= 0;
90 private int _topIndex
= 0;
91 private int _dragState
= DRAG_NONE
;
92 private int _dragX0
= 0;
93 private int _dragX
= 0;
94 private int _idealNameSpace
= 0;
95 // private double _timeStep = 10000000;
96 private long _time0bak
;
97 private long _time1bak
;
98 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
99 private ItemData _data
= null;
100 private List
<SelectionListener
> _selectionListeners
;
101 private final List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
102 private final List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
103 private final Cursor _dragCursor3
;
104 private final Cursor _WaitCursor
;
106 // Vertical formatting formatting for the state control view
107 private final boolean _visibleVerticalScroll
= true;
108 private int _borderWidth
= 0;
109 private int _headerHeight
= 0;
111 private Listener mouseScrollFilterListener
;
113 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
114 protected Color
[] fEventColorMap
= null;
116 private MouseScrollNotifier fMouseScrollNotifier
;
117 private final Object fMouseScrollNotifierLock
= new Object();
118 private class MouseScrollNotifier
extends Thread
{
119 private final static long DELAY
= 400L;
120 private final static long POLLING_INTERVAL
= 10L;
121 private long fLastScrollTime
= Long
.MAX_VALUE
;
125 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
127 Thread
.sleep(POLLING_INTERVAL
);
128 } catch (Exception e
) {
132 if (!isInterrupted()) {
133 Display
.getDefault().asyncExec(new Runnable() {
139 _timeProvider
.notifyStartFinishTime();
143 synchronized (fMouseScrollNotifierLock
) {
144 fMouseScrollNotifier
= null;
148 public void mouseScrolled() {
149 fLastScrollTime
= System
.currentTimeMillis();
154 * Standard constructor
157 * The parent composite object
159 * The color scheme to use
161 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
163 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
165 _data
= new ItemData();
167 addFocusListener(this);
168 addMouseListener(this);
169 addMouseMoveListener(this);
170 addMouseTrackListener(this);
171 addMouseWheelListener(this);
172 addTraverseListener(this);
173 addKeyListener(this);
174 addControlListener(this);
175 ScrollBar scrollHor
= getHorizontalBar();
177 if (scrollHor
!= null) {
178 scrollHor
.addSelectionListener(this);
181 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
182 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
186 public void dispose() {
188 _dragCursor3
.dispose();
189 _WaitCursor
.dispose();
190 fResourceManager
.dispose();
194 * Sets the timegraph provider used by this timegraph viewer.
196 * @param timeGraphProvider the timegraph provider
198 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
199 fTimeGraphProvider
= timeGraphProvider
;
200 _data
.provider
= timeGraphProvider
;
202 if (fEventColorMap
!= null) {
203 for (Color color
: fEventColorMap
) {
204 fResourceManager
.destroyColor(color
.getRGB());
207 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
208 if (stateItems
!= null) {
209 fEventColorMap
= new Color
[stateItems
.length
];
210 for (int i
= 0; i
< stateItems
.length
; i
++) {
211 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
214 fEventColorMap
= new Color
[] { };
219 * Assign the given time provider
221 * @param timeProvider
224 public void setTimeProvider(ITimeDataProvider timeProvider
) {
225 _timeProvider
= timeProvider
;
231 * Add a selection listener
234 * The listener to add
236 public void addSelectionListener(SelectionListener listener
) {
237 if (listener
== null) {
238 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
240 if (null == _selectionListeners
) {
241 _selectionListeners
= new ArrayList
<SelectionListener
>();
243 _selectionListeners
.add(listener
);
247 * Remove a selection listener
250 * The listener to remove
252 public void removeSelectionListener(SelectionListener listener
) {
253 if (null != _selectionListeners
) {
254 _selectionListeners
.remove(listener
);
259 * Selection changed callback
261 public void fireSelectionChanged() {
262 if (null != _selectionListeners
) {
263 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
264 while (it
.hasNext()) {
265 SelectionListener listener
= it
.next();
266 listener
.widgetSelected(null);
272 * Default selection callback
274 public void fireDefaultSelection() {
275 if (null != _selectionListeners
) {
276 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
277 while (it
.hasNext()) {
278 SelectionListener listener
= it
.next();
279 listener
.widgetDefaultSelected(null);
285 * Get the traces in the model
287 * @return The array of traces
289 public ITimeGraphEntry
[] getTraces() {
290 return _data
.getTraces();
294 * Get the on/off trace filters
296 * @return The array of filters
298 public boolean[] getTraceFilter() {
299 return _data
.getTraceFilter();
303 * Refresh the data for the thing
305 public void refreshData() {
312 * Refresh data for the given traces
315 * The traces to refresh
317 public void refreshData(ITimeGraphEntry
[] traces
) {
318 _data
.refreshData(traces
);
324 * Adjust the scoll bars
326 public void adjustScrolls() {
327 if (null == _timeProvider
) {
328 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
334 long time0
= _timeProvider
.getTime0();
335 long time1
= _timeProvider
.getTime1();
337 long timeMin
= _timeProvider
.getMinTime();
338 long timeMax
= _timeProvider
.getMaxTime();
340 long delta
= timeMax
- timeMin
;
343 int thumb
= H_SCROLLBAR_MAX
;
346 // Thumb size (page size)
347 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
348 // At the beginning of visible window
349 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
352 // position, minimum, maximum, thumb size, increment (half page)t, page
353 // increment size (full page)
354 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
357 boolean ensureVisibleItem(int idx
, boolean redraw
) {
358 boolean changed
= false;
360 for (idx
= 0; idx
< _data
._expandedItems
.length
; idx
++) {
361 if (_data
._expandedItems
[idx
]._selected
) {
366 if (idx
>= _data
._expandedItems
.length
) {
369 if (idx
< _topIndex
) {
371 //FIXME:getVerticalBar().setSelection(_topItem);
377 int page
= countPerPage();
378 if (idx
>= _topIndex
+ page
) {
379 setTopIndex(idx
- page
+ 1);
380 //FIXME:getVerticalBar().setSelection(_topItem);
391 * Assign the given index as the top one
396 public void setTopIndex(int idx
) {
397 idx
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
398 idx
= Math
.max(0, idx
);
404 * Set the expanded state of a given entry
409 * True if expanded, false if collapsed
411 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
412 Item item
= _data
.findItem(entry
);
413 if (item
!= null && item
._expanded
!= expanded
) {
414 item
._expanded
= expanded
;
415 _data
.updateExpandedItems();
421 * Collapses all nodes of the viewer's tree, starting with the root.
425 public void collapseAll() {
426 for (Item item
: _data
._items
) {
427 item
._expanded
= false;
429 _data
.updateExpandedItems();
434 * Expands all nodes of the viewer's tree, starting with the root.
438 public void expandAll() {
439 for (Item item
: _data
._items
) {
440 item
._expanded
= true;
442 _data
.updateExpandedItems();
447 * Add a tree listener
450 * The listener to add
452 public void addTreeListener(ITimeGraphTreeListener listener
) {
453 if (!_treeListeners
.contains(listener
)) {
454 _treeListeners
.add(listener
);
459 * Remove a tree listener
462 * The listener to remove
464 public void removeTreeListener(ITimeGraphTreeListener listener
) {
465 if (_treeListeners
.contains(listener
)) {
466 _treeListeners
.remove(listener
);
471 * Tree event callback
476 * The expanded state (true for expanded, false for collapsed)
478 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
479 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
480 for (ITimeGraphTreeListener listener
: _treeListeners
) {
482 listener
.treeExpanded(event
);
484 listener
.treeCollapsed(event
);
490 public ISelection
getSelection() {
491 TimeGraphSelection sel
= new TimeGraphSelection();
492 ITimeGraphEntry trace
= getSelectedTrace();
493 if (null != trace
&& null != _timeProvider
) {
494 long selectedTime
= _timeProvider
.getSelectedTime();
495 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
506 * Get the selection object
508 * @return The selection
510 public ISelection
getSelectionTrace() {
511 TimeGraphSelection sel
= new TimeGraphSelection();
512 ITimeGraphEntry trace
= getSelectedTrace();
520 * Enable/disable one of the traces in the model
523 * 1 to enable it, -1 to disable. The method returns immediately
524 * if another value is used.
526 public void selectTrace(int n
) {
527 if ((n
!= 1) && (n
!= -1)) {
531 boolean changed
= false;
532 int lastSelection
= -1;
533 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
534 Item item
= _data
._expandedItems
[i
];
535 if (item
._selected
) {
537 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
538 item
._selected
= false;
539 item
= _data
._expandedItems
[i
+ 1];
540 item
._selected
= true;
542 } else if ((-1 == n
) && (i
> 0)) {
543 item
._selected
= false;
544 item
= _data
._expandedItems
[i
- 1];
545 item
._selected
= true;
552 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
553 Item item
= _data
._expandedItems
[0];
554 item
._selected
= true;
559 ensureVisibleItem(-1, false);
561 fireSelectionChanged();
569 * 1 for next event, -1 for previous event
571 public void selectEvent(int n
) {
572 if (null == _timeProvider
) {
575 ITimeGraphEntry trace
= getSelectedTrace();
579 long selectedTime
= _timeProvider
.getSelectedTime();
580 long endTime
= _timeProvider
.getEndTime();
581 ITimeEvent nextEvent
;
582 if (-1 == n
&& selectedTime
> endTime
) {
583 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
585 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
587 if (null == nextEvent
&& -1 == n
) {
588 nextEvent
= Utils
.getFirstEvent(trace
);
590 if (null != nextEvent
) {
591 long nextTime
= nextEvent
.getTime();
592 // If last event detected e.g. going back or not moving to a next
594 if (nextTime
<= selectedTime
&& n
== 1) {
595 // Select to the end of this last event
596 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
597 // but not beyond the end of the trace
598 if (nextTime
> endTime
) {
601 } else if (n
== -1) {
602 // for previous event go to its end time unless we were already there
603 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
604 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
607 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
608 fireSelectionChanged();
610 _timeProvider
.setSelectedTimeNotify(endTime
, true);
611 fireSelectionChanged();
616 * Select the next event
618 public void selectNextEvent() {
620 // Notify if visible time window has been adjusted
621 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
625 * Select the previous event
627 public void selectPrevEvent() {
629 // Notify if visible time window has been adjusted
630 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
634 * Select the next trace
636 public void selectNextTrace() {
641 * Select the previous trace
643 public void selectPrevTrace() {
648 * Zoom based on mouse cursor location with mouse scrolling
650 * @param zoomIn true to zoom in, false to zoom out
652 public void zoom(boolean zoomIn
) {
653 int globalX
= getDisplay().getCursorLocation().x
;
654 Point p
= toControl(globalX
, 0);
655 int nameSpace
= _timeProvider
.getNameSpace();
656 int timeSpace
= _timeProvider
.getTimeSpace();
657 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
658 long time0
= _timeProvider
.getTime0();
659 long time1
= _timeProvider
.getTime1();
660 long interval
= time1
- time0
;
663 } // to allow getting out of single point interval
666 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
668 newInterval
= (long) Math
.ceil(interval
* 1.25);
670 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
671 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
672 long newTime1
= newTime0
+ newInterval
;
673 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
674 synchronized (fMouseScrollNotifierLock
) {
675 if (fMouseScrollNotifier
== null) {
676 fMouseScrollNotifier
= new MouseScrollNotifier();
677 fMouseScrollNotifier
.start();
679 fMouseScrollNotifier
.mouseScrolled();
684 * zoom in using single click
686 public void zoomIn() {
687 long _time0
= _timeProvider
.getTime0();
688 long _time1
= _timeProvider
.getTime1();
689 long _range
= _time1
- _time0
;
690 long selTime
= _timeProvider
.getSelectedTime();
691 if (selTime
<= _time0
|| selTime
>= _time1
) {
692 selTime
= (_time0
+ _time1
) / 2;
694 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
695 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
697 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
699 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
700 // + time1 + " inaccuracy:" + inaccuracy);
702 if (inaccuracy
> 0 && inaccuracy
< 100) {
703 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
707 long m
= _timeProvider
.getMinTimeInterval();
708 if ((time1
- time0
) < m
) {
709 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
713 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
717 * zoom out using single click
719 public void zoomOut() {
720 long _time0
= _timeProvider
.getTime0();
721 long _time1
= _timeProvider
.getTime1();
722 long selTime
= _timeProvider
.getSelectedTime();
723 if (selTime
<= _time0
|| selTime
>= _time1
) {
724 selTime
= (_time0
+ _time1
) / 2;
726 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
727 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
729 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
730 if (inaccuracy
> 0 && inaccuracy
< 100) {
731 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
735 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
739 * Return the currently selected trace
741 * @return The entry matching the trace
743 public ITimeGraphEntry
getSelectedTrace() {
744 ITimeGraphEntry trace
= null;
745 int idx
= getSelectedIndex();
747 trace
= _data
._expandedItems
[idx
]._trace
;
753 * Retrieve the index of the currently selected item
757 public int getSelectedIndex() {
759 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
760 Item item
= _data
._expandedItems
[i
];
761 if (item
._selected
) {
769 boolean toggle(int idx
) {
770 boolean toggled
= false;
771 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
772 Item item
= _data
._expandedItems
[idx
];
773 if (item
._hasChildren
) {
774 item
._expanded
= !item
._expanded
;
775 _data
.updateExpandedItems();
779 fireTreeEvent(item
._trace
, item
._expanded
);
785 int getItemIndexAtY(int y
) {
789 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
791 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
792 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
799 int idx
= y
/ _itemHeight
;
801 if (idx
< _data
._expandedItems
.length
) {
807 boolean isOverSplitLine(int x
) {
808 if (x
< 0 || null == _timeProvider
) {
812 int nameWidth
= _timeProvider
.getNameSpace();
813 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
819 ITimeGraphEntry
getEntry(Point pt
) {
820 int idx
= getItemIndexAtY(pt
.y
);
821 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
824 long getTimeAtX(int x
) {
825 if (null == _timeProvider
) {
829 Point size
= getCtrlSize();
830 long time0
= _timeProvider
.getTime0();
831 long time1
= _timeProvider
.getTime1();
832 int nameWidth
= _timeProvider
.getNameSpace();
834 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
835 if (x
>= 0 && size
.x
>= nameWidth
) {
836 if (time1
- time0
> timeWidth
) {
837 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
838 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
840 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
841 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
847 void selectItem(int idx
, boolean addSelection
) {
848 boolean changed
= false;
850 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
851 Item item
= _data
._expandedItems
[idx
];
852 changed
= (item
._selected
== false);
853 item
._selected
= true;
856 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
857 Item item
= _data
._expandedItems
[i
];
858 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
861 item
._selected
= i
== idx
;
864 changed
|= ensureVisibleItem(idx
, true);
871 * Callback for item selection
874 * The entry matching the trace
875 * @param addSelection
876 * If the selection is added or removed
878 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
879 int idx
= _data
.findItemIndex(trace
);
880 selectItem(idx
, addSelection
);
884 * Retrieve the number of entries shown per page.
888 public int countPerPage() {
889 int height
= getCtrlSize().y
;
891 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
893 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
894 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
895 if (ySum
>= height
) {
900 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
901 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
902 if (ySum
>= height
) {
910 count
= height
/ _itemHeight
;
916 * Get the index of the top element
920 public int getTopIndex() {
925 * Get the number of expanded items
927 * @return The count of expanded items
929 public int getExpandedElementCount() {
930 return _data
._expandedItems
.length
;
934 * Get an array of all expanded elements
936 * @return The expanded elements
938 public ITimeGraphEntry
[] getExpandedElements() {
939 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
940 for (Item item
: _data
._expandedItems
) {
941 elements
.add(item
._trace
);
943 return elements
.toArray(new ITimeGraphEntry
[0]);
946 Point
getCtrlSize() {
947 Point size
= getSize();
948 if (getHorizontalBar().isVisible()) {
949 size
.y
-= getHorizontalBar().getSize().y
;
954 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
956 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
957 int width
= nameWidth
;
958 int height
= _itemHeight
;
959 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
961 for (int i
= _topIndex
; i
< idx
; i
++) {
962 ySum
+= _data
._expandedItems
[i
].itemHeight
;
965 height
= _data
._expandedItems
[idx
].itemHeight
;
967 return new Rectangle(x
, y
, width
, height
);
970 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
971 int x
= bound
.x
+ nameWidth
;
972 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
973 int width
= bound
.width
- x
;
974 int height
= _itemHeight
;
975 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
977 for (int i
= _topIndex
; i
< idx
; i
++) {
978 ySum
+= _data
._expandedItems
[i
].itemHeight
;
981 height
= _data
._expandedItems
[idx
].itemHeight
;
983 return new Rectangle(x
, y
, width
, height
);
987 void paint(Rectangle bounds
, PaintEvent e
) {
989 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
990 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
992 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
997 int nameSpace
= _timeProvider
.getNameSpace();
999 // draw empty name space background
1000 gc
.setBackground(_colors
.getBkColor(false, false, true));
1001 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1003 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1005 // draw selected time
1006 long time0
= _timeProvider
.getTime0();
1007 long time1
= _timeProvider
.getTime1();
1008 long selectedTime
= _timeProvider
.getSelectedTime();
1009 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1010 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1011 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1012 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1013 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1016 // draw drag line, no line if name space is 0.
1017 if (DRAG_SPLIT_LINE
== _dragState
) {
1018 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1019 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1020 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1021 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1022 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1027 * Draw many items at once
1030 * The rectangle of the area
1031 * @param timeProvider
1034 * The array items to draw
1036 * The index of the first element to draw
1038 * The width reserved for the names
1040 * Reference to the SWT GC object
1042 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1043 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1044 for (int i
= topIndex
; i
< items
.length
; i
++) {
1045 Item item
= items
[i
];
1046 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1048 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1054 * @param item the item to draw
1055 * @param bounds the container rectangle
1056 * @param i the item index
1057 * @param nameSpace the name space
1060 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1061 ITimeGraphEntry entry
= item
._trace
;
1062 long time0
= timeProvider
.getTime0();
1063 long time1
= timeProvider
.getTime1();
1064 long selectedTime
= timeProvider
.getSelectedTime();
1066 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1067 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1071 if (! item
._trace
.hasTimeEvents()) {
1072 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1073 nameRect
.width
+= statesRect
.width
;
1074 drawName(item
, nameRect
, gc
);
1076 drawName(item
, nameRect
, gc
);
1078 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1079 if (rect
.isEmpty()) {
1080 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1083 if (time1
<= time0
) {
1084 gc
.setBackground(_colors
.getBkColor(false, false, false));
1085 gc
.fillRectangle(rect
);
1086 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1090 // Initialize _rect1 to same values as enclosing rectangle rect
1091 Rectangle stateRect
= Utils
.clone(rect
);
1092 boolean selected
= item
._selected
;
1093 // K pixels per second
1094 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1096 if (item
._trace
.hasTimeEvents()) {
1097 fillSpace(rect
, gc
, selected
);
1098 // Drawing rectangle is smaller than reserved space
1100 stateRect
.height
-= 6;
1102 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1103 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1106 while (iterator
.hasNext()) {
1107 ITimeEvent event
= iterator
.next();
1108 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1109 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1110 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1111 // event is out of bounds
1114 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1115 stateRect
.x
= Math
.max(rect
.x
, x
);
1116 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1117 if (stateRect
.x
== lastX
) {
1118 stateRect
.width
-= 1;
1119 if (stateRect
.width
> 0) {
1120 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1121 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1127 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1128 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
1131 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1134 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1135 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1136 if (! hasTimeEvents
) {
1137 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1138 gc
.fillRectangle(bounds
);
1139 if (item
._selected
&& _isInFocus
) {
1140 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1141 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1144 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1145 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1146 gc
.fillRectangle(bounds
);
1149 // No name to be drawn
1150 if (_timeProvider
.getNameSpace() == 0) {
1154 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1155 if (item
._hasChildren
) {
1156 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1157 gc
.setBackground(_colors
.getBkColor(false, false, false));
1158 Rectangle rect
= Utils
.clone(bounds
);
1159 rect
.x
+= leftMargin
;
1160 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1161 rect
.width
= EXPAND_SIZE
;
1162 rect
.height
= EXPAND_SIZE
;
1163 gc
.fillRectangle(rect
);
1164 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1165 int midy
= rect
.y
+ rect
.height
/ 2;
1166 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1167 if (!item
._expanded
) {
1168 int midx
= rect
.x
+ rect
.width
/ 2;
1169 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1172 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1174 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1177 int imgHeight
= img
.getImageData().height
;
1178 int imgWidth
= img
.getImageData().width
;
1180 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1181 gc
.drawImage(img
, x
, y
);
1182 leftMargin
+= imgWidth
+ MARGIN
;
1184 String name
= item
._name
;
1185 Point size
= gc
.stringExtent(name
);
1186 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1187 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1189 if (hasTimeEvents
) {
1190 // cut long string with "..."
1191 int width
= bounds
.width
- leftMargin
;
1193 while (size
.x
> width
&& name
.length() > 1) {
1195 name
= name
.substring(0, name
.length() - 1);
1196 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1199 name
+= "..."; //$NON-NLS-1$
1202 Rectangle rect
= Utils
.clone(bounds
);
1203 rect
.x
+= leftMargin
;
1204 rect
.width
-= leftMargin
;
1206 if (rect
.width
> 0) {
1207 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1208 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1209 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1210 leftMargin
+= textWidth
+ MARGIN
;
1213 if (hasTimeEvents
) {
1215 int x
= bounds
.x
+ leftMargin
;
1216 int width
= bounds
.width
- x
;
1217 int midy
= bounds
.y
+ bounds
.height
/ 2;
1218 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1219 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1224 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1225 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1227 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1231 boolean visible
= rect
.width
== 0 ?
false : true;
1234 Color stateColor
= null;
1235 if (colorIdx
< fEventColorMap
.length
) {
1236 stateColor
= fEventColorMap
[colorIdx
];
1238 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1241 timeSelected
= timeSelected
&& selected
;
1243 // modify the color?
1245 // fill all rect area
1246 gc
.setBackground(stateColor
);
1247 gc
.fillRectangle(rect
);
1248 // get the border color?
1249 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1252 if (!timeSelected
) {
1253 // Draw the top and bottom borders i.e. no side borders
1255 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1257 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1260 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1261 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1263 // selected rectangle area is not visible but can be represented
1264 // with a broken vertical line of specified width.
1267 gc.setForeground(stateColor);
1268 int s = gc.getLineStyle();
1269 int w = gc.getLineWidth();
1270 gc.setLineStyle(SWT.LINE_DOT);
1271 gc.setLineWidth(width);
1272 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1273 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1275 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1278 if (!timeSelected) {
1279 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1280 gc.drawPoint(rect.x, rect.y);
1281 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1285 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1288 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1289 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1290 gc
.fillRectangle(rect
);
1292 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1293 int midy
= rect
.y
+ rect
.height
/ 2;
1294 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1298 public void keyTraversed(TraverseEvent e
) {
1299 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1305 public void keyPressed(KeyEvent e
) {
1307 if (_data
._expandedItems
.length
== 0) {
1310 if (SWT
.HOME
== e
.keyCode
) {
1312 } else if (SWT
.END
== e
.keyCode
) {
1313 idx
= _data
._expandedItems
.length
- 1;
1314 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1315 idx
= getSelectedIndex();
1318 } else if (idx
< _data
._expandedItems
.length
- 1) {
1321 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1322 idx
= getSelectedIndex();
1325 } else if (idx
> 0) {
1328 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1330 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1332 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1333 int page
= countPerPage();
1334 idx
= getSelectedIndex();
1339 if (idx
>= _data
._expandedItems
.length
) {
1340 idx
= _data
._expandedItems
.length
- 1;
1342 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1343 int page
= countPerPage();
1344 idx
= getSelectedIndex();
1352 } else if (SWT
.CR
== e
.keyCode
) {
1353 idx
= getSelectedIndex();
1355 if (_data
._expandedItems
[idx
]._hasChildren
) {
1358 fireDefaultSelection();
1364 selectItem(idx
, false);
1365 fireSelectionChanged();
1370 public void keyReleased(KeyEvent e
) {
1374 public void focusGained(FocusEvent e
) {
1376 if (mouseScrollFilterListener
== null) {
1377 mouseScrollFilterListener
= new Listener() {
1378 // This filter is used to prevent horizontal scrolling of the view
1379 // when the mouse wheel is used to zoom
1381 public void handleEvent(Event event
) {
1385 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1391 public void focusLost(FocusEvent e
) {
1393 if (mouseScrollFilterListener
!= null) {
1394 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1395 mouseScrollFilterListener
= null;
1397 if (DRAG_NONE
!= _dragState
) {
1399 _dragState
= DRAG_NONE
;
1405 * @return If the current view is focused
1407 public boolean isInFocus() {
1412 * Provide the possibility to control the wait cursor externally e.g. data
1413 * requests in progress
1415 * @param waitInd Should we wait indefinitely?
1417 public void waitCursor(boolean waitInd
) {
1418 // Update cursor as indicated
1420 setCursor(_WaitCursor
);
1421 _isWaitCursor
= true;
1424 _isWaitCursor
= false;
1427 // Get ready for next mouse move
1428 _isDragCursor3
= false;
1433 * If the x, y position is over the vertical split line (name to time
1434 * ranges), then change the cursor to a drag cursor to indicate the user the
1435 * possibility of resizing
1441 void updateCursor(int x
, int y
) {
1442 // if Wait cursor not active, check for the need to change to a drag
1444 if (_isWaitCursor
== false) {
1445 boolean isSplitLine
= isOverSplitLine(x
);
1446 // No dragcursor is name space is fixed to zero
1447 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1448 setCursor(_dragCursor3
);
1449 _isDragCursor3
= true;
1450 } else if (!isSplitLine
&& _isDragCursor3
) {
1452 _isDragCursor3
= false;
1458 public void mouseMove(MouseEvent e
) {
1459 if (null == _timeProvider
) {
1462 Point size
= getCtrlSize();
1463 if (DRAG_TRACE_ITEM
== _dragState
) {
1464 int nameWidth
= _timeProvider
.getNameSpace();
1465 int x
= e
.x
- nameWidth
;
1466 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1468 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1469 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1470 long time1
= _time1bak
- timeDelta
;
1471 long maxTime
= _timeProvider
.getMaxTime();
1472 if (time1
> maxTime
) {
1475 long time0
= time1
- (_time1bak
- _time0bak
);
1476 if (time0
< _timeProvider
.getMinTime()) {
1477 time0
= _timeProvider
.getMinTime();
1478 time1
= time0
+ (_time1bak
- _time0bak
);
1480 _timeProvider
.setStartFinishTime(time0
, time1
);
1482 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1484 _timeProvider
.setNameSpace(e
.x
);
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
) {
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 (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();
1564 public void mouseUp(MouseEvent e
) {
1565 if (DRAG_NONE
!= _dragState
) {
1567 if (DRAG_TRACE_ITEM
== _dragState
) {
1568 // Notify time provider to check the need for listener
1570 _timeProvider
.notifyStartFinishTime();
1571 if (_dragX
== _dragX0
) { // click without drag
1572 long time
= getTimeAtX(e
.x
);
1573 _timeProvider
.setSelectedTimeNotify(time
, false);
1575 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1578 _dragState
= DRAG_NONE
;
1583 public void mouseEnter(MouseEvent e
) {
1587 public void mouseExit(MouseEvent e
) {
1588 if (_mouseOverSplitLine
) {
1589 _mouseOverSplitLine
= false;
1595 public void mouseHover(MouseEvent e
) {
1599 public void mouseScrolled(MouseEvent e
) {
1600 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1603 boolean zoomScroll
= false;
1604 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1605 Point parentSize
= getParent().getSize();
1606 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1607 // over the parent control
1608 if (e
.x
> getCtrlSize().x
) {
1609 // over the horizontal scroll bar
1611 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1612 // over the name space
1618 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1621 } else if (e
.count
< 0) {
1625 setTopIndex(getTopIndex() - e
.count
);
1630 public void controlMoved(ControlEvent e
) {
1634 public void controlResized(ControlEvent e
) {
1639 public void widgetDefaultSelected(SelectionEvent e
) {
1643 public void widgetSelected(SelectionEvent e
) {
1644 if (e
.widget
== getVerticalBar()) {
1645 setTopIndex(getVerticalBar().getSelection());
1646 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1647 int start
= getHorizontalBar().getSelection();
1648 long time0
= _timeProvider
.getTime0();
1649 long time1
= _timeProvider
.getTime1();
1650 long timeMin
= _timeProvider
.getMinTime();
1651 long timeMax
= _timeProvider
.getMaxTime();
1652 long delta
= timeMax
- timeMin
;
1654 long range
= time1
- time0
;
1655 // _timeRangeFixed = true;
1656 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1657 time1
= time0
+ range
;
1659 // TODO: Follow-up with Bug 310310
1660 // In Linux SWT.DRAG is the only value received
1661 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1662 if (e
.detail
== SWT
.DRAG
) {
1663 _timeProvider
.setStartFinishTime(time0
, time1
);
1665 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1671 * @return The current visibility of the vertical scroll bar
1673 public boolean isVisibleVerticalScroll() {
1674 return _visibleVerticalScroll
;
1678 public int getBorderWidth() {
1679 return _borderWidth
;
1683 * Set the border width
1685 * @param borderWidth
1688 public void setBorderWidth(int borderWidth
) {
1689 this._borderWidth
= borderWidth
;
1693 * @return The current height of the header row
1695 public int getHeaderHeight() {
1696 return _headerHeight
;
1700 * Set the height of the header row
1702 * @param headerHeight
1705 public void setHeaderHeight(int headerHeight
) {
1706 this._headerHeight
= headerHeight
;
1710 * @return The height of regular item rows
1712 public int getItemHeight() {
1717 * Set the height of regular itew rows
1722 public void setItemHeight(int rowHeight
) {
1723 this._itemHeight
= rowHeight
;
1727 * Set the minimum item width
1729 * @param width The minimum width
1731 public void setMinimumItemWidth(int width
) {
1732 this._minimumItemWidth
= width
;
1736 * @return The minimum item width
1738 public int getMinimumItemWidth() {
1739 return _minimumItemWidth
;
1743 * @return The entries that are currently filtered out
1745 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1746 return _data
.getFilteredOut();
1751 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1752 if (listener
!= null) {
1753 if (!_selectionChangedListeners
.contains(listener
)) {
1754 _selectionChangedListeners
.add(listener
);
1761 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1762 if (listener
!= null) {
1763 _selectionChangedListeners
.remove(listener
);
1769 public void setSelection(ISelection selection
) {
1770 if (selection
instanceof TimeGraphSelection
) {
1771 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1772 Object ob
= sel
.getFirstElement();
1773 if (ob
instanceof ITimeGraphEntry
) {
1774 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1775 selectItem(trace
, false);
1781 private class ItemData
{
1782 public Item
[] _expandedItems
= new Item
[0];
1783 public Item
[] _items
= new Item
[0];
1784 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1785 private boolean traceFilter
[] = new boolean[0];
1786 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1787 public ITimeGraphPresentationProvider provider
;
1792 Item
findItem(ITimeGraphEntry entry
) {
1793 if (entry
== null) {
1797 for (int i
= 0; i
< _items
.length
; i
++) {
1798 Item item
= _items
[i
];
1799 if (item
._trace
== entry
) {
1807 int findItemIndex(ITimeGraphEntry trace
) {
1808 if (trace
== null) {
1812 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
1813 Item item
= _expandedItems
[i
];
1814 if (item
._trace
== trace
) {
1822 public void refreshData() {
1823 List
<Item
> itemList
= new ArrayList
<Item
>();
1824 filteredOut
.clear();
1825 for (int i
= 0; i
< _traces
.length
; i
++) {
1826 ITimeGraphEntry entry
= _traces
[i
];
1827 refreshData(itemList
, null, 0, entry
);
1829 _items
= itemList
.toArray(new Item
[0]);
1830 updateExpandedItems();
1833 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
1834 Item item
= new Item(entry
, entry
.getName(), level
);
1835 if (parent
!= null) {
1836 parent
.children
.add(item
);
1838 item
.itemHeight
= provider
.getItemHeight(entry
);
1840 if (entry
.hasChildren()) {
1841 item
._expanded
= true;
1842 item
._hasChildren
= true;
1843 for (ITimeGraphEntry child
: entry
.getChildren()) {
1844 refreshData(itemList
, item
, level
+ 1, child
);
1849 public void updateExpandedItems() {
1850 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
1851 for (int i
= 0; i
< _traces
.length
; i
++) {
1852 ITimeGraphEntry entry
= _traces
[i
];
1853 Item item
= findItem(entry
);
1854 refreshExpanded(expandedItemList
, item
);
1856 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
1859 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
1860 expandedItemList
.add(item
);
1861 if (item
._hasChildren
&& item
._expanded
) {
1862 for (Item child
: item
.children
) {
1863 refreshExpanded(expandedItemList
, child
);
1868 public void refreshData(ITimeGraphEntry traces
[]) {
1869 if (traces
== null || traces
.length
== 0) {
1871 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
1872 traceFilter
= new boolean[traces
.length
];
1873 java
.util
.Arrays
.fill(traceFilter
, true);
1880 public ITimeGraphEntry
[] getTraces() {
1884 public boolean[] getTraceFilter() {
1888 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1893 private class Item
{
1894 public boolean _expanded
;
1895 public boolean _selected
;
1896 public boolean _hasChildren
;
1897 public int itemHeight
;
1899 public List
<Item
> children
;
1900 public String _name
;
1901 public ITimeGraphEntry _trace
;
1903 public Item(ITimeGraphEntry trace
, String name
, int level
) {
1904 this._trace
= trace
;
1907 this.children
= new ArrayList
<Item
>();
1911 public String
toString() {