Commit | Line | Data |
---|---|---|
d7d40e67 | 1 | /********************************************************************** |
843c272b | 2 | * Copyright (c) 2013, 2014 Ericsson, École Polytechnique de Montréal |
d7d40e67 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 | |
843c272b | 11 | * Geneviève Bastien - Moved some methods to TmfTimeViewer |
d7d40e67 BH |
12 | **********************************************************************/ |
13 | package org.eclipse.linuxtools.tmf.ui.viewers.xycharts; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal; | |
16 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; | |
17 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; | |
18 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal; | |
d7d40e67 | 19 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
843c272b | 20 | import org.eclipse.linuxtools.tmf.ui.viewers.TmfTimeViewer; |
d7d40e67 BH |
21 | import org.eclipse.swt.SWT; |
22 | import org.eclipse.swt.widgets.Composite; | |
23 | import org.eclipse.swt.widgets.Control; | |
24 | import org.eclipse.swt.widgets.Display; | |
25 | import org.swtchart.Chart; | |
26 | import org.swtchart.IAxis; | |
27 | import org.swtchart.ISeries; | |
28 | import org.swtchart.ISeriesSet; | |
29 | ||
30 | /** | |
31 | * Base class for a XY-Chart based on SWT chart. It provides a methods to define | |
32 | * zoom, selection and tool tip providers. It also provides call backs to be | |
33 | * notified by any changes caused by selection and zoom. | |
34 | * | |
35 | * @author Bernd Hufmann | |
36 | * @since 3.0 | |
37 | */ | |
843c272b | 38 | public abstract class TmfXYChartViewer extends TmfTimeViewer implements ITmfChartTimeProvider { |
d7d40e67 BH |
39 | |
40 | // ------------------------------------------------------------------------ | |
41 | // Attributes | |
42 | // ------------------------------------------------------------------------ | |
43 | /** | |
44 | * The offset to apply to any x position. This offset ensures better | |
45 | * precision when converting long to double and back. | |
46 | */ | |
47 | private long fTimeOffset; | |
d7d40e67 BH |
48 | /** The SWT Chart reference */ |
49 | private Chart fSwtChart; | |
0b09e0cf BH |
50 | /** The mouse selection provider */ |
51 | private TmfBaseProvider fMouseSelectionProvider; | |
73920960 BH |
52 | /** The mouse drag zoom provider */ |
53 | private TmfBaseProvider fMouseDragZoomProvider; | |
4bcc4ed6 BH |
54 | /** The mouse wheel zoom provider */ |
55 | private TmfBaseProvider fMouseWheelZoomProvider; | |
5791dcef BH |
56 | /** The tooltip provider */ |
57 | private TmfBaseProvider fToolTipProvider; | |
eee2beeb BH |
58 | /** The middle mouse drag provider */ |
59 | private TmfBaseProvider fMouseDragProvider; | |
d7d40e67 BH |
60 | |
61 | // ------------------------------------------------------------------------ | |
62 | // Constructors | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | /** | |
66 | * Constructs a TmfXYChartViewer. | |
67 | * | |
68 | * @param parent | |
69 | * The parent composite | |
70 | * @param title | |
71 | * The title of the viewer | |
72 | * @param xLabel | |
73 | * The label of the xAxis | |
74 | * @param yLabel | |
75 | * The label of the yAXIS | |
76 | */ | |
77 | public TmfXYChartViewer(Composite parent, String title, String xLabel, String yLabel) { | |
78 | super(parent, title); | |
79 | fSwtChart = new Chart(parent, SWT.NONE); | |
80 | ||
81 | IAxis xAxis = fSwtChart.getAxisSet().getXAxis(0); | |
82 | IAxis yAxis = fSwtChart.getAxisSet().getYAxis(0); | |
83 | ||
84 | /* Set the title/labels, or hide them if they are not provided */ | |
85 | if (title == null) { | |
86 | fSwtChart.getTitle().setVisible(false); | |
87 | } else { | |
88 | fSwtChart.getTitle().setText(title); | |
89 | } | |
90 | if (xLabel == null) { | |
91 | xAxis.getTitle().setVisible(false); | |
92 | } else { | |
93 | xAxis.getTitle().setText(xLabel); | |
94 | } | |
95 | if (yLabel == null) { | |
96 | yAxis.getTitle().setVisible(false); | |
97 | } else { | |
98 | yAxis.getTitle().setText(yLabel); | |
99 | } | |
0b09e0cf BH |
100 | |
101 | fMouseSelectionProvider = new TmfMouseSelectionProvider(this); | |
73920960 | 102 | fMouseDragZoomProvider = new TmfMouseDragZoomProvider(this); |
4bcc4ed6 | 103 | fMouseWheelZoomProvider = new TmfMouseWheelZoomProvider(this); |
5791dcef | 104 | fToolTipProvider = new TmfSimpleTooltipProvider(this); |
eee2beeb | 105 | fMouseDragProvider = new TmfMouseDragProvider(this); |
d7d40e67 BH |
106 | } |
107 | ||
108 | // ------------------------------------------------------------------------ | |
109 | // Getter/Setters | |
110 | // ------------------------------------------------------------------------ | |
111 | /** | |
112 | * Sets the time offset to apply. | |
113 | * @see ITmfChartTimeProvider#getTimeOffset() | |
114 | * | |
115 | * @param timeOffset | |
116 | * The time offset to apply | |
117 | */ | |
118 | protected void setTimeOffset(long timeOffset) { | |
119 | fTimeOffset = timeOffset; | |
120 | } | |
121 | ||
d7d40e67 BH |
122 | /** |
123 | * Sets the SWT Chart reference | |
124 | * | |
125 | * @param chart | |
126 | * The SWT chart to set. | |
127 | */ | |
128 | protected void setSwtChart(Chart chart) { | |
129 | fSwtChart = chart; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Gets the SWT Chart reference | |
134 | * | |
135 | * @return the SWT chart to set. | |
136 | */ | |
137 | protected Chart getSwtChart() { | |
138 | return fSwtChart; | |
139 | } | |
140 | ||
0b09e0cf BH |
141 | /** |
142 | * Sets a mouse selection provider. An existing provider will be | |
143 | * disposed. Use <code>null</code> to disable the mouse selection provider. | |
144 | * | |
145 | * @param provider | |
146 | * The selection provider to set | |
147 | */ | |
148 | public void setSelectionProvider(TmfBaseProvider provider) { | |
149 | if (fMouseSelectionProvider != null) { | |
150 | fMouseSelectionProvider.dispose(); | |
151 | } | |
152 | fMouseSelectionProvider = provider; | |
153 | } | |
154 | ||
73920960 BH |
155 | /** |
156 | * Sets a mouse drag zoom provider. An existing provider will be | |
157 | * disposed. Use <code>null</code> to disable the mouse drag zoom provider. | |
158 | * | |
159 | * @param provider | |
160 | * The mouse drag zoom provider to set | |
161 | */ | |
162 | public void setMouseDragZoomProvider(TmfBaseProvider provider) { | |
163 | if (fMouseDragZoomProvider != null) { | |
164 | fMouseDragZoomProvider.dispose(); | |
165 | } | |
166 | fMouseDragZoomProvider = provider; | |
167 | } | |
168 | ||
4bcc4ed6 BH |
169 | /** |
170 | * Sets a mouse wheel zoom provider. An existing provider will be | |
171 | * disposed. Use <code>null</code> to disable the mouse wheel zoom | |
172 | * provider. | |
173 | * | |
174 | * @param provider | |
175 | * The mouse wheel zoom provider to set | |
176 | */ | |
177 | public void setMouseWheelZoomProvider(TmfBaseProvider provider) { | |
178 | if (fMouseWheelZoomProvider != null) { | |
179 | fMouseWheelZoomProvider.dispose(); | |
180 | } | |
181 | fMouseWheelZoomProvider = provider; | |
182 | } | |
183 | ||
5791dcef BH |
184 | /** |
185 | * Sets a tooltip provider. An existing provider will be | |
186 | * disposed. Use <code>null</code> to disable the tooltip provider. | |
187 | * | |
188 | * @param provider | |
189 | * The tooltip provider to set | |
190 | */ | |
191 | public void setTooltipProvider(TmfBaseProvider provider) { | |
192 | if (fToolTipProvider != null) { | |
193 | fToolTipProvider.dispose(); | |
194 | } | |
195 | fToolTipProvider = provider; | |
196 | } | |
197 | ||
eee2beeb BH |
198 | /** |
199 | * Sets a mouse drag provider. An existing provider will be | |
200 | * disposed. Use <code>null</code> to disable the mouse drag provider. | |
201 | * | |
202 | * @param provider | |
203 | * The mouse drag provider to set | |
204 | */ | |
205 | public void setMouseDrageProvider(TmfBaseProvider provider) { | |
206 | if (fMouseDragProvider != null) { | |
207 | fMouseDragProvider.dispose(); | |
208 | } | |
209 | fMouseDragProvider = provider; | |
210 | } | |
211 | ||
d7d40e67 BH |
212 | // ------------------------------------------------------------------------ |
213 | // ITmfChartTimeProvider | |
214 | // ------------------------------------------------------------------------ | |
d7d40e67 BH |
215 | |
216 | @Override | |
217 | public long getTimeOffset() { | |
218 | return fTimeOffset; | |
219 | } | |
220 | ||
221 | // ------------------------------------------------------------------------ | |
222 | // ITmfViewer interface | |
223 | // ------------------------------------------------------------------------ | |
224 | @Override | |
225 | public Control getControl() { | |
226 | return fSwtChart; | |
227 | } | |
228 | ||
229 | @Override | |
230 | public void refresh() { | |
231 | fSwtChart.redraw(); | |
232 | } | |
233 | ||
234 | // ------------------------------------------------------------------------ | |
235 | // TmfComponent | |
236 | // ------------------------------------------------------------------------ | |
237 | @Override | |
238 | public void dispose() { | |
239 | super.dispose(); | |
240 | fSwtChart.dispose(); | |
0b09e0cf BH |
241 | |
242 | if (fMouseSelectionProvider != null) { | |
243 | fMouseSelectionProvider.dispose(); | |
244 | } | |
73920960 BH |
245 | |
246 | if (fMouseDragZoomProvider != null) { | |
247 | fMouseDragZoomProvider.dispose(); | |
248 | } | |
4bcc4ed6 BH |
249 | |
250 | if (fMouseWheelZoomProvider != null) { | |
251 | fMouseWheelZoomProvider.dispose(); | |
252 | } | |
5791dcef BH |
253 | |
254 | if (fToolTipProvider != null) { | |
255 | fToolTipProvider.dispose(); | |
256 | } | |
eee2beeb BH |
257 | |
258 | if (fMouseDragProvider != null) { | |
259 | fMouseDragProvider.dispose(); | |
260 | } | |
d7d40e67 BH |
261 | } |
262 | ||
263 | // ------------------------------------------------------------------------ | |
264 | // Operations | |
265 | // ------------------------------------------------------------------------ | |
266 | /** | |
267 | * A Method to load a trace into the viewer. | |
268 | * | |
269 | * @param trace | |
270 | * A trace to apply in the viewer | |
271 | */ | |
843c272b | 272 | @Override |
d7d40e67 | 273 | public void loadTrace(ITmfTrace trace) { |
843c272b | 274 | super.loadTrace(trace); |
d7d40e67 BH |
275 | clearContent(); |
276 | updateContent(); | |
277 | } | |
278 | ||
279 | /** | |
280 | * Resets the content of the viewer | |
281 | */ | |
843c272b | 282 | @Override |
d7d40e67 | 283 | public void reset() { |
843c272b | 284 | super.reset(); |
d7d40e67 BH |
285 | clearContent(); |
286 | } | |
287 | ||
288 | /** | |
289 | * Method to implement to update the chart content. | |
290 | */ | |
291 | protected abstract void updateContent(); | |
292 | ||
293 | // ------------------------------------------------------------------------ | |
294 | // Signal Handler | |
295 | // ------------------------------------------------------------------------ | |
296 | ||
d7d40e67 BH |
297 | /** |
298 | * Signal handler for handling of the time synch signal. | |
299 | * | |
300 | * @param signal | |
301 | * The time synch signal {@link TmfTimeSynchSignal} | |
302 | */ | |
843c272b | 303 | @Override |
d7d40e67 BH |
304 | @TmfSignalHandler |
305 | public void selectionRangeUpdated(TmfTimeSynchSignal signal) { | |
843c272b GB |
306 | super.selectionRangeUpdated(signal); |
307 | if ((signal.getSource() != this) && (getTrace() != null)) { | |
0b09e0cf BH |
308 | if (fMouseSelectionProvider != null) { |
309 | fMouseSelectionProvider.refresh(); | |
310 | } | |
d7d40e67 BH |
311 | } |
312 | } | |
313 | ||
314 | /** | |
315 | * Signal handler for handling of the time range synch signal. | |
316 | * | |
317 | * @param signal | |
318 | * The time range synch signal {@link TmfRangeSynchSignal} | |
319 | */ | |
843c272b | 320 | @Override |
d7d40e67 BH |
321 | @TmfSignalHandler |
322 | public void timeRangeUpdated(TmfRangeSynchSignal signal) { | |
843c272b | 323 | super.timeRangeUpdated(signal); |
d7d40e67 BH |
324 | updateContent(); |
325 | } | |
326 | ||
d7d40e67 BH |
327 | /** |
328 | * Signal handler for handling the signal that notifies about an updated | |
329 | * timestamp format. | |
330 | * | |
331 | * @param signal | |
332 | * The trace updated signal | |
333 | * {@link TmfTimestampFormatUpdateSignal} | |
334 | */ | |
335 | @TmfSignalHandler | |
336 | public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) { | |
337 | fSwtChart.getAxisSet().adjustRange(); | |
338 | fSwtChart.redraw(); | |
339 | } | |
340 | ||
341 | // ------------------------------------------------------------------------ | |
342 | // Helper Methods | |
343 | // ------------------------------------------------------------------------ | |
344 | ||
345 | /** | |
346 | * Clears the view content. | |
347 | */ | |
348 | protected void clearContent() { | |
349 | if (!fSwtChart.isDisposed()) { | |
350 | ISeriesSet set = fSwtChart.getSeriesSet(); | |
351 | ISeries[] series = set.getSeries(); | |
352 | for (int i = 0; i < series.length; i++) { | |
353 | set.deleteSeries(series[i].getId()); | |
354 | } | |
355 | fSwtChart.redraw(); | |
356 | } | |
357 | } | |
358 | ||
359 | /** | |
360 | * Returns the current or default display. | |
361 | * | |
362 | * @return the current or default display | |
363 | */ | |
364 | protected static Display getDisplay() { | |
365 | Display display = Display.getCurrent(); | |
366 | // may be null if outside the UI thread | |
367 | if (display == null) { | |
368 | display = Display.getDefault(); | |
369 | } | |
370 | return display; | |
371 | } | |
372 | ||
373 | } |