tmf: add simple tooltip provider for swt charts
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / xycharts / TmfXYChartViewer.java
CommitLineData
d7d40e67
BH
1/**********************************************************************
2 * Copyright (c) 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12package org.eclipse.linuxtools.tmf.ui.viewers.xycharts;
13
14import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
15import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
73920960 16import org.eclipse.linuxtools.tmf.core.signal.TmfSignalThrottler;
d7d40e67
BH
17import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
18import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal;
19import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
20import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
21import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
22import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal;
23import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
24import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
25import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
0b09e0cf 26import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
d7d40e67
BH
27import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
28import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
29import org.eclipse.linuxtools.tmf.ui.viewers.TmfViewer;
30import org.eclipse.swt.SWT;
31import org.eclipse.swt.widgets.Composite;
32import org.eclipse.swt.widgets.Control;
33import org.eclipse.swt.widgets.Display;
34import org.swtchart.Chart;
35import org.swtchart.IAxis;
36import org.swtchart.ISeries;
37import org.swtchart.ISeriesSet;
38
39/**
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.
43 *
44 * @author Bernd Hufmann
45 * @since 3.0
46 */
47public abstract class TmfXYChartViewer extends TmfViewer implements ITmfChartTimeProvider {
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52 /**
53 * The offset to apply to any x position. This offset ensures better
54 * precision when converting long to double and back.
55 */
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;
73920960
BH
75 /** A signal throttler for range updates */
76 private final TmfSignalThrottler fTimeRangeSyncThrottle = new TmfSignalThrottler(this, 200);
0b09e0cf
BH
77 /** The mouse selection provider */
78 private TmfBaseProvider fMouseSelectionProvider;
73920960
BH
79 /** The mouse drag zoom provider */
80 private TmfBaseProvider fMouseDragZoomProvider;
4bcc4ed6
BH
81 /** The mouse wheel zoom provider */
82 private TmfBaseProvider fMouseWheelZoomProvider;
5791dcef
BH
83 /** The tooltip provider */
84 private TmfBaseProvider fToolTipProvider;
d7d40e67
BH
85
86 // ------------------------------------------------------------------------
87 // Constructors
88 // ------------------------------------------------------------------------
89
90 /**
91 * Constructs a TmfXYChartViewer.
92 *
93 * @param parent
94 * The parent composite
95 * @param title
96 * The title of the viewer
97 * @param xLabel
98 * The label of the xAxis
99 * @param yLabel
100 * The label of the yAXIS
101 */
102 public TmfXYChartViewer(Composite parent, String title, String xLabel, String yLabel) {
103 super(parent, title);
104 fSwtChart = new Chart(parent, SWT.NONE);
105
106 IAxis xAxis = fSwtChart.getAxisSet().getXAxis(0);
107 IAxis yAxis = fSwtChart.getAxisSet().getYAxis(0);
108
109 /* Set the title/labels, or hide them if they are not provided */
110 if (title == null) {
111 fSwtChart.getTitle().setVisible(false);
112 } else {
113 fSwtChart.getTitle().setText(title);
114 }
115 if (xLabel == null) {
116 xAxis.getTitle().setVisible(false);
117 } else {
118 xAxis.getTitle().setText(xLabel);
119 }
120 if (yLabel == null) {
121 yAxis.getTitle().setVisible(false);
122 } else {
123 yAxis.getTitle().setText(yLabel);
124 }
0b09e0cf
BH
125
126 fMouseSelectionProvider = new TmfMouseSelectionProvider(this);
73920960 127 fMouseDragZoomProvider = new TmfMouseDragZoomProvider(this);
4bcc4ed6 128 fMouseWheelZoomProvider = new TmfMouseWheelZoomProvider(this);
5791dcef 129 fToolTipProvider = new TmfSimpleTooltipProvider(this);
d7d40e67
BH
130 }
131
132 // ------------------------------------------------------------------------
133 // Getter/Setters
134 // ------------------------------------------------------------------------
135 /**
136 * Sets the time offset to apply.
137 * @see ITmfChartTimeProvider#getTimeOffset()
138 *
139 * @param timeOffset
140 * The time offset to apply
141 */
142 protected void setTimeOffset(long timeOffset) {
143 fTimeOffset = timeOffset;
144 }
145
146 /**
147 * Sets the start time of the trace
148 *
149 * @param startTime
150 * The start time to set
151 */
152 protected void setStartTime(long startTime) {
153 fStartTime = startTime;
154 }
155
156 /**
157 * Sets the end time of the trace
158 *
159 * @param endTime
160 * The start time to set
161 */
162 protected void setEndTime(long endTime) {
163 fEndTime = endTime;
164 }
165
166 /**
167 * Sets the start time of the current time range window
168 *
169 * @param windowStartTime
170 * The start time to set
171 */
172 protected void setWindowStartTime(long windowStartTime) {
173 fWindowStartTime = windowStartTime;
174 }
175
176 /**
177 * Sets the end time of the current time range window
178 *
179 * @param windowEndTime
180 * The start time to set
181 */
182 protected void setWindowEndTime(long windowEndTime) {
183 fWindowEndTime = windowEndTime;
184 }
185
186 /**
187 * Sets the start time of the current time range window
188 *
189 * @param windowDuration
190 * The start time to set
191 */
192 protected void setWindowDuration(long windowDuration) {
193 fWindowDuration = windowDuration;
194 }
195
196 /**
197 * Sets the begin time of the selection range.
198 *
199 * @param selectionBeginTime
200 * The begin time to set
201 */
202 protected void setSelectionBeginTime(long selectionBeginTime) {
203 fSelectionBeginTime = selectionBeginTime;
204 }
205
206 /**
207 * Sets the end time of the selection range.
208 *
209 * @param selectionEndTime
210 * The end time to set
211 */
212 protected void setSelectionEndTime(long selectionEndTime) {
213 fSelectionEndTime = selectionEndTime;
214 }
215
216 /**
217 * Sets the trace that is displayed by this viewer.
218 *
219 * @param trace
220 * The trace to set
221 */
222 protected void setTrace(ITmfTrace trace) {
223 fTrace = trace;
224 }
225
226 /**
227 * Gets the trace that is displayed by this viewer.
228 *
229 * @return the trace
230 */
231 protected ITmfTrace getTrace() {
232 return fTrace;
233 }
234
235 /**
236 * Sets the SWT Chart reference
237 *
238 * @param chart
239 * The SWT chart to set.
240 */
241 protected void setSwtChart(Chart chart) {
242 fSwtChart = chart;
243 }
244
245 /**
246 * Gets the SWT Chart reference
247 *
248 * @return the SWT chart to set.
249 */
250 protected Chart getSwtChart() {
251 return fSwtChart;
252 }
253
0b09e0cf
BH
254 /**
255 * Sets a mouse selection provider. An existing provider will be
256 * disposed. Use <code>null</code> to disable the mouse selection provider.
257 *
258 * @param provider
259 * The selection provider to set
260 */
261 public void setSelectionProvider(TmfBaseProvider provider) {
262 if (fMouseSelectionProvider != null) {
263 fMouseSelectionProvider.dispose();
264 }
265 fMouseSelectionProvider = provider;
266 }
267
73920960
BH
268 /**
269 * Sets a mouse drag zoom provider. An existing provider will be
270 * disposed. Use <code>null</code> to disable the mouse drag zoom provider.
271 *
272 * @param provider
273 * The mouse drag zoom provider to set
274 */
275 public void setMouseDragZoomProvider(TmfBaseProvider provider) {
276 if (fMouseDragZoomProvider != null) {
277 fMouseDragZoomProvider.dispose();
278 }
279 fMouseDragZoomProvider = provider;
280 }
281
4bcc4ed6
BH
282 /**
283 * Sets a mouse wheel zoom provider. An existing provider will be
284 * disposed. Use <code>null</code> to disable the mouse wheel zoom
285 * provider.
286 *
287 * @param provider
288 * The mouse wheel zoom provider to set
289 */
290 public void setMouseWheelZoomProvider(TmfBaseProvider provider) {
291 if (fMouseWheelZoomProvider != null) {
292 fMouseWheelZoomProvider.dispose();
293 }
294 fMouseWheelZoomProvider = provider;
295 }
296
5791dcef
BH
297 /**
298 * Sets a tooltip provider. An existing provider will be
299 * disposed. Use <code>null</code> to disable the tooltip provider.
300 *
301 * @param provider
302 * The tooltip provider to set
303 */
304 public void setTooltipProvider(TmfBaseProvider provider) {
305 if (fToolTipProvider != null) {
306 fToolTipProvider.dispose();
307 }
308 fToolTipProvider = provider;
309 }
310
d7d40e67
BH
311 // ------------------------------------------------------------------------
312 // ITmfChartTimeProvider
313 // ------------------------------------------------------------------------
314 @Override
315 public long getStartTime() {
316 return fStartTime;
317 }
318
319 @Override
320 public long getEndTime() {
321 return fEndTime;
322 }
323
324 @Override
325 public long getWindowStartTime() {
326 return fWindowStartTime;
327 }
328
329 @Override
330 public long getWindowEndTime() {
331 return fWindowEndTime;
332 }
333
334 @Override
335 public long getWindowDuration() {
336 return fWindowDuration;
337 }
338
339 @Override
340 public long getSelectionBeginTime() {
341 return fSelectionBeginTime;
342 }
343
344 @Override
345 public long getSelectionEndTime() {
346 return fSelectionEndTime;
347 }
348
349 @Override
350 public long getTimeOffset() {
351 return fTimeOffset;
352 }
353
0b09e0cf
BH
354 @Override
355 public void updateSelectionRange(final long currentBeginTime, final long currentEndTime) {
356 if (fTrace != null) {
357 setSelectionBeginTime(currentBeginTime);
358 setSelectionEndTime(currentEndTime);
359
360 final ITmfTimestamp startTimestamp = new TmfTimestamp(fSelectionBeginTime, ITmfTimestamp.NANOSECOND_SCALE);
361 final ITmfTimestamp endTimestamp = new TmfTimestamp(fSelectionEndTime, ITmfTimestamp.NANOSECOND_SCALE);
362
363 TmfTimeSynchSignal signal = new TmfTimeSynchSignal(TmfXYChartViewer.this, startTimestamp, endTimestamp);
364 broadcast(signal);
365 }
366 }
367
73920960
BH
368 @Override
369 public void updateWindow(long windowStartTime, long windowEndTime) {
370
371 setWindowStartTime(windowStartTime);
372 setWindowEndTime(windowEndTime);
373 fWindowDuration = windowEndTime - windowStartTime;
374
375 // Build the new time range; keep the current time
376 TmfTimeRange timeRange = new TmfTimeRange(
377 new TmfTimestamp(fWindowStartTime, ITmfTimestamp.NANOSECOND_SCALE),
378 new TmfTimestamp(fWindowEndTime, ITmfTimestamp.NANOSECOND_SCALE));
379
380 // Send the signal
381 TmfRangeSynchSignal signal = new TmfRangeSynchSignal(this, timeRange);
382 fTimeRangeSyncThrottle.queue(signal);
383 }
384
d7d40e67
BH
385 // ------------------------------------------------------------------------
386 // ITmfViewer interface
387 // ------------------------------------------------------------------------
388 @Override
389 public Control getControl() {
390 return fSwtChart;
391 }
392
393 @Override
394 public void refresh() {
395 fSwtChart.redraw();
396 }
397
398 // ------------------------------------------------------------------------
399 // TmfComponent
400 // ------------------------------------------------------------------------
401 @Override
402 public void dispose() {
403 super.dispose();
404 fSwtChart.dispose();
0b09e0cf
BH
405
406 if (fMouseSelectionProvider != null) {
407 fMouseSelectionProvider.dispose();
408 }
73920960
BH
409
410 if (fMouseDragZoomProvider != null) {
411 fMouseDragZoomProvider.dispose();
412 }
4bcc4ed6
BH
413
414 if (fMouseWheelZoomProvider != null) {
415 fMouseWheelZoomProvider.dispose();
416 }
5791dcef
BH
417
418 if (fToolTipProvider != null) {
419 fToolTipProvider.dispose();
420 }
d7d40e67
BH
421 }
422
423 // ------------------------------------------------------------------------
424 // Operations
425 // ------------------------------------------------------------------------
426 /**
427 * A Method to load a trace into the viewer.
428 *
429 * @param trace
430 * A trace to apply in the viewer
431 */
432 public void loadTrace(ITmfTrace trace) {
433 fTrace = trace;
434
435 long timestamp = TmfTraceManager.getInstance().getSelectionBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
436 long windowStartTime = TmfTraceManager.getInstance().getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
437 long startTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
438 long endTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
439
440 setSelectionBeginTime(timestamp);
441 setSelectionEndTime(timestamp);
442 setStartTime(startTime);
443 setWindowStartTime(windowStartTime);
444 setWindowDuration(fTrace.getInitialRangeOffset().getValue());
445 setEndTime(endTime);
446 setWindowEndTime(windowStartTime + getWindowDuration());
447 clearContent();
448 updateContent();
449 }
450
451 /**
452 * Resets the content of the viewer
453 */
454 public void reset() {
455 // Reset the internal data
456 setSelectionBeginTime(0);
457 setSelectionEndTime(0);
458 setStartTime(0);
459 setWindowStartTime(0);
460 setWindowDuration(0);
461 setEndTime(0);
462 setWindowEndTime(0);
463 setTrace(null);
464 clearContent();
465 }
466
467 /**
468 * Method to implement to update the chart content.
469 */
470 protected abstract void updateContent();
471
472 // ------------------------------------------------------------------------
473 // Signal Handler
474 // ------------------------------------------------------------------------
475
476 /**
477 * Signal handler for handling of the trace opened signal.
478 *
479 * @param signal
480 * The trace opened signal {@link TmfTraceOpenedSignal}
481 */
482 @TmfSignalHandler
483 public void traceOpened(TmfTraceOpenedSignal signal) {
484 fTrace = signal.getTrace();
485 loadTrace(getTrace());
486 }
487
488 /**
489 * Signal handler for handling of the trace selected signal.
490 *
491 * @param signal
492 * The trace selected signal {@link TmfTraceSelectedSignal}
493 */
494 @TmfSignalHandler
495 public void traceSelected(TmfTraceSelectedSignal signal) {
496 if (fTrace != signal.getTrace()) {
497 fTrace = signal.getTrace();
498 loadTrace(getTrace());
499 }
500 }
501
502 /**
503 * Signal handler for handling of the trace closed signal.
504 *
505 * @param signal
506 * The trace closed signal {@link TmfTraceClosedSignal}
507 */
508 @TmfSignalHandler
509 public void traceClosed(TmfTraceClosedSignal signal) {
510
511 if (signal.getTrace() != fTrace) {
512 return;
513 }
514
515 // Reset the internal data
516 fTrace = null;
517 reset();
518 }
519
520 /**
521 * Signal handler for handling of the time synch signal.
522 *
523 * @param signal
524 * The time synch signal {@link TmfTimeSynchSignal}
525 */
526 @TmfSignalHandler
527 public void selectionRangeUpdated(TmfTimeSynchSignal signal) {
528 if ((signal.getSource() != this) && (fTrace != null)) {
529 ITmfTimestamp selectedTime = signal.getBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
530 ITmfTimestamp selectedEndTime = signal.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
531 setSelectionBeginTime(selectedTime.getValue());
532 setSelectionEndTime(selectedEndTime.getValue());
0b09e0cf
BH
533 if (fMouseSelectionProvider != null) {
534 fMouseSelectionProvider.refresh();
535 }
d7d40e67
BH
536 }
537 }
538
539 /**
540 * Signal handler for handling of the time range synch signal.
541 *
542 * @param signal
543 * The time range synch signal {@link TmfRangeSynchSignal}
544 */
545 @TmfSignalHandler
546 public void timeRangeUpdated(TmfRangeSynchSignal signal) {
547
548 if (fTrace != null) {
549 // Validate the time range
550 TmfTimeRange range = signal.getCurrentRange().getIntersection(fTrace.getTimeRange());
551 if (range == null) {
552 return;
553 }
554
555 if (signal.getSource() != this) {
556 // Update the time range
557 long windowStartTime = range.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
558 long windowEndTime = range.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
559 long windowDuration = windowEndTime - windowStartTime;
560
561 setWindowStartTime(windowStartTime);
562 setWindowEndTime(windowEndTime);
563 setWindowDuration(windowDuration);
564 }
565 }
566 updateContent();
567 }
568
569 /**
570 * Signal handler for handling of the trace range updated signal.
571 *
572 * @param signal
573 * The trace range signal {@link TmfTraceRangeUpdatedSignal}
574 */
575 @TmfSignalHandler
576 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
577
578 if (signal.getTrace() != fTrace) {
579 return;
580 }
581
582 TmfTimeRange fullRange = signal.getRange();
583
584 long traceStartTime = fullRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
585 long traceEndTime = fullRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
586
587 setStartTime(traceStartTime);
588 setEndTime(traceEndTime);
589 }
590
591 /**
592 * Signal handler for handling of the trace updated signal.
593 *
594 * @param signal
595 * The trace updated signal {@link TmfTraceUpdatedSignal}
596 */
597 @TmfSignalHandler
598 public void traceUpdated(TmfTraceUpdatedSignal signal) {
599 if (signal.getTrace() != fTrace) {
600 return;
601 }
602 TmfTimeRange fullRange = signal.getTrace().getTimeRange();
603 long traceStartTime = fullRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
604 long traceEndTime = fullRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
605
606 setStartTime(traceStartTime);
607 setEndTime(traceEndTime);
608 }
609
610 /**
611 * Signal handler for handling the signal that notifies about an updated
612 * timestamp format.
613 *
614 * @param signal
615 * The trace updated signal
616 * {@link TmfTimestampFormatUpdateSignal}
617 */
618 @TmfSignalHandler
619 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
620 fSwtChart.getAxisSet().adjustRange();
621 fSwtChart.redraw();
622 }
623
624 // ------------------------------------------------------------------------
625 // Helper Methods
626 // ------------------------------------------------------------------------
627
628 /**
629 * Clears the view content.
630 */
631 protected void clearContent() {
632 if (!fSwtChart.isDisposed()) {
633 ISeriesSet set = fSwtChart.getSeriesSet();
634 ISeries[] series = set.getSeries();
635 for (int i = 0; i < series.length; i++) {
636 set.deleteSeries(series[i].getId());
637 }
638 fSwtChart.redraw();
639 }
640 }
641
642 /**
643 * Returns the current or default display.
644 *
645 * @return the current or default display
646 */
647 protected static Display getDisplay() {
648 Display display = Display.getCurrent();
649 // may be null if outside the UI thread
650 if (display == null) {
651 display = Display.getDefault();
652 }
653 return display;
654 }
655
656}
This page took 0.078715 seconds and 5 git commands to generate.