Commit | Line | Data |
---|---|---|
83842d7d | 1 | /********************************************************************** |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
83842d7d BH |
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.linuxtools.tmf.ui.viewers.xycharts.barcharts; | |
13 | ||
14 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
15 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
16 | import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.ITmfChartTimeProvider; | |
17 | import org.eclipse.linuxtools.tmf.ui.viewers.xycharts.TmfBaseProvider; | |
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.swtchart.IAxis; | |
26 | import org.swtchart.ISeries; | |
27 | ||
28 | /** | |
29 | * Tool tip provider for TMF bar chart viewer. It displays the y value of | |
30 | * position x as well as it highlights the bar of the x position. | |
31 | * It only considers the first series of the chart. | |
32 | * | |
33 | * @autor Bernd Hufmann | |
34 | * @since 3.0 | |
35 | */ | |
36 | public class TmfHistogramTooltipProvider extends TmfBaseProvider implements MouseTrackListener, MouseMoveListener, PaintListener { | |
37 | ||
38 | // ------------------------------------------------------------------------ | |
39 | // Attributes | |
40 | // ------------------------------------------------------------------------ | |
41 | /** X coordinate for highlighting */ | |
42 | private int fHighlightX; | |
43 | /** y coordinate for highlighting */ | |
44 | private int fHighlightY; | |
45 | /** Flag to do highlighting or not */ | |
46 | private boolean fIsHighlight; | |
47 | ||
48 | // ------------------------------------------------------------------------ | |
49 | // Constructors | |
50 | // ------------------------------------------------------------------------ | |
51 | /** | |
52 | * Constructor for a tool tip provider. | |
53 | * | |
54 | * @param tmfChartViewer | |
55 | * - the parent chart viewer | |
56 | */ | |
57 | public TmfHistogramTooltipProvider(ITmfChartTimeProvider tmfChartViewer) { | |
58 | super(tmfChartViewer); | |
59 | register(); | |
60 | } | |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // TmfBaseProvider | |
64 | // ------------------------------------------------------------------------ | |
65 | @Override | |
66 | public void register() { | |
67 | getChart().getPlotArea().addMouseTrackListener(this); | |
68 | getChart().getPlotArea().addMouseMoveListener(this); | |
69 | getChart().getPlotArea().addPaintListener(this); | |
70 | } | |
71 | ||
72 | @Override | |
73 | public void deregister() { | |
74 | if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) { | |
75 | getChart().getPlotArea().removeMouseTrackListener(this); | |
76 | getChart().getPlotArea().removeMouseMoveListener(this); | |
77 | getChart().getPlotArea().removePaintListener(this); | |
78 | } | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void refresh() { | |
83 | // nothing to do | |
84 | } | |
85 | ||
86 | // ------------------------------------------------------------------------ | |
87 | // MouseTrackListener | |
88 | // ------------------------------------------------------------------------ | |
89 | @Override | |
90 | public void mouseEnter(MouseEvent e) { | |
91 | } | |
92 | ||
93 | @Override | |
94 | public void mouseExit(MouseEvent e) { | |
95 | } | |
96 | ||
97 | @Override | |
98 | public void mouseHover(MouseEvent e) { | |
99 | if (getChartViewer().getWindowDuration() != 0) { | |
100 | IAxis xAxis = getChart().getAxisSet().getXAxis(0); | |
101 | IAxis yAxis = getChart().getAxisSet().getYAxis(0); | |
102 | ||
103 | double xCoordinate = xAxis.getDataCoordinate(e.x); | |
104 | ||
105 | ISeries[] series = getChart().getSeriesSet().getSeries(); | |
106 | ||
107 | if ((xCoordinate < 0) || (series.length == 0)) { | |
108 | return; | |
109 | } | |
110 | ||
111 | double y = 0.0; | |
112 | double rangeStart = 0.0; | |
113 | double rangeEnd = 0.0; | |
114 | ||
115 | // Consider first series only | |
116 | double[] xS = series[0].getXSeries(); | |
117 | double[] yS = series[0].getYSeries(); | |
118 | ||
119 | if ((xS == null) || (yS == null)) { | |
120 | return; | |
121 | } | |
122 | ||
123 | for (int i = 0; i < xS.length - 1; i++) { | |
124 | int pixel = xAxis.getPixelCoordinate(xS[i]); | |
125 | if (pixel <= e.x) { | |
126 | rangeStart = xS[i]; | |
127 | rangeEnd = (long) xS[i + 1]; | |
128 | if (xCoordinate >= rangeStart) { | |
129 | y = yS[i + 1]; | |
130 | } else { | |
131 | y = yS[i]; | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | ITmfChartTimeProvider viewer = getChartViewer(); | |
137 | ||
138 | /* set tooltip of closest data point */ | |
139 | StringBuffer buffer = new StringBuffer(); | |
140 | buffer.append("Range=["); //$NON-NLS-1$ | |
141 | buffer.append(new TmfTimestamp((long) rangeStart + viewer.getTimeOffset(), ITmfTimestamp.NANOSECOND_SCALE).toString()); | |
142 | buffer.append(','); | |
143 | buffer.append(new TmfTimestamp((long) rangeEnd + viewer.getTimeOffset(), ITmfTimestamp.NANOSECOND_SCALE).toString()); | |
144 | buffer.append("]\n"); //$NON-NLS-1$ | |
145 | buffer.append("y="); //$NON-NLS-1$ | |
146 | buffer.append((long) y); | |
147 | getChart().getPlotArea().setToolTipText(buffer.toString()); | |
148 | ||
149 | fHighlightX = e.x; | |
150 | fHighlightY = yAxis.getPixelCoordinate(y); | |
151 | fIsHighlight = true; | |
152 | getChart().redraw(); | |
153 | } | |
154 | } | |
155 | ||
156 | // ------------------------------------------------------------------------ | |
157 | // MouseMoveListener | |
158 | // ------------------------------------------------------------------------ | |
159 | @Override | |
160 | public void mouseMove(MouseEvent e) { | |
161 | fIsHighlight = false; | |
162 | getChart().redraw(); | |
163 | } | |
164 | ||
165 | // ------------------------------------------------------------------------ | |
166 | // PaintListener | |
167 | // ------------------------------------------------------------------------ | |
168 | @Override | |
169 | public void paintControl(PaintEvent e) { | |
170 | if (fIsHighlight) { | |
171 | e.gc.setBackground(Display.getDefault().getSystemColor( | |
172 | SWT.COLOR_RED)); | |
173 | e.gc.setAlpha(128); | |
174 | ||
175 | e.gc.fillOval(fHighlightX - 5, fHighlightY - 5, 10, 10); | |
176 | } | |
177 | } | |
178 | ||
179 | } |