1 /*****************************************************************************
2 * Copyright (c) 2007, 2008 Intel Corporation, 2009, 2010, 2011, 2012 Ericsson.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Intel Corporation - Initial API and implementation
10 * Ruslan A. Scherbakov, Intel - Initial API and implementation
11 * Alvaro Sanchez-Leon - Updated for TMF
12 * Patrick Tasse - Refactoring
14 *****************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
21 import java
.util
.Vector
;
23 import org
.eclipse
.jface
.resource
.JFaceResources
;
24 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
27 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.ControlEvent
;
36 import org
.eclipse
.swt
.events
.ControlListener
;
37 import org
.eclipse
.swt
.events
.FocusEvent
;
38 import org
.eclipse
.swt
.events
.FocusListener
;
39 import org
.eclipse
.swt
.events
.KeyEvent
;
40 import org
.eclipse
.swt
.events
.KeyListener
;
41 import org
.eclipse
.swt
.events
.MouseEvent
;
42 import org
.eclipse
.swt
.events
.MouseListener
;
43 import org
.eclipse
.swt
.events
.MouseMoveListener
;
44 import org
.eclipse
.swt
.events
.MouseTrackListener
;
45 import org
.eclipse
.swt
.events
.MouseWheelListener
;
46 import org
.eclipse
.swt
.events
.PaintEvent
;
47 import org
.eclipse
.swt
.events
.SelectionEvent
;
48 import org
.eclipse
.swt
.events
.SelectionListener
;
49 import org
.eclipse
.swt
.events
.TraverseEvent
;
50 import org
.eclipse
.swt
.events
.TraverseListener
;
51 import org
.eclipse
.swt
.graphics
.Color
;
52 import org
.eclipse
.swt
.graphics
.Cursor
;
53 import org
.eclipse
.swt
.graphics
.GC
;
54 import org
.eclipse
.swt
.graphics
.Image
;
55 import org
.eclipse
.swt
.graphics
.Point
;
56 import org
.eclipse
.swt
.graphics
.Rectangle
;
57 import org
.eclipse
.swt
.widgets
.Composite
;
58 import org
.eclipse
.swt
.widgets
.Display
;
59 import org
.eclipse
.swt
.widgets
.Event
;
60 import org
.eclipse
.swt
.widgets
.Listener
;
61 import org
.eclipse
.swt
.widgets
.ScrollBar
;
64 * Time graph control implementation
67 * @author Alvaro Sanchez-Leon
68 * @author Patrick Tasse
70 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
{
72 private static final int DRAG_NONE
= 0;
73 private static final int DRAG_TRACE_ITEM
= 1;
74 private static final int DRAG_SPLIT_LINE
= 2;
75 public static final boolean DEFAULT_DRAW_THREAD_JOIN
= true;
76 public static final boolean DEFAULT_DRAW_THREAD_WAIT
= true;
77 public static final boolean DEFAULT_DRAW_THREAD_RELEASE
= true;
78 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
79 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
81 private static final double zoomCoeff
= 1.5;
83 private ITimeDataProvider _timeProvider
;
84 private boolean _isInFocus
= false;
85 private boolean _isDragCursor3
= false;
86 private boolean _isWaitCursor
= true;
87 private boolean _mouseOverSplitLine
= false;
88 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
89 private int _minimumItemWidth
= 0;
90 private int _topIndex
= 0;
91 private int _dragState
= DRAG_NONE
;
92 private int _dragX0
= 0;
93 private int _dragX
= 0;
94 private int _idealNameSpace
= 0;
95 // private double _timeStep = 10000000;
96 private long _time0bak
;
97 private long _time1bak
;
98 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
99 private ItemData _data
= null;
100 private List
<SelectionListener
> _selectionListeners
;
101 private final List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
102 private final List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
103 private final Cursor _dragCursor3
;
104 private final Cursor _WaitCursor
;
106 // Vertical formatting formatting for the state control view
107 private final boolean _visibleVerticalScroll
= true;
108 private int _borderWidth
= 0;
109 private int _headerHeight
= 0;
111 private Listener mouseScrollFilterListener
;
113 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
114 protected Color
[] fEventColorMap
= null;
116 private MouseScrollNotifier fMouseScrollNotifier
;
117 private final Object fMouseScrollNotifierLock
= new Object();
118 private class MouseScrollNotifier
extends Thread
{
119 private final static long DELAY
= 400L;
120 private final static long POLLING_INTERVAL
= 10L;
121 private long fLastScrollTime
= Long
.MAX_VALUE
;
125 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
127 Thread
.sleep(POLLING_INTERVAL
);
128 } catch (Exception e
) {
132 if (!isInterrupted()) {
133 Display
.getDefault().asyncExec(new Runnable() {
139 _timeProvider
.notifyStartFinishTime();
143 synchronized (fMouseScrollNotifierLock
) {
144 fMouseScrollNotifier
= null;
148 public void mouseScrolled() {
149 fLastScrollTime
= System
.currentTimeMillis();
154 * Standard constructor
157 * The parent composite object
159 * The color scheme to use
161 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
163 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
165 _data
= new ItemData();
167 addFocusListener(this);
168 addMouseListener(this);
169 addMouseMoveListener(this);
170 addMouseTrackListener(this);
171 addMouseWheelListener(this);
172 addTraverseListener(this);
173 addKeyListener(this);
174 addControlListener(this);
175 ScrollBar scrollHor
= getHorizontalBar();
177 if (scrollHor
!= null) {
178 scrollHor
.addSelectionListener(this);
181 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
182 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
186 public void dispose() {
188 _dragCursor3
.dispose();
189 _WaitCursor
.dispose();
190 fResourceManager
.dispose();
194 * Sets the timegraph provider used by this timegraph viewer.
196 * @param timeGraphProvider the timegraph provider
198 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
199 fTimeGraphProvider
= timeGraphProvider
;
200 _data
.provider
= timeGraphProvider
;
202 if (fEventColorMap
!= null) {
203 for (Color color
: fEventColorMap
) {
204 fResourceManager
.destroyColor(color
.getRGB());
207 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
208 if (stateItems
!= null) {
209 fEventColorMap
= new Color
[stateItems
.length
];
210 for (int i
= 0; i
< stateItems
.length
; i
++) {
211 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
214 fEventColorMap
= new Color
[] { };
219 * Assign the given time provider
221 * @param timeProvider
224 public void setTimeProvider(ITimeDataProvider timeProvider
) {
225 _timeProvider
= timeProvider
;
231 * Add a selection listener
234 * The listener to add
236 public void addSelectionListener(SelectionListener listener
) {
237 if (listener
== null) {
238 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
240 if (null == _selectionListeners
) {
241 _selectionListeners
= new ArrayList
<SelectionListener
>();
243 _selectionListeners
.add(listener
);
247 * Remove a selection listener
250 * The listener to remove
252 public void removeSelectionListener(SelectionListener listener
) {
253 if (null != _selectionListeners
) {
254 _selectionListeners
.remove(listener
);
259 * Selection changed callback
261 public void fireSelectionChanged() {
262 if (null != _selectionListeners
) {
263 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
264 while (it
.hasNext()) {
265 SelectionListener listener
= it
.next();
266 listener
.widgetSelected(null);
272 * Default selection callback
274 public void fireDefaultSelection() {
275 if (null != _selectionListeners
) {
276 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
277 while (it
.hasNext()) {
278 SelectionListener listener
= it
.next();
279 listener
.widgetDefaultSelected(null);
285 * Get the traces in the model
287 * @return The array of traces
289 public ITimeGraphEntry
[] getTraces() {
290 return _data
.getTraces();
294 * Get the on/off trace filters
296 * @return The array of filters
298 public boolean[] getTraceFilter() {
299 return _data
.getTraceFilter();
303 * Refresh the data for the thing
305 public void refreshData() {
312 * Refresh data for the given traces
315 * The traces to refresh
317 public void refreshData(ITimeGraphEntry
[] traces
) {
318 _data
.refreshData(traces
);
324 * Adjust the scoll bars
326 public void adjustScrolls() {
327 if (null == _timeProvider
) {
328 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
334 long time0
= _timeProvider
.getTime0();
335 long time1
= _timeProvider
.getTime1();
337 long timeMin
= _timeProvider
.getMinTime();
338 long timeMax
= _timeProvider
.getMaxTime();
340 long delta
= timeMax
- timeMin
;
343 int thumb
= H_SCROLLBAR_MAX
;
346 // Thumb size (page size)
347 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
348 // At the beginning of visible window
349 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
352 // position, minimum, maximum, thumb size, increment (half page)t, page
353 // increment size (full page)
354 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
357 boolean ensureVisibleItem(int idx
, boolean redraw
) {
358 boolean changed
= false;
360 for (idx
= 0; idx
< _data
._expandedItems
.length
; idx
++) {
361 if (_data
._expandedItems
[idx
]._selected
) {
366 if (idx
>= _data
._expandedItems
.length
) {
369 if (idx
< _topIndex
) {
371 //FIXME:getVerticalBar().setSelection(_topItem);
377 int page
= countPerPage();
378 if (idx
>= _topIndex
+ page
) {
379 setTopIndex(idx
- page
+ 1);
380 //FIXME:getVerticalBar().setSelection(_topItem);
391 * Assign the given index as the top one
396 public void setTopIndex(int idx
) {
397 idx
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
398 idx
= Math
.max(0, idx
);
404 * Set the expanded state of a given entry
409 * True if expanded, false if collapsed
411 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
412 Item item
= _data
.findItem(entry
);
413 if (item
!= null && item
._expanded
!= expanded
) {
414 item
._expanded
= expanded
;
415 _data
.updateExpandedItems();
421 * Add a tree listener
424 * The listener to add
426 public void addTreeListener(ITimeGraphTreeListener listener
) {
427 if (!_treeListeners
.contains(listener
)) {
428 _treeListeners
.add(listener
);
433 * Remove a tree listener
436 * The listener to remove
438 public void removeTreeListener(ITimeGraphTreeListener listener
) {
439 if (_treeListeners
.contains(listener
)) {
440 _treeListeners
.remove(listener
);
445 * Tree event callback
450 * The expanded state (true for expanded, false for collapsed)
452 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
453 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
454 for (ITimeGraphTreeListener listener
: _treeListeners
) {
456 listener
.treeExpanded(event
);
458 listener
.treeCollapsed(event
);
464 public ISelection
getSelection() {
465 TimeGraphSelection sel
= new TimeGraphSelection();
466 ITimeGraphEntry trace
= getSelectedTrace();
467 if (null != trace
&& null != _timeProvider
) {
468 long selectedTime
= _timeProvider
.getSelectedTime();
469 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
480 * Get the selection object
482 * @return The selection
484 public ISelection
getSelectionTrace() {
485 TimeGraphSelection sel
= new TimeGraphSelection();
486 ITimeGraphEntry trace
= getSelectedTrace();
494 // TODO select implementation for selectTrace
495 // public void selectTrace(int n) {
496 // if (n != 1 && n != -1)
498 // boolean changed = false;
499 // int lastSelection = -1;
500 // for (int i = 0; i < _data._expandedItems.length; i++) {
501 // TimeGraphItem item = (TimeGraphItem) _data._expandedItems[i];
502 // if (item._selected) {
503 // lastSelection = i;
504 // if (1 == n && i < _data._expandedItems.length - 1) {
505 // item._selected = false;
506 // if (item._hasChildren) {
507 // _data.expandItem(i);
508 // fireTreeEvent(item._trace, item._expanded);
510 // item = (TimeGraphItem) _data._expandedItems[i + 1];
511 // if (item._hasChildren) {
512 // _data.expandItem(i + 1);
513 // fireTreeEvent(item._trace, item._expanded);
514 // item = (TimeGraphItem) _data._expandedItems[i + 2];
516 // item._selected = true;
518 // } else if (-1 == n && i > 0) {
520 // TimeGraphItem prevItem = (TimeGraphItem) _data._expandedItems[i];
521 // if (prevItem._hasChildren) {
522 // if (prevItem._expanded) {
525 // prevItem = (TimeGraphItem) _data._expandedItems[i];
528 // if (!prevItem._expanded) {
529 // _data.expandItem(i);
530 // fireTreeEvent(prevItem._trace, prevItem._expanded);
531 // prevItem = (TimeGraphItem) _data._expandedItems[i + prevItem.children.size()];
532 // item._selected = false;
533 // prevItem._selected = true;
537 // item._selected = false;
538 // prevItem._selected = true;
545 // if (lastSelection < 0 && _data._expandedItems.length > 0) {
546 // TimeGraphItem item = (TimeGraphItem) _data._expandedItems[0];
547 // if (item._hasChildren) {
548 // _data.expandItem(0);
549 // fireTreeEvent(item._trace, item._expanded);
550 // item = (TimeGraphItem) _data._expandedItems[1];
551 // item._selected = true;
554 // item._selected = true;
559 // ensureVisibleItem(-1, false);
561 // fireSelectionChanged();
566 * Enable/disable one of the traces in the model
569 * 1 to enable it, -1 to disable. The method returns immediately
570 * if another value is used.
572 public void selectTrace(int n
) {
573 if ((n
!= 1) && (n
!= -1)) {
577 boolean changed
= false;
578 int lastSelection
= -1;
579 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
580 Item item
= _data
._expandedItems
[i
];
581 if (item
._selected
) {
583 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
584 item
._selected
= false;
585 item
= _data
._expandedItems
[i
+ 1];
586 item
._selected
= true;
588 } else if ((-1 == n
) && (i
> 0)) {
589 item
._selected
= false;
590 item
= _data
._expandedItems
[i
- 1];
591 item
._selected
= true;
598 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
599 Item item
= _data
._expandedItems
[0];
600 item
._selected
= true;
605 ensureVisibleItem(-1, false);
607 fireSelectionChanged();
615 * 1 to enable, -1 to disable
617 public void selectEvent(int n
) {
618 if (null == _timeProvider
) {
621 ITimeGraphEntry trace
= getSelectedTrace();
625 long selectedTime
= _timeProvider
.getSelectedTime();
626 long endTime
= _timeProvider
.getEndTime();
627 ITimeEvent nextEvent
;
628 if (-1 == n
&& selectedTime
> endTime
) {
629 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
631 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
633 if (null == nextEvent
&& -1 == n
) {
634 nextEvent
= Utils
.getFirstEvent(trace
);
636 if (null != nextEvent
) {
637 long nextTime
= nextEvent
.getTime();
638 // If last event detected e.g. going back or not moving to a next
640 if (nextTime
<= selectedTime
&& n
== 1) {
641 // Select to the end of this last event
642 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
643 // but not beyond the end of the trace
644 if (nextTime
> endTime
) {
648 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
649 fireSelectionChanged();
651 _timeProvider
.setSelectedTimeNotify(endTime
, true);
652 fireSelectionChanged();
657 * Select the next event
659 public void selectNextEvent() {
661 // Notify if visible time window has been adjusted
662 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
666 * Select the previous event
668 public void selectPrevEvent() {
670 // Notify if visible time window has been adjusted
671 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
675 * Select the next trace
677 public void selectNextTrace() {
682 * Select the previous trace
684 public void selectPrevTrace() {
689 * Zoom based on mouse cursor location with mouse scrolling
691 * @param zoomIn true to zoom in, false to zoom out
693 public void zoom(boolean zoomIn
) {
694 int globalX
= getDisplay().getCursorLocation().x
;
695 Point p
= toControl(globalX
, 0);
696 int nameSpace
= _timeProvider
.getNameSpace();
697 int timeSpace
= _timeProvider
.getTimeSpace();
698 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
699 long time0
= _timeProvider
.getTime0();
700 long time1
= _timeProvider
.getTime1();
701 long interval
= time1
- time0
;
704 } // to allow getting out of single point interval
707 newInterval
= Math
.max(Math
.round(interval
* 0.8), _timeProvider
.getMinTimeInterval());
709 newInterval
= (long) Math
.ceil(interval
* 1.25);
711 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
712 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
713 long newTime1
= newTime0
+ newInterval
;
714 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
715 synchronized (fMouseScrollNotifierLock
) {
716 if (fMouseScrollNotifier
== null) {
717 fMouseScrollNotifier
= new MouseScrollNotifier();
718 fMouseScrollNotifier
.start();
720 fMouseScrollNotifier
.mouseScrolled();
725 * zoom in using single click
727 public void zoomIn() {
728 long _time0
= _timeProvider
.getTime0();
729 long _time1
= _timeProvider
.getTime1();
730 long _range
= _time1
- _time0
;
731 long selTime
= _timeProvider
.getSelectedTime();
732 if (selTime
<= _time0
|| selTime
>= _time1
) {
733 selTime
= (_time0
+ _time1
) / 2;
735 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
736 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
738 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
740 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
741 // + time1 + " inaccuracy:" + inaccuracy);
743 if (inaccuracy
> 0 && inaccuracy
< 100) {
744 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
748 long m
= _timeProvider
.getMinTimeInterval();
749 if ((time1
- time0
) < m
) {
750 time0
= selTime
- (selTime
- _time0
) * m
/ _range
;
754 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
758 * zoom out using single click
760 public void zoomOut() {
761 long _time0
= _timeProvider
.getTime0();
762 long _time1
= _timeProvider
.getTime1();
763 long selTime
= _timeProvider
.getSelectedTime();
764 if (selTime
<= _time0
|| selTime
>= _time1
) {
765 selTime
= (_time0
+ _time1
) / 2;
767 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
768 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
770 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
771 if (inaccuracy
> 0 && inaccuracy
< 100) {
772 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
776 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
780 * Return the currently selected trace
782 * @return The entry matching the trace
784 public ITimeGraphEntry
getSelectedTrace() {
785 ITimeGraphEntry trace
= null;
786 int idx
= getSelectedIndex();
788 trace
= _data
._expandedItems
[idx
]._trace
;
794 * Retrieve the index of the currently selected item
798 public int getSelectedIndex() {
800 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
801 Item item
= _data
._expandedItems
[i
];
802 if (item
._selected
) {
810 boolean toggle(int idx
) {
811 boolean toggled
= false;
812 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
813 Item item
= _data
._expandedItems
[idx
];
814 if (item
._hasChildren
) {
815 item
._expanded
= !item
._expanded
;
816 _data
.updateExpandedItems();
820 fireTreeEvent(item
._trace
, item
._expanded
);
826 int getItemIndexAtY(int y
) {
830 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
832 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
833 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
840 int idx
= y
/ _itemHeight
;
842 if (idx
< _data
._expandedItems
.length
) {
848 boolean isOverSplitLine(int x
) {
849 if (x
< 0 || null == _timeProvider
) {
853 int nameWidth
= _timeProvider
.getNameSpace();
854 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
860 ITimeGraphEntry
getEntry(Point pt
) {
861 int idx
= getItemIndexAtY(pt
.y
);
862 return idx
>= 0 ? _data
._expandedItems
[idx
]._trace
: null;
865 long getTimeAtX(int x
) {
866 if (null == _timeProvider
) {
870 Point size
= getCtrlSize();
871 long time0
= _timeProvider
.getTime0();
872 long time1
= _timeProvider
.getTime1();
873 int nameWidth
= _timeProvider
.getNameSpace();
875 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
876 if (x
>= 0 && size
.x
>= nameWidth
) {
877 if (time1
- time0
> timeWidth
) {
878 // get the last possible time represented by the pixel position
879 // by taking the time of the next pixel position minus 1 nanosecond
880 hitTime
= time0
+ (long) ((time1
- time0
) * ((double) (x
+ 1) / timeWidth
)) - 1;
882 hitTime
= time0
+ Math
.round((time1
- time0
) * ((double) x
/ timeWidth
));
888 void selectItem(int idx
, boolean addSelection
) {
889 boolean changed
= false;
891 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
892 Item item
= _data
._expandedItems
[idx
];
893 changed
= (item
._selected
== false);
894 item
._selected
= true;
897 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
898 Item item
= _data
._expandedItems
[i
];
899 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
902 item
._selected
= i
== idx
;
905 changed
|= ensureVisibleItem(idx
, true);
912 * Callback for item selection
915 * The entry matching the trace
916 * @param addSelection
917 * If the selection is added or removed
919 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
920 int idx
= _data
.findItemIndex(trace
);
921 selectItem(idx
, addSelection
);
925 * Retrieve the number of entries shown per page.
929 public int countPerPage() {
930 int height
= getCtrlSize().y
;
932 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
934 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
935 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
936 if (ySum
>= height
) {
941 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
942 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
943 if (ySum
>= height
) {
951 count
= height
/ _itemHeight
;
957 * Get the index of the top element
961 public int getTopIndex() {
966 * Get the number of expanded items
968 * @return The count of expanded items
970 public int getExpandedElementCount() {
971 return _data
._expandedItems
.length
;
975 * Get an array of all expanded elements
977 * @return The expanded elements
979 public ITimeGraphEntry
[] getExpandedElements() {
980 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
981 for (Item item
: _data
._expandedItems
) {
982 elements
.add(item
._trace
);
984 return elements
.toArray(new ITimeGraphEntry
[0]);
987 Point
getCtrlSize() {
988 Point size
= getSize();
989 if (getHorizontalBar().isVisible()) {
990 size
.y
-= getHorizontalBar().getSize().y
;
995 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
997 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
998 int width
= nameWidth
;
999 int height
= _itemHeight
;
1000 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1002 for (int i
= _topIndex
; i
< idx
; i
++) {
1003 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1006 height
= _data
._expandedItems
[idx
].itemHeight
;
1008 return new Rectangle(x
, y
, width
, height
);
1011 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1012 int x
= bound
.x
+ nameWidth
;
1013 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
1014 int width
= bound
.width
- x
;
1015 int height
= _itemHeight
;
1016 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
1018 for (int i
= _topIndex
; i
< idx
; i
++) {
1019 ySum
+= _data
._expandedItems
[i
].itemHeight
;
1022 height
= _data
._expandedItems
[idx
].itemHeight
;
1024 return new Rectangle(x
, y
, width
, height
);
1028 void paint(Rectangle bounds
, PaintEvent e
) {
1030 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
1031 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1033 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
) {
1037 _idealNameSpace
= 0;
1038 int nameSpace
= _timeProvider
.getNameSpace();
1040 // draw empty name space background
1041 gc
.setBackground(_colors
.getBkColor(false, false, true));
1042 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1044 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
1046 // draw selected time
1047 long time0
= _timeProvider
.getTime0();
1048 long time1
= _timeProvider
.getTime1();
1049 long selectedTime
= _timeProvider
.getSelectedTime();
1050 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1051 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1052 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1053 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1054 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1057 // draw drag line, no line if name space is 0.
1058 if (DRAG_SPLIT_LINE
== _dragState
) {
1059 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
1060 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1061 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
1062 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
1063 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1068 * Draw many items at once
1071 * The rectangle of the area
1072 * @param timeProvider
1075 * The array items to draw
1077 * The index of the first element to draw
1079 * The width reserved for the names
1081 * Reference to the SWT GC object
1083 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1084 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1085 for (int i
= topIndex
; i
< items
.length
; i
++) {
1086 Item item
= items
[i
];
1087 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1089 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1095 * @param item the item to draw
1096 * @param bounds the container rectangle
1097 * @param i the item index
1098 * @param nameSpace the name space
1101 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1102 ITimeGraphEntry entry
= item
._trace
;
1103 long time0
= timeProvider
.getTime0();
1104 long time1
= timeProvider
.getTime1();
1105 long selectedTime
= timeProvider
.getSelectedTime();
1107 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1108 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1112 if (! item
._trace
.hasTimeEvents()) {
1113 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1114 nameRect
.width
+= statesRect
.width
;
1115 drawName(item
, nameRect
, gc
);
1117 drawName(item
, nameRect
, gc
);
1119 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1120 if (rect
.isEmpty()) {
1121 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1124 if (time1
<= time0
) {
1125 gc
.setBackground(_colors
.getBkColor(false, false, false));
1126 gc
.fillRectangle(rect
);
1127 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1131 // Initialize _rect1 to same values as enclosing rectangle rect
1132 Rectangle stateRect
= Utils
.clone(rect
);
1133 boolean selected
= item
._selected
;
1134 // K pixels per second
1135 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1137 if (item
._trace
.hasTimeEvents()) {
1138 fillSpace(rect
, gc
, selected
);
1139 // Drawing rectangle is smaller than reserved space
1141 stateRect
.height
-= 6;
1143 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1144 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1147 while (iterator
.hasNext()) {
1148 ITimeEvent event
= iterator
.next();
1149 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1150 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1151 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1152 // event is out of bounds
1155 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1156 stateRect
.x
= Math
.max(rect
.x
, x
);
1157 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1158 if (stateRect
.x
== lastX
) {
1159 stateRect
.width
-= 1;
1160 if (stateRect
.width
> 0) {
1161 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1162 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1168 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1169 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
1172 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1175 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1176 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
1177 if (! hasTimeEvents
) {
1178 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
1179 gc
.fillRectangle(bounds
);
1180 if (item
._selected
&& _isInFocus
) {
1181 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
1182 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1185 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
1186 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1187 gc
.fillRectangle(bounds
);
1190 // No name to be drawn
1191 if (_timeProvider
.getNameSpace() == 0) {
1195 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1196 if (item
._hasChildren
) {
1197 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1198 gc
.setBackground(_colors
.getBkColor(false, false, false));
1199 Rectangle rect
= Utils
.clone(bounds
);
1200 rect
.x
+= leftMargin
;
1201 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1202 rect
.width
= EXPAND_SIZE
;
1203 rect
.height
= EXPAND_SIZE
;
1204 gc
.fillRectangle(rect
);
1205 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1206 int midy
= rect
.y
+ rect
.height
/ 2;
1207 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1208 if (!item
._expanded
) {
1209 int midx
= rect
.x
+ rect
.width
/ 2;
1210 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1213 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1215 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1218 int imgHeight
= img
.getImageData().height
;
1219 int imgWidth
= img
.getImageData().width
;
1221 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1222 gc
.drawImage(img
, x
, y
);
1223 leftMargin
+= imgWidth
+ MARGIN
;
1225 String name
= item
._name
;
1226 Point size
= gc
.stringExtent(name
);
1227 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1228 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1230 if (hasTimeEvents
) {
1231 // cut long string with "..."
1232 int width
= bounds
.width
- leftMargin
;
1234 while (size
.x
> width
&& name
.length() > 1) {
1236 name
= name
.substring(0, name
.length() - 1);
1237 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1240 name
+= "..."; //$NON-NLS-1$
1243 Rectangle rect
= Utils
.clone(bounds
);
1244 rect
.x
+= leftMargin
;
1245 rect
.width
-= leftMargin
;
1247 if (rect
.width
> 0) {
1248 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1249 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1250 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1251 leftMargin
+= textWidth
+ MARGIN
;
1254 if (hasTimeEvents
) {
1256 int x
= bounds
.x
+ leftMargin
;
1257 int width
= bounds
.width
- x
;
1258 int midy
= bounds
.y
+ bounds
.height
/ 2;
1259 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1260 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1265 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1266 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1268 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1272 boolean visible
= rect
.width
== 0 ?
false : true;
1275 Color stateColor
= null;
1276 if (colorIdx
< fEventColorMap
.length
) {
1277 stateColor
= fEventColorMap
[colorIdx
];
1279 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1282 timeSelected
= timeSelected
&& selected
;
1284 // modify the color?
1286 // fill all rect area
1287 gc
.setBackground(stateColor
);
1288 gc
.fillRectangle(rect
);
1289 // get the border color?
1290 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1293 if (!timeSelected
) {
1294 // Draw the top and bottom borders i.e. no side borders
1296 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1298 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1301 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1302 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1304 // selected rectangle area is not visible but can be represented
1305 // with a broken vertical line of specified width.
1308 gc.setForeground(stateColor);
1309 int s = gc.getLineStyle();
1310 int w = gc.getLineWidth();
1311 gc.setLineStyle(SWT.LINE_DOT);
1312 gc.setLineWidth(width);
1313 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1314 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1316 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1319 if (!timeSelected) {
1320 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1321 gc.drawPoint(rect.x, rect.y);
1322 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1326 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1329 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1330 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1331 gc
.fillRectangle(rect
);
1333 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1334 int midy
= rect
.y
+ rect
.height
/ 2;
1335 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1339 public void keyTraversed(TraverseEvent e
) {
1340 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1346 public void keyPressed(KeyEvent e
) {
1348 if (_data
._expandedItems
.length
== 0) {
1351 if (SWT
.HOME
== e
.keyCode
) {
1353 } else if (SWT
.END
== e
.keyCode
) {
1354 idx
= _data
._expandedItems
.length
- 1;
1355 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1356 idx
= getSelectedIndex();
1359 } else if (idx
< _data
._expandedItems
.length
- 1) {
1362 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1363 idx
= getSelectedIndex();
1366 } else if (idx
> 0) {
1369 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1371 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1373 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1374 int page
= countPerPage();
1375 idx
= getSelectedIndex();
1380 if (idx
>= _data
._expandedItems
.length
) {
1381 idx
= _data
._expandedItems
.length
- 1;
1383 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1384 int page
= countPerPage();
1385 idx
= getSelectedIndex();
1393 } else if (SWT
.CR
== e
.keyCode
) {
1394 idx
= getSelectedIndex();
1396 if (_data
._expandedItems
[idx
]._hasChildren
) {
1399 fireDefaultSelection();
1405 selectItem(idx
, false);
1406 fireSelectionChanged();
1411 public void keyReleased(KeyEvent e
) {
1415 public void focusGained(FocusEvent e
) {
1417 if (mouseScrollFilterListener
== null) {
1418 mouseScrollFilterListener
= new Listener() {
1419 // This filter is used to prevent horizontal scrolling of the view
1420 // when the mouse wheel is used to zoom
1422 public void handleEvent(Event event
) {
1426 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1432 public void focusLost(FocusEvent e
) {
1434 if (mouseScrollFilterListener
!= null) {
1435 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1436 mouseScrollFilterListener
= null;
1438 if (DRAG_NONE
!= _dragState
) {
1440 _dragState
= DRAG_NONE
;
1446 * @return If the current view is focused
1448 public boolean isInFocus() {
1453 * Provide the possibility to control the wait cursor externally e.g. data
1454 * requests in progress
1456 * @param waitInd Should we wait indefinitely?
1458 public void waitCursor(boolean waitInd
) {
1459 // Update cursor as indicated
1461 setCursor(_WaitCursor
);
1462 _isWaitCursor
= true;
1465 _isWaitCursor
= false;
1468 // Get ready for next mouse move
1469 _isDragCursor3
= false;
1474 * If the x, y position is over the vertical split line (name to time
1475 * ranges), then change the cursor to a drag cursor to indicate the user the
1476 * possibility of resizing
1482 void updateCursor(int x
, int y
) {
1483 // if Wait cursor not active, check for the need to change to a drag
1485 if (_isWaitCursor
== false) {
1486 boolean isSplitLine
= isOverSplitLine(x
);
1487 // No dragcursor is name space is fixed to zero
1488 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1489 setCursor(_dragCursor3
);
1490 _isDragCursor3
= true;
1491 } else if (!isSplitLine
&& _isDragCursor3
) {
1493 _isDragCursor3
= false;
1499 public void mouseMove(MouseEvent e
) {
1500 if (null == _timeProvider
) {
1503 Point size
= getCtrlSize();
1504 if (DRAG_TRACE_ITEM
== _dragState
) {
1505 int nameWidth
= _timeProvider
.getNameSpace();
1506 int x
= e
.x
- nameWidth
;
1507 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1509 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1510 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1511 long time1
= _time1bak
- timeDelta
;
1512 long maxTime
= _timeProvider
.getMaxTime();
1513 if (time1
> maxTime
) {
1516 long time0
= time1
- (_time1bak
- _time0bak
);
1517 if (time0
< _timeProvider
.getMinTime()) {
1518 time0
= _timeProvider
.getMinTime();
1519 time1
= time0
+ (_time1bak
- _time0bak
);
1521 _timeProvider
.setStartFinishTime(time0
, time1
);
1523 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1525 _timeProvider
.setNameSpace(e
.x
);
1526 } else if (DRAG_NONE
== _dragState
) {
1527 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1528 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1531 _mouseOverSplitLine
= mouseOverSplitLine
;
1533 updateCursor(e
.x
, e
.y
);
1537 public void mouseDoubleClick(MouseEvent e
) {
1538 if (null == _timeProvider
) {
1541 if (1 == e
.button
) {
1542 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1543 _timeProvider
.setNameSpace(_idealNameSpace
);
1544 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1545 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1548 _mouseOverSplitLine
= mouseOverSplitLine
;
1551 int idx
= getItemIndexAtY(e
.y
);
1553 selectItem(idx
, false);
1554 fireDefaultSelection();
1560 public void mouseDown(MouseEvent e
) {
1561 if (null == _timeProvider
) {
1565 if (1 == e
.button
) {
1566 int nameSpace
= _timeProvider
.getNameSpace();
1567 if (nameSpace
!= 0) {
1568 if (isOverSplitLine(e
.x
)) {
1569 _dragState
= DRAG_SPLIT_LINE
;
1570 _dragX
= _dragX0
= e
.x
;
1571 _time0bak
= _timeProvider
.getTime0();
1572 _time1bak
= _timeProvider
.getTime1();
1578 idx
= getItemIndexAtY(e
.y
);
1580 Item item
= _data
._expandedItems
[idx
];
1581 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1584 long hitTime
= getTimeAtX(e
.x
);
1586 // _timeProvider.setSelectedTimeInt(hitTime, false);
1588 _dragState
= DRAG_TRACE_ITEM
;
1589 _dragX
= _dragX0
= e
.x
- nameSpace
;
1590 _time0bak
= _timeProvider
.getTime0();
1591 _time1bak
= _timeProvider
.getTime1();
1594 selectItem(idx
, false);
1595 fireSelectionChanged();
1597 selectItem(idx
, false); // clear selection
1599 fireSelectionChanged();
1605 public void mouseUp(MouseEvent e
) {
1606 if (DRAG_NONE
!= _dragState
) {
1608 if (DRAG_TRACE_ITEM
== _dragState
) {
1609 // Notify time provider to check the need for listener
1611 _timeProvider
.notifyStartFinishTime();
1612 if (_dragX
== _dragX0
) { // click without drag
1613 long time
= getTimeAtX(e
.x
);
1614 _timeProvider
.setSelectedTimeNotify(time
, false);
1616 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1619 _dragState
= DRAG_NONE
;
1624 public void mouseEnter(MouseEvent e
) {
1628 public void mouseExit(MouseEvent e
) {
1629 if (_mouseOverSplitLine
) {
1630 _mouseOverSplitLine
= false;
1636 public void mouseHover(MouseEvent e
) {
1640 public void mouseScrolled(MouseEvent e
) {
1641 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1644 boolean zoomScroll
= false;
1645 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1646 Point parentSize
= getParent().getSize();
1647 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1648 // over the parent control
1649 if (e
.x
> getCtrlSize().x
) {
1650 // over the horizontal scroll bar
1652 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1653 // over the name space
1659 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1662 } else if (e
.count
< 0) {
1666 setTopIndex(getTopIndex() - e
.count
);
1671 public void controlMoved(ControlEvent e
) {
1675 public void controlResized(ControlEvent e
) {
1680 public void widgetDefaultSelected(SelectionEvent e
) {
1684 public void widgetSelected(SelectionEvent e
) {
1685 if (e
.widget
== getVerticalBar()) {
1686 setTopIndex(getVerticalBar().getSelection());
1687 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1688 int start
= getHorizontalBar().getSelection();
1689 long time0
= _timeProvider
.getTime0();
1690 long time1
= _timeProvider
.getTime1();
1691 long timeMin
= _timeProvider
.getMinTime();
1692 long timeMax
= _timeProvider
.getMaxTime();
1693 long delta
= timeMax
- timeMin
;
1695 long range
= time1
- time0
;
1696 // _timeRangeFixed = true;
1697 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1698 time1
= time0
+ range
;
1700 // TODO: Follow-up with Bug 310310
1701 // In Linux SWT.DRAG is the only value received
1702 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1703 if (e
.detail
== SWT
.DRAG
) {
1704 _timeProvider
.setStartFinishTime(time0
, time1
);
1706 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1712 * @return The current visibility of the vertical scroll bar
1714 public boolean isVisibleVerticalScroll() {
1715 return _visibleVerticalScroll
;
1719 public int getBorderWidth() {
1720 return _borderWidth
;
1724 * Set the border width
1726 * @param borderWidth
1729 public void setBorderWidth(int borderWidth
) {
1730 this._borderWidth
= borderWidth
;
1734 * @return The current height of the header row
1736 public int getHeaderHeight() {
1737 return _headerHeight
;
1741 * Set the height of the header row
1743 * @param headerHeight
1746 public void setHeaderHeight(int headerHeight
) {
1747 this._headerHeight
= headerHeight
;
1751 * @return The height of regular item rows
1753 public int getItemHeight() {
1758 * Set the height of regular itew rows
1763 public void setItemHeight(int rowHeight
) {
1764 this._itemHeight
= rowHeight
;
1768 * Set the minimum item width
1770 * @param width The minimum width
1772 public void setMinimumItemWidth(int width
) {
1773 this._minimumItemWidth
= width
;
1777 * @return The minimum item width
1779 public int getMinimumItemWidth() {
1780 return _minimumItemWidth
;
1784 * @return The entries that are currently filtered out
1786 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1787 return _data
.getFilteredOut();
1792 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1793 if (listener
!= null) {
1794 if (!_selectionChangedListeners
.contains(listener
)) {
1795 _selectionChangedListeners
.add(listener
);
1802 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1803 if (listener
!= null) {
1804 _selectionChangedListeners
.remove(listener
);
1810 public void setSelection(ISelection selection
) {
1811 if (selection
instanceof TimeGraphSelection
) {
1812 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1813 Object ob
= sel
.getFirstElement();
1814 if (ob
instanceof ITimeGraphEntry
) {
1815 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1816 selectItem(trace
, false);
1822 private class ItemData
{
1823 public Item
[] _expandedItems
= new Item
[0];
1824 public Item
[] _items
= new Item
[0];
1825 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1826 private boolean traceFilter
[] = new boolean[0];
1827 private final Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1828 public ITimeGraphPresentationProvider provider
;
1833 Item
findItem(ITimeGraphEntry entry
) {
1834 if (entry
== null) {
1838 for (int i
= 0; i
< _items
.length
; i
++) {
1839 Item item
= _items
[i
];
1840 if (item
._trace
== entry
) {
1848 int findItemIndex(ITimeGraphEntry trace
) {
1849 if (trace
== null) {
1853 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
1854 Item item
= _expandedItems
[i
];
1855 if (item
._trace
== trace
) {
1863 public void refreshData() {
1864 List
<Item
> itemList
= new ArrayList
<Item
>();
1865 filteredOut
.clear();
1866 for (int i
= 0; i
< _traces
.length
; i
++) {
1867 ITimeGraphEntry entry
= _traces
[i
];
1868 refreshData(itemList
, null, 0, entry
);
1870 _items
= itemList
.toArray(new Item
[0]);
1871 updateExpandedItems();
1874 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
1875 Item item
= new Item(entry
, entry
.getName(), level
);
1876 if (parent
!= null) {
1877 parent
.children
.add(item
);
1879 item
.itemHeight
= provider
.getItemHeight(entry
);
1881 if (entry
.hasChildren()) {
1882 item
._expanded
= true;
1883 item
._hasChildren
= true;
1884 for (ITimeGraphEntry child
: entry
.getChildren()) {
1885 refreshData(itemList
, item
, level
+ 1, child
);
1890 public void updateExpandedItems() {
1891 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
1892 for (int i
= 0; i
< _traces
.length
; i
++) {
1893 ITimeGraphEntry entry
= _traces
[i
];
1894 Item item
= findItem(entry
);
1895 refreshExpanded(expandedItemList
, item
);
1897 _expandedItems
= expandedItemList
.toArray(new Item
[0]);
1900 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
1901 expandedItemList
.add(item
);
1902 if (item
._hasChildren
&& item
._expanded
) {
1903 for (Item child
: item
.children
) {
1904 refreshExpanded(expandedItemList
, child
);
1909 public void expandItem(int idx
) {
1910 if (idx
< 0 || idx
>= _expandedItems
.length
) {
1913 Item item
= _expandedItems
[idx
];
1914 if (item
._hasChildren
&& !item
._expanded
) {
1915 item
._expanded
= true;
1916 updateExpandedItems();
1920 public void refreshData(ITimeGraphEntry traces
[]) {
1921 if (traces
== null || traces
.length
== 0) {
1923 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
1924 traceFilter
= new boolean[traces
.length
];
1925 java
.util
.Arrays
.fill(traceFilter
, true);
1932 public ITimeGraphEntry
[] getTraces() {
1936 public boolean[] getTraceFilter() {
1940 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1945 private class Item
{
1946 public boolean _expanded
;
1947 public boolean _selected
;
1948 public boolean _hasChildren
;
1949 public int itemHeight
;
1951 public List
<Item
> children
;
1952 public String _name
;
1953 public ITimeGraphEntry _trace
;
1955 public Item(ITimeGraphEntry trace
, String name
, int level
) {
1956 this._trace
= trace
;
1959 this.children
= new ArrayList
<Item
>();
1963 public String
toString() {