tmf: Simple warning fixes in tmf.ui and tests
[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 = 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 eventStartTime = threadEvent.getTime();
173 eventDuration = threadEvent.getDuration();
174 if (eventDuration < 0 && nextEvent != null) {
175 eventEndTime = nextEvent.getTime();
176 eventDuration = eventEndTime - eventStartTime;
177 } else {
178 eventEndTime = eventStartTime + eventDuration;
179 }
180
181 // TODO: Check if we need "format"
182 // TimeFormat format = TimeFormat.RELATIVE;
183 Resolution res = Resolution.NANOSEC;
184 if (_timeDataProvider.isCalendarFormat()) {
185 // format = TimeFormat.ABSOLUTE; // Absolute format
186 // // (calendar)
187 // Add Date
188 addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
189 Utils.formatDate(eventStartTime)
190 : "?"); //$NON-NLS-1$
191 if (eventDuration > 0) {
192 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
193 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
194 : "?"); //$NON-NLS-1$
195
196 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
197 Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)
198 : "?"); //$NON-NLS-1$
199 } else {
200 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
201 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
202 : "?"); //$NON-NLS-1$
203 }
204 } else {
205 if (eventDuration > 0) {
206 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
207 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
208 : "?"); //$NON-NLS-1$
209
210 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
211 Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)
212 : "?"); //$NON-NLS-1$
213 } else {
214 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
215 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
216 : "?"); //$NON-NLS-1$
217 }
218 }
219
220 if (eventDuration > 0) {
221 // Duration in relative format in any case
222 addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
223 Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)
224 : "?"); //$NON-NLS-1$
225 }
226 }
227 }
228
229 @Override
230 public void mouseHover(MouseEvent event) {
231 Point pt = new Point(event.x, event.y);
232 TimeGraphControl threadStates = (TimeGraphControl) event.widget;
233 ITimeGraphEntry entry = threadStates.getEntry(pt);
234 _tipTable.remove(0, _tipTable.getItemCount() - 1);
235 fillValues(pt, threadStates, entry);
236 if (_tipTable.getItemCount() == 0) {
237 return;
238 }
239 _tipTable.getColumn(0).pack();
240 _tipTable.getColumn(1).pack();
241 _tipShell.pack();
242 _tipPosition = control.toDisplay(pt);
243 _tipShell.pack();
244 setHoverLocation(_tipShell, _tipPosition);
245 _tipShell.setVisible(true);
246 }
247 });
248 }
249
250 private static void setHoverLocation(Shell shell, Point position) {
251 Rectangle displayBounds = shell.getDisplay().getBounds();
252 Rectangle shellBounds = shell.getBounds();
253 shellBounds.x = Math.max(Math.min(position.x, displayBounds.width
254 - shellBounds.width), 0);
255 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height
256 - shellBounds.height), 0);
257 shell.setBounds(shellBounds);
258 }
259
260 }
This page took 0.037902 seconds and 6 git commands to generate.