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
.MenuDetectEvent
;
42 import org
.eclipse
.swt
.events
.MenuDetectListener
;
43 import org
.eclipse
.swt
.events
.MouseEvent
;
44 import org
.eclipse
.swt
.events
.MouseListener
;
45 import org
.eclipse
.swt
.events
.MouseMoveListener
;
46 import org
.eclipse
.swt
.events
.MouseTrackListener
;
47 import org
.eclipse
.swt
.events
.MouseWheelListener
;
48 import org
.eclipse
.swt
.events
.PaintEvent
;
49 import org
.eclipse
.swt
.events
.SelectionEvent
;
50 import org
.eclipse
.swt
.events
.SelectionListener
;
51 import org
.eclipse
.swt
.events
.TraverseEvent
;
52 import org
.eclipse
.swt
.events
.TraverseListener
;
53 import org
.eclipse
.swt
.events
.TypedEvent
;
54 import org
.eclipse
.swt
.graphics
.Color
;
55 import org
.eclipse
.swt
.graphics
.Cursor
;
56 import org
.eclipse
.swt
.graphics
.GC
;
57 import org
.eclipse
.swt
.graphics
.Image
;
58 import org
.eclipse
.swt
.graphics
.Point
;
59 import org
.eclipse
.swt
.graphics
.Rectangle
;
60 import org
.eclipse
.swt
.widgets
.Composite
;
61 import org
.eclipse
.swt
.widgets
.Display
;
62 import org
.eclipse
.swt
.widgets
.Event
;
63 import org
.eclipse
.swt
.widgets
.Listener
;
64 import org
.eclipse
.swt
.widgets
.ScrollBar
;
67 * Time graph control implementation
70 * @author Alvaro Sanchez-Leon
71 * @author Patrick Tasse
73 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
, MenuDetectListener
{
75 private static final int DRAG_NONE
= 0;
76 private static final int DRAG_TRACE_ITEM
= 1;
77 private static final int DRAG_SPLIT_LINE
= 2;
78 public static final boolean DEFAULT_DRAW_THREAD_JOIN
= true;
79 public static final boolean DEFAULT_DRAW_THREAD_WAIT
= true;
80 public static final boolean DEFAULT_DRAW_THREAD_RELEASE
= true;
81 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
82 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
84 private static final double zoomCoeff
= 1.5;
86 private ITimeDataProvider _timeProvider
;
87 private boolean _isInFocus
= false;
88 private boolean _isDragCursor3
= false;
89 private boolean _isWaitCursor
= true;
90 private boolean _mouseOverSplitLine
= false;
91 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
92 private int _minimumItemWidth
= 0;
93 private int _topIndex
= 0;
94 private int _dragState
= DRAG_NONE
;
95 private int _dragX0
= 0;
96 private int _dragX
= 0;
97 private int _idealNameSpace
= 0;
98 // private double _timeStep = 10000000;
99 private long _time0bak
;
100 private long _time1bak
;
101 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
102 private ItemData _data
= null;
103 private List
<SelectionListener
> _selectionListeners
;
104 private final List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
105 private final List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
106 private final List
<MenuDetectListener
> _timeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
107 private final List
<MenuDetectListener
> _timeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
108 private final Cursor _dragCursor3
;
109 private final Cursor _WaitCursor
;
111 // Vertical formatting formatting for the state control view
112 private final boolean _visibleVerticalScroll
= true;
113 private int _borderWidth
= 0;
114 private int _headerHeight
= 0;
116 private Listener mouseScrollFilterListener
;
118 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
119 protected Color
[] fEventColorMap
= null;
121 private MouseScrollNotifier fMouseScrollNotifier
;
122 private final Object fMouseScrollNotifierLock
= new Object();
123 private class MouseScrollNotifier
extends Thread
{
124 private final static long DELAY
= 400L;
125 private final static long POLLING_INTERVAL
= 10L;
126 private long fLastScrollTime
= Long
.MAX_VALUE
;
130 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
132 Thread
.sleep(POLLING_INTERVAL
);
133 } catch (Exception e
) {
137 if (!isInterrupted()) {
138 Display
.getDefault().asyncExec(new Runnable() {
144 _timeProvider
.notifyStartFinishTime();
148 synchronized (fMouseScrollNotifierLock
) {
149 fMouseScrollNotifier
= null;
153 public void mouseScrolled() {
154 fLastScrollTime
= System
.currentTimeMillis();
159 * Standard constructor
162 * The parent composite object
164 * The color scheme to use
166 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
168 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
170 _data
= new ItemData();
172 addFocusListener(this);
173 addMouseListener(this);
174 addMouseMoveListener(this);
175 addMouseTrackListener(this);
176 addMouseWheelListener(this);
177 addTraverseListener(this);
178 addKeyListener(this);
179 addControlListener(this);
180 addMenuDetectListener(this);
181 ScrollBar scrollHor
= getHorizontalBar();
183 if (scrollHor
!= null) {
184 scrollHor
.addSelectionListener(this);
187 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
188 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
192 public void dispose() {
194 _dragCursor3
.dispose();
195 _WaitCursor
.dispose();
196 fResourceManager
.dispose();
200 * Sets the timegraph provider used by this timegraph viewer.
202 * @param timeGraphProvider the timegraph provider
204 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
205 fTimeGraphProvider
= timeGraphProvider
;
206 _data
.provider
= timeGraphProvider
;
208 if (fEventColorMap
!= null) {
209 for (Color color
: fEventColorMap
) {
210 fResourceManager
.destroyColor(color
.getRGB());
213 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
214 if (stateItems
!= null) {
215 fEventColorMap
= new Color
[stateItems
.length
];
216 for (int i
= 0; i
< stateItems
.length
; i
++) {
217 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
220 fEventColorMap
= new Color
[] { };
225 * Assign the given time provider
227 * @param timeProvider
230 public void setTimeProvider(ITimeDataProvider timeProvider
) {
231 _timeProvider
= timeProvider
;
237 * Add a selection listener
240 * The listener to add
242 public void addSelectionListener(SelectionListener listener
) {
243 if (listener
== null) {
244 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
246 if (null == _selectionListeners
) {
247 _selectionListeners
= new ArrayList
<SelectionListener
>();
249 _selectionListeners
.add(listener
);
253 * Remove a selection listener
256 * The listener to remove
258 public void removeSelectionListener(SelectionListener listener
) {
259 if (null != _selectionListeners
) {
260 _selectionListeners
.remove(listener
);
265 * Selection changed callback
267 public void fireSelectionChanged() {
268 if (null != _selectionListeners
) {
269 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
270 while (it
.hasNext()) {
271 SelectionListener listener
= it
.next();
272 listener
.widgetSelected(null);
278 * Default selection callback
280 public void fireDefaultSelection() {
281 if (null != _selectionListeners
) {
282 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
283 while (it
.hasNext()) {
284 SelectionListener listener
= it
.next();
285 listener
.widgetDefaultSelected(null);
291 * Get the traces in the model
293 * @return The array of traces
295 public ITimeGraphEntry
[] getTraces() {
296 return _data
.getTraces();
300 * Get the on/off trace filters
302 * @return The array of filters
304 public boolean[] getTraceFilter() {
305 return _data
.getTraceFilter();
309 * Refresh the data for the thing
311 public void refreshData() {
318 * Refresh data for the given traces
321 * The traces to refresh
323 public void refreshData(ITimeGraphEntry
[] traces
) {
324 _data
.refreshData(traces
);
330 * Adjust the scoll bars
332 public void adjustScrolls() {
333 if (null == _timeProvider
) {
334 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
340 long time0
= _timeProvider
.getTime0();
341 long time1
= _timeProvider
.getTime1();
343 long timeMin
= _timeProvider
.getMinTime();
344 long timeMax
= _timeProvider
.getMaxTime();
346 long delta
= timeMax
- timeMin
;
349 int thumb
= H_SCROLLBAR_MAX
;
352 // Thumb size (page size)
353 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
354 // At the beginning of visible window
355 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
358 // position, minimum, maximum, thumb size, increment (half page)t, page
359 // increment size (full page)
360 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
363 boolean ensureVisibleItem(int idx
, boolean redraw
) {
364 boolean changed
= false;
366 for (idx
= 0; idx
< _data
._expandedItems
.length
; idx
++) {
367 if (_data
._expandedItems
[idx
]._selected
) {
372 if (idx
>= _data
._expandedItems
.length
) {
375 if (idx
< _topIndex
) {
377 //FIXME:getVerticalBar().setSelection(_topItem);
383 int page
= countPerPage();
384 if (idx
>= _topIndex
+ page
) {
385 setTopIndex(idx
- page
+ 1);
386 //FIXME:getVerticalBar().setSelection(_topItem);
397 * Assign the given index as the top one
402 public void setTopIndex(int idx
) {
403 idx
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
404 idx
= Math
.max(0, idx
);
410 * Set the expanded state of a given entry
415 * True if expanded, false if collapsed
417 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
418 Item item
= _data
.findItem(entry
);
419 if (item
!= null && item
._expanded
!= expanded
) {
420 item
._expanded
= expanded
;
421 _data
.updateExpandedItems();
427 * Add a tree listener
430 * The listener to add
432 public void addTreeListener(ITimeGraphTreeListener listener
) {
433 if (!_treeListeners
.contains(listener
)) {
434 _treeListeners
.add(listener
);
439 * Remove a tree listener
442 * The listener to remove
444 public void removeTreeListener(ITimeGraphTreeListener listener
) {
445 if (_treeListeners
.contains(listener
)) {
446 _treeListeners
.remove(listener
);
451 * Tree event callback
456 * The expanded state (true for expanded, false for collapsed)
458 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
459 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
460 for (ITimeGraphTreeListener listener
: _treeListeners
) {
462 listener
.treeExpanded(event
);
464 listener
.treeCollapsed(event
);
470 * Add a menu listener on {@link ITimeGraphEntry}s
473 * The listener to add
475 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
476 if (!_timeGraphEntryMenuListeners
.contains(listener
)) {
477 _timeGraphEntryMenuListeners
.add(listener
);
482 * Remove a menu listener on {@link ITimeGraphEntry}s
485 * The listener to remove
487 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
488 if (_timeGraphEntryMenuListeners
.contains(listener
)) {
489 _timeGraphEntryMenuListeners
.remove(listener
);
494 * Menu event callback on {@link ITimeGraphEntry}s
497 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
499 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
500 for (MenuDetectListener listener
: _timeGraphEntryMenuListeners
) {
501 listener
.menuDetected(event
);
506 * Add a menu listener on {@link ITimeEvent}s
509 * The listener to add
511 public void addTimeEventMenuListener(MenuDetectListener listener
) {
512 if (!_timeEventMenuListeners
.contains(listener
)) {
513 _timeEventMenuListeners
.add(listener
);
518 * Remove a menu listener on {@link ITimeEvent}s
521 * The listener to remove
523 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
524 if (_timeEventMenuListeners
.contains(listener
)) {
525 _timeEventMenuListeners
.remove(listener
);
530 * Menu event callback on {@link ITimeEvent}s
533 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
535 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
536 for (MenuDetectListener listener
: _timeEventMenuListeners
) {
537 listener
.menuDetected(event
);
542 public ISelection
getSelection() {
543 TimeGraphSelection sel
= new TimeGraphSelection();
544 ITimeGraphEntry trace
= getSelectedTrace();
545 if (null != trace
&& null != _timeProvider
) {
546 long selectedTime
= _timeProvider
.getSelectedTime();
547 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
558 * Get the selection object
560 * @return The selection
562 public ISelection
getSelectionTrace() {
563 TimeGraphSelection sel
= new TimeGraphSelection();
564 ITimeGraphEntry trace
= getSelectedTrace();
572 * Enable/disable one of the traces in the model
575 * 1 to enable it, -1 to disable. The method returns immediately
576 * if another value is used.
578 public void selectTrace(int n
) {
579 if ((n
!= 1) && (n
!= -1)) {
583 boolean changed
= false;
584 int lastSelection
= -1;
585 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
586 Item item
= _data
._expandedItems
[i
];
587 if (item
._selected
) {
589 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
590 item
._selected
= false;
591 item
= _data
._expandedItems
[i
+ 1];
592 item
._selected
= true;
594 } else if ((-1 == n
) && (i
> 0)) {
595 item
._selected
= false;
596 item
= _data
._expandedItems
[i
- 1];
597 item
._selected
= true;
604 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
605 Item item
= _data
._expandedItems
[0];
606 item
._selected
= true;
611 ensureVisibleItem(-1, false);
613 fireSelectionChanged();
621 * 1 for next event, -1 for previous event
623 public void selectEvent(int n
) {
624 if (null == _timeProvider
) {
627 ITimeGraphEntry trace
= getSelectedTrace();
631 long selectedTime
= _timeProvider
.getSelectedTime();
632 long endTime
= _timeProvider
.getEndTime();
633 ITimeEvent nextEvent
;
634 if (-1 == n
&& selectedTime
> endTime
) {
635 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
637 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
639 if (null == nextEvent
&& -1 == n
) {
640 nextEvent
= Utils
.getFirstEvent(trace
);
642 if (null != nextEvent
) {
643 long nextTime
= nextEvent
.getTime();
644 // If last event detected e.g. going back or not moving to a next
646 if (nextTime
<= selectedTime
&& n
== 1) {
647 // Select to the end of this last event
648 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
649 // but not beyond the end of the trace
650 if (nextTime
> endTime
) {
653 } else if (n
== -1) {
654 // for previous event go to its end time unless we were already there
655 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
656 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
659 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
660 fireSelectionChanged();
662 _timeProvider
.setSelectedTimeNotify(endTime
, true);
663 fireSelectionChanged();
668 * Select the next event
670 public void selectNextEvent() {
672 // Notify if visible time window has been adjusted
673 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
677 * Select the previous event
679 public void selectPrevEvent() {
681 // Notify if visible time window has been adjusted
682 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
686 * Select the next trace
688 public void selectNextTrace() {
693 * Select the previous trace
695 public void selectPrevTrace() {
700 * Zoom based on mouse cursor location with mouse scrolling
702 * @param zoomIn true to zoom in, false to zoom out
704 public void zoom(boolean zoomIn
) {
705 int globalX
= getDisplay().getCursorLocation().x
;
706 Point p
= toControl(globalX
, 0);
707 int nameSpace
= _timeProvider
.getNameSpace();
708 int timeSpace
= _timeProvider
.getTimeSpace();
709 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
710 long time0
= _timeProvider
.getTime0();
711 long time1
= _timeProvider
.getTime1();
712 long interval
= time1
- time0
;
715 } // to allow getting out of single point interval
718 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
720 newInterval
= (long) Math
.ceil(interval
* 1.25);
722 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
723 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
724 long newTime1
= newTime0
+ newInterval
;
725 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
726 synchronized (fMouseScrollNotifierLock
) {
727 if (fMouseScrollNotifier
== null) {
728 fMouseScrollNotifier
= new MouseScrollNotifier();
729 fMouseScrollNotifier
.start();
731 fMouseScrollNotifier
.mouseScrolled();
736 * zoom in using single click
738 public void zoomIn() {
739 long _time0
= _timeProvider
.getTime0();
740 long _time1
= _timeProvider
.getTime1();
741 long _range
= _time1
- _time0
;
742 long selTime
= _timeProvider
.getSelectedTime();
743 if (selTime
<= _time0
|| selTime
>= _time1
) {
744 selTime
= (_time0
+ _time1
) / 2;
746 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
747 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
749 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
751 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
752 // + time1 + " inaccuracy:" + inaccuracy);
754 if (inaccuracy
> 0 && inaccuracy
< 100) {
755 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
759 long m
= _timeProvider
.getMinTimeInterval();
760 if ((time1
- time0
) < m
) {
761 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
765 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
769 * zoom out using single click
771 public void zoomOut() {
772 long _time0
= _timeProvider
.getTime0();
773 long _time1
= _timeProvider
.getTime1();
774 long selTime
= _timeProvider
.getSelectedTime();
775 if (selTime
<= _time0
|| selTime
>= _time1
) {
776 selTime
= (_time0
+ _time1
) / 2;
778 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
779 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
781 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
782 if (inaccuracy
> 0 && inaccuracy
< 100) {
783 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
787 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
791 * Return the currently selected trace
793 * @return The entry matching the trace
795 public ITimeGraphEntry
getSelectedTrace() {
796 ITimeGraphEntry trace
= null;
797 int idx
= getSelectedIndex();
799 trace
= _data
._expandedItems
[idx
]._trace
;
805 * Retrieve the index of the currently selected item
809 public int getSelectedIndex() {
811 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
812 Item item
= _data
._expandedItems
[i
];
813 if (item
._selected
) {
821 boolean toggle(int idx
) {
822 boolean toggled
= false;
823 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
824 Item item
= _data
._expandedItems
[idx
];
825 if (item
._hasChildren
) {
826 item
._expanded
= !item
._expanded
;
827 _data
.updateExpandedItems();
831 fireTreeEvent(item
._trace
, item
._expanded
);
837 int getItemIndexAtY(int y
) {
841 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
843 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
844 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
851 int idx
= y
/ _itemHeight
;
853 if (idx
< _data
._expandedItems
.length
) {
859 boolean isOverSplitLine(int x
) {
860 if (x
< 0 || null == _timeProvider
) {
864 int nameWidth
= _timeProvider
.getNameSpace();
865 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
871 ITimeGraphEntry
getEntry(Point pt
) {
872 int idx
= getItemIndexAtY(pt
.y
);
873 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
876 long getTimeAtX(int x
) {
877 if (null == _timeProvider
) {
881 Point size
= getCtrlSize();
882 long time0
= _timeProvider
.getTime0();
883 long time1
= _timeProvider
.getTime1();
884 int nameWidth
= _timeProvider
.getNameSpace();
886 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
887 if (x
>= 0 && size
.x
>= nameWidth
) {
888 if (time1
- time0
> timeWidth
) {
889 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
890 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
892 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
893 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
899 void selectItem(int idx
, boolean addSelection
) {
900 boolean changed
= false;
902 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
903 Item item
= _data
._expandedItems
[idx
];
904 changed
= (item
._selected
== false);
905 item
._selected
= true;
908 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
909 Item item
= _data
._expandedItems
[i
];
910 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
913 item
._selected
= i
== idx
;
916 changed
|= ensureVisibleItem(idx
, true);
923 * Callback for item selection
926 * The entry matching the trace
927 * @param addSelection
928 * If the selection is added or removed
930 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
931 int idx
= _data
.findItemIndex(trace
);
932 selectItem(idx
, addSelection
);
936 * Retrieve the number of entries shown per page.
940 public int countPerPage() {
941 int height
= getCtrlSize().y
;
943 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
945 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
946 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
947 if (ySum
>= height
) {
952 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
953 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
954 if (ySum
>= height
) {
962 count
= height
/ _itemHeight
;
968 * Get the index of the top element
972 public int getTopIndex() {
977 * Get the number of expanded items
979 * @return The count of expanded items
981 public int getExpandedElementCount() {
982 return _data
._expandedItems
.length
;
986 * Get an array of all expanded elements
988 * @return The expanded elements
990 public ITimeGraphEntry
[] getExpandedElements() {
991 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
992 for (Item item
: _data
._expandedItems
) {
993 elements
.add(item
._trace
);
995 return elements
.toArray(new ITimeGraphEntry
[0]);
998 Point
getCtrlSize() {
999 Point size
= getSize();
1000 if (getHorizontalBar().isVisible()) {
1001 size
.y
-= getHorizontalBar().getSize().y
;
1006 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1008 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1009 int width
= nameWidth
;
1010 int height
= _itemHeight
;
1011 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1013 for (int i
= _topIndex
; i
< idx
; i
++) {
1014 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1017 height
= _data
._expandedItems
[idx
].itemHeight
;
1019 return new Rectangle(x
, y
, width
, height
);
1022 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1023 int x
= bound
.x
+ nameWidth
;
1024 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1025 int width
= bound
.width
- x
;
1026 int height
= _itemHeight
;
1027 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1029 for (int i
= _topIndex
; i
< idx
; i
++) {
1030 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1033 height
= _data
._expandedItems
[idx
].itemHeight
;
1035 return new Rectangle(x
, y
, width
, height
);
1039 void paint(Rectangle bounds
, PaintEvent e
) {
1041 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
1042 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1044 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
1048 _idealNameSpace
= 0;
1049 int nameSpace
= _timeProvider
.getNameSpace();
1051 // draw empty name space background
1052 gc
.setBackground(_colors
.getBkColor(false, false, true));
1053 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1055 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1057 // draw selected time
1058 long time0
= _timeProvider
.getTime0();
1059 long time1
= _timeProvider
.getTime1();
1060 long selectedTime
= _timeProvider
.getSelectedTime();
1061 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1062 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1063 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1064 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1065 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1068 // draw drag line, no line if name space is 0.
1069 if (DRAG_SPLIT_LINE
== _dragState
) {
1070 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1071 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1072 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1073 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1074 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1079 * Draw many items at once
1082 * The rectangle of the area
1083 * @param timeProvider
1086 * The array items to draw
1088 * The index of the first element to draw
1090 * The width reserved for the names
1092 * Reference to the SWT GC object
1094 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1095 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1096 for (int i
= topIndex
; i
< items
.length
; i
++) {
1097 Item item
= items
[i
];
1098 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1100 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1106 * @param item the item to draw
1107 * @param bounds the container rectangle
1108 * @param i the item index
1109 * @param nameSpace the name space
1112 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1113 ITimeGraphEntry entry
= item
._trace
;
1114 long time0
= timeProvider
.getTime0();
1115 long time1
= timeProvider
.getTime1();
1116 long selectedTime
= timeProvider
.getSelectedTime();
1118 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1119 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1123 if (! item
._trace
.hasTimeEvents()) {
1124 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1125 nameRect
.width
+= statesRect
.width
;
1126 drawName(item
, nameRect
, gc
);
1128 drawName(item
, nameRect
, gc
);
1130 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1131 if (rect
.isEmpty()) {
1132 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1135 if (time1
<= time0
) {
1136 gc
.setBackground(_colors
.getBkColor(false, false, false));
1137 gc
.fillRectangle(rect
);
1138 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1142 // Initialize _rect1 to same values as enclosing rectangle rect
1143 Rectangle stateRect
= Utils
.clone(rect
);
1144 boolean selected
= item
._selected
;
1145 // K pixels per second
1146 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1148 if (item
._trace
.hasTimeEvents()) {
1149 fillSpace(rect
, gc
, selected
);
1150 // Drawing rectangle is smaller than reserved space
1152 stateRect
.height
-= 6;
1154 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1155 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1158 while (iterator
.hasNext()) {
1159 ITimeEvent event
= iterator
.next();
1160 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1161 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1162 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1163 // event is out of bounds
1166 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1167 stateRect
.x
= Math
.max(rect
.x
, x
);
1168 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1169 if (stateRect
.x
== lastX
) {
1170 stateRect
.width
-= 1;
1171 if (stateRect
.width
> 0) {
1172 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1173 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1179 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1180 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
1183 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1186 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1187 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1188 if (! hasTimeEvents
) {
1189 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1190 gc
.fillRectangle(bounds
);
1191 if (item
._selected
&& _isInFocus
) {
1192 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1193 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1196 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1197 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1198 gc
.fillRectangle(bounds
);
1201 // No name to be drawn
1202 if (_timeProvider
.getNameSpace() == 0) {
1206 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1207 if (item
._hasChildren
) {
1208 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1209 gc
.setBackground(_colors
.getBkColor(false, false, false));
1210 Rectangle rect
= Utils
.clone(bounds
);
1211 rect
.x
+= leftMargin
;
1212 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1213 rect
.width
= EXPAND_SIZE
;
1214 rect
.height
= EXPAND_SIZE
;
1215 gc
.fillRectangle(rect
);
1216 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1217 int midy
= rect
.y
+ rect
.height
/ 2;
1218 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1219 if (!item
._expanded
) {
1220 int midx
= rect
.x
+ rect
.width
/ 2;
1221 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1224 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1226 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1229 int imgHeight
= img
.getImageData().height
;
1230 int imgWidth
= img
.getImageData().width
;
1232 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1233 gc
.drawImage(img
, x
, y
);
1234 leftMargin
+= imgWidth
+ MARGIN
;
1236 String name
= item
._name
;
1237 Point size
= gc
.stringExtent(name
);
1238 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1239 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1241 if (hasTimeEvents
) {
1242 // cut long string with "..."
1243 int width
= bounds
.width
- leftMargin
;
1245 while (size
.x
> width
&& name
.length() > 1) {
1247 name
= name
.substring(0, name
.length() - 1);
1248 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1251 name
+= "..."; //$NON-NLS-1$
1254 Rectangle rect
= Utils
.clone(bounds
);
1255 rect
.x
+= leftMargin
;
1256 rect
.width
-= leftMargin
;
1258 if (rect
.width
> 0) {
1259 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1260 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1261 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1262 leftMargin
+= textWidth
+ MARGIN
;
1265 if (hasTimeEvents
) {
1267 int x
= bounds
.x
+ leftMargin
;
1268 int width
= bounds
.width
- x
;
1269 int midy
= bounds
.y
+ bounds
.height
/ 2;
1270 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1271 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1276 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1277 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1279 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1283 boolean visible
= rect
.width
== 0 ?
false : true;
1286 Color stateColor
= null;
1287 if (colorIdx
< fEventColorMap
.length
) {
1288 stateColor
= fEventColorMap
[colorIdx
];
1290 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1293 timeSelected
= timeSelected
&& selected
;
1295 // modify the color?
1297 // fill all rect area
1298 gc
.setBackground(stateColor
);
1299 gc
.fillRectangle(rect
);
1300 // get the border color?
1301 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1304 if (!timeSelected
) {
1305 // Draw the top and bottom borders i.e. no side borders
1307 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1309 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1312 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1313 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1315 // selected rectangle area is not visible but can be represented
1316 // with a broken vertical line of specified width.
1319 gc.setForeground(stateColor);
1320 int s = gc.getLineStyle();
1321 int w = gc.getLineWidth();
1322 gc.setLineStyle(SWT.LINE_DOT);
1323 gc.setLineWidth(width);
1324 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1325 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1327 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1330 if (!timeSelected) {
1331 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1332 gc.drawPoint(rect.x, rect.y);
1333 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1337 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1340 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1341 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1342 gc
.fillRectangle(rect
);
1344 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1345 int midy
= rect
.y
+ rect
.height
/ 2;
1346 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1350 public void keyTraversed(TraverseEvent e
) {
1351 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1357 public void keyPressed(KeyEvent e
) {
1359 if (_data
._expandedItems
.length
== 0) {
1362 if (SWT
.HOME
== e
.keyCode
) {
1364 } else if (SWT
.END
== e
.keyCode
) {
1365 idx
= _data
._expandedItems
.length
- 1;
1366 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1367 idx
= getSelectedIndex();
1370 } else if (idx
< _data
._expandedItems
.length
- 1) {
1373 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1374 idx
= getSelectedIndex();
1377 } else if (idx
> 0) {
1380 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1382 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1384 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1385 int page
= countPerPage();
1386 idx
= getSelectedIndex();
1391 if (idx
>= _data
._expandedItems
.length
) {
1392 idx
= _data
._expandedItems
.length
- 1;
1394 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1395 int page
= countPerPage();
1396 idx
= getSelectedIndex();
1404 } else if (SWT
.CR
== e
.keyCode
) {
1405 idx
= getSelectedIndex();
1407 if (_data
._expandedItems
[idx
]._hasChildren
) {
1410 fireDefaultSelection();
1416 selectItem(idx
, false);
1417 fireSelectionChanged();
1422 public void keyReleased(KeyEvent e
) {
1426 public void focusGained(FocusEvent e
) {
1428 if (mouseScrollFilterListener
== null) {
1429 mouseScrollFilterListener
= new Listener() {
1430 // This filter is used to prevent horizontal scrolling of the view
1431 // when the mouse wheel is used to zoom
1433 public void handleEvent(Event event
) {
1437 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1443 public void focusLost(FocusEvent e
) {
1445 if (mouseScrollFilterListener
!= null) {
1446 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1447 mouseScrollFilterListener
= null;
1449 if (DRAG_NONE
!= _dragState
) {
1451 _dragState
= DRAG_NONE
;
1457 * @return If the current view is focused
1459 public boolean isInFocus() {
1464 * Provide the possibility to control the wait cursor externally e.g. data
1465 * requests in progress
1467 * @param waitInd Should we wait indefinitely?
1469 public void waitCursor(boolean waitInd
) {
1470 // Update cursor as indicated
1472 setCursor(_WaitCursor
);
1473 _isWaitCursor
= true;
1476 _isWaitCursor
= false;
1479 // Get ready for next mouse move
1480 _isDragCursor3
= false;
1485 * If the x, y position is over the vertical split line (name to time
1486 * ranges), then change the cursor to a drag cursor to indicate the user the
1487 * possibility of resizing
1493 void updateCursor(int x
, int y
) {
1494 // if Wait cursor not active, check for the need to change to a drag
1496 if (_isWaitCursor
== false) {
1497 boolean isSplitLine
= isOverSplitLine(x
);
1498 // No dragcursor is name space is fixed to zero
1499 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1500 setCursor(_dragCursor3
);
1501 _isDragCursor3
= true;
1502 } else if (!isSplitLine
&& _isDragCursor3
) {
1504 _isDragCursor3
= false;
1510 public void mouseMove(MouseEvent e
) {
1511 if (null == _timeProvider
) {
1514 Point size
= getCtrlSize();
1515 if (DRAG_TRACE_ITEM
== _dragState
) {
1516 int nameWidth
= _timeProvider
.getNameSpace();
1517 int x
= e
.x
- nameWidth
;
1518 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1520 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1521 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1522 long time1
= _time1bak
- timeDelta
;
1523 long maxTime
= _timeProvider
.getMaxTime();
1524 if (time1
> maxTime
) {
1527 long time0
= time1
- (_time1bak
- _time0bak
);
1528 if (time0
< _timeProvider
.getMinTime()) {
1529 time0
= _timeProvider
.getMinTime();
1530 time1
= time0
+ (_time1bak
- _time0bak
);
1532 _timeProvider
.setStartFinishTime(time0
, time1
);
1534 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1536 _timeProvider
.setNameSpace(e
.x
);
1537 } else if (DRAG_NONE
== _dragState
) {
1538 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1539 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1542 _mouseOverSplitLine
= mouseOverSplitLine
;
1544 updateCursor(e
.x
, e
.y
);
1548 public void mouseDoubleClick(MouseEvent e
) {
1549 if (null == _timeProvider
) {
1552 if (1 == e
.button
) {
1553 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1554 _timeProvider
.setNameSpace(_idealNameSpace
);
1555 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1556 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1559 _mouseOverSplitLine
= mouseOverSplitLine
;
1562 int idx
= getItemIndexAtY(e
.y
);
1564 selectItem(idx
, false);
1565 fireDefaultSelection();
1571 public void mouseDown(MouseEvent e
) {
1572 if (null == _timeProvider
) {
1576 if (1 == e
.button
) {
1577 int nameSpace
= _timeProvider
.getNameSpace();
1578 if (nameSpace
!= 0) {
1579 if (isOverSplitLine(e
.x
)) {
1580 _dragState
= DRAG_SPLIT_LINE
;
1581 _dragX
= _dragX0
= e
.x
;
1582 _time0bak
= _timeProvider
.getTime0();
1583 _time1bak
= _timeProvider
.getTime1();
1589 idx
= getItemIndexAtY(e
.y
);
1591 Item item
= _data
._expandedItems
[idx
];
1592 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1595 long hitTime
= getTimeAtX(e
.x
);
1597 // _timeProvider.setSelectedTimeInt(hitTime, false);
1599 _dragState
= DRAG_TRACE_ITEM
;
1600 _dragX
= _dragX0
= e
.x
- nameSpace
;
1601 _time0bak
= _timeProvider
.getTime0();
1602 _time1bak
= _timeProvider
.getTime1();
1605 selectItem(idx
, false);
1606 fireSelectionChanged();
1608 selectItem(idx
, false); // clear selection
1610 fireSelectionChanged();
1616 public void mouseUp(MouseEvent e
) {
1617 if (DRAG_NONE
!= _dragState
) {
1619 if (DRAG_TRACE_ITEM
== _dragState
) {
1620 // Notify time provider to check the need for listener
1622 _timeProvider
.notifyStartFinishTime();
1623 if (_dragX
== _dragX0
) { // click without drag
1624 long time
= getTimeAtX(e
.x
);
1625 _timeProvider
.setSelectedTimeNotify(time
, false);
1627 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1630 _dragState
= DRAG_NONE
;
1635 public void mouseEnter(MouseEvent e
) {
1639 public void mouseExit(MouseEvent e
) {
1640 if (_mouseOverSplitLine
) {
1641 _mouseOverSplitLine
= false;
1647 public void mouseHover(MouseEvent e
) {
1651 public void mouseScrolled(MouseEvent e
) {
1652 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1655 boolean zoomScroll
= false;
1656 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1657 Point parentSize
= getParent().getSize();
1658 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1659 // over the parent control
1660 if (e
.x
> getCtrlSize().x
) {
1661 // over the horizontal scroll bar
1663 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1664 // over the name space
1670 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1673 } else if (e
.count
< 0) {
1677 setTopIndex(getTopIndex() - e
.count
);
1682 public void controlMoved(ControlEvent e
) {
1686 public void controlResized(ControlEvent e
) {
1691 public void widgetDefaultSelected(SelectionEvent e
) {
1695 public void widgetSelected(SelectionEvent e
) {
1696 if (e
.widget
== getVerticalBar()) {
1697 setTopIndex(getVerticalBar().getSelection());
1698 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1699 int start
= getHorizontalBar().getSelection();
1700 long time0
= _timeProvider
.getTime0();
1701 long time1
= _timeProvider
.getTime1();
1702 long timeMin
= _timeProvider
.getMinTime();
1703 long timeMax
= _timeProvider
.getMaxTime();
1704 long delta
= timeMax
- timeMin
;
1706 long range
= time1
- time0
;
1707 // _timeRangeFixed = true;
1708 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1709 time1
= time0
+ range
;
1711 // TODO: Follow-up with Bug 310310
1712 // In Linux SWT.DRAG is the only value received
1713 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1714 if (e
.detail
== SWT
.DRAG
) {
1715 _timeProvider
.setStartFinishTime(time0
, time1
);
1717 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1723 * @return The current visibility of the vertical scroll bar
1725 public boolean isVisibleVerticalScroll() {
1726 return _visibleVerticalScroll
;
1730 public int getBorderWidth() {
1731 return _borderWidth
;
1735 * Set the border width
1737 * @param borderWidth
1740 public void setBorderWidth(int borderWidth
) {
1741 this._borderWidth
= borderWidth
;
1745 * @return The current height of the header row
1747 public int getHeaderHeight() {
1748 return _headerHeight
;
1752 * Set the height of the header row
1754 * @param headerHeight
1757 public void setHeaderHeight(int headerHeight
) {
1758 this._headerHeight
= headerHeight
;
1762 * @return The height of regular item rows
1764 public int getItemHeight() {
1769 * Set the height of regular itew rows
1774 public void setItemHeight(int rowHeight
) {
1775 this._itemHeight
= rowHeight
;
1779 * Set the minimum item width
1781 * @param width The minimum width
1783 public void setMinimumItemWidth(int width
) {
1784 this._minimumItemWidth
= width
;
1788 * @return The minimum item width
1790 public int getMinimumItemWidth() {
1791 return _minimumItemWidth
;
1795 * @return The entries that are currently filtered out
1797 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1798 return _data
.getFilteredOut();
1803 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1804 if (listener
!= null) {
1805 if (!_selectionChangedListeners
.contains(listener
)) {
1806 _selectionChangedListeners
.add(listener
);
1813 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1814 if (listener
!= null) {
1815 _selectionChangedListeners
.remove(listener
);
1821 public void setSelection(ISelection selection
) {
1822 if (selection
instanceof TimeGraphSelection
) {
1823 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1824 Object ob
= sel
.getFirstElement();
1825 if (ob
instanceof ITimeGraphEntry
) {
1826 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1827 selectItem(trace
, false);
1833 private class ItemData
{
1834 public Item
[] _expandedItems
= new Item
[0];
1835 public Item
[] _items
= new Item
[0];
1836 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1837 private boolean traceFilter
[] = new boolean[0];
1838 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1839 public ITimeGraphPresentationProvider provider
;
1844 Item
findItem(ITimeGraphEntry entry
) {
1845 if (entry
== null) {
1849 for (int i
= 0; i
< _items
.length
; i
++) {
1850 Item item
= _items
[i
];
1851 if (item
._trace
== entry
) {
1859 int findItemIndex(ITimeGraphEntry trace
) {
1860 if (trace
== null) {
1864 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
1865 Item item
= _expandedItems
[i
];
1866 if (item
._trace
== trace
) {
1874 public void refreshData() {
1875 List
<Item
> itemList
= new ArrayList
<Item
>();
1876 filteredOut
.clear();
1877 for (int i
= 0; i
< _traces
.length
; i
++) {
1878 ITimeGraphEntry entry
= _traces
[i
];
1879 refreshData(itemList
, null, 0, entry
);
1881 _items
= itemList
.toArray(new Item
[0]);
1882 updateExpandedItems();
1885 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
1886 Item item
= new Item(entry
, entry
.getName(), level
);
1887 if (parent
!= null) {
1888 parent
.children
.add(item
);
1890 item
.itemHeight
= provider
.getItemHeight(entry
);
1892 if (entry
.hasChildren()) {
1893 item
._expanded
= true;
1894 item
._hasChildren
= true;
1895 for (ITimeGraphEntry child
: entry
.getChildren()) {
1896 refreshData(itemList
, item
, level
+ 1, child
);
1901 public void updateExpandedItems() {
1902 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
1903 for (int i
= 0; i
< _traces
.length
; i
++) {
1904 ITimeGraphEntry entry
= _traces
[i
];
1905 Item item
= findItem(entry
);
1906 refreshExpanded(expandedItemList
, item
);
1908 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
1911 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
1912 expandedItemList
.add(item
);
1913 if (item
._hasChildren
&& item
._expanded
) {
1914 for (Item child
: item
.children
) {
1915 refreshExpanded(expandedItemList
, child
);
1920 public void refreshData(ITimeGraphEntry traces
[]) {
1921 if (traces
== null || traces
.length
== 0) {
1923 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
1924 traceFilter
= new boolean[traces
.length
];
1925 java
.util
.Arrays
.fill(traceFilter
, true);
1932 public ITimeGraphEntry
[] getTraces() {
1936 public boolean[] getTraceFilter() {
1940 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1945 private class Item
{
1946 public boolean _expanded
;
1947 public boolean _selected
;
1948 public boolean _hasChildren
;
1949 public int itemHeight
;
1951 public List
<Item
> children
;
1952 public String _name
;
1953 public ITimeGraphEntry _trace
;
1955 public Item(ITimeGraphEntry trace
, String name
, int level
) {
1956 this._trace
= trace
;
1959 this.children
= new ArrayList
<Item
>();
1963 public String
toString() {
1969 public void menuDetected(MenuDetectEvent e
) {
1970 if (null == _timeProvider
) {
1973 Point p
= toControl(e
.x
, e
.y
);
1974 int idx
= getItemIndexAtY(p
.y
);
1975 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
1976 Item item
= _data
._expandedItems
[idx
];
1977 ITimeGraphEntry entry
= item
._trace
;
1978 if (entry
.hasTimeEvents()) {
1979 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
1980 if (event
!= null) {
1982 fireMenuEventOnTimeEvent(e
);
1987 fireMenuEventOnTimeGraphEntry(e
);