Merge corrected branch 'master'
[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.ITimeGraphProvider;
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.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 TimeGraphTooltipHandler {
44
45 private Shell _tipShell;
46 private Table _tipTable;
47 private TimeGraphItem _tipItem;
48 private Point _tipPosition;
49 private ITimeDataProvider _timeDataProvider;
50 ITimeGraphProvider _utilImp = null;
51
52 public TimeGraphTooltipHandler(Shell parent, ITimeGraphProvider 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 //FIXME: remove old listeners
84 control.addMouseListener(new MouseAdapter() {
85 @Override
86 public void mouseDown(MouseEvent e) {
87 if (_tipShell.isVisible())
88 _tipShell.setVisible(false);
89 }
90 });
91
92 control.addMouseTrackListener(new MouseTrackAdapter() {
93 @Override
94 public void mouseExit(MouseEvent e) {
95 if (_tipShell.isVisible())
96 _tipShell.setVisible(false);
97 _tipItem = null;
98
99 }
100
101 private void addItem(String name, String value) {
102 TableItem line = new TableItem(_tipTable, SWT.NONE);
103 line.setText(0, name);
104 line.setText(1, value);
105 }
106
107 private void fillValues(Point pt, TimeGraphControl threadStates, TimeGraphItem item) {
108 if (item == null) {
109 return;
110 }
111 if (item._trace.getTimeEventsIterator() == null) {
112 addItem(Messages.TmfTimeTipHandler_TRACE_GROUP_NAME, item.toString());
113 addItem(Messages.TmfTimeTipHandler_NUMBER_OF_TRACES, "" + item.children.size()); //$NON-NLS-1$
114 } else {
115 ITimeGraphEntry thrd = item._trace;
116 ITimeEvent threadEvent = Utils.findEvent(thrd, threadStates.getTimeAtX(pt.x), 2);
117 ITimeEvent nextEvent = Utils.findEvent(thrd, threadStates.getTimeAtX(pt.x), 1);
118 // thread name
119 addItem(Messages.TmfTimeTipHandler_TRACE_NAME, thrd.getName());
120 // class name
121 String traceClass = _utilImp.getTraceClassName(thrd);
122 if (traceClass != null) {
123 addItem(Messages.TmfTimeTipHandler_TRACE_CLASS_NAME, traceClass);
124 }
125 // thread state
126 String state = _utilImp.getEventName(threadEvent);
127 if (state != null) {
128 addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state);
129 }
130
131 // This block receives a
132 // list of <String, String> values to be added to the tip
133 // table
134 Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(threadEvent);
135 if (eventAddOns != null) {
136 for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
137 String message = (String) iter.next();
138 addItem(message, eventAddOns.get(message));
139 }
140 }
141
142 long eventStartTime = -1;
143 long eventDuration = -1;
144 long eventEndTime = -1;
145
146 if (threadEvent != null) {
147 eventStartTime = threadEvent.getTime();
148 eventDuration = threadEvent.getDuration();
149 if (eventDuration < 0 && nextEvent != null) {
150 eventEndTime = nextEvent.getTime();
151 eventDuration = eventEndTime - eventStartTime;
152 } else {
153 eventEndTime = eventStartTime + eventDuration;
154 }
155 }
156
157 // TODO: Check if we need "format"
158 // TimeFormat format = TimeFormat.RELATIVE;
159 Resolution res = Resolution.NANOSEC;
160 if (_timeDataProvider.isCalendarFormat()) {
161 // format = TimeFormat.ABSOLUTE; // Absolute format
162 // // (calendar)
163 // Add Date
164 addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
165 Utils.formatDate(eventStartTime)
166 : "?"); //$NON-NLS-1$
167 if (eventDuration > 0) {
168 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
169 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
170 : "?"); //$NON-NLS-1$
171
172 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
173 Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)
174 : "?"); //$NON-NLS-1$
175 } else {
176 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
177 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
178 : "?"); //$NON-NLS-1$
179 }
180 } else {
181 if (eventDuration > 0) {
182 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
183 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
184 : "?"); //$NON-NLS-1$
185
186 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
187 Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)
188 : "?"); //$NON-NLS-1$
189 } else {
190 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
191 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
192 : "?"); //$NON-NLS-1$
193 }
194 }
195
196 if (eventDuration > 0) {
197 // Duration in relative format in any case
198 addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
199 Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)
200 : "?"); //$NON-NLS-1$
201 }
202 }
203 }
204
205 @Override
206 public void mouseHover(MouseEvent event) {
207 Point pt = new Point(event.x, event.y);
208 Widget widget = event.widget;
209 TimeGraphItem item = null;
210 if (widget instanceof TimeGraphControl) {
211 TimeGraphControl threadStates = (TimeGraphControl) widget;
212 item = (TimeGraphItem) threadStates.getItem(pt);
213 _tipTable.remove(0, _tipTable.getItemCount() - 1);
214 new TableColumn(_tipTable, SWT.NONE);
215 new TableColumn(_tipTable, SWT.NONE);
216 fillValues(pt, threadStates, item);
217 _tipTable.getColumn(0).setWidth(200);
218 _tipTable.getColumn(1).pack();
219 _tipTable.setSize(_tipTable.computeSize(SWT.DEFAULT, 200));
220 _tipShell.pack();
221 } else if (widget == null) {
222 _tipShell.setVisible(false);
223 _tipItem = null;
224 return;
225 }
226 if (item == _tipItem)
227 return;
228 _tipItem = item;
229 _tipPosition = control.toDisplay(pt);
230 _tipShell.pack();
231 setHoverLocation(_tipShell, _tipPosition);
232 _tipShell.setVisible(true);
233 }
234 });
235 }
236
237 private void setHoverLocation(Shell shell, Point position) {
238 Rectangle displayBounds = shell.getDisplay().getBounds();
239 Rectangle shellBounds = shell.getBounds();
240 shellBounds.x = Math.max(Math.min(position.x, displayBounds.width
241 - shellBounds.width), 0);
242 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height
243 - shellBounds.height), 0);
244 shell.setBounds(shellBounds);
245 }
246
247 }
This page took 0.054226 seconds and 6 git commands to generate.