0664b719566b2d9f75499c715059f465742f4457
[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.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Display;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Table;
39 import org.eclipse.swt.widgets.TableColumn;
40 import org.eclipse.swt.widgets.TableItem;
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 final Shell _tipShell;
52 private final Table _tipTable;
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 composite object
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 final Display display = parent.getDisplay();
70
71 this._utilImp = rUtilImpl;
72 this._timeDataProvider = timeProv;
73 _tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);
74 GridLayout gridLayout = new GridLayout();
75 gridLayout.numColumns = 2;
76 gridLayout.marginWidth = 2;
77 gridLayout.marginHeight = 2;
78 _tipShell.setLayout(gridLayout);
79 GridData data = new GridData(GridData.BEGINNING, GridData.BEGINNING,
80 true, true);
81 _tipShell.setLayoutData(data);
82 _tipShell.setBackground(display
83 .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
84
85 _tipTable = new Table(_tipShell, SWT.NONE);
86 new TableColumn(_tipTable, SWT.NONE);
87 new TableColumn(_tipTable, SWT.NONE);
88 _tipTable.setForeground(display
89 .getSystemColor(SWT.COLOR_INFO_FOREGROUND));
90 _tipTable.setBackground(display
91 .getSystemColor(SWT.COLOR_INFO_BACKGROUND));
92 _tipTable.setHeaderVisible(false);
93 _tipTable.setLinesVisible(false);
94
95 // tipTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
96 // | GridData.VERTICAL_ALIGN_CENTER));
97 }
98
99 /**
100 * Callback for the mouse-over tooltip
101 *
102 * @param control
103 * The control object to use
104 */
105 public void activateHoverHelp(final Control control) {
106 //FIXME: remove old listeners
107 control.addMouseListener(new MouseAdapter() {
108 @Override
109 public void mouseDown(MouseEvent e) {
110 if (_tipShell.isVisible()) {
111 _tipShell.setVisible(false);
112 }
113 }
114 });
115
116 control.addMouseMoveListener(new MouseMoveListener() {
117 @Override
118 public void mouseMove(MouseEvent e) {
119 if (_tipShell.isVisible()) {
120 _tipShell.setVisible(false);
121 }
122 }
123 });
124
125 control.addMouseTrackListener(new MouseTrackAdapter() {
126 @Override
127 public void mouseExit(MouseEvent e) {
128 if (_tipShell.isVisible()) {
129 _tipShell.setVisible(false);
130 }
131 }
132
133 private void addItem(String name, String value) {
134 TableItem line = new TableItem(_tipTable, SWT.NONE);
135 line.setText(0, name);
136 line.setText(1, value);
137 }
138
139 private void fillValues(Point pt, TimeGraphControl threadStates, ITimeGraphEntry entry) {
140 if (entry == null) {
141 return;
142 }
143 if (entry.hasTimeEvents()) {
144 ITimeEvent threadEvent = Utils.findEvent(entry, threadStates.getTimeAtX(pt.x), 2);
145 ITimeEvent nextEvent = Utils.findEvent(entry, threadStates.getTimeAtX(pt.x), 1);
146 // state name
147 addItem(_utilImp.getStateTypeName(), entry.getName());
148 if (threadEvent == null) {
149 return;
150 }
151 // thread state
152 String state = _utilImp.getEventName(threadEvent);
153 if (state != null) {
154 addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state);
155 }
156
157 // This block receives a
158 // list of <String, String> values to be added to the tip
159 // table
160 Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(threadEvent);
161 if (eventAddOns != null) {
162 for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
163 String message = (String) iter.next();
164 addItem(message, eventAddOns.get(message));
165 }
166 }
167
168 long eventStartTime = -1;
169 long eventDuration = -1;
170 long eventEndTime = -1;
171
172 if (threadEvent != null) {
173 eventStartTime = threadEvent.getTime();
174 eventDuration = threadEvent.getDuration();
175 if (eventDuration < 0 && nextEvent != null) {
176 eventEndTime = nextEvent.getTime();
177 eventDuration = eventEndTime - eventStartTime;
178 } else {
179 eventEndTime = eventStartTime + eventDuration;
180 }
181 }
182
183 // TODO: Check if we need "format"
184 // TimeFormat format = TimeFormat.RELATIVE;
185 Resolution res = Resolution.NANOSEC;
186 if (_timeDataProvider.isCalendarFormat()) {
187 // format = TimeFormat.ABSOLUTE; // Absolute format
188 // // (calendar)
189 // Add Date
190 addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
191 Utils.formatDate(eventStartTime)
192 : "?"); //$NON-NLS-1$
193 if (eventDuration > 0) {
194 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
195 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
196 : "?"); //$NON-NLS-1$
197
198 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
199 Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)
200 : "?"); //$NON-NLS-1$
201 } else {
202 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
203 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
204 : "?"); //$NON-NLS-1$
205 }
206 } else {
207 if (eventDuration > 0) {
208 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
209 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
210 : "?"); //$NON-NLS-1$
211
212 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
213 Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)
214 : "?"); //$NON-NLS-1$
215 } else {
216 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
217 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
218 : "?"); //$NON-NLS-1$
219 }
220 }
221
222 if (eventDuration > 0) {
223 // Duration in relative format in any case
224 addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
225 Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)
226 : "?"); //$NON-NLS-1$
227 }
228 }
229 }
230
231 @Override
232 public void mouseHover(MouseEvent event) {
233 Point pt = new Point(event.x, event.y);
234 TimeGraphControl threadStates = (TimeGraphControl) event.widget;
235 ITimeGraphEntry entry = threadStates.getEntry(pt);
236 _tipTable.remove(0, _tipTable.getItemCount() - 1);
237 fillValues(pt, threadStates, entry);
238 if (_tipTable.getItemCount() == 0) {
239 return;
240 }
241 _tipTable.getColumn(0).pack();
242 _tipTable.getColumn(1).pack();
243 _tipShell.pack();
244 _tipPosition = control.toDisplay(pt);
245 _tipShell.pack();
246 setHoverLocation(_tipShell, _tipPosition);
247 _tipShell.setVisible(true);
248 }
249 });
250 }
251
252 private void setHoverLocation(Shell shell, Point position) {
253 Rectangle displayBounds = shell.getDisplay().getBounds();
254 Rectangle shellBounds = shell.getBounds();
255 shellBounds.x = Math.max(Math.min(position.x, displayBounds.width
256 - shellBounds.width), 0);
257 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height
258 - shellBounds.height), 0);
259 shell.setBounds(shellBounds);
260 }
261
262 }
This page took 0.036901 seconds and 5 git commands to generate.