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 * Add display of links between items
17 *****************************************************************************/
19 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.Iterator
;
24 import java
.util
.List
;
26 import org
.eclipse
.jface
.action
.IStatusLineManager
;
27 import org
.eclipse
.jface
.resource
.JFaceResources
;
28 import org
.eclipse
.jface
.resource
.LocalResourceManager
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
32 import org
.eclipse
.jface
.viewers
.ViewerFilter
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestampDelta
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider2
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphTreeListener
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.StateItem
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.TimeGraphTreeExpansionEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.ControlEvent
;
46 import org
.eclipse
.swt
.events
.ControlListener
;
47 import org
.eclipse
.swt
.events
.FocusEvent
;
48 import org
.eclipse
.swt
.events
.FocusListener
;
49 import org
.eclipse
.swt
.events
.KeyEvent
;
50 import org
.eclipse
.swt
.events
.KeyListener
;
51 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
52 import org
.eclipse
.swt
.events
.MenuDetectListener
;
53 import org
.eclipse
.swt
.events
.MouseEvent
;
54 import org
.eclipse
.swt
.events
.MouseListener
;
55 import org
.eclipse
.swt
.events
.MouseMoveListener
;
56 import org
.eclipse
.swt
.events
.MouseTrackListener
;
57 import org
.eclipse
.swt
.events
.MouseWheelListener
;
58 import org
.eclipse
.swt
.events
.PaintEvent
;
59 import org
.eclipse
.swt
.events
.SelectionEvent
;
60 import org
.eclipse
.swt
.events
.SelectionListener
;
61 import org
.eclipse
.swt
.events
.TraverseEvent
;
62 import org
.eclipse
.swt
.events
.TraverseListener
;
63 import org
.eclipse
.swt
.events
.TypedEvent
;
64 import org
.eclipse
.swt
.graphics
.Color
;
65 import org
.eclipse
.swt
.graphics
.Cursor
;
66 import org
.eclipse
.swt
.graphics
.GC
;
67 import org
.eclipse
.swt
.graphics
.Image
;
68 import org
.eclipse
.swt
.graphics
.Point
;
69 import org
.eclipse
.swt
.graphics
.Rectangle
;
70 import org
.eclipse
.swt
.widgets
.Composite
;
71 import org
.eclipse
.swt
.widgets
.Display
;
72 import org
.eclipse
.swt
.widgets
.Event
;
73 import org
.eclipse
.swt
.widgets
.Listener
;
74 import org
.eclipse
.swt
.widgets
.ScrollBar
;
77 * Time graph control implementation
80 * @author Alvaro Sanchez-Leon
81 * @author Patrick Tasse
83 public class TimeGraphControl
extends TimeGraphBaseControl
implements FocusListener
, KeyListener
, MouseMoveListener
, MouseListener
, MouseWheelListener
, ControlListener
, SelectionListener
, MouseTrackListener
, TraverseListener
, ISelectionProvider
, MenuDetectListener
, ITmfTimeGraphDrawingHelper
{
86 /** Max scrollbar size */
87 public static final int H_SCROLLBAR_MAX
= Integer
.MAX_VALUE
- 1;
89 private static final int DRAG_NONE
= 0;
90 private static final int DRAG_TRACE_ITEM
= 1;
91 private static final int DRAG_SPLIT_LINE
= 2;
92 private static final int DRAG_ZOOM
= 3;
93 private static final int DRAG_SELECTION
= 4;
95 private static final int CUSTOM_ITEM_HEIGHT
= -1; // get item height from provider
97 private static final double ZOOM_FACTOR
= 1.5;
98 private static final double ZOOM_IN_FACTOR
= 0.8;
99 private static final double ZOOM_OUT_FACTOR
= 1.25;
101 private static final int SNAP_WIDTH
= 2;
103 private static final int NO_STATUS
= -1;
105 /** Resource manager */
106 private LocalResourceManager fResourceManager
= new LocalResourceManager(JFaceResources
.getResources());
108 /** Color map for event types */
109 private Color
[] fEventColorMap
= null;
111 private ITimeDataProvider fTimeProvider
;
112 private IStatusLineManager fStatusLineManager
= null;
113 private TimeGraphScale fTimeGraphScale
= null;
115 private boolean fIsInFocus
= false;
116 private boolean fMouseOverSplitLine
= false;
117 private int fGlobalItemHeight
= CUSTOM_ITEM_HEIGHT
;
118 private int fMinimumItemWidth
= 0;
119 private int fTopIndex
= 0;
120 private int fDragState
= DRAG_NONE
;
121 private int fDragButton
;
122 private int fDragX0
= 0;
123 private int fDragX
= 0;
124 private long fDragTime0
= 0; // used to preserve accuracy of modified selection
125 private int fIdealNameSpace
= 0;
126 private long fTime0bak
;
127 private long fTime1bak
;
128 private ITimeGraphPresentationProvider fTimeGraphProvider
= null;
129 private ItemData fItemData
= null;
130 private List
<SelectionListener
> fSelectionListeners
;
131 private final List
<ISelectionChangedListener
> fSelectionChangedListeners
= new ArrayList
<ISelectionChangedListener
>();
132 private final List
<ITimeGraphTreeListener
> fTreeListeners
= new ArrayList
<ITimeGraphTreeListener
>();
133 private final List
<MenuDetectListener
> fTimeGraphEntryMenuListeners
= new ArrayList
<MenuDetectListener
>();
134 private final List
<MenuDetectListener
> fTimeEventMenuListeners
= new ArrayList
<MenuDetectListener
>();
135 private final Cursor fDragCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_HAND
);
136 private final Cursor fResizeCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_IBEAM
);
137 private final Cursor fWaitCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_WAIT
);
138 private final Cursor fZoomCursor
= Display
.getDefault().getSystemCursor(SWT
.CURSOR_SIZEWE
);
139 private final List
<ViewerFilter
> fFilters
= new ArrayList
<ViewerFilter
>();
140 private MenuDetectEvent fPendingMenuDetectEvent
= null;
142 private int fBorderWidth
= 0;
143 private int fHeaderHeight
= 0;
145 private Listener fMouseScrollFilterListener
;
147 private MouseScrollNotifier fMouseScrollNotifier
;
148 private final Object fMouseScrollNotifierLock
= new Object();
150 private class MouseScrollNotifier
extends Thread
{
151 private static final long DELAY
= 400L;
152 private static final long POLLING_INTERVAL
= 10L;
153 private long fLastScrollTime
= Long
.MAX_VALUE
;
157 while ((System
.currentTimeMillis() - fLastScrollTime
) < DELAY
) {
159 Thread
.sleep(POLLING_INTERVAL
);
160 } catch (Exception e
) {
164 if (!isInterrupted()) {
165 Display
.getDefault().asyncExec(new Runnable() {
171 fTimeProvider
.notifyStartFinishTime();
175 synchronized (fMouseScrollNotifierLock
) {
176 fMouseScrollNotifier
= null;
180 public void mouseScrolled() {
181 fLastScrollTime
= System
.currentTimeMillis();
186 * Standard constructor
189 * The parent composite object
191 * The color scheme to use
193 public TimeGraphControl(Composite parent
, TimeGraphColorScheme colors
) {
195 super(parent
, colors
, SWT
.NO_BACKGROUND
| SWT
.H_SCROLL
| SWT
.DOUBLE_BUFFERED
);
197 fItemData
= new ItemData();
199 addFocusListener(this);
200 addMouseListener(this);
201 addMouseMoveListener(this);
202 addMouseTrackListener(this);
203 addMouseWheelListener(this);
204 addTraverseListener(this);
205 addKeyListener(this);
206 addControlListener(this);
207 addMenuDetectListener(this);
208 ScrollBar scrollHor
= getHorizontalBar();
210 if (scrollHor
!= null) {
211 scrollHor
.addSelectionListener(this);
216 public void dispose() {
218 fResourceManager
.dispose();
222 * Sets the timegraph provider used by this timegraph viewer.
224 * @param timeGraphProvider the timegraph provider
226 public void setTimeGraphProvider(ITimeGraphPresentationProvider timeGraphProvider
) {
227 fTimeGraphProvider
= timeGraphProvider
;
229 if (timeGraphProvider
instanceof ITimeGraphPresentationProvider2
) {
230 ((ITimeGraphPresentationProvider2
) timeGraphProvider
).setDrawingHelper(this);
233 if (fEventColorMap
!= null) {
234 for (Color color
: fEventColorMap
) {
235 fResourceManager
.destroyColor(color
.getRGB());
238 StateItem
[] stateItems
= fTimeGraphProvider
.getStateTable();
239 if (stateItems
!= null) {
240 fEventColorMap
= new Color
[stateItems
.length
];
241 for (int i
= 0; i
< stateItems
.length
; i
++) {
242 fEventColorMap
[i
] = fResourceManager
.createColor(stateItems
[i
].getStateColor());
245 fEventColorMap
= new Color
[] { };
250 * Assign the given time provider
252 * @param timeProvider
255 public void setTimeProvider(ITimeDataProvider timeProvider
) {
256 fTimeProvider
= timeProvider
;
262 * Assign the status line manager
264 * @param statusLineManager
265 * The status line manager, or null to disable status line messages
268 public void setStatusLineManager(IStatusLineManager statusLineManager
) {
269 if (fStatusLineManager
!= null && statusLineManager
== null) {
270 fStatusLineManager
.setMessage(""); //$NON-NLS-1$
272 fStatusLineManager
= statusLineManager
;
276 * Assign the time graph scale
278 * @param timeGraphScale
279 * The time graph scale
282 public void setTimeGraphScale(TimeGraphScale timeGraphScale
) {
283 fTimeGraphScale
= timeGraphScale
;
287 * Add a selection listener
290 * The listener to add
292 public void addSelectionListener(SelectionListener listener
) {
293 if (listener
== null) {
294 SWT
.error(SWT
.ERROR_NULL_ARGUMENT
);
296 if (null == fSelectionListeners
) {
297 fSelectionListeners
= new ArrayList
<SelectionListener
>();
299 fSelectionListeners
.add(listener
);
303 * Remove a selection listener
306 * The listener to remove
308 public void removeSelectionListener(SelectionListener listener
) {
309 if (null != fSelectionListeners
) {
310 fSelectionListeners
.remove(listener
);
315 * Selection changed callback
317 public void fireSelectionChanged() {
318 if (null != fSelectionListeners
) {
319 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
320 while (it
.hasNext()) {
321 SelectionListener listener
= it
.next();
322 listener
.widgetSelected(null);
328 * Default selection callback
330 public void fireDefaultSelection() {
331 if (null != fSelectionListeners
) {
332 Iterator
<SelectionListener
> it
= fSelectionListeners
.iterator();
333 while (it
.hasNext()) {
334 SelectionListener listener
= it
.next();
335 listener
.widgetDefaultSelected(null);
341 * Get the traces in the model
343 * @return The array of traces
345 public ITimeGraphEntry
[] getTraces() {
346 return fItemData
.getTraces();
350 * Get the on/off trace filters
352 * @return The array of filters
354 public boolean[] getTraceFilter() {
355 return fItemData
.getTraceFilter();
359 * Refresh the data for the thing
361 public void refreshData() {
362 fItemData
.refreshData();
368 * Refresh data for the given traces
371 * The traces to refresh
373 public void refreshData(ITimeGraphEntry
[] traces
) {
374 fItemData
.refreshData(traces
);
380 * Refresh the links (arrows) of this widget
382 * @param events The link events to refresh
385 public void refreshArrows(List
<ILinkEvent
> events
) {
386 fItemData
.refreshArrows(events
);
390 * Adjust the scoll bars
392 public void adjustScrolls() {
393 if (null == fTimeProvider
) {
394 getHorizontalBar().setValues(0, 1, 1, 1, 1, 1);
400 long time0
= fTimeProvider
.getTime0();
401 long time1
= fTimeProvider
.getTime1();
403 long timeMin
= fTimeProvider
.getMinTime();
404 long timeMax
= fTimeProvider
.getMaxTime();
406 long delta
= timeMax
- timeMin
;
409 int thumb
= H_SCROLLBAR_MAX
;
412 // Thumb size (page size)
413 thumb
= Math
.max(1, (int) (H_SCROLLBAR_MAX
* ((double) (time1
- time0
) / delta
)));
414 // At the beginning of visible window
415 timePos
= (int) (H_SCROLLBAR_MAX
* ((double) (time0
- timeMin
) / delta
));
418 // position, minimum, maximum, thumb size, increment (half page)t, page
419 // increment size (full page)
420 getHorizontalBar().setValues(timePos
, 0, H_SCROLLBAR_MAX
, thumb
, Math
.max(1, thumb
/ 2), Math
.max(2, thumb
));
423 boolean ensureVisibleItem(int idx
, boolean redraw
) {
424 boolean changed
= false;
427 for (index
= 0; index
< fItemData
.fExpandedItems
.length
; index
++) {
428 if (fItemData
.fExpandedItems
[index
].fSelected
) {
433 if (index
>= fItemData
.fExpandedItems
.length
) {
436 if (index
< fTopIndex
) {
443 int page
= countPerPage();
444 if (index
>= fTopIndex
+ page
) {
445 setTopIndex(index
- page
+ 1);
456 * Assign the given index as the top one
461 public void setTopIndex(int idx
) {
462 int index
= Math
.min(idx
, fItemData
.fExpandedItems
.length
- countPerPage());
463 index
= Math
.max(0, index
);
469 * Set the expanded state of a given entry
474 * True if expanded, false if collapsed
476 public void setExpandedState(ITimeGraphEntry entry
, boolean expanded
) {
477 Item item
= fItemData
.findItem(entry
);
478 if (item
!= null && item
.fExpanded
!= expanded
) {
479 item
.fExpanded
= expanded
;
480 fItemData
.updateExpandedItems();
486 * Collapses all nodes of the viewer's tree, starting with the root.
490 public void collapseAll() {
491 for (Item item
: fItemData
.fItems
) {
492 item
.fExpanded
= false;
494 fItemData
.updateExpandedItems();
499 * Expands all nodes of the viewer's tree, starting with the root.
503 public void expandAll() {
504 for (Item item
: fItemData
.fItems
) {
505 item
.fExpanded
= true;
507 fItemData
.updateExpandedItems();
512 * Add a tree listener
515 * The listener to add
517 public void addTreeListener(ITimeGraphTreeListener listener
) {
518 if (!fTreeListeners
.contains(listener
)) {
519 fTreeListeners
.add(listener
);
524 * Remove a tree listener
527 * The listener to remove
529 public void removeTreeListener(ITimeGraphTreeListener listener
) {
530 if (fTreeListeners
.contains(listener
)) {
531 fTreeListeners
.remove(listener
);
536 * Tree event callback
541 * The expanded state (true for expanded, false for collapsed)
543 public void fireTreeEvent(ITimeGraphEntry entry
, boolean expanded
) {
544 TimeGraphTreeExpansionEvent event
= new TimeGraphTreeExpansionEvent(this, entry
);
545 for (ITimeGraphTreeListener listener
: fTreeListeners
) {
547 listener
.treeExpanded(event
);
549 listener
.treeCollapsed(event
);
555 * Add a menu listener on {@link ITimeGraphEntry}s
557 * The listener to add
560 public void addTimeGraphEntryMenuListener(MenuDetectListener listener
) {
561 if (!fTimeGraphEntryMenuListeners
.contains(listener
)) {
562 fTimeGraphEntryMenuListeners
.add(listener
);
567 * Remove a menu listener on {@link ITimeGraphEntry}s
570 * The listener to remove
573 public void removeTimeGraphEntryMenuListener(MenuDetectListener listener
) {
574 if (fTimeGraphEntryMenuListeners
.contains(listener
)) {
575 fTimeGraphEntryMenuListeners
.remove(listener
);
580 * Menu event callback on {@link ITimeGraphEntry}s
583 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeGraphEntry}
585 private void fireMenuEventOnTimeGraphEntry(MenuDetectEvent event
) {
586 for (MenuDetectListener listener
: fTimeGraphEntryMenuListeners
) {
587 listener
.menuDetected(event
);
592 * Add a menu listener on {@link ITimeEvent}s
595 * The listener to add
598 public void addTimeEventMenuListener(MenuDetectListener listener
) {
599 if (!fTimeEventMenuListeners
.contains(listener
)) {
600 fTimeEventMenuListeners
.add(listener
);
605 * Remove a menu listener on {@link ITimeEvent}s
608 * The listener to remove
611 public void removeTimeEventMenuListener(MenuDetectListener listener
) {
612 if (fTimeEventMenuListeners
.contains(listener
)) {
613 fTimeEventMenuListeners
.remove(listener
);
618 * Menu event callback on {@link ITimeEvent}s
621 * The MenuDetectEvent, with field {@link TypedEvent#data} set to the selected {@link ITimeEvent}
623 private void fireMenuEventOnTimeEvent(MenuDetectEvent event
) {
624 for (MenuDetectListener listener
: fTimeEventMenuListeners
) {
625 listener
.menuDetected(event
);
629 @SuppressWarnings("deprecation")
631 public ISelection
getSelection() {
632 TimeGraphSelection sel
= new TimeGraphSelection();
633 ITimeGraphEntry trace
= getSelectedTrace();
634 if (null != trace
&& null != fTimeProvider
) {
636 if (fTimeProvider
instanceof ITimeDataProvider2
) {
637 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
639 selectedTime
= fTimeProvider
.getSelectedTime();
641 ITimeEvent event
= Utils
.findEvent(trace
, selectedTime
, 0);
652 * Get the selection object
654 * @return The selection
656 public ISelection
getSelectionTrace() {
657 TimeGraphSelection sel
= new TimeGraphSelection();
658 ITimeGraphEntry trace
= getSelectedTrace();
666 * Enable/disable one of the traces in the model
669 * 1 to enable it, -1 to disable. The method returns immediately
670 * if another value is used.
672 public void selectTrace(int n
) {
673 if ((n
!= 1) && (n
!= -1)) {
677 boolean changed
= false;
678 int lastSelection
= -1;
679 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
680 Item item
= fItemData
.fExpandedItems
[i
];
681 if (item
.fSelected
) {
683 if ((1 == n
) && (i
< fItemData
.fExpandedItems
.length
- 1)) {
684 item
.fSelected
= false;
685 item
= fItemData
.fExpandedItems
[i
+ 1];
686 item
.fSelected
= true;
688 } else if ((-1 == n
) && (i
> 0)) {
689 item
.fSelected
= false;
690 item
= fItemData
.fExpandedItems
[i
- 1];
691 item
.fSelected
= true;
698 if (lastSelection
< 0 && fItemData
.fExpandedItems
.length
> 0) {
699 Item item
= fItemData
.fExpandedItems
[0];
700 item
.fSelected
= true;
705 ensureVisibleItem(-1, false);
707 fireSelectionChanged();
715 * 1 for next event, -1 for previous event
717 public void selectEvent(int n
) {
718 if (null == fTimeProvider
) {
721 ITimeGraphEntry trace
= getSelectedTrace();
726 if (fTimeProvider
instanceof ITimeDataProvider2
) {
727 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
729 @SuppressWarnings("deprecation")
730 long time
= fTimeProvider
.getSelectedTime();
733 long endTime
= fTimeProvider
.getEndTime();
734 ITimeEvent nextEvent
;
735 if (-1 == n
&& selectedTime
> endTime
) {
736 nextEvent
= Utils
.findEvent(trace
, selectedTime
, 0);
738 nextEvent
= Utils
.findEvent(trace
, selectedTime
, n
);
740 if (null == nextEvent
&& -1 == n
) {
741 nextEvent
= Utils
.getFirstEvent(trace
);
743 if (null != nextEvent
) {
744 long nextTime
= nextEvent
.getTime();
745 // If last event detected e.g. going back or not moving to a next
747 if (nextTime
<= selectedTime
&& n
== 1) {
748 // Select to the end of this last event
749 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
750 // but not beyond the end of the trace
751 if (nextTime
> endTime
) {
754 } else if (n
== -1 && nextEvent
.getTime() + nextEvent
.getDuration() < selectedTime
) {
755 // for previous event go to its end time unless we were already there
756 nextTime
= nextEvent
.getTime() + nextEvent
.getDuration();
758 fTimeProvider
.setSelectedTimeNotify(nextTime
, true);
759 fireSelectionChanged();
761 fTimeProvider
.setSelectedTimeNotify(endTime
, true);
762 fireSelectionChanged();
767 * Select the next event
769 public void selectNextEvent() {
771 // Notify if visible time window has been adjusted
772 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
776 * Select the previous event
778 public void selectPrevEvent() {
780 // Notify if visible time window has been adjusted
781 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getTime0(), fTimeProvider
.getTime1());
785 * Select the next trace
787 public void selectNextTrace() {
792 * Select the previous trace
794 public void selectPrevTrace() {
799 * Zoom based on mouse cursor location with mouse scrolling
801 * @param zoomIn true to zoom in, false to zoom out
803 public void zoom(boolean zoomIn
) {
804 int globalX
= getDisplay().getCursorLocation().x
;
805 Point p
= toControl(globalX
, 0);
806 int nameSpace
= fTimeProvider
.getNameSpace();
807 int timeSpace
= fTimeProvider
.getTimeSpace();
808 int xPos
= Math
.max(nameSpace
, Math
.min(nameSpace
+ timeSpace
, p
.x
));
809 long time0
= fTimeProvider
.getTime0();
810 long time1
= fTimeProvider
.getTime1();
811 long interval
= time1
- time0
;
814 } // to allow getting out of single point interval
817 newInterval
= Math
.max(Math
.round(interval
* ZOOM_IN_FACTOR
), fTimeProvider
.getMinTimeInterval());
819 newInterval
= (long) Math
.ceil(interval
* ZOOM_OUT_FACTOR
);
821 long center
= time0
+ Math
.round(((double) (xPos
- nameSpace
) / timeSpace
* interval
));
822 long newTime0
= center
- Math
.round((double) newInterval
* (center
- time0
) / interval
);
823 long newTime1
= newTime0
+ newInterval
;
824 fTimeProvider
.setStartFinishTime(newTime0
, newTime1
);
825 synchronized (fMouseScrollNotifierLock
) {
826 if (fMouseScrollNotifier
== null) {
827 fMouseScrollNotifier
= new MouseScrollNotifier();
828 fMouseScrollNotifier
.start();
830 fMouseScrollNotifier
.mouseScrolled();
835 * zoom in using single click
837 public void zoomIn() {
838 long prevTime0
= fTimeProvider
.getTime0();
839 long prevTime1
= fTimeProvider
.getTime1();
840 long prevRange
= prevTime1
- prevTime0
;
841 if (prevRange
== 0) {
845 if (fTimeProvider
instanceof ITimeDataProvider2
) {
846 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
847 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
849 @SuppressWarnings("deprecation")
850 long selectedTime
= fTimeProvider
.getSelectedTime();
851 selTime
= selectedTime
;
853 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
854 selTime
= (prevTime0
+ prevTime1
) / 2;
856 long time0
= selTime
- (long) ((selTime
- prevTime0
) / ZOOM_FACTOR
);
857 long time1
= selTime
+ (long) ((prevTime1
- selTime
) / ZOOM_FACTOR
);
859 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
861 if (inaccuracy
> 0 && inaccuracy
< 100) {
862 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
866 long min
= fTimeProvider
.getMinTimeInterval();
867 if ((time1
- time0
) < min
) {
868 time0
= selTime
- (selTime
- prevTime0
) * min
/ prevRange
;
872 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
876 * zoom out using single click
878 public void zoomOut() {
879 long prevTime0
= fTimeProvider
.getTime0();
880 long prevTime1
= fTimeProvider
.getTime1();
882 if (fTimeProvider
instanceof ITimeDataProvider2
) {
883 ITimeDataProvider2 provider
= ((ITimeDataProvider2
) fTimeProvider
);
884 selTime
= (provider
.getSelectionEnd() + provider
.getSelectionBegin()) / 2;
886 @SuppressWarnings("deprecation")
887 long selectedTime
= fTimeProvider
.getSelectedTime();
888 selTime
= selectedTime
;
890 if (selTime
<= prevTime0
|| selTime
>= prevTime1
) {
891 selTime
= (prevTime0
+ prevTime1
) / 2;
893 long time0
= (long) (selTime
- (selTime
- prevTime0
) * ZOOM_FACTOR
);
894 long time1
= (long) (selTime
+ (prevTime1
- selTime
) * ZOOM_FACTOR
);
896 long inaccuracy
= (fTimeProvider
.getMaxTime() - fTimeProvider
.getMinTime()) - (time1
- time0
);
897 if (inaccuracy
> 0 && inaccuracy
< 100) {
898 fTimeProvider
.setStartFinishTimeNotify(fTimeProvider
.getMinTime(), fTimeProvider
.getMaxTime());
902 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
906 * Return the currently selected trace
908 * @return The entry matching the trace
910 public ITimeGraphEntry
getSelectedTrace() {
911 ITimeGraphEntry trace
= null;
912 int idx
= getSelectedIndex();
914 trace
= fItemData
.fExpandedItems
[idx
].fTrace
;
920 * Retrieve the index of the currently selected item
924 public int getSelectedIndex() {
926 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
927 Item item
= fItemData
.fExpandedItems
[i
];
928 if (item
.fSelected
) {
936 boolean toggle(int idx
) {
937 boolean toggled
= false;
938 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
939 Item item
= fItemData
.fExpandedItems
[idx
];
940 if (item
.fHasChildren
) {
941 item
.fExpanded
= !item
.fExpanded
;
942 fItemData
.updateExpandedItems();
946 fireTreeEvent(item
.fTrace
, item
.fExpanded
);
952 int getItemIndexAtY(int y
) {
956 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
958 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
959 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
966 int idx
= y
/ fGlobalItemHeight
;
968 if (idx
< fItemData
.fExpandedItems
.length
) {
974 boolean isOverSplitLine(int x
) {
975 if (x
< 0 || null == fTimeProvider
) {
978 int nameWidth
= fTimeProvider
.getNameSpace();
979 return Math
.abs(x
- nameWidth
) < SNAP_WIDTH
;
982 ITimeGraphEntry
getEntry(Point pt
) {
983 int idx
= getItemIndexAtY(pt
.y
);
984 return idx
>= 0 ? fItemData
.fExpandedItems
[idx
].fTrace
: null;
991 public int getXForTime(long time
) {
992 if (null == fTimeProvider
) {
995 long time0
= fTimeProvider
.getTime0();
996 long time1
= fTimeProvider
.getTime1();
997 int width
= getCtrlSize().x
;
998 int nameSpace
= fTimeProvider
.getNameSpace();
999 double pixelsPerNanoSec
= (width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1000 int x
= getBounds().x
+ nameSpace
+ (int) ((time
- time0
) * pixelsPerNanoSec
);
1008 public long getTimeAtX(int coord
) {
1009 if (null == fTimeProvider
) {
1013 Point size
= getCtrlSize();
1014 long time0
= fTimeProvider
.getTime0();
1015 long time1
= fTimeProvider
.getTime1();
1016 int nameWidth
= fTimeProvider
.getNameSpace();
1017 final int x
= coord
- nameWidth
;
1018 int timeWidth
= size
.x
- nameWidth
- RIGHT_MARGIN
;
1019 if (x
>= 0 && size
.x
>= nameWidth
) {
1020 if (time1
- time0
> timeWidth
) {
1021 // nanosecond smaller than one pixel: use the first integer nanosecond of this pixel's time range
1022 hitTime
= time0
+ (long) Math
.ceil((time1
- time0
) * ((double) x
/ timeWidth
));
1024 // nanosecond greater than one pixel: use the nanosecond that covers this pixel start position
1025 hitTime
= time0
+ (long) Math
.floor((time1
- time0
) * ((double) x
/ timeWidth
));
1031 void selectItem(int idx
, boolean addSelection
) {
1032 boolean changed
= false;
1034 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
1035 Item item
= fItemData
.fExpandedItems
[idx
];
1036 changed
= !item
.fSelected
;
1037 item
.fSelected
= true;
1040 for (int i
= 0; i
< fItemData
.fExpandedItems
.length
; i
++) {
1041 Item item
= fItemData
.fExpandedItems
[i
];
1042 if ((i
== idx
&& !item
.fSelected
) || (idx
== -1 && item
.fSelected
)) {
1045 item
.fSelected
= i
== idx
;
1048 changed
|= ensureVisibleItem(idx
, true);
1055 * Callback for item selection
1058 * The entry matching the trace
1059 * @param addSelection
1060 * If the selection is added or removed
1062 public void selectItem(ITimeGraphEntry trace
, boolean addSelection
) {
1063 int idx
= fItemData
.findItemIndex(trace
);
1064 selectItem(idx
, addSelection
);
1068 * Retrieve the number of entries shown per page.
1072 public int countPerPage() {
1073 int height
= getCtrlSize().y
;
1075 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1077 for (int idx
= fTopIndex
; idx
< fItemData
.fExpandedItems
.length
; idx
++) {
1078 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1079 if (ySum
>= height
) {
1084 for (int idx
= fTopIndex
- 1; idx
>= 0; idx
--) {
1085 ySum
+= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1086 if (ySum
>= height
) {
1094 count
= height
/ fGlobalItemHeight
;
1100 * Get the index of the top element
1104 public int getTopIndex() {
1109 * Get the number of expanded items
1111 * @return The count of expanded items
1113 public int getExpandedElementCount() {
1114 return fItemData
.fExpandedItems
.length
;
1118 * Get an array of all expanded elements
1120 * @return The expanded elements
1122 public ITimeGraphEntry
[] getExpandedElements() {
1123 ArrayList
<ITimeGraphEntry
> elements
= new ArrayList
<ITimeGraphEntry
>();
1124 for (Item item
: fItemData
.fExpandedItems
) {
1125 elements
.add(item
.fTrace
);
1127 return elements
.toArray(new ITimeGraphEntry
[0]);
1130 Point
getCtrlSize() {
1131 Point size
= getSize();
1132 if (getHorizontalBar().isVisible()) {
1133 size
.y
-= getHorizontalBar().getSize().y
;
1138 Rectangle
getNameRect(Rectangle bound
, int idx
, int nameWidth
) {
1140 int y
= bound
.y
+ (idx
- fTopIndex
) * fGlobalItemHeight
;
1141 int width
= nameWidth
;
1142 int height
= fGlobalItemHeight
;
1143 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1145 for (int i
= fTopIndex
; i
< idx
; i
++) {
1146 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1149 height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1151 return new Rectangle(x
, y
, width
, height
);
1154 Rectangle
getStatesRect(Rectangle bound
, int idx
, int nameWidth
) {
1155 int x
= bound
.x
+ nameWidth
;
1156 int y
= bound
.y
+ (idx
- fTopIndex
) * fGlobalItemHeight
;
1157 int width
= bound
.width
- x
;
1158 int height
= fGlobalItemHeight
;
1159 if (fGlobalItemHeight
== CUSTOM_ITEM_HEIGHT
) {
1161 for (int i
= fTopIndex
; i
< idx
; i
++) {
1162 ySum
+= fItemData
.fExpandedItems
[i
].fItemHeight
;
1165 height
= fItemData
.fExpandedItems
[idx
].fItemHeight
;
1167 return new Rectangle(x
, y
, width
, height
);
1171 void paint(Rectangle bounds
, PaintEvent e
) {
1173 gc
.setBackground(getColorScheme().getColor(TimeGraphColorScheme
.BACKGROUND
));
1174 drawBackground(gc
, bounds
.x
, bounds
.y
, bounds
.width
, bounds
.height
);
1176 if (bounds
.width
< 2 || bounds
.height
< 2 || null == fTimeProvider
) {
1180 fIdealNameSpace
= 0;
1181 int nameSpace
= fTimeProvider
.getNameSpace();
1183 // draw empty name space background
1184 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1185 drawBackground(gc
, bounds
.x
, bounds
.y
, nameSpace
, bounds
.height
);
1188 drawItems(bounds
, fTimeProvider
, fItemData
.fExpandedItems
, fTopIndex
, nameSpace
, gc
);
1189 drawLinks(bounds
, fTimeProvider
, fItemData
.fLinks
, nameSpace
, gc
);
1190 fTimeGraphProvider
.postDrawControl(bounds
, gc
);
1192 int alpha
= gc
.getAlpha();
1195 long time0
= fTimeProvider
.getTime0();
1196 long time1
= fTimeProvider
.getTime1();
1197 long selectionBegin
;
1199 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1200 selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1201 selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1203 @SuppressWarnings("deprecation")
1204 long selectedTime
= fTimeProvider
.getSelectedTime();
1205 selectionBegin
= selectedTime
;
1206 selectionEnd
= selectedTime
;
1208 double pixelsPerNanoSec
= (bounds
.width
- nameSpace
<= RIGHT_MARGIN
) ?
0 : (double) (bounds
.width
- nameSpace
- RIGHT_MARGIN
) / (time1
- time0
);
1209 int x0
= bounds
.x
+ nameSpace
+ (int) ((selectionBegin
- time0
) * pixelsPerNanoSec
);
1210 int x1
= bounds
.x
+ nameSpace
+ (int) ((selectionEnd
- time0
) * pixelsPerNanoSec
);
1212 // draw selection lines
1213 if (fDragState
!= DRAG_SELECTION
) {
1214 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1215 if (x0
>= nameSpace
&& x0
< bounds
.x
+ bounds
.width
) {
1216 gc
.drawLine(x0
, bounds
.y
, x0
, bounds
.y
+ bounds
.height
);
1219 if (x1
>= nameSpace
&& x1
< bounds
.x
+ bounds
.width
) {
1220 gc
.drawLine(x1
, bounds
.y
, x1
, bounds
.y
+ bounds
.height
);
1225 // draw selection background
1226 if (selectionBegin
!= 0 && selectionEnd
!= 0 && fDragState
!= DRAG_SELECTION
) {
1227 x0
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x0
));
1228 x1
= Math
.max(nameSpace
, Math
.min(bounds
.x
+ bounds
.width
, x1
));
1229 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1231 gc
.fillRectangle(new Rectangle(x0
+ 1, bounds
.y
, x1
- x0
- 1, bounds
.height
));
1232 } else if (x0
- x1
> 1) {
1233 gc
.fillRectangle(new Rectangle(x1
+ 1, bounds
.y
, x0
- x1
- 1, bounds
.height
));
1237 // draw drag selection background
1238 if (fDragState
== DRAG_ZOOM
|| fDragState
== DRAG_SELECTION
) {
1239 gc
.setBackground(getColorScheme().getBkColor(false, false, true));
1240 if (fDragX0
< fDragX
) {
1241 gc
.fillRectangle(new Rectangle(fDragX0
, bounds
.y
, fDragX
- fDragX0
, bounds
.height
));
1242 } else if (fDragX0
> fDragX
) {
1243 gc
.fillRectangle(new Rectangle(fDragX
, bounds
.y
, fDragX0
- fDragX
, bounds
.height
));
1248 if (DRAG_SPLIT_LINE
== fDragState
) {
1249 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.BLACK
));
1250 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1251 } else if (DRAG_ZOOM
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1252 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.TOOL_FOREGROUND
));
1253 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1254 if (fDragX
!= fDragX0
) {
1255 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1257 } else if (DRAG_SELECTION
== fDragState
&& Math
.max(fDragX
, fDragX0
) > nameSpace
) {
1258 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.SELECTED_TIME
));
1259 gc
.drawLine(fDragX0
, bounds
.y
, fDragX0
, bounds
.y
+ bounds
.height
- 1);
1260 if (fDragX
!= fDragX0
) {
1261 gc
.drawLine(fDragX
, bounds
.y
, fDragX
, bounds
.y
+ bounds
.height
- 1);
1263 } else if (DRAG_NONE
== fDragState
&& fMouseOverSplitLine
&& fTimeProvider
.getNameSpace() > 0) {
1264 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.RED
));
1265 gc
.drawLine(bounds
.x
+ nameSpace
, bounds
.y
, bounds
.x
+ nameSpace
, bounds
.y
+ bounds
.height
- 1);
1272 * Draw many items at once
1275 * The rectangle of the area
1276 * @param timeProvider
1279 * The array items to draw
1281 * The index of the first element to draw
1283 * The width reserved for the names
1285 * Reference to the SWT GC object
1287 public void drawItems(Rectangle bounds
, ITimeDataProvider timeProvider
,
1288 Item
[] items
, int topIndex
, int nameSpace
, GC gc
) {
1289 for (int i
= topIndex
; i
< items
.length
; i
++) {
1290 Item item
= items
[i
];
1291 drawItem(item
, bounds
, timeProvider
, i
, nameSpace
, gc
);
1298 * @param item the item to draw
1299 * @param bounds the container rectangle
1300 * @param timeProvider Time provider
1301 * @param i the item index
1302 * @param nameSpace the name space
1303 * @param gc Graphics context
1305 protected void drawItem(Item item
, Rectangle bounds
, ITimeDataProvider timeProvider
, int i
, int nameSpace
, GC gc
) {
1306 ITimeGraphEntry entry
= item
.fTrace
;
1307 long time0
= timeProvider
.getTime0();
1308 long time1
= timeProvider
.getTime1();
1310 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1311 selectedTime
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1313 @SuppressWarnings("deprecation")
1314 long time
= fTimeProvider
.getSelectedTime();
1315 selectedTime
= time
;
1318 Rectangle nameRect
= getNameRect(bounds
, i
, nameSpace
);
1319 if (nameRect
.y
>= bounds
.y
+ bounds
.height
) {
1323 if (! item
.fTrace
.hasTimeEvents()) {
1324 Rectangle statesRect
= getStatesRect(bounds
, i
, nameSpace
);
1325 nameRect
.width
+= statesRect
.width
;
1326 drawName(item
, nameRect
, gc
);
1328 drawName(item
, nameRect
, gc
);
1330 Rectangle rect
= getStatesRect(bounds
, i
, nameSpace
);
1331 if (rect
.isEmpty()) {
1332 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1335 if (time1
<= time0
) {
1336 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1337 gc
.fillRectangle(rect
);
1338 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1342 // Initialize _rect1 to same values as enclosing rectangle rect
1343 Rectangle stateRect
= Utils
.clone(rect
);
1344 boolean selected
= item
.fSelected
;
1345 // K pixels per second
1346 double pixelsPerNanoSec
= (rect
.width
<= RIGHT_MARGIN
) ?
0 : (double) (rect
.width
- RIGHT_MARGIN
) / (time1
- time0
);
1348 if (item
.fTrace
.hasTimeEvents()) {
1349 fillSpace(rect
, gc
, selected
);
1350 // Drawing rectangle is smaller than reserved space
1352 stateRect
.height
-= 6;
1354 long maxDuration
= (timeProvider
.getTimeSpace() == 0) ? Long
.MAX_VALUE
: 1 * (time1
- time0
) / timeProvider
.getTimeSpace();
1355 Iterator
<ITimeEvent
> iterator
= entry
.getTimeEventsIterator(time0
, time1
, maxDuration
);
1358 while (iterator
.hasNext()) {
1359 ITimeEvent event
= iterator
.next();
1360 int x
= rect
.x
+ (int) ((event
.getTime() - time0
) * pixelsPerNanoSec
);
1361 int xEnd
= rect
.x
+ (int) ((event
.getTime() + event
.getDuration() - time0
) * pixelsPerNanoSec
);
1362 if (x
>= rect
.x
+ rect
.width
|| xEnd
< rect
.x
) {
1363 // event is out of bounds
1366 xEnd
= Math
.min(rect
.x
+ rect
.width
, xEnd
);
1367 stateRect
.x
= Math
.max(rect
.x
, x
);
1368 stateRect
.width
= Math
.max(0, xEnd
- stateRect
.x
+ 1);
1369 if (stateRect
.x
== lastX
) {
1370 stateRect
.width
-= 1;
1371 if (stateRect
.width
> 0) {
1372 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1373 gc
.drawPoint(stateRect
.x
, stateRect
.y
- 2);
1377 boolean timeSelected
= selectedTime
>= event
.getTime() && selectedTime
< event
.getTime() + event
.getDuration();
1378 if (drawState(getColorScheme(), event
, stateRect
, gc
, selected
, timeSelected
)) {
1383 fTimeGraphProvider
.postDrawEntry(entry
, rect
, gc
);
1390 * The rectangle of the area
1391 * @param timeProvider
1394 * The array items to draw
1396 * The width reserved for the names
1398 * Reference to the SWT GC object
1401 public void drawLinks(Rectangle bounds
, ITimeDataProvider timeProvider
,
1402 List
<ILinkEvent
> links
, int nameSpace
, GC gc
) {
1403 for (ILinkEvent event
: links
) {
1404 drawLink(event
, bounds
, timeProvider
, nameSpace
, gc
);
1409 * Draws the link type events of this item
1414 * the container rectangle
1415 * @param timeProvider
1423 protected void drawLink(ILinkEvent event
, Rectangle bounds
, ITimeDataProvider timeProvider
, int nameSpace
, GC gc
) {
1424 int srcIndex
= fItemData
.findItemIndex(event
.getEntry());
1425 int destIndex
= fItemData
.findItemIndex(event
.getDestinationEntry());
1427 if ((srcIndex
== -1) || (destIndex
== -1)) {
1431 Rectangle src
= getStatesRect(bounds
, srcIndex
, nameSpace
);
1432 Rectangle dst
= getStatesRect(bounds
, destIndex
, nameSpace
);
1434 int x0
= getXForTime(event
.getTime());
1435 int x1
= getXForTime(event
.getTime() + event
.getDuration());
1436 int y0
= src
.y
+ src
.height
/ 2;
1437 int y1
= dst
.y
+ dst
.height
/ 2;
1438 drawArrow(getColorScheme(), event
, new Rectangle(x0
, y0
, x1
- x0
, y1
- y0
), gc
);
1442 * Draw the state (color fill)
1447 * Time event for which we're drawing the state
1452 * @return true if the state was drawn
1455 protected boolean drawArrow(TimeGraphColorScheme colors
, ITimeEvent event
,
1456 Rectangle rect
, GC gc
) {
1458 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1462 boolean visible
= ((rect
.height
== 0) && (rect
.width
== 0)) ?
false : true;
1465 Color stateColor
= null;
1466 if (colorIdx
< fEventColorMap
.length
) {
1467 stateColor
= fEventColorMap
[colorIdx
];
1469 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1472 gc
.setForeground(stateColor
);
1473 gc
.setBackground(stateColor
);
1475 /* Draw the arrow */
1476 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
1477 drawArrowHead(rect
.x
, rect
.y
, rect
.x
+ rect
.width
, rect
.y
+ rect
.height
, gc
);
1480 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1485 * @author Francis Giraldeau
1488 * http://stackoverflow.com/questions/3010803/draw-arrow-on-line-algorithm
1490 * The algorithm was taken from this site, not the code itself
1492 private static void drawArrowHead(int x0
, int y0
, int x1
, int y1
, GC gc
)
1495 double cos
= 0.9510;
1496 double sin
= 0.3090;
1499 double len
= Math
.sqrt(lenx
* lenx
+ leny
* leny
);
1501 double dx
= factor
* lenx
/ len
;
1502 double dy
= factor
* leny
/ len
;
1503 int end1X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* -sin
)));
1504 int end1Y
= (int) Math
.round((y1
- (dx
* sin
+ dy
* cos
)));
1505 int end2X
= (int) Math
.round((x1
- (dx
* cos
+ dy
* sin
)));
1506 int end2Y
= (int) Math
.round((y1
- (dx
* -sin
+ dy
* cos
)));
1507 int[] arrow
= new int[] { x1
, y1
, end1X
, end1Y
, end2X
, end2Y
, x1
, y1
};
1508 gc
.fillPolygon(arrow
);
1512 * Draw the name of an item.
1517 * Where to draw the name
1521 protected void drawName(Item item
, Rectangle bounds
, GC gc
) {
1522 boolean hasTimeEvents
= item
.fTrace
.hasTimeEvents();
1523 if (! hasTimeEvents
) {
1524 gc
.setBackground(getColorScheme().getBkColorGroup(item
.fSelected
, fIsInFocus
));
1525 gc
.fillRectangle(bounds
);
1526 if (item
.fSelected
&& fIsInFocus
) {
1527 gc
.setForeground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, false));
1528 gc
.drawRectangle(bounds
.x
, bounds
.y
, bounds
.width
- 1, bounds
.height
- 1);
1531 gc
.setBackground(getColorScheme().getBkColor(item
.fSelected
, fIsInFocus
, true));
1532 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1533 gc
.fillRectangle(bounds
);
1536 // No name to be drawn
1537 if (fTimeProvider
.getNameSpace() == 0) {
1541 int leftMargin
= MARGIN
+ item
.fLevel
* EXPAND_SIZE
;
1542 if (item
.fHasChildren
) {
1543 gc
.setForeground(getColorScheme().getFgColorGroup(false, false));
1544 gc
.setBackground(getColorScheme().getBkColor(false, false, false));
1545 Rectangle rect
= Utils
.clone(bounds
);
1546 rect
.x
+= leftMargin
;
1547 rect
.y
+= (bounds
.height
- EXPAND_SIZE
) / 2;
1548 rect
.width
= EXPAND_SIZE
;
1549 rect
.height
= EXPAND_SIZE
;
1550 gc
.fillRectangle(rect
);
1551 gc
.drawRectangle(rect
.x
, rect
.y
, rect
.width
- 1, rect
.height
- 1);
1552 int midy
= rect
.y
+ rect
.height
/ 2;
1553 gc
.drawLine(rect
.x
+ 2, midy
, rect
.x
+ rect
.width
- 3, midy
);
1554 if (!item
.fExpanded
) {
1555 int midx
= rect
.x
+ rect
.width
/ 2;
1556 gc
.drawLine(midx
, rect
.y
+ 2, midx
, rect
.y
+ rect
.height
- 3);
1559 leftMargin
+= EXPAND_SIZE
+ MARGIN
;
1561 Image img
= fTimeGraphProvider
.getItemImage(item
.fTrace
);
1564 int imgHeight
= img
.getImageData().height
;
1565 int imgWidth
= img
.getImageData().width
;
1567 int y
= bounds
.y
+ (bounds
.height
- imgHeight
) / 2;
1568 gc
.drawImage(img
, x
, y
);
1569 leftMargin
+= imgWidth
+ MARGIN
;
1571 String name
= item
.fName
;
1572 Point size
= gc
.stringExtent(name
);
1573 if (fIdealNameSpace
< leftMargin
+ size
.x
+ MARGIN
) {
1574 fIdealNameSpace
= leftMargin
+ size
.x
+ MARGIN
;
1576 if (hasTimeEvents
) {
1577 // cut long string with "..."
1578 int width
= bounds
.width
- leftMargin
;
1580 while (size
.x
> width
&& name
.length() > 1) {
1582 name
= name
.substring(0, name
.length() - 1);
1583 size
= gc
.stringExtent(name
+ "..."); //$NON-NLS-1$
1586 name
+= "..."; //$NON-NLS-1$
1589 Rectangle rect
= Utils
.clone(bounds
);
1590 rect
.x
+= leftMargin
;
1591 rect
.width
-= leftMargin
;
1593 if (rect
.width
> 0) {
1594 rect
.y
+= (bounds
.height
- gc
.stringExtent(name
).y
) / 2;
1595 gc
.setForeground(getColorScheme().getFgColor(item
.fSelected
, fIsInFocus
));
1596 int textWidth
= Utils
.drawText(gc
, name
, rect
, true);
1597 leftMargin
+= textWidth
+ MARGIN
;
1600 if (hasTimeEvents
) {
1602 int x
= bounds
.x
+ leftMargin
;
1603 int width
= bounds
.width
- x
;
1604 int midy
= bounds
.y
+ bounds
.height
/ 2;
1605 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1606 gc
.drawLine(x
, midy
, x
+ width
, midy
);
1612 * Draw the state (color fill)
1617 * Time event for which we're drawing the state
1623 * Is this time event currently selected (so it appears
1625 * @param timeSelected
1626 * Is the timestamp currently selected
1627 * @return true if the state was drawn
1630 protected boolean drawState(TimeGraphColorScheme colors
, ITimeEvent event
,
1631 Rectangle rect
, GC gc
, boolean selected
, boolean timeSelected
) {
1633 int colorIdx
= fTimeGraphProvider
.getStateTableIndex(event
);
1634 if (colorIdx
< 0 && colorIdx
!= ITimeGraphPresentationProvider
.TRANSPARENT
) {
1637 boolean visible
= rect
.width
== 0 ?
false : true;
1640 if (colorIdx
== ITimeGraphPresentationProvider
.TRANSPARENT
) {
1641 // Only draw the top and bottom borders
1642 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1643 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1644 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1645 if (rect
.width
== 1) {
1646 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1647 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1651 Color stateColor
= null;
1652 if (colorIdx
< fEventColorMap
.length
) {
1653 stateColor
= fEventColorMap
[colorIdx
];
1655 stateColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
);
1658 boolean reallySelected
= timeSelected
&& selected
;
1659 // fill all rect area
1660 gc
.setBackground(stateColor
);
1661 gc
.fillRectangle(rect
);
1662 // get the border color?
1663 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1666 if (!reallySelected
) {
1667 // Draw the top and bottom borders i.e. no side borders
1668 gc
.drawLine(rect
.x
, rect
.y
, rect
.x
+ rect
.width
- 1, rect
.y
);
1669 gc
.drawLine(rect
.x
, rect
.y
+ rect
.height
- 1, rect
.x
+ rect
.width
- 1, rect
.y
+ rect
.height
- 1);
1672 gc
.setForeground(Display
.getDefault().getSystemColor(SWT
.COLOR_BLACK
));
1673 gc
.drawPoint(rect
.x
, rect
.y
- 2);
1675 fTimeGraphProvider
.postDrawEvent(event
, rect
, gc
);
1680 * Fill the space between two contiguous time events
1687 * Is this time event selected or not
1689 protected void fillSpace(Rectangle rect
, GC gc
, boolean selected
) {
1690 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, false));
1691 gc
.fillRectangle(rect
);
1692 if (fDragState
== DRAG_ZOOM
) {
1693 gc
.setBackground(getColorScheme().getBkColor(selected
, fIsInFocus
, true));
1694 if (fDragX0
< fDragX
) {
1695 gc
.fillRectangle(new Rectangle(fDragX0
, rect
.y
, fDragX
- fDragX0
, rect
.height
));
1696 } else if (fDragX0
> fDragX
) {
1697 gc
.fillRectangle(new Rectangle(fDragX
, rect
.y
, fDragX0
- fDragX
, rect
.height
));
1701 gc
.setForeground(getColorScheme().getColor(TimeGraphColorScheme
.MID_LINE
));
1702 int midy
= rect
.y
+ rect
.height
/ 2;
1703 gc
.drawLine(rect
.x
, midy
, rect
.x
+ rect
.width
, midy
);
1707 public void keyTraversed(TraverseEvent e
) {
1708 if ((e
.detail
== SWT
.TRAVERSE_TAB_NEXT
) || (e
.detail
== SWT
.TRAVERSE_TAB_PREVIOUS
)) {
1714 public void keyPressed(KeyEvent e
) {
1716 if (fItemData
.fExpandedItems
.length
== 0) {
1719 if (SWT
.HOME
== e
.keyCode
) {
1721 } else if (SWT
.END
== e
.keyCode
) {
1722 idx
= fItemData
.fExpandedItems
.length
- 1;
1723 } else if (SWT
.ARROW_DOWN
== e
.keyCode
) {
1724 idx
= getSelectedIndex();
1727 } else if (idx
< fItemData
.fExpandedItems
.length
- 1) {
1730 } else if (SWT
.ARROW_UP
== e
.keyCode
) {
1731 idx
= getSelectedIndex();
1734 } else if (idx
> 0) {
1737 } else if (SWT
.ARROW_LEFT
== e
.keyCode
) {
1739 } else if (SWT
.ARROW_RIGHT
== e
.keyCode
) {
1741 } else if (SWT
.PAGE_DOWN
== e
.keyCode
) {
1742 int page
= countPerPage();
1743 idx
= getSelectedIndex();
1748 if (idx
>= fItemData
.fExpandedItems
.length
) {
1749 idx
= fItemData
.fExpandedItems
.length
- 1;
1751 } else if (SWT
.PAGE_UP
== e
.keyCode
) {
1752 int page
= countPerPage();
1753 idx
= getSelectedIndex();
1761 } else if (SWT
.CR
== e
.keyCode
) {
1762 idx
= getSelectedIndex();
1764 if (fItemData
.fExpandedItems
[idx
].fHasChildren
) {
1767 fireDefaultSelection();
1773 selectItem(idx
, false);
1774 fireSelectionChanged();
1776 int x
= toControl(e
.display
.getCursorLocation()).x
;
1777 updateCursor(x
, e
.stateMask
| e
.keyCode
);
1781 public void keyReleased(KeyEvent e
) {
1782 int x
= toControl(e
.display
.getCursorLocation()).x
;
1783 updateCursor(x
, e
.stateMask
& ~e
.keyCode
);
1787 public void focusGained(FocusEvent e
) {
1789 if (fMouseScrollFilterListener
== null) {
1790 fMouseScrollFilterListener
= new Listener() {
1791 // This filter is used to prevent horizontal scrolling of the view
1792 // when the mouse wheel is used to zoom
1794 public void handleEvent(Event event
) {
1798 getDisplay().addFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1801 updateStatusLine(NO_STATUS
);
1805 public void focusLost(FocusEvent e
) {
1807 if (fMouseScrollFilterListener
!= null) {
1808 getDisplay().removeFilter(SWT
.MouseWheel
, fMouseScrollFilterListener
);
1809 fMouseScrollFilterListener
= null;
1811 if (DRAG_NONE
!= fDragState
) {
1813 fDragState
= DRAG_NONE
;
1816 updateStatusLine(NO_STATUS
);
1820 * @return If the current view is focused
1822 public boolean isInFocus() {
1827 * Provide the possibility to control the wait cursor externally e.g. data
1828 * requests in progress
1830 * @param waitInd Should we wait indefinitely?
1832 public void waitCursor(boolean waitInd
) {
1833 // Update cursor as indicated
1835 setCursor(fWaitCursor
);
1841 private void updateCursor(int x
, int stateMask
) {
1842 // if Wait cursor not active, check for the need to change the cursor
1843 if (getCursor() == fWaitCursor
) {
1846 Cursor cursor
= null;
1847 if (fDragState
== DRAG_SPLIT_LINE
) {
1848 } else if (fDragState
== DRAG_SELECTION
) {
1849 cursor
= fResizeCursor
;
1850 } else if (fDragState
== DRAG_TRACE_ITEM
) {
1851 cursor
= fDragCursor
;
1852 } else if (fDragState
== DRAG_ZOOM
) {
1853 cursor
= fZoomCursor
;
1854 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
) {
1855 cursor
= fDragCursor
;
1856 } else if ((stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
1857 cursor
= fResizeCursor
;
1859 if (!isOverSplitLine(x
) &&fTimeProvider
instanceof ITimeDataProvider2
) {
1860 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1861 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1862 int xBegin
= getXForTime(selectionBegin
);
1863 int xEnd
= getXForTime(selectionEnd
);
1864 if (Math
.abs(x
- xBegin
) < SNAP_WIDTH
|| Math
.abs(x
- xEnd
) < SNAP_WIDTH
) {
1865 cursor
= fResizeCursor
;
1869 if (getCursor() != cursor
) {
1874 private void updateStatusLine(int x
) {
1875 if (fStatusLineManager
== null) {
1878 StringBuilder message
= new StringBuilder();
1879 if (x
>= 0 && fDragState
== DRAG_NONE
) {
1880 long time
= getTimeAtX(x
);
1882 message
.append("T: "); //$NON-NLS-1$
1883 message
.append(new CtfTmfTimestamp(time
).toString());
1884 message
.append(" T1: "); //$NON-NLS-1$
1885 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1886 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
1887 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
1888 message
.append(new CtfTmfTimestamp(Math
.min(selectionBegin
, selectionEnd
)).toString());
1889 if (selectionBegin
!= selectionEnd
) {
1890 message
.append(" T2: "); //$NON-NLS-1$
1891 message
.append(new CtfTmfTimestamp(Math
.max(selectionBegin
, selectionEnd
)).toString());
1892 message
.append(" \u0394: "); //$NON-NLS-1$
1893 message
.append(new TmfTimestampDelta(Math
.abs(selectionBegin
- selectionEnd
), ITmfTimestamp
.NANOSECOND_SCALE
));
1896 @SuppressWarnings("deprecation")
1897 long selectedTime
= fTimeProvider
.getSelectedTime();
1898 message
.append(new CtfTmfTimestamp(selectedTime
));
1901 } else if (fDragState
== DRAG_SELECTION
|| fDragState
== DRAG_ZOOM
) {
1902 long time0
= fDragTime0
;
1903 long time
= getTimeAtX(fDragX
);
1904 message
.append("T1: "); //$NON-NLS-1$
1905 message
.append(new CtfTmfTimestamp(Math
.min(time
, time0
)).toString());
1906 if (time
!= time0
) {
1907 message
.append(" T2: "); //$NON-NLS-1$
1908 message
.append(new CtfTmfTimestamp(Math
.max(time
, time0
)).toString());
1909 message
.append(" \u0394: "); //$NON-NLS-1$
1910 message
.append(new TmfTimestampDelta(Math
.abs(time
- time0
), ITmfTimestamp
.NANOSECOND_SCALE
));
1913 fStatusLineManager
.setMessage(message
.toString());
1917 public void mouseMove(MouseEvent e
) {
1918 if (null == fTimeProvider
) {
1921 Point size
= getCtrlSize();
1922 if (DRAG_TRACE_ITEM
== fDragState
) {
1923 int nameWidth
= fTimeProvider
.getNameSpace();
1924 if (e
.x
> nameWidth
&& size
.x
> nameWidth
&& fDragX
!= e
.x
) {
1926 double pixelsPerNanoSec
= (size
.x
- nameWidth
<= RIGHT_MARGIN
) ?
0 : (double) (size
.x
- nameWidth
- RIGHT_MARGIN
) / (fTime1bak
- fTime0bak
);
1927 long timeDelta
= (long) ((pixelsPerNanoSec
== 0) ?
0 : ((fDragX
- fDragX0
) / pixelsPerNanoSec
));
1928 long time1
= fTime1bak
- timeDelta
;
1929 long maxTime
= fTimeProvider
.getMaxTime();
1930 if (time1
> maxTime
) {
1933 long time0
= time1
- (fTime1bak
- fTime0bak
);
1934 if (time0
< fTimeProvider
.getMinTime()) {
1935 time0
= fTimeProvider
.getMinTime();
1936 time1
= time0
+ (fTime1bak
- fTime0bak
);
1938 fTimeProvider
.setStartFinishTime(time0
, time1
);
1940 } else if (DRAG_SPLIT_LINE
== fDragState
) {
1942 fTimeProvider
.setNameSpace(e
.x
);
1943 } else if (DRAG_SELECTION
== fDragState
) {
1944 if (fTimeProvider
instanceof ITimeDataProvider2
) {
1945 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1947 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1949 } else if (DRAG_ZOOM
== fDragState
) {
1950 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), size
.x
- RIGHT_MARGIN
);
1952 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
1953 } else if (DRAG_NONE
== fDragState
) {
1954 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1955 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1958 fMouseOverSplitLine
= mouseOverSplitLine
;
1960 updateCursor(e
.x
, e
.stateMask
);
1961 updateStatusLine(e
.x
);
1965 public void mouseDoubleClick(MouseEvent e
) {
1966 if (null == fTimeProvider
) {
1969 if (1 == e
.button
&& (e
.stateMask
& SWT
.BUTTON_MASK
) == 0) {
1970 if (isOverSplitLine(e
.x
) && fTimeProvider
.getNameSpace() != 0) {
1971 fTimeProvider
.setNameSpace(fIdealNameSpace
);
1972 boolean mouseOverSplitLine
= isOverSplitLine(e
.x
);
1973 if (fMouseOverSplitLine
!= mouseOverSplitLine
) {
1976 fMouseOverSplitLine
= mouseOverSplitLine
;
1979 int idx
= getItemIndexAtY(e
.y
);
1981 selectItem(idx
, false);
1982 fireDefaultSelection();
1988 public void mouseDown(MouseEvent e
) {
1989 if (fDragState
!= DRAG_NONE
|| null == fTimeProvider
) {
1993 if (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == 0) {
1994 int nameSpace
= fTimeProvider
.getNameSpace();
1995 if (nameSpace
!= 0 && isOverSplitLine(e
.x
)) {
1996 fDragState
= DRAG_SPLIT_LINE
;
1997 fDragButton
= e
.button
;
2000 fTime0bak
= fTimeProvider
.getTime0();
2001 fTime1bak
= fTimeProvider
.getTime1();
2003 updateCursor(e
.x
, e
.stateMask
);
2007 if (1 == e
.button
&& ((e
.stateMask
& SWT
.MODIFIER_MASK
) == 0 || (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
)) {
2008 int nameSpace
= fTimeProvider
.getNameSpace();
2009 idx
= getItemIndexAtY(e
.y
);
2011 Item item
= fItemData
.fExpandedItems
[idx
];
2012 if (item
.fHasChildren
&& e
.x
< nameSpace
&& e
.x
< MARGIN
+ (item
.fLevel
+ 1) * EXPAND_SIZE
) {
2016 selectItem(idx
, false);
2017 fireSelectionChanged();
2019 selectItem(idx
, false); // clear selection
2020 fireSelectionChanged();
2022 long hitTime
= getTimeAtX(e
.x
);
2026 fDragState
= DRAG_SELECTION
;
2027 fDragButton
= e
.button
;
2030 fDragTime0
= getTimeAtX(fDragX0
);
2031 if (fTimeProvider
instanceof ITimeDataProvider2
) {
2032 long selectionBegin
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionBegin();
2033 long selectionEnd
= ((ITimeDataProvider2
) fTimeProvider
).getSelectionEnd();
2034 int xBegin
= getXForTime(selectionBegin
);
2035 int xEnd
= getXForTime(selectionEnd
);
2036 if ((e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.SHIFT
) {
2037 long time
= getTimeAtX(e
.x
);
2038 if (Math
.abs(time
- selectionBegin
) < Math
.abs(time
- selectionEnd
)) {
2040 fDragTime0
= selectionEnd
;
2043 fDragTime0
= selectionBegin
;
2046 long time
= getTimeAtX(e
.x
);
2047 if (Math
.abs(e
.x
- xBegin
) < SNAP_WIDTH
&& Math
.abs(time
- selectionBegin
) <= Math
.abs(time
- selectionEnd
)) {
2049 fDragTime0
= selectionEnd
;
2050 } else if (Math
.abs(e
.x
- xEnd
) < SNAP_WIDTH
&& Math
.abs(time
- selectionEnd
) <= Math
.abs(time
- selectionBegin
)) {
2052 fDragTime0
= selectionBegin
;
2056 fTime0bak
= fTimeProvider
.getTime0();
2057 fTime1bak
= fTimeProvider
.getTime1();
2059 updateCursor(e
.x
, e
.stateMask
);
2060 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2062 } else if (2 == e
.button
|| (1 == e
.button
&& (e
.stateMask
& SWT
.MODIFIER_MASK
) == SWT
.CTRL
)) {
2063 long hitTime
= getTimeAtX(e
.x
);
2066 fDragState
= DRAG_TRACE_ITEM
;
2067 fDragButton
= e
.button
;
2070 fTime0bak
= fTimeProvider
.getTime0();
2071 fTime1bak
= fTimeProvider
.getTime1();
2072 updateCursor(e
.x
, e
.stateMask
);
2074 } else if (3 == e
.button
) {
2075 if (fTimeProvider
.getTime0() == fTimeProvider
.getTime1() || getCtrlSize().x
- fTimeProvider
.getNameSpace() <= 0) {
2079 fDragX
= Math
.min(Math
.max(e
.x
, fTimeProvider
.getNameSpace()), getCtrlSize().x
- RIGHT_MARGIN
);
2081 fDragState
= DRAG_ZOOM
;
2082 fDragButton
= e
.button
;
2084 updateCursor(e
.x
, e
.stateMask
);
2085 fTimeGraphScale
.setDragRange(fDragX0
, fDragX
);
2090 public void mouseUp(MouseEvent e
) {
2091 if (fPendingMenuDetectEvent
!= null && e
.button
== 3) {
2092 menuDetected(fPendingMenuDetectEvent
);
2094 if (DRAG_NONE
!= fDragState
) {
2096 if (e
.button
== fDragButton
&& DRAG_TRACE_ITEM
== fDragState
) {
2097 if (fDragX
!= fDragX0
) {
2098 fTimeProvider
.notifyStartFinishTime();
2100 fDragState
= DRAG_NONE
;
2101 } else if (e
.button
== fDragButton
&& DRAG_SPLIT_LINE
== fDragState
) {
2102 fDragState
= DRAG_NONE
;
2104 } else if (e
.button
== fDragButton
&& DRAG_SELECTION
== fDragState
) {
2105 if (fDragX
== fDragX0
) { // click without selecting anything
2106 long time
= getTimeAtX(e
.x
);
2107 fTimeProvider
.setSelectedTimeNotify(time
, false);
2109 long time0
= fDragTime0
;
2110 long time1
= getTimeAtX(fDragX
);
2111 if (time0
<= time1
) {
2112 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time0
, time1
);
2114 ((ITimeDataProvider2
) fTimeProvider
).setSelectionRangeNotify(time1
, time0
);
2117 fDragState
= DRAG_NONE
;
2119 fTimeGraphScale
.setDragRange(-1, -1);
2120 } else if (e
.button
== fDragButton
&& DRAG_ZOOM
== fDragState
) {
2121 int nameWidth
= fTimeProvider
.getNameSpace();
2122 if (Math
.max(fDragX
, fDragX0
) > nameWidth
&& fDragX
!= fDragX0
) {
2123 long time0
= getTimeAtX(fDragX0
);
2124 long time1
= getTimeAtX(fDragX
);
2125 if (time0
< time1
) {
2126 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2128 fTimeProvider
.setStartFinishTimeNotify(time1
, time0
);
2133 fDragState
= DRAG_NONE
;
2134 fTimeGraphScale
.setDragRange(-1, -1);
2137 updateCursor(e
.x
, e
.stateMask
);
2138 updateStatusLine(e
.x
);
2142 public void mouseEnter(MouseEvent e
) {
2146 public void mouseExit(MouseEvent e
) {
2147 if (fMouseOverSplitLine
) {
2148 fMouseOverSplitLine
= false;
2151 updateStatusLine(NO_STATUS
);
2155 public void mouseHover(MouseEvent e
) {
2159 public void mouseScrolled(MouseEvent e
) {
2160 if ((fMouseScrollFilterListener
== null) || fDragState
!= DRAG_NONE
) {
2163 boolean zoomScroll
= false;
2164 Point p
= getParent().toControl(getDisplay().getCursorLocation());
2165 Point parentSize
= getParent().getSize();
2166 if (p
.x
>= 0 && p
.x
< parentSize
.x
&& p
.y
>= 0 && p
.y
< parentSize
.y
) {
2167 // over the parent control
2168 if (e
.x
> getCtrlSize().x
) {
2169 // over the horizontal scroll bar
2171 } else if (e
.y
>= 0 && e
.y
< getCtrlSize().y
&& e
.x
< fTimeProvider
.getNameSpace()) {
2172 // over the name space
2178 if (zoomScroll
&& fTimeProvider
.getTime0() != fTimeProvider
.getTime1()) {
2181 } else if (e
.count
< 0) {
2185 setTopIndex(getTopIndex() - e
.count
);
2190 public void controlMoved(ControlEvent e
) {
2194 public void controlResized(ControlEvent e
) {
2199 public void widgetDefaultSelected(SelectionEvent e
) {
2203 public void widgetSelected(SelectionEvent e
) {
2204 if (e
.widget
== getVerticalBar()) {
2205 setTopIndex(getVerticalBar().getSelection());
2206 } else if (e
.widget
== getHorizontalBar() && null != fTimeProvider
) {
2207 int start
= getHorizontalBar().getSelection();
2208 long time0
= fTimeProvider
.getTime0();
2209 long time1
= fTimeProvider
.getTime1();
2210 long timeMin
= fTimeProvider
.getMinTime();
2211 long timeMax
= fTimeProvider
.getMaxTime();
2212 long delta
= timeMax
- timeMin
;
2214 long range
= time1
- time0
;
2215 time0
= timeMin
+ Math
.round(delta
* ((double) start
/ H_SCROLLBAR_MAX
));
2216 time1
= time0
+ range
;
2218 // TODO: Follow-up with Bug 310310
2219 // In Linux SWT.DRAG is the only value received
2220 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=310310
2221 if (e
.detail
== SWT
.DRAG
) {
2222 fTimeProvider
.setStartFinishTime(time0
, time1
);
2224 fTimeProvider
.setStartFinishTimeNotify(time0
, time1
);
2230 public int getBorderWidth() {
2231 return fBorderWidth
;
2235 * Set the border width
2237 * @param borderWidth
2240 public void setBorderWidth(int borderWidth
) {
2241 this.fBorderWidth
= borderWidth
;
2245 * @return The current height of the header row
2247 public int getHeaderHeight() {
2248 return fHeaderHeight
;
2252 * Set the height of the header row
2254 * @param headerHeight
2257 public void setHeaderHeight(int headerHeight
) {
2258 this.fHeaderHeight
= headerHeight
;
2262 * @return The height of regular item rows
2264 public int getItemHeight() {
2265 return fGlobalItemHeight
;
2269 * Set the height of regular itew rows
2274 public void setItemHeight(int rowHeight
) {
2275 this.fGlobalItemHeight
= rowHeight
;
2279 * Set the minimum item width
2281 * @param width The minimum width
2283 public void setMinimumItemWidth(int width
) {
2284 this.fMinimumItemWidth
= width
;
2288 * @return The minimum item width
2290 public int getMinimumItemWidth() {
2291 return fMinimumItemWidth
;
2295 * @return The entries that are currently filtered out
2299 public List
<ITimeGraphEntry
> getFilteredOut() {
2300 return fItemData
.getFilteredOut();
2304 public void addSelectionChangedListener(ISelectionChangedListener listener
) {
2305 if (listener
!= null && !fSelectionChangedListeners
.contains(listener
)) {
2306 fSelectionChangedListeners
.add(listener
);
2311 public void removeSelectionChangedListener(ISelectionChangedListener listener
) {
2312 if (listener
!= null) {
2313 fSelectionChangedListeners
.remove(listener
);
2318 public void setSelection(ISelection selection
) {
2319 if (selection
instanceof TimeGraphSelection
) {
2320 TimeGraphSelection sel
= (TimeGraphSelection
) selection
;
2321 Object ob
= sel
.getFirstElement();
2322 if (ob
instanceof ITimeGraphEntry
) {
2323 ITimeGraphEntry trace
= (ITimeGraphEntry
) ob
;
2324 selectItem(trace
, false);
2331 * @param filter The filter object to be attached to the view
2334 public void addFilter(ViewerFilter filter
) {
2335 if (!fFilters
.contains(filter
)) {
2336 fFilters
.add(filter
);
2341 * @param filter The filter object to be attached to the view
2344 public void removeFilter(ViewerFilter filter
) {
2345 fFilters
.remove(filter
);
2348 private class ItemData
{
2349 private Item
[] fExpandedItems
= new Item
[0];
2350 private Item
[] fItems
= new Item
[0];
2351 private ITimeGraphEntry fTraces
[] = new ITimeGraphEntry
[0];
2352 private List
<ILinkEvent
> fLinks
= new ArrayList
<ILinkEvent
>();
2353 private boolean fTraceFilter
[] = new boolean[0];
2354 private final ArrayList
<ITimeGraphEntry
> fFilteredOut
= new ArrayList
<ITimeGraphEntry
>();
2359 Item
findItem(ITimeGraphEntry entry
) {
2360 if (entry
== null) {
2364 for (int i
= 0; i
< fItems
.length
; i
++) {
2365 Item item
= fItems
[i
];
2366 if (item
.fTrace
== entry
) {
2374 int findItemIndex(ITimeGraphEntry trace
) {
2375 if (trace
== null) {
2379 for (int i
= 0; i
< fExpandedItems
.length
; i
++) {
2380 Item item
= fExpandedItems
[i
];
2381 if (item
.fTrace
== trace
) {
2389 public void refreshData() {
2390 List
<Item
> itemList
= new ArrayList
<Item
>();
2391 fFilteredOut
.clear();
2392 ITimeGraphEntry selection
= getSelectedTrace();
2393 for (int i
= 0; i
< fTraces
.length
; i
++) {
2394 ITimeGraphEntry entry
= fTraces
[i
];
2395 refreshData(itemList
, null, 0, entry
);
2397 fItems
= itemList
.toArray(new Item
[0]);
2398 updateExpandedItems();
2399 if (selection
!= null) {
2400 for (Item item
: fExpandedItems
) {
2401 if (item
.fTrace
== selection
) {
2402 item
.fSelected
= true;
2409 private void refreshData(List
<Item
> itemList
, Item parent
, int level
, ITimeGraphEntry entry
) {
2410 Item item
= new Item(entry
, entry
.getName(), level
);
2411 if (parent
!= null) {
2412 parent
.fChildren
.add(item
);
2414 item
.fItemHeight
= fTimeGraphProvider
.getItemHeight(entry
);
2416 if (entry
.hasChildren()) {
2417 item
.fExpanded
= true;
2418 item
.fHasChildren
= true;
2419 for (ITimeGraphEntry child
: entry
.getChildren()) {
2420 refreshData(itemList
, item
, level
+ 1, child
);
2425 public void updateExpandedItems() {
2426 List
<Item
> expandedItemList
= new ArrayList
<Item
>();
2427 for (int i
= 0; i
< fTraces
.length
; i
++) {
2428 ITimeGraphEntry entry
= fTraces
[i
];
2429 Item item
= findItem(entry
);
2430 refreshExpanded(expandedItemList
, item
);
2432 fExpandedItems
= expandedItemList
.toArray(new Item
[0]);
2435 private void refreshExpanded(List
<Item
> expandedItemList
, Item item
) {
2436 // Check for filters
2437 boolean display
= true;
2438 for (ViewerFilter filter
: fFilters
) {
2439 if (!filter
.select(null, item
.fTrace
.getParent(), item
.fTrace
)) {
2445 expandedItemList
.add(item
);
2446 if (item
.fHasChildren
&& item
.fExpanded
) {
2447 for (Item child
: item
.fChildren
) {
2448 refreshExpanded(expandedItemList
, child
);
2454 public void refreshData(ITimeGraphEntry traces
[]) {
2455 if (traces
== null) {
2456 fTraceFilter
= null;
2459 if (traces
.length
== 0) {
2460 fTraceFilter
= null;
2461 } else if (fTraceFilter
== null || traces
.length
!= fTraceFilter
.length
) {
2462 fTraceFilter
= new boolean[traces
.length
];
2463 java
.util
.Arrays
.fill(fTraceFilter
, true);
2465 fTraces
= Arrays
.copyOf(traces
, traces
.length
);
2471 public void refreshArrows(List
<ILinkEvent
> events
) {
2472 /* If links are null, reset the list */
2473 if (events
!= null) {
2476 fLinks
= new ArrayList
<ILinkEvent
>();
2480 public ITimeGraphEntry
[] getTraces() {
2484 public boolean[] getTraceFilter() {
2485 return fTraceFilter
;
2488 public List
<ITimeGraphEntry
> getFilteredOut() {
2489 return fFilteredOut
;
2493 private class Item
{
2494 private boolean fExpanded
;
2495 private boolean fSelected
;
2496 private boolean fHasChildren
;
2497 private int fItemHeight
;
2499 private List
<Item
> fChildren
;
2500 private String fName
;
2501 private ITimeGraphEntry fTrace
;
2503 public Item(ITimeGraphEntry trace
, String name
, int level
) {
2504 this.fTrace
= trace
;
2506 this.fLevel
= level
;
2507 this.fChildren
= new ArrayList
<Item
>();
2511 public String
toString() {
2520 public void menuDetected(MenuDetectEvent e
) {
2521 if (null == fTimeProvider
) {
2524 if (e
.detail
== SWT
.MENU_MOUSE
) {
2525 if (fPendingMenuDetectEvent
== null) {
2526 /* Feature in Linux. The MenuDetectEvent is received before mouseDown.
2527 * Store the event and trigger it later just before handling mouseUp.
2528 * This allows for the method to detect if mouse is used to drag zoom.
2530 fPendingMenuDetectEvent
= e
;
2533 fPendingMenuDetectEvent
= null;
2534 if (fDragState
!= DRAG_ZOOM
|| fDragX
!= fDragX0
) {
2538 if (fDragState
!= DRAG_NONE
) {
2542 Point p
= toControl(e
.x
, e
.y
);
2543 int idx
= getItemIndexAtY(p
.y
);
2544 if (idx
>= 0 && idx
< fItemData
.fExpandedItems
.length
) {
2545 Item item
= fItemData
.fExpandedItems
[idx
];
2546 ITimeGraphEntry entry
= item
.fTrace
;
2547 if (entry
.hasTimeEvents()) {
2548 ITimeEvent event
= Utils
.findEvent(entry
, getTimeAtX(p
.x
), 2);
2549 if (event
!= null) {
2551 fireMenuEventOnTimeEvent(e
);
2556 fireMenuEventOnTimeGraphEntry(e
);