Fix for bug 383935: Disappearing tool tip in time graph and other issues
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / timegraph / widgets / TimeGraphTooltipHandler.java
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.GridLayout;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.TableColumn;
39 import org.eclipse.swt.widgets.TableItem;
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 Table _tipTable;
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 _tipTable = new Table(_tipShell, SWT.NONE);
87 new TableColumn(_tipTable, SWT.NONE);
88 new TableColumn(_tipTable, SWT.NONE);
89 _tipTable.setForeground(display
90 .getSystemColor(SWT.COLOR_INFO_FOREGROUND));
91 _tipTable.setBackground(display
92 .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
93 _tipTable.setHeaderVisible(false);
94 _tipTable.setLinesVisible(false);
95
96 _tipTable.addMouseListener(new MouseAdapter() {
97 @Override
98 public void mouseDown(MouseEvent e) {
99 _tipShell.dispose();
100 }
101 });
102
103 _tipTable.addMouseTrackListener(new MouseTrackAdapter() {
104 @Override
105 public void mouseExit(MouseEvent e) {
106 _tipShell.dispose();
107 }
108 });
109
110 _tipTable.addMouseMoveListener(new MouseMoveListener() {
111 @Override
112 public void mouseMove(MouseEvent e) {
113 _tipShell.dispose();
114 }
115 });
116 }
117
118 /**
119 * Callback for the mouse-over tooltip
120 *
121 * @param control
122 * The control object to use
123 */
124 public void activateHoverHelp(final Control control) {
125 control.addMouseListener(new MouseAdapter() {
126 @Override
127 public void mouseDown(MouseEvent e) {
128 if (_tipShell != null && ! _tipShell.isDisposed()) {
129 _tipShell.dispose();
130 }
131 }
132 });
133
134 control.addMouseMoveListener(new MouseMoveListener() {
135 @Override
136 public void mouseMove(MouseEvent e) {
137 if (_tipShell != null && ! _tipShell.isDisposed()) {
138 _tipShell.dispose();
139 }
140 }
141 });
142
143 control.addMouseTrackListener(new MouseTrackAdapter() {
144 @Override
145 public void mouseExit(MouseEvent e) {
146 if (_tipShell != null && ! _tipShell.isDisposed()) {
147 Point pt = control.toDisplay(e.x, e.y);
148 if (! _tipShell.getBounds().contains(pt)) {
149 _tipShell.dispose();
150 }
151 }
152 }
153
154 private void addItem(String name, String value) {
155 TableItem line = new TableItem(_tipTable, SWT.NONE);
156 line.setText(0, name);
157 line.setText(1, value);
158 }
159
160 private void fillValues(Point pt, TimeGraphControl timeGraphControl, ITimeGraphEntry entry) {
161 if (entry == null) {
162 return;
163 }
164 if (entry.hasTimeEvents()) {
165 long currPixelTime = timeGraphControl.getTimeAtX(pt.x);
166 long nextPixelTime = timeGraphControl.getTimeAtX(pt.x + 1);
167 if (nextPixelTime == currPixelTime) {
168 nextPixelTime++;
169 }
170 ITimeEvent currEvent = Utils.findEvent(entry, currPixelTime, 0);
171 ITimeEvent nextEvent = Utils.findEvent(entry, currPixelTime, 1);
172
173 // if there is no current event at the start of the current pixel range,
174 // or if the current event starts before the current pixel range,
175 // use the next event as long as it starts within the current pixel range
176 if (currEvent == null || currEvent.getTime() < currPixelTime) {
177 if (nextEvent != null && nextEvent.getTime() < nextPixelTime) {
178 currEvent = nextEvent;
179 }
180 }
181
182 // state name
183 addItem(_utilImp.getStateTypeName(), entry.getName());
184 if (currEvent == null) {
185 return;
186 }
187 // state
188 String state = _utilImp.getEventName(currEvent);
189 if (state != null) {
190 addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state);
191 }
192
193 // This block receives a list of <String, String> values to be added to the tip table
194 Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(currEvent);
195 if (eventAddOns != null) {
196 for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
197 String message = iter.next();
198 addItem(message, eventAddOns.get(message));
199 }
200 }
201
202 long eventStartTime = -1;
203 long eventDuration = -1;
204 long eventEndTime = -1;
205
206 eventStartTime = currEvent.getTime();
207 eventDuration = currEvent.getDuration();
208 if (eventDuration < 0 && nextEvent != null) {
209 eventEndTime = nextEvent.getTime();
210 eventDuration = eventEndTime - eventStartTime;
211 } else {
212 eventEndTime = eventStartTime + eventDuration;
213 }
214
215 Resolution res = Resolution.NANOSEC;
216 if (_timeDataProvider.isCalendarFormat()) {
217 addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
218 Utils.formatDate(eventStartTime)
219 : "?"); //$NON-NLS-1$
220 if (eventDuration > 0) {
221 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
222 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
223 : "?"); //$NON-NLS-1$
224
225 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
226 Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)
227 : "?"); //$NON-NLS-1$
228 } else {
229 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
230 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
231 : "?"); //$NON-NLS-1$
232 }
233 } else {
234 if (eventDuration > 0) {
235 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
236 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
237 : "?"); //$NON-NLS-1$
238
239 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
240 Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)
241 : "?"); //$NON-NLS-1$
242 } else {
243 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
244 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
245 : "?"); //$NON-NLS-1$
246 }
247 }
248
249 if (eventDuration > 0) {
250 // Duration in relative format in any case
251 addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
252 Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)
253 : "?"); //$NON-NLS-1$
254 }
255 }
256 }
257
258 @Override
259 public void mouseHover(MouseEvent event) {
260 Point pt = new Point(event.x, event.y);
261 TimeGraphControl timeGraphControl = (TimeGraphControl) event.widget;
262 createTooltipShell(timeGraphControl.getShell());
263 ITimeGraphEntry entry = timeGraphControl.getEntry(pt);
264 _tipTable.remove(0, _tipTable.getItemCount() - 1);
265 fillValues(pt, timeGraphControl, entry);
266 if (_tipTable.getItemCount() == 0) {
267 return;
268 }
269 _tipTable.getColumn(0).pack();
270 _tipTable.getColumn(1).pack();
271 _tipShell.pack();
272 _tipPosition = control.toDisplay(pt);
273 _tipShell.pack();
274 setHoverLocation(_tipShell, _tipPosition);
275 _tipShell.setVisible(true);
276 }
277 });
278 }
279
280 private static void setHoverLocation(Shell shell, Point position) {
281 Rectangle displayBounds = shell.getDisplay().getBounds();
282 Rectangle shellBounds = shell.getBounds();
283 if (position.x + shellBounds.width + 16 > displayBounds.width && position.x - shellBounds.width - 16 >= 0) {
284 shellBounds.x = position.x - shellBounds.width - 16;
285 } else {
286 shellBounds.x = Math.max(Math.min(position.x + 16, displayBounds.width - shellBounds.width), 0);
287 }
288 if (position.y + shellBounds.height + 16 > displayBounds.height && position.y - shellBounds.height - 16 >= 0) {
289 shellBounds.y = position.y - shellBounds.height - 16;
290 } else {
291 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0);
292 }
293 shell.setBounds(shellBounds);
294 }
295
296 }
This page took 0.042347 seconds and 6 git commands to generate.