1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Ericsson, École Polytechnique de Montréal
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
10 * Bernd Hufmann - Initial API and implementation
11 * Geneviève Bastien - Moved some methods to TmfTimeViewer
12 **********************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
;
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
;
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
;
25 import org
.swtchart
.Chart
;
26 import org
.swtchart
.IAxis
;
27 import org
.swtchart
.ISeries
;
28 import org
.swtchart
.ISeriesSet
;
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.
35 * @author Bernd Hufmann
38 public abstract class TmfXYChartViewer
extends TmfTimeViewer
implements ITmfChartTimeProvider
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 * The offset to apply to any x position. This offset ensures better
45 * precision when converting long to double and back.
47 private long fTimeOffset
;
48 /** The SWT Chart reference */
49 private Chart fSwtChart
;
50 /** The mouse selection provider */
51 private TmfBaseProvider fMouseSelectionProvider
;
52 /** The mouse drag zoom provider */
53 private TmfBaseProvider fMouseDragZoomProvider
;
54 /** The mouse wheel zoom provider */
55 private TmfBaseProvider fMouseWheelZoomProvider
;
56 /** The tooltip provider */
57 private TmfBaseProvider fToolTipProvider
;
58 /** The middle mouse drag provider */
59 private TmfBaseProvider fMouseDragProvider
;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
66 * Constructs a TmfXYChartViewer.
69 * The parent composite
71 * The title of the viewer
73 * The label of the xAxis
75 * The label of the yAXIS
77 public TmfXYChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
79 fSwtChart
= new Chart(parent
, SWT
.NONE
);
81 IAxis xAxis
= fSwtChart
.getAxisSet().getXAxis(0);
82 IAxis yAxis
= fSwtChart
.getAxisSet().getYAxis(0);
84 /* Set the title/labels, or hide them if they are not provided */
86 fSwtChart
.getTitle().setVisible(false);
88 fSwtChart
.getTitle().setText(title
);
91 xAxis
.getTitle().setVisible(false);
93 xAxis
.getTitle().setText(xLabel
);
96 yAxis
.getTitle().setVisible(false);
98 yAxis
.getTitle().setText(yLabel
);
101 fMouseSelectionProvider
= new TmfMouseSelectionProvider(this);
102 fMouseDragZoomProvider
= new TmfMouseDragZoomProvider(this);
103 fMouseWheelZoomProvider
= new TmfMouseWheelZoomProvider(this);
104 fToolTipProvider
= new TmfSimpleTooltipProvider(this);
105 fMouseDragProvider
= new TmfMouseDragProvider(this);
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
112 * Sets the time offset to apply.
113 * @see ITmfChartTimeProvider#getTimeOffset()
116 * The time offset to apply
118 protected void setTimeOffset(long timeOffset
) {
119 fTimeOffset
= timeOffset
;
123 * Sets the SWT Chart reference
126 * The SWT chart to set.
128 protected void setSwtChart(Chart chart
) {
133 * Gets the SWT Chart reference
135 * @return the SWT chart to set.
137 protected Chart
getSwtChart() {
142 * Sets a mouse selection provider. An existing provider will be
143 * disposed. Use <code>null</code> to disable the mouse selection provider.
146 * The selection provider to set
148 public void setSelectionProvider(TmfBaseProvider provider
) {
149 if (fMouseSelectionProvider
!= null) {
150 fMouseSelectionProvider
.dispose();
152 fMouseSelectionProvider
= provider
;
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.
160 * The mouse drag zoom provider to set
162 public void setMouseDragZoomProvider(TmfBaseProvider provider
) {
163 if (fMouseDragZoomProvider
!= null) {
164 fMouseDragZoomProvider
.dispose();
166 fMouseDragZoomProvider
= provider
;
170 * Sets a mouse wheel zoom provider. An existing provider will be
171 * disposed. Use <code>null</code> to disable the mouse wheel zoom
175 * The mouse wheel zoom provider to set
177 public void setMouseWheelZoomProvider(TmfBaseProvider provider
) {
178 if (fMouseWheelZoomProvider
!= null) {
179 fMouseWheelZoomProvider
.dispose();
181 fMouseWheelZoomProvider
= provider
;
185 * Sets a tooltip provider. An existing provider will be
186 * disposed. Use <code>null</code> to disable the tooltip provider.
189 * The tooltip provider to set
191 public void setTooltipProvider(TmfBaseProvider provider
) {
192 if (fToolTipProvider
!= null) {
193 fToolTipProvider
.dispose();
195 fToolTipProvider
= provider
;
199 * Sets a mouse drag provider. An existing provider will be
200 * disposed. Use <code>null</code> to disable the mouse drag provider.
203 * The mouse drag provider to set
205 public void setMouseDrageProvider(TmfBaseProvider provider
) {
206 if (fMouseDragProvider
!= null) {
207 fMouseDragProvider
.dispose();
209 fMouseDragProvider
= provider
;
212 // ------------------------------------------------------------------------
213 // ITmfChartTimeProvider
214 // ------------------------------------------------------------------------
217 public long getTimeOffset() {
221 // ------------------------------------------------------------------------
222 // ITmfViewer interface
223 // ------------------------------------------------------------------------
225 public Control
getControl() {
230 public void refresh() {
234 // ------------------------------------------------------------------------
236 // ------------------------------------------------------------------------
238 public void dispose() {
242 if (fMouseSelectionProvider
!= null) {
243 fMouseSelectionProvider
.dispose();
246 if (fMouseDragZoomProvider
!= null) {
247 fMouseDragZoomProvider
.dispose();
250 if (fMouseWheelZoomProvider
!= null) {
251 fMouseWheelZoomProvider
.dispose();
254 if (fToolTipProvider
!= null) {
255 fToolTipProvider
.dispose();
258 if (fMouseDragProvider
!= null) {
259 fMouseDragProvider
.dispose();
263 // ------------------------------------------------------------------------
265 // ------------------------------------------------------------------------
267 * A Method to load a trace into the viewer.
270 * A trace to apply in the viewer
273 public void loadTrace(ITmfTrace trace
) {
274 super.loadTrace(trace
);
280 * Resets the content of the viewer
283 public void reset() {
289 * Method to implement to update the chart content.
291 protected abstract void updateContent();
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
298 * Signal handler for handling of the time synch signal.
301 * The time synch signal {@link TmfTimeSynchSignal}
305 public void selectionRangeUpdated(TmfTimeSynchSignal signal
) {
306 super.selectionRangeUpdated(signal
);
307 if ((signal
.getSource() != this) && (getTrace() != null)) {
308 if (fMouseSelectionProvider
!= null) {
309 fMouseSelectionProvider
.refresh();
315 * Signal handler for handling of the time range synch signal.
318 * The time range synch signal {@link TmfRangeSynchSignal}
322 public void timeRangeUpdated(TmfRangeSynchSignal signal
) {
323 super.timeRangeUpdated(signal
);
328 * Signal handler for handling the signal that notifies about an updated
332 * The trace updated signal
333 * {@link TmfTimestampFormatUpdateSignal}
336 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
337 fSwtChart
.getAxisSet().adjustRange();
341 // ------------------------------------------------------------------------
343 // ------------------------------------------------------------------------
346 * Clears the view content.
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());
360 * Returns the current or default display.
362 * @return the current or default display
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();