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
.TmfSignalThrottler
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimestampFormatUpdateSignal
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.TmfViewer
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.swtchart
.Chart
;
35 import org
.swtchart
.IAxis
;
36 import org
.swtchart
.ISeries
;
37 import org
.swtchart
.ISeriesSet
;
40 * Base class for a XY-Chart based on SWT chart. It provides a methods to define
41 * zoom, selection and tool tip providers. It also provides call backs to be
42 * notified by any changes caused by selection and zoom.
44 * @author Bernd Hufmann
47 public abstract class TmfXYChartViewer
extends TmfViewer
implements ITmfChartTimeProvider
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 * The offset to apply to any x position. This offset ensures better
54 * precision when converting long to double and back.
56 private long fTimeOffset
;
57 /** Start time of trace */
58 private long fStartTime
;
59 /** End time of trace */
60 private long fEndTime
;
61 /** Start time of current time range */
62 private long fWindowStartTime
;
63 /** End time of current time range */
64 private long fWindowEndTime
;
65 /** Duration of current time range */
66 private long fWindowDuration
;
67 /** Current begin time of selection range */
68 private long fSelectionBeginTime
;
69 /** Current end of selection range */
70 private long fSelectionEndTime
;
71 /** The trace that is displayed by this viewer */
72 private ITmfTrace fTrace
;
73 /** The SWT Chart reference */
74 private Chart fSwtChart
;
75 /** A signal throttler for range updates */
76 private final TmfSignalThrottler fTimeRangeSyncThrottle
= new TmfSignalThrottler(this, 200);
77 /** The mouse selection provider */
78 private TmfBaseProvider fMouseSelectionProvider
;
79 /** The mouse drag zoom provider */
80 private TmfBaseProvider fMouseDragZoomProvider
;
81 /** The mouse wheel zoom provider */
82 private TmfBaseProvider fMouseWheelZoomProvider
;
83 /** The tooltip provider */
84 private TmfBaseProvider fToolTipProvider
;
85 /** The middle mouse drag provider */
86 private TmfBaseProvider fMouseDragProvider
;
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * Constructs a TmfXYChartViewer.
96 * The parent composite
98 * The title of the viewer
100 * The label of the xAxis
102 * The label of the yAXIS
104 public TmfXYChartViewer(Composite parent
, String title
, String xLabel
, String yLabel
) {
105 super(parent
, title
);
106 fSwtChart
= new Chart(parent
, SWT
.NONE
);
108 IAxis xAxis
= fSwtChart
.getAxisSet().getXAxis(0);
109 IAxis yAxis
= fSwtChart
.getAxisSet().getYAxis(0);
111 /* Set the title/labels, or hide them if they are not provided */
113 fSwtChart
.getTitle().setVisible(false);
115 fSwtChart
.getTitle().setText(title
);
117 if (xLabel
== null) {
118 xAxis
.getTitle().setVisible(false);
120 xAxis
.getTitle().setText(xLabel
);
122 if (yLabel
== null) {
123 yAxis
.getTitle().setVisible(false);
125 yAxis
.getTitle().setText(yLabel
);
128 fMouseSelectionProvider
= new TmfMouseSelectionProvider(this);
129 fMouseDragZoomProvider
= new TmfMouseDragZoomProvider(this);
130 fMouseWheelZoomProvider
= new TmfMouseWheelZoomProvider(this);
131 fToolTipProvider
= new TmfSimpleTooltipProvider(this);
132 fMouseDragProvider
= new TmfMouseDragProvider(this);
135 // ------------------------------------------------------------------------
137 // ------------------------------------------------------------------------
139 * Sets the time offset to apply.
140 * @see ITmfChartTimeProvider#getTimeOffset()
143 * The time offset to apply
145 protected void setTimeOffset(long timeOffset
) {
146 fTimeOffset
= timeOffset
;
150 * Sets the start time of the trace
153 * The start time to set
155 protected void setStartTime(long startTime
) {
156 fStartTime
= startTime
;
160 * Sets the end time of the trace
163 * The start time to set
165 protected void setEndTime(long endTime
) {
170 * Sets the start time of the current time range window
172 * @param windowStartTime
173 * The start time to set
175 protected void setWindowStartTime(long windowStartTime
) {
176 fWindowStartTime
= windowStartTime
;
180 * Sets the end time of the current time range window
182 * @param windowEndTime
183 * The start time to set
185 protected void setWindowEndTime(long windowEndTime
) {
186 fWindowEndTime
= windowEndTime
;
190 * Sets the start time of the current time range window
192 * @param windowDuration
193 * The start time to set
195 protected void setWindowDuration(long windowDuration
) {
196 fWindowDuration
= windowDuration
;
200 * Sets the begin time of the selection range.
202 * @param selectionBeginTime
203 * The begin time to set
205 protected void setSelectionBeginTime(long selectionBeginTime
) {
206 fSelectionBeginTime
= selectionBeginTime
;
210 * Sets the end time of the selection range.
212 * @param selectionEndTime
213 * The end time to set
215 protected void setSelectionEndTime(long selectionEndTime
) {
216 fSelectionEndTime
= selectionEndTime
;
220 * Sets the trace that is displayed by this viewer.
225 protected void setTrace(ITmfTrace trace
) {
230 * Gets the trace that is displayed by this viewer.
234 protected ITmfTrace
getTrace() {
239 * Sets the SWT Chart reference
242 * The SWT chart to set.
244 protected void setSwtChart(Chart chart
) {
249 * Gets the SWT Chart reference
251 * @return the SWT chart to set.
253 protected Chart
getSwtChart() {
258 * Sets a mouse selection provider. An existing provider will be
259 * disposed. Use <code>null</code> to disable the mouse selection provider.
262 * The selection provider to set
264 public void setSelectionProvider(TmfBaseProvider provider
) {
265 if (fMouseSelectionProvider
!= null) {
266 fMouseSelectionProvider
.dispose();
268 fMouseSelectionProvider
= provider
;
272 * Sets a mouse drag zoom provider. An existing provider will be
273 * disposed. Use <code>null</code> to disable the mouse drag zoom provider.
276 * The mouse drag zoom provider to set
278 public void setMouseDragZoomProvider(TmfBaseProvider provider
) {
279 if (fMouseDragZoomProvider
!= null) {
280 fMouseDragZoomProvider
.dispose();
282 fMouseDragZoomProvider
= provider
;
286 * Sets a mouse wheel zoom provider. An existing provider will be
287 * disposed. Use <code>null</code> to disable the mouse wheel zoom
291 * The mouse wheel zoom provider to set
293 public void setMouseWheelZoomProvider(TmfBaseProvider provider
) {
294 if (fMouseWheelZoomProvider
!= null) {
295 fMouseWheelZoomProvider
.dispose();
297 fMouseWheelZoomProvider
= provider
;
301 * Sets a tooltip provider. An existing provider will be
302 * disposed. Use <code>null</code> to disable the tooltip provider.
305 * The tooltip provider to set
307 public void setTooltipProvider(TmfBaseProvider provider
) {
308 if (fToolTipProvider
!= null) {
309 fToolTipProvider
.dispose();
311 fToolTipProvider
= provider
;
315 * Sets a mouse drag provider. An existing provider will be
316 * disposed. Use <code>null</code> to disable the mouse drag provider.
319 * The mouse drag provider to set
321 public void setMouseDrageProvider(TmfBaseProvider provider
) {
322 if (fMouseDragProvider
!= null) {
323 fMouseDragProvider
.dispose();
325 fMouseDragProvider
= provider
;
328 // ------------------------------------------------------------------------
329 // ITmfChartTimeProvider
330 // ------------------------------------------------------------------------
332 public long getStartTime() {
337 public long getEndTime() {
342 public long getWindowStartTime() {
343 return fWindowStartTime
;
347 public long getWindowEndTime() {
348 return fWindowEndTime
;
352 public long getWindowDuration() {
353 return fWindowDuration
;
357 public long getSelectionBeginTime() {
358 return fSelectionBeginTime
;
362 public long getSelectionEndTime() {
363 return fSelectionEndTime
;
367 public long getTimeOffset() {
372 public void updateSelectionRange(final long currentBeginTime
, final long currentEndTime
) {
373 if (fTrace
!= null) {
374 setSelectionBeginTime(currentBeginTime
);
375 setSelectionEndTime(currentEndTime
);
377 final ITmfTimestamp startTimestamp
= new TmfTimestamp(fSelectionBeginTime
, ITmfTimestamp
.NANOSECOND_SCALE
);
378 final ITmfTimestamp endTimestamp
= new TmfTimestamp(fSelectionEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
);
380 TmfTimeSynchSignal signal
= new TmfTimeSynchSignal(TmfXYChartViewer
.this, startTimestamp
, endTimestamp
);
386 public void updateWindow(long windowStartTime
, long windowEndTime
) {
388 setWindowStartTime(windowStartTime
);
389 setWindowEndTime(windowEndTime
);
390 fWindowDuration
= windowEndTime
- windowStartTime
;
392 // Build the new time range; keep the current time
393 TmfTimeRange timeRange
= new TmfTimeRange(
394 new TmfTimestamp(fWindowStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
395 new TmfTimestamp(fWindowEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
));
398 TmfRangeSynchSignal signal
= new TmfRangeSynchSignal(this, timeRange
);
399 fTimeRangeSyncThrottle
.queue(signal
);
402 // ------------------------------------------------------------------------
403 // ITmfViewer interface
404 // ------------------------------------------------------------------------
406 public Control
getControl() {
411 public void refresh() {
415 // ------------------------------------------------------------------------
417 // ------------------------------------------------------------------------
419 public void dispose() {
423 if (fMouseSelectionProvider
!= null) {
424 fMouseSelectionProvider
.dispose();
427 if (fMouseDragZoomProvider
!= null) {
428 fMouseDragZoomProvider
.dispose();
431 if (fMouseWheelZoomProvider
!= null) {
432 fMouseWheelZoomProvider
.dispose();
435 if (fToolTipProvider
!= null) {
436 fToolTipProvider
.dispose();
439 if (fMouseDragProvider
!= null) {
440 fMouseDragProvider
.dispose();
444 // ------------------------------------------------------------------------
446 // ------------------------------------------------------------------------
448 * A Method to load a trace into the viewer.
451 * A trace to apply in the viewer
453 public void loadTrace(ITmfTrace trace
) {
456 long timestamp
= TmfTraceManager
.getInstance().getSelectionBeginTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
457 long windowStartTime
= TmfTraceManager
.getInstance().getCurrentRange().getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
458 long startTime
= fTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
459 long endTime
= fTrace
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
461 setSelectionBeginTime(timestamp
);
462 setSelectionEndTime(timestamp
);
463 setStartTime(startTime
);
464 setWindowStartTime(windowStartTime
);
465 setWindowDuration(fTrace
.getInitialRangeOffset().getValue());
467 setWindowEndTime(windowStartTime
+ getWindowDuration());
473 * Resets the content of the viewer
475 public void reset() {
476 // Reset the internal data
477 setSelectionBeginTime(0);
478 setSelectionEndTime(0);
480 setWindowStartTime(0);
481 setWindowDuration(0);
489 * Method to implement to update the chart content.
491 protected abstract void updateContent();
493 // ------------------------------------------------------------------------
495 // ------------------------------------------------------------------------
498 * Signal handler for handling of the trace opened signal.
501 * The trace opened signal {@link TmfTraceOpenedSignal}
504 public void traceOpened(TmfTraceOpenedSignal signal
) {
505 fTrace
= signal
.getTrace();
506 loadTrace(getTrace());
510 * Signal handler for handling of the trace selected signal.
513 * The trace selected signal {@link TmfTraceSelectedSignal}
516 public void traceSelected(TmfTraceSelectedSignal signal
) {
517 if (fTrace
!= signal
.getTrace()) {
518 fTrace
= signal
.getTrace();
519 loadTrace(getTrace());
524 * Signal handler for handling of the trace closed signal.
527 * The trace closed signal {@link TmfTraceClosedSignal}
530 public void traceClosed(TmfTraceClosedSignal signal
) {
532 if (signal
.getTrace() != fTrace
) {
536 // Reset the internal data
542 * Signal handler for handling of the time synch signal.
545 * The time synch signal {@link TmfTimeSynchSignal}
548 public void selectionRangeUpdated(TmfTimeSynchSignal signal
) {
549 if ((signal
.getSource() != this) && (fTrace
!= null)) {
550 ITmfTimestamp selectedTime
= signal
.getBeginTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
);
551 ITmfTimestamp selectedEndTime
= signal
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
);
552 setSelectionBeginTime(selectedTime
.getValue());
553 setSelectionEndTime(selectedEndTime
.getValue());
554 if (fMouseSelectionProvider
!= null) {
555 fMouseSelectionProvider
.refresh();
561 * Signal handler for handling of the time range synch signal.
564 * The time range synch signal {@link TmfRangeSynchSignal}
567 public void timeRangeUpdated(TmfRangeSynchSignal signal
) {
569 if (fTrace
!= null) {
570 // Validate the time range
571 TmfTimeRange range
= signal
.getCurrentRange().getIntersection(fTrace
.getTimeRange());
576 if (signal
.getSource() != this) {
577 // Update the time range
578 long windowStartTime
= range
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
579 long windowEndTime
= range
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
580 long windowDuration
= windowEndTime
- windowStartTime
;
582 setWindowStartTime(windowStartTime
);
583 setWindowEndTime(windowEndTime
);
584 setWindowDuration(windowDuration
);
591 * Signal handler for handling of the trace range updated signal.
594 * The trace range signal {@link TmfTraceRangeUpdatedSignal}
597 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal
) {
599 if (signal
.getTrace() != fTrace
) {
603 TmfTimeRange fullRange
= signal
.getRange();
605 long traceStartTime
= fullRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
606 long traceEndTime
= fullRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
608 setStartTime(traceStartTime
);
609 setEndTime(traceEndTime
);
613 * Signal handler for handling of the trace updated signal.
616 * The trace updated signal {@link TmfTraceUpdatedSignal}
619 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
620 if (signal
.getTrace() != fTrace
) {
623 TmfTimeRange fullRange
= signal
.getTrace().getTimeRange();
624 long traceStartTime
= fullRange
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
625 long traceEndTime
= fullRange
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
627 setStartTime(traceStartTime
);
628 setEndTime(traceEndTime
);
632 * Signal handler for handling the signal that notifies about an updated
636 * The trace updated signal
637 * {@link TmfTimestampFormatUpdateSignal}
640 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
641 fSwtChart
.getAxisSet().adjustRange();
645 // ------------------------------------------------------------------------
647 // ------------------------------------------------------------------------
650 * Clears the view content.
652 protected void clearContent() {
653 if (!fSwtChart
.isDisposed()) {
654 ISeriesSet set
= fSwtChart
.getSeriesSet();
655 ISeries
[] series
= set
.getSeries();
656 for (int i
= 0; i
< series
.length
; i
++) {
657 set
.deleteSeries(series
[i
].getId());
664 * Returns the current or default display.
666 * @return the current or default display
668 protected static Display
getDisplay() {
669 Display display
= Display
.getCurrent();
670 // may be null if outside the UI thread
671 if (display
== null) {
672 display
= Display
.getDefault();