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
;
63 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
{
65 private static final int DRAG_NONE
= 0;
66 private static final int DRAG_TRACE_ITEM
= 1;
67 private static final int DRAG_SPLIT_LINE
= 2;
68 public static final boolean DEFAULT_DRAW_THREAD_JOIN
= true;
69 public static final boolean DEFAULT_DRAW_THREAD_WAIT
= true;
70 public static final boolean DEFAULT_DRAW_THREAD_RELEASE
= true;
71 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
72 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
74 private static final double zoomCoeff
= 1.5;
76 private ITimeDataProvider _timeProvider
;
77 private boolean _isInFocus
= false;
78 private boolean _isDragCursor3
= false;
79 private boolean _isWaitCursor
= true;
80 private boolean _mouseOverSplitLine
= false;
81 private int _itemHeight
= CUSTOM_ITEM_HEIGHT
;
82 private int _minimumItemWidth
= 0;
83 private int _topIndex
= 0;
84 private int _dragState
= DRAG_NONE
;
85 private int _dragX0
= 0;
86 private int _dragX
= 0;
87 private int _idealNameSpace
= 0;
88 // private double _timeStep = 10000000;
89 private long _time0bak
;
90 private long _time1bak
;
91 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
92 private ItemData _data
= null;
93 private List
<SelectionListener
> _selectionListeners
;
94 private List
<ISelectionChangedListener
> _selectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
95 private List
<ITimeGraphTreeListener
> _treeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
96 private Cursor _dragCursor3
;
97 private Cursor _WaitCursor
;
99 // Vertical formatting formatting for the state control view
100 private boolean _visibleVerticalScroll
= true;
101 private int _borderWidth
= 0;
102 private int _headerHeight
= 0;
104 private Listener mouseScrollFilterListener
;
106 protected LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
107 protected Color
[] fEventColorMap
= null;
109 private MouseScrollNotifier fMouseScrollNotifier
;
110 private Object fMouseScrollNotifierLock
= new Object();
111 private class MouseScrollNotifier
extends Thread
{
112 private final static long DELAY
= 400L;
113 private final static long POLLING_INTERVAL
= 10L;
114 private long fLastScrollTime
= Long
.MAX_VALUE
;
118 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
120 Thread
.sleep(POLLING_INTERVAL
);
121 } catch (Exception e
) {
125 if (!isInterrupted()) {
126 Display
.getDefault().asyncExec(new Runnable() {
132 _timeProvider
.notifyStartFinishTime();
136 synchronized (fMouseScrollNotifierLock
) {
137 fMouseScrollNotifier
= null;
141 public void mouseScrolled() {
142 fLastScrollTime
= System
.currentTimeMillis();
146 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
148 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
150 _data
= new ItemData();
152 addFocusListener(this);
153 addMouseListener(this);
154 addMouseMoveListener(this);
155 addMouseTrackListener(this);
156 addMouseWheelListener(this);
157 addTraverseListener(this);
158 addKeyListener(this);
159 addControlListener(this);
160 ScrollBar scrollHor
= getHorizontalBar();
162 if (scrollHor
!= null) {
163 scrollHor
.addSelectionListener(this);
166 _dragCursor3
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
167 _WaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
171 public void dispose() {
173 _dragCursor3
.dispose();
174 _WaitCursor
.dispose();
175 fResourceManager
.dispose();
179 * Sets the timegraph provider used by this timegraph viewer.
181 * @param timeGraphProvider the timegraph provider
183 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
184 fTimeGraphProvider
= timeGraphProvider
;
185 _data
.provider
= timeGraphProvider
;
187 if (fEventColorMap
!= null) {
188 for (Color color
: fEventColorMap
) {
189 fResourceManager
.destroyColor(color
.getRGB());
192 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
193 if (stateItems
!= null) {
194 fEventColorMap
= new Color
[stateItems
.length
];
195 for (int i
= 0; i
< stateItems
.length
; i
++) {
196 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
199 fEventColorMap
= new Color
[] { };
203 public void setTimeProvider(ITimeDataProvider timeProvider
) {
204 _timeProvider
= timeProvider
;
209 public void addSelectionListener(SelectionListener listener
) {
210 if (listener
== null)
211 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
212 if (null == _selectionListeners
)
213 _selectionListeners
= new ArrayList
<SelectionListener
>();
214 _selectionListeners
.add(listener
);
217 public void removeSelectionListener(SelectionListener listener
) {
218 if (null != _selectionListeners
)
219 _selectionListeners
.remove(listener
);
222 public void fireSelectionChanged() {
223 if (null != _selectionListeners
) {
224 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
225 while (it
.hasNext()) {
226 SelectionListener listener
= it
.next();
227 listener
.widgetSelected(null);
232 public void fireDefaultSelection() {
233 if (null != _selectionListeners
) {
234 Iterator
<SelectionListener
> it
= _selectionListeners
.iterator();
235 while (it
.hasNext()) {
236 SelectionListener listener
= it
.next();
237 listener
.widgetDefaultSelected(null);
242 public ITimeGraphEntry
[] getTraces() {
243 return _data
.getTraces();
246 public boolean[] getTraceFilter() {
247 return _data
.getTraceFilter();
250 public void refreshData() {
256 public void refreshData(ITimeGraphEntry traces
[]) {
257 _data
.refreshData(traces
);
262 public void adjustScrolls() {
263 if (null == _timeProvider
) {
264 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
270 long time0
= _timeProvider
.getTime0();
271 long time1
= _timeProvider
.getTime1();
273 long timeMin
= _timeProvider
.getMinTime();
274 long timeMax
= _timeProvider
.getMaxTime();
276 long delta
= timeMax
- timeMin
;
279 int thumb
= H_SCROLLBAR_MAX
;
282 // Thumb size (page size)
283 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
284 // At the beginning of visible window
285 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
288 // position, minimum, maximum, thumb size, increment (half page)t, page
289 // increment size (full page)
290 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
293 boolean ensureVisibleItem(int idx
, boolean redraw
) {
294 boolean changed
= false;
296 for (idx
= 0; idx
< _data
._expandedItems
.length
; idx
++) {
297 if (((TimeGraphItem
) _data
._expandedItems
[idx
])._selected
)
301 if (idx
>= _data
._expandedItems
.length
)
303 if (idx
< _topIndex
) {
305 //FIXME:getVerticalBar().setSelection(_topItem);
310 int page
= countPerPage();
311 if (idx
>= _topIndex
+ page
) {
312 setTopIndex(idx
- page
+ 1);
313 //FIXME:getVerticalBar().setSelection(_topItem);
322 public void setTopIndex(int idx
) {
323 idx
= Math
.min(idx
, _data
._expandedItems
.length
- countPerPage());
324 idx
= Math
.max(0, idx
);
329 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
330 TimeGraphItem item
= _data
.findItem(entry
);
331 if (item
!= null && item
._expanded
!= expanded
) {
332 item
._expanded
= expanded
;
333 _data
.updateExpandedItems();
338 public void addTreeListener (ITimeGraphTreeListener listener
) {
339 if (!_treeListeners
.contains(listener
)) {
340 _treeListeners
.add(listener
);
344 public void removeTreeListener (ITimeGraphTreeListener listener
) {
345 if (_treeListeners
.contains(listener
)) {
346 _treeListeners
.remove(listener
);
350 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
351 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
352 for (ITimeGraphTreeListener listener
: _treeListeners
) {
354 listener
.treeExpanded(event
);
356 listener
.treeCollapsed(event
);
362 public ISelection
getSelection() {
363 TimeGraphSelection sel
= new TimeGraphSelection();
364 ITimeGraphEntry trace
= getSelectedTrace();
365 if (null != trace
&& null != _timeProvider
) {
366 long selectedTime
= _timeProvider
.getSelectedTime();
367 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
376 public ISelection
getSelectionTrace() {
377 TimeGraphSelection sel
= new TimeGraphSelection();
378 ITimeGraphEntry trace
= getSelectedTrace();
386 // TODO select implementation for selectTrace
387 // public void selectTrace(int n) {
388 // if (n != 1 && n != -1)
390 // boolean changed = false;
391 // int lastSelection = -1;
392 // for (int i = 0; i < _data._expandedItems.length; i++) {
393 // TimeGraphItem item = (TimeGraphItem) _data._expandedItems[i];
394 // if (item._selected) {
395 // lastSelection = i;
396 // if (1 == n && i < _data._expandedItems.length - 1) {
397 // item._selected = false;
398 // if (item._hasChildren) {
399 // _data.expandItem(i);
400 // fireTreeEvent(item._trace, item._expanded);
402 // item = (TimeGraphItem) _data._expandedItems[i + 1];
403 // if (item._hasChildren) {
404 // _data.expandItem(i + 1);
405 // fireTreeEvent(item._trace, item._expanded);
406 // item = (TimeGraphItem) _data._expandedItems[i + 2];
408 // item._selected = true;
410 // } else if (-1 == n && i > 0) {
412 // TimeGraphItem prevItem = (TimeGraphItem) _data._expandedItems[i];
413 // if (prevItem._hasChildren) {
414 // if (prevItem._expanded) {
417 // prevItem = (TimeGraphItem) _data._expandedItems[i];
420 // if (!prevItem._expanded) {
421 // _data.expandItem(i);
422 // fireTreeEvent(prevItem._trace, prevItem._expanded);
423 // prevItem = (TimeGraphItem) _data._expandedItems[i + prevItem.children.size()];
424 // item._selected = false;
425 // prevItem._selected = true;
429 // item._selected = false;
430 // prevItem._selected = true;
437 // if (lastSelection < 0 && _data._expandedItems.length > 0) {
438 // TimeGraphItem item = (TimeGraphItem) _data._expandedItems[0];
439 // if (item._hasChildren) {
440 // _data.expandItem(0);
441 // fireTreeEvent(item._trace, item._expanded);
442 // item = (TimeGraphItem) _data._expandedItems[1];
443 // item._selected = true;
446 // item._selected = true;
451 // ensureVisibleItem(-1, false);
453 // fireSelectionChanged();
457 public void selectTrace(int n
) {
458 if ((n
!= 1) && (n
!= -1)) {
462 boolean changed
= false;
463 int lastSelection
= -1;
464 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
465 TimeGraphItem item
= (TimeGraphItem
) _data
._expandedItems
[i
];
466 if (item
._selected
) {
468 if ((1 == n
) && (i
< _data
._expandedItems
.length
- 1)) {
469 item
._selected
= false;
470 item
= (TimeGraphItem
) _data
._expandedItems
[i
+ 1];
471 item
._selected
= true;
473 } else if ((-1 == n
) && (i
> 0)) {
474 item
._selected
= false;
475 item
= (TimeGraphItem
) _data
._expandedItems
[i
- 1];
476 item
._selected
= true;
483 if (lastSelection
< 0 && _data
._expandedItems
.length
> 0) {
484 TimeGraphItem item
= (TimeGraphItem
) _data
._expandedItems
[0];
485 item
._selected
= true;
490 ensureVisibleItem(-1, false);
492 fireSelectionChanged();
496 public void selectEvent(int n
) {
497 if (null == _timeProvider
)
499 ITimeGraphEntry trace
= getSelectedTrace();
502 long selectedTime
= _timeProvider
.getSelectedTime();
503 long endTime
= _timeProvider
.getEndTime();
504 ITimeEvent nextEvent
;
505 if (-1 == n
&& selectedTime
> endTime
)
506 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
508 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
509 if (null == nextEvent
&& -1 == n
)
510 nextEvent
= Utils
.getFirstEvent(trace
);
511 if (null != nextEvent
) {
512 long nextTime
= nextEvent
.getTime();
513 // If last event detected e.g. going back or not moving to a next
515 if (nextTime
<= selectedTime
&& n
== 1) {
516 // Select to the end of this last event
517 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
518 // but not beyond the end of the trace
519 if (nextTime
> endTime
) {
523 _timeProvider
.setSelectedTimeNotify(nextTime
, true);
524 fireSelectionChanged();
526 _timeProvider
.setSelectedTimeNotify(endTime
, true);
527 fireSelectionChanged();
531 public void selectNextEvent() {
533 // Notify if visible time window has been adjusted
534 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
537 public void selectPrevEvent() {
539 // Notify if visible time window has been adjusted
540 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getTime0(), _timeProvider
.getTime1());
543 public void selectNextTrace() {
547 public void selectPrevTrace() {
552 * Zoom based on mouse cursor location with mouse scrolling
554 * @param zoomIn true to zoom in, false to zoom out
556 public void zoom(boolean zoomIn
) {
557 int globalX
= getDisplay().getCursorLocation().x
;
558 Point p
= toControl(globalX
, 0);
559 int nameSpace
= _timeProvider
.getNameSpace();
560 int timeSpace
= _timeProvider
.getTimeSpace();
561 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
562 long time0
= _timeProvider
.getTime0();
563 long time1
= _timeProvider
.getTime1();
564 long interval
= time1
- time0
;
567 } // to allow getting out of single point interval
570 newInterval
= Math
.max(Math
.round((double) interval
* 0.8), _timeProvider
.getMinTimeInterval());
572 newInterval
= (long) Math
.ceil((double) interval
* 1.25);
574 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
575 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
576 long newTime1
= newTime0
+ newInterval
;
577 _timeProvider
.setStartFinishTime(newTime0
, newTime1
);
578 synchronized (fMouseScrollNotifierLock
) {
579 if (fMouseScrollNotifier
== null) {
580 fMouseScrollNotifier
= new MouseScrollNotifier();
581 fMouseScrollNotifier
.start();
583 fMouseScrollNotifier
.mouseScrolled();
588 * zoom in using single click
590 public void zoomIn() {
591 long _time0
= _timeProvider
.getTime0();
592 long _time1
= _timeProvider
.getTime1();
593 long _range
= _time1
- _time0
;
594 long selTime
= _timeProvider
.getSelectedTime();
595 if (selTime
<= _time0
|| selTime
>= _time1
) {
596 selTime
= (_time0
+ _time1
) / 2;
598 long time0
= selTime
- (long) ((selTime
- _time0
) / zoomCoeff
);
599 long time1
= selTime
+ (long) ((_time1
- selTime
) / zoomCoeff
);
601 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
603 // Trace.debug("selTime:" + selTime + " time0:" + time0 + " time1:"
604 // + time1 + " inaccuracy:" + inaccuracy);
606 if (inaccuracy
> 0 && inaccuracy
< 100) {
607 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
611 long m
= _timeProvider
.getMinTimeInterval();
612 if ((time1
- time0
) < m
) {
613 time0
= selTime
- (long) ((selTime
- _time0
) * m
/ _range
);
617 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
621 * zoom out using single click
623 public void zoomOut() {
624 long _time0
= _timeProvider
.getTime0();
625 long _time1
= _timeProvider
.getTime1();
626 long selTime
= _timeProvider
.getSelectedTime();
627 if (selTime
<= _time0
|| selTime
>= _time1
) {
628 selTime
= (_time0
+ _time1
) / 2;
630 long time0
= (long) (selTime
- (selTime
- _time0
) * zoomCoeff
);
631 long time1
= (long) (selTime
+ (_time1
- selTime
) * zoomCoeff
);
633 long inaccuracy
= (_timeProvider
.getMaxTime() - _timeProvider
.getMinTime()) - (time1
- time0
);
634 if (inaccuracy
> 0 && inaccuracy
< 100) {
635 _timeProvider
.setStartFinishTimeNotify(_timeProvider
.getMinTime(), _timeProvider
.getMaxTime());
639 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
642 public ITimeGraphEntry
getSelectedTrace() {
643 ITimeGraphEntry trace
= null;
644 int idx
= getSelectedIndex();
646 trace
= _data
._expandedItems
[idx
]._trace
;
650 public int getSelectedIndex() {
652 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
653 TimeGraphItem item
= (TimeGraphItem
) _data
._expandedItems
[i
];
654 if (item
._selected
) {
662 boolean toggle(int idx
) {
663 boolean toggled
= false;
664 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
665 TimeGraphItem item
= (TimeGraphItem
) _data
._expandedItems
[idx
];
666 if (item
._hasChildren
) {
667 item
._expanded
= !item
._expanded
;
668 _data
.updateExpandedItems();
672 fireTreeEvent(item
._trace
, item
._expanded
);
678 int getItemIndexAtY(int y
) {
682 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
684 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
685 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
692 int idx
= y
/ _itemHeight
;
694 if (idx
< _data
._expandedItems
.length
) {
700 boolean isOverSplitLine(int x
) {
701 if (x
< 0 || null == _timeProvider
)
704 int nameWidth
= _timeProvider
.getNameSpace();
705 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
712 TimeGraphItem
getItem(Point pt
) {
713 int idx
= getItemIndexAtY(pt
.y
);
714 return idx
>= 0 ?
(TimeGraphItem
) _data
._expandedItems
[idx
] : null;
717 long getTimeAtX(int x
) {
718 if (null == _timeProvider
)
721 Point size
= getCtrlSize();
722 long time0
= _timeProvider
.getTime0();
723 long time1
= _timeProvider
.getTime1();
724 int nameWidth
= _timeProvider
.getNameSpace();
726 if (x
>= 0 && size
.x
>= nameWidth
) {
727 if (time1
- time0
> size
.x
- nameWidth
- RIGHT_MARGIN
) {
728 // get the last possible time represented by the pixel position
729 // by taking the time of the next pixel position minus 1
731 hitTime
= time0
+ (long) ((time1
- time0
) * ((double) (x
+ 1) / (size
.x
- nameWidth
- RIGHT_MARGIN
))) - 1;
733 hitTime
= time0
+ (long) ((time1
- time0
) * ((double) (x
) / (size
.x
- nameWidth
- RIGHT_MARGIN
)));
739 void selectItem(int idx
, boolean addSelection
) {
740 boolean changed
= false;
742 if (idx
>= 0 && idx
< _data
._expandedItems
.length
) {
743 TimeGraphItem item
= (TimeGraphItem
) _data
._expandedItems
[idx
];
744 changed
= (item
._selected
== false);
745 item
._selected
= true;
748 for (int i
= 0; i
< _data
._expandedItems
.length
; i
++) {
749 TimeGraphItem item
= (TimeGraphItem
) _data
._expandedItems
[i
];
750 if ((i
== idx
&& !item
._selected
) || (idx
== -1 && item
._selected
)) {
753 item
._selected
= i
== idx
;
756 changed
|= ensureVisibleItem(idx
, true);
761 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
762 int idx
= _data
.findItemIndex(trace
);
763 selectItem(idx
, addSelection
);
766 public int countPerPage() {
767 int height
= getCtrlSize().y
;
769 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
771 for (int idx
= _topIndex
; idx
< _data
._expandedItems
.length
; idx
++) {
772 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
773 if (ySum
>= height
) {
778 for (int idx
= _topIndex
- 1; idx
>= 0; idx
--) {
779 ySum
+= _data
._expandedItems
[idx
].itemHeight
;
780 if (ySum
>= height
) {
788 count
= height
/ _itemHeight
;
793 public int getTopIndex() {
797 public int getExpandedElementCount() {
798 return _data
._expandedItems
.length
;
801 public ITimeGraphEntry
[] getExpandedElements() {
802 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
803 for (TimeGraphItem item
: _data
._expandedItems
) {
804 elements
.add(item
._trace
);
806 return elements
.toArray(new ITimeGraphEntry
[0]);
809 Point
getCtrlSize() {
810 Point size
= getSize();
811 if (getHorizontalBar().isVisible()) {
812 size
.y
-= getHorizontalBar().getSize().y
;
817 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
819 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
820 int width
= nameWidth
;
821 int height
= _itemHeight
;
822 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
824 for (int i
= _topIndex
; i
< idx
; i
++) {
825 ySum
+= _data
._expandedItems
[i
].itemHeight
;
828 height
= _data
._expandedItems
[idx
].itemHeight
;
830 return new Rectangle(x
, y
, width
, height
);
833 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
834 int x
= bound
.x
+ nameWidth
;
835 int y
= bound
.y
+ (idx
- _topIndex
) * _itemHeight
;
836 int width
= bound
.width
- x
;
837 int height
= _itemHeight
;
838 if (_itemHeight
== CUSTOM_ITEM_HEIGHT
) {
840 for (int i
= _topIndex
; i
< idx
; i
++) {
841 ySum
+= _data
._expandedItems
[i
].itemHeight
;
844 height
= _data
._expandedItems
[idx
].itemHeight
;
846 return new Rectangle(x
, y
, width
, height
);
850 void paint(Rectangle bounds
, PaintEvent e
) {
852 gc
.setBackground(_colors
.getColor(TimeGraphColorScheme
.BACKGROUND
));
853 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
855 if (bounds
.width
< 2 || bounds
.height
< 2 || null == _timeProvider
)
859 int nameSpace
= _timeProvider
.getNameSpace();
861 // draw empty name space background
862 gc
.setBackground(_colors
.getBkColor(false, false, true));
863 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
865 drawItems(bounds
, _timeProvider
, _data
._expandedItems
, _topIndex
, nameSpace
, gc
);
867 // draw selected time
868 long time0
= _timeProvider
.getTime0();
869 long time1
= _timeProvider
.getTime1();
870 long selectedTime
= _timeProvider
.getSelectedTime();
871 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
872 int x
= bounds
.x
+ nameSpace
+ (int) ((double) (selectedTime
- time0
) * pixelsPerNanoSec
);
873 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
874 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.SELECTED_TIME
));
875 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
878 // draw drag line, no line if name space is 0.
879 if (DRAG_SPLIT_LINE
== _dragState
) {
880 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.BLACK
));
881 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
882 } else if (DRAG_NONE
== _dragState
&& _mouseOverSplitLine
&& _timeProvider
.getNameSpace() > 0) {
883 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.RED
));
884 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
888 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
, TimeGraphItem
[] items
, int topIndex
, int nameSpace
, GC gc
) {
889 for (int i
= topIndex
; i
< items
.length
; i
++) {
890 TimeGraphItem item
= (TimeGraphItem
) items
[i
];
891 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
893 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
899 * @param item the item to draw
900 * @param bounds the container rectangle
901 * @param i the item index
902 * @param nameSpace the name space
905 protected void drawItem(TimeGraphItem item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
906 ITimeGraphEntry entry
= item
._trace
;
907 long time0
= timeProvider
.getTime0();
908 long time1
= timeProvider
.getTime1();
909 long selectedTime
= timeProvider
.getSelectedTime();
911 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
912 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
916 if (! item
._trace
.hasTimeEvents()) {
917 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
918 nameRect
.width
+= statesRect
.width
;
919 drawName(item
, nameRect
, gc
);
921 drawName(item
, nameRect
, gc
);
923 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
924 if (rect
.isEmpty()) {
925 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
928 if (time1
<= time0
) {
929 gc
.setBackground(_colors
.getBkColor(false, false, false));
930 gc
.fillRectangle(rect
);
931 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
935 // Initialize _rect1 to same values as enclosing rectangle rect
936 Rectangle stateRect
= Utils
.clone(rect
);
937 boolean selected
= item
._selected
;
938 // K pixels per second
939 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
941 if (item
._trace
.hasTimeEvents()) {
942 fillSpace(rect
, gc
, selected
);
943 // Drawing rectangle is smaller than reserved space
945 stateRect
.height
-= 6;
947 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
948 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
951 while (iterator
.hasNext()) {
952 ITimeEvent event
= iterator
.next();
953 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
954 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
955 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
956 // event is out of bounds
959 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
960 stateRect
.x
= Math
.max(rect
.x
, x
);
961 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
962 if (stateRect
.x
== lastX
) {
963 stateRect
.width
-= 1;
964 if (stateRect
.width
> 0) {
965 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
966 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
972 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
973 drawState(_colors
, event
, stateRect
, gc
, selected
, timeSelected
);
976 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
979 protected void drawName(TimeGraphItem item
, Rectangle bounds
, GC gc
) {
980 boolean hasTimeEvents
= item
._trace
.hasTimeEvents();
981 if (! hasTimeEvents
) {
982 gc
.setBackground(_colors
.getBkColorGroup(item
._selected
, _isInFocus
));
983 gc
.fillRectangle(bounds
);
984 if (item
._selected
&& _isInFocus
) {
985 gc
.setForeground(_colors
.getBkColor(item
._selected
, _isInFocus
, false));
986 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
989 gc
.setBackground(_colors
.getBkColor(item
._selected
, _isInFocus
, true));
990 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
991 gc
.fillRectangle(bounds
);
994 // No name to be drawn
995 if (_timeProvider
.getNameSpace() == 0) {
999 int leftMargin
= MARGIN
+ item
.level
* EXPAND_SIZE
;
1000 if (item
._hasChildren
) {
1001 gc
.setForeground(_colors
.getFgColorGroup(false, false));
1002 gc
.setBackground(_colors
.getBkColor(false, false, false));
1003 Rectangle rect
= Utils
.clone(bounds
);
1004 rect
.x
+= leftMargin
;
1005 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1006 rect
.width
= EXPAND_SIZE
;
1007 rect
.height
= EXPAND_SIZE
;
1008 gc
.fillRectangle(rect
);
1009 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1010 int midy
= rect
.y
+ rect
.height
/ 2;
1011 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1012 if (!item
._expanded
) {
1013 int midx
= rect
.x
+ rect
.width
/ 2;
1014 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1017 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1019 Image img
= fTimeGraphProvider
.getItemImage(item
._trace
);
1022 int imgHeight
= img
.getImageData().height
;
1023 int imgWidth
= img
.getImageData().width
;
1025 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1026 gc
.drawImage(img
, x
, y
);
1027 leftMargin
+= imgWidth
+ MARGIN
;
1029 String name
= item
._name
;
1030 Point size
= gc
.stringExtent(name
);
1031 if (_idealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1032 _idealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1034 if (hasTimeEvents
) {
1035 // cut long string with "..."
1036 int width
= bounds
.width
- leftMargin
;
1038 while (size
.x
> width
&& name
.length() > 1) {
1040 name
= name
.substring(0, name
.length() - 1);
1041 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1044 name
+= "..."; //$NON-NLS-1$
1047 Rectangle rect
= Utils
.clone(bounds
);
1048 rect
.x
+= leftMargin
;
1049 rect
.width
-= leftMargin
;
1051 if (rect
.width
> 0) {
1052 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1053 gc
.setForeground(_colors
.getFgColor(item
._selected
, _isInFocus
));
1054 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1055 leftMargin
+= textWidth
+ MARGIN
;
1058 if (hasTimeEvents
) {
1060 int x
= bounds
.x
+ leftMargin
;
1061 int width
= bounds
.width
- x
;
1062 int midy
= bounds
.y
+ bounds
.height
/ 2;
1063 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1064 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1069 protected void drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1070 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1072 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1076 boolean visible
= rect
.width
== 0 ?
false : true;
1079 Color stateColor
= null;
1080 if (colorIdx
< fEventColorMap
.length
) {
1081 stateColor
= fEventColorMap
[colorIdx
];
1083 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1086 timeSelected
= timeSelected
&& selected
;
1088 // modify the color?
1090 // fill all rect area
1091 gc
.setBackground(stateColor
);
1092 gc
.fillRectangle(rect
);
1093 // get the border color?
1094 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1097 if (!timeSelected
) {
1098 // Draw the top and bottom borders i.e. no side borders
1100 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1102 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1105 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1106 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1108 // selected rectangle area is not visible but can be represented
1109 // with a broken vertical line of specified width.
1112 gc.setForeground(stateColor);
1113 int s = gc.getLineStyle();
1114 int w = gc.getLineWidth();
1115 gc.setLineStyle(SWT.LINE_DOT);
1116 gc.setLineWidth(width);
1117 // Trace.debug("Rectangle not visible, drawing vertical line with: "
1118 // + rect.x + "," + rect.y + "," + rect.x + "," + rect.y
1120 gc.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1);
1123 if (!timeSelected) {
1124 gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
1125 gc.drawPoint(rect.x, rect.y);
1126 gc.drawPoint(rect.x, rect.y + rect.height - 1);
1130 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1133 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1134 gc
.setBackground(_colors
.getBkColor(selected
, _isInFocus
, false));
1135 gc
.fillRectangle(rect
);
1137 gc
.setForeground(_colors
.getColor(TimeGraphColorScheme
.MID_LINE
));
1138 int midy
= rect
.y
+ rect
.height
/ 2;
1139 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1143 public void keyTraversed(TraverseEvent e
) {
1144 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
))
1149 public void keyPressed(KeyEvent e
) {
1151 if (_data
._expandedItems
.length
== 0) {
1154 if (SWT
.HOME
== e
.keyCode
) {
1156 } else if (SWT
.END
== e
.keyCode
) {
1157 idx
= _data
._expandedItems
.length
- 1;
1158 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1159 idx
= getSelectedIndex();
1162 else if (idx
< _data
._expandedItems
.length
- 1)
1164 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1165 idx
= getSelectedIndex();
1170 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1172 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1174 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1175 int page
= countPerPage();
1176 idx
= getSelectedIndex();
1180 if (idx
>= _data
._expandedItems
.length
)
1181 idx
= _data
._expandedItems
.length
- 1;
1182 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1183 int page
= countPerPage();
1184 idx
= getSelectedIndex();
1190 } else if (SWT
.CR
== e
.keyCode
) {
1191 idx
= getSelectedIndex();
1193 if (_data
._expandedItems
[idx
]._hasChildren
) {
1196 fireDefaultSelection();
1202 selectItem(idx
, false);
1203 fireSelectionChanged();
1208 public void keyReleased(KeyEvent e
) {
1212 public void focusGained(FocusEvent e
) {
1214 if (mouseScrollFilterListener
== null) {
1215 mouseScrollFilterListener
= new Listener() {
1216 // This filter is used to prevent horizontal scrolling of the view
1217 // when the mouse wheel is used to zoom
1219 public void handleEvent(Event event
) {
1223 getDisplay().addFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1229 public void focusLost(FocusEvent e
) {
1231 if (mouseScrollFilterListener
!= null) {
1232 getDisplay().removeFilter(SWT
.MouseWheel
, mouseScrollFilterListener
);
1233 mouseScrollFilterListener
= null;
1235 if (DRAG_NONE
!= _dragState
) {
1237 _dragState
= DRAG_NONE
;
1242 public boolean isInFocus() {
1247 * Provide the possibilty to control the wait cursor externally e.g. data
1248 * requests in progress
1252 public void waitCursor(boolean waitInd
) {
1253 // Update cursor as indicated
1255 setCursor(_WaitCursor
);
1256 _isWaitCursor
= true;
1259 _isWaitCursor
= false;
1262 // Get ready for next mouse move
1263 _isDragCursor3
= false;
1268 * If the x, y position is over the vertical split line (name to time
1269 * ranges), then change the cursor to a drag cursor to indicate the user the
1270 * possibility of resizing
1276 void updateCursor(int x
, int y
) {
1277 // if Wait cursor not active, check for the need to change to a drag
1279 if (_isWaitCursor
== false) {
1280 boolean isSplitLine
= isOverSplitLine(x
);
1281 // No dragcursor is name space is fixed to zero
1282 if (isSplitLine
&& !_isDragCursor3
&& _timeProvider
.getNameSpace() > 0) {
1283 setCursor(_dragCursor3
);
1284 _isDragCursor3
= true;
1285 } else if (!isSplitLine
&& _isDragCursor3
) {
1287 _isDragCursor3
= false;
1293 public void mouseMove(MouseEvent e
) {
1294 if (null == _timeProvider
)
1296 Point size
= getCtrlSize();
1297 if (DRAG_TRACE_ITEM
== _dragState
) {
1298 int nameWidth
= _timeProvider
.getNameSpace();
1299 int x
= e
.x
- nameWidth
;
1300 if (x
> 0 && size
.x
> nameWidth
&& _dragX
!= x
) {
1302 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (_time1bak
- _time0bak
);
1303 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((_dragX
- _dragX0
) / pixelsPerNanoSec
));
1304 long time1
= _time1bak
- timeDelta
;
1305 long maxTime
= _timeProvider
.getMaxTime();
1306 if (time1
> maxTime
)
1308 long time0
= time1
- (_time1bak
- _time0bak
);
1309 if (time0
< _timeProvider
.getMinTime()) {
1310 time0
= _timeProvider
.getMinTime();
1311 time1
= time0
+ (_time1bak
- _time0bak
);
1313 _timeProvider
.setStartFinishTime(time0
, time1
);
1315 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1317 _timeProvider
.setNameSpace(e
.x
);
1318 } else if (DRAG_NONE
== _dragState
) {
1319 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1320 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1323 _mouseOverSplitLine
= mouseOverSplitLine
;
1325 updateCursor(e
.x
, e
.y
);
1329 public void mouseDoubleClick(MouseEvent e
) {
1330 if (null == _timeProvider
)
1332 if (1 == e
.button
) {
1333 if (isOverSplitLine(e
.x
) && _timeProvider
.getNameSpace() != 0) {
1334 _timeProvider
.setNameSpace(_idealNameSpace
);
1335 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1336 if (_mouseOverSplitLine
!= mouseOverSplitLine
) {
1339 _mouseOverSplitLine
= mouseOverSplitLine
;
1342 int idx
= getItemIndexAtY(e
.y
);
1344 selectItem(idx
, false);
1345 fireDefaultSelection();
1351 public void mouseDown(MouseEvent e
) {
1352 if (null == _timeProvider
)
1355 if (1 == e
.button
) {
1356 int nameSpace
= _timeProvider
.getNameSpace();
1357 if (nameSpace
!= 0) {
1358 if (isOverSplitLine(e
.x
)) {
1359 _dragState
= DRAG_SPLIT_LINE
;
1360 _dragX
= _dragX0
= e
.x
;
1361 _time0bak
= _timeProvider
.getTime0();
1362 _time1bak
= _timeProvider
.getTime1();
1368 idx
= getItemIndexAtY(e
.y
);
1370 TimeGraphItem item
= _data
._expandedItems
[idx
];
1371 if (item
._hasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.level
+ 1) * EXPAND_SIZE
) {
1374 long hitTime
= getTimeAtX(e
.x
);
1376 // _timeProvider.setSelectedTimeInt(hitTime, false);
1378 _dragState
= DRAG_TRACE_ITEM
;
1379 _dragX
= _dragX0
= e
.x
- nameSpace
;
1380 _time0bak
= _timeProvider
.getTime0();
1381 _time1bak
= _timeProvider
.getTime1();
1384 selectItem(idx
, false);
1385 fireSelectionChanged();
1387 selectItem(idx
, false); // clear selection
1389 fireSelectionChanged();
1395 public void mouseUp(MouseEvent e
) {
1396 if (DRAG_NONE
!= _dragState
) {
1398 if (DRAG_TRACE_ITEM
== _dragState
) {
1399 // Notify time provider to check the need for listener
1401 _timeProvider
.notifyStartFinishTime();
1402 if (_dragX
== _dragX0
) { // click without drag
1403 long time
= getTimeAtX(e
.x
);
1404 _timeProvider
.setSelectedTimeNotify(time
, false);
1406 } else if (DRAG_SPLIT_LINE
== _dragState
) {
1409 _dragState
= DRAG_NONE
;
1414 public void mouseEnter(MouseEvent e
) {
1418 public void mouseExit(MouseEvent e
) {
1419 if (_mouseOverSplitLine
) {
1420 _mouseOverSplitLine
= false;
1426 public void mouseHover(MouseEvent e
) {
1430 public void mouseScrolled(MouseEvent e
) {
1431 if ((mouseScrollFilterListener
== null) || _dragState
!= DRAG_NONE
) {
1434 boolean zoomScroll
= false;
1435 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1436 Point parentSize
= getParent().getSize();
1437 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1438 // over the parent control
1439 if (e
.x
> getCtrlSize().x
) {
1440 // over the horizontal scroll bar
1442 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< _timeProvider
.getNameSpace()) {
1443 // over the name space
1449 if (zoomScroll
&& _timeProvider
.getTime0() != _timeProvider
.getTime1()) {
1452 } else if (e
.count
< 0) {
1456 setTopIndex(getTopIndex() - e
.count
);
1461 public void controlMoved(ControlEvent e
) {
1465 public void controlResized(ControlEvent e
) {
1470 public void widgetDefaultSelected(SelectionEvent e
) {
1474 public void widgetSelected(SelectionEvent e
) {
1475 if (e
.widget
== getVerticalBar()) {
1476 setTopIndex(getVerticalBar().getSelection());
1477 } else if (e
.widget
== getHorizontalBar() && null != _timeProvider
) {
1478 int start
= getHorizontalBar().getSelection();
1479 long time0
= _timeProvider
.getTime0();
1480 long time1
= _timeProvider
.getTime1();
1481 long timeMin
= _timeProvider
.getMinTime();
1482 long timeMax
= _timeProvider
.getMaxTime();
1483 long delta
= timeMax
- timeMin
;
1485 long range
= time1
- time0
;
1486 // _timeRangeFixed = true;
1487 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1488 time1
= time0
+ range
;
1490 // TODO: Follow-up with Bug 310310
1491 // In Linux SWT.DRAG is the only value received
1492 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1493 if (e
.detail
== SWT
.DRAG
) {
1494 _timeProvider
.setStartFinishTime(time0
, time1
);
1496 _timeProvider
.setStartFinishTimeNotify(time0
, time1
);
1501 public boolean isVisibleVerticalScroll() {
1502 return _visibleVerticalScroll
;
1506 public int getBorderWidth() {
1507 return _borderWidth
;
1510 public void setBorderWidth(int borderWidth
) {
1511 this._borderWidth
= borderWidth
;
1514 public int getHeaderHeight() {
1515 return _headerHeight
;
1518 public void setHeaderHeight(int headerHeight
) {
1519 this._headerHeight
= headerHeight
;
1522 public int getItemHeight() {
1526 public void setItemHeight(int rowHeight
) {
1527 this._itemHeight
= rowHeight
;
1530 public void setMinimumItemWidth(int width
) {
1531 this._minimumItemWidth
= width
;
1534 public int getMinimumItemWidth() {
1535 return _minimumItemWidth
;
1538 public Vector
<ITimeGraphEntry
> getFilteredOut() {
1539 return _data
.getFilteredOut();
1544 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1545 if (listener
!= null) {
1546 if (!_selectionChangedListeners
.contains(listener
)) {
1547 _selectionChangedListeners
.add(listener
);
1554 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1555 if (listener
!= null) {
1556 _selectionChangedListeners
.remove(listener
);
1562 public void setSelection(ISelection selection
) {
1563 if (selection
instanceof TimeGraphSelection
) {
1564 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1565 Object ob
= sel
.getFirstElement();
1566 if (ob
instanceof ITimeGraphEntry
) {
1567 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1568 selectItem(trace
, false);
1577 public TimeGraphItem
[] _expandedItems
= new TimeGraphItem
[0];
1578 public TimeGraphItem
[] _items
= new TimeGraphItem
[0];
1579 private ITimeGraphEntry _traces
[] = new ITimeGraphEntry
[0];
1580 private boolean traceFilter
[] = new boolean[0];
1581 private Vector
<ITimeGraphEntry
> filteredOut
= new Vector
<ITimeGraphEntry
>();
1582 public ITimeGraphPresentationProvider provider
;
1587 TimeGraphItem
findItem(ITimeGraphEntry entry
) {
1591 for (int i
= 0; i
< _items
.length
; i
++) {
1592 TimeGraphItem item
= _items
[i
];
1593 if (item
._trace
== entry
) {
1601 int findItemIndex(ITimeGraphEntry trace
) {
1605 for (int i
= 0; i
< _expandedItems
.length
; i
++) {
1606 TimeGraphItem item
= _expandedItems
[i
];
1607 if (item
._trace
== trace
) {
1615 public void refreshData() {
1616 List
<TimeGraphItem
> itemList
= new ArrayList
<TimeGraphItem
>();
1617 filteredOut
.clear();
1618 for (int i
= 0; i
< _traces
.length
; i
++) {
1619 ITimeGraphEntry entry
= _traces
[i
];
1620 refreshData(itemList
, null, 0, entry
);
1622 _items
= itemList
.toArray(new TimeGraphItem
[0]);
1623 updateExpandedItems();
1626 private void refreshData(List
<TimeGraphItem
> itemList
, TimeGraphItem parent
, int level
, ITimeGraphEntry entry
) {
1627 TimeGraphItem item
= new TimeGraphItem(entry
, entry
.getName(), level
);
1628 if (parent
!= null) {
1629 parent
.children
.add(item
);
1631 item
.itemHeight
= provider
.getItemHeight(entry
);
1633 if (entry
.hasChildren()) {
1634 item
._expanded
= true;
1635 item
._hasChildren
= true;
1636 for (ITimeGraphEntry child
: entry
.getChildren()) {
1637 refreshData(itemList
, item
, level
+ 1, child
);
1642 public void updateExpandedItems() {
1643 List
<TimeGraphItem
> expandedItemList
= new ArrayList
<TimeGraphItem
>();
1644 for (int i
= 0; i
< _traces
.length
; i
++) {
1645 ITimeGraphEntry entry
= _traces
[i
];
1646 TimeGraphItem item
= findItem(entry
);
1647 refreshExpanded(expandedItemList
, item
);
1649 _expandedItems
= expandedItemList
.toArray(new TimeGraphItem
[0]);
1652 private void refreshExpanded(List
<TimeGraphItem
> expandedItemList
, TimeGraphItem item
) {
1653 expandedItemList
.add(item
);
1654 if (item
._hasChildren
&& item
._expanded
) {
1655 for (TimeGraphItem child
: item
.children
) {
1656 refreshExpanded(expandedItemList
, child
);
1661 public void expandItem(int idx
) {
1662 if (idx
< 0 || idx
>= _expandedItems
.length
)
1664 TimeGraphItem item
= (TimeGraphItem
) _expandedItems
[idx
];
1665 if (item
._hasChildren
&& !item
._expanded
) {
1666 item
._expanded
= true;
1667 updateExpandedItems();
1671 public void refreshData(ITimeGraphEntry traces
[]) {
1672 if (traces
== null || traces
.length
== 0) {
1674 } else if (traceFilter
== null || traces
.length
!= traceFilter
.length
) {
1675 traceFilter
= new boolean[traces
.length
];
1676 java
.util
.Arrays
.fill(traceFilter
, true);
1683 public ITimeGraphEntry
[] getTraces() {
1687 public boolean[] getTraceFilter() {
1691 public Vector
<ITimeGraphEntry
> getFilteredOut() {