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