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 * Patrick Tasse - Fix setFocus
13 **********************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.events
.MouseAdapter
;
18 import org
.eclipse
.swt
.events
.MouseEvent
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Control
;
21 import org
.eclipse
.swt
.widgets
.Display
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.TmfTimeViewer
;
28 import org
.swtchart
.Chart
;
29 import org
.swtchart
.IAxis
;
30 import org
.swtchart
.ISeries
;
31 import org
.swtchart
.ISeriesSet
;
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.
38 * @author Bernd Hufmann
40 public abstract class TmfXYChartViewer
extends TmfTimeViewer
implements ITmfChartTimeProvider
{
42 // ------------------------------------------------------------------------
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
;
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.
61 private boolean fSendTimeAlignSignals
= false;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Constructs a TmfXYChartViewer.
72 * The parent composite
74 * The title of the viewer
76 * The label of the xAxis
78 * The label of the yAXIS
80 public TmfXYChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
82 fSwtChart
= new Chart(parent
, SWT
.NONE
) {
84 public boolean setFocus() {
85 return fSwtChart
.getPlotArea().setFocus();
88 fSwtChart
.getPlotArea().addMouseListener(new MouseAdapter() {
90 public void mouseDown(MouseEvent e
) {
91 fSwtChart
.getPlotArea().setFocus();
95 IAxis xAxis
= fSwtChart
.getAxisSet().getXAxis(0);
96 IAxis yAxis
= fSwtChart
.getAxisSet().getYAxis(0);
98 /* Set the title/labels, or hide them if they are not provided */
100 fSwtChart
.getTitle().setVisible(false);
102 fSwtChart
.getTitle().setText(title
);
104 if (xLabel
== null) {
105 xAxis
.getTitle().setVisible(false);
107 xAxis
.getTitle().setText(xLabel
);
109 if (yLabel
== null) {
110 yAxis
.getTitle().setVisible(false);
112 yAxis
.getTitle().setText(yLabel
);
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);
122 // ------------------------------------------------------------------------
124 // ------------------------------------------------------------------------
127 * Sets the SWT Chart reference
130 * The SWT chart to set.
132 protected void setSwtChart(Chart chart
) {
137 * Gets the SWT Chart reference
139 * @return the SWT chart to set.
141 protected Chart
getSwtChart() {
146 * Sets a mouse selection provider. An existing provider will be
147 * disposed. Use <code>null</code> to disable the mouse selection provider.
150 * The selection provider to set
152 public void setSelectionProvider(TmfBaseProvider provider
) {
153 if (fMouseSelectionProvider
!= null) {
154 fMouseSelectionProvider
.dispose();
156 fMouseSelectionProvider
= provider
;
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.
164 * The mouse drag zoom provider to set
166 public void setMouseDragZoomProvider(TmfBaseProvider provider
) {
167 if (fMouseDragZoomProvider
!= null) {
168 fMouseDragZoomProvider
.dispose();
170 fMouseDragZoomProvider
= provider
;
174 * Sets a mouse wheel zoom provider. An existing provider will be
175 * disposed. Use <code>null</code> to disable the mouse wheel zoom
179 * The mouse wheel zoom provider to set
181 public void setMouseWheelZoomProvider(TmfBaseProvider provider
) {
182 if (fMouseWheelZoomProvider
!= null) {
183 fMouseWheelZoomProvider
.dispose();
185 fMouseWheelZoomProvider
= provider
;
189 * Sets a tooltip provider. An existing provider will be
190 * disposed. Use <code>null</code> to disable the tooltip provider.
193 * The tooltip provider to set
195 public void setTooltipProvider(TmfBaseProvider provider
) {
196 if (fToolTipProvider
!= null) {
197 fToolTipProvider
.dispose();
199 fToolTipProvider
= provider
;
203 * Sets a mouse drag provider. An existing provider will be
204 * disposed. Use <code>null</code> to disable the mouse drag provider.
207 * The mouse drag provider to set
209 public void setMouseDrageProvider(TmfBaseProvider provider
) {
210 if (fMouseDragProvider
!= null) {
211 fMouseDragProvider
.dispose();
213 fMouseDragProvider
= provider
;
216 // ------------------------------------------------------------------------
217 // ITmfChartTimeProvider
218 // ------------------------------------------------------------------------
221 public long getTimeOffset() {
222 return getWindowStartTime() - 1;
225 // ------------------------------------------------------------------------
226 // ITmfViewer interface
227 // ------------------------------------------------------------------------
229 public Control
getControl() {
234 public void refresh() {
238 // ------------------------------------------------------------------------
240 // ------------------------------------------------------------------------
242 public void dispose() {
246 if (fMouseSelectionProvider
!= null) {
247 fMouseSelectionProvider
.dispose();
250 if (fMouseDragZoomProvider
!= null) {
251 fMouseDragZoomProvider
.dispose();
254 if (fMouseWheelZoomProvider
!= null) {
255 fMouseWheelZoomProvider
.dispose();
258 if (fToolTipProvider
!= null) {
259 fToolTipProvider
.dispose();
262 if (fMouseDragProvider
!= null) {
263 fMouseDragProvider
.dispose();
267 // ------------------------------------------------------------------------
269 // ------------------------------------------------------------------------
271 * A Method to load a trace into the viewer.
274 * A trace to apply in the viewer
277 public void loadTrace(ITmfTrace trace
) {
278 super.loadTrace(trace
);
284 * Resets the content of the viewer
287 public void reset() {
293 * Method to implement to update the chart content.
295 protected abstract void updateContent();
297 // ------------------------------------------------------------------------
299 // ------------------------------------------------------------------------
302 * Signal handler for handling of the time synch signal.
305 * The time synch signal {@link TmfSelectionRangeUpdatedSignal}
309 public void selectionRangeUpdated(TmfSelectionRangeUpdatedSignal signal
) {
310 super.selectionRangeUpdated(signal
);
311 if ((signal
.getSource() != this) && (getTrace() != null)) {
312 if (fMouseSelectionProvider
!= null) {
313 fMouseSelectionProvider
.refresh();
319 * Signal handler for handling of the window range signal.
322 * The {@link TmfWindowRangeUpdatedSignal}
326 public void windowRangeUpdated(TmfWindowRangeUpdatedSignal signal
) {
327 super.windowRangeUpdated(signal
);
332 * Signal handler for handling the signal that notifies about an updated
336 * The trace updated signal
337 * {@link TmfTimestampFormatUpdateSignal}
340 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
341 fSwtChart
.getAxisSet().adjustRange();
345 // ------------------------------------------------------------------------
347 // ------------------------------------------------------------------------
350 * Clears the view content.
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());
364 * Returns the current or default display.
366 * @return the current or default display
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();
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.
382 * @return the offset in pixels
386 public int getPointAreaOffset() {
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]);
398 return getSwtChart().toControl(getSwtChart().getPlotArea().toDisplay(pixelCoordinate
, 0)).x
;
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.
408 * @return the width in pixels
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
;
427 return getSwtChart().getPlotArea().getSize().x
;
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.
434 * @param sendTimeAlignSignals
435 * whether or not to send time alignment signals
438 public void setSendTimeAlignSignals(boolean sendTimeAlignSignals
) {
439 fSendTimeAlignSignals
= sendTimeAlignSignals
;
443 * Returns whether or not to send time alignment signals.
445 * @return whether or not to send time alignment signals.
448 public boolean isSendTimeAlignSignals() {
449 return fSendTimeAlignSignals
;