6e7bba9616e60d2819b8d00154c338f0028998fe
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / xycharts / TmfClosestDataPointTooltipProvider.java
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 org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.MouseEvent;
18 import org.eclipse.swt.events.MouseMoveListener;
19 import org.eclipse.swt.events.MouseTrackListener;
20 import org.eclipse.swt.events.PaintEvent;
21 import org.eclipse.swt.events.PaintListener;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
24 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
25 import org.swtchart.IAxis;
26 import org.swtchart.ISeries;
27
28 /**
29 * Abstract tooltip provider for xy chart viewers. It displays the y value and y
30 * value of the data point of the mouse position. Extending classes can provide
31 * a custom tooltip text.
32 *
33 * @author Bernd Hufmann
34 * @since 2.0
35 */
36 public class TmfClosestDataPointTooltipProvider extends TmfBaseProvider implements MouseTrackListener, MouseMoveListener, PaintListener {
37
38 // ------------------------------------------------------------------------
39 // Constants
40 // ------------------------------------------------------------------------
41 private static final int ALPHA = 128;
42 private static final int HIGHLIGHT_RADIUS = 5;
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47 /** X coordinate for highlighting */
48 private int fHighlightX;
49 /** y coordinate for highlighting */
50 private int fHighlightY;
51 /** Flag to do highlighting or not */
52 private boolean fIsHighlight;
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57 /**
58 * Constructor for a tool tip provider.
59 *
60 * @param tmfChartViewer
61 * - the parent chart viewer
62 */
63 public TmfClosestDataPointTooltipProvider(ITmfChartTimeProvider tmfChartViewer) {
64 super(tmfChartViewer);
65 register();
66 }
67
68 // ------------------------------------------------------------------------
69 // TmfBaseProvider
70 // ------------------------------------------------------------------------
71 @Override
72 public void register() {
73 getChart().getPlotArea().addMouseTrackListener(this);
74 getChart().getPlotArea().addMouseMoveListener(this);
75 getChart().getPlotArea().addPaintListener(this);
76 }
77
78 @Override
79 public void deregister() {
80 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
81 getChart().getPlotArea().removeMouseTrackListener(this);
82 getChart().getPlotArea().removeMouseMoveListener(this);
83 getChart().getPlotArea().removePaintListener(this);
84 }
85 }
86
87 @Override
88 public void refresh() {
89 // nothing to do
90 }
91
92 // ------------------------------------------------------------------------
93 // MouseTrackListener
94 // ------------------------------------------------------------------------
95 @Override
96 public void mouseEnter(MouseEvent e) {
97 }
98
99 @Override
100 public void mouseExit(MouseEvent e) {
101 }
102
103 @Override
104 public void mouseHover(MouseEvent e) {
105 if ((getChartViewer().getWindowDuration() != 0) && (e != null)) {
106 IAxis xAxis = getChart().getAxisSet().getXAxis(0);
107 IAxis yAxis = getChart().getAxisSet().getYAxis(0);
108
109 ISeries[] series = getChart().getSeriesSet().getSeries();
110
111 double smallestDistance = Double.MAX_VALUE;
112 Parameter param = null;
113
114 // go over all series
115 for (int k = 0; k < series.length; k++) {
116 ISeries serie = series[k];
117 double[] xS = serie.getXSeries();
118 double[] yS = serie.getYSeries();
119
120 if ((xS == null) || (yS == null)) {
121 continue;
122 }
123 // go over all data points
124 for (int i = 0; i < xS.length; i++) {
125 int xs = xAxis.getPixelCoordinate(xS[i]) - e.x;
126 int ys = yAxis.getPixelCoordinate(yS[i]) - e.y;
127 double currentDistance = xs * xs + ys * ys;
128
129 /*
130 * Check for smallest distance to mouse position and
131 * only consider it if the mouse is close the data point.
132 */
133 if ((currentDistance < smallestDistance) && (currentDistance < (HIGHLIGHT_RADIUS * HIGHLIGHT_RADIUS))) {
134 smallestDistance = currentDistance;
135 fHighlightX = xs + e.x;
136 fHighlightY = ys + e.y;
137 if (param == null) {
138 param = new Parameter();
139 }
140 param.setSeriesIndex(k);
141 param.setDataIndex(i);
142 }
143 }
144 }
145 String tooltip = null;
146 if (param != null) {
147 tooltip = createToolTipText(param);
148 if (tooltip != null) {
149 fIsHighlight = true;
150 getChart().redraw();
151 }
152 }
153 /*
154 * Note that tooltip might be null which will clear the
155 * previous tooltip string. This is intentional.
156 */
157 getChart().getPlotArea().setToolTipText(tooltip);
158 }
159 }
160
161 // ------------------------------------------------------------------------
162 // MouseMoveListener
163 // ------------------------------------------------------------------------
164 @Override
165 public void mouseMove(@Nullable MouseEvent e) {
166 if (fIsHighlight) {
167 fIsHighlight = false;
168 getChart().redraw();
169 }
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("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 }
This page took 0.036666 seconds and 4 git commands to generate.