Commit | Line | Data |
---|---|---|
34313553 PT |
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 | |
7 | * | |
8 | * Contributors: | |
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 | |
13 | * | |
14 | *****************************************************************************/ | |
15 | package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets; | |
16 | ||
17 | import java.util.Iterator; | |
18 | import java.util.Map; | |
19 | ||
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; | |
40 | ||
41 | /** | |
42 | * Handler for the tool tips in the generic time graph view. | |
43 | * | |
44 | * @version 1.0 | |
45 | * @author Alvaro Sanchez-Leon | |
46 | * @author Patrick Tasse | |
47 | */ | |
48 | public class TimeGraphTooltipHandler { | |
49 | ||
50 | private Shell _tipShell; | |
51 | private Composite _tipComposite; | |
52 | private Point _tipPosition; | |
53 | private final ITimeDataProvider _timeDataProvider; | |
54 | ITimeGraphPresentationProvider _utilImp = null; | |
55 | ||
56 | /** | |
57 | * Standard constructor | |
58 | * | |
59 | * @param parent | |
60 | * The parent shell (unused, can be null) | |
61 | * @param rUtilImpl | |
62 | * The presentation provider | |
63 | * @param timeProv | |
64 | * The time provider | |
65 | */ | |
66 | public TimeGraphTooltipHandler(Shell parent, ITimeGraphPresentationProvider rUtilImpl, | |
67 | ITimeDataProvider timeProv) { | |
68 | ||
69 | this._utilImp = rUtilImpl; | |
70 | this._timeDataProvider = timeProv; | |
71 | } | |
72 | ||
73 | private void createTooltipShell(Shell parent) { | |
74 | final Display display = parent.getDisplay(); | |
75 | if (_tipShell != null && ! _tipShell.isDisposed()) { | |
76 | _tipShell.dispose(); | |
77 | } | |
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)); | |
85 | ||
86 | _tipComposite = new Composite(_tipShell, SWT.NONE); | |
87 | _tipComposite.setLayout(new GridLayout(3, false)); | |
88 | setupControl(_tipComposite); | |
89 | ||
90 | } | |
91 | ||
92 | /** | |
93 | * Callback for the mouse-over tooltip | |
94 | * | |
95 | * @param control | |
96 | * The control object to use | |
97 | */ | |
98 | public void activateHoverHelp(final Control control) { | |
99 | control.addMouseListener(new MouseAdapter() { | |
100 | @Override | |
101 | public void mouseDown(MouseEvent e) { | |
102 | if (_tipShell != null && ! _tipShell.isDisposed()) { | |
103 | _tipShell.dispose(); | |
104 | } | |
105 | } | |
106 | }); | |
107 | ||
108 | control.addMouseMoveListener(new MouseMoveListener() { | |
109 | @Override | |
110 | public void mouseMove(MouseEvent e) { | |
111 | if (_tipShell != null && ! _tipShell.isDisposed()) { | |
112 | _tipShell.dispose(); | |
113 | } | |
114 | } | |
115 | }); | |
116 | ||
117 | control.addMouseTrackListener(new MouseTrackAdapter() { | |
118 | @Override | |
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)) { | |
123 | _tipShell.dispose(); | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
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); | |
140 | } | |
141 | ||
142 | private void fillValues(Point pt, TimeGraphControl timeGraphControl, ITimeGraphEntry entry) { | |
143 | if (entry == null) { | |
144 | return; | |
145 | } | |
146 | if (entry.hasTimeEvents()) { | |
147 | long currPixelTime = timeGraphControl.getTimeAtX(pt.x); | |
148 | long nextPixelTime = timeGraphControl.getTimeAtX(pt.x + 1); | |
149 | if (nextPixelTime == currPixelTime) { | |
150 | nextPixelTime++; | |
151 | } | |
152 | ITimeEvent currEvent = Utils.findEvent(entry, currPixelTime, 0); | |
153 | ITimeEvent nextEvent = Utils.findEvent(entry, currPixelTime, 1); | |
154 | ||
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(); | |
162 | } | |
163 | } | |
164 | ||
165 | // state name | |
166 | String stateTypeName = _utilImp.getStateTypeName(entry); | |
167 | String entryName = entry.getName(); | |
168 | if (stateTypeName == null) { | |
169 | stateTypeName = _utilImp.getStateTypeName(); | |
170 | } | |
171 | ||
172 | if (!entryName.isEmpty()) { | |
173 | addItem(stateTypeName, entry.getName()); | |
174 | } | |
175 | ||
176 | if (currEvent == null) { | |
177 | return; | |
178 | } | |
179 | ||
180 | // state | |
181 | String state = _utilImp.getEventName(currEvent); | |
182 | if (state != null) { | |
183 | addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state); | |
184 | } | |
185 | ||
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)); | |
192 | } | |
193 | } | |
194 | ||
195 | long eventStartTime = -1; | |
196 | long eventDuration = -1; | |
197 | long eventEndTime = -1; | |
198 | ||
199 | eventStartTime = currEvent.getTime(); | |
200 | eventDuration = currEvent.getDuration(); | |
201 | if (eventDuration < 0 && nextEvent != null) { | |
202 | eventEndTime = nextEvent.getTime(); | |
203 | eventDuration = eventEndTime - eventStartTime; | |
204 | } else { | |
205 | eventEndTime = eventStartTime + eventDuration; | |
206 | } | |
207 | ||
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$ | |
217 | ||
218 | addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ? | |
219 | Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res) | |
220 | : "?"); //$NON-NLS-1$ | |
221 | } else { | |
222 | addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ? | |
223 | Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res) | |
224 | : "?"); //$NON-NLS-1$ | |
225 | } | |
226 | } else { | |
227 | if (eventDuration > 0) { | |
228 | addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ? | |
229 | Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res) | |
230 | : "?"); //$NON-NLS-1$ | |
231 | ||
232 | addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ? | |
233 | Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res) | |
234 | : "?"); //$NON-NLS-1$ | |
235 | } else { | |
236 | addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ? | |
237 | Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res) | |
238 | : "?"); //$NON-NLS-1$ | |
239 | } | |
240 | } | |
241 | ||
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$ | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | @Override | |
252 | public void mouseHover(MouseEvent event) { | |
5b2b9bd7 PT |
253 | if ((event.stateMask & SWT.BUTTON_MASK) != 0) { |
254 | return; | |
255 | } | |
34313553 PT |
256 | Point pt = new Point(event.x, event.y); |
257 | TimeGraphControl timeGraphControl = (TimeGraphControl) event.widget; | |
258 | createTooltipShell(timeGraphControl.getShell()); | |
259 | ITimeGraphEntry entry = timeGraphControl.getEntry(pt); | |
260 | for (Control child : _tipComposite.getChildren()) { | |
261 | child.dispose(); | |
262 | } | |
263 | fillValues(pt, timeGraphControl, entry); | |
264 | if (_tipComposite.getChildren().length == 0) { | |
265 | return; | |
266 | } | |
267 | _tipShell.pack(); | |
268 | _tipPosition = control.toDisplay(pt); | |
269 | _tipShell.pack(); | |
270 | setHoverLocation(_tipShell, _tipPosition); | |
271 | _tipShell.setVisible(true); | |
272 | } | |
273 | }); | |
274 | } | |
275 | ||
276 | private static void setHoverLocation(Shell shell, Point position) { | |
277 | Rectangle displayBounds = shell.getDisplay().getBounds(); | |
278 | Rectangle shellBounds = shell.getBounds(); | |
279 | if (position.x + shellBounds.width + 16 > displayBounds.width && position.x - shellBounds.width - 16 >= 0) { | |
280 | shellBounds.x = position.x - shellBounds.width - 16; | |
281 | } else { | |
282 | shellBounds.x = Math.max(Math.min(position.x + 16, displayBounds.width - shellBounds.width), 0); | |
283 | } | |
284 | if (position.y + shellBounds.height + 16 > displayBounds.height && position.y - shellBounds.height - 16 >= 0) { | |
285 | shellBounds.y = position.y - shellBounds.height - 16; | |
286 | } else { | |
287 | shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0); | |
288 | } | |
289 | shell.setBounds(shellBounds); | |
290 | } | |
291 | ||
292 | private void setupControl(Control control) { | |
293 | control.setForeground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND)); | |
294 | control.setBackground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND)); | |
295 | ||
296 | control.addMouseListener(new MouseAdapter() { | |
297 | @Override | |
298 | public void mouseDown(MouseEvent e) { | |
299 | _tipShell.dispose(); | |
300 | } | |
301 | }); | |
302 | ||
303 | control.addMouseTrackListener(new MouseTrackAdapter() { | |
304 | @Override | |
305 | public void mouseExit(MouseEvent e) { | |
306 | _tipShell.dispose(); | |
307 | } | |
308 | }); | |
309 | ||
310 | control.addMouseMoveListener(new MouseMoveListener() { | |
311 | @Override | |
312 | public void mouseMove(MouseEvent e) { | |
313 | _tipShell.dispose(); | |
314 | } | |
315 | }); | |
316 | } | |
549f3c43 | 317 | } |