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