Upgrade to Tycho 0.23.0
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / xycharts / TmfXYChartViewer.java
... / ...
CommitLineData
1/**********************************************************************
2 * Copyright (c) 2013, 2015 Ericsson, École Polytechnique de Montréal
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 * Geneviève Bastien - Moved some methods to TmfTimeViewer
12 * Patrick Tasse - Fix setFocus
13 **********************************************************************/
14package org.eclipse.tracecompass.tmf.ui.viewers.xycharts;
15
16import org.eclipse.swt.SWT;
17import org.eclipse.swt.events.MouseAdapter;
18import org.eclipse.swt.events.MouseEvent;
19import org.eclipse.swt.widgets.Composite;
20import org.eclipse.swt.widgets.Control;
21import org.eclipse.swt.widgets.Display;
22import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
23import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
24import org.eclipse.tracecompass.tmf.core.signal.TmfTimestampFormatUpdateSignal;
25import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
26import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
27import org.eclipse.tracecompass.tmf.ui.viewers.TmfTimeViewer;
28import org.swtchart.Chart;
29import org.swtchart.IAxis;
30import org.swtchart.ISeries;
31import 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
39 */
40public abstract class TmfXYChartViewer extends TmfTimeViewer implements ITmfChartTimeProvider {
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45 /** The SWT Chart reference */
46 private Chart fSwtChart;
47 /** The mouse selection provider */
48 private TmfBaseProvider fMouseSelectionProvider;
49 /** The mouse drag zoom provider */
50 private TmfBaseProvider fMouseDragZoomProvider;
51 /** The mouse wheel zoom provider */
52 private TmfBaseProvider fMouseWheelZoomProvider;
53 /** The tooltip provider */
54 private TmfBaseProvider fToolTipProvider;
55 /** The middle mouse drag provider */
56 private TmfBaseProvider fMouseDragProvider;
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
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);
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 });
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 }
114
115 fMouseSelectionProvider = new TmfMouseSelectionProvider(this);
116 fMouseDragZoomProvider = new TmfMouseDragZoomProvider(this);
117 fMouseWheelZoomProvider = new TmfMouseWheelZoomProvider(this);
118 fToolTipProvider = new TmfSimpleTooltipProvider(this);
119 fMouseDragProvider = new TmfMouseDragProvider(this);
120 }
121
122 // ------------------------------------------------------------------------
123 // Getter/Setters
124 // ------------------------------------------------------------------------
125
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
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
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
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
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
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
216 // ------------------------------------------------------------------------
217 // ITmfChartTimeProvider
218 // ------------------------------------------------------------------------
219
220 @Override
221 public long getTimeOffset() {
222 return getWindowStartTime() - 1;
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();
245
246 if (fMouseSelectionProvider != null) {
247 fMouseSelectionProvider.dispose();
248 }
249
250 if (fMouseDragZoomProvider != null) {
251 fMouseDragZoomProvider.dispose();
252 }
253
254 if (fMouseWheelZoomProvider != null) {
255 fMouseWheelZoomProvider.dispose();
256 }
257
258 if (fToolTipProvider != null) {
259 fToolTipProvider.dispose();
260 }
261
262 if (fMouseDragProvider != null) {
263 fMouseDragProvider.dispose();
264 }
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 */
276 @Override
277 public void loadTrace(ITmfTrace trace) {
278 super.loadTrace(trace);
279 clearContent();
280 updateContent();
281 }
282
283 /**
284 * Resets the content of the viewer
285 */
286 @Override
287 public void reset() {
288 super.reset();
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
301 /**
302 * Signal handler for handling of the time synch signal.
303 *
304 * @param signal
305 * The time synch signal {@link TmfSelectionRangeUpdatedSignal}
306 */
307 @Override
308 @TmfSignalHandler
309 public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal) {
310 super.selectionRangeUpdated(signal);
311 if ((signal.getSource() != this) && (getTrace() != null)) {
312 if (fMouseSelectionProvider != null) {
313 fMouseSelectionProvider.refresh();
314 }
315 }
316 }
317
318 /**
319 * Signal handler for handling of the window range signal.
320 *
321 * @param signal
322 * The {@link TmfWindowRangeUpdatedSignal}
323 */
324 @Override
325 @TmfSignalHandler
326 public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal) {
327 super.windowRangeUpdated(signal);
328 updateContent();
329 }
330
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
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() {
387
388 int pixelCoordinate = 0;
389 IAxis[] xAxes = getSwtChart().getAxisSet().getXAxes();
390 ISeries[] series = fSwtChart.getSeriesSet().getSeries();
391 if ((xAxes.length > 0) && (series.length > 0) &&
392 (series[0].getXSeries() != null) && (series[0].getXSeries().length > 0)) {
393 IAxis axis = xAxes[0];
394 // All series have the same X series
395 double[] xSeries = series[0].getXSeries();
396 pixelCoordinate = axis.getPixelCoordinate(xSeries[0]);
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();
414 ISeries[] series = fSwtChart.getSeriesSet().getSeries();
415 if ((xAxes.length > 0) && (series.length > 0) &&
416 (series[0].getXSeries() != null) && (series[0].getXSeries().length > 0)) {
417 IAxis axis = xAxes[0];
418 // All series have the same X series
419 double[] xSeries = series[0].getXSeries();
420 int x1 = getPointAreaOffset();
421 int x2 = axis.getPixelCoordinate(xSeries[xSeries.length - 1]);
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
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 }
451}
This page took 0.027608 seconds and 5 git commands to generate.