Fix for bug289620
[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
b0d3496e 20import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.TmfTimeAnalysisProvider;\r
79a3a76e 21import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.Messages;\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
b0d3496e
ASL
84 public void mouseDown(MouseEvent e) {\r
85 if (_tipShell.isVisible())\r
86 _tipShell.setVisible(false);\r
87 }\r
88 });\r
89\r
90 control.addMouseTrackListener(new MouseTrackAdapter() {\r
b0d3496e
ASL
91 public void mouseExit(MouseEvent e) {\r
92 if (_tipShell.isVisible())\r
93 _tipShell.setVisible(false);\r
94 _tipItem = null;\r
95\r
96 }\r
97\r
98 private void addItem(String name, String value) {\r
99 TableItem line = new TableItem(_tipTable, SWT.NONE);\r
100 line.setText(0, name);\r
101 line.setText(1, value);\r
102 }\r
103\r
104 private void fillValues(Point pt, TmfTimeStatesCtrl threadStates,\r
105 Item item) {\r
106 if (item instanceof TraceItem) {\r
107 ITmfTimeAnalysisEntry thrd = ((TraceItem) item)._trace;\r
108 ITimeEvent threadEvent = Utils.findEvent(thrd, threadStates\r
109 .hitTimeTest(pt.x, pt.y), 2);\r
110 ITimeEvent nextEvent = Utils.findEvent(thrd, threadStates\r
111 .hitTimeTest(pt.x, pt.y), 1);\r
112 // thread name\r
113 addItem(Messages._TRACE_NAME, thrd.getName());\r
114 // class name\r
115 addItem(Messages._TRACE_CLASS_NAME, _utilImp\r
116 .getTraceClassName(thrd));\r
117 // thread state\r
118 addItem(Messages._TRACE_STATE, _utilImp\r
119 .getEventName(threadEvent));\r
120\r
121 // This block receives a\r
122 // list of <String, String> values to be added to the tip\r
123 // table\r
124 Map<String, String> eventAddOns = _utilImp\r
125 .getEventHoverToolTipInfo(threadEvent);\r
126 for (Iterator<String> iter = eventAddOns.keySet()\r
127 .iterator(); iter.hasNext();) {\r
128 String message = (String) iter.next();\r
129 addItem(message, eventAddOns.get(message));\r
130 }\r
131\r
79a3a76e
FC
132 // start time\r
133 long startTime = threadEvent == null ? thrd.getStartTime()\r
134 : threadEvent.getTime();\r
135\r
136 long duration = threadEvent == null ? -1 : threadEvent\r
137 .getDuration();\r
138\r
139 if (duration < 0 && threadEvent != null\r
140 && nextEvent != null) {\r
141 long stopTime = nextEvent.getTime();\r
142 duration = stopTime - startTime;\r
b0d3496e 143 }\r
79a3a76e 144 long eventEndtime = startTime + duration;\r
b0d3496e
ASL
145\r
146// TODO: Check if we need "format" \r
147// TimeFormat format = TimeFormat.RELATIVE;\r
148 Resolution res = Resolution.NANOSEC;\r
149 if (_timeDataProvider.isCalendarFormat()) {\r
150// format = TimeFormat.ABSOLUTE; // Absolute format\r
151// // (calendar)\r
152 // Add Date\r
79a3a76e
FC
153 addItem(Messages._TRACE_DATE, Utils\r
154 .formatDate(startTime));\r
b0d3496e 155 }\r
79a3a76e
FC
156 addItem(Messages._TRACE_START_TIME, Utils.formatTime(\r
157 startTime, TimeFormat.RELATIVE, res));\r
b0d3496e 158\r
79a3a76e
FC
159 addItem(Messages._TRACE_STOP_TIME, Utils.formatTime(\r
160 eventEndtime, TimeFormat.RELATIVE, res));\r
161\r
162 // Duration in relative format in any case\r
163 addItem(Messages._DURATION, duration > -1 ? Utils\r
164 .formatTime(duration, TimeFormat.RELATIVE, res)\r
165 : "?");\r
b0d3496e
ASL
166\r
167 } else if (item instanceof GroupItem) {\r
168 addItem(Messages._TRACE_GROUP_NAME, item.toString());\r
169 addItem(Messages._NUMBER_OF_TRACES, ""\r
170 + ((GroupItem) item)._traces.size());\r
171 }\r
172 }\r
173\r
b0d3496e
ASL
174 public void mouseHover(MouseEvent event) {\r
175 Point pt = new Point(event.x, event.y);\r
176 Widget widget = event.widget;\r
177 Item item = null;\r
178 if (widget instanceof TmfTimeStatesCtrl) {\r
179 TmfTimeStatesCtrl threadStates = (TmfTimeStatesCtrl) widget;\r
180 item = (Item) threadStates.getItem(pt);\r
181 _tipTable.remove(0, _tipTable.getItemCount() - 1);\r
182 new TableColumn(_tipTable, SWT.NONE);\r
183 new TableColumn(_tipTable, SWT.NONE);\r
184 fillValues(pt, threadStates, item);\r
185 _tipTable.getColumn(0).setWidth(200);\r
186 _tipTable.getColumn(1).pack();\r
187 _tipTable.setSize(_tipTable.computeSize(SWT.DEFAULT, 200));\r
188 _tipShell.pack();\r
189 } else if (widget == null) {\r
190 _tipShell.setVisible(false);\r
191 _tipItem = null;\r
192 return;\r
193 }\r
194 if (item == _tipItem)\r
195 return;\r
196 _tipItem = item;\r
197 _tipPosition = control.toDisplay(pt);\r
198 _tipShell.pack();\r
199 setHoverLocation(_tipShell, _tipPosition);\r
200 _tipShell.setVisible(true);\r
201 }\r
202 });\r
203 }\r
204\r
205 private void setHoverLocation(Shell shell, Point position) {\r
206 Rectangle displayBounds = shell.getDisplay().getBounds();\r
207 Rectangle shellBounds = shell.getBounds();\r
208 shellBounds.x = Math.max(Math.min(position.x, displayBounds.width\r
209 - shellBounds.width), 0);\r
210 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height\r
211 - shellBounds.height), 0);\r
212 shell.setBounds(shellBounds);\r
213 }\r
214\r
215}
This page took 0.032349 seconds and 5 git commands to generate.