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
.TmfWindowRangeUpdatedSignal
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
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
37 public abstract class TmfXYChartViewer
extends TmfTimeViewer
implements ITmfChartTimeProvider
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
43 * The offset to apply to any x position. This offset ensures better
44 * precision when converting long to double and back.
46 private long fTimeOffset
;
47 /** The SWT Chart reference */
48 private Chart fSwtChart
;
49 /** The mouse selection provider */
50 private TmfBaseProvider fMouseSelectionProvider
;
51 /** The mouse drag zoom provider */
52 private TmfBaseProvider fMouseDragZoomProvider
;
53 /** The mouse wheel zoom provider */
54 private TmfBaseProvider fMouseWheelZoomProvider
;
55 /** The tooltip provider */
56 private TmfBaseProvider fToolTipProvider
;
57 /** The middle mouse drag provider */
58 private TmfBaseProvider fMouseDragProvider
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * Constructs a TmfXYChartViewer.
68 * The parent composite
70 * The title of the viewer
72 * The label of the xAxis
74 * The label of the yAXIS
76 public TmfXYChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
78 fSwtChart
= new Chart(parent
, SWT
.NONE
);
80 IAxis xAxis
= fSwtChart
.getAxisSet().getXAxis(0);
81 IAxis yAxis
= fSwtChart
.getAxisSet().getYAxis(0);
83 /* Set the title/labels, or hide them if they are not provided */
85 fSwtChart
.getTitle().setVisible(false);
87 fSwtChart
.getTitle().setText(title
);
90 xAxis
.getTitle().setVisible(false);
92 xAxis
.getTitle().setText(xLabel
);
95 yAxis
.getTitle().setVisible(false);
97 yAxis
.getTitle().setText(yLabel
);
100 fMouseSelectionProvider
= new TmfMouseSelectionProvider(this);
101 fMouseDragZoomProvider
= new TmfMouseDragZoomProvider(this);
102 fMouseWheelZoomProvider
= new TmfMouseWheelZoomProvider(this);
103 fToolTipProvider
= new TmfSimpleTooltipProvider(this);
104 fMouseDragProvider
= new TmfMouseDragProvider(this);
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
111 * Sets the time offset to apply.
112 * @see ITmfChartTimeProvider#getTimeOffset()
115 * The time offset to apply
117 protected void setTimeOffset(long timeOffset
) {
118 fTimeOffset
= timeOffset
;
122 * Sets the SWT Chart reference
125 * The SWT chart to set.
127 protected void setSwtChart(Chart chart
) {
132 * Gets the SWT Chart reference
134 * @return the SWT chart to set.
136 protected Chart
getSwtChart() {
141 * Sets a mouse selection provider. An existing provider will be
142 * disposed. Use <code>null</code> to disable the mouse selection provider.
145 * The selection provider to set
147 public void setSelectionProvider(TmfBaseProvider provider
) {
148 if (fMouseSelectionProvider
!= null) {
149 fMouseSelectionProvider
.dispose();
151 fMouseSelectionProvider
= provider
;
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.
159 * The mouse drag zoom provider to set
161 public void setMouseDragZoomProvider(TmfBaseProvider provider
) {
162 if (fMouseDragZoomProvider
!= null) {
163 fMouseDragZoomProvider
.dispose();
165 fMouseDragZoomProvider
= provider
;
169 * Sets a mouse wheel zoom provider. An existing provider will be
170 * disposed. Use <code>null</code> to disable the mouse wheel zoom
174 * The mouse wheel zoom provider to set
176 public void setMouseWheelZoomProvider(TmfBaseProvider provider
) {
177 if (fMouseWheelZoomProvider
!= null) {
178 fMouseWheelZoomProvider
.dispose();
180 fMouseWheelZoomProvider
= provider
;
184 * Sets a tooltip provider. An existing provider will be
185 * disposed. Use <code>null</code> to disable the tooltip provider.
188 * The tooltip provider to set
190 public void setTooltipProvider(TmfBaseProvider provider
) {
191 if (fToolTipProvider
!= null) {
192 fToolTipProvider
.dispose();
194 fToolTipProvider
= provider
;
198 * Sets a mouse drag provider. An existing provider will be
199 * disposed. Use <code>null</code> to disable the mouse drag provider.
202 * The mouse drag provider to set
204 public void setMouseDrageProvider(TmfBaseProvider provider
) {
205 if (fMouseDragProvider
!= null) {
206 fMouseDragProvider
.dispose();
208 fMouseDragProvider
= provider
;
211 // ------------------------------------------------------------------------
212 // ITmfChartTimeProvider
213 // ------------------------------------------------------------------------
216 public long getTimeOffset() {
220 // ------------------------------------------------------------------------
221 // ITmfViewer interface
222 // ------------------------------------------------------------------------
224 public Control
getControl() {
229 public void refresh() {
233 // ------------------------------------------------------------------------
235 // ------------------------------------------------------------------------
237 public void dispose() {
241 if (fMouseSelectionProvider
!= null) {
242 fMouseSelectionProvider
.dispose();
245 if (fMouseDragZoomProvider
!= null) {
246 fMouseDragZoomProvider
.dispose();
249 if (fMouseWheelZoomProvider
!= null) {
250 fMouseWheelZoomProvider
.dispose();
253 if (fToolTipProvider
!= null) {
254 fToolTipProvider
.dispose();
257 if (fMouseDragProvider
!= null) {
258 fMouseDragProvider
.dispose();
262 // ------------------------------------------------------------------------
264 // ------------------------------------------------------------------------
266 * A Method to load a trace into the viewer.
269 * A trace to apply in the viewer
272 public void loadTrace(ITmfTrace trace
) {
273 super.loadTrace(trace
);
279 * Resets the content of the viewer
282 public void reset() {
288 * Method to implement to update the chart content.
290 protected abstract void updateContent();
292 // ------------------------------------------------------------------------
294 // ------------------------------------------------------------------------
297 * Signal handler for handling of the time synch signal.
300 * The time synch signal {@link TmfSelectionRangeUpdatedSignal}
304 public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal
) {
305 super.selectionRangeUpdated(signal
);
306 if ((signal
.getSource() != this) && (getTrace() != null)) {
307 if (fMouseSelectionProvider
!= null) {
308 fMouseSelectionProvider
.refresh();
314 * Signal handler for handling of the window range signal.
317 * The {@link TmfWindowRangeUpdatedSignal}
321 public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal
) {
322 super.windowRangeUpdated(signal
);
327 * Signal handler for handling the signal that notifies about an updated
331 * The trace updated signal
332 * {@link TmfTimestampFormatUpdateSignal}
335 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
336 fSwtChart
.getAxisSet().adjustRange();
340 // ------------------------------------------------------------------------
342 // ------------------------------------------------------------------------
345 * Clears the view content.
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());
359 * Returns the current or default display.
361 * @return the current or default display
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();