1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others.
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 in TmfXYChartViewer
11 * Geneviève Bastien - Moved methods from TmfXYChartViewer to this interface
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
;
16 import org
.eclipse
.jdt
.annotation
.Nullable
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
19 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalThrottler
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceContext
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
35 * Abstract class that extends {@link TmfViewer} that adds methods to
36 * synchronize with a trace's time information.
38 * This class will be extended by viewers who require time information to update
42 * It provides three times of data:
43 * - start and end time of the trace (available)
44 * - start, end and duration of the current time window, ie the visible time range
45 * - start and end of the time range selected
48 * @author Bernd Hufmann
49 * @author Geneviève Bastien
51 public abstract class TmfTimeViewer
extends TmfViewer
implements ITmfTimeProvider
{
53 /** Start time of trace */
54 private long fStartTime
;
55 /** End time of trace */
56 private long fEndTime
;
57 /** Start time of current time range */
58 private long fWindowStartTime
;
59 /** End time of current time range */
60 private long fWindowEndTime
;
61 /** Current begin time of selection range */
62 private long fSelectionBeginTime
;
63 /** Current end of selection range */
64 private long fSelectionEndTime
;
65 /** The trace that is displayed by this viewer */
66 private ITmfTrace fTrace
;
67 /** A signal throttler for range updates */
68 private final TmfSignalThrottler fTimeRangeSyncThrottle
= new TmfSignalThrottler(this, 200);
71 * Default constructor.
73 public TmfTimeViewer() {
78 * Constructor that initializes the parent of the viewer
81 * The parent composite that holds this viewer
83 public TmfTimeViewer(Composite parent
) {
84 this(parent
, ""); //$NON-NLS-1$
88 * Constructor that initializes the parent of the viewer and that sets the
92 * The parent composite that holds this viewer
94 * The name of the viewer
96 public TmfTimeViewer(Composite parent
, String name
) {
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
105 * Sets the start time of the trace
108 * The start time to set
110 protected void setStartTime(long startTime
) {
111 fStartTime
= startTime
;
115 * Sets the end time of the trace
118 * The start time to set
120 protected void setEndTime(long endTime
) {
125 * Sets the start time and end of the current time range window (visible
128 * @param windowStartTime
129 * The start time to set
130 * @param windowEndTime
131 * The start time to set
134 protected void setWindowRange(long windowStartTime
, long windowEndTime
) {
135 fWindowStartTime
= windowStartTime
;
136 fWindowEndTime
= windowEndTime
;
140 * Sets the begin and end time of the selected range without sending the
141 * {@link TmfSelectionRangeUpdatedSignal} signal.
143 * @param selectionBeginTime
144 * The begin time to set
145 * @param selectionEndTime
146 * The end time to set
150 protected void setSelectionRange(long selectionBeginTime
, long selectionEndTime
) {
151 fSelectionBeginTime
= selectionBeginTime
;
152 fSelectionEndTime
= selectionEndTime
;
156 * Sets the trace that is displayed by this viewer.
161 protected void setTrace(ITmfTrace trace
) {
166 * Gets the trace that is displayed by this viewer.
170 protected ITmfTrace
getTrace() {
174 // ------------------------------------------------------------------------
176 // ------------------------------------------------------------------------
179 public long getStartTime() {
184 public long getEndTime() {
189 public long getWindowStartTime() {
190 return fWindowStartTime
;
194 public long getWindowEndTime() {
195 return fWindowEndTime
;
199 public long getWindowDuration() {
200 return getWindowEndTime() - getWindowStartTime();
204 public long getSelectionBeginTime() {
205 return fSelectionBeginTime
;
209 public long getSelectionEndTime() {
210 return fSelectionEndTime
;
214 public void updateSelectionRange(final long currentBeginTime
, final long currentEndTime
) {
215 if (fTrace
!= null) {
216 setSelectionRange(currentBeginTime
, currentEndTime
);
218 final ITmfTimestamp startTimestamp
= TmfTimestamp
.fromNanos(getSelectionBeginTime());
219 final ITmfTimestamp endTimestamp
= TmfTimestamp
.fromNanos(getSelectionEndTime());
221 TmfSelectionRangeUpdatedSignal signal
= new TmfSelectionRangeUpdatedSignal(this, startTimestamp
, endTimestamp
);
227 public void updateWindow(long windowStartTime
, long windowEndTime
) {
229 setWindowRange(windowStartTime
, windowEndTime
);
231 // Build the new time range; keep the current time
232 TmfTimeRange timeRange
= new TmfTimeRange(
233 TmfTimestamp
.fromNanos(getWindowStartTime()),
234 TmfTimestamp
.fromNanos(getWindowEndTime()));
237 TmfWindowRangeUpdatedSignal signal
= new TmfWindowRangeUpdatedSignal(this, timeRange
);
238 fTimeRangeSyncThrottle
.queue(signal
);
241 // ------------------------------------------------------------------------
243 // ------------------------------------------------------------------------
245 * A Method to load a trace into the viewer.
248 * A trace to apply in the viewer
250 public void loadTrace(ITmfTrace trace
) {
253 TmfTraceContext ctx
= TmfTraceManager
.getInstance().getCurrentTraceContext();
254 long selectionStart
= ctx
.getSelectionRange().getStartTime().toNanos();
255 long selectionEnd
= ctx
.getSelectionRange().getEndTime().toNanos();
256 TmfTimeRange windowRange
= ctx
.getWindowRange();
258 long windowStartTime
= windowRange
.getStartTime().toNanos();
259 long windowEndTime
= windowRange
.getEndTime().toNanos();
260 long startTime
= fTrace
.getStartTime().toNanos();
261 long endTime
= fTrace
.getEndTime().toNanos();
263 setSelectionRange(selectionStart
, selectionEnd
);
265 setStartTime(startTime
);
266 setWindowRange(windowStartTime
, windowEndTime
);
271 * Resets the content of the viewer
273 public void reset() {
274 // Reset the internal data
275 setSelectionRange(0, 0);
277 setWindowRange(0, 0);
282 // ------------------------------------------------------------------------
284 // ------------------------------------------------------------------------
287 * Signal handler for handling of the trace opened signal.
290 * The trace opened signal {@link TmfTraceOpenedSignal}
293 public void traceOpened(@Nullable TmfTraceOpenedSignal signal
) {
294 if (signal
== null) {
297 fTrace
= signal
.getTrace();
298 loadTrace(getTrace());
302 * Signal handler for handling of the trace selected signal.
305 * The trace selected signal {@link TmfTraceSelectedSignal}
308 public void traceSelected(@Nullable TmfTraceSelectedSignal signal
) {
309 if (signal
!= null && fTrace
!= signal
.getTrace()) {
310 fTrace
= signal
.getTrace();
311 loadTrace(getTrace());
316 * Signal handler for handling of the trace closed signal.
319 * The trace closed signal {@link TmfTraceClosedSignal}
322 public void traceClosed(@Nullable TmfTraceClosedSignal signal
) {
324 if (signal
== null || signal
.getTrace() != fTrace
) {
328 // Reset the internal data
334 * Signal handler for handling of the selected range signal.
337 * The {@link TmfSelectionRangeUpdatedSignal}
341 public void selectionRangeUpdated(@Nullable TmfSelectionRangeUpdatedSignal signal
) {
342 if (signal
!= null && (signal
.getSource() != this) && (fTrace
!= null)) {
343 long selectedTime
= signal
.getBeginTime().toNanos();
344 long selectedEndTime
= signal
.getEndTime().toNanos();
345 setSelectionRange(selectedTime
, selectedEndTime
);
350 * Signal handler for handling of the window range signal.
353 * The {@link TmfWindowRangeUpdatedSignal}
357 public void windowRangeUpdated(@Nullable TmfWindowRangeUpdatedSignal signal
) {
359 if (signal
!= null && fTrace
!= null) {
360 // Validate the time range
361 TmfTimeRange range
= signal
.getCurrentRange().getIntersection(fTrace
.getTimeRange());
366 if (signal
.getSource() != this) {
367 // Update the time range
368 long windowStartTime
= range
.getStartTime().toNanos();
369 long windowEndTime
= range
.getEndTime().toNanos();
371 setWindowRange(windowStartTime
, windowEndTime
);
377 * Signal handler for handling of the trace range updated signal.
380 * The trace range signal {@link TmfTraceRangeUpdatedSignal}
383 public void traceRangeUpdated(@Nullable TmfTraceRangeUpdatedSignal signal
) {
384 if (signal
== null || signal
.getTrace() != fTrace
) {
388 TmfTimeRange fullRange
= signal
.getRange();
390 long traceStartTime
= fullRange
.getStartTime().toNanos();
391 long traceEndTime
= fullRange
.getEndTime().toNanos();
393 setStartTime(traceStartTime
);
394 setEndTime(traceEndTime
);
398 * Signal handler for handling of the trace updated signal.
401 * The trace updated signal {@link TmfTraceUpdatedSignal}
404 public void traceUpdated(@Nullable TmfTraceUpdatedSignal signal
) {
405 if (signal
== null || signal
.getTrace() != fTrace
) {
408 TmfTimeRange fullRange
= signal
.getTrace().getTimeRange();
410 long traceStartTime
= fullRange
.getStartTime().toNanos();
411 long traceEndTime
= fullRange
.getEndTime().toNanos();
413 setStartTime(traceStartTime
);
414 setEndTime(traceEndTime
);