1 /**********************************************************************
2 * Copyright (c) 2013 Ericsson
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 **********************************************************************/
12 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
;
14 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
15 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.TmfViewer
;
29 import org
.eclipse
.swt
.SWT
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.swtchart
.Chart
;
34 import org
.swtchart
.IAxis
;
35 import org
.swtchart
.ISeries
;
36 import org
.swtchart
.ISeriesSet
;
39 * Base class for a XY-Chart based on SWT chart. It provides a methods to define
40 * zoom, selection and tool tip providers. It also provides call backs to be
41 * notified by any changes caused by selection and zoom.
43 * @author Bernd Hufmann
46 public abstract class TmfXYChartViewer
extends TmfViewer
implements ITmfChartTimeProvider
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 * The offset to apply to any x position. This offset ensures better
53 * precision when converting long to double and back.
55 private long fTimeOffset
;
56 /** Start time of trace */
57 private long fStartTime
;
58 /** End time of trace */
59 private long fEndTime
;
60 /** Start time of current time range */
61 private long fWindowStartTime
;
62 /** End time of current time range */
63 private long fWindowEndTime
;
64 /** Duration of current time range */
65 private long fWindowDuration
;
66 /** Current begin time of selection range */
67 private long fSelectionBeginTime
;
68 /** Current end of selection range */
69 private long fSelectionEndTime
;
70 /** The trace that is displayed by this viewer */
71 private ITmfTrace fTrace
;
72 /** The SWT Chart reference */
73 private Chart fSwtChart
;
74 /** The mouse selection provider */
75 private TmfBaseProvider fMouseSelectionProvider
;
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
82 * Constructs a TmfXYChartViewer.
85 * The parent composite
87 * The title of the viewer
89 * The label of the xAxis
91 * The label of the yAXIS
93 public TmfXYChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
95 fSwtChart
= new Chart(parent
, SWT
.NONE
);
97 IAxis xAxis
= fSwtChart
.getAxisSet().getXAxis(0);
98 IAxis yAxis
= fSwtChart
.getAxisSet().getYAxis(0);
100 /* Set the title/labels, or hide them if they are not provided */
102 fSwtChart
.getTitle().setVisible(false);
104 fSwtChart
.getTitle().setText(title
);
106 if (xLabel
== null) {
107 xAxis
.getTitle().setVisible(false);
109 xAxis
.getTitle().setText(xLabel
);
111 if (yLabel
== null) {
112 yAxis
.getTitle().setVisible(false);
114 yAxis
.getTitle().setText(yLabel
);
117 fMouseSelectionProvider
= new TmfMouseSelectionProvider(this);
120 // ------------------------------------------------------------------------
122 // ------------------------------------------------------------------------
124 * Sets the time offset to apply.
125 * @see ITmfChartTimeProvider#getTimeOffset()
128 * The time offset to apply
130 protected void setTimeOffset(long timeOffset
) {
131 fTimeOffset
= timeOffset
;
135 * Sets the start time of the trace
138 * The start time to set
140 protected void setStartTime(long startTime
) {
141 fStartTime
= startTime
;
145 * Sets the end time of the trace
148 * The start time to set
150 protected void setEndTime(long endTime
) {
155 * Sets the start time of the current time range window
157 * @param windowStartTime
158 * The start time to set
160 protected void setWindowStartTime(long windowStartTime
) {
161 fWindowStartTime
= windowStartTime
;
165 * Sets the end time of the current time range window
167 * @param windowEndTime
168 * The start time to set
170 protected void setWindowEndTime(long windowEndTime
) {
171 fWindowEndTime
= windowEndTime
;
175 * Sets the start time of the current time range window
177 * @param windowDuration
178 * The start time to set
180 protected void setWindowDuration(long windowDuration
) {
181 fWindowDuration
= windowDuration
;
185 * Sets the begin time of the selection range.
187 * @param selectionBeginTime
188 * The begin time to set
190 protected void setSelectionBeginTime(long selectionBeginTime
) {
191 fSelectionBeginTime
= selectionBeginTime
;
195 * Sets the end time of the selection range.
197 * @param selectionEndTime
198 * The end time to set
200 protected void setSelectionEndTime(long selectionEndTime
) {
201 fSelectionEndTime
= selectionEndTime
;
205 * Sets the trace that is displayed by this viewer.
210 protected void setTrace(ITmfTrace trace
) {
215 * Gets the trace that is displayed by this viewer.
219 protected ITmfTrace
getTrace() {
224 * Sets the SWT Chart reference
227 * The SWT chart to set.
229 protected void setSwtChart(Chart chart
) {
234 * Gets the SWT Chart reference
236 * @return the SWT chart to set.
238 protected Chart
getSwtChart() {
243 * Sets a mouse selection provider. An existing provider will be
244 * disposed. Use <code>null</code> to disable the mouse selection provider.
247 * The selection provider to set
249 public void setSelectionProvider(TmfBaseProvider provider
) {
250 if (fMouseSelectionProvider
!= null) {
251 fMouseSelectionProvider
.dispose();
253 fMouseSelectionProvider
= provider
;
256 // ------------------------------------------------------------------------
257 // ITmfChartTimeProvider
258 // ------------------------------------------------------------------------
260 public long getStartTime() {
265 public long getEndTime() {
270 public long getWindowStartTime() {
271 return fWindowStartTime
;
275 public long getWindowEndTime() {
276 return fWindowEndTime
;
280 public long getWindowDuration() {
281 return fWindowDuration
;
285 public long getSelectionBeginTime() {
286 return fSelectionBeginTime
;
290 public long getSelectionEndTime() {
291 return fSelectionEndTime
;
295 public long getTimeOffset() {
300 public void updateSelectionRange(final long currentBeginTime
, final long currentEndTime
) {
301 if (fTrace
!= null) {
302 setSelectionBeginTime(currentBeginTime
);
303 setSelectionEndTime(currentEndTime
);
305 final ITmfTimestamp startTimestamp
= new TmfTimestamp(fSelectionBeginTime
, ITmfTimestamp
.NANOSECOND_SCALE
);
306 final ITmfTimestamp endTimestamp
= new TmfTimestamp(fSelectionEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
);
308 TmfTimeSynchSignal signal
= new TmfTimeSynchSignal(TmfXYChartViewer
.this, startTimestamp
, endTimestamp
);
313 // ------------------------------------------------------------------------
314 // ITmfViewer interface
315 // ------------------------------------------------------------------------
317 public Control
getControl() {
322 public void refresh() {
326 // ------------------------------------------------------------------------
328 // ------------------------------------------------------------------------
330 public void dispose() {
334 if (fMouseSelectionProvider
!= null) {
335 fMouseSelectionProvider
.dispose();
339 // ------------------------------------------------------------------------
341 // ------------------------------------------------------------------------
343 * A Method to load a trace into the viewer.
346 * A trace to apply in the viewer
348 public void loadTrace(ITmfTrace trace
) {
351 long timestamp
= TmfTraceManager
.getInstance().getSelectionBeginTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
352 long windowStartTime
= TmfTraceManager
.getInstance().getCurrentRange().getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
353 long startTime
= fTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
354 long endTime
= fTrace
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
356 setSelectionBeginTime(timestamp
);
357 setSelectionEndTime(timestamp
);
358 setStartTime(startTime
);
359 setWindowStartTime(windowStartTime
);
360 setWindowDuration(fTrace
.getInitialRangeOffset().getValue());
362 setWindowEndTime(windowStartTime
+ getWindowDuration());
368 * Resets the content of the viewer
370 public void reset() {
371 // Reset the internal data
372 setSelectionBeginTime(0);
373 setSelectionEndTime(0);
375 setWindowStartTime(0);
376 setWindowDuration(0);
384 * Method to implement to update the chart content.
386 protected abstract void updateContent();
388 // ------------------------------------------------------------------------
390 // ------------------------------------------------------------------------
393 * Signal handler for handling of the trace opened signal.
396 * The trace opened signal {@link TmfTraceOpenedSignal}
399 public void traceOpened(TmfTraceOpenedSignal signal
) {
400 fTrace
= signal
.getTrace();
401 loadTrace(getTrace());
405 * Signal handler for handling of the trace selected signal.
408 * The trace selected signal {@link TmfTraceSelectedSignal}
411 public void traceSelected(TmfTraceSelectedSignal signal
) {
412 if (fTrace
!= signal
.getTrace()) {
413 fTrace
= signal
.getTrace();
414 loadTrace(getTrace());
419 * Signal handler for handling of the trace closed signal.
422 * The trace closed signal {@link TmfTraceClosedSignal}
425 public void traceClosed(TmfTraceClosedSignal signal
) {
427 if (signal
.getTrace() != fTrace
) {
431 // Reset the internal data
437 * Signal handler for handling of the time synch signal.
440 * The time synch signal {@link TmfTimeSynchSignal}
443 public void selectionRangeUpdated(TmfTimeSynchSignal signal
) {
444 if ((signal
.getSource() != this) && (fTrace
!= null)) {
445 ITmfTimestamp selectedTime
= signal
.getBeginTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
);
446 ITmfTimestamp selectedEndTime
= signal
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
);
447 setSelectionBeginTime(selectedTime
.getValue());
448 setSelectionEndTime(selectedEndTime
.getValue());
449 if (fMouseSelectionProvider
!= null) {
450 fMouseSelectionProvider
.refresh();
456 * Signal handler for handling of the time range synch signal.
459 * The time range synch signal {@link TmfRangeSynchSignal}
462 public void timeRangeUpdated(TmfRangeSynchSignal signal
) {
464 if (fTrace
!= null) {
465 // Validate the time range
466 TmfTimeRange range
= signal
.getCurrentRange().getIntersection(fTrace
.getTimeRange());
471 if (signal
.getSource() != this) {
472 // Update the time range
473 long windowStartTime
= range
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
474 long windowEndTime
= range
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
475 long windowDuration
= windowEndTime
- windowStartTime
;
477 setWindowStartTime(windowStartTime
);
478 setWindowEndTime(windowEndTime
);
479 setWindowDuration(windowDuration
);
486 * Signal handler for handling of the trace range updated signal.
489 * The trace range signal {@link TmfTraceRangeUpdatedSignal}
492 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal
) {
494 if (signal
.getTrace() != fTrace
) {
498 TmfTimeRange fullRange
= signal
.getRange();
500 long traceStartTime
= fullRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
501 long traceEndTime
= fullRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
503 setStartTime(traceStartTime
);
504 setEndTime(traceEndTime
);
508 * Signal handler for handling of the trace updated signal.
511 * The trace updated signal {@link TmfTraceUpdatedSignal}
514 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
515 if (signal
.getTrace() != fTrace
) {
518 TmfTimeRange fullRange
= signal
.getTrace().getTimeRange();
519 long traceStartTime
= fullRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
520 long traceEndTime
= fullRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
522 setStartTime(traceStartTime
);
523 setEndTime(traceEndTime
);
527 * Signal handler for handling the signal that notifies about an updated
531 * The trace updated signal
532 * {@link TmfTimestampFormatUpdateSignal}
535 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
536 fSwtChart
.getAxisSet().adjustRange();
540 // ------------------------------------------------------------------------
542 // ------------------------------------------------------------------------
545 * Clears the view content.
547 protected void clearContent() {
548 if (!fSwtChart
.isDisposed()) {
549 ISeriesSet set
= fSwtChart
.getSeriesSet();
550 ISeries
[] series
= set
.getSeries();
551 for (int i
= 0; i
< series
.length
; i
++) {
552 set
.deleteSeries(series
[i
].getId());
559 * Returns the current or default display.
561 * @return the current or default display
563 protected static Display
getDisplay() {
564 Display display
= Display
.getCurrent();
565 // may be null if outside the UI thread
566 if (display
== null) {
567 display
= Display
.getDefault();