Commit | Line | Data |
---|---|---|
a2de198a BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | **********************************************************************/ | |
12 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts; | |
13 | ||
14 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
15 | ||
16 | import org.eclipse.jdt.annotation.NonNull; | |
a2de198a BH |
17 | import org.eclipse.jdt.annotation.Nullable; |
18 | import org.eclipse.swt.SWT; | |
19 | import org.eclipse.swt.events.MouseEvent; | |
20 | import org.eclipse.swt.events.MouseMoveListener; | |
21 | import org.eclipse.swt.events.MouseTrackListener; | |
22 | import org.eclipse.swt.events.PaintEvent; | |
23 | import org.eclipse.swt.events.PaintListener; | |
24 | import org.eclipse.swt.widgets.Display; | |
25 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
26 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
27 | import org.swtchart.IAxis; | |
28 | import org.swtchart.ISeries; | |
29 | ||
30 | /** | |
31 | * Abstract tooltip provider for xy chart viewers. It displays the y value and y | |
32 | * value of the data point of the mouse position. Extending classes can provide | |
33 | * a custom tooltip text. | |
34 | * | |
35 | * @author Bernd Hufmann | |
36 | * @since 2.0 | |
37 | */ | |
a2de198a BH |
38 | public class TmfClosestDataPointTooltipProvider extends TmfBaseProvider implements MouseTrackListener, MouseMoveListener, PaintListener { |
39 | ||
40 | // ------------------------------------------------------------------------ | |
41 | // Constants | |
42 | // ------------------------------------------------------------------------ | |
43 | private static final int ALPHA = 128; | |
44 | private static final int HIGHLIGHT_RADIUS = 5; | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Attributes | |
48 | // ------------------------------------------------------------------------ | |
49 | /** X coordinate for highlighting */ | |
50 | private int fHighlightX; | |
51 | /** y coordinate for highlighting */ | |
52 | private int fHighlightY; | |
53 | /** Flag to do highlighting or not */ | |
54 | private boolean fIsHighlight; | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Constructors | |
58 | // ------------------------------------------------------------------------ | |
59 | /** | |
60 | * Constructor for a tool tip provider. | |
61 | * | |
62 | * @param tmfChartViewer | |
63 | * - the parent chart viewer | |
64 | */ | |
65 | public TmfClosestDataPointTooltipProvider(ITmfChartTimeProvider tmfChartViewer) { | |
66 | super(tmfChartViewer); | |
67 | register(); | |
68 | } | |
69 | ||
70 | // ------------------------------------------------------------------------ | |
71 | // TmfBaseProvider | |
72 | // ------------------------------------------------------------------------ | |
73 | @Override | |
74 | public void register() { | |
75 | getChart().getPlotArea().addMouseTrackListener(this); | |
76 | getChart().getPlotArea().addMouseMoveListener(this); | |
77 | getChart().getPlotArea().addPaintListener(this); | |
78 | } | |
79 | ||
80 | @Override | |
81 | public void deregister() { | |
82 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
83 | getChart().getPlotArea().removeMouseTrackListener(this); | |
84 | getChart().getPlotArea().removeMouseMoveListener(this); | |
85 | getChart().getPlotArea().removePaintListener(this); | |
86 | } | |
87 | } | |
88 | ||
89 | @Override | |
90 | public void refresh() { | |
91 | // nothing to do | |
92 | } | |
93 | ||
94 | // ------------------------------------------------------------------------ | |
95 | // MouseTrackListener | |
96 | // ------------------------------------------------------------------------ | |
97 | @Override | |
98 | public void mouseEnter(MouseEvent e) { | |
99 | } | |
100 | ||
101 | @Override | |
102 | public void mouseExit(MouseEvent e) { | |
103 | } | |
104 | ||
105 | @Override | |
106 | public void mouseHover(MouseEvent e) { | |
107 | if ((getChartViewer().getWindowDuration() != 0) && (e != null)) { | |
108 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
109 | IAxis yAxis = getChart().getAxisSet().getYAxis(0); | |
110 | ||
111 | ISeries[] series = getChart().getSeriesSet().getSeries(); | |
112 | ||
113 | double smallestDistance = Double.MAX_VALUE; | |
114 | Parameter param = null; | |
115 | ||
116 | // go over all series | |
117 | for (int k = 0; k < series.length; k++) { | |
118 | ISeries serie = series[k]; | |
119 | double[] xS = serie.getXSeries(); | |
120 | double[] yS = serie.getYSeries(); | |
121 | ||
122 | if ((xS == null) || (yS == null)) { | |
123 | continue; | |
124 | } | |
125 | // go over all data points | |
126 | for (int i = 0; i < xS.length; i++) { | |
127 | int xs = xAxis.getPixelCoordinate(xS[i]) - e.x; | |
128 | int ys = yAxis.getPixelCoordinate(yS[i]) - e.y; | |
129 | double currentDistance = xs * xs + ys * ys; | |
130 | ||
131 | /* | |
132 | * Check for smallest distance to mouse position and | |
133 | * only consider it if the mouse is close the data point. | |
134 | */ | |
135 | if ((currentDistance < smallestDistance) && (currentDistance < (HIGHLIGHT_RADIUS * HIGHLIGHT_RADIUS))) { | |
136 | smallestDistance = currentDistance; | |
137 | fHighlightX = xs + e.x; | |
138 | fHighlightY = ys + e.y; | |
139 | if (param == null) { | |
140 | param = new Parameter(); | |
141 | } | |
142 | param.setSeriesIndex(k); | |
143 | param.setDataIndex(i); | |
144 | } | |
145 | } | |
146 | } | |
147 | String tooltip = null; | |
148 | if (param != null) { | |
149 | tooltip = createToolTipText(param); | |
150 | if (tooltip != null) { | |
151 | fIsHighlight = true; | |
152 | getChart().redraw(); | |
153 | } | |
154 | } | |
155 | /* | |
156 | * Note that tooltip might be null which will clear the | |
157 | * previous tooltip string. This is intentional. | |
158 | */ | |
159 | getChart().getPlotArea().setToolTipText(tooltip); | |
160 | } | |
161 | } | |
162 | ||
163 | // ------------------------------------------------------------------------ | |
164 | // MouseMoveListener | |
165 | // ------------------------------------------------------------------------ | |
166 | @Override | |
167 | public void mouseMove(@Nullable MouseEvent e) { | |
168 | fIsHighlight = false; | |
169 | getChart().redraw(); | |
170 | } | |
171 | ||
172 | // ------------------------------------------------------------------------ | |
173 | // PaintListener | |
174 | // ------------------------------------------------------------------------ | |
175 | @Override | |
176 | public void paintControl(PaintEvent e) { | |
177 | if (fIsHighlight && e != null) { | |
178 | e.gc.setBackground(Display.getDefault().getSystemColor( | |
179 | SWT.COLOR_RED)); | |
180 | e.gc.setAlpha(ALPHA); | |
181 | ||
182 | e.gc.fillOval(fHighlightX - HIGHLIGHT_RADIUS, fHighlightY - HIGHLIGHT_RADIUS, | |
183 | 2 * HIGHLIGHT_RADIUS, 2 * HIGHLIGHT_RADIUS); | |
184 | } | |
185 | } | |
186 | ||
187 | /** | |
188 | * Creates the tooltip based on the given parameter. | |
189 | * | |
190 | * @param param | |
191 | * parameter to create the tooltip string | |
192 | * @return the tooltip based on the given parameter. | |
193 | */ | |
194 | protected String createToolTipText(@NonNull Parameter param) { | |
195 | ISeries[] series = getChart().getSeriesSet().getSeries(); | |
196 | int seriesIndex = param.getSeriesIndex(); | |
197 | int dataIndex = param.getDataIndex(); | |
198 | if ((series != null) && (seriesIndex < series.length)) { | |
199 | ISeries serie = series[seriesIndex]; | |
200 | double[] xS = serie.getXSeries(); | |
201 | double[] yS = serie.getYSeries(); | |
202 | if ((xS != null) && (yS != null) && (dataIndex < xS.length) && (dataIndex < yS.length)) { | |
203 | StringBuffer buffer = new StringBuffer(); | |
204 | buffer.append(checkNotNull("x=")); //$NON-NLS-1$ | |
205 | buffer.append(new TmfTimestamp((long) xS[dataIndex] + getChartViewer().getTimeOffset(), ITmfTimestamp.NANOSECOND_SCALE).toString()); | |
206 | buffer.append('\n'); | |
207 | buffer.append("y="); //$NON-NLS-1$ | |
208 | buffer.append((long) yS[dataIndex]); | |
209 | return buffer.toString(); | |
210 | } | |
211 | } | |
212 | return null; | |
213 | } | |
214 | ||
215 | /** | |
216 | * Parameter class | |
217 | */ | |
218 | protected static class Parameter { | |
219 | /* A series index */ | |
220 | private int seriesIndex; | |
221 | /* A data point index within a series */ | |
222 | private int dataIndex; | |
223 | ||
224 | /** | |
225 | * @return the series index | |
226 | */ | |
227 | public int getSeriesIndex() { | |
228 | return seriesIndex; | |
229 | } | |
230 | ||
231 | /** | |
232 | * @param seriesIndex | |
233 | * index the seriesIndex to set | |
234 | */ | |
235 | public void setSeriesIndex(int seriesIndex) { | |
236 | this.seriesIndex = seriesIndex; | |
237 | } | |
238 | ||
239 | /** | |
240 | * @return the data index | |
241 | */ | |
242 | public int getDataIndex() { | |
243 | return dataIndex; | |
244 | } | |
245 | ||
246 | /** | |
247 | * @param dataIndex | |
248 | * the data index to set | |
249 | */ | |
250 | public void setDataIndex(int dataIndex) { | |
251 | this.dataIndex = dataIndex; | |
252 | } | |
253 | ||
254 | } | |
255 | } |