1 /*****************************************************************************
2 * Copyright (c) 2007 Intel Corporation, 2009, 2012 Ericsson.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Intel Corporation - Initial API and implementation
10 * Vitaly A. Provodin, Intel - Initial API and implementation
11 * Alvaro Sanchez-Leon - Updated for TMF
12 * Patrick Tasse - Refactoring
14 *****************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
;
17 import java
.util
.Iterator
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Messages
;
21 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.ITimeGraphPresentationProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
24 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
25 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.MouseAdapter
;
28 import org
.eclipse
.swt
.events
.MouseEvent
;
29 import org
.eclipse
.swt
.events
.MouseMoveListener
;
30 import org
.eclipse
.swt
.events
.MouseTrackAdapter
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.graphics
.Rectangle
;
33 import org
.eclipse
.swt
.layout
.GridData
;
34 import org
.eclipse
.swt
.layout
.GridLayout
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Label
;
39 import org
.eclipse
.swt
.widgets
.Shell
;
42 * Handler for the tool tips in the generic time graph view.
45 * @author Alvaro Sanchez-Leon
46 * @author Patrick Tasse
48 public class TimeGraphTooltipHandler
{
50 private Shell _tipShell
;
51 private Composite _tipComposite
;
52 private Point _tipPosition
;
53 private final ITimeDataProvider _timeDataProvider
;
54 ITimeGraphPresentationProvider _utilImp
= null;
57 * Standard constructor
60 * The parent shell (unused, can be null)
62 * The presentation provider
66 public TimeGraphTooltipHandler(Shell parent
, ITimeGraphPresentationProvider rUtilImpl
,
67 ITimeDataProvider timeProv
) {
69 this._utilImp
= rUtilImpl
;
70 this._timeDataProvider
= timeProv
;
73 private void createTooltipShell(Shell parent
) {
74 final Display display
= parent
.getDisplay();
75 if (_tipShell
!= null && ! _tipShell
.isDisposed()) {
78 _tipShell
= new Shell(parent
, SWT
.ON_TOP
| SWT
.TOOL
);
79 GridLayout gridLayout
= new GridLayout();
80 gridLayout
.numColumns
= 2;
81 gridLayout
.marginWidth
= 2;
82 gridLayout
.marginHeight
= 2;
83 _tipShell
.setLayout(gridLayout
);
84 _tipShell
.setBackground(display
.getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
86 _tipComposite
= new Composite(_tipShell
, SWT
.NONE
);
87 _tipComposite
.setLayout(new GridLayout(3, false));
88 setupControl(_tipComposite
);
93 * Callback for the mouse-over tooltip
96 * The control object to use
98 public void activateHoverHelp(final Control control
) {
99 control
.addMouseListener(new MouseAdapter() {
101 public void mouseDown(MouseEvent e
) {
102 if (_tipShell
!= null && ! _tipShell
.isDisposed()) {
108 control
.addMouseMoveListener(new MouseMoveListener() {
110 public void mouseMove(MouseEvent e
) {
111 if (_tipShell
!= null && ! _tipShell
.isDisposed()) {
117 control
.addMouseTrackListener(new MouseTrackAdapter() {
119 public void mouseExit(MouseEvent e
) {
120 if (_tipShell
!= null && ! _tipShell
.isDisposed()) {
121 Point pt
= control
.toDisplay(e
.x
, e
.y
);
122 if (! _tipShell
.getBounds().contains(pt
)) {
128 private void addItem(String name
, String value
) {
129 Label nameLabel
= new Label(_tipComposite
, SWT
.NO_FOCUS
);
130 nameLabel
.setText(name
);
131 setupControl(nameLabel
);
132 Label separator
= new Label(_tipComposite
, SWT
.NO_FOCUS
| SWT
.SEPARATOR
| SWT
.VERTICAL
);
133 GridData gd
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
134 gd
.heightHint
= nameLabel
.computeSize(SWT
.DEFAULT
, SWT
.DEFAULT
).y
;
135 separator
.setLayoutData(gd
);
136 setupControl(separator
);
137 Label valueLabel
= new Label(_tipComposite
, SWT
.NO_FOCUS
);
138 valueLabel
.setText(value
);
139 setupControl(valueLabel
);
142 private void fillValues(Point pt
, TimeGraphControl timeGraphControl
, ITimeGraphEntry entry
) {
146 if (entry
.hasTimeEvents()) {
147 long currPixelTime
= timeGraphControl
.getTimeAtX(pt
.x
);
148 long nextPixelTime
= timeGraphControl
.getTimeAtX(pt
.x
+ 1);
149 if (nextPixelTime
== currPixelTime
) {
152 ITimeEvent currEvent
= Utils
.findEvent(entry
, currPixelTime
, 0);
153 ITimeEvent nextEvent
= Utils
.findEvent(entry
, currPixelTime
, 1);
155 // if there is no current event at the start of the current pixel range,
156 // or if the current event starts before the current pixel range,
157 // use the next event as long as it starts within the current pixel range
158 if (currEvent
== null || currEvent
.getTime() < currPixelTime
) {
159 if (nextEvent
!= null && nextEvent
.getTime() < nextPixelTime
) {
160 currEvent
= nextEvent
;
161 currPixelTime
= nextEvent
.getTime();
166 String stateTypeName
= _utilImp
.getStateTypeName(entry
);
167 String entryName
= entry
.getName();
168 if (stateTypeName
== null) {
169 stateTypeName
= _utilImp
.getStateTypeName();
172 if (!entryName
.isEmpty()) {
173 addItem(stateTypeName
, entry
.getName());
176 if (currEvent
== null) {
181 String state
= _utilImp
.getEventName(currEvent
);
183 addItem(Messages
.TmfTimeTipHandler_TRACE_STATE
, state
);
186 // This block receives a list of <String, String> values to be added to the tip table
187 Map
<String
, String
> eventAddOns
= _utilImp
.getEventHoverToolTipInfo(currEvent
, currPixelTime
);
188 if (eventAddOns
!= null) {
189 for (Iterator
<String
> iter
= eventAddOns
.keySet().iterator(); iter
.hasNext();) {
190 String message
= iter
.next();
191 addItem(message
, eventAddOns
.get(message
));
195 long eventStartTime
= -1;
196 long eventDuration
= -1;
197 long eventEndTime
= -1;
199 eventStartTime
= currEvent
.getTime();
200 eventDuration
= currEvent
.getDuration();
201 if (eventDuration
< 0 && nextEvent
!= null) {
202 eventEndTime
= nextEvent
.getTime();
203 eventDuration
= eventEndTime
- eventStartTime
;
205 eventEndTime
= eventStartTime
+ eventDuration
;
208 Resolution res
= Resolution
.NANOSEC
;
209 if (_timeDataProvider
.isCalendarFormat()) {
210 addItem(Messages
.TmfTimeTipHandler_TRACE_DATE
, eventStartTime
> -1 ?
211 Utils
.formatDate(eventStartTime
)
212 : "?"); //$NON-NLS-1$
213 if (eventDuration
> 0) {
214 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
215 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
216 : "?"); //$NON-NLS-1$
218 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
219 Utils
.formatTime(eventEndTime
, TimeFormat
.ABSOLUTE
, res
)
220 : "?"); //$NON-NLS-1$
222 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
223 Utils
.formatTime(eventStartTime
, TimeFormat
.ABSOLUTE
, res
)
224 : "?"); //$NON-NLS-1$
227 if (eventDuration
> 0) {
228 addItem(Messages
.TmfTimeTipHandler_TRACE_START_TIME
, eventStartTime
> -1 ?
229 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
230 : "?"); //$NON-NLS-1$
232 addItem(Messages
.TmfTimeTipHandler_TRACE_STOP_TIME
, eventEndTime
> -1 ?
233 Utils
.formatTime(eventEndTime
, TimeFormat
.RELATIVE
, res
)
234 : "?"); //$NON-NLS-1$
236 addItem(Messages
.TmfTimeTipHandler_TRACE_EVENT_TIME
, eventStartTime
> -1 ?
237 Utils
.formatTime(eventStartTime
, TimeFormat
.RELATIVE
, res
)
238 : "?"); //$NON-NLS-1$
242 if (eventDuration
> 0) {
243 // Duration in relative format in any case
244 addItem(Messages
.TmfTimeTipHandler_DURATION
, eventDuration
> -1 ?
245 Utils
.formatTime(eventDuration
, TimeFormat
.RELATIVE
, res
)
246 : "?"); //$NON-NLS-1$
252 public void mouseHover(MouseEvent event
) {
253 Point pt
= new Point(event
.x
, event
.y
);
254 TimeGraphControl timeGraphControl
= (TimeGraphControl
) event
.widget
;
255 createTooltipShell(timeGraphControl
.getShell());
256 ITimeGraphEntry entry
= timeGraphControl
.getEntry(pt
);
257 for (Control child
: _tipComposite
.getChildren()) {
260 fillValues(pt
, timeGraphControl
, entry
);
261 if (_tipComposite
.getChildren().length
== 0) {
265 _tipPosition
= control
.toDisplay(pt
);
267 setHoverLocation(_tipShell
, _tipPosition
);
268 _tipShell
.setVisible(true);
273 private static void setHoverLocation(Shell shell
, Point position
) {
274 Rectangle displayBounds
= shell
.getDisplay().getBounds();
275 Rectangle shellBounds
= shell
.getBounds();
276 if (position
.x
+ shellBounds
.width
+ 16 > displayBounds
.width
&& position
.x
- shellBounds
.width
- 16 >= 0) {
277 shellBounds
.x
= position
.x
- shellBounds
.width
- 16;
279 shellBounds
.x
= Math
.max(Math
.min(position
.x
+ 16, displayBounds
.width
- shellBounds
.width
), 0);
281 if (position
.y
+ shellBounds
.height
+ 16 > displayBounds
.height
&& position
.y
- shellBounds
.height
- 16 >= 0) {
282 shellBounds
.y
= position
.y
- shellBounds
.height
- 16;
284 shellBounds
.y
= Math
.max(Math
.min(position
.y
+ 16, displayBounds
.height
- shellBounds
.height
), 0);
286 shell
.setBounds(shellBounds
);
289 private void setupControl(Control control
) {
290 control
.setForeground(_tipShell
.getDisplay().getSystemColor(SWT
.COLOR_INFO_FOREGROUND
));
291 control
.setBackground(_tipShell
.getDisplay().getSystemColor(SWT
.COLOR_INFO_BACKGROUND
));
293 control
.addMouseListener(new MouseAdapter() {
295 public void mouseDown(MouseEvent e
) {
300 control
.addMouseTrackListener(new MouseTrackAdapter() {
302 public void mouseExit(MouseEvent e
) {
307 control
.addMouseMoveListener(new MouseMoveListener() {
309 public void mouseMove(MouseEvent e
) {