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