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