2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / timeAnalysis / widgets / TmfTimeTipHandler.java
CommitLineData
b0d3496e
ASL
1/*****************************************************************************\r
2 * Copyright (c) 2007, Intel Corporation.\r
3 * All rights reserved. This program and the accompanying materials\r
4 * are made available under the terms of the Eclipse Public License v1.0\r
5 * which accompanies this distribution, and is available at\r
6 * http://www.eclipse.org/legal/epl-v10.html\r
7 *\r
8 * Contributors:\r
9 * Intel Corporation - Initial API and implementation\r
10 * Vitaly A. Provodin, Intel - Initial API and implementation\r
11 * Alvaro Sanchex-Leon - Udpated for TMF\r
12 *\r
13 * $Id: ThreadsTipHandler.java,v 1.5 2007/06/06 19:16:16 gnagarajan Exp $\r
14 *****************************************************************************/\r
15package org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.widgets;\r
16\r
17import java.util.Iterator;\r
18import java.util.Map;\r
19\r
79a3a76e 20import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.Messages;\r
a5823d5f 21import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.TmfTimeAnalysisProvider;\r
b0d3496e
ASL
22import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.ITimeAnalysisViewer.TimeFormat;\r
23import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITimeEvent;\r
24import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.ITmfTimeAnalysisEntry;\r
25import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.widgets.Utils.Resolution;\r
26import org.eclipse.swt.SWT;\r
27import org.eclipse.swt.events.MouseAdapter;\r
28import org.eclipse.swt.events.MouseEvent;\r
29import org.eclipse.swt.events.MouseTrackAdapter;\r
30import org.eclipse.swt.graphics.Point;\r
31import org.eclipse.swt.graphics.Rectangle;\r
32import org.eclipse.swt.layout.GridData;\r
33import org.eclipse.swt.layout.GridLayout;\r
34import org.eclipse.swt.widgets.Control;\r
35import org.eclipse.swt.widgets.Display;\r
36import org.eclipse.swt.widgets.Shell;\r
37import org.eclipse.swt.widgets.Table;\r
38import org.eclipse.swt.widgets.TableColumn;\r
39import org.eclipse.swt.widgets.TableItem;\r
40import org.eclipse.swt.widgets.Widget;\r
41\r
42\r
43public class TmfTimeTipHandler {\r
44\r
45 private Shell _tipShell;\r
46 private Table _tipTable;\r
47 private Item _tipItem;\r
48 private Point _tipPosition;\r
49 private ITimeDataProvider _timeDataProvider;\r
50 TmfTimeAnalysisProvider _utilImp = null;\r
51\r
52 public TmfTimeTipHandler(Shell parent, TmfTimeAnalysisProvider rUtilImpl,\r
53 ITimeDataProvider timeProv) {\r
54 final Display display = parent.getDisplay();\r
55\r
56 this._utilImp = rUtilImpl;\r
57 this._timeDataProvider = timeProv;\r
58 _tipShell = new Shell(parent, SWT.ON_TOP | SWT.TOOL);\r
59 GridLayout gridLayout = new GridLayout();\r
60 gridLayout.numColumns = 2;\r
61 gridLayout.marginWidth = 2;\r
62 gridLayout.marginHeight = 2;\r
63 _tipShell.setLayout(gridLayout);\r
64 GridData data = new GridData(GridData.BEGINNING, GridData.BEGINNING,\r
65 true, true);\r
66 _tipShell.setLayoutData(data);\r
67 _tipShell.setBackground(display\r
68 .getSystemColor(SWT.COLOR_INFO_BACKGROUND));\r
69\r
70 _tipTable = new Table(_tipShell, SWT.NONE);\r
71 _tipTable.setForeground(display\r
72 .getSystemColor(SWT.COLOR_INFO_FOREGROUND));\r
73 _tipTable.setBackground(display\r
74 .getSystemColor(SWT.COLOR_INFO_BACKGROUND));\r
75 _tipTable.setHeaderVisible(false);\r
76 _tipTable.setLinesVisible(false);\r
77\r
78 // tipTable.setLayoutData(new GridData(GridData.FILL_HORIZONTAL\r
79 // | GridData.VERTICAL_ALIGN_CENTER));\r
80 }\r
81\r
82 public void activateHoverHelp(final Control control) {\r
83 control.addMouseListener(new MouseAdapter() {\r
4e3aa37d 84 @Override\r
b0d3496e
ASL
85 public void mouseDown(MouseEvent e) {\r
86 if (_tipShell.isVisible())\r
87 _tipShell.setVisible(false);\r
88 }\r
89 });\r
90\r
91 control.addMouseTrackListener(new MouseTrackAdapter() {\r
4e3aa37d 92 @Override\r
b0d3496e
ASL
93 public void mouseExit(MouseEvent e) {\r
94 if (_tipShell.isVisible())\r
95 _tipShell.setVisible(false);\r
96 _tipItem = null;\r
97\r
98 }\r
99\r
100 private void addItem(String name, String value) {\r
101 TableItem line = new TableItem(_tipTable, SWT.NONE);\r
102 line.setText(0, name);\r
103 line.setText(1, value);\r
104 }\r
105\r
106 private void fillValues(Point pt, TmfTimeStatesCtrl threadStates,\r
107 Item item) {\r
108 if (item instanceof TraceItem) {\r
109 ITmfTimeAnalysisEntry thrd = ((TraceItem) item)._trace;\r
110 ITimeEvent threadEvent = Utils.findEvent(thrd, threadStates\r
111 .hitTimeTest(pt.x, pt.y), 2);\r
112 ITimeEvent nextEvent = Utils.findEvent(thrd, threadStates\r
113 .hitTimeTest(pt.x, pt.y), 1);\r
114 // thread name\r
115 addItem(Messages._TRACE_NAME, thrd.getName());\r
116 // class name\r
117 addItem(Messages._TRACE_CLASS_NAME, _utilImp\r
118 .getTraceClassName(thrd));\r
119 // thread state\r
120 addItem(Messages._TRACE_STATE, _utilImp\r
121 .getEventName(threadEvent));\r
122\r
123 // This block receives a\r
124 // list of <String, String> values to be added to the tip\r
125 // table\r
126 Map<String, String> eventAddOns = _utilImp\r
127 .getEventHoverToolTipInfo(threadEvent);\r
128 for (Iterator<String> iter = eventAddOns.keySet()\r
129 .iterator(); iter.hasNext();) {\r
130 String message = (String) iter.next();\r
131 addItem(message, eventAddOns.get(message));\r
132 }\r
133\r
a5823d5f
ASL
134 long eventStartTime = -1;\r
135 long eventDuration = -1;\r
136 long eventEndTime = -1;\r
137 \r
138 if (threadEvent != null) {\r
139 eventStartTime = threadEvent.getTime();\r
140 eventDuration = threadEvent.getDuration();\r
141 if (eventDuration < 0 && nextEvent != null) {\r
142 eventEndTime = nextEvent.getTime();\r
143 eventDuration = eventEndTime - eventStartTime;\r
144 } else {\r
145 eventEndTime = eventStartTime + eventDuration;\r
146 }\r
b0d3496e
ASL
147 }\r
148\r
149// TODO: Check if we need "format" \r
150// TimeFormat format = TimeFormat.RELATIVE;\r
151 Resolution res = Resolution.NANOSEC;\r
152 if (_timeDataProvider.isCalendarFormat()) {\r
153// format = TimeFormat.ABSOLUTE; // Absolute format\r
154// // (calendar)\r
155 // Add Date\r
a5823d5f
ASL
156 addItem(Messages._TRACE_DATE, eventStartTime > -1 ?\r
157 Utils.formatDate(eventStartTime)\r
3b38ea61 158 : "?"); //$NON-NLS-1$\r
a5823d5f
ASL
159 if (eventDuration > 0) {\r
160 addItem(Messages._TRACE_START_TIME, eventStartTime > -1 ?\r
161 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)\r
3b38ea61 162 : "?"); //$NON-NLS-1$\r
a5823d5f
ASL
163 \r
164 addItem(Messages._TRACE_STOP_TIME, eventEndTime > -1 ?\r
165 Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)\r
3b38ea61 166 : "?"); //$NON-NLS-1$\r
a5823d5f
ASL
167 } else {\r
168 addItem(Messages._TRACE_EVENT_TIME, eventStartTime > -1 ?\r
169 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)\r
3b38ea61 170 : "?"); //$NON-NLS-1$\r
a5823d5f
ASL
171 }\r
172 } else {\r
173 if (eventDuration > 0) {\r
174 addItem(Messages._TRACE_START_TIME, eventStartTime > -1 ?\r
175 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)\r
3b38ea61 176 : "?"); //$NON-NLS-1$\r
a5823d5f
ASL
177 \r
178 addItem(Messages._TRACE_STOP_TIME, eventEndTime > -1 ?\r
179 Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)\r
3b38ea61 180 : "?"); //$NON-NLS-1$\r
a5823d5f
ASL
181 } else {\r
182 addItem(Messages._TRACE_EVENT_TIME, eventStartTime > -1 ?\r
183 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)\r
3b38ea61 184 : "?"); //$NON-NLS-1$\r
a5823d5f 185 }\r
b0d3496e 186 }\r
79a3a76e 187\r
a5823d5f
ASL
188 if (eventDuration > 0) {\r
189 // Duration in relative format in any case\r
190 addItem(Messages._DURATION, eventDuration > -1 ?\r
191 Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)\r
3b38ea61 192 : "?"); //$NON-NLS-1$\r
a5823d5f 193 }\r
b0d3496e
ASL
194\r
195 } else if (item instanceof GroupItem) {\r
196 addItem(Messages._TRACE_GROUP_NAME, item.toString());\r
3b38ea61 197 addItem(Messages._NUMBER_OF_TRACES, "" + ((GroupItem) item)._traces.size()); //$NON-NLS-1$\r
b0d3496e
ASL
198 }\r
199 }\r
200\r
4e3aa37d 201 @Override\r
b0d3496e
ASL
202 public void mouseHover(MouseEvent event) {\r
203 Point pt = new Point(event.x, event.y);\r
204 Widget widget = event.widget;\r
205 Item item = null;\r
206 if (widget instanceof TmfTimeStatesCtrl) {\r
207 TmfTimeStatesCtrl threadStates = (TmfTimeStatesCtrl) widget;\r
208 item = (Item) threadStates.getItem(pt);\r
209 _tipTable.remove(0, _tipTable.getItemCount() - 1);\r
210 new TableColumn(_tipTable, SWT.NONE);\r
211 new TableColumn(_tipTable, SWT.NONE);\r
212 fillValues(pt, threadStates, item);\r
213 _tipTable.getColumn(0).setWidth(200);\r
214 _tipTable.getColumn(1).pack();\r
215 _tipTable.setSize(_tipTable.computeSize(SWT.DEFAULT, 200));\r
216 _tipShell.pack();\r
217 } else if (widget == null) {\r
218 _tipShell.setVisible(false);\r
219 _tipItem = null;\r
220 return;\r
221 }\r
222 if (item == _tipItem)\r
223 return;\r
224 _tipItem = item;\r
225 _tipPosition = control.toDisplay(pt);\r
226 _tipShell.pack();\r
227 setHoverLocation(_tipShell, _tipPosition);\r
228 _tipShell.setVisible(true);\r
229 }\r
230 });\r
231 }\r
232\r
233 private void setHoverLocation(Shell shell, Point position) {\r
234 Rectangle displayBounds = shell.getDisplay().getBounds();\r
235 Rectangle shellBounds = shell.getBounds();\r
236 shellBounds.x = Math.max(Math.min(position.x, displayBounds.width\r
237 - shellBounds.width), 0);\r
238 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height\r
239 - shellBounds.height), 0);\r
240 shell.setBounds(shellBounds);\r
241 }\r
242\r
243}
This page took 0.038042 seconds and 5 git commands to generate.