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