Commit | Line | Data |
---|---|---|
d7d40e67 | 1 | /********************************************************************** |
4e72adee | 2 | * Copyright (c) 2013, 2015 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 |
abf7b9b0 | 12 | * Patrick Tasse - Fix setFocus |
d7d40e67 | 13 | **********************************************************************/ |
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.ui.viewers.xycharts; |
d7d40e67 | 15 | |
d7d40e67 | 16 | import org.eclipse.swt.SWT; |
abf7b9b0 PT |
17 | import org.eclipse.swt.events.MouseAdapter; |
18 | import org.eclipse.swt.events.MouseEvent; | |
d7d40e67 BH |
19 | import org.eclipse.swt.widgets.Composite; |
20 | import org.eclipse.swt.widgets.Control; | |
21 | import org.eclipse.swt.widgets.Display; | |
97c71024 | 22 | import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal; |
54404589 | 23 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
2bdf0193 | 24 | import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal; |
54404589 | 25 | import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal; |
2bdf0193 AM |
26 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
27 | import org.eclipse.tracecompass.tmf.ui.viewers.TmfTimeViewer; | |
d7d40e67 BH |
28 | import org.swtchart.Chart; |
29 | import org.swtchart.IAxis; | |
30 | import org.swtchart.ISeries; | |
31 | import org.swtchart.ISeriesSet; | |
32 | ||
33 | /** | |
34 | * Base class for a XY-Chart based on SWT chart. It provides a methods to define | |
35 | * zoom, selection and tool tip providers. It also provides call backs to be | |
36 | * notified by any changes caused by selection and zoom. | |
37 | * | |
38 | * @author Bernd Hufmann | |
d7d40e67 | 39 | */ |
843c272b | 40 | public abstract class TmfXYChartViewer extends TmfTimeViewer implements ITmfChartTimeProvider { |
d7d40e67 BH |
41 | |
42 | // ------------------------------------------------------------------------ | |
43 | // Attributes | |
44 | // ------------------------------------------------------------------------ | |
d7d40e67 BH |
45 | /** The SWT Chart reference */ |
46 | private Chart fSwtChart; | |
0b09e0cf BH |
47 | /** The mouse selection provider */ |
48 | private TmfBaseProvider fMouseSelectionProvider; | |
73920960 BH |
49 | /** The mouse drag zoom provider */ |
50 | private TmfBaseProvider fMouseDragZoomProvider; | |
4bcc4ed6 BH |
51 | /** The mouse wheel zoom provider */ |
52 | private TmfBaseProvider fMouseWheelZoomProvider; | |
5791dcef BH |
53 | /** The tooltip provider */ |
54 | private TmfBaseProvider fToolTipProvider; | |
eee2beeb BH |
55 | /** The middle mouse drag provider */ |
56 | private TmfBaseProvider fMouseDragProvider; | |
54404589 MAL |
57 | /** |
58 | * Whether or not to send time alignment signals. This should be set to true | |
59 | * for viewers that are part of an aligned view. | |
60 | */ | |
61 | private boolean fSendTimeAlignSignals = false; | |
62 | ||
d7d40e67 BH |
63 | |
64 | // ------------------------------------------------------------------------ | |
65 | // Constructors | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
68 | /** | |
69 | * Constructs a TmfXYChartViewer. | |
70 | * | |
71 | * @param parent | |
72 | * The parent composite | |
73 | * @param title | |
74 | * The title of the viewer | |
75 | * @param xLabel | |
76 | * The label of the xAxis | |
77 | * @param yLabel | |
78 | * The label of the yAXIS | |
79 | */ | |
80 | public TmfXYChartViewer(Composite parent, String title, String xLabel, String yLabel) { | |
81 | super(parent, title); | |
abf7b9b0 PT |
82 | fSwtChart = new Chart(parent, SWT.NONE) { |
83 | @Override | |
84 | public boolean setFocus() { | |
85 | return fSwtChart.getPlotArea().setFocus(); | |
86 | } | |
87 | }; | |
88 | fSwtChart.getPlotArea().addMouseListener(new MouseAdapter() { | |
89 | @Override | |
90 | public void mouseDown(MouseEvent e) { | |
91 | fSwtChart.getPlotArea().setFocus(); | |
92 | } | |
93 | }); | |
d7d40e67 BH |
94 | |
95 | IAxis xAxis = fSwtChart.getAxisSet().getXAxis(0); | |
96 | IAxis yAxis = fSwtChart.getAxisSet().getYAxis(0); | |
97 | ||
98 | /* Set the title/labels, or hide them if they are not provided */ | |
99 | if (title == null) { | |
100 | fSwtChart.getTitle().setVisible(false); | |
101 | } else { | |
102 | fSwtChart.getTitle().setText(title); | |
103 | } | |
104 | if (xLabel == null) { | |
105 | xAxis.getTitle().setVisible(false); | |
106 | } else { | |
107 | xAxis.getTitle().setText(xLabel); | |
108 | } | |
109 | if (yLabel == null) { | |
110 | yAxis.getTitle().setVisible(false); | |
111 | } else { | |
112 | yAxis.getTitle().setText(yLabel); | |
113 | } | |
0b09e0cf BH |
114 | |
115 | fMouseSelectionProvider = new TmfMouseSelectionProvider(this); | |
73920960 | 116 | fMouseDragZoomProvider = new TmfMouseDragZoomProvider(this); |
4bcc4ed6 | 117 | fMouseWheelZoomProvider = new TmfMouseWheelZoomProvider(this); |
5791dcef | 118 | fToolTipProvider = new TmfSimpleTooltipProvider(this); |
eee2beeb | 119 | fMouseDragProvider = new TmfMouseDragProvider(this); |
d7d40e67 BH |
120 | } |
121 | ||
122 | // ------------------------------------------------------------------------ | |
123 | // Getter/Setters | |
124 | // ------------------------------------------------------------------------ | |
d7d40e67 | 125 | |
d7d40e67 BH |
126 | /** |
127 | * Sets the SWT Chart reference | |
128 | * | |
129 | * @param chart | |
130 | * The SWT chart to set. | |
131 | */ | |
132 | protected void setSwtChart(Chart chart) { | |
133 | fSwtChart = chart; | |
134 | } | |
135 | ||
136 | /** | |
137 | * Gets the SWT Chart reference | |
138 | * | |
139 | * @return the SWT chart to set. | |
140 | */ | |
141 | protected Chart getSwtChart() { | |
142 | return fSwtChart; | |
143 | } | |
144 | ||
0b09e0cf BH |
145 | /** |
146 | * Sets a mouse selection provider. An existing provider will be | |
147 | * disposed. Use <code>null</code> to disable the mouse selection provider. | |
148 | * | |
149 | * @param provider | |
150 | * The selection provider to set | |
151 | */ | |
152 | public void setSelectionProvider(TmfBaseProvider provider) { | |
153 | if (fMouseSelectionProvider != null) { | |
154 | fMouseSelectionProvider.dispose(); | |
155 | } | |
156 | fMouseSelectionProvider = provider; | |
157 | } | |
158 | ||
73920960 BH |
159 | /** |
160 | * Sets a mouse drag zoom provider. An existing provider will be | |
161 | * disposed. Use <code>null</code> to disable the mouse drag zoom provider. | |
162 | * | |
163 | * @param provider | |
164 | * The mouse drag zoom provider to set | |
165 | */ | |
166 | public void setMouseDragZoomProvider(TmfBaseProvider provider) { | |
167 | if (fMouseDragZoomProvider != null) { | |
168 | fMouseDragZoomProvider.dispose(); | |
169 | } | |
170 | fMouseDragZoomProvider = provider; | |
171 | } | |
172 | ||
4bcc4ed6 BH |
173 | /** |
174 | * Sets a mouse wheel zoom provider. An existing provider will be | |
175 | * disposed. Use <code>null</code> to disable the mouse wheel zoom | |
176 | * provider. | |
177 | * | |
178 | * @param provider | |
179 | * The mouse wheel zoom provider to set | |
180 | */ | |
181 | public void setMouseWheelZoomProvider(TmfBaseProvider provider) { | |
182 | if (fMouseWheelZoomProvider != null) { | |
183 | fMouseWheelZoomProvider.dispose(); | |
184 | } | |
185 | fMouseWheelZoomProvider = provider; | |
186 | } | |
187 | ||
5791dcef BH |
188 | /** |
189 | * Sets a tooltip provider. An existing provider will be | |
190 | * disposed. Use <code>null</code> to disable the tooltip provider. | |
191 | * | |
192 | * @param provider | |
193 | * The tooltip provider to set | |
194 | */ | |
195 | public void setTooltipProvider(TmfBaseProvider provider) { | |
196 | if (fToolTipProvider != null) { | |
197 | fToolTipProvider.dispose(); | |
198 | } | |
199 | fToolTipProvider = provider; | |
200 | } | |
201 | ||
eee2beeb BH |
202 | /** |
203 | * Sets a mouse drag provider. An existing provider will be | |
204 | * disposed. Use <code>null</code> to disable the mouse drag provider. | |
205 | * | |
206 | * @param provider | |
207 | * The mouse drag provider to set | |
208 | */ | |
209 | public void setMouseDrageProvider(TmfBaseProvider provider) { | |
210 | if (fMouseDragProvider != null) { | |
211 | fMouseDragProvider.dispose(); | |
212 | } | |
213 | fMouseDragProvider = provider; | |
214 | } | |
215 | ||
d7d40e67 BH |
216 | // ------------------------------------------------------------------------ |
217 | // ITmfChartTimeProvider | |
218 | // ------------------------------------------------------------------------ | |
d7d40e67 BH |
219 | |
220 | @Override | |
221 | public long getTimeOffset() { | |
4e72adee | 222 | return getWindowStartTime() - 1; |
d7d40e67 BH |
223 | } |
224 | ||
225 | // ------------------------------------------------------------------------ | |
226 | // ITmfViewer interface | |
227 | // ------------------------------------------------------------------------ | |
228 | @Override | |
229 | public Control getControl() { | |
230 | return fSwtChart; | |
231 | } | |
232 | ||
233 | @Override | |
234 | public void refresh() { | |
235 | fSwtChart.redraw(); | |
236 | } | |
237 | ||
238 | // ------------------------------------------------------------------------ | |
239 | // TmfComponent | |
240 | // ------------------------------------------------------------------------ | |
241 | @Override | |
242 | public void dispose() { | |
243 | super.dispose(); | |
244 | fSwtChart.dispose(); | |
0b09e0cf BH |
245 | |
246 | if (fMouseSelectionProvider != null) { | |
247 | fMouseSelectionProvider.dispose(); | |
248 | } | |
73920960 BH |
249 | |
250 | if (fMouseDragZoomProvider != null) { | |
251 | fMouseDragZoomProvider.dispose(); | |
252 | } | |
4bcc4ed6 BH |
253 | |
254 | if (fMouseWheelZoomProvider != null) { | |
255 | fMouseWheelZoomProvider.dispose(); | |
256 | } | |
5791dcef BH |
257 | |
258 | if (fToolTipProvider != null) { | |
259 | fToolTipProvider.dispose(); | |
260 | } | |
eee2beeb BH |
261 | |
262 | if (fMouseDragProvider != null) { | |
263 | fMouseDragProvider.dispose(); | |
264 | } | |
d7d40e67 BH |
265 | } |
266 | ||
267 | // ------------------------------------------------------------------------ | |
268 | // Operations | |
269 | // ------------------------------------------------------------------------ | |
270 | /** | |
271 | * A Method to load a trace into the viewer. | |
272 | * | |
273 | * @param trace | |
274 | * A trace to apply in the viewer | |
275 | */ | |
843c272b | 276 | @Override |
d7d40e67 | 277 | public void loadTrace(ITmfTrace trace) { |
843c272b | 278 | super.loadTrace(trace); |
d7d40e67 BH |
279 | clearContent(); |
280 | updateContent(); | |
281 | } | |
282 | ||
283 | /** | |
284 | * Resets the content of the viewer | |
285 | */ | |
843c272b | 286 | @Override |
d7d40e67 | 287 | public void reset() { |
843c272b | 288 | super.reset(); |
d7d40e67 BH |
289 | clearContent(); |
290 | } | |
291 | ||
292 | /** | |
293 | * Method to implement to update the chart content. | |
294 | */ | |
295 | protected abstract void updateContent(); | |
296 | ||
297 | // ------------------------------------------------------------------------ | |
298 | // Signal Handler | |
299 | // ------------------------------------------------------------------------ | |
300 | ||
d7d40e67 BH |
301 | /** |
302 | * Signal handler for handling of the time synch signal. | |
303 | * | |
304 | * @param signal | |
97c71024 | 305 | * The time synch signal {@link TmfSelectionRangeUpdatedSignal} |
d7d40e67 | 306 | */ |
843c272b | 307 | @Override |
d7d40e67 | 308 | @TmfSignalHandler |
97c71024 | 309 | public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal) { |
843c272b GB |
310 | super.selectionRangeUpdated(signal); |
311 | if ((signal.getSource() != this) && (getTrace() != null)) { | |
0b09e0cf BH |
312 | if (fMouseSelectionProvider != null) { |
313 | fMouseSelectionProvider.refresh(); | |
314 | } | |
d7d40e67 BH |
315 | } |
316 | } | |
317 | ||
318 | /** | |
97c71024 | 319 | * Signal handler for handling of the window range signal. |
d7d40e67 BH |
320 | * |
321 | * @param signal | |
97c71024 | 322 | * The {@link TmfWindowRangeUpdatedSignal} |
d7d40e67 | 323 | */ |
843c272b | 324 | @Override |
d7d40e67 | 325 | @TmfSignalHandler |
97c71024 AM |
326 | public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal) { |
327 | super.windowRangeUpdated(signal); | |
d7d40e67 BH |
328 | updateContent(); |
329 | } | |
330 | ||
d7d40e67 BH |
331 | /** |
332 | * Signal handler for handling the signal that notifies about an updated | |
333 | * timestamp format. | |
334 | * | |
335 | * @param signal | |
336 | * The trace updated signal | |
337 | * {@link TmfTimestampFormatUpdateSignal} | |
338 | */ | |
339 | @TmfSignalHandler | |
340 | public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) { | |
341 | fSwtChart.getAxisSet().adjustRange(); | |
342 | fSwtChart.redraw(); | |
343 | } | |
344 | ||
345 | // ------------------------------------------------------------------------ | |
346 | // Helper Methods | |
347 | // ------------------------------------------------------------------------ | |
348 | ||
349 | /** | |
350 | * Clears the view content. | |
351 | */ | |
352 | protected void clearContent() { | |
353 | if (!fSwtChart.isDisposed()) { | |
354 | ISeriesSet set = fSwtChart.getSeriesSet(); | |
355 | ISeries[] series = set.getSeries(); | |
356 | for (int i = 0; i < series.length; i++) { | |
357 | set.deleteSeries(series[i].getId()); | |
358 | } | |
359 | fSwtChart.redraw(); | |
360 | } | |
361 | } | |
362 | ||
363 | /** | |
364 | * Returns the current or default display. | |
365 | * | |
366 | * @return the current or default display | |
367 | */ | |
368 | protected static Display getDisplay() { | |
369 | Display display = Display.getCurrent(); | |
370 | // may be null if outside the UI thread | |
371 | if (display == null) { | |
372 | display = Display.getDefault(); | |
373 | } | |
374 | return display; | |
375 | } | |
376 | ||
54404589 MAL |
377 | /** |
378 | * Get the offset of the point area, relative to the XY chart viewer | |
379 | * control. We consider the point area to be from where the first point | |
380 | * could be drawn to where the last point could be drawn. | |
381 | * | |
382 | * @return the offset in pixels | |
383 | * | |
384 | * @since 1.0 | |
385 | */ | |
386 | public int getPointAreaOffset() { | |
7c56e8ed | 387 | |
54404589 MAL |
388 | int pixelCoordinate = 0; |
389 | IAxis[] xAxes = getSwtChart().getAxisSet().getXAxes(); | |
d7408049 BH |
390 | ISeries[] series = fSwtChart.getSeriesSet().getSeries(); |
391 | if ((xAxes.length > 0) && (series.length > 0) && | |
392 | (series[0].getXSeries() != null) && (series[0].getXSeries().length > 0)) { | |
54404589 | 393 | IAxis axis = xAxes[0]; |
d7408049 BH |
394 | // All series have the same X series |
395 | double[] xSeries = series[0].getXSeries(); | |
396 | pixelCoordinate = axis.getPixelCoordinate(xSeries[0]); | |
54404589 MAL |
397 | } |
398 | return getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(pixelCoordinate, 0)).x; | |
399 | } | |
400 | ||
401 | /** | |
402 | * Get the width of the point area. We consider the point area to be from | |
403 | * where the first point could be drawn to where the last point could be | |
404 | * drawn. The point area differs from the plot area because there might be a | |
405 | * gap between where the plot area start and where the fist point is drawn. | |
406 | * This also matches the width that the use can select. | |
407 | * | |
408 | * @return the width in pixels | |
409 | * | |
410 | * @since 1.0 | |
411 | */ | |
412 | public int getPointAreaWidth() { | |
413 | IAxis[] xAxes = getSwtChart().getAxisSet().getXAxes(); | |
d7408049 BH |
414 | ISeries[] series = fSwtChart.getSeriesSet().getSeries(); |
415 | if ((xAxes.length > 0) && (series.length > 0) && | |
416 | (series[0].getXSeries() != null) && (series[0].getXSeries().length > 0)) { | |
54404589 | 417 | IAxis axis = xAxes[0]; |
d7408049 BH |
418 | // All series have the same X series |
419 | double[] xSeries = series[0].getXSeries(); | |
54404589 | 420 | int x1 = getPointAreaOffset(); |
d7408049 | 421 | int x2 = axis.getPixelCoordinate(xSeries[xSeries.length - 1]); |
54404589 MAL |
422 | x2 = getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(x2, 0)).x; |
423 | int width = x2 - x1; | |
424 | return width; | |
425 | } | |
426 | ||
427 | return getSwtChart().getPlotArea().getSize().x; | |
428 | } | |
429 | ||
54404589 MAL |
430 | /** |
431 | * Sets whether or not to send time alignment signals. This should be set to | |
432 | * true for viewers that are part of an aligned view. | |
433 | * | |
434 | * @param sendTimeAlignSignals | |
435 | * whether or not to send time alignment signals | |
436 | * @since 1.0 | |
437 | */ | |
438 | public void setSendTimeAlignSignals(boolean sendTimeAlignSignals) { | |
439 | fSendTimeAlignSignals = sendTimeAlignSignals; | |
440 | } | |
441 | ||
442 | /** | |
443 | * Returns whether or not to send time alignment signals. | |
444 | * | |
445 | * @return whether or not to send time alignment signals. | |
446 | * @since 1.0 | |
447 | */ | |
448 | public boolean isSendTimeAlignSignals() { | |
449 | return fSendTimeAlignSignals; | |
450 | } | |
d7d40e67 | 451 | } |