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 ITimeGraphEntry thrd = item._trace;
123 ITimeEvent threadEvent = Utils.findEvent(thrd, threadStates.getTimeAtX(pt.x), 2);
124 ITimeEvent nextEvent = Utils.findEvent(thrd, threadStates.getTimeAtX(pt.x), 1);
125 // state name
126 addItem(_utilImp.getStateTypeName(), thrd.getName());
127 if (threadEvent == null) {
128 return;
129 }
130 // thread state
131 String state = _utilImp.getEventName(threadEvent);
132 if (state != null) {
133 addItem(Messages.TmfTimeTipHandler_TRACE_STATE, state);
134 }
135
136 // This block receives a
137 // list of <String, String> values to be added to the tip
138 // table
139 Map<String, String> eventAddOns = _utilImp.getEventHoverToolTipInfo(threadEvent);
140 if (eventAddOns != null) {
141 for (Iterator<String> iter = eventAddOns.keySet().iterator(); iter.hasNext();) {
142 String message = (String) iter.next();
143 addItem(message, eventAddOns.get(message));
144 }
145 }
146
147 long eventStartTime = -1;
148 long eventDuration = -1;
149 long eventEndTime = -1;
150
151 if (threadEvent != null) {
152 eventStartTime = threadEvent.getTime();
153 eventDuration = threadEvent.getDuration();
154 if (eventDuration < 0 && nextEvent != null) {
155 eventEndTime = nextEvent.getTime();
156 eventDuration = eventEndTime - eventStartTime;
157 } else {
158 eventEndTime = eventStartTime + eventDuration;
159 }
160 }
161
162 // TODO: Check if we need "format"
163 // TimeFormat format = TimeFormat.RELATIVE;
164 Resolution res = Resolution.NANOSEC;
165 if (_timeDataProvider.isCalendarFormat()) {
166 // format = TimeFormat.ABSOLUTE; // Absolute format
167 // // (calendar)
168 // Add Date
169 addItem(Messages.TmfTimeTipHandler_TRACE_DATE, eventStartTime > -1 ?
170 Utils.formatDate(eventStartTime)
171 : "?"); //$NON-NLS-1$
172 if (eventDuration > 0) {
173 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
174 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
175 : "?"); //$NON-NLS-1$
176
177 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
178 Utils.formatTime(eventEndTime, TimeFormat.ABSOLUTE, res)
179 : "?"); //$NON-NLS-1$
180 } else {
181 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
182 Utils.formatTime(eventStartTime, TimeFormat.ABSOLUTE, res)
183 : "?"); //$NON-NLS-1$
184 }
185 } else {
186 if (eventDuration > 0) {
187 addItem(Messages.TmfTimeTipHandler_TRACE_START_TIME, eventStartTime > -1 ?
188 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
189 : "?"); //$NON-NLS-1$
190
191 addItem(Messages.TmfTimeTipHandler_TRACE_STOP_TIME, eventEndTime > -1 ?
192 Utils.formatTime(eventEndTime, TimeFormat.RELATIVE, res)
193 : "?"); //$NON-NLS-1$
194 } else {
195 addItem(Messages.TmfTimeTipHandler_TRACE_EVENT_TIME, eventStartTime > -1 ?
196 Utils.formatTime(eventStartTime, TimeFormat.RELATIVE, res)
197 : "?"); //$NON-NLS-1$
198 }
199 }
200
201 if (eventDuration > 0) {
202 // Duration in relative format in any case
203 addItem(Messages.TmfTimeTipHandler_DURATION, eventDuration > -1 ?
204 Utils.formatTime(eventDuration, TimeFormat.RELATIVE, res)
205 : "?"); //$NON-NLS-1$
206 }
207 }
208 }
209
210 @Override
211 public void mouseHover(MouseEvent event) {
212 Point pt = new Point(event.x, event.y);
213 TimeGraphControl threadStates = (TimeGraphControl) event.widget;
214 TimeGraphItem item = threadStates.getItem(pt);
215 _tipTable.remove(0, _tipTable.getItemCount() - 1);
216 fillValues(pt, threadStates, item);
217 if (_tipTable.getItemCount() == 0) {
218 return;
219 }
220 _tipTable.getColumn(0).pack();
221 _tipTable.getColumn(1).pack();
222 _tipShell.pack();
223 _tipPosition = control.toDisplay(pt);
224 _tipShell.pack();
225 setHoverLocation(_tipShell, _tipPosition);
226 _tipShell.setVisible(true);
227 }
228 });
229 }
230
231 private void setHoverLocation(Shell shell, Point position) {
232 Rectangle displayBounds = shell.getDisplay().getBounds();
233 Rectangle shellBounds = shell.getBounds();
234 shellBounds.x = Math.max(Math.min(position.x, displayBounds.width
235 - shellBounds.width), 0);
236 shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height
237 - shellBounds.height), 0);
238 shell.setBounds(shellBounds);
239 }
240
241 }
This page took 0.035894 seconds and 6 git commands to generate.