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