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