1 /*****************************************************************************
2 * Copyright (c) 2007, 2013 Intel Corporation and others
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Intel Corporation - Initial API and implementation
11 * Ruslan A. Scherbakov, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon, Ericsson - Updated for TMF
13 * Patrick Tasse, Ericsson - Refactoring
14 * Geneviève Bastien, École Polytechnique de Montréal - Move code to
15 * provide base classes for time graph view
16 *****************************************************************************/
18 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
25 import org
.eclipse
.jface
.action
.IStatusLineManager
;
26 import org
.eclipse
.jface
.resource
.JFaceResources
;
27 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
28 import org
.eclipse
.jface
.viewers
.ISelection
;
29 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
30 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
31 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
42 import org
.eclipse
.swt
.SWT
;
43 import org
.eclipse
.swt
.events
.ControlEvent
;
44 import org
.eclipse
.swt
.events
.ControlListener
;
45 import org
.eclipse
.swt
.events
.FocusEvent
;
46 import org
.eclipse
.swt
.events
.FocusListener
;
47 import org
.eclipse
.swt
.events
.KeyEvent
;
48 import org
.eclipse
.swt
.events
.KeyListener
;
49 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
50 import org
.eclipse
.swt
.events
.MenuDetectListener
;
51 import org
.eclipse
.swt
.events
.MouseEvent
;
52 import org
.eclipse
.swt
.events
.MouseListener
;
53 import org
.eclipse
.swt
.events
.MouseMoveListener
;
54 import org
.eclipse
.swt
.events
.MouseTrackListener
;
55 import org
.eclipse
.swt
.events
.MouseWheelListener
;
56 import org
.eclipse
.swt
.events
.PaintEvent
;
57 import org
.eclipse
.swt
.events
.SelectionEvent
;
58 import org
.eclipse
.swt
.events
.SelectionListener
;
59 import org
.eclipse
.swt
.events
.TraverseEvent
;
60 import org
.eclipse
.swt
.events
.TraverseListener
;
61 import org
.eclipse
.swt
.events
.TypedEvent
;
62 import org
.eclipse
.swt
.graphics
.Color
;
63 import org
.eclipse
.swt
.graphics
.Cursor
;
64 import org
.eclipse
.swt
.graphics
.GC
;
65 import org
.eclipse
.swt
.graphics
.Image
;
66 import org
.eclipse
.swt
.graphics
.Point
;
67 import org
.eclipse
.swt
.graphics
.Rectangle
;
68 import org
.eclipse
.swt
.widgets
.Composite
;
69 import org
.eclipse
.swt
.widgets
.Display
;
70 import org
.eclipse
.swt
.widgets
.Event
;
71 import org
.eclipse
.swt
.widgets
.Listener
;
72 import org
.eclipse
.swt
.widgets
.ScrollBar
;
75 * Time graph control implementation
78 * @author Alvaro Sanchez-Leon
79 * @author Patrick Tasse
81 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
, MenuDetectListener
, ITmfTimeGraphDrawingHelper
{
84 /** Max scrollbar size */
85 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
87 private static final int DRAG_NONE
= 0;
88 private static final int DRAG_TRACE_ITEM
= 1;
89 private static final int DRAG_SPLIT_LINE
= 2;
90 private static final int DRAG_ZOOM
= 3;
91 private static final int DRAG_SELECTION
= 4;
93 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
95 private static final double ZOOM_FACTOR
= 1.5;
96 private static final double ZOOM_IN_FACTOR
= 0.8;
97 private static final double ZOOM_OUT_FACTOR
= 1.25;
99 private static final int SNAP_WIDTH
= 2;
101 private static final int NO_STATUS
= -1;
103 /** Resource manager */
104 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
106 /** Color map for event types */
107 private Color
[] fEventColorMap
= null;
109 private ITimeDataProvider fTimeProvider
;
110 private IStatusLineManager fStatusLineManager
= null;
111 private TimeGraphScale fTimeGraphScale
= null;
113 private boolean fIsInFocus
= false;
114 private boolean fMouseOverSplitLine
= false;
115 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
116 private int fMinimumItemWidth
= 0;
117 private int fTopIndex
= 0;
118 private int fDragState
= DRAG_NONE
;
119 private int fDragButton
;
120 private int fDragX0
= 0;
121 private int fDragX
= 0;
122 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
123 private int fIdealNameSpace
= 0;
124 private long fTime0bak
;
125 private long fTime1bak
;
126 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
127 private ItemData fItemData
= null;
128 private List
<SelectionListener
> fSelectionListeners
;
129 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
130 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
131 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
132 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
133 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
134 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
135 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
136 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
137 private final List
<ViewerFilter
> fFilters
= new ArrayList
<ViewerFilter
>();
138 private MenuDetectEvent fPendingMenuDetectEvent
= null;
140 private int fBorderWidth
= 0;
141 private int fHeaderHeight
= 0;
143 private Listener fMouseScrollFilterListener
;
145 private MouseScrollNotifier fMouseScrollNotifier
;
146 private final Object fMouseScrollNotifierLock
= new Object();
148 private class MouseScrollNotifier
extends Thread
{
149 private static final long DELAY
= 400L;
150 private static final long POLLING_INTERVAL
= 10L;
151 private long fLastScrollTime
= Long
.MAX_VALUE
;
155 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
157 Thread
.sleep(POLLING_INTERVAL
);
158 } catch (Exception e
) {
162 if (!isInterrupted()) {
163 Display
.getDefault().asyncExec(new Runnable() {
169 fTimeProvider
.notifyStartFinishTime();
173 synchronized (fMouseScrollNotifierLock
) {
174 fMouseScrollNotifier
= null;
178 public void mouseScrolled() {
179 fLastScrollTime
= System
.currentTimeMillis();
184 * Standard constructor
187 * The parent composite object
189 * The color scheme to use
191 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
193 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
195 fItemData
= new ItemData();
197 addFocusListener(this);
198 addMouseListener(this);
199 addMouseMoveListener(this);
200 addMouseTrackListener(this);
201 addMouseWheelListener(this);
202 addTraverseListener(this);
203 addKeyListener(this);
204 addControlListener(this);
205 addMenuDetectListener(this);
206 ScrollBar scrollHor
= getHorizontalBar();
208 if (scrollHor
!= null) {
209 scrollHor
.addSelectionListener(this);
214 public void dispose() {
216 fResourceManager
.dispose();
220 * Sets the timegraph provider used by this timegraph viewer.
222 * @param timeGraphProvider the timegraph provider
224 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
225 fTimeGraphProvider
= timeGraphProvider
;
227 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
228 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
231 if (fEventColorMap
!= null) {
232 for (Color color
: fEventColorMap
) {
233 fResourceManager
.destroyColor(color
.getRGB());
236 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
237 if (stateItems
!= null) {
238 fEventColorMap
= new Color
[stateItems
.length
];
239 for (int i
= 0; i
< stateItems
.length
; i
++) {
240 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
243 fEventColorMap
= new Color
[] { };
248 * Assign the given time provider
250 * @param timeProvider
253 public void setTimeProvider(ITimeDataProvider timeProvider
) {
254 fTimeProvider
= timeProvider
;
260 * Assign the status line manager
262 * @param statusLineManager
263 * The status line manager, or null to disable status line messages
266 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
267 if (fStatusLineManager
!= null && statusLineManager
== null) {
268 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
270 fStatusLineManager
= statusLineManager
;
274 * Assign the time graph scale
276 * @param timeGraphScale
277 * The time graph scale
280 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
281 fTimeGraphScale
= timeGraphScale
;
285 * Add a selection listener
288 * The listener to add
290 public void addSelectionListener(SelectionListener listener
) {
291 if (listener
== null) {
292 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
294 if (null == fSelectionListeners
) {
295 fSelectionListeners
= new ArrayList
<SelectionListener
>();
297 fSelectionListeners
.add(listener
);
301 * Remove a selection listener
304 * The listener to remove
306 public void removeSelectionListener(SelectionListener listener
) {
307 if (null != fSelectionListeners
) {
308 fSelectionListeners
.remove(listener
);
313 * Selection changed callback
315 public void fireSelectionChanged() {
316 if (null != fSelectionListeners
) {
317 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
318 while (it
.hasNext()) {
319 SelectionListener listener
= it
.next();
320 listener
.widgetSelected(null);
326 * Default selection callback
328 public void fireDefaultSelection() {
329 if (null != fSelectionListeners
) {
330 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
331 while (it
.hasNext()) {
332 SelectionListener listener
= it
.next();
333 listener
.widgetDefaultSelected(null);
339 * Get the traces in the model
341 * @return The array of traces
343 public ITimeGraphEntry
[] getTraces() {
344 return fItemData
.getTraces();
348 * Get the on/off trace filters
350 * @return The array of filters
352 public boolean[] getTraceFilter() {
353 return fItemData
.getTraceFilter();
357 * Refresh the data for the thing
359 public void refreshData() {
360 fItemData
.refreshData();
366 * Refresh data for the given traces
369 * The traces to refresh
371 public void refreshData(ITimeGraphEntry
[] traces
) {
372 fItemData
.refreshData(traces
);
378 * Adjust the scoll bars
380 public void adjustScrolls() {
381 if (null == fTimeProvider
) {
382 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
388 long time0
= fTimeProvider
.getTime0();
389 long time1
= fTimeProvider
.getTime1();
391 long timeMin
= fTimeProvider
.getMinTime();
392 long timeMax
= fTimeProvider
.getMaxTime();
394 long delta
= timeMax
- timeMin
;
397 int thumb
= H_SCROLLBAR_MAX
;
400 // Thumb size (page size)
401 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
402 // At the beginning of visible window
403 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
406 // position, minimum, maximum, thumb size, increment (half page)t, page
407 // increment size (full page)
408 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
411 boolean ensureVisibleItem(int idx
, boolean redraw
) {
412 boolean changed
= false;
415 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
416 if (fItemData
.fExpandedItems
[index
].fSelected
) {
421 if (index
>= fItemData
.fExpandedItems
.length
) {
424 if (index
< fTopIndex
) {
431 int page
= countPerPage();
432 if (index
>= fTopIndex
+ page
) {
433 setTopIndex(index
- page
+ 1);
444 * Assign the given index as the top one
449 public void setTopIndex(int idx
) {
450 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
451 index
= Math
.max(0, index
);
457 * Set the expanded state of a given entry
462 * True if expanded, false if collapsed
464 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
465 Item item
= fItemData
.findItem(entry
);
466 if (item
!= null && item
.fExpanded
!= expanded
) {
467 item
.fExpanded
= expanded
;
468 fItemData
.updateExpandedItems();
474 * Collapses all nodes of the viewer's tree, starting with the root.
478 public void collapseAll() {
479 for (Item item
: fItemData
.fItems
) {
480 item
.fExpanded
= false;
482 fItemData
.updateExpandedItems();
487 * Expands all nodes of the viewer's tree, starting with the root.
491 public void expandAll() {
492 for (Item item
: fItemData
.fItems
) {
493 item
.fExpanded
= true;
495 fItemData
.updateExpandedItems();
500 * Add a tree listener
503 * The listener to add
505 public void addTreeListener(ITimeGraphTreeListener listener
) {
506 if (!fTreeListeners
.contains(listener
)) {
507 fTreeListeners
.add(listener
);
512 * Remove a tree listener
515 * The listener to remove
517 public void removeTreeListener(ITimeGraphTreeListener listener
) {
518 if (fTreeListeners
.contains(listener
)) {
519 fTreeListeners
.remove(listener
);
524 * Tree event callback
529 * The expanded state (true for expanded, false for collapsed)
531 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
532 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
533 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
535 listener
.treeExpanded(event
);
537 listener
.treeCollapsed(event
);
543 * Add a menu listener on {@link ITimeGraphEntry}s
545 * The listener to add
548 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
549 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
550 fTimeGraphEntryMenuListeners
.add(listener
);
555 * Remove a menu listener on {@link ITimeGraphEntry}s
558 * The listener to remove
561 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
562 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
563 fTimeGraphEntryMenuListeners
.remove(listener
);
568 * Menu event callback on {@link ITimeGraphEntry}s
571 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
573 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
574 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
575 listener
.menuDetected(event
);
580 * Add a menu listener on {@link ITimeEvent}s
583 * The listener to add
586 public void addTimeEventMenuListener(MenuDetectListener listener
) {
587 if (!fTimeEventMenuListeners
.contains(listener
)) {
588 fTimeEventMenuListeners
.add(listener
);
593 * Remove a menu listener on {@link ITimeEvent}s
596 * The listener to remove
599 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
600 if (fTimeEventMenuListeners
.contains(listener
)) {
601 fTimeEventMenuListeners
.remove(listener
);
606 * Menu event callback on {@link ITimeEvent}s
609 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
611 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
612 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
613 listener
.menuDetected(event
);
617 @SuppressWarnings("deprecation")
619 public ISelection
getSelection() {
620 TimeGraphSelection sel
= new TimeGraphSelection();
621 ITimeGraphEntry trace
= getSelectedTrace();
622 if (null != trace
&& null != fTimeProvider
) {
624 if (fTimeProvider
instanceof ITimeDataProvider2
) {
625 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
627 selectedTime
= fTimeProvider
.getSelectedTime();
629 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
640 * Get the selection object
642 * @return The selection
644 public ISelection
getSelectionTrace() {
645 TimeGraphSelection sel
= new TimeGraphSelection();
646 ITimeGraphEntry trace
= getSelectedTrace();
654 * Enable/disable one of the traces in the model
657 * 1 to enable it, -1 to disable. The method returns immediately
658 * if another value is used.
660 public void selectTrace(int n
) {
661 if ((n
!= 1) && (n
!= -1)) {
665 boolean changed
= false;
666 int lastSelection
= -1;
667 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
668 Item item
= fItemData
.fExpandedItems
[i
];
669 if (item
.fSelected
) {
671 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
672 item
.fSelected
= false;
673 item
= fItemData
.fExpandedItems
[i
+ 1];
674 item
.fSelected
= true;
676 } else if ((-1 == n
) && (i
> 0)) {
677 item
.fSelected
= false;
678 item
= fItemData
.fExpandedItems
[i
- 1];
679 item
.fSelected
= true;
686 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
687 Item item
= fItemData
.fExpandedItems
[0];
688 item
.fSelected
= true;
693 ensureVisibleItem(-1, false);
695 fireSelectionChanged();
703 * 1 for next event, -1 for previous event
705 public void selectEvent(int n
) {
706 if (null == fTimeProvider
) {
709 ITimeGraphEntry trace
= getSelectedTrace();
714 if (fTimeProvider
instanceof ITimeDataProvider2
) {
715 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
717 @SuppressWarnings("deprecation")
718 long time
= fTimeProvider
.getSelectedTime();
721 long endTime
= fTimeProvider
.getEndTime();
722 ITimeEvent nextEvent
;
723 if (-1 == n
&& selectedTime
> endTime
) {
724 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
726 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
728 if (null == nextEvent
&& -1 == n
) {
729 nextEvent
= Utils
.getFirstEvent(trace
);
731 if (null != nextEvent
) {
732 long nextTime
= nextEvent
.getTime();
733 // If last event detected e.g. going back or not moving to a next
735 if (nextTime
<= selectedTime
&& n
== 1) {
736 // Select to the end of this last event
737 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
738 // but not beyond the end of the trace
739 if (nextTime
> endTime
) {
742 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
743 // for previous event go to its end time unless we were already there
744 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
746 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
747 fireSelectionChanged();
749 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
750 fireSelectionChanged();
755 * Select the next event
757 public void selectNextEvent() {
759 // Notify if visible time window has been adjusted
760 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
764 * Select the previous event
766 public void selectPrevEvent() {
768 // Notify if visible time window has been adjusted
769 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
773 * Select the next trace
775 public void selectNextTrace() {
780 * Select the previous trace
782 public void selectPrevTrace() {
787 * Zoom based on mouse cursor location with mouse scrolling
789 * @param zoomIn true to zoom in, false to zoom out
791 public void zoom(boolean zoomIn
) {
792 int globalX
= getDisplay().getCursorLocation().x
;
793 Point p
= toControl(globalX
, 0);
794 int nameSpace
= fTimeProvider
.getNameSpace();
795 int timeSpace
= fTimeProvider
.getTimeSpace();
796 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
797 long time0
= fTimeProvider
.getTime0();
798 long time1
= fTimeProvider
.getTime1();
799 long interval
= time1
- time0
;
802 } // to allow getting out of single point interval
805 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
807 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
809 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
810 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
811 long newTime1
= newTime0
+ newInterval
;
812 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
813 synchronized (fMouseScrollNotifierLock
) {
814 if (fMouseScrollNotifier
== null) {
815 fMouseScrollNotifier
= new MouseScrollNotifier();
816 fMouseScrollNotifier
.start();
818 fMouseScrollNotifier
.mouseScrolled();
823 * zoom in using single click
825 public void zoomIn() {
826 long prevTime0
= fTimeProvider
.getTime0();
827 long prevTime1
= fTimeProvider
.getTime1();
828 long prevRange
= prevTime1
- prevTime0
;
829 if (prevRange
== 0) {
833 if (fTimeProvider
instanceof ITimeDataProvider2
) {
834 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
835 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
837 @SuppressWarnings("deprecation")
838 long selectedTime
= fTimeProvider
.getSelectedTime();
839 selTime
= selectedTime
;
841 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
842 selTime
= (prevTime0
+ prevTime1
) / 2;
844 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
845 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
847 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
849 if (inaccuracy
> 0 && inaccuracy
< 100) {
850 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
854 long min
= fTimeProvider
.getMinTimeInterval();
855 if ((time1
- time0
) < min
) {
856 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
860 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
864 * zoom out using single click
866 public void zoomOut() {
867 long prevTime0
= fTimeProvider
.getTime0();
868 long prevTime1
= fTimeProvider
.getTime1();
870 if (fTimeProvider
instanceof ITimeDataProvider2
) {
871 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
872 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
874 @SuppressWarnings("deprecation")
875 long selectedTime
= fTimeProvider
.getSelectedTime();
876 selTime
= selectedTime
;
878 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
879 selTime
= (prevTime0
+ prevTime1
) / 2;
881 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
882 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
884 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
885 if (inaccuracy
> 0 && inaccuracy
< 100) {
886 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
890 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
894 * Return the currently selected trace
896 * @return The entry matching the trace
898 public ITimeGraphEntry
getSelectedTrace() {
899 ITimeGraphEntry trace
= null;
900 int idx
= getSelectedIndex();
902 trace
= fItemData
.fExpandedItems
[idx
].fTrace
;
908 * Retrieve the index of the currently selected item
912 public int getSelectedIndex() {
914 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
915 Item item
= fItemData
.fExpandedItems
[i
];
916 if (item
.fSelected
) {
924 boolean toggle(int idx
) {
925 boolean toggled
= false;
926 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
927 Item item
= fItemData
.fExpandedItems
[idx
];
928 if (item
.fHasChildren
) {
929 item
.fExpanded
= !item
.fExpanded
;
930 fItemData
.updateExpandedItems();
934 fireTreeEvent(item
.fTrace
, item
.fExpanded
);
940 int getItemIndexAtY(int y
) {
944 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
946 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
947 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
954 int idx
= y
/ fGlobalItemHeight
;
956 if (idx
< fItemData
.fExpandedItems
.length
) {
962 boolean isOverSplitLine(int x
) {
963 if (x
< 0 || null == fTimeProvider
) {
966 int nameWidth
= fTimeProvider
.getNameSpace();
967 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
970 ITimeGraphEntry
getEntry(Point pt
) {
971 int idx
= getItemIndexAtY(pt
.y
);
972 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fTrace
: null;
979 public int getXForTime(long time
) {
980 if (null == fTimeProvider
) {
983 long time0
= fTimeProvider
.getTime0();
984 long time1
= fTimeProvider
.getTime1();
985 int width
= getCtrlSize().x
;
986 int nameSpace
= fTimeProvider
.getNameSpace();
987 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
988 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
996 public long getTimeAtX(int coord
) {
997 if (null == fTimeProvider
) {
1001 Point size
= getCtrlSize();
1002 long time0
= fTimeProvider
.getTime0();
1003 long time1
= fTimeProvider
.getTime1();
1004 int nameWidth
= fTimeProvider
.getNameSpace();
1005 final int x
= coord
- nameWidth
;
1006 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1007 if (x
>= 0 && size
.x
>= nameWidth
) {
1008 if (time1
- time0
> timeWidth
) {
1009 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1010 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1012 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1013 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1019 void selectItem(int idx
, boolean addSelection
) {
1020 boolean changed
= false;
1022 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1023 Item item
= fItemData
.fExpandedItems
[idx
];
1024 changed
= !item
.fSelected
;
1025 item
.fSelected
= true;
1028 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1029 Item item
= fItemData
.fExpandedItems
[i
];
1030 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1033 item
.fSelected
= i
== idx
;
1036 changed
|= ensureVisibleItem(idx
, true);
1043 * Callback for item selection
1046 * The entry matching the trace
1047 * @param addSelection
1048 * If the selection is added or removed
1050 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1051 int idx
= fItemData
.findItemIndex(trace
);
1052 selectItem(idx
, addSelection
);
1056 * Retrieve the number of entries shown per page.
1060 public int countPerPage() {
1061 int height
= getCtrlSize().y
;
1063 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1065 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1066 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1067 if (ySum
>= height
) {
1072 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1073 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1074 if (ySum
>= height
) {
1082 count
= height
/ fGlobalItemHeight
;
1088 * Get the index of the top element
1092 public int getTopIndex() {
1097 * Get the number of expanded items
1099 * @return The count of expanded items
1101 public int getExpandedElementCount() {
1102 return fItemData
.fExpandedItems
.length
;
1106 * Get an array of all expanded elements
1108 * @return The expanded elements
1110 public ITimeGraphEntry
[] getExpandedElements() {
1111 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1112 for (Item item
: fItemData
.fExpandedItems
) {
1113 elements
.add(item
.fTrace
);
1115 return elements
.toArray(new ITimeGraphEntry
[0]);
1118 Point
getCtrlSize() {
1119 Point size
= getSize();
1120 if (getHorizontalBar().isVisible()) {
1121 size
.y
-= getHorizontalBar().getSize().y
;
1126 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1128 int y
= bound
.y
+ (idx
- fTopIndex
) * fGlobalItemHeight
;
1129 int width
= nameWidth
;
1130 int height
= fGlobalItemHeight
;
1131 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1133 for (int i
= fTopIndex
; i
< idx
; i
++) {
1134 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1137 height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1139 return new Rectangle(x
, y
, width
, height
);
1142 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1143 int x
= bound
.x
+ nameWidth
;
1144 int y
= bound
.y
+ (idx
- fTopIndex
) * fGlobalItemHeight
;
1145 int width
= bound
.width
- x
;
1146 int height
= fGlobalItemHeight
;
1147 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1149 for (int i
= fTopIndex
; i
< idx
; i
++) {
1150 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1153 height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1155 return new Rectangle(x
, y
, width
, height
);
1159 void paint(Rectangle bounds
, PaintEvent e
) {
1161 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1162 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1164 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1168 fIdealNameSpace
= 0;
1169 int nameSpace
= fTimeProvider
.getNameSpace();
1171 // draw empty name space background
1172 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1173 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1176 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1178 int alpha
= gc
.getAlpha();
1181 long time0
= fTimeProvider
.getTime0();
1182 long time1
= fTimeProvider
.getTime1();
1183 long selectionBegin
;
1185 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1186 selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1187 selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1189 @SuppressWarnings("deprecation")
1190 long selectedTime
= fTimeProvider
.getSelectedTime();
1191 selectionBegin
= selectedTime
;
1192 selectionEnd
= selectedTime
;
1194 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1195 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1196 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1198 // draw selection lines
1199 if (fDragState
!= DRAG_SELECTION
) {
1200 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1201 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1202 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1205 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1206 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1211 // draw selection background
1212 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1213 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1214 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1215 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1217 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1218 } else if (x0
- x1
> 1) {
1219 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1223 // draw drag selection background
1224 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1225 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1226 if (fDragX0
< fDragX
) {
1227 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1228 } else if (fDragX0
> fDragX
) {
1229 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1234 if (DRAG_SPLIT_LINE
== fDragState
) {
1235 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1236 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1237 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1238 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1239 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1240 if (fDragX
!= fDragX0
) {
1241 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1243 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1244 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1245 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1246 if (fDragX
!= fDragX0
) {
1247 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1249 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1250 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1251 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1258 * Draw many items at once
1261 * The rectangle of the area
1262 * @param timeProvider
1265 * The array items to draw
1267 * The index of the first element to draw
1269 * The width reserved for the names
1271 * Reference to the SWT GC object
1273 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1274 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1275 for (int i
= topIndex
; i
< items
.length
; i
++) {
1276 Item item
= items
[i
];
1277 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1279 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1285 * @param item the item to draw
1286 * @param bounds the container rectangle
1287 * @param timeProvider Time provider
1288 * @param i the item index
1289 * @param nameSpace the name space
1290 * @param gc Graphics context
1292 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1293 ITimeGraphEntry entry
= item
.fTrace
;
1294 long time0
= timeProvider
.getTime0();
1295 long time1
= timeProvider
.getTime1();
1297 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1298 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1300 @SuppressWarnings("deprecation")
1301 long time
= fTimeProvider
.getSelectedTime();
1302 selectedTime
= time
;
1305 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1306 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1310 if (! item
.fTrace
.hasTimeEvents()) {
1311 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1312 nameRect
.width
+= statesRect
.width
;
1313 drawName(item
, nameRect
, gc
);
1315 drawName(item
, nameRect
, gc
);
1317 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1318 if (rect
.isEmpty()) {
1319 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1322 if (time1
<= time0
) {
1323 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1324 gc
.fillRectangle(rect
);
1325 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1329 // Initialize _rect1 to same values as enclosing rectangle rect
1330 Rectangle stateRect
= Utils
.clone(rect
);
1331 boolean selected
= item
.fSelected
;
1332 // K pixels per second
1333 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1335 if (item
.fTrace
.hasTimeEvents()) {
1336 fillSpace(rect
, gc
, selected
);
1337 // Drawing rectangle is smaller than reserved space
1339 stateRect
.height
-= 6;
1341 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1342 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1345 while (iterator
.hasNext()) {
1346 ITimeEvent event
= iterator
.next();
1347 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1348 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1349 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1350 // event is out of bounds
1353 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1354 stateRect
.x
= Math
.max(rect
.x
, x
);
1355 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1356 if (stateRect
.x
== lastX
) {
1357 stateRect
.width
-= 1;
1358 if (stateRect
.width
> 0) {
1359 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1360 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1364 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1365 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1370 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1374 * Draw the name of an item.
1379 * Where to draw the name
1383 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1384 boolean hasTimeEvents
= item
.fTrace
.hasTimeEvents();
1385 if (! hasTimeEvents
) {
1386 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1387 gc
.fillRectangle(bounds
);
1388 if (item
.fSelected
&& fIsInFocus
) {
1389 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1390 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1393 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1394 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1395 gc
.fillRectangle(bounds
);
1398 // No name to be drawn
1399 if (fTimeProvider
.getNameSpace() == 0) {
1403 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1404 if (item
.fHasChildren
) {
1405 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1406 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1407 Rectangle rect
= Utils
.clone(bounds
);
1408 rect
.x
+= leftMargin
;
1409 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1410 rect
.width
= EXPAND_SIZE
;
1411 rect
.height
= EXPAND_SIZE
;
1412 gc
.fillRectangle(rect
);
1413 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1414 int midy
= rect
.y
+ rect
.height
/ 2;
1415 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1416 if (!item
.fExpanded
) {
1417 int midx
= rect
.x
+ rect
.width
/ 2;
1418 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1421 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1423 Image img
= fTimeGraphProvider
.getItemImage(item
.fTrace
);
1426 int imgHeight
= img
.getImageData().height
;
1427 int imgWidth
= img
.getImageData().width
;
1429 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1430 gc
.drawImage(img
, x
, y
);
1431 leftMargin
+= imgWidth
+ MARGIN
;
1433 String name
= item
.fName
;
1434 Point size
= gc
.stringExtent(name
);
1435 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1436 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1438 if (hasTimeEvents
) {
1439 // cut long string with "..."
1440 int width
= bounds
.width
- leftMargin
;
1442 while (size
.x
> width
&& name
.length() > 1) {
1444 name
= name
.substring(0, name
.length() - 1);
1445 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1448 name
+= "..."; //$NON-NLS-1$
1451 Rectangle rect
= Utils
.clone(bounds
);
1452 rect
.x
+= leftMargin
;
1453 rect
.width
-= leftMargin
;
1455 if (rect
.width
> 0) {
1456 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1457 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1458 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1459 leftMargin
+= textWidth
+ MARGIN
;
1462 if (hasTimeEvents
) {
1464 int x
= bounds
.x
+ leftMargin
;
1465 int width
= bounds
.width
- x
;
1466 int midy
= bounds
.y
+ bounds
.height
/ 2;
1467 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1468 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1474 * Draw the state (color fill)
1479 * Time event for which we're drawing the state
1485 * Is this time event currently selected (so it appears
1487 * @param timeSelected
1488 * Is the timestamp currently selected
1489 * @return true if the state was drawn
1492 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1493 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1495 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1496 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1499 boolean visible
= rect
.width
== 0 ?
false : true;
1502 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1503 // Only draw the top and bottom borders
1504 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1505 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1506 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1507 if (rect
.width
== 1) {
1508 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1509 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1513 Color stateColor
= null;
1514 if (colorIdx
< fEventColorMap
.length
) {
1515 stateColor
= fEventColorMap
[colorIdx
];
1517 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1520 boolean reallySelected
= timeSelected
&& selected
;
1521 // fill all rect area
1522 gc
.setBackground(stateColor
);
1523 gc
.fillRectangle(rect
);
1524 // get the border color?
1525 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1528 if (!reallySelected
) {
1529 // Draw the top and bottom borders i.e. no side borders
1530 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1531 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1534 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1535 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1537 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1542 * Fill the space between two contiguous time events
1549 * Is this time event selected or not
1551 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1552 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1553 gc
.fillRectangle(rect
);
1554 if (fDragState
== DRAG_ZOOM
) {
1555 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1556 if (fDragX0
< fDragX
) {
1557 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1558 } else if (fDragX0
> fDragX
) {
1559 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1563 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1564 int midy
= rect
.y
+ rect
.height
/ 2;
1565 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1569 public void keyTraversed(TraverseEvent e
) {
1570 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1576 public void keyPressed(KeyEvent e
) {
1578 if (fItemData
.fExpandedItems
.length
== 0) {
1581 if (SWT
.HOME
== e
.keyCode
) {
1583 } else if (SWT
.END
== e
.keyCode
) {
1584 idx
= fItemData
.fExpandedItems
.length
- 1;
1585 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1586 idx
= getSelectedIndex();
1589 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1592 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1593 idx
= getSelectedIndex();
1596 } else if (idx
> 0) {
1599 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1601 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1603 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1604 int page
= countPerPage();
1605 idx
= getSelectedIndex();
1610 if (idx
>= fItemData
.fExpandedItems
.length
) {
1611 idx
= fItemData
.fExpandedItems
.length
- 1;
1613 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1614 int page
= countPerPage();
1615 idx
= getSelectedIndex();
1623 } else if (SWT
.CR
== e
.keyCode
) {
1624 idx
= getSelectedIndex();
1626 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1629 fireDefaultSelection();
1635 selectItem(idx
, false);
1636 fireSelectionChanged();
1638 int x
= toControl(e
.display
.getCursorLocation()).x
;
1639 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1643 public void keyReleased(KeyEvent e
) {
1644 int x
= toControl(e
.display
.getCursorLocation()).x
;
1645 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1649 public void focusGained(FocusEvent e
) {
1651 if (fMouseScrollFilterListener
== null) {
1652 fMouseScrollFilterListener
= new Listener() {
1653 // This filter is used to prevent horizontal scrolling of the view
1654 // when the mouse wheel is used to zoom
1656 public void handleEvent(Event event
) {
1660 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1663 updateStatusLine(NO_STATUS
);
1667 public void focusLost(FocusEvent e
) {
1669 if (fMouseScrollFilterListener
!= null) {
1670 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1671 fMouseScrollFilterListener
= null;
1673 if (DRAG_NONE
!= fDragState
) {
1675 fDragState
= DRAG_NONE
;
1678 updateStatusLine(NO_STATUS
);
1682 * @return If the current view is focused
1684 public boolean isInFocus() {
1689 * Provide the possibility to control the wait cursor externally e.g. data
1690 * requests in progress
1692 * @param waitInd Should we wait indefinitely?
1694 public void waitCursor(boolean waitInd
) {
1695 // Update cursor as indicated
1697 setCursor(fWaitCursor
);
1703 private void updateCursor(int x
, int stateMask
) {
1704 // if Wait cursor not active, check for the need to change the cursor
1705 if (getCursor() == fWaitCursor
) {
1708 Cursor cursor
= null;
1709 if (fDragState
== DRAG_SPLIT_LINE
) {
1710 } else if (fDragState
== DRAG_SELECTION
) {
1711 cursor
= fResizeCursor
;
1712 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1713 cursor
= fDragCursor
;
1714 } else if (fDragState
== DRAG_ZOOM
) {
1715 cursor
= fZoomCursor
;
1716 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1717 cursor
= fDragCursor
;
1718 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1719 cursor
= fResizeCursor
;
1721 if (!isOverSplitLine(x
) &&fTimeProvider
instanceof ITimeDataProvider2
) {
1722 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1723 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1724 int xBegin
= getXForTime(selectionBegin
);
1725 int xEnd
= getXForTime(selectionEnd
);
1726 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1727 cursor
= fResizeCursor
;
1731 if (getCursor() != cursor
) {
1736 private void updateStatusLine(int x
) {
1737 if (fStatusLineManager
== null) {
1740 StringBuilder message
= new StringBuilder();
1741 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1742 long time
= getTimeAtX(x
);
1744 message
.append("T: "); //$NON-NLS-1$
1745 message
.append(new CtfTmfTimestamp(time
).toString());
1746 message
.append(" T1: "); //$NON-NLS-1$
1747 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1748 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1749 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1750 message
.append(new CtfTmfTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1751 if (selectionBegin
!= selectionEnd
) {
1752 message
.append(" T2: "); //$NON-NLS-1$
1753 message
.append(new CtfTmfTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1754 message
.append(" \u0394: "); //$NON-NLS-1$
1755 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1758 @SuppressWarnings("deprecation")
1759 long selectedTime
= fTimeProvider
.getSelectedTime();
1760 message
.append(new CtfTmfTimestamp(selectedTime
));
1763 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1764 long time0
= fDragTime0
;
1765 long time
= getTimeAtX(fDragX
);
1766 message
.append("T1: "); //$NON-NLS-1$
1767 message
.append(new CtfTmfTimestamp(Math
.min(time
, time0
)).toString());
1768 if (time
!= time0
) {
1769 message
.append(" T2: "); //$NON-NLS-1$
1770 message
.append(new CtfTmfTimestamp(Math
.max(time
, time0
)).toString());
1771 message
.append(" \u0394: "); //$NON-NLS-1$
1772 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1775 fStatusLineManager
.setMessage(message
.toString());
1779 public void mouseMove(MouseEvent e
) {
1780 if (null == fTimeProvider
) {
1783 Point size
= getCtrlSize();
1784 if (DRAG_TRACE_ITEM
== fDragState
) {
1785 int nameWidth
= fTimeProvider
.getNameSpace();
1786 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1788 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1789 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1790 long time1
= fTime1bak
- timeDelta
;
1791 long maxTime
= fTimeProvider
.getMaxTime();
1792 if (time1
> maxTime
) {
1795 long time0
= time1
- (fTime1bak
- fTime0bak
);
1796 if (time0
< fTimeProvider
.getMinTime()) {
1797 time0
= fTimeProvider
.getMinTime();
1798 time1
= time0
+ (fTime1bak
- fTime0bak
);
1800 fTimeProvider
.setStartFinishTime(time0
, time1
);
1802 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1804 fTimeProvider
.setNameSpace(e
.x
);
1805 } else if (DRAG_SELECTION
== fDragState
) {
1806 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1807 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1809 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1811 } else if (DRAG_ZOOM
== fDragState
) {
1812 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1814 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1815 } else if (DRAG_NONE
== fDragState
) {
1816 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1817 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1820 fMouseOverSplitLine
= mouseOverSplitLine
;
1822 updateCursor(e
.x
, e
.stateMask
);
1823 updateStatusLine(e
.x
);
1827 public void mouseDoubleClick(MouseEvent e
) {
1828 if (null == fTimeProvider
) {
1831 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1832 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
1833 fTimeProvider
.setNameSpace(fIdealNameSpace
);
1834 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1835 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1838 fMouseOverSplitLine
= mouseOverSplitLine
;
1841 int idx
= getItemIndexAtY(e
.y
);
1843 selectItem(idx
, false);
1844 fireDefaultSelection();
1850 public void mouseDown(MouseEvent e
) {
1851 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
) {
1855 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
1856 int nameSpace
= fTimeProvider
.getNameSpace();
1857 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
1858 fDragState
= DRAG_SPLIT_LINE
;
1859 fDragButton
= e
.button
;
1862 fTime0bak
= fTimeProvider
.getTime0();
1863 fTime1bak
= fTimeProvider
.getTime1();
1865 updateCursor(e
.x
, e
.stateMask
);
1869 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
1870 int nameSpace
= fTimeProvider
.getNameSpace();
1871 idx
= getItemIndexAtY(e
.y
);
1873 Item item
= fItemData
.fExpandedItems
[idx
];
1874 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
1878 selectItem(idx
, false);
1879 fireSelectionChanged();
1881 selectItem(idx
, false); // clear selection
1882 fireSelectionChanged();
1884 long hitTime
= getTimeAtX(e
.x
);
1888 fDragState
= DRAG_SELECTION
;
1889 fDragButton
= e
.button
;
1892 fDragTime0
= getTimeAtX(fDragX0
);
1893 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1894 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1895 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1896 int xBegin
= getXForTime(selectionBegin
);
1897 int xEnd
= getXForTime(selectionEnd
);
1898 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1899 long time
= getTimeAtX(e
.x
);
1900 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
1902 fDragTime0
= selectionEnd
;
1905 fDragTime0
= selectionBegin
;
1908 long time
= getTimeAtX(e
.x
);
1909 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
1911 fDragTime0
= selectionEnd
;
1912 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
1914 fDragTime0
= selectionBegin
;
1918 fTime0bak
= fTimeProvider
.getTime0();
1919 fTime1bak
= fTimeProvider
.getTime1();
1921 updateCursor(e
.x
, e
.stateMask
);
1922 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1924 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
1925 long hitTime
= getTimeAtX(e
.x
);
1928 fDragState
= DRAG_TRACE_ITEM
;
1929 fDragButton
= e
.button
;
1932 fTime0bak
= fTimeProvider
.getTime0();
1933 fTime1bak
= fTimeProvider
.getTime1();
1934 updateCursor(e
.x
, e
.stateMask
);
1936 } else if (3 == e
.button
) {
1937 if (fTimeProvider
.getTime0() == fTimeProvider
.getTime1() || getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
1941 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
1943 fDragState
= DRAG_ZOOM
;
1944 fDragButton
= e
.button
;
1946 updateCursor(e
.x
, e
.stateMask
);
1947 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1952 public void mouseUp(MouseEvent e
) {
1953 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
1954 menuDetected(fPendingMenuDetectEvent
);
1956 if (DRAG_NONE
!= fDragState
) {
1958 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
1959 if (fDragX
!= fDragX0
) {
1960 fTimeProvider
.notifyStartFinishTime();
1962 fDragState
= DRAG_NONE
;
1963 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
1964 fDragState
= DRAG_NONE
;
1966 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
1967 if (fDragX
== fDragX0
) { // click without selecting anything
1968 long time
= getTimeAtX(e
.x
);
1969 fTimeProvider
.setSelectedTimeNotify(time
, false);
1971 long time0
= fDragTime0
;
1972 long time1
= getTimeAtX(fDragX
);
1973 if (time0
<= time1
) {
1974 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time0
, time1
);
1976 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time1
, time0
);
1979 fDragState
= DRAG_NONE
;
1981 fTimeGraphScale
.setDragRange(-1, -1);
1982 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
1983 int nameWidth
= fTimeProvider
.getNameSpace();
1984 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
1985 long time0
= getTimeAtX(fDragX0
);
1986 long time1
= getTimeAtX(fDragX
);
1987 if (time0
< time1
) {
1988 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
1990 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
1995 fDragState
= DRAG_NONE
;
1996 fTimeGraphScale
.setDragRange(-1, -1);
1999 updateCursor(e
.x
, e
.stateMask
);
2000 updateStatusLine(e
.x
);
2004 public void mouseEnter(MouseEvent e
) {
2008 public void mouseExit(MouseEvent e
) {
2009 if (fMouseOverSplitLine
) {
2010 fMouseOverSplitLine
= false;
2013 updateStatusLine(NO_STATUS
);
2017 public void mouseHover(MouseEvent e
) {
2021 public void mouseScrolled(MouseEvent e
) {
2022 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2025 boolean zoomScroll
= false;
2026 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2027 Point parentSize
= getParent().getSize();
2028 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2029 // over the parent control
2030 if (e
.x
> getCtrlSize().x
) {
2031 // over the horizontal scroll bar
2033 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2034 // over the name space
2040 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2043 } else if (e
.count
< 0) {
2047 setTopIndex(getTopIndex() - e
.count
);
2052 public void controlMoved(ControlEvent e
) {
2056 public void controlResized(ControlEvent e
) {
2061 public void widgetDefaultSelected(SelectionEvent e
) {
2065 public void widgetSelected(SelectionEvent e
) {
2066 if (e
.widget
== getVerticalBar()) {
2067 setTopIndex(getVerticalBar().getSelection());
2068 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2069 int start
= getHorizontalBar().getSelection();
2070 long time0
= fTimeProvider
.getTime0();
2071 long time1
= fTimeProvider
.getTime1();
2072 long timeMin
= fTimeProvider
.getMinTime();
2073 long timeMax
= fTimeProvider
.getMaxTime();
2074 long delta
= timeMax
- timeMin
;
2076 long range
= time1
- time0
;
2077 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2078 time1
= time0
+ range
;
2080 // TODO: Follow-up with Bug 310310
2081 // In Linux SWT.DRAG is the only value received
2082 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2083 if (e
.detail
== SWT
.DRAG
) {
2084 fTimeProvider
.setStartFinishTime(time0
, time1
);
2086 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2092 public int getBorderWidth() {
2093 return fBorderWidth
;
2097 * Set the border width
2099 * @param borderWidth
2102 public void setBorderWidth(int borderWidth
) {
2103 this.fBorderWidth
= borderWidth
;
2107 * @return The current height of the header row
2109 public int getHeaderHeight() {
2110 return fHeaderHeight
;
2114 * Set the height of the header row
2116 * @param headerHeight
2119 public void setHeaderHeight(int headerHeight
) {
2120 this.fHeaderHeight
= headerHeight
;
2124 * @return The height of regular item rows
2126 public int getItemHeight() {
2127 return fGlobalItemHeight
;
2131 * Set the height of regular itew rows
2136 public void setItemHeight(int rowHeight
) {
2137 this.fGlobalItemHeight
= rowHeight
;
2141 * Set the minimum item width
2143 * @param width The minimum width
2145 public void setMinimumItemWidth(int width
) {
2146 this.fMinimumItemWidth
= width
;
2150 * @return The minimum item width
2152 public int getMinimumItemWidth() {
2153 return fMinimumItemWidth
;
2157 * @return The entries that are currently filtered out
2161 public List
<ITimeGraphEntry
> getFilteredOut() {
2162 return fItemData
.getFilteredOut();
2166 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2167 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2168 fSelectionChangedListeners
.add(listener
);
2173 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2174 if (listener
!= null) {
2175 fSelectionChangedListeners
.remove(listener
);
2180 public void setSelection(ISelection selection
) {
2181 if (selection
instanceof TimeGraphSelection
) {
2182 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2183 Object ob
= sel
.getFirstElement();
2184 if (ob
instanceof ITimeGraphEntry
) {
2185 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2186 selectItem(trace
, false);
2193 * @param filter The filter object to be attached to the view
2196 public void addFilter(ViewerFilter filter
) {
2197 if (!fFilters
.contains(filter
)) {
2198 fFilters
.add(filter
);
2203 * @param filter The filter object to be attached to the view
2206 public void removeFilter(ViewerFilter filter
) {
2207 fFilters
.remove(filter
);
2210 private class ItemData
{
2211 private Item
[] fExpandedItems
= new Item
[0];
2212 private Item
[] fItems
= new Item
[0];
2213 private ITimeGraphEntry fTraces
[] = new ITimeGraphEntry
[0];
2214 private boolean fTraceFilter
[] = new boolean[0];
2215 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<ITimeGraphEntry
>();
2220 Item
findItem(ITimeGraphEntry entry
) {
2221 if (entry
== null) {
2225 for (int i
= 0; i
< fItems
.length
; i
++) {
2226 Item item
= fItems
[i
];
2227 if (item
.fTrace
== entry
) {
2235 int findItemIndex(ITimeGraphEntry trace
) {
2236 if (trace
== null) {
2240 for (int i
= 0; i
< fExpandedItems
.length
; i
++) {
2241 Item item
= fExpandedItems
[i
];
2242 if (item
.fTrace
== trace
) {
2250 public void refreshData() {
2251 List
<Item
> itemList
= new ArrayList
<Item
>();
2252 fFilteredOut
.clear();
2253 ITimeGraphEntry selection
= getSelectedTrace();
2254 for (int i
= 0; i
< fTraces
.length
; i
++) {
2255 ITimeGraphEntry entry
= fTraces
[i
];
2256 refreshData(itemList
, null, 0, entry
);
2258 fItems
= itemList
.toArray(new Item
[0]);
2259 updateExpandedItems();
2260 if (selection
!= null) {
2261 for (Item item
: fExpandedItems
) {
2262 if (item
.fTrace
== selection
) {
2263 item
.fSelected
= true;
2270 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2271 Item item
= new Item(entry
, entry
.getName(), level
);
2272 if (parent
!= null) {
2273 parent
.fChildren
.add(item
);
2275 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2277 if (entry
.hasChildren()) {
2278 item
.fExpanded
= true;
2279 item
.fHasChildren
= true;
2280 for (ITimeGraphEntry child
: entry
.getChildren()) {
2281 refreshData(itemList
, item
, level
+ 1, child
);
2286 public void updateExpandedItems() {
2287 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2288 for (int i
= 0; i
< fTraces
.length
; i
++) {
2289 ITimeGraphEntry entry
= fTraces
[i
];
2290 Item item
= findItem(entry
);
2291 refreshExpanded(expandedItemList
, item
);
2293 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2296 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2297 // Check for filters
2298 boolean display
= true;
2299 for (ViewerFilter filter
: fFilters
) {
2300 if (!filter
.select(null, item
.fTrace
.getParent(), item
.fTrace
)) {
2306 expandedItemList
.add(item
);
2307 if (item
.fHasChildren
&& item
.fExpanded
) {
2308 for (Item child
: item
.fChildren
) {
2309 refreshExpanded(expandedItemList
, child
);
2315 public void refreshData(ITimeGraphEntry traces
[]) {
2316 if (traces
== null) {
2317 fTraceFilter
= null;
2320 if (traces
.length
== 0) {
2321 fTraceFilter
= null;
2322 } else if (fTraceFilter
== null || traces
.length
!= fTraceFilter
.length
) {
2323 fTraceFilter
= new boolean[traces
.length
];
2324 java
.util
.Arrays
.fill(fTraceFilter
, true);
2326 fTraces
= Arrays
.copyOf(traces
, traces
.length
);
2332 public ITimeGraphEntry
[] getTraces() {
2336 public boolean[] getTraceFilter() {
2337 return fTraceFilter
;
2340 public List
<ITimeGraphEntry
> getFilteredOut() {
2341 return fFilteredOut
;
2345 private class Item
{
2346 private boolean fExpanded
;
2347 private boolean fSelected
;
2348 private boolean fHasChildren
;
2349 private int fItemHeight
;
2351 private List
<Item
> fChildren
;
2352 private String fName
;
2353 private ITimeGraphEntry fTrace
;
2355 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2356 this.fTrace
= trace
;
2358 this.fLevel
= level
;
2359 this.fChildren
= new ArrayList
<Item
>();
2363 public String
toString() {
2372 public void menuDetected(MenuDetectEvent e
) {
2373 if (null == fTimeProvider
) {
2376 if (e
.detail
== SWT
.MENU_MOUSE
) {
2377 if (fPendingMenuDetectEvent
== null) {
2378 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2379 * Store the event and trigger it later just before handling mouseUp.
2380 * This allows for the method to detect if mouse is used to drag zoom.
2382 fPendingMenuDetectEvent
= e
;
2385 fPendingMenuDetectEvent
= null;
2386 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2390 if (fDragState
!= DRAG_NONE
) {
2394 Point p
= toControl(e
.x
, e
.y
);
2395 int idx
= getItemIndexAtY(p
.y
);
2396 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2397 Item item
= fItemData
.fExpandedItems
[idx
];
2398 ITimeGraphEntry entry
= item
.fTrace
;
2399 if (entry
.hasTimeEvents()) {
2400 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2401 if (event
!= null) {
2403 fireMenuEventOnTimeEvent(e
);
2408 fireMenuEventOnTimeGraphEntry(e
);