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;
370 for (idx
= 0; idx
< _data
._expandedItems
.length
; idx
++) {
371 if (_data
._expandedItems
[idx
]._selected
) {
376 if (idx
>= _data
._expandedItems
.length
) {
379 if (idx
< _topIndex
) {
381 //FIXME:getVerticalBar().setSelection(_topItem);
387 int page
= countPerPage();
388 if (idx
>= _topIndex
+ page
) {
389 setTopIndex(idx
- page
+ 1);
390 //FIXME:getVerticalBar().setSelection(_topItem);
401 * Assign the given index as the top one
406 public void setTopIndex(int idx
) {
407 idx
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
408 idx
= Math
.max(0, idx
);
414 * Set the expanded state of a given entry
419 * True if expanded, false if collapsed
421 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
422 Item item
= _data
.findItem(entry
);
423 if (item
!= null && item
._expanded
!= expanded
) {
424 item
._expanded
= expanded
;
425 _data
.updateExpandedItems();
431 * Collapses all nodes of the viewer's tree, starting with the root.
435 public void collapseAll() {
436 for (Item item
: _data
._items
) {
437 item
._expanded
= false;
439 _data
.updateExpandedItems();
444 * Expands all nodes of the viewer's tree, starting with the root.
448 public void expandAll() {
449 for (Item item
: _data
._items
) {
450 item
._expanded
= true;
452 _data
.updateExpandedItems();
457 * Add a tree listener
460 * The listener to add
462 public void addTreeListener(ITimeGraphTreeListener listener
) {
463 if (!_treeListeners
.contains(listener
)) {
464 _treeListeners
.add(listener
);
469 * Remove a tree listener
472 * The listener to remove
474 public void removeTreeListener(ITimeGraphTreeListener listener
) {
475 if (_treeListeners
.contains(listener
)) {
476 _treeListeners
.remove(listener
);
481 * Tree event callback
486 * The expanded state (true for expanded, false for collapsed)
488 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
489 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
490 for (ITimeGraphTreeListener listener
: _treeListeners
) {
492 listener
.treeExpanded(event
);
494 listener
.treeCollapsed(event
);
500 * Add a menu listener on {@link ITimeGraphEntry}s
502 * The listener to add
505 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
506 if (!_timeGraphEntryMenuListeners
.contains(listener
)) {
507 _timeGraphEntryMenuListeners
.add(listener
);
512 * Remove a menu listener on {@link ITimeGraphEntry}s
515 * The listener to remove
518 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
519 if (_timeGraphEntryMenuListeners
.contains(listener
)) {
520 _timeGraphEntryMenuListeners
.remove(listener
);
525 * Menu event callback on {@link ITimeGraphEntry}s
528 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
530 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
531 for (MenuDetectListener listener
: _timeGraphEntryMenuListeners
) {
532 listener
.menuDetected(event
);
537 * Add a menu listener on {@link ITimeEvent}s
540 * The listener to add
543 public void addTimeEventMenuListener(MenuDetectListener listener
) {
544 if (!_timeEventMenuListeners
.contains(listener
)) {
545 _timeEventMenuListeners
.add(listener
);
550 * Remove a menu listener on {@link ITimeEvent}s
553 * The listener to remove
556 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
557 if (_timeEventMenuListeners
.contains(listener
)) {
558 _timeEventMenuListeners
.remove(listener
);
563 * Menu event callback on {@link ITimeEvent}s
566 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
568 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
569 for (MenuDetectListener listener
: _timeEventMenuListeners
) {
570 listener
.menuDetected(event
);
575 public ISelection
getSelection() {
576 TimeGraphSelection sel
= new TimeGraphSelection();
577 ITimeGraphEntry trace
= getSelectedTrace();
578 if (null != trace
&& null != _timeProvider
) {
579 long selectedTime
= _timeProvider
.getSelectedTime();
580 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
591 * Get the selection object
593 * @return The selection
595 public ISelection
getSelectionTrace() {
596 TimeGraphSelection sel
= new TimeGraphSelection();
597 ITimeGraphEntry trace
= getSelectedTrace();
605 * Enable/disable one of the traces in the model
608 * 1 to enable it, -1 to disable. The method returns immediately
609 * if another value is used.
611 public void selectTrace(int n
) {
612 if ((n
!= 1) && (n
!= -1)) {
616 boolean changed
= false;
617 int lastSelection
= -1;
618 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
619 Item item
= _data
._expandedItems
[i
];
620 if (item
._selected
) {
622 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
623 item
._selected
= false;
624 item
= _data
._expandedItems
[i
+ 1];
625 item
._selected
= true;
627 } else if ((-1 == n
) && (i
> 0)) {
628 item
._selected
= false;
629 item
= _data
._expandedItems
[i
- 1];
630 item
._selected
= true;
637 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
638 Item item
= _data
._expandedItems
[0];
639 item
._selected
= true;
644 ensureVisibleItem(-1, false);
646 fireSelectionChanged();
654 * 1 for next event, -1 for previous event
656 public void selectEvent(int n
) {
657 if (null == _timeProvider
) {
660 ITimeGraphEntry trace
= getSelectedTrace();
664 long selectedTime
= _timeProvider
.getSelectedTime();
665 long endTime
= _timeProvider
.getEndTime();
666 ITimeEvent nextEvent
;
667 if (-1 == n
&& selectedTime
> endTime
) {
668 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
670 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
672 if (null == nextEvent
&& -1 == n
) {
673 nextEvent
= Utils
.getFirstEvent(trace
);
675 if (null != nextEvent
) {
676 long nextTime
= nextEvent
.getTime();
677 // If last event detected e.g. going back or not moving to a next
679 if (nextTime
<= selectedTime
&& n
== 1) {
680 // Select to the end of this last event
681 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
682 // but not beyond the end of the trace
683 if (nextTime
> endTime
) {
686 } else if (n
== -1) {
687 // for previous event go to its end time unless we were already there
688 if (nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
689 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
692 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
693 fireSelectionChanged();
695 _timeProvider
.setSelectedTimeNotify(endTime
, true);
696 fireSelectionChanged();
701 * Select the next event
703 public void selectNextEvent() {
705 // Notify if visible time window has been adjusted
706 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
710 * Select the previous event
712 public void selectPrevEvent() {
714 // Notify if visible time window has been adjusted
715 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
719 * Select the next trace
721 public void selectNextTrace() {
726 * Select the previous trace
728 public void selectPrevTrace() {
733 * Zoom based on mouse cursor location with mouse scrolling
735 * @param zoomIn true to zoom in, false to zoom out
737 public void zoom(boolean zoomIn
) {
738 int globalX
= getDisplay().getCursorLocation().x
;
739 Point p
= toControl(globalX
, 0);
740 int nameSpace
= _timeProvider
.getNameSpace();
741 int timeSpace
= _timeProvider
.getTimeSpace();
742 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
743 long time0
= _timeProvider
.getTime0();
744 long time1
= _timeProvider
.getTime1();
745 long interval
= time1
- time0
;
748 } // to allow getting out of single point interval
751 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
753 newInterval
= (long) Math
.ceil(interval
* 1.25);
755 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
756 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
757 long newTime1
= newTime0
+ newInterval
;
758 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
759 synchronized (fMouseScrollNotifierLock
) {
760 if (fMouseScrollNotifier
== null) {
761 fMouseScrollNotifier
= new MouseScrollNotifier();
762 fMouseScrollNotifier
.start();
764 fMouseScrollNotifier
.mouseScrolled();
769 * zoom in using single click
771 public void zoomIn() {
772 long _time0
= _timeProvider
.getTime0();
773 long _time1
= _timeProvider
.getTime1();
774 long _range
= _time1
- _time0
;
775 long selTime
= _timeProvider
.getSelectedTime();
776 if (selTime
<= _time0
|| selTime
>= _time1
) {
777 selTime
= (_time0
+ _time1
) / 2;
779 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
780 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
782 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
784 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
785 // + time1 + " inaccuracy:" + inaccuracy);
787 if (inaccuracy
> 0 && inaccuracy
< 100) {
788 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
792 long m
= _timeProvider
.getMinTimeInterval();
793 if ((time1
- time0
) < m
) {
794 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
798 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
802 * zoom out using single click
804 public void zoomOut() {
805 long _time0
= _timeProvider
.getTime0();
806 long _time1
= _timeProvider
.getTime1();
807 long selTime
= _timeProvider
.getSelectedTime();
808 if (selTime
<= _time0
|| selTime
>= _time1
) {
809 selTime
= (_time0
+ _time1
) / 2;
811 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
812 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
814 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
815 if (inaccuracy
> 0 && inaccuracy
< 100) {
816 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
820 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
824 * Return the currently selected trace
826 * @return The entry matching the trace
828 public ITimeGraphEntry
getSelectedTrace() {
829 ITimeGraphEntry trace
= null;
830 int idx
= getSelectedIndex();
832 trace
= _data
._expandedItems
[idx
]._trace
;
838 * Retrieve the index of the currently selected item
842 public int getSelectedIndex() {
844 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
845 Item item
= _data
._expandedItems
[i
];
846 if (item
._selected
) {
854 boolean toggle(int idx
) {
855 boolean toggled
= false;
856 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
857 Item item
= _data
._expandedItems
[idx
];
858 if (item
._hasChildren
) {
859 item
._expanded
= !item
._expanded
;
860 _data
.updateExpandedItems();
864 fireTreeEvent(item
._trace
, item
._expanded
);
870 int getItemIndexAtY(int y
) {
874 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
876 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
877 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
884 int idx
= y
/ _itemHeight
;
886 if (idx
< _data
._expandedItems
.length
) {
892 boolean isOverSplitLine(int x
) {
893 if (x
< 0 || null == _timeProvider
) {
897 int nameWidth
= _timeProvider
.getNameSpace();
898 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
904 ITimeGraphEntry
getEntry(Point pt
) {
905 int idx
= getItemIndexAtY(pt
.y
);
906 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
910 * Return the x coordinate corresponding to a time
912 * @param time the time
913 * @return the x coordinate corresponding to the time
917 public int getXForTime(long time
) {
918 if (null == _timeProvider
) {
921 long time0
= _timeProvider
.getTime0();
922 long time1
= _timeProvider
.getTime1();
923 int width
= getCtrlSize().x
;
924 int nameSpace
= _timeProvider
.getNameSpace();
925 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
926 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
931 * Return the time corresponding to an x coordinate
933 * @param x the x coordinate
934 * @return the time corresponding to the x coordinate
938 public long getTimeAtX(int x
) {
939 if (null == _timeProvider
) {
943 Point size
= getCtrlSize();
944 long time0
= _timeProvider
.getTime0();
945 long time1
= _timeProvider
.getTime1();
946 int nameWidth
= _timeProvider
.getNameSpace();
948 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
949 if (x
>= 0 && size
.x
>= nameWidth
) {
950 if (time1
- time0
> timeWidth
) {
951 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
952 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
954 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
955 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
961 void selectItem(int idx
, boolean addSelection
) {
962 boolean changed
= false;
964 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
965 Item item
= _data
._expandedItems
[idx
];
966 changed
= (item
._selected
== false);
967 item
._selected
= true;
970 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
971 Item item
= _data
._expandedItems
[i
];
972 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
975 item
._selected
= i
== idx
;
978 changed
|= ensureVisibleItem(idx
, true);
985 * Callback for item selection
988 * The entry matching the trace
989 * @param addSelection
990 * If the selection is added or removed
992 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
993 int idx
= _data
.findItemIndex(trace
);
994 selectItem(idx
, addSelection
);
998 * Retrieve the number of entries shown per page.
1002 public int countPerPage() {
1003 int height
= getCtrlSize().y
;
1005 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1007 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
1008 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1009 if (ySum
>= height
) {
1014 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
1015 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
1016 if (ySum
>= height
) {
1024 count
= height
/ _itemHeight
;
1030 * Get the index of the top element
1034 public int getTopIndex() {
1039 * Get the number of expanded items
1041 * @return The count of expanded items
1043 public int getExpandedElementCount() {
1044 return _data
._expandedItems
.length
;
1048 * Get an array of all expanded elements
1050 * @return The expanded elements
1052 public ITimeGraphEntry
[] getExpandedElements() {
1053 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1054 for (Item item
: _data
._expandedItems
) {
1055 elements
.add(item
._trace
);
1057 return elements
.toArray(new ITimeGraphEntry
[0]);
1060 Point
getCtrlSize() {
1061 Point size
= getSize();
1062 if (getHorizontalBar().isVisible()) {
1063 size
.y
-= getHorizontalBar().getSize().y
;
1068 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1070 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1071 int width
= nameWidth
;
1072 int height
= _itemHeight
;
1073 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1075 for (int i
= _topIndex
; i
< idx
; i
++) {
1076 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1079 height
= _data
._expandedItems
[idx
].itemHeight
;
1081 return new Rectangle(x
, y
, width
, height
);
1084 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1085 int x
= bound
.x
+ nameWidth
;
1086 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1087 int width
= bound
.width
- x
;
1088 int height
= _itemHeight
;
1089 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1091 for (int i
= _topIndex
; i
< idx
; i
++) {
1092 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1095 height
= _data
._expandedItems
[idx
].itemHeight
;
1097 return new Rectangle(x
, y
, width
, height
);
1101 void paint(Rectangle bounds
, PaintEvent e
) {
1103 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
1104 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1106 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
1110 _idealNameSpace
= 0;
1111 int nameSpace
= _timeProvider
.getNameSpace();
1113 // draw empty name space background
1114 gc
.setBackground(_colors
.getBkColor(false, false, true));
1115 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1117 if (_dragState
== DRAG_ZOOM
) {
1118 // draw selected zoom region background
1119 gc
.setBackground(_colors
.getBkColor(false, false, true));
1120 if (_dragX0
< _dragX
) {
1121 gc
.fillRectangle(new Rectangle(_dragX0
, bounds
.y
, _dragX
- _dragX0
, bounds
.height
));
1122 } else if (_dragX0
> _dragX
) {
1123 gc
.fillRectangle(new Rectangle(_dragX
, bounds
.y
, _dragX0
- _dragX
, bounds
.height
));
1127 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1129 // draw selected time
1130 long time0
= _timeProvider
.getTime0();
1131 long time1
= _timeProvider
.getTime1();
1132 long selectedTime
= _timeProvider
.getSelectedTime();
1133 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1134 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1135 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1136 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1137 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1140 // draw drag line, no line if name space is 0.
1141 if (DRAG_SPLIT_LINE
== _dragState
) {
1142 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1143 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1144 } else if (DRAG_ZOOM
== _dragState
&& Math
.max(_dragX
, _dragX0
) > nameSpace
) {
1145 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1146 gc
.drawLine(_dragX0
, bounds
.y
, _dragX0
, bounds
.y
+ bounds
.height
- 1);
1147 gc
.drawLine(_dragX
, bounds
.y
, _dragX
, bounds
.y
+ bounds
.height
- 1);
1148 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1149 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1150 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1155 * Draw many items at once
1158 * The rectangle of the area
1159 * @param timeProvider
1162 * The array items to draw
1164 * The index of the first element to draw
1166 * The width reserved for the names
1168 * Reference to the SWT GC object
1170 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1171 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1172 for (int i
= topIndex
; i
< items
.length
; i
++) {
1173 Item item
= items
[i
];
1174 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1176 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1182 * @param item the item to draw
1183 * @param bounds the container rectangle
1184 * @param timeProvider Time provider
1185 * @param i the item index
1186 * @param nameSpace the name space
1187 * @param gc Graphics context
1189 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1190 ITimeGraphEntry entry
= item
._trace
;
1191 long time0
= timeProvider
.getTime0();
1192 long time1
= timeProvider
.getTime1();
1193 long selectedTime
= timeProvider
.getSelectedTime();
1195 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1196 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1200 if (! item
._trace
.hasTimeEvents()) {
1201 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1202 nameRect
.width
+= statesRect
.width
;
1203 drawName(item
, nameRect
, gc
);
1205 drawName(item
, nameRect
, gc
);
1207 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1208 if (rect
.isEmpty()) {
1209 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1212 if (time1
<= time0
) {
1213 gc
.setBackground(_colors
.getBkColor(false, false, false));
1214 gc
.fillRectangle(rect
);
1215 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1219 // Initialize _rect1 to same values as enclosing rectangle rect
1220 Rectangle stateRect
= Utils
.clone(rect
);
1221 boolean selected
= item
._selected
;
1222 // K pixels per second
1223 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1225 if (item
._trace
.hasTimeEvents()) {
1226 fillSpace(rect
, gc
, selected
);
1227 // Drawing rectangle is smaller than reserved space
1229 stateRect
.height
-= 6;
1231 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1232 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1235 while (iterator
.hasNext()) {
1236 ITimeEvent event
= iterator
.next();
1237 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1238 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1239 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1240 // event is out of bounds
1243 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1244 stateRect
.x
= Math
.max(rect
.x
, x
);
1245 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1246 if (stateRect
.x
== lastX
) {
1247 stateRect
.width
-= 1;
1248 if (stateRect
.width
> 0) {
1249 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1250 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1256 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1257 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
1260 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1264 * Draw the name of an item.
1269 * Where to draw the name
1273 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1274 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1275 if (! hasTimeEvents
) {
1276 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1277 gc
.fillRectangle(bounds
);
1278 if (item
._selected
&& _isInFocus
) {
1279 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1280 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1283 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1284 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1285 gc
.fillRectangle(bounds
);
1288 // No name to be drawn
1289 if (_timeProvider
.getNameSpace() == 0) {
1293 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1294 if (item
._hasChildren
) {
1295 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1296 gc
.setBackground(_colors
.getBkColor(false, false, false));
1297 Rectangle rect
= Utils
.clone(bounds
);
1298 rect
.x
+= leftMargin
;
1299 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1300 rect
.width
= EXPAND_SIZE
;
1301 rect
.height
= EXPAND_SIZE
;
1302 gc
.fillRectangle(rect
);
1303 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1304 int midy
= rect
.y
+ rect
.height
/ 2;
1305 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1306 if (!item
._expanded
) {
1307 int midx
= rect
.x
+ rect
.width
/ 2;
1308 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1311 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1313 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1316 int imgHeight
= img
.getImageData().height
;
1317 int imgWidth
= img
.getImageData().width
;
1319 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1320 gc
.drawImage(img
, x
, y
);
1321 leftMargin
+= imgWidth
+ MARGIN
;
1323 String name
= item
._name
;
1324 Point size
= gc
.stringExtent(name
);
1325 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1326 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1328 if (hasTimeEvents
) {
1329 // cut long string with "..."
1330 int width
= bounds
.width
- leftMargin
;
1332 while (size
.x
> width
&& name
.length() > 1) {
1334 name
= name
.substring(0, name
.length() - 1);
1335 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1338 name
+= "..."; //$NON-NLS-1$
1341 Rectangle rect
= Utils
.clone(bounds
);
1342 rect
.x
+= leftMargin
;
1343 rect
.width
-= leftMargin
;
1345 if (rect
.width
> 0) {
1346 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1347 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1348 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1349 leftMargin
+= textWidth
+ MARGIN
;
1352 if (hasTimeEvents
) {
1354 int x
= bounds
.x
+ leftMargin
;
1355 int width
= bounds
.width
- x
;
1356 int midy
= bounds
.y
+ bounds
.height
/ 2;
1357 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1358 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1364 * Draw the state (color fill)
1369 * Time event for which we're drawing the state
1375 * Is this time event currently selected (so it appears
1377 * @param timeSelected
1378 * Is the timestamp currently selected
1380 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1381 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1383 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1387 boolean visible
= rect
.width
== 0 ?
false : true;
1390 Color stateColor
= null;
1391 if (colorIdx
< fEventColorMap
.length
) {
1392 stateColor
= fEventColorMap
[colorIdx
];
1394 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1397 timeSelected
= timeSelected
&& selected
;
1399 // modify the color?
1401 // fill all rect area
1402 gc
.setBackground(stateColor
);
1403 gc
.fillRectangle(rect
);
1404 // get the border color?
1405 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1408 if (!timeSelected
) {
1409 // Draw the top and bottom borders i.e. no side borders
1411 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1413 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1416 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1417 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1419 // selected rectangle area is not visible but can be represented
1420 // with a broken vertical line of specified width.
1423 gc.setForeground(stateColor);
1424 int s = gc.getLineStyle();
1425 int w = gc.getLineWidth();
1426 gc.setLineStyle(SWT.LINE_DOT);
1427 gc.setLineWidth(width);
1428 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1429 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1431 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1434 if (!timeSelected) {
1435 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1436 gc.drawPoint(rect.x, rect.y);
1437 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1441 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1445 * Fill the space between two contiguous time events
1452 * Is this time event selected or not
1454 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1455 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1456 gc
.fillRectangle(rect
);
1457 if (_dragState
== DRAG_ZOOM
) {
1458 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, true));
1459 if (_dragX0
< _dragX
) {
1460 gc
.fillRectangle(new Rectangle(_dragX0
, rect
.y
, _dragX
- _dragX0
, rect
.height
));
1461 } else if (_dragX0
> _dragX
) {
1462 gc
.fillRectangle(new Rectangle(_dragX
, rect
.y
, _dragX0
- _dragX
, rect
.height
));
1466 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1467 int midy
= rect
.y
+ rect
.height
/ 2;
1468 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1472 public void keyTraversed(TraverseEvent e
) {
1473 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1479 public void keyPressed(KeyEvent e
) {
1481 if (_data
._expandedItems
.length
== 0) {
1484 if (SWT
.HOME
== e
.keyCode
) {
1486 } else if (SWT
.END
== e
.keyCode
) {
1487 idx
= _data
._expandedItems
.length
- 1;
1488 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1489 idx
= getSelectedIndex();
1492 } else if (idx
< _data
._expandedItems
.length
- 1) {
1495 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1496 idx
= getSelectedIndex();
1499 } else if (idx
> 0) {
1502 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1504 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1506 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1507 int page
= countPerPage();
1508 idx
= getSelectedIndex();
1513 if (idx
>= _data
._expandedItems
.length
) {
1514 idx
= _data
._expandedItems
.length
- 1;
1516 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1517 int page
= countPerPage();
1518 idx
= getSelectedIndex();
1526 } else if (SWT
.CR
== e
.keyCode
) {
1527 idx
= getSelectedIndex();
1529 if (_data
._expandedItems
[idx
]._hasChildren
) {
1532 fireDefaultSelection();
1538 selectItem(idx
, false);
1539 fireSelectionChanged();
1544 public void keyReleased(KeyEvent e
) {
1548 public void focusGained(FocusEvent e
) {
1550 if (mouseScrollFilterListener
== null) {
1551 mouseScrollFilterListener
= new Listener() {
1552 // This filter is used to prevent horizontal scrolling of the view
1553 // when the mouse wheel is used to zoom
1555 public void handleEvent(Event event
) {
1559 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1565 public void focusLost(FocusEvent e
) {
1567 if (mouseScrollFilterListener
!= null) {
1568 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1569 mouseScrollFilterListener
= null;
1571 if (DRAG_NONE
!= _dragState
) {
1573 _dragState
= DRAG_NONE
;
1579 * @return If the current view is focused
1581 public boolean isInFocus() {
1586 * Provide the possibility to control the wait cursor externally e.g. data
1587 * requests in progress
1589 * @param waitInd Should we wait indefinitely?
1591 public void waitCursor(boolean waitInd
) {
1592 // Update cursor as indicated
1594 setCursor(_WaitCursor
);
1595 _isWaitCursor
= true;
1598 _isWaitCursor
= false;
1601 // Get ready for next mouse move
1602 _isDragCursor3
= false;
1607 * If the x, y position is over the vertical split line (name to time
1608 * ranges), then change the cursor to a drag cursor to indicate the user the
1609 * possibility of resizing
1615 void updateCursor(int x
, int y
) {
1616 // if Wait cursor not active, check for the need to change to a drag
1618 if (_isWaitCursor
== false) {
1619 boolean isSplitLine
= isOverSplitLine(x
);
1620 // No dragcursor is name space is fixed to zero
1621 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1622 setCursor(_dragCursor3
);
1623 _isDragCursor3
= true;
1624 } else if (!isSplitLine
&& _isDragCursor3
) {
1626 _isDragCursor3
= false;
1632 public void mouseMove(MouseEvent e
) {
1633 if (null == _timeProvider
) {
1636 Point size
= getCtrlSize();
1637 if (DRAG_TRACE_ITEM
== _dragState
) {
1638 int nameWidth
= _timeProvider
.getNameSpace();
1639 int x
= e
.x
- nameWidth
;
1640 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1642 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1643 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1644 long time1
= _time1bak
- timeDelta
;
1645 long maxTime
= _timeProvider
.getMaxTime();
1646 if (time1
> maxTime
) {
1649 long time0
= time1
- (_time1bak
- _time0bak
);
1650 if (time0
< _timeProvider
.getMinTime()) {
1651 time0
= _timeProvider
.getMinTime();
1652 time1
= time0
+ (_time1bak
- _time0bak
);
1654 _timeProvider
.setStartFinishTime(time0
, time1
);
1656 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1658 _timeProvider
.setNameSpace(e
.x
);
1659 } else if (DRAG_ZOOM
== _dragState
) {
1660 _dragX
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1662 } else if (DRAG_NONE
== _dragState
) {
1663 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1664 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1667 _mouseOverSplitLine
= mouseOverSplitLine
;
1669 updateCursor(e
.x
, e
.y
);
1673 public void mouseDoubleClick(MouseEvent e
) {
1674 if (null == _timeProvider
) {
1677 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1678 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1679 _timeProvider
.setNameSpace(_idealNameSpace
);
1680 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1681 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1684 _mouseOverSplitLine
= mouseOverSplitLine
;
1687 int idx
= getItemIndexAtY(e
.y
);
1689 selectItem(idx
, false);
1690 fireDefaultSelection();
1696 public void mouseDown(MouseEvent e
) {
1697 if (_dragState
!= DRAG_NONE
|| null == _timeProvider
) {
1701 if (1 == e
.button
) {
1702 int nameSpace
= _timeProvider
.getNameSpace();
1703 if (nameSpace
!= 0) {
1704 if (isOverSplitLine(e
.x
)) {
1705 _dragState
= DRAG_SPLIT_LINE
;
1706 _dragX
= _dragX0
= e
.x
;
1707 _time0bak
= _timeProvider
.getTime0();
1708 _time1bak
= _timeProvider
.getTime1();
1714 idx
= getItemIndexAtY(e
.y
);
1716 Item item
= _data
._expandedItems
[idx
];
1717 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1720 long hitTime
= getTimeAtX(e
.x
);
1722 // _timeProvider.setSelectedTimeInt(hitTime, false);
1724 _dragState
= DRAG_TRACE_ITEM
;
1725 _dragX
= _dragX0
= e
.x
- nameSpace
;
1726 _time0bak
= _timeProvider
.getTime0();
1727 _time1bak
= _timeProvider
.getTime1();
1730 selectItem(idx
, false);
1731 fireSelectionChanged();
1733 selectItem(idx
, false); // clear selection
1735 fireSelectionChanged();
1737 } else if (3 == e
.button
) {
1738 if (_timeProvider
.getTime0() == _timeProvider
.getTime1() || getCtrlSize().x
- _timeProvider
.getNameSpace() <= 0) {
1742 _dragX
= _dragX0
= Math
.min(Math
.max(e
.x
, _timeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1743 _dragState
= DRAG_ZOOM
;
1748 public void mouseUp(MouseEvent e
) {
1749 if (DRAG_NONE
!= _dragState
) {
1751 if (e
.button
== 1 && DRAG_TRACE_ITEM
== _dragState
) {
1752 if (_dragX
== _dragX0
) { // click without drag
1753 long time
= getTimeAtX(e
.x
);
1754 _timeProvider
.setSelectedTimeNotify(time
, false);
1756 // Notify time provider to check the need for listener
1758 _timeProvider
.notifyStartFinishTime();
1760 _dragState
= DRAG_NONE
;
1761 } else if (e
.button
== 1 && DRAG_SPLIT_LINE
== _dragState
) {
1763 _dragState
= DRAG_NONE
;
1764 } else if (e
.button
== 3 && DRAG_ZOOM
== _dragState
) {
1765 Point size
= getCtrlSize();
1766 int nameWidth
= _timeProvider
.getNameSpace();
1767 int x
= e
.x
- nameWidth
;
1768 if (Math
.max(_dragX
, _dragX0
) > nameWidth
&& _dragX
!= _dragX0
) {
1769 long time0
= getTimeAtX(_dragX0
);
1770 long time1
= getTimeAtX(_dragX
);
1771 if (time0
< time1
) {
1772 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1774 _timeProvider
.setStartFinishTimeNotify(time1
, time0
);
1779 _dragState
= DRAG_NONE
;
1785 public void mouseEnter(MouseEvent e
) {
1789 public void mouseExit(MouseEvent e
) {
1790 if (_mouseOverSplitLine
) {
1791 _mouseOverSplitLine
= false;
1797 public void mouseHover(MouseEvent e
) {
1801 public void mouseScrolled(MouseEvent e
) {
1802 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1805 boolean zoomScroll
= false;
1806 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1807 Point parentSize
= getParent().getSize();
1808 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1809 // over the parent control
1810 if (e
.x
> getCtrlSize().x
) {
1811 // over the horizontal scroll bar
1813 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1814 // over the name space
1820 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1823 } else if (e
.count
< 0) {
1827 setTopIndex(getTopIndex() - e
.count
);
1832 public void controlMoved(ControlEvent e
) {
1836 public void controlResized(ControlEvent e
) {
1841 public void widgetDefaultSelected(SelectionEvent e
) {
1845 public void widgetSelected(SelectionEvent e
) {
1846 if (e
.widget
== getVerticalBar()) {
1847 setTopIndex(getVerticalBar().getSelection());
1848 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1849 int start
= getHorizontalBar().getSelection();
1850 long time0
= _timeProvider
.getTime0();
1851 long time1
= _timeProvider
.getTime1();
1852 long timeMin
= _timeProvider
.getMinTime();
1853 long timeMax
= _timeProvider
.getMaxTime();
1854 long delta
= timeMax
- timeMin
;
1856 long range
= time1
- time0
;
1857 // _timeRangeFixed = true;
1858 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1859 time1
= time0
+ range
;
1861 // TODO: Follow-up with Bug 310310
1862 // In Linux SWT.DRAG is the only value received
1863 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1864 if (e
.detail
== SWT
.DRAG
) {
1865 _timeProvider
.setStartFinishTime(time0
, time1
);
1867 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1873 * @return The current visibility of the vertical scroll bar
1875 public boolean isVisibleVerticalScroll() {
1876 return _visibleVerticalScroll
;
1880 public int getBorderWidth() {
1881 return _borderWidth
;
1885 * Set the border width
1887 * @param borderWidth
1890 public void setBorderWidth(int borderWidth
) {
1891 this._borderWidth
= borderWidth
;
1895 * @return The current height of the header row
1897 public int getHeaderHeight() {
1898 return _headerHeight
;
1902 * Set the height of the header row
1904 * @param headerHeight
1907 public void setHeaderHeight(int headerHeight
) {
1908 this._headerHeight
= headerHeight
;
1912 * @return The height of regular item rows
1914 public int getItemHeight() {
1919 * Set the height of regular itew rows
1924 public void setItemHeight(int rowHeight
) {
1925 this._itemHeight
= rowHeight
;
1929 * Set the minimum item width
1931 * @param width The minimum width
1933 public void setMinimumItemWidth(int width
) {
1934 this._minimumItemWidth
= width
;
1938 * @return The minimum item width
1940 public int getMinimumItemWidth() {
1941 return _minimumItemWidth
;
1945 * @return The entries that are currently filtered out
1947 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1948 return _data
.getFilteredOut();
1953 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1954 if (listener
!= null) {
1955 if (!_selectionChangedListeners
.contains(listener
)) {
1956 _selectionChangedListeners
.add(listener
);
1963 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1964 if (listener
!= null) {
1965 _selectionChangedListeners
.remove(listener
);
1971 public void setSelection(ISelection selection
) {
1972 if (selection
instanceof TimeGraphSelection
) {
1973 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1974 Object ob
= sel
.getFirstElement();
1975 if (ob
instanceof ITimeGraphEntry
) {
1976 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1977 selectItem(trace
, false);
1983 private class ItemData
{
1984 public Item
[] _expandedItems
= new Item
[0];
1985 public Item
[] _items
= new Item
[0];
1986 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1987 private boolean traceFilter
[] = new boolean[0];
1988 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1989 public ITimeGraphPresentationProvider provider
;
1994 Item
findItem(ITimeGraphEntry entry
) {
1995 if (entry
== null) {
1999 for (int i
= 0; i
< _items
.length
; i
++) {
2000 Item item
= _items
[i
];
2001 if (item
._trace
== entry
) {
2009 int findItemIndex(ITimeGraphEntry trace
) {
2010 if (trace
== null) {
2014 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
2015 Item item
= _expandedItems
[i
];
2016 if (item
._trace
== trace
) {
2024 public void refreshData() {
2025 List
<Item
> itemList
= new ArrayList
<Item
>();
2026 filteredOut
.clear();
2027 for (int i
= 0; i
< _traces
.length
; i
++) {
2028 ITimeGraphEntry entry
= _traces
[i
];
2029 refreshData(itemList
, null, 0, entry
);
2031 _items
= itemList
.toArray(new Item
[0]);
2032 updateExpandedItems();
2035 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2036 Item item
= new Item(entry
, entry
.getName(), level
);
2037 if (parent
!= null) {
2038 parent
.children
.add(item
);
2040 item
.itemHeight
= provider
.getItemHeight(entry
);
2042 if (entry
.hasChildren()) {
2043 item
._expanded
= true;
2044 item
._hasChildren
= true;
2045 for (ITimeGraphEntry child
: entry
.getChildren()) {
2046 refreshData(itemList
, item
, level
+ 1, child
);
2051 public void updateExpandedItems() {
2052 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2053 for (int i
= 0; i
< _traces
.length
; i
++) {
2054 ITimeGraphEntry entry
= _traces
[i
];
2055 Item item
= findItem(entry
);
2056 refreshExpanded(expandedItemList
, item
);
2058 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
2061 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2062 expandedItemList
.add(item
);
2063 if (item
._hasChildren
&& item
._expanded
) {
2064 for (Item child
: item
.children
) {
2065 refreshExpanded(expandedItemList
, child
);
2070 public void refreshData(ITimeGraphEntry traces
[]) {
2071 if (traces
== null || traces
.length
== 0) {
2073 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
2074 traceFilter
= new boolean[traces
.length
];
2075 java
.util
.Arrays
.fill(traceFilter
, true);
2082 public ITimeGraphEntry
[] getTraces() {
2086 public boolean[] getTraceFilter() {
2090 public Vector
<ITimeGraphEntry
> getFilteredOut() {
2095 private class Item
{
2096 public boolean _expanded
;
2097 public boolean _selected
;
2098 public boolean _hasChildren
;
2099 public int itemHeight
;
2101 public List
<Item
> children
;
2102 public String _name
;
2103 public ITimeGraphEntry _trace
;
2105 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2106 this._trace
= trace
;
2109 this.children
= new ArrayList
<Item
>();
2113 public String
toString() {
2122 public void menuDetected(MenuDetectEvent e
) {
2123 if (null == _timeProvider
) {
2126 Point p
= toControl(e
.x
, e
.y
);
2127 int idx
= getItemIndexAtY(p
.y
);
2128 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
2129 Item item
= _data
._expandedItems
[idx
];
2130 ITimeGraphEntry entry
= item
._trace
;
2131 if (entry
.hasTimeEvents()) {
2132 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2133 if (event
!= null) {
2135 fireMenuEventOnTimeEvent(e
);
2140 fireMenuEventOnTimeGraphEntry(e
);