1 /*****************************************************************************
2 * Copyright (c) 2007, 2013 Intel Corporation, 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
13 *****************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.Iterator
;
20 import java
.util
.List
;
22 import org
.eclipse
.jface
.resource
.JFaceResources
;
23 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
24 import org
.eclipse
.jface
.viewers
.ISelection
;
25 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
26 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
27 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.ControlEvent
;
36 import org
.eclipse
.swt
.events
.ControlListener
;
37 import org
.eclipse
.swt
.events
.FocusEvent
;
38 import org
.eclipse
.swt
.events
.FocusListener
;
39 import org
.eclipse
.swt
.events
.KeyEvent
;
40 import org
.eclipse
.swt
.events
.KeyListener
;
41 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
42 import org
.eclipse
.swt
.events
.MenuDetectListener
;
43 import org
.eclipse
.swt
.events
.MouseEvent
;
44 import org
.eclipse
.swt
.events
.MouseListener
;
45 import org
.eclipse
.swt
.events
.MouseMoveListener
;
46 import org
.eclipse
.swt
.events
.MouseTrackListener
;
47 import org
.eclipse
.swt
.events
.MouseWheelListener
;
48 import org
.eclipse
.swt
.events
.PaintEvent
;
49 import org
.eclipse
.swt
.events
.SelectionEvent
;
50 import org
.eclipse
.swt
.events
.SelectionListener
;
51 import org
.eclipse
.swt
.events
.TraverseEvent
;
52 import org
.eclipse
.swt
.events
.TraverseListener
;
53 import org
.eclipse
.swt
.events
.TypedEvent
;
54 import org
.eclipse
.swt
.graphics
.Color
;
55 import org
.eclipse
.swt
.graphics
.Cursor
;
56 import org
.eclipse
.swt
.graphics
.GC
;
57 import org
.eclipse
.swt
.graphics
.Image
;
58 import org
.eclipse
.swt
.graphics
.Point
;
59 import org
.eclipse
.swt
.graphics
.Rectangle
;
60 import org
.eclipse
.swt
.widgets
.Composite
;
61 import org
.eclipse
.swt
.widgets
.Display
;
62 import org
.eclipse
.swt
.widgets
.Event
;
63 import org
.eclipse
.swt
.widgets
.Listener
;
64 import org
.eclipse
.swt
.widgets
.ScrollBar
;
67 * Time graph control implementation
70 * @author Alvaro Sanchez-Leon
71 * @author Patrick Tasse
73 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
, MenuDetectListener
{
76 /** Max scrollbar size */
77 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
79 private static final int DRAG_NONE
= 0;
80 private static final int DRAG_TRACE_ITEM
= 1;
81 private static final int DRAG_SPLIT_LINE
= 2;
82 private static final int DRAG_ZOOM
= 3;
84 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
86 private static final double ZOOM_FACTOR
= 1.5;
87 private static final double ZOOM_IN_FACTOR
= 0.8;
88 private static final double ZOOM_OUT_FACTOR
= 1.25;
90 /** Resource manager */
91 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
93 /** Color map for event types */
94 private Color
[] fEventColorMap
= null;
96 private ITimeDataProvider fTimeProvider
;
97 private boolean fIsInFocus
= false;
98 private boolean fIsDragCursor
= false;
99 private boolean fIsWaitCursor
= true;
100 private boolean fMouseOverSplitLine
= false;
101 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
102 private int fMinimumItemWidth
= 0;
103 private int fTopIndex
= 0;
104 private int fDragState
= DRAG_NONE
;
105 private int fDragX0
= 0;
106 private int fDragX
= 0;
107 private int fIdealNameSpace
= 0;
108 private long fTime0bak
;
109 private long fTime1bak
;
110 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
111 private ItemData fItemData
= null;
112 private List
<SelectionListener
> fSelectionListeners
;
113 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
114 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
115 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
116 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
117 private final Cursor fDragCursor
;
118 private final Cursor fWaitCursor
;
119 private final List
<ViewerFilter
> fFilters
= new ArrayList
<ViewerFilter
>();
120 private MenuDetectEvent fPendingMenuDetectEvent
= null;
122 private int fBorderWidth
= 0;
123 private int fHeaderHeight
= 0;
125 private Listener fMouseScrollFilterListener
;
127 private MouseScrollNotifier fMouseScrollNotifier
;
128 private final Object fMouseScrollNotifierLock
= new Object();
130 private class MouseScrollNotifier
extends Thread
{
131 private static final long DELAY
= 400L;
132 private static final long POLLING_INTERVAL
= 10L;
133 private long fLastScrollTime
= Long
.MAX_VALUE
;
137 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
139 Thread
.sleep(POLLING_INTERVAL
);
140 } catch (Exception e
) {
144 if (!isInterrupted()) {
145 Display
.getDefault().asyncExec(new Runnable() {
151 fTimeProvider
.notifyStartFinishTime();
155 synchronized (fMouseScrollNotifierLock
) {
156 fMouseScrollNotifier
= null;
160 public void mouseScrolled() {
161 fLastScrollTime
= System
.currentTimeMillis();
166 * Standard constructor
169 * The parent composite object
171 * The color scheme to use
173 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
175 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
177 fItemData
= new ItemData();
179 addFocusListener(this);
180 addMouseListener(this);
181 addMouseMoveListener(this);
182 addMouseTrackListener(this);
183 addMouseWheelListener(this);
184 addTraverseListener(this);
185 addKeyListener(this);
186 addControlListener(this);
187 addMenuDetectListener(this);
188 ScrollBar scrollHor
= getHorizontalBar();
190 if (scrollHor
!= null) {
191 scrollHor
.addSelectionListener(this);
194 fDragCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_SIZEWE
);
195 fWaitCursor
= new Cursor(super.getDisplay(), SWT
.CURSOR_WAIT
);
199 public void dispose() {
201 fDragCursor
.dispose();
202 fWaitCursor
.dispose();
203 fResourceManager
.dispose();
207 * Sets the timegraph provider used by this timegraph viewer.
209 * @param timeGraphProvider the timegraph provider
211 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
212 fTimeGraphProvider
= timeGraphProvider
;
214 if (fEventColorMap
!= null) {
215 for (Color color
: fEventColorMap
) {
216 fResourceManager
.destroyColor(color
.getRGB());
219 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
220 if (stateItems
!= null) {
221 fEventColorMap
= new Color
[stateItems
.length
];
222 for (int i
= 0; i
< stateItems
.length
; i
++) {
223 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
226 fEventColorMap
= new Color
[] { };
231 * Assign the given time provider
233 * @param timeProvider
236 public void setTimeProvider(ITimeDataProvider timeProvider
) {
237 fTimeProvider
= timeProvider
;
243 * Add a selection listener
246 * The listener to add
248 public void addSelectionListener(SelectionListener listener
) {
249 if (listener
== null) {
250 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
252 if (null == fSelectionListeners
) {
253 fSelectionListeners
= new ArrayList
<SelectionListener
>();
255 fSelectionListeners
.add(listener
);
259 * Remove a selection listener
262 * The listener to remove
264 public void removeSelectionListener(SelectionListener listener
) {
265 if (null != fSelectionListeners
) {
266 fSelectionListeners
.remove(listener
);
271 * Selection changed callback
273 public void fireSelectionChanged() {
274 if (null != fSelectionListeners
) {
275 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
276 while (it
.hasNext()) {
277 SelectionListener listener
= it
.next();
278 listener
.widgetSelected(null);
284 * Default selection callback
286 public void fireDefaultSelection() {
287 if (null != fSelectionListeners
) {
288 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
289 while (it
.hasNext()) {
290 SelectionListener listener
= it
.next();
291 listener
.widgetDefaultSelected(null);
297 * Get the traces in the model
299 * @return The array of traces
301 public ITimeGraphEntry
[] getTraces() {
302 return fItemData
.getTraces();
306 * Get the on/off trace filters
308 * @return The array of filters
310 public boolean[] getTraceFilter() {
311 return fItemData
.getTraceFilter();
315 * Refresh the data for the thing
317 public void refreshData() {
318 fItemData
.refreshData();
324 * Refresh data for the given traces
327 * The traces to refresh
329 public void refreshData(ITimeGraphEntry
[] traces
) {
330 fItemData
.refreshData(traces
);
336 * Adjust the scoll bars
338 public void adjustScrolls() {
339 if (null == fTimeProvider
) {
340 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
346 long time0
= fTimeProvider
.getTime0();
347 long time1
= fTimeProvider
.getTime1();
349 long timeMin
= fTimeProvider
.getMinTime();
350 long timeMax
= fTimeProvider
.getMaxTime();
352 long delta
= timeMax
- timeMin
;
355 int thumb
= H_SCROLLBAR_MAX
;
358 // Thumb size (page size)
359 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
360 // At the beginning of visible window
361 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
364 // position, minimum, maximum, thumb size, increment (half page)t, page
365 // increment size (full page)
366 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
369 boolean ensureVisibleItem(int idx
, boolean redraw
) {
370 boolean changed
= false;
373 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
374 if (fItemData
.fExpandedItems
[index
].fSelected
) {
379 if (index
>= fItemData
.fExpandedItems
.length
) {
382 if (index
< fTopIndex
) {
389 int page
= countPerPage();
390 if (index
>= fTopIndex
+ page
) {
391 setTopIndex(index
- page
+ 1);
402 * Assign the given index as the top one
407 public void setTopIndex(int idx
) {
408 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
409 index
= Math
.max(0, index
);
415 * Set the expanded state of a given entry
420 * True if expanded, false if collapsed
422 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
423 Item item
= fItemData
.findItem(entry
);
424 if (item
!= null && item
.fExpanded
!= expanded
) {
425 item
.fExpanded
= expanded
;
426 fItemData
.updateExpandedItems();
432 * Collapses all nodes of the viewer's tree, starting with the root.
436 public void collapseAll() {
437 for (Item item
: fItemData
.fItems
) {
438 item
.fExpanded
= false;
440 fItemData
.updateExpandedItems();
445 * Expands all nodes of the viewer's tree, starting with the root.
449 public void expandAll() {
450 for (Item item
: fItemData
.fItems
) {
451 item
.fExpanded
= true;
453 fItemData
.updateExpandedItems();
458 * Add a tree listener
461 * The listener to add
463 public void addTreeListener(ITimeGraphTreeListener listener
) {
464 if (!fTreeListeners
.contains(listener
)) {
465 fTreeListeners
.add(listener
);
470 * Remove a tree listener
473 * The listener to remove
475 public void removeTreeListener(ITimeGraphTreeListener listener
) {
476 if (fTreeListeners
.contains(listener
)) {
477 fTreeListeners
.remove(listener
);
482 * Tree event callback
487 * The expanded state (true for expanded, false for collapsed)
489 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
490 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
491 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
493 listener
.treeExpanded(event
);
495 listener
.treeCollapsed(event
);
501 * Add a menu listener on {@link ITimeGraphEntry}s
503 * The listener to add
506 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
507 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
508 fTimeGraphEntryMenuListeners
.add(listener
);
513 * Remove a menu listener on {@link ITimeGraphEntry}s
516 * The listener to remove
519 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
520 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
521 fTimeGraphEntryMenuListeners
.remove(listener
);
526 * Menu event callback on {@link ITimeGraphEntry}s
529 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
531 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
532 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
533 listener
.menuDetected(event
);
538 * Add a menu listener on {@link ITimeEvent}s
541 * The listener to add
544 public void addTimeEventMenuListener(MenuDetectListener listener
) {
545 if (!fTimeEventMenuListeners
.contains(listener
)) {
546 fTimeEventMenuListeners
.add(listener
);
551 * Remove a menu listener on {@link ITimeEvent}s
554 * The listener to remove
557 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
558 if (fTimeEventMenuListeners
.contains(listener
)) {
559 fTimeEventMenuListeners
.remove(listener
);
564 * Menu event callback on {@link ITimeEvent}s
567 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
569 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
570 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
571 listener
.menuDetected(event
);
576 public ISelection
getSelection() {
577 TimeGraphSelection sel
= new TimeGraphSelection();
578 ITimeGraphEntry trace
= getSelectedTrace();
579 if (null != trace
&& null != fTimeProvider
) {
580 long selectedTime
= fTimeProvider
.getSelectedTime();
581 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
592 * Get the selection object
594 * @return The selection
596 public ISelection
getSelectionTrace() {
597 TimeGraphSelection sel
= new TimeGraphSelection();
598 ITimeGraphEntry trace
= getSelectedTrace();
606 * Enable/disable one of the traces in the model
609 * 1 to enable it, -1 to disable. The method returns immediately
610 * if another value is used.
612 public void selectTrace(int n
) {
613 if ((n
!= 1) && (n
!= -1)) {
617 boolean changed
= false;
618 int lastSelection
= -1;
619 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
620 Item item
= fItemData
.fExpandedItems
[i
];
621 if (item
.fSelected
) {
623 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
624 item
.fSelected
= false;
625 item
= fItemData
.fExpandedItems
[i
+ 1];
626 item
.fSelected
= true;
628 } else if ((-1 == n
) && (i
> 0)) {
629 item
.fSelected
= false;
630 item
= fItemData
.fExpandedItems
[i
- 1];
631 item
.fSelected
= true;
638 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
639 Item item
= fItemData
.fExpandedItems
[0];
640 item
.fSelected
= true;
645 ensureVisibleItem(-1, false);
647 fireSelectionChanged();
655 * 1 for next event, -1 for previous event
657 public void selectEvent(int n
) {
658 if (null == fTimeProvider
) {
661 ITimeGraphEntry trace
= getSelectedTrace();
665 long selectedTime
= fTimeProvider
.getSelectedTime();
666 long endTime
= fTimeProvider
.getEndTime();
667 ITimeEvent nextEvent
;
668 if (-1 == n
&& selectedTime
> endTime
) {
669 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
671 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
673 if (null == nextEvent
&& -1 == n
) {
674 nextEvent
= Utils
.getFirstEvent(trace
);
676 if (null != nextEvent
) {
677 long nextTime
= nextEvent
.getTime();
678 // If last event detected e.g. going back or not moving to a next
680 if (nextTime
<= selectedTime
&& n
== 1) {
681 // Select to the end of this last event
682 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
683 // but not beyond the end of the trace
684 if (nextTime
> endTime
) {
687 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
688 // for previous event go to its end time unless we were already there
689 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
691 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
692 fireSelectionChanged();
694 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
695 fireSelectionChanged();
700 * Select the next event
702 public void selectNextEvent() {
704 // Notify if visible time window has been adjusted
705 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
709 * Select the previous event
711 public void selectPrevEvent() {
713 // Notify if visible time window has been adjusted
714 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
718 * Select the next trace
720 public void selectNextTrace() {
725 * Select the previous trace
727 public void selectPrevTrace() {
732 * Zoom based on mouse cursor location with mouse scrolling
734 * @param zoomIn true to zoom in, false to zoom out
736 public void zoom(boolean zoomIn
) {
737 int globalX
= getDisplay().getCursorLocation().x
;
738 Point p
= toControl(globalX
, 0);
739 int nameSpace
= fTimeProvider
.getNameSpace();
740 int timeSpace
= fTimeProvider
.getTimeSpace();
741 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
742 long time0
= fTimeProvider
.getTime0();
743 long time1
= fTimeProvider
.getTime1();
744 long interval
= time1
- time0
;
747 } // to allow getting out of single point interval
750 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
752 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
754 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
755 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
756 long newTime1
= newTime0
+ newInterval
;
757 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
758 synchronized (fMouseScrollNotifierLock
) {
759 if (fMouseScrollNotifier
== null) {
760 fMouseScrollNotifier
= new MouseScrollNotifier();
761 fMouseScrollNotifier
.start();
763 fMouseScrollNotifier
.mouseScrolled();
768 * zoom in using single click
770 public void zoomIn() {
771 long prevTime0
= fTimeProvider
.getTime0();
772 long prevTime1
= fTimeProvider
.getTime1();
773 long prevRange
= prevTime1
- prevTime0
;
774 if (prevRange
== 0) {
777 long selTime
= fTimeProvider
.getSelectedTime();
778 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
779 selTime
= (prevTime0
+ prevTime1
) / 2;
781 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
782 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
784 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
786 if (inaccuracy
> 0 && inaccuracy
< 100) {
787 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
791 long min
= fTimeProvider
.getMinTimeInterval();
792 if ((time1
- time0
) < min
) {
793 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
797 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
801 * zoom out using single click
803 public void zoomOut() {
804 long prevTime0
= fTimeProvider
.getTime0();
805 long prevTime1
= fTimeProvider
.getTime1();
806 long selTime
= fTimeProvider
.getSelectedTime();
807 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
808 selTime
= (prevTime0
+ prevTime1
) / 2;
810 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
811 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
813 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
814 if (inaccuracy
> 0 && inaccuracy
< 100) {
815 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
819 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
823 * Return the currently selected trace
825 * @return The entry matching the trace
827 public ITimeGraphEntry
getSelectedTrace() {
828 ITimeGraphEntry trace
= null;
829 int idx
= getSelectedIndex();
831 trace
= fItemData
.fExpandedItems
[idx
].fTrace
;
837 * Retrieve the index of the currently selected item
841 public int getSelectedIndex() {
843 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
844 Item item
= fItemData
.fExpandedItems
[i
];
845 if (item
.fSelected
) {
853 boolean toggle(int idx
) {
854 boolean toggled
= false;
855 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
856 Item item
= fItemData
.fExpandedItems
[idx
];
857 if (item
.fHasChildren
) {
858 item
.fExpanded
= !item
.fExpanded
;
859 fItemData
.updateExpandedItems();
863 fireTreeEvent(item
.fTrace
, item
.fExpanded
);
869 int getItemIndexAtY(int y
) {
873 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
875 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
876 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
883 int idx
= y
/ fGlobalItemHeight
;
885 if (idx
< fItemData
.fExpandedItems
.length
) {
891 boolean isOverSplitLine(int x
) {
892 if (x
< 0 || null == fTimeProvider
) {
896 int nameWidth
= fTimeProvider
.getNameSpace();
897 if (x
> nameWidth
- w
&& x
< nameWidth
+ w
) {
903 ITimeGraphEntry
getEntry(Point pt
) {
904 int idx
= getItemIndexAtY(pt
.y
);
905 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fTrace
: null;
909 * Return the x coordinate corresponding to a time
911 * @param time the time
912 * @return the x coordinate corresponding to the time
916 public int getXForTime(long time
) {
917 if (null == fTimeProvider
) {
920 long time0
= fTimeProvider
.getTime0();
921 long time1
= fTimeProvider
.getTime1();
922 int width
= getCtrlSize().x
;
923 int nameSpace
= fTimeProvider
.getNameSpace();
924 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
925 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
930 * Return the time corresponding to an x coordinate
932 * @param coord The X coordinate
933 * @return The time corresponding to the x coordinate
937 public long getTimeAtX(int coord
) {
938 if (null == fTimeProvider
) {
942 Point size
= getCtrlSize();
943 long time0
= fTimeProvider
.getTime0();
944 long time1
= fTimeProvider
.getTime1();
945 int nameWidth
= fTimeProvider
.getNameSpace();
946 final int x
= coord
- nameWidth
;
947 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
948 if (x
>= 0 && size
.x
>= nameWidth
) {
949 if (time1
- time0
> timeWidth
) {
950 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
951 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
953 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
954 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
960 void selectItem(int idx
, boolean addSelection
) {
961 boolean changed
= false;
963 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
964 Item item
= fItemData
.fExpandedItems
[idx
];
965 changed
= !item
.fSelected
;
966 item
.fSelected
= true;
969 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
970 Item item
= fItemData
.fExpandedItems
[i
];
971 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
974 item
.fSelected
= i
== idx
;
977 changed
|= ensureVisibleItem(idx
, true);
984 * Callback for item selection
987 * The entry matching the trace
988 * @param addSelection
989 * If the selection is added or removed
991 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
992 int idx
= fItemData
.findItemIndex(trace
);
993 selectItem(idx
, addSelection
);
997 * Retrieve the number of entries shown per page.
1001 public int countPerPage() {
1002 int height
= getCtrlSize().y
;
1004 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1006 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1007 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1008 if (ySum
>= height
) {
1013 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1014 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1015 if (ySum
>= height
) {
1023 count
= height
/ fGlobalItemHeight
;
1029 * Get the index of the top element
1033 public int getTopIndex() {
1038 * Get the number of expanded items
1040 * @return The count of expanded items
1042 public int getExpandedElementCount() {
1043 return fItemData
.fExpandedItems
.length
;
1047 * Get an array of all expanded elements
1049 * @return The expanded elements
1051 public ITimeGraphEntry
[] getExpandedElements() {
1052 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1053 for (Item item
: fItemData
.fExpandedItems
) {
1054 elements
.add(item
.fTrace
);
1056 return elements
.toArray(new ITimeGraphEntry
[0]);
1059 Point
getCtrlSize() {
1060 Point size
= getSize();
1061 if (getHorizontalBar().isVisible()) {
1062 size
.y
-= getHorizontalBar().getSize().y
;
1067 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1069 int y
= bound
.y
+ (idx
- fTopIndex
) * fGlobalItemHeight
;
1070 int width
= nameWidth
;
1071 int height
= fGlobalItemHeight
;
1072 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1074 for (int i
= fTopIndex
; i
< idx
; i
++) {
1075 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1078 height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1080 return new Rectangle(x
, y
, width
, height
);
1083 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1084 int x
= bound
.x
+ nameWidth
;
1085 int y
= bound
.y
+ (idx
- fTopIndex
) * fGlobalItemHeight
;
1086 int width
= bound
.width
- x
;
1087 int height
= fGlobalItemHeight
;
1088 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1090 for (int i
= fTopIndex
; i
< idx
; i
++) {
1091 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1094 height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1096 return new Rectangle(x
, y
, width
, height
);
1100 void paint(Rectangle bounds
, PaintEvent e
) {
1102 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1103 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1105 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1109 fIdealNameSpace
= 0;
1110 int nameSpace
= fTimeProvider
.getNameSpace();
1112 // draw empty name space background
1113 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1114 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1116 if (fDragState
== DRAG_ZOOM
) {
1117 // draw selected zoom region background
1118 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1119 if (fDragX0
< fDragX
) {
1120 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1121 } else if (fDragX0
> fDragX
) {
1122 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1126 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1128 // draw selected time
1129 long time0
= fTimeProvider
.getTime0();
1130 long time1
= fTimeProvider
.getTime1();
1131 long selectedTime
= fTimeProvider
.getSelectedTime();
1132 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1133 int x
= bounds
.x
+ nameSpace
+ (int) ((selectedTime
- time0
) * pixelsPerNanoSec
);
1134 if (x
>= nameSpace
&& x
< bounds
.x
+ bounds
.width
) {
1135 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1136 gc
.drawLine(x
, bounds
.y
, x
, bounds
.y
+ bounds
.height
);
1139 // draw drag line, no line if name space is 0.
1140 if (DRAG_SPLIT_LINE
== fDragState
) {
1141 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1142 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1143 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
&& fDragX
!= fDragX0
) {
1144 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1145 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1146 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1147 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1148 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1149 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1154 * Draw many items at once
1157 * The rectangle of the area
1158 * @param timeProvider
1161 * The array items to draw
1163 * The index of the first element to draw
1165 * The width reserved for the names
1167 * Reference to the SWT GC object
1169 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1170 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1171 for (int i
= topIndex
; i
< items
.length
; i
++) {
1172 Item item
= items
[i
];
1173 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1175 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1181 * @param item the item to draw
1182 * @param bounds the container rectangle
1183 * @param timeProvider Time provider
1184 * @param i the item index
1185 * @param nameSpace the name space
1186 * @param gc Graphics context
1188 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1189 ITimeGraphEntry entry
= item
.fTrace
;
1190 long time0
= timeProvider
.getTime0();
1191 long time1
= timeProvider
.getTime1();
1192 long selectedTime
= timeProvider
.getSelectedTime();
1194 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1195 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1199 if (! item
.fTrace
.hasTimeEvents()) {
1200 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1201 nameRect
.width
+= statesRect
.width
;
1202 drawName(item
, nameRect
, gc
);
1204 drawName(item
, nameRect
, gc
);
1206 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1207 if (rect
.isEmpty()) {
1208 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1211 if (time1
<= time0
) {
1212 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1213 gc
.fillRectangle(rect
);
1214 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1218 // Initialize _rect1 to same values as enclosing rectangle rect
1219 Rectangle stateRect
= Utils
.clone(rect
);
1220 boolean selected
= item
.fSelected
;
1221 // K pixels per second
1222 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1224 if (item
.fTrace
.hasTimeEvents()) {
1225 fillSpace(rect
, gc
, selected
);
1226 // Drawing rectangle is smaller than reserved space
1228 stateRect
.height
-= 6;
1230 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1231 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1234 while (iterator
.hasNext()) {
1235 ITimeEvent event
= iterator
.next();
1236 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1237 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1238 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1239 // event is out of bounds
1242 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1243 stateRect
.x
= Math
.max(rect
.x
, x
);
1244 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1245 if (stateRect
.x
== lastX
) {
1246 stateRect
.width
-= 1;
1247 if (stateRect
.width
> 0) {
1248 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1249 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1253 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1254 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1259 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1263 * Draw the name of an item.
1268 * Where to draw the name
1272 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1273 boolean hasTimeEvents
= item
.fTrace
.hasTimeEvents();
1274 if (! hasTimeEvents
) {
1275 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1276 gc
.fillRectangle(bounds
);
1277 if (item
.fSelected
&& fIsInFocus
) {
1278 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1279 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1282 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1283 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1284 gc
.fillRectangle(bounds
);
1287 // No name to be drawn
1288 if (fTimeProvider
.getNameSpace() == 0) {
1292 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1293 if (item
.fHasChildren
) {
1294 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1295 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1296 Rectangle rect
= Utils
.clone(bounds
);
1297 rect
.x
+= leftMargin
;
1298 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1299 rect
.width
= EXPAND_SIZE
;
1300 rect
.height
= EXPAND_SIZE
;
1301 gc
.fillRectangle(rect
);
1302 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1303 int midy
= rect
.y
+ rect
.height
/ 2;
1304 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1305 if (!item
.fExpanded
) {
1306 int midx
= rect
.x
+ rect
.width
/ 2;
1307 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1310 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1312 Image img
= fTimeGraphProvider
.getItemImage(item
.fTrace
);
1315 int imgHeight
= img
.getImageData().height
;
1316 int imgWidth
= img
.getImageData().width
;
1318 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1319 gc
.drawImage(img
, x
, y
);
1320 leftMargin
+= imgWidth
+ MARGIN
;
1322 String name
= item
.fName
;
1323 Point size
= gc
.stringExtent(name
);
1324 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1325 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1327 if (hasTimeEvents
) {
1328 // cut long string with "..."
1329 int width
= bounds
.width
- leftMargin
;
1331 while (size
.x
> width
&& name
.length() > 1) {
1333 name
= name
.substring(0, name
.length() - 1);
1334 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1337 name
+= "..."; //$NON-NLS-1$
1340 Rectangle rect
= Utils
.clone(bounds
);
1341 rect
.x
+= leftMargin
;
1342 rect
.width
-= leftMargin
;
1344 if (rect
.width
> 0) {
1345 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1346 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1347 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1348 leftMargin
+= textWidth
+ MARGIN
;
1351 if (hasTimeEvents
) {
1353 int x
= bounds
.x
+ leftMargin
;
1354 int width
= bounds
.width
- x
;
1355 int midy
= bounds
.y
+ bounds
.height
/ 2;
1356 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1357 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1363 * Draw the state (color fill)
1368 * Time event for which we're drawing the state
1374 * Is this time event currently selected (so it appears
1376 * @param timeSelected
1377 * Is the timestamp currently selected
1378 * @return true if the state was drawn
1381 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1382 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1384 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1385 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1388 boolean visible
= rect
.width
== 0 ?
false : true;
1391 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1392 // Only draw the top and bottom borders
1393 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1394 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1395 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1396 if (rect
.width
== 1) {
1397 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1398 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1402 Color stateColor
= null;
1403 if (colorIdx
< fEventColorMap
.length
) {
1404 stateColor
= fEventColorMap
[colorIdx
];
1406 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1409 boolean reallySelected
= timeSelected
&& selected
;
1410 // fill all rect area
1411 gc
.setBackground(stateColor
);
1412 gc
.fillRectangle(rect
);
1413 // get the border color?
1414 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1417 if (!reallySelected
) {
1418 // Draw the top and bottom borders i.e. no side borders
1419 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1420 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1423 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1424 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1426 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1431 * Fill the space between two contiguous time events
1438 * Is this time event selected or not
1440 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1441 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1442 gc
.fillRectangle(rect
);
1443 if (fDragState
== DRAG_ZOOM
) {
1444 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1445 if (fDragX0
< fDragX
) {
1446 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1447 } else if (fDragX0
> fDragX
) {
1448 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1452 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1453 int midy
= rect
.y
+ rect
.height
/ 2;
1454 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1458 public void keyTraversed(TraverseEvent e
) {
1459 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1465 public void keyPressed(KeyEvent e
) {
1467 if (fItemData
.fExpandedItems
.length
== 0) {
1470 if (SWT
.HOME
== e
.keyCode
) {
1472 } else if (SWT
.END
== e
.keyCode
) {
1473 idx
= fItemData
.fExpandedItems
.length
- 1;
1474 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1475 idx
= getSelectedIndex();
1478 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1481 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1482 idx
= getSelectedIndex();
1485 } else if (idx
> 0) {
1488 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1490 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1492 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1493 int page
= countPerPage();
1494 idx
= getSelectedIndex();
1499 if (idx
>= fItemData
.fExpandedItems
.length
) {
1500 idx
= fItemData
.fExpandedItems
.length
- 1;
1502 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1503 int page
= countPerPage();
1504 idx
= getSelectedIndex();
1512 } else if (SWT
.CR
== e
.keyCode
) {
1513 idx
= getSelectedIndex();
1515 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1518 fireDefaultSelection();
1524 selectItem(idx
, false);
1525 fireSelectionChanged();
1530 public void keyReleased(KeyEvent e
) {
1534 public void focusGained(FocusEvent e
) {
1536 if (fMouseScrollFilterListener
== null) {
1537 fMouseScrollFilterListener
= new Listener() {
1538 // This filter is used to prevent horizontal scrolling of the view
1539 // when the mouse wheel is used to zoom
1541 public void handleEvent(Event event
) {
1545 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1551 public void focusLost(FocusEvent e
) {
1553 if (fMouseScrollFilterListener
!= null) {
1554 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1555 fMouseScrollFilterListener
= null;
1557 if (DRAG_NONE
!= fDragState
) {
1559 fDragState
= DRAG_NONE
;
1565 * @return If the current view is focused
1567 public boolean isInFocus() {
1572 * Provide the possibility to control the wait cursor externally e.g. data
1573 * requests in progress
1575 * @param waitInd Should we wait indefinitely?
1577 public void waitCursor(boolean waitInd
) {
1578 // Update cursor as indicated
1580 setCursor(fWaitCursor
);
1581 fIsWaitCursor
= true;
1584 fIsWaitCursor
= false;
1587 // Get ready for next mouse move
1588 fIsDragCursor
= false;
1593 * If the x, y position is over the vertical split line (name to time
1594 * ranges), then change the cursor to a drag cursor to indicate the user the
1595 * possibility of resizing
1601 void updateCursor(int x
, int y
) {
1602 // if Wait cursor not active, check for the need to change to a drag
1604 if (!fIsWaitCursor
) {
1605 boolean isSplitLine
= isOverSplitLine(x
);
1606 // No dragcursor is name space is fixed to zero
1607 if (isSplitLine
&& !fIsDragCursor
&& fTimeProvider
.getNameSpace() > 0) {
1608 setCursor(fDragCursor
);
1609 fIsDragCursor
= true;
1610 } else if (!isSplitLine
&& fIsDragCursor
) {
1612 fIsDragCursor
= false;
1618 public void mouseMove(MouseEvent e
) {
1619 if (null == fTimeProvider
) {
1622 Point size
= getCtrlSize();
1623 if (DRAG_TRACE_ITEM
== fDragState
) {
1624 int nameWidth
= fTimeProvider
.getNameSpace();
1625 int x
= e
.x
- nameWidth
;
1626 if (x
> 0 && size
.x
> nameWidth
&& fDragX
!= x
) {
1628 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1629 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1630 long time1
= fTime1bak
- timeDelta
;
1631 long maxTime
= fTimeProvider
.getMaxTime();
1632 if (time1
> maxTime
) {
1635 long time0
= time1
- (fTime1bak
- fTime0bak
);
1636 if (time0
< fTimeProvider
.getMinTime()) {
1637 time0
= fTimeProvider
.getMinTime();
1638 time1
= time0
+ (fTime1bak
- fTime0bak
);
1640 fTimeProvider
.setStartFinishTime(time0
, time1
);
1642 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1644 fTimeProvider
.setNameSpace(e
.x
);
1645 } else if (DRAG_ZOOM
== fDragState
) {
1646 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1648 } else if (DRAG_NONE
== fDragState
) {
1649 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1650 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1653 fMouseOverSplitLine
= mouseOverSplitLine
;
1655 updateCursor(e
.x
, e
.y
);
1659 public void mouseDoubleClick(MouseEvent e
) {
1660 if (null == fTimeProvider
) {
1663 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1664 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
1665 fTimeProvider
.setNameSpace(fIdealNameSpace
);
1666 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1667 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1670 fMouseOverSplitLine
= mouseOverSplitLine
;
1673 int idx
= getItemIndexAtY(e
.y
);
1675 selectItem(idx
, false);
1676 fireDefaultSelection();
1682 public void mouseDown(MouseEvent e
) {
1683 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
) {
1687 if (1 == e
.button
) {
1688 int nameSpace
= fTimeProvider
.getNameSpace();
1689 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
1690 fDragState
= DRAG_SPLIT_LINE
;
1693 fTime0bak
= fTimeProvider
.getTime0();
1694 fTime1bak
= fTimeProvider
.getTime1();
1699 idx
= getItemIndexAtY(e
.y
);
1701 Item item
= fItemData
.fExpandedItems
[idx
];
1702 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
1705 long hitTime
= getTimeAtX(e
.x
);
1708 fDragState
= DRAG_TRACE_ITEM
;
1709 fDragX
= e
.x
- nameSpace
;
1711 fTime0bak
= fTimeProvider
.getTime0();
1712 fTime1bak
= fTimeProvider
.getTime1();
1715 selectItem(idx
, false);
1716 fireSelectionChanged();
1718 selectItem(idx
, false); // clear selection
1720 fireSelectionChanged();
1722 } else if (3 == e
.button
) {
1723 if (fTimeProvider
.getTime0() == fTimeProvider
.getTime1() || getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
1727 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1729 fDragState
= DRAG_ZOOM
;
1734 public void mouseUp(MouseEvent e
) {
1735 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
1736 menuDetected(fPendingMenuDetectEvent
);
1738 if (DRAG_NONE
!= fDragState
) {
1740 if (e
.button
== 1 && DRAG_TRACE_ITEM
== fDragState
) {
1741 if (fDragX
== fDragX0
) { // click without drag
1742 long time
= getTimeAtX(e
.x
);
1743 fTimeProvider
.setSelectedTimeNotify(time
, false);
1745 // Notify time provider to check the need for listener
1747 fTimeProvider
.notifyStartFinishTime();
1749 fDragState
= DRAG_NONE
;
1750 } else if (e
.button
== 1 && DRAG_SPLIT_LINE
== fDragState
) {
1752 fDragState
= DRAG_NONE
;
1753 } else if (e
.button
== 3 && DRAG_ZOOM
== fDragState
) {
1754 int nameWidth
= fTimeProvider
.getNameSpace();
1755 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
1756 long time0
= getTimeAtX(fDragX0
);
1757 long time1
= getTimeAtX(fDragX
);
1758 if (time0
< time1
) {
1759 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1761 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
1766 fDragState
= DRAG_NONE
;
1772 public void mouseEnter(MouseEvent e
) {
1776 public void mouseExit(MouseEvent e
) {
1777 if (fMouseOverSplitLine
) {
1778 fMouseOverSplitLine
= false;
1784 public void mouseHover(MouseEvent e
) {
1788 public void mouseScrolled(MouseEvent e
) {
1789 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
1792 boolean zoomScroll
= false;
1793 Point p
= getParent().toControl(getDisplay().getCursorLocation());
1794 Point parentSize
= getParent().getSize();
1795 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
1796 // over the parent control
1797 if (e
.x
> getCtrlSize().x
) {
1798 // over the horizontal scroll bar
1800 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
1801 // over the name space
1807 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
1810 } else if (e
.count
< 0) {
1814 setTopIndex(getTopIndex() - e
.count
);
1819 public void controlMoved(ControlEvent e
) {
1823 public void controlResized(ControlEvent e
) {
1828 public void widgetDefaultSelected(SelectionEvent e
) {
1832 public void widgetSelected(SelectionEvent e
) {
1833 if (e
.widget
== getVerticalBar()) {
1834 setTopIndex(getVerticalBar().getSelection());
1835 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
1836 int start
= getHorizontalBar().getSelection();
1837 long time0
= fTimeProvider
.getTime0();
1838 long time1
= fTimeProvider
.getTime1();
1839 long timeMin
= fTimeProvider
.getMinTime();
1840 long timeMax
= fTimeProvider
.getMaxTime();
1841 long delta
= timeMax
- timeMin
;
1843 long range
= time1
- time0
;
1844 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
1845 time1
= time0
+ range
;
1847 // TODO: Follow-up with Bug 310310
1848 // In Linux SWT.DRAG is the only value received
1849 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
1850 if (e
.detail
== SWT
.DRAG
) {
1851 fTimeProvider
.setStartFinishTime(time0
, time1
);
1853 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1859 public int getBorderWidth() {
1860 return fBorderWidth
;
1864 * Set the border width
1866 * @param borderWidth
1869 public void setBorderWidth(int borderWidth
) {
1870 this.fBorderWidth
= borderWidth
;
1874 * @return The current height of the header row
1876 public int getHeaderHeight() {
1877 return fHeaderHeight
;
1881 * Set the height of the header row
1883 * @param headerHeight
1886 public void setHeaderHeight(int headerHeight
) {
1887 this.fHeaderHeight
= headerHeight
;
1891 * @return The height of regular item rows
1893 public int getItemHeight() {
1894 return fGlobalItemHeight
;
1898 * Set the height of regular itew rows
1903 public void setItemHeight(int rowHeight
) {
1904 this.fGlobalItemHeight
= rowHeight
;
1908 * Set the minimum item width
1910 * @param width The minimum width
1912 public void setMinimumItemWidth(int width
) {
1913 this.fMinimumItemWidth
= width
;
1917 * @return The minimum item width
1919 public int getMinimumItemWidth() {
1920 return fMinimumItemWidth
;
1924 * @return The entries that are currently filtered out
1928 public List
<ITimeGraphEntry
> getFilteredOut() {
1929 return fItemData
.getFilteredOut();
1933 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
1934 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
1935 fSelectionChangedListeners
.add(listener
);
1940 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
1941 if (listener
!= null) {
1942 fSelectionChangedListeners
.remove(listener
);
1947 public void setSelection(ISelection selection
) {
1948 if (selection
instanceof TimeGraphSelection
) {
1949 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
1950 Object ob
= sel
.getFirstElement();
1951 if (ob
instanceof ITimeGraphEntry
) {
1952 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
1953 selectItem(trace
, false);
1960 * @param filter The filter object to be attached to the view
1963 public void addFilter(ViewerFilter filter
) {
1964 if (!fFilters
.contains(filter
)) {
1965 fFilters
.add(filter
);
1970 * @param filter The filter object to be attached to the view
1973 public void removeFilter(ViewerFilter filter
) {
1974 fFilters
.remove(filter
);
1977 private class ItemData
{
1978 private Item
[] fExpandedItems
= new Item
[0];
1979 private Item
[] fItems
= new Item
[0];
1980 private ITimeGraphEntry fTraces
[] = new ITimeGraphEntry
[0];
1981 private boolean fTraceFilter
[] = new boolean[0];
1982 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<ITimeGraphEntry
>();
1987 Item
findItem(ITimeGraphEntry entry
) {
1988 if (entry
== null) {
1992 for (int i
= 0; i
< fItems
.length
; i
++) {
1993 Item item
= fItems
[i
];
1994 if (item
.fTrace
== entry
) {
2002 int findItemIndex(ITimeGraphEntry trace
) {
2003 if (trace
== null) {
2007 for (int i
= 0; i
< fExpandedItems
.length
; i
++) {
2008 Item item
= fExpandedItems
[i
];
2009 if (item
.fTrace
== trace
) {
2017 public void refreshData() {
2018 List
<Item
> itemList
= new ArrayList
<Item
>();
2019 fFilteredOut
.clear();
2020 ITimeGraphEntry selection
= getSelectedTrace();
2021 for (int i
= 0; i
< fTraces
.length
; i
++) {
2022 ITimeGraphEntry entry
= fTraces
[i
];
2023 refreshData(itemList
, null, 0, entry
);
2025 fItems
= itemList
.toArray(new Item
[0]);
2026 updateExpandedItems();
2027 if (selection
!= null) {
2028 for (Item item
: fExpandedItems
) {
2029 if (item
.fTrace
== selection
) {
2030 item
.fSelected
= true;
2037 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2038 Item item
= new Item(entry
, entry
.getName(), level
);
2039 if (parent
!= null) {
2040 parent
.fChildren
.add(item
);
2042 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2044 if (entry
.hasChildren()) {
2045 item
.fExpanded
= true;
2046 item
.fHasChildren
= true;
2047 for (ITimeGraphEntry child
: entry
.getChildren()) {
2048 refreshData(itemList
, item
, level
+ 1, child
);
2053 public void updateExpandedItems() {
2054 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2055 for (int i
= 0; i
< fTraces
.length
; i
++) {
2056 ITimeGraphEntry entry
= fTraces
[i
];
2057 Item item
= findItem(entry
);
2058 refreshExpanded(expandedItemList
, item
);
2060 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2063 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2064 // Check for filters
2065 boolean display
= true;
2066 for (ViewerFilter filter
: fFilters
) {
2067 if (!filter
.select(null, item
.fTrace
.getParent(), item
.fTrace
)) {
2073 expandedItemList
.add(item
);
2074 if (item
.fHasChildren
&& item
.fExpanded
) {
2075 for (Item child
: item
.fChildren
) {
2076 refreshExpanded(expandedItemList
, child
);
2082 public void refreshData(ITimeGraphEntry traces
[]) {
2083 if (traces
== null) {
2084 fTraceFilter
= null;
2087 if (traces
.length
== 0) {
2088 fTraceFilter
= null;
2089 } else if (fTraceFilter
== null || traces
.length
!= fTraceFilter
.length
) {
2090 fTraceFilter
= new boolean[traces
.length
];
2091 java
.util
.Arrays
.fill(fTraceFilter
, true);
2093 fTraces
= Arrays
.copyOf(traces
, traces
.length
);
2099 public ITimeGraphEntry
[] getTraces() {
2103 public boolean[] getTraceFilter() {
2104 return fTraceFilter
;
2107 public List
<ITimeGraphEntry
> getFilteredOut() {
2108 return fFilteredOut
;
2112 private class Item
{
2113 private boolean fExpanded
;
2114 private boolean fSelected
;
2115 private boolean fHasChildren
;
2116 private int fItemHeight
;
2118 private List
<Item
> fChildren
;
2119 private String fName
;
2120 private ITimeGraphEntry fTrace
;
2122 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2123 this.fTrace
= trace
;
2125 this.fLevel
= level
;
2126 this.fChildren
= new ArrayList
<Item
>();
2130 public String
toString() {
2139 public void menuDetected(MenuDetectEvent e
) {
2140 if (null == fTimeProvider
) {
2143 if (e
.detail
== SWT
.MENU_MOUSE
) {
2144 if (fPendingMenuDetectEvent
== null) {
2145 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2146 * Store the event and trigger it later just before handling mouseUp.
2147 * This allows for the method to detect if mouse is used to drag zoom.
2149 fPendingMenuDetectEvent
= e
;
2152 fPendingMenuDetectEvent
= null;
2153 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2157 if (fDragState
!= DRAG_NONE
) {
2161 Point p
= toControl(e
.x
, e
.y
);
2162 int idx
= getItemIndexAtY(p
.y
);
2163 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2164 Item item
= fItemData
.fExpandedItems
[idx
];
2165 ITimeGraphEntry entry
= item
.fTrace
;
2166 if (entry
.hasTimeEvents()) {
2167 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2168 if (event
!= null) {
2170 fireMenuEventOnTimeEvent(e
);
2175 fireMenuEventOnTimeGraphEntry(e
);