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