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 /** Max scrollbar size */
76 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
78 /** Resource manager */
79 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
81 /** Color map for event types */
82 protected Color
[] fEventColorMap
= null;
84 private static final int DRAG_NONE
= 0;
85 private static final int DRAG_TRACE_ITEM
= 1;
86 private static final int DRAG_SPLIT_LINE
= 2;
87 private static final int DRAG_ZOOM
= 3;
89 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
91 private static final double zoomCoeff
= 1.5;
93 private ITimeDataProvider _timeProvider
;
94 private boolean _isInFocus
= false;
95 private boolean _isDragCursor3
= false;
96 private boolean _isWaitCursor
= true;
97 private boolean _mouseOverSplitLine
= false;
98 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
99 private int _minimumItemWidth
= 0;
100 private int _topIndex
= 0;
101 private int _dragState
= DRAG_NONE
;
102 private int _dragX0
= 0;
103 private int _dragX
= 0;
104 private int _idealNameSpace
= 0;
105 // private double _timeStep = 10000000;
106 private long _time0bak
;
107 private long _time1bak
;
108 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
109 private ItemData _data
= null;
110 private List
<SelectionListener
> _selectionListeners
;
111 private final List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
112 private final List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
113 private final List
<MenuDetectListener
> _timeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
114 private final List
<MenuDetectListener
> _timeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
115 private final Cursor _dragCursor3
;
116 private final Cursor _WaitCursor
;
118 // Vertical formatting formatting for the state control view
119 private final boolean _visibleVerticalScroll
= true;
120 private int _borderWidth
= 0;
121 private int _headerHeight
= 0;
123 private Listener mouseScrollFilterListener
;
125 private MouseScrollNotifier fMouseScrollNotifier
;
126 private final Object fMouseScrollNotifierLock
= new Object();
127 private class MouseScrollNotifier
extends Thread
{
128 private final static long DELAY
= 400L;
129 private final static long POLLING_INTERVAL
= 10L;
130 private long fLastScrollTime
= Long
.MAX_VALUE
;
134 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
136 Thread
.sleep(POLLING_INTERVAL
);
137 } catch (Exception e
) {
141 if (!isInterrupted()) {
142 Display
.getDefault().asyncExec(new Runnable() {
148 _timeProvider
.notifyStartFinishTime();
152 synchronized (fMouseScrollNotifierLock
) {
153 fMouseScrollNotifier
= null;
157 public void mouseScrolled() {
158 fLastScrollTime
= System
.currentTimeMillis();
163 * Standard constructor
166 * The parent composite object
168 * The color scheme to use
170 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
172 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
174 _data
= new ItemData();
176 addFocusListener(this);
177 addMouseListener(this);
178 addMouseMoveListener(this);
179 addMouseTrackListener(this);
180 addMouseWheelListener(this);
181 addTraverseListener(this);
182 addKeyListener(this);
183 addControlListener(this);
184 addMenuDetectListener(this);
185 ScrollBar scrollHor
= getHorizontalBar();
187 if (scrollHor
!= null) {
188 scrollHor
.addSelectionListener(this);
191 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
192 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
196 public void dispose() {
198 _dragCursor3
.dispose();
199 _WaitCursor
.dispose();
200 fResourceManager
.dispose();
204 * Sets the timegraph provider used by this timegraph viewer.
206 * @param timeGraphProvider the timegraph provider
208 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
209 fTimeGraphProvider
= timeGraphProvider
;
210 _data
.provider
= timeGraphProvider
;
212 if (fEventColorMap
!= null) {
213 for (Color color
: fEventColorMap
) {
214 fResourceManager
.destroyColor(color
.getRGB());
217 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
218 if (stateItems
!= null) {
219 fEventColorMap
= new Color
[stateItems
.length
];
220 for (int i
= 0; i
< stateItems
.length
; i
++) {
221 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
224 fEventColorMap
= new Color
[] { };
229 * Assign the given time provider
231 * @param timeProvider
234 public void setTimeProvider(ITimeDataProvider timeProvider
) {
235 _timeProvider
= timeProvider
;
241 * Add a selection listener
244 * The listener to add
246 public void addSelectionListener(SelectionListener listener
) {
247 if (listener
== null) {
248 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
250 if (null == _selectionListeners
) {
251 _selectionListeners
= new ArrayList
<SelectionListener
>();
253 _selectionListeners
.add(listener
);
257 * Remove a selection listener
260 * The listener to remove
262 public void removeSelectionListener(SelectionListener listener
) {
263 if (null != _selectionListeners
) {
264 _selectionListeners
.remove(listener
);
269 * Selection changed callback
271 public void fireSelectionChanged() {
272 if (null != _selectionListeners
) {
273 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
274 while (it
.hasNext()) {
275 SelectionListener listener
= it
.next();
276 listener
.widgetSelected(null);
282 * Default selection callback
284 public void fireDefaultSelection() {
285 if (null != _selectionListeners
) {
286 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
287 while (it
.hasNext()) {
288 SelectionListener listener
= it
.next();
289 listener
.widgetDefaultSelected(null);
295 * Get the traces in the model
297 * @return The array of traces
299 public ITimeGraphEntry
[] getTraces() {
300 return _data
.getTraces();
304 * Get the on/off trace filters
306 * @return The array of filters
308 public boolean[] getTraceFilter() {
309 return _data
.getTraceFilter();
313 * Refresh the data for the thing
315 public void refreshData() {
322 * Refresh data for the given traces
325 * The traces to refresh
327 public void refreshData(ITimeGraphEntry
[] traces
) {
328 _data
.refreshData(traces
);
334 * Adjust the scoll bars
336 public void adjustScrolls() {
337 if (null == _timeProvider
) {
338 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
344 long time0
= _timeProvider
.getTime0();
345 long time1
= _timeProvider
.getTime1();
347 long timeMin
= _timeProvider
.getMinTime();
348 long timeMax
= _timeProvider
.getMaxTime();
350 long delta
= timeMax
- timeMin
;
353 int thumb
= H_SCROLLBAR_MAX
;
356 // Thumb size (page size)
357 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
358 // At the beginning of visible window
359 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
362 // position, minimum, maximum, thumb size, increment (half page)t, page
363 // increment size (full page)
364 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
367 boolean ensureVisibleItem(int idx
, boolean redraw
) {
368 boolean changed
= false;
371 for (index
= 0; index
< _data
._expandedItems
.length
; index
++) {
372 if (_data
._expandedItems
[index
]._selected
) {
377 if (index
>= _data
._expandedItems
.length
) {
380 if (index
< _topIndex
) {
382 //FIXME:getVerticalBar().setSelection(_topItem);
388 int page
= countPerPage();
389 if (index
>= _topIndex
+ page
) {
390 setTopIndex(index
- page
+ 1);
391 //FIXME:getVerticalBar().setSelection(_topItem);
402 * Assign the given index as the top one
407 public void setTopIndex(int idx
) {
408 int index
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
409 index
= Math
.max(0, index
);
415 * Set the expanded state of a given entry
420 * True if expanded, false if collapsed
422 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
423 Item item
= _data
.findItem(entry
);
424 if (item
!= null && item
._expanded
!= expanded
) {
425 item
._expanded
= expanded
;
426 _data
.updateExpandedItems();
432 * Collapses all nodes of the viewer's tree, starting with the root.
436 public void collapseAll() {
437 for (Item item
: _data
._items
) {
438 item
._expanded
= false;
440 _data
.updateExpandedItems();
445 * Expands all nodes of the viewer's tree, starting with the root.
449 public void expandAll() {
450 for (Item item
: _data
._items
) {
451 item
._expanded
= true;
453 _data
.updateExpandedItems();
458 * Add a tree listener
461 * The listener to add
463 public void addTreeListener(ITimeGraphTreeListener listener
) {
464 if (!_treeListeners
.contains(listener
)) {
465 _treeListeners
.add(listener
);
470 * Remove a tree listener
473 * The listener to remove
475 public void removeTreeListener(ITimeGraphTreeListener listener
) {
476 if (_treeListeners
.contains(listener
)) {
477 _treeListeners
.remove(listener
);
482 * Tree event callback
487 * The expanded state (true for expanded, false for collapsed)
489 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
490 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
491 for (ITimeGraphTreeListener listener
: _treeListeners
) {
493 listener
.treeExpanded(event
);
495 listener
.treeCollapsed(event
);
501 * Add a menu listener on {@link ITimeGraphEntry}s
503 * The listener to add
506 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
507 if (!_timeGraphEntryMenuListeners
.contains(listener
)) {
508 _timeGraphEntryMenuListeners
.add(listener
);
513 * Remove a menu listener on {@link ITimeGraphEntry}s
516 * The listener to remove
519 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
520 if (_timeGraphEntryMenuListeners
.contains(listener
)) {
521 _timeGraphEntryMenuListeners
.remove(listener
);
526 * Menu event callback on {@link ITimeGraphEntry}s
529 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
531 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
532 for (MenuDetectListener listener
: _timeGraphEntryMenuListeners
) {
533 listener
.menuDetected(event
);
538 * Add a menu listener on {@link ITimeEvent}s
541 * The listener to add
544 public void addTimeEventMenuListener(MenuDetectListener listener
) {
545 if (!_timeEventMenuListeners
.contains(listener
)) {
546 _timeEventMenuListeners
.add(listener
);
551 * Remove a menu listener on {@link ITimeEvent}s
554 * The listener to remove
557 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
558 if (_timeEventMenuListeners
.contains(listener
)) {
559 _timeEventMenuListeners
.remove(listener
);
564 * Menu event callback on {@link ITimeEvent}s
567 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
569 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
570 for (MenuDetectListener listener
: _timeEventMenuListeners
) {
571 listener
.menuDetected(event
);
576 public ISelection
getSelection() {
577 TimeGraphSelection sel
= new TimeGraphSelection();
578 ITimeGraphEntry trace
= getSelectedTrace();
579 if (null != trace
&& null != _timeProvider
) {
580 long selectedTime
= _timeProvider
.getSelectedTime();
581 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
592 * Get the selection object
594 * @return The selection
596 public ISelection
getSelectionTrace() {
597 TimeGraphSelection sel
= new TimeGraphSelection();
598 ITimeGraphEntry trace
= getSelectedTrace();
606 * Enable/disable one of the traces in the model
609 * 1 to enable it, -1 to disable. The method returns immediately
610 * if another value is used.
612 public void selectTrace(int n
) {
613 if ((n
!= 1) && (n
!= -1)) {
617 boolean changed
= false;
618 int lastSelection
= -1;
619 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
620 Item item
= _data
._expandedItems
[i
];
621 if (item
._selected
) {
623 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
624 item
._selected
= false;
625 item
= _data
._expandedItems
[i
+ 1];
626 item
._selected
= true;
628 } else if ((-1 == n
) && (i
> 0)) {
629 item
._selected
= false;
630 item
= _data
._expandedItems
[i
- 1];
631 item
._selected
= true;
638 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
639 Item item
= _data
._expandedItems
[0];
640 item
._selected
= true;
645 ensureVisibleItem(-1, false);
647 fireSelectionChanged();
655 * 1 for next event, -1 for previous event
657 public void selectEvent(int n
) {
658 if (null == _timeProvider
) {
661 ITimeGraphEntry trace
= getSelectedTrace();
665 long selectedTime
= _timeProvider
.getSelectedTime();
666 long endTime
= _timeProvider
.getEndTime();
667 ITimeEvent nextEvent
;
668 if (-1 == n
&& selectedTime
> endTime
) {
669 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
671 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
673 if (null == nextEvent
&& -1 == n
) {
674 nextEvent
= Utils
.getFirstEvent(trace
);
676 if (null != nextEvent
) {
677 long nextTime
= nextEvent
.getTime();
678 // If last event detected e.g. going back or not moving to a next
680 if (nextTime
<= selectedTime
&& n
== 1) {
681 // Select to the end of this last event
682 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
683 // but not beyond the end of the trace
684 if (nextTime
> endTime
) {
687 } else if (n
== -1) {
688 // for previous event go to its end time unless we were already there
689 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
690 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
693 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
694 fireSelectionChanged();
696 _timeProvider
.setSelectedTimeNotify(endTime
, true);
697 fireSelectionChanged();
702 * Select the next event
704 public void selectNextEvent() {
706 // Notify if visible time window has been adjusted
707 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
711 * Select the previous event
713 public void selectPrevEvent() {
715 // Notify if visible time window has been adjusted
716 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
720 * Select the next trace
722 public void selectNextTrace() {
727 * Select the previous trace
729 public void selectPrevTrace() {
734 * Zoom based on mouse cursor location with mouse scrolling
736 * @param zoomIn true to zoom in, false to zoom out
738 public void zoom(boolean zoomIn
) {
739 int globalX
= getDisplay().getCursorLocation().x
;
740 Point p
= toControl(globalX
, 0);
741 int nameSpace
= _timeProvider
.getNameSpace();
742 int timeSpace
= _timeProvider
.getTimeSpace();
743 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
744 long time0
= _timeProvider
.getTime0();
745 long time1
= _timeProvider
.getTime1();
746 long interval
= time1
- time0
;
749 } // to allow getting out of single point interval
752 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
754 newInterval
= (long) Math
.ceil(interval
* 1.25);
756 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
757 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
758 long newTime1
= newTime0
+ newInterval
;
759 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
760 synchronized (fMouseScrollNotifierLock
) {
761 if (fMouseScrollNotifier
== null) {
762 fMouseScrollNotifier
= new MouseScrollNotifier();
763 fMouseScrollNotifier
.start();
765 fMouseScrollNotifier
.mouseScrolled();
770 * zoom in using single click
772 public void zoomIn() {
773 long _time0
= _timeProvider
.getTime0();
774 long _time1
= _timeProvider
.getTime1();
775 long _range
= _time1
- _time0
;
776 long selTime
= _timeProvider
.getSelectedTime();
777 if (selTime
<= _time0
|| selTime
>= _time1
) {
778 selTime
= (_time0
+ _time1
) / 2;
780 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
781 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
783 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
785 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
786 // + time1 + " inaccuracy:" + inaccuracy);
788 if (inaccuracy
> 0 && inaccuracy
< 100) {
789 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
793 long m
= _timeProvider
.getMinTimeInterval();
794 if ((time1
- time0
) < m
) {
795 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
799 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
803 * zoom out using single click
805 public void zoomOut() {
806 long _time0
= _timeProvider
.getTime0();
807 long _time1
= _timeProvider
.getTime1();
808 long selTime
= _timeProvider
.getSelectedTime();
809 if (selTime
<= _time0
|| selTime
>= _time1
) {
810 selTime
= (_time0
+ _time1
) / 2;
812 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
813 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
815 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
816 if (inaccuracy
> 0 && inaccuracy
< 100) {
817 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
821 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
825 * Return the currently selected trace
827 * @return The entry matching the trace
829 public ITimeGraphEntry
getSelectedTrace() {
830 ITimeGraphEntry trace
= null;
831 int idx
= getSelectedIndex();
833 trace
= _data
._expandedItems
[idx
]._trace
;
839 * Retrieve the index of the currently selected item
843 public int getSelectedIndex() {
845 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
846 Item item
= _data
._expandedItems
[i
];
847 if (item
._selected
) {
855 boolean toggle(int idx
) {
856 boolean toggled
= false;
857 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
858 Item item
= _data
._expandedItems
[idx
];
859 if (item
._hasChildren
) {
860 item
._expanded
= !item
._expanded
;
861 _data
.updateExpandedItems();
865 fireTreeEvent(item
._trace
, item
._expanded
);
871 int getItemIndexAtY(int y
) {
875 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
877 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
878 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
885 int idx
= y
/ _itemHeight
;
887 if (idx
< _data
._expandedItems
.length
) {
893 boolean isOverSplitLine(int x
) {
894 if (x
< 0 || null == _timeProvider
) {
898 int nameWidth
= _timeProvider
.getNameSpace();
899 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
905 ITimeGraphEntry
getEntry(Point pt
) {
906 int idx
= getItemIndexAtY(pt
.y
);
907 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
911 * Return the x coordinate corresponding to a time
913 * @param time the time
914 * @return the x coordinate corresponding to the time
918 public int getXForTime(long time
) {
919 if (null == _timeProvider
) {
922 long time0
= _timeProvider
.getTime0();
923 long time1
= _timeProvider
.getTime1();
924 int width
= getCtrlSize().x
;
925 int nameSpace
= _timeProvider
.getNameSpace();
926 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
927 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
932 * Return the time corresponding to an x coordinate
934 * @param coord The X coordinate
935 * @return The time corresponding to the x coordinate
939 public long getTimeAtX(int coord
) {
940 if (null == _timeProvider
) {
944 Point size
= getCtrlSize();
945 long time0
= _timeProvider
.getTime0();
946 long time1
= _timeProvider
.getTime1();
947 int nameWidth
= _timeProvider
.getNameSpace();
948 final int x
= coord
- nameWidth
;
949 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
950 if (x
>= 0 && size
.x
>= nameWidth
) {
951 if (time1
- time0
> timeWidth
) {
952 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
953 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
955 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
956 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
962 void selectItem(int idx
, boolean addSelection
) {
963 boolean changed
= false;
965 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
966 Item item
= _data
._expandedItems
[idx
];
967 changed
= (item
._selected
== false);
968 item
._selected
= true;
971 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
972 Item item
= _data
._expandedItems
[i
];
973 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
976 item
._selected
= i
== idx
;
979 changed
|= ensureVisibleItem(idx
, true);
986 * Callback for item selection
989 * The entry matching the trace
990 * @param addSelection
991 * If the selection is added or removed
993 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
994 int idx
= _data
.findItemIndex(trace
);
995 selectItem(idx
, addSelection
);
999 * Retrieve the number of entries shown per page.
1003 public int countPerPage() {
1004 int height
= getCtrlSize().y
;
1006 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1008 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
1009 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1010 if (ySum
>= height
) {
1015 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
1016 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1017 if (ySum
>= height
) {
1025 count
= height
/ _itemHeight
;
1031 * Get the index of the top element
1035 public int getTopIndex() {
1040 * Get the number of expanded items
1042 * @return The count of expanded items
1044 public int getExpandedElementCount() {
1045 return _data
._expandedItems
.length
;
1049 * Get an array of all expanded elements
1051 * @return The expanded elements
1053 public ITimeGraphEntry
[] getExpandedElements() {
1054 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1055 for (Item item
: _data
._expandedItems
) {
1056 elements
.add(item
._trace
);
1058 return elements
.toArray(new ITimeGraphEntry
[0]);
1061 Point
getCtrlSize() {
1062 Point size
= getSize();
1063 if (getHorizontalBar().isVisible()) {
1064 size
.y
-= getHorizontalBar().getSize().y
;
1069 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1071 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1072 int width
= nameWidth
;
1073 int height
= _itemHeight
;
1074 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1076 for (int i
= _topIndex
; i
< idx
; i
++) {
1077 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1080 height
= _data
._expandedItems
[idx
].itemHeight
;
1082 return new Rectangle(x
, y
, width
, height
);
1085 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1086 int x
= bound
.x
+ nameWidth
;
1087 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1088 int width
= bound
.width
- x
;
1089 int height
= _itemHeight
;
1090 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1092 for (int i
= _topIndex
; i
< idx
; i
++) {
1093 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1096 height
= _data
._expandedItems
[idx
].itemHeight
;
1098 return new Rectangle(x
, y
, width
, height
);
1102 void paint(Rectangle bounds
, PaintEvent e
) {
1104 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
1105 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1107 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
1111 _idealNameSpace
= 0;
1112 int nameSpace
= _timeProvider
.getNameSpace();
1114 // draw empty name space background
1115 gc
.setBackground(_colors
.getBkColor(false, false, true));
1116 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1118 if (_dragState
== DRAG_ZOOM
) {
1119 // draw selected zoom region background
1120 gc
.setBackground(_colors
.getBkColor(false, false, true));
1121 if (_dragX0
< _dragX
) {
1122 gc
.fillRectangle(new Rectangle(_dragX0
, bounds
.y
, _dragX
- _dragX0
, bounds
.height
));
1123 } else if (_dragX0
> _dragX
) {
1124 gc
.fillRectangle(new Rectangle(_dragX
, bounds
.y
, _dragX0
- _dragX
, bounds
.height
));
1128 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1130 // draw selected time
1131 long time0
= _timeProvider
.getTime0();
1132 long time1
= _timeProvider
.getTime1();
1133 long selectedTime
= _timeProvider
.getSelectedTime();
1134 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1135 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1136 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1137 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1138 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1141 // draw drag line, no line if name space is 0.
1142 if (DRAG_SPLIT_LINE
== _dragState
) {
1143 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1144 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1145 } else if (DRAG_ZOOM
== _dragState
&& Math
.max(_dragX
, _dragX0
) > nameSpace
) {
1146 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1147 gc
.drawLine(_dragX0
, bounds
.y
, _dragX0
, bounds
.y
+ bounds
.height
- 1);
1148 gc
.drawLine(_dragX
, bounds
.y
, _dragX
, bounds
.y
+ bounds
.height
- 1);
1149 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1150 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1151 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1156 * Draw many items at once
1159 * The rectangle of the area
1160 * @param timeProvider
1163 * The array items to draw
1165 * The index of the first element to draw
1167 * The width reserved for the names
1169 * Reference to the SWT GC object
1171 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1172 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1173 for (int i
= topIndex
; i
< items
.length
; i
++) {
1174 Item item
= items
[i
];
1175 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1177 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1183 * @param item the item to draw
1184 * @param bounds the container rectangle
1185 * @param timeProvider Time provider
1186 * @param i the item index
1187 * @param nameSpace the name space
1188 * @param gc Graphics context
1190 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1191 ITimeGraphEntry entry
= item
._trace
;
1192 long time0
= timeProvider
.getTime0();
1193 long time1
= timeProvider
.getTime1();
1194 long selectedTime
= timeProvider
.getSelectedTime();
1196 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1197 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1201 if (! item
._trace
.hasTimeEvents()) {
1202 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1203 nameRect
.width
+= statesRect
.width
;
1204 drawName(item
, nameRect
, gc
);
1206 drawName(item
, nameRect
, gc
);
1208 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1209 if (rect
.isEmpty()) {
1210 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1213 if (time1
<= time0
) {
1214 gc
.setBackground(_colors
.getBkColor(false, false, false));
1215 gc
.fillRectangle(rect
);
1216 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1220 // Initialize _rect1 to same values as enclosing rectangle rect
1221 Rectangle stateRect
= Utils
.clone(rect
);
1222 boolean selected
= item
._selected
;
1223 // K pixels per second
1224 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1226 if (item
._trace
.hasTimeEvents()) {
1227 fillSpace(rect
, gc
, selected
);
1228 // Drawing rectangle is smaller than reserved space
1230 stateRect
.height
-= 6;
1232 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1233 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1236 while (iterator
.hasNext()) {
1237 ITimeEvent event
= iterator
.next();
1238 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1239 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1240 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1241 // event is out of bounds
1244 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1245 stateRect
.x
= Math
.max(rect
.x
, x
);
1246 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1247 if (stateRect
.x
== lastX
) {
1248 stateRect
.width
-= 1;
1249 if (stateRect
.width
> 0) {
1250 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1251 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1257 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1258 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
1261 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1265 * Draw the name of an item.
1270 * Where to draw the name
1274 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1275 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1276 if (! hasTimeEvents
) {
1277 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1278 gc
.fillRectangle(bounds
);
1279 if (item
._selected
&& _isInFocus
) {
1280 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1281 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1284 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1285 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1286 gc
.fillRectangle(bounds
);
1289 // No name to be drawn
1290 if (_timeProvider
.getNameSpace() == 0) {
1294 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1295 if (item
._hasChildren
) {
1296 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1297 gc
.setBackground(_colors
.getBkColor(false, false, false));
1298 Rectangle rect
= Utils
.clone(bounds
);
1299 rect
.x
+= leftMargin
;
1300 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1301 rect
.width
= EXPAND_SIZE
;
1302 rect
.height
= EXPAND_SIZE
;
1303 gc
.fillRectangle(rect
);
1304 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1305 int midy
= rect
.y
+ rect
.height
/ 2;
1306 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1307 if (!item
._expanded
) {
1308 int midx
= rect
.x
+ rect
.width
/ 2;
1309 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1312 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1314 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1317 int imgHeight
= img
.getImageData().height
;
1318 int imgWidth
= img
.getImageData().width
;
1320 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1321 gc
.drawImage(img
, x
, y
);
1322 leftMargin
+= imgWidth
+ MARGIN
;
1324 String name
= item
._name
;
1325 Point size
= gc
.stringExtent(name
);
1326 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1327 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1329 if (hasTimeEvents
) {
1330 // cut long string with "..."
1331 int width
= bounds
.width
- leftMargin
;
1333 while (size
.x
> width
&& name
.length() > 1) {
1335 name
= name
.substring(0, name
.length() - 1);
1336 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1339 name
+= "..."; //$NON-NLS-1$
1342 Rectangle rect
= Utils
.clone(bounds
);
1343 rect
.x
+= leftMargin
;
1344 rect
.width
-= leftMargin
;
1346 if (rect
.width
> 0) {
1347 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1348 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1349 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1350 leftMargin
+= textWidth
+ MARGIN
;
1353 if (hasTimeEvents
) {
1355 int x
= bounds
.x
+ leftMargin
;
1356 int width
= bounds
.width
- x
;
1357 int midy
= bounds
.y
+ bounds
.height
/ 2;
1358 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1359 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1365 * Draw the state (color fill)
1370 * Time event for which we're drawing the state
1376 * Is this time event currently selected (so it appears
1378 * @param timeSelected
1379 * Is the timestamp currently selected
1381 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1382 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1384 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1388 boolean visible
= rect
.width
== 0 ?
false : true;
1391 Color stateColor
= null;
1392 if (colorIdx
< fEventColorMap
.length
) {
1393 stateColor
= fEventColorMap
[colorIdx
];
1395 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1398 boolean reallySelected
= timeSelected
&& selected
;
1399 if (reallySelected
) {
1400 // modify the color?
1402 // fill all rect area
1403 gc
.setBackground(stateColor
);
1404 gc
.fillRectangle(rect
);
1405 // get the border color?
1406 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1409 if (!reallySelected
) {
1410 // Draw the top and bottom borders i.e. no side borders
1412 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1414 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1417 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1418 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1420 // selected rectangle area is not visible but can be represented
1421 // with a broken vertical line of specified width.
1424 gc.setForeground(stateColor);
1425 int s = gc.getLineStyle();
1426 int w = gc.getLineWidth();
1427 gc.setLineStyle(SWT.LINE_DOT);
1428 gc.setLineWidth(width);
1429 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1430 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1432 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1435 if (!timeSelected) {
1436 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1437 gc.drawPoint(rect.x, rect.y);
1438 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1442 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1446 * Fill the space between two contiguous time events
1453 * Is this time event selected or not
1455 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1456 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1457 gc
.fillRectangle(rect
);
1458 if (_dragState
== DRAG_ZOOM
) {
1459 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, true));
1460 if (_dragX0
< _dragX
) {
1461 gc
.fillRectangle(new Rectangle(_dragX0
, rect
.y
, _dragX
- _dragX0
, rect
.height
));
1462 } else if (_dragX0
> _dragX
) {
1463 gc
.fillRectangle(new Rectangle(_dragX
, rect
.y
, _dragX0
- _dragX
, rect
.height
));
1467 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1468 int midy
= rect
.y
+ rect
.height
/ 2;
1469 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1473 public void keyTraversed(TraverseEvent e
) {
1474 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1480 public void keyPressed(KeyEvent e
) {
1482 if (_data
._expandedItems
.length
== 0) {
1485 if (SWT
.HOME
== e
.keyCode
) {
1487 } else if (SWT
.END
== e
.keyCode
) {
1488 idx
= _data
._expandedItems
.length
- 1;
1489 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1490 idx
= getSelectedIndex();
1493 } else if (idx
< _data
._expandedItems
.length
- 1) {
1496 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1497 idx
= getSelectedIndex();
1500 } else if (idx
> 0) {
1503 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1505 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1507 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1508 int page
= countPerPage();
1509 idx
= getSelectedIndex();
1514 if (idx
>= _data
._expandedItems
.length
) {
1515 idx
= _data
._expandedItems
.length
- 1;
1517 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1518 int page
= countPerPage();
1519 idx
= getSelectedIndex();
1527 } else if (SWT
.CR
== e
.keyCode
) {
1528 idx
= getSelectedIndex();
1530 if (_data
._expandedItems
[idx
]._hasChildren
) {
1533 fireDefaultSelection();
1539 selectItem(idx
, false);
1540 fireSelectionChanged();
1545 public void keyReleased(KeyEvent e
) {
1549 public void focusGained(FocusEvent e
) {
1551 if (mouseScrollFilterListener
== null) {
1552 mouseScrollFilterListener
= new Listener() {
1553 // This filter is used to prevent horizontal scrolling of the view
1554 // when the mouse wheel is used to zoom
1556 public void handleEvent(Event event
) {
1560 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1566 public void focusLost(FocusEvent e
) {
1568 if (mouseScrollFilterListener
!= null) {
1569 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1570 mouseScrollFilterListener
= null;
1572 if (DRAG_NONE
!= _dragState
) {
1574 _dragState
= DRAG_NONE
;
1580 * @return If the current view is focused
1582 public boolean isInFocus() {
1587 * Provide the possibility to control the wait cursor externally e.g. data
1588 * requests in progress
1590 * @param waitInd Should we wait indefinitely?
1592 public void waitCursor(boolean waitInd
) {
1593 // Update cursor as indicated
1595 setCursor(_WaitCursor
);
1596 _isWaitCursor
= true;
1599 _isWaitCursor
= false;
1602 // Get ready for next mouse move
1603 _isDragCursor3
= false;
1608 * If the x, y position is over the vertical split line (name to time
1609 * ranges), then change the cursor to a drag cursor to indicate the user the
1610 * possibility of resizing
1616 void updateCursor(int x
, int y
) {
1617 // if Wait cursor not active, check for the need to change to a drag
1619 if (_isWaitCursor
== false) {
1620 boolean isSplitLine
= isOverSplitLine(x
);
1621 // No dragcursor is name space is fixed to zero
1622 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1623 setCursor(_dragCursor3
);
1624 _isDragCursor3
= true;
1625 } else if (!isSplitLine
&& _isDragCursor3
) {
1627 _isDragCursor3
= false;
1633 public void mouseMove(MouseEvent e
) {
1634 if (null == _timeProvider
) {
1637 Point size
= getCtrlSize();
1638 if (DRAG_TRACE_ITEM
== _dragState
) {
1639 int nameWidth
= _timeProvider
.getNameSpace();
1640 int x
= e
.x
- nameWidth
;
1641 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1643 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1644 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1645 long time1
= _time1bak
- timeDelta
;
1646 long maxTime
= _timeProvider
.getMaxTime();
1647 if (time1
> maxTime
) {
1650 long time0
= time1
- (_time1bak
- _time0bak
);
1651 if (time0
< _timeProvider
.getMinTime()) {
1652 time0
= _timeProvider
.getMinTime();
1653 time1
= time0
+ (_time1bak
- _time0bak
);
1655 _timeProvider
.setStartFinishTime(time0
, time1
);
1657 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1659 _timeProvider
.setNameSpace(e
.x
);
1660 } else if (DRAG_ZOOM
== _dragState
) {
1661 _dragX
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1663 } else if (DRAG_NONE
== _dragState
) {
1664 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1665 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1668 _mouseOverSplitLine
= mouseOverSplitLine
;
1670 updateCursor(e
.x
, e
.y
);
1674 public void mouseDoubleClick(MouseEvent e
) {
1675 if (null == _timeProvider
) {
1678 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1679 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1680 _timeProvider
.setNameSpace(_idealNameSpace
);
1681 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1682 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1685 _mouseOverSplitLine
= mouseOverSplitLine
;
1688 int idx
= getItemIndexAtY(e
.y
);
1690 selectItem(idx
, false);
1691 fireDefaultSelection();
1697 public void mouseDown(MouseEvent e
) {
1698 if (_dragState
!= DRAG_NONE
|| null == _timeProvider
) {
1702 if (1 == e
.button
) {
1703 int nameSpace
= _timeProvider
.getNameSpace();
1704 if (nameSpace
!= 0) {
1705 if (isOverSplitLine(e
.x
)) {
1706 _dragState
= DRAG_SPLIT_LINE
;
1707 _dragX
= _dragX0
= e
.x
;
1708 _time0bak
= _timeProvider
.getTime0();
1709 _time1bak
= _timeProvider
.getTime1();
1715 idx
= getItemIndexAtY(e
.y
);
1717 Item item
= _data
._expandedItems
[idx
];
1718 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1721 long hitTime
= getTimeAtX(e
.x
);
1723 // _timeProvider.setSelectedTimeInt(hitTime, false);
1725 _dragState
= DRAG_TRACE_ITEM
;
1726 _dragX
= _dragX0
= e
.x
- nameSpace
;
1727 _time0bak
= _timeProvider
.getTime0();
1728 _time1bak
= _timeProvider
.getTime1();
1731 selectItem(idx
, false);
1732 fireSelectionChanged();
1734 selectItem(idx
, false); // clear selection
1736 fireSelectionChanged();
1738 } else if (3 == e
.button
) {
1739 if (_timeProvider
.getTime0() == _timeProvider
.getTime1() || getCtrlSize().x
- _timeProvider
.getNameSpace() <= 0) {
1743 _dragX
= _dragX0
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1744 _dragState
= DRAG_ZOOM
;
1749 public void mouseUp(MouseEvent e
) {
1750 if (DRAG_NONE
!= _dragState
) {
1752 if (e
.button
== 1 && DRAG_TRACE_ITEM
== _dragState
) {
1753 if (_dragX
== _dragX0
) { // click without drag
1754 long time
= getTimeAtX(e
.x
);
1755 _timeProvider
.setSelectedTimeNotify(time
, false);
1757 // Notify time provider to check the need for listener
1759 _timeProvider
.notifyStartFinishTime();
1761 _dragState
= DRAG_NONE
;
1762 } else if (e
.button
== 1 && DRAG_SPLIT_LINE
== _dragState
) {
1764 _dragState
= DRAG_NONE
;
1765 } else if (e
.button
== 3 && DRAG_ZOOM
== _dragState
) {
1766 int nameWidth
= _timeProvider
.getNameSpace();
1767 if (Math
.max(_dragX
, _dragX0
) > nameWidth
&& _dragX
!= _dragX0
) {
1768 long time0
= getTimeAtX(_dragX0
);
1769 long time1
= getTimeAtX(_dragX
);
1770 if (time0
< time1
) {
1771 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1773 _timeProvider
.setStartFinishTimeNotify(time1
, time0
);
1778 _dragState
= DRAG_NONE
;
1784 public void mouseEnter(MouseEvent e
) {
1788 public void mouseExit(MouseEvent e
) {
1789 if (_mouseOverSplitLine
) {
1790 _mouseOverSplitLine
= false;
1796 public void mouseHover(MouseEvent e
) {
1800 public void mouseScrolled(MouseEvent e
) {
1801 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1804 boolean zoomScroll
= false;
1805 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1806 Point parentSize
= getParent().getSize();
1807 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1808 // over the parent control
1809 if (e
.x
> getCtrlSize().x
) {
1810 // over the horizontal scroll bar
1812 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1813 // over the name space
1819 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1822 } else if (e
.count
< 0) {
1826 setTopIndex(getTopIndex() - e
.count
);
1831 public void controlMoved(ControlEvent e
) {
1835 public void controlResized(ControlEvent e
) {
1840 public void widgetDefaultSelected(SelectionEvent e
) {
1844 public void widgetSelected(SelectionEvent e
) {
1845 if (e
.widget
== getVerticalBar()) {
1846 setTopIndex(getVerticalBar().getSelection());
1847 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1848 int start
= getHorizontalBar().getSelection();
1849 long time0
= _timeProvider
.getTime0();
1850 long time1
= _timeProvider
.getTime1();
1851 long timeMin
= _timeProvider
.getMinTime();
1852 long timeMax
= _timeProvider
.getMaxTime();
1853 long delta
= timeMax
- timeMin
;
1855 long range
= time1
- time0
;
1856 // _timeRangeFixed = true;
1857 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1858 time1
= time0
+ range
;
1860 // TODO: Follow-up with Bug 310310
1861 // In Linux SWT.DRAG is the only value received
1862 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1863 if (e
.detail
== SWT
.DRAG
) {
1864 _timeProvider
.setStartFinishTime(time0
, time1
);
1866 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1872 * @return The current visibility of the vertical scroll bar
1874 public boolean isVisibleVerticalScroll() {
1875 return _visibleVerticalScroll
;
1879 public int getBorderWidth() {
1880 return _borderWidth
;
1884 * Set the border width
1886 * @param borderWidth
1889 public void setBorderWidth(int borderWidth
) {
1890 this._borderWidth
= borderWidth
;
1894 * @return The current height of the header row
1896 public int getHeaderHeight() {
1897 return _headerHeight
;
1901 * Set the height of the header row
1903 * @param headerHeight
1906 public void setHeaderHeight(int headerHeight
) {
1907 this._headerHeight
= headerHeight
;
1911 * @return The height of regular item rows
1913 public int getItemHeight() {
1918 * Set the height of regular itew rows
1923 public void setItemHeight(int rowHeight
) {
1924 this._itemHeight
= rowHeight
;
1928 * Set the minimum item width
1930 * @param width The minimum width
1932 public void setMinimumItemWidth(int width
) {
1933 this._minimumItemWidth
= width
;
1937 * @return The minimum item width
1939 public int getMinimumItemWidth() {
1940 return _minimumItemWidth
;
1944 * @return The entries that are currently filtered out
1946 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1947 return _data
.getFilteredOut();
1952 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1953 if (listener
!= null) {
1954 if (!_selectionChangedListeners
.contains(listener
)) {
1955 _selectionChangedListeners
.add(listener
);
1962 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1963 if (listener
!= null) {
1964 _selectionChangedListeners
.remove(listener
);
1970 public void setSelection(ISelection selection
) {
1971 if (selection
instanceof TimeGraphSelection
) {
1972 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1973 Object ob
= sel
.getFirstElement();
1974 if (ob
instanceof ITimeGraphEntry
) {
1975 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1976 selectItem(trace
, false);
1982 private class ItemData
{
1983 public Item
[] _expandedItems
= new Item
[0];
1984 public Item
[] _items
= new Item
[0];
1985 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1986 private boolean traceFilter
[] = new boolean[0];
1987 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1988 public ITimeGraphPresentationProvider provider
;
1993 Item
findItem(ITimeGraphEntry entry
) {
1994 if (entry
== null) {
1998 for (int i
= 0; i
< _items
.length
; i
++) {
1999 Item item
= _items
[i
];
2000 if (item
._trace
== entry
) {
2008 int findItemIndex(ITimeGraphEntry trace
) {
2009 if (trace
== null) {
2013 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
2014 Item item
= _expandedItems
[i
];
2015 if (item
._trace
== trace
) {
2023 public void refreshData() {
2024 List
<Item
> itemList
= new ArrayList
<Item
>();
2025 filteredOut
.clear();
2026 for (int i
= 0; i
< _traces
.length
; i
++) {
2027 ITimeGraphEntry entry
= _traces
[i
];
2028 refreshData(itemList
, null, 0, entry
);
2030 _items
= itemList
.toArray(new Item
[0]);
2031 updateExpandedItems();
2034 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2035 Item item
= new Item(entry
, entry
.getName(), level
);
2036 if (parent
!= null) {
2037 parent
.children
.add(item
);
2039 item
.itemHeight
= provider
.getItemHeight(entry
);
2041 if (entry
.hasChildren()) {
2042 item
._expanded
= true;
2043 item
._hasChildren
= true;
2044 for (ITimeGraphEntry child
: entry
.getChildren()) {
2045 refreshData(itemList
, item
, level
+ 1, child
);
2050 public void updateExpandedItems() {
2051 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2052 for (int i
= 0; i
< _traces
.length
; i
++) {
2053 ITimeGraphEntry entry
= _traces
[i
];
2054 Item item
= findItem(entry
);
2055 refreshExpanded(expandedItemList
, item
);
2057 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
2060 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2061 expandedItemList
.add(item
);
2062 if (item
._hasChildren
&& item
._expanded
) {
2063 for (Item child
: item
.children
) {
2064 refreshExpanded(expandedItemList
, child
);
2069 public void refreshData(ITimeGraphEntry traces
[]) {
2070 if (traces
== null || traces
.length
== 0) {
2072 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
2073 traceFilter
= new boolean[traces
.length
];
2074 java
.util
.Arrays
.fill(traceFilter
, true);
2081 public ITimeGraphEntry
[] getTraces() {
2085 public boolean[] getTraceFilter() {
2089 public Vector
<ITimeGraphEntry
> getFilteredOut() {
2094 private class Item
{
2095 public boolean _expanded
;
2096 public boolean _selected
;
2097 public boolean _hasChildren
;
2098 public int itemHeight
;
2100 public List
<Item
> children
;
2101 public String _name
;
2102 public ITimeGraphEntry _trace
;
2104 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2105 this._trace
= trace
;
2108 this.children
= new ArrayList
<Item
>();
2112 public String
toString() {
2121 public void menuDetected(MenuDetectEvent e
) {
2122 if (null == _timeProvider
) {
2125 Point p
= toControl(e
.x
, e
.y
);
2126 int idx
= getItemIndexAtY(p
.y
);
2127 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
2128 Item item
= _data
._expandedItems
[idx
];
2129 ITimeGraphEntry entry
= item
._trace
;
2130 if (entry
.hasTimeEvents()) {
2131 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2132 if (event
!= null) {
2134 fireMenuEventOnTimeEvent(e
);
2139 fireMenuEventOnTimeGraphEntry(e
);