1 /**********************************************************************
2 * Copyright (c) 2013, 2015 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
.TmfSelectionRangeUpdatedSignal
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
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 // ------------------------------------------------------------------------
42 /** The SWT Chart reference */
43 private Chart fSwtChart
;
44 /** The mouse selection provider */
45 private TmfBaseProvider fMouseSelectionProvider
;
46 /** The mouse drag zoom provider */
47 private TmfBaseProvider fMouseDragZoomProvider
;
48 /** The mouse wheel zoom provider */
49 private TmfBaseProvider fMouseWheelZoomProvider
;
50 /** The tooltip provider */
51 private TmfBaseProvider fToolTipProvider
;
52 /** The middle mouse drag provider */
53 private TmfBaseProvider fMouseDragProvider
;
55 * Whether or not to send time alignment signals. This should be set to true
56 * for viewers that are part of an aligned view.
58 private boolean fSendTimeAlignSignals
= false;
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 // ------------------------------------------------------------------------
113 * Sets the SWT Chart reference
116 * The SWT chart to set.
118 protected void setSwtChart(Chart chart
) {
123 * Gets the SWT Chart reference
125 * @return the SWT chart to set.
127 protected Chart
getSwtChart() {
132 * Sets a mouse selection provider. An existing provider will be
133 * disposed. Use <code>null</code> to disable the mouse selection provider.
136 * The selection provider to set
138 public void setSelectionProvider(TmfBaseProvider provider
) {
139 if (fMouseSelectionProvider
!= null) {
140 fMouseSelectionProvider
.dispose();
142 fMouseSelectionProvider
= provider
;
146 * Sets a mouse drag zoom provider. An existing provider will be
147 * disposed. Use <code>null</code> to disable the mouse drag zoom provider.
150 * The mouse drag zoom provider to set
152 public void setMouseDragZoomProvider(TmfBaseProvider provider
) {
153 if (fMouseDragZoomProvider
!= null) {
154 fMouseDragZoomProvider
.dispose();
156 fMouseDragZoomProvider
= provider
;
160 * Sets a mouse wheel zoom provider. An existing provider will be
161 * disposed. Use <code>null</code> to disable the mouse wheel zoom
165 * The mouse wheel zoom provider to set
167 public void setMouseWheelZoomProvider(TmfBaseProvider provider
) {
168 if (fMouseWheelZoomProvider
!= null) {
169 fMouseWheelZoomProvider
.dispose();
171 fMouseWheelZoomProvider
= provider
;
175 * Sets a tooltip provider. An existing provider will be
176 * disposed. Use <code>null</code> to disable the tooltip provider.
179 * The tooltip provider to set
181 public void setTooltipProvider(TmfBaseProvider provider
) {
182 if (fToolTipProvider
!= null) {
183 fToolTipProvider
.dispose();
185 fToolTipProvider
= provider
;
189 * Sets a mouse drag provider. An existing provider will be
190 * disposed. Use <code>null</code> to disable the mouse drag provider.
193 * The mouse drag provider to set
195 public void setMouseDrageProvider(TmfBaseProvider provider
) {
196 if (fMouseDragProvider
!= null) {
197 fMouseDragProvider
.dispose();
199 fMouseDragProvider
= provider
;
202 // ------------------------------------------------------------------------
203 // ITmfChartTimeProvider
204 // ------------------------------------------------------------------------
207 public long getTimeOffset() {
208 return getWindowStartTime() - 1;
211 // ------------------------------------------------------------------------
212 // ITmfViewer interface
213 // ------------------------------------------------------------------------
215 public Control
getControl() {
220 public void refresh() {
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
228 public void dispose() {
232 if (fMouseSelectionProvider
!= null) {
233 fMouseSelectionProvider
.dispose();
236 if (fMouseDragZoomProvider
!= null) {
237 fMouseDragZoomProvider
.dispose();
240 if (fMouseWheelZoomProvider
!= null) {
241 fMouseWheelZoomProvider
.dispose();
244 if (fToolTipProvider
!= null) {
245 fToolTipProvider
.dispose();
248 if (fMouseDragProvider
!= null) {
249 fMouseDragProvider
.dispose();
253 // ------------------------------------------------------------------------
255 // ------------------------------------------------------------------------
257 * A Method to load a trace into the viewer.
260 * A trace to apply in the viewer
263 public void loadTrace(ITmfTrace trace
) {
264 super.loadTrace(trace
);
270 * Resets the content of the viewer
273 public void reset() {
279 * Method to implement to update the chart content.
281 protected abstract void updateContent();
283 // ------------------------------------------------------------------------
285 // ------------------------------------------------------------------------
288 * Signal handler for handling of the time synch signal.
291 * The time synch signal {@link TmfSelectionRangeUpdatedSignal}
295 public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal
) {
296 super.selectionRangeUpdated(signal
);
297 if ((signal
.getSource() != this) && (getTrace() != null)) {
298 if (fMouseSelectionProvider
!= null) {
299 fMouseSelectionProvider
.refresh();
305 * Signal handler for handling of the window range signal.
308 * The {@link TmfWindowRangeUpdatedSignal}
312 public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal
) {
313 super.windowRangeUpdated(signal
);
318 * Signal handler for handling the signal that notifies about an updated
322 * The trace updated signal
323 * {@link TmfTimestampFormatUpdateSignal}
326 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
327 fSwtChart
.getAxisSet().adjustRange();
331 // ------------------------------------------------------------------------
333 // ------------------------------------------------------------------------
336 * Clears the view content.
338 protected void clearContent() {
339 if (!fSwtChart
.isDisposed()) {
340 ISeriesSet set
= fSwtChart
.getSeriesSet();
341 ISeries
[] series
= set
.getSeries();
342 for (int i
= 0; i
< series
.length
; i
++) {
343 set
.deleteSeries(series
[i
].getId());
350 * Returns the current or default display.
352 * @return the current or default display
354 protected static Display
getDisplay() {
355 Display display
= Display
.getCurrent();
356 // may be null if outside the UI thread
357 if (display
== null) {
358 display
= Display
.getDefault();
364 * Get the offset of the point area, relative to the XY chart viewer
365 * control. We consider the point area to be from where the first point
366 * could be drawn to where the last point could be drawn.
368 * @return the offset in pixels
372 public int getPointAreaOffset() {
374 int pixelCoordinate
= 0;
375 IAxis
[] xAxes
= getSwtChart().getAxisSet().getXAxes();
376 ISeries
[] series
= fSwtChart
.getSeriesSet().getSeries();
377 if ((xAxes
.length
> 0) && (series
.length
> 0) &&
378 (series
[0].getXSeries() != null) && (series
[0].getXSeries().length
> 0)) {
379 IAxis axis
= xAxes
[0];
380 // All series have the same X series
381 double[] xSeries
= series
[0].getXSeries();
382 pixelCoordinate
= axis
.getPixelCoordinate(xSeries
[0]);
384 return getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(pixelCoordinate
, 0)).x
;
388 * Get the width of the point area. We consider the point area to be from
389 * where the first point could be drawn to where the last point could be
390 * drawn. The point area differs from the plot area because there might be a
391 * gap between where the plot area start and where the fist point is drawn.
392 * This also matches the width that the use can select.
394 * @return the width in pixels
398 public int getPointAreaWidth() {
399 IAxis
[] xAxes
= getSwtChart().getAxisSet().getXAxes();
400 ISeries
[] series
= fSwtChart
.getSeriesSet().getSeries();
401 if ((xAxes
.length
> 0) && (series
.length
> 0) &&
402 (series
[0].getXSeries() != null) && (series
[0].getXSeries().length
> 0)) {
403 IAxis axis
= xAxes
[0];
404 // All series have the same X series
405 double[] xSeries
= series
[0].getXSeries();
406 int x1
= getPointAreaOffset();
407 int x2
= axis
.getPixelCoordinate(xSeries
[xSeries
.length
- 1]);
408 x2
= getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(x2
, 0)).x
;
413 return getSwtChart().getPlotArea().getSize().x
;
417 * Sets whether or not to send time alignment signals. This should be set to
418 * true for viewers that are part of an aligned view.
420 * @param sendTimeAlignSignals
421 * whether or not to send time alignment signals
424 public void setSendTimeAlignSignals(boolean sendTimeAlignSignals
) {
425 fSendTimeAlignSignals
= sendTimeAlignSignals
;
429 * Returns whether or not to send time alignment signals.
431 * @return whether or not to send time alignment signals.
434 public boolean isSendTimeAlignSignals() {
435 return fSendTimeAlignSignals
;