tmf: Add missing Javadoc to tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / TimeGraphTooltipHandler.java
1
2 /*****************************************************************************
3 * Copyright (c) 2007 Intel Corporation, 2009, 2012 Ericsson.
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
8 *
9 * Contributors:
10 * Intel Corporation - Initial API and implementation
11 * Vitaly A. Provodin, Intel - Initial API and implementation
12 * Alvaro Sanchez-Leon - Updated for TMF
13 * Patrick Tasse - Refactoring
14 *
15 *****************************************************************************/
16 package org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets;
17
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import org.eclipse.linuxtools.internal.tmf.ui.Messages;
22 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider;
23 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
24 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
25 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
26 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.MouseAdapter;
29 import org.eclipse.swt.events.MouseEvent;
30 import org.eclipse.swt.events.MouseMoveListener;
31 import org.eclipse.swt.events.MouseTrackAdapter;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.Rectangle;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Display;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Shell;
41
42 /**
43 * Handler for the tool tips in the generic time graph view.
44 *
45 * @version 1.0
46 * @author Alvaro Sanchez-Leon
47 * @author Patrick Tasse
48 */
49 public class TimeGraphTooltipHandler {
50
51 private Shell _tipShell;
52 private Composite _tipComposite;
53 private Point _tipPosition;
54 private final ITimeDataProvider _timeDataProvider;
55 ITimeGraphPresentationProvider _utilImp = null;
56
57 /**
58 * Standard constructor
59 *
60 * @param parent
61 * The parent shell (unused, can be null)
62 * @param rUtilImpl
63 * The presentation provider
64 * @param timeProv
65 * The time provider
66 */
67 public TimeGraphTooltipHandler(Shell parent, ITimeGraphPresentationProvider rUtilImpl,
68 ITimeDataProvider timeProv) {
69
70 this._utilImp = rUtilImpl;
71 this._timeDataProvider = timeProv;
72 }
73
74 private void createTooltipShell(Shell parent) {
75 final Display display = parent.getDisplay();
76 if (_tipShell != null && ! _tipShell.isDisposed()) {
77 _tipShell.dispose();
78 }
79 _tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
80 GridLayout gridLayout = new GridLayout();
81 gridLayout.numColumns = 2;
82 gridLayout.marginWidth = 2;
83 gridLayout.marginHeight = 2;
84 _tipShell.setLayout(gridLayout);
85 _tipShell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
86
87 _tipComposite = new Composite(_tipShell, SWT.NONE);
88 _tipComposite.setLayout(new GridLayout(3, false));
89 setupControl(_tipComposite);
90
91 }
92
93 /**
94 * Callback for the mouse-over tooltip
95 *
96 * @param control
97 * The control object to use
98 */
99 public void activateHoverHelp(final Control control) {
100 control.addMouseListener(new MouseAdapter() {
101 @Override
102 public void mouseDown(MouseEvent e) {
103 if (_tipShell != null && ! _tipShell.isDisposed()) {
104 _tipShell.dispose();
105 }
106 }
107 });
108
109 control.addMouseMoveListener(new MouseMoveListener() {
110 @Override
111 public void mouseMove(MouseEvent e) {
112 if (_tipShell != null && ! _tipShell.isDisposed()) {
113 _tipShell.dispose();
114 }
115 }
116 });
117
118 control.addMouseTrackListener(new MouseTrackAdapter() {
119 @Override
120 public void mouseExit(MouseEvent e) {
121 if (_tipShell != null && ! _tipShell.isDisposed()) {
122 Point pt = control.toDisplay(e.x, e.y);
123 if (! _tipShell.getBounds().contains(pt)) {
124 _tipShell.dispose();
125 }
126 }
127 }
128
129 private void addItem(String name, String value) {
130 Label nameLabel = new Label(_tipComposite, SWT.NO_FOCUS);
131 nameLabel.setText(name);
132 setupControl(nameLabel);
133 Label separator = new Label(_tipComposite, SWT.NO_FOCUS | SWT.SEPARATOR | SWT.VERTICAL);
134 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
135 gd.heightHint = nameLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
136 separator.setLayoutData(gd);
137 setupControl(separator);
138 Label valueLabel = new Label(_tipComposite, SWT.NO_FOCUS);
139 valueLabel.setText(value);
140 setupControl(valueLabel);
141 }
142
143 private void fillValues(Point pt, TimeGraphControl timeGraphControl, ITimeGraphEntry entry) {
144 if (entry == null) {
145 return;
146 }
147 if (entry.hasTimeEvents()) {
148 long currPixelTime = timeGraphControl.getTimeAtX(pt.x);
149 long nextPixelTime = timeGraphControl.getTimeAtX(pt.x + 1);
150 if (nextPixelTime == currPixelTime) {
151 nextPixelTime++;
152 }
153 ITimeEvent currEvent = Utils.findEvent(entry, currPixelTime, 0);
154 ITimeEvent nextEvent = Utils.findEvent(entry, currPixelTime, 1);
155
156 // if there is no current event at the start of the current pixel range,
157 // or if the current event starts before the current pixel range,
158 // use the next event as long as it starts within the current pixel range
159 if (currEvent == null || currEvent.getTime() < currPixelTime) {
160 if (nextEvent != null && nextEvent.getTime() < nextPixelTime) {
161 currEvent = nextEvent;
162 currPixelTime = nextEvent.getTime();
163 }
164 }
165
166 // state name
167 String stateTypeName = _utilImp.getStateTypeName(entry);
168 String entryName = entry.getName();
169 if (stateTypeName == null) {
170 stateTypeName = _utilImp.getStateTypeName();
171 }
172
173 if (!entryName.isEmpty()) {
174 addItem(stateTypeName, entry.getName());
175 }
176
177 if (currEvent == null) {
178 return;
179 }
180
181 // state
182 String state = _utilImp.getEventName(currEvent);
183 if (state != null) {
184 addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state);
185 }
186
187 // This block receives a list of <String, String> values to be added to the tip table
188 Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(currEvent, currPixelTime);
189 if (eventAddOns != null) {
190 for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
191 String message = iter.next();
192 addItem(message, eventAddOns.get(message));
193 }
194 }
195
196 long eventStartTime = -1;
197 long eventDuration = -1;
198 long eventEndTime = -1;
199
200 eventStartTime = currEvent.getTime();
201 eventDuration = currEvent.getDuration();
202 if (eventDuration < 0 && nextEvent != null) {
203 eventEndTime = nextEvent.getTime();
204 eventDuration = eventEndTime - eventStartTime;
205 } else {
206 eventEndTime = eventStartTime + eventDuration;
207 }
208
209 Resolution res = Resolution.NANOSEC;
210 TimeFormat tf = _timeDataProvider.getTimeFormat();
211 if (tf == TimeFormat.CALENDAR) {
212 addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
213 Utils.formatDate(eventStartTime)
214 : "?"); //$NON-NLS-1$
215 }
216 if (eventDuration > 0) {
217 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
218 Utils.formatTime(eventStartTime, tf, res)
219 : "?"); //$NON-NLS-1$
220
221 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
222 Utils.formatTime(eventEndTime, tf, res)
223 : "?"); //$NON-NLS-1$
224 } else {
225 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
226 Utils.formatTime(eventStartTime, tf, res)
227 : "?"); //$NON-NLS-1$
228 }
229
230 if (eventDuration > 0) {
231 // Duration in relative format in any case
232 if (tf == TimeFormat.CALENDAR) {
233 tf = TimeFormat.RELATIVE;
234 }
235 addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
236 Utils.formatTime(eventDuration, tf, res)
237 : "?"); //$NON-NLS-1$
238 }
239 }
240 }
241
242 @Override
243 public void mouseHover(MouseEvent event) {
244 if ((event.stateMask & SWT.BUTTON_MASK) != 0) {
245 return;
246 }
247 Point pt = new Point(event.x, event.y);
248 TimeGraphControl timeGraphControl = (TimeGraphControl) event.widget;
249 createTooltipShell(timeGraphControl.getShell());
250 ITimeGraphEntry entry = timeGraphControl.getEntry(pt);
251 for (Control child : _tipComposite.getChildren()) {
252 child.dispose();
253 }
254 fillValues(pt, timeGraphControl, entry);
255 if (_tipComposite.getChildren().length == 0) {
256 return;
257 }
258 _tipShell.pack();
259 _tipPosition = control.toDisplay(pt);
260 _tipShell.pack();
261 setHoverLocation(_tipShell, _tipPosition);
262 _tipShell.setVisible(true);
263 }
264 });
265 }
266
267 private static void setHoverLocation(Shell shell, Point position) {
268 Rectangle displayBounds = shell.getDisplay().getBounds();
269 Rectangle shellBounds = shell.getBounds();
270 if (position.x + shellBounds.width + 16 > displayBounds.width && position.x - shellBounds.width - 16 >= 0) {
271 shellBounds.x = position.x - shellBounds.width - 16;
272 } else {
273 shellBounds.x = Math.max(Math.min(position.x + 16, displayBounds.width - shellBounds.width), 0);
274 }
275 if (position.y + shellBounds.height + 16 > displayBounds.height && position.y - shellBounds.height - 16 >= 0) {
276 shellBounds.y = position.y - shellBounds.height - 16;
277 } else {
278 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0);
279 }
280 shell.setBounds(shellBounds);
281 }
282
283 private void setupControl(Control control) {
284 control.setForeground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
285 control.setBackground(_tipShell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
286
287 control.addMouseListener(new MouseAdapter() {
288 @Override
289 public void mouseDown(MouseEvent e) {
290 _tipShell.dispose();
291 }
292 });
293
294 control.addMouseTrackListener(new MouseTrackAdapter() {
295 @Override
296 public void mouseExit(MouseEvent e) {
297 _tipShell.dispose();
298 }
299 });
300
301 control.addMouseMoveListener(new MouseMoveListener() {
302 @Override
303 public void mouseMove(MouseEvent e) {
304 _tipShell.dispose();
305 }
306 });
307 }
308 }
This page took 0.039406 seconds and 6 git commands to generate.