tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / TmfTimeViewer.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 in TmfXYChartViewer
11 * Geneviève Bastien - Moved methods from TmfXYChartViewer to this interface
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.viewers;
15
16 import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
17 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
18 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalThrottler;
19 import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
20 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
21 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
22 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
23 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceSelectedSignal;
24 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
25 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
27 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
30 import org.eclipse.swt.widgets.Composite;
31
32 /**
33 * Abstract class that extends {@link TmfViewer} that adds methods to
34 * synchronize with a trace's time information.
35 *
36 * This class will be extended by viewers who require time information to update
37 * their content.
38 *
39 * <pre>
40 * It provides three times of data:
41 * - start and end time of the trace (available)
42 * - start, end and duration of the current time window, ie the visible time range
43 * - start and end of the time range selected
44 * </pre>
45 *
46 * @author Bernd Hufmann
47 * @author Geneviève Bastien
48 * @since 3.0
49 */
50 public abstract class TmfTimeViewer extends TmfViewer implements ITmfTimeProvider {
51
52 /** Start time of trace */
53 private long fStartTime;
54 /** End time of trace */
55 private long fEndTime;
56 /** Start time of current time range */
57 private long fWindowStartTime;
58 /** End time of current time range */
59 private long fWindowEndTime;
60 /** Duration of current time range */
61 private long fWindowDuration;
62 /** Current begin time of selection range */
63 private long fSelectionBeginTime;
64 /** Current end of selection range */
65 private long fSelectionEndTime;
66 /** The trace that is displayed by this viewer */
67 private ITmfTrace fTrace;
68 /** A signal throttler for range updates */
69 private final TmfSignalThrottler fTimeRangeSyncThrottle = new TmfSignalThrottler(this, 200);
70
71 /**
72 * Default constructor.
73 */
74 public TmfTimeViewer() {
75 super();
76 }
77
78 /**
79 * Constructor that initializes the parent of the viewer
80 *
81 * @param parent
82 * The parent composite that holds this viewer
83 */
84 public TmfTimeViewer(Composite parent) {
85 this(parent, ""); //$NON-NLS-1$
86 }
87
88 /**
89 * Constructor that initializes the parent of the viewer and that sets the
90 * name of the viewer
91 *
92 * @param parent
93 * The parent composite that holds this viewer
94 * @param name
95 * The name of the viewer
96 */
97 public TmfTimeViewer(Composite parent, String name) {
98 init(parent, name);
99 }
100
101 // ------------------------------------------------------------------------
102 // Getter/Setters
103 // ------------------------------------------------------------------------
104
105 /**
106 * Sets the start time of the trace
107 *
108 * @param startTime
109 * The start time to set
110 */
111 protected void setStartTime(long startTime) {
112 fStartTime = startTime;
113 }
114
115 /**
116 * Sets the end time of the trace
117 *
118 * @param endTime
119 * The start time to set
120 */
121 protected void setEndTime(long endTime) {
122 fEndTime = endTime;
123 }
124
125 /**
126 * Sets the start time of the current time range window (visible range)
127 *
128 * @param windowStartTime
129 * The start time to set
130 */
131 protected void setWindowStartTime(long windowStartTime) {
132 fWindowStartTime = windowStartTime;
133 }
134
135 /**
136 * Sets the end time of the current time range window (visible range)
137 *
138 * @param windowEndTime
139 * The start time to set
140 */
141 protected void setWindowEndTime(long windowEndTime) {
142 fWindowEndTime = windowEndTime;
143 }
144
145 /**
146 * Sets the duration of the current time range window (visible range)
147 *
148 * @param windowDuration
149 * The window duration
150 */
151 protected void setWindowDuration(long windowDuration) {
152 fWindowDuration = windowDuration;
153 }
154
155 /**
156 * Sets the begin time of the selected range.
157 *
158 * @param selectionBeginTime
159 * The begin time to set
160 */
161 protected void setSelectionBeginTime(long selectionBeginTime) {
162 fSelectionBeginTime = selectionBeginTime;
163 }
164
165 /**
166 * Sets the end time of the selected range.
167 *
168 * @param selectionEndTime
169 * The end time to set
170 */
171 protected void setSelectionEndTime(long selectionEndTime) {
172 fSelectionEndTime = selectionEndTime;
173 }
174
175 /**
176 * Sets the trace that is displayed by this viewer.
177 *
178 * @param trace
179 * The trace to set
180 */
181 protected void setTrace(ITmfTrace trace) {
182 fTrace = trace;
183 }
184
185 /**
186 * Gets the trace that is displayed by this viewer.
187 *
188 * @return the trace
189 */
190 protected ITmfTrace getTrace() {
191 return fTrace;
192 }
193
194 // ------------------------------------------------------------------------
195 // ITmfTimeProvider
196 // ------------------------------------------------------------------------
197
198 @Override
199 public long getStartTime() {
200 return fStartTime;
201 }
202
203 @Override
204 public long getEndTime() {
205 return fEndTime;
206 }
207
208 @Override
209 public long getWindowStartTime() {
210 return fWindowStartTime;
211 }
212
213 @Override
214 public long getWindowEndTime() {
215 return fWindowEndTime;
216 }
217
218 @Override
219 public long getWindowDuration() {
220 return fWindowDuration;
221 }
222
223 @Override
224 public long getSelectionBeginTime() {
225 return fSelectionBeginTime;
226 }
227
228 @Override
229 public long getSelectionEndTime() {
230 return fSelectionEndTime;
231 }
232
233 @Override
234 public void updateSelectionRange(final long currentBeginTime, final long currentEndTime) {
235 if (fTrace != null) {
236 setSelectionBeginTime(currentBeginTime);
237 setSelectionEndTime(currentEndTime);
238
239 final ITmfTimestamp startTimestamp = new TmfTimestamp(getSelectionBeginTime(), ITmfTimestamp.NANOSECOND_SCALE);
240 final ITmfTimestamp endTimestamp = new TmfTimestamp(getSelectionEndTime(), ITmfTimestamp.NANOSECOND_SCALE);
241
242 TmfTimeSynchSignal signal = new TmfTimeSynchSignal(this, startTimestamp, endTimestamp);
243 broadcast(signal);
244 }
245 }
246
247 @Override
248 public void updateWindow(long windowStartTime, long windowEndTime) {
249
250 setWindowStartTime(windowStartTime);
251 setWindowEndTime(windowEndTime);
252 setWindowDuration(windowEndTime - windowStartTime);
253
254 // Build the new time range; keep the current time
255 TmfTimeRange timeRange = new TmfTimeRange(
256 new TmfTimestamp(getWindowStartTime(), ITmfTimestamp.NANOSECOND_SCALE),
257 new TmfTimestamp(getWindowEndTime(), ITmfTimestamp.NANOSECOND_SCALE));
258
259 // Send the signal
260 TmfRangeSynchSignal signal = new TmfRangeSynchSignal(this, timeRange);
261 fTimeRangeSyncThrottle.queue(signal);
262 }
263
264 // ------------------------------------------------------------------------
265 // Operations
266 // ------------------------------------------------------------------------
267 /**
268 * A Method to load a trace into the viewer.
269 *
270 * @param trace
271 * A trace to apply in the viewer
272 */
273 public void loadTrace(ITmfTrace trace) {
274 fTrace = trace;
275
276 long timestamp = TmfTraceManager.getInstance().getSelectionBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
277 TmfTimeRange currentRange = TmfTraceManager.getInstance().getCurrentRange();
278 long windowStartTime = currentRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
279 long windowEndTime = currentRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
280 long windowDuration = windowEndTime - windowStartTime;
281 long startTime = fTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
282 long endTime = fTrace.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
283
284 setSelectionBeginTime(timestamp);
285 setSelectionEndTime(timestamp);
286 setStartTime(startTime);
287 setWindowStartTime(windowStartTime);
288 setWindowEndTime(windowEndTime);
289 setWindowDuration(windowDuration);
290 setEndTime(endTime);
291 }
292
293 /**
294 * Resets the content of the viewer
295 */
296 public void reset() {
297 // Reset the internal data
298 setSelectionBeginTime(0);
299 setSelectionEndTime(0);
300 setStartTime(0);
301 setWindowStartTime(0);
302 setWindowDuration(0);
303 setEndTime(0);
304 setWindowEndTime(0);
305 setTrace(null);
306 }
307
308 // ------------------------------------------------------------------------
309 // Signal Handler
310 // ------------------------------------------------------------------------
311
312 /**
313 * Signal handler for handling of the trace opened signal.
314 *
315 * @param signal
316 * The trace opened signal {@link TmfTraceOpenedSignal}
317 */
318 @TmfSignalHandler
319 public void traceOpened(TmfTraceOpenedSignal signal) {
320 fTrace = signal.getTrace();
321 loadTrace(getTrace());
322 }
323
324 /**
325 * Signal handler for handling of the trace selected signal.
326 *
327 * @param signal
328 * The trace selected signal {@link TmfTraceSelectedSignal}
329 */
330 @TmfSignalHandler
331 public void traceSelected(TmfTraceSelectedSignal signal) {
332 if (fTrace != signal.getTrace()) {
333 fTrace = signal.getTrace();
334 loadTrace(getTrace());
335 }
336 }
337
338 /**
339 * Signal handler for handling of the trace closed signal.
340 *
341 * @param signal
342 * The trace closed signal {@link TmfTraceClosedSignal}
343 */
344 @TmfSignalHandler
345 public void traceClosed(TmfTraceClosedSignal signal) {
346
347 if (signal.getTrace() != fTrace) {
348 return;
349 }
350
351 // Reset the internal data
352 fTrace = null;
353 reset();
354 }
355
356 /**
357 * Signal handler for handling of the time synch signal, ie the selected range.
358 *
359 * @param signal
360 * The time synch signal {@link TmfTimeSynchSignal}
361 */
362 @TmfSignalHandler
363 public void selectionRangeUpdated(TmfTimeSynchSignal signal) {
364 if ((signal.getSource() != this) && (fTrace != null)) {
365 ITmfTimestamp selectedTime = signal.getBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
366 ITmfTimestamp selectedEndTime = signal.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
367 setSelectionBeginTime(selectedTime.getValue());
368 setSelectionEndTime(selectedEndTime.getValue());
369 }
370 }
371
372 /**
373 * Signal handler for handling of the time range synch signal, ie the visible range.
374 *
375 * @param signal
376 * The time range synch signal {@link TmfRangeSynchSignal}
377 */
378 @TmfSignalHandler
379 public void timeRangeUpdated(TmfRangeSynchSignal signal) {
380
381 if (fTrace != null) {
382 // Validate the time range
383 TmfTimeRange range = signal.getCurrentRange().getIntersection(fTrace.getTimeRange());
384 if (range == null) {
385 return;
386 }
387
388 if (signal.getSource() != this) {
389 // Update the time range
390 long windowStartTime = range.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
391 long windowEndTime = range.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
392 long windowDuration = windowEndTime - windowStartTime;
393
394 setWindowStartTime(windowStartTime);
395 setWindowEndTime(windowEndTime);
396 setWindowDuration(windowDuration);
397 }
398 }
399 }
400
401 /**
402 * Signal handler for handling of the trace range updated signal.
403 *
404 * @param signal
405 * The trace range signal {@link TmfTraceRangeUpdatedSignal}
406 */
407 @TmfSignalHandler
408 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
409
410 if (signal.getTrace() != fTrace) {
411 return;
412 }
413
414 TmfTimeRange fullRange = signal.getRange();
415
416 long traceStartTime = fullRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
417 long traceEndTime = fullRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
418
419 setStartTime(traceStartTime);
420 setEndTime(traceEndTime);
421 }
422
423 /**
424 * Signal handler for handling of the trace updated signal.
425 *
426 * @param signal
427 * The trace updated signal {@link TmfTraceUpdatedSignal}
428 */
429 @TmfSignalHandler
430 public void traceUpdated(TmfTraceUpdatedSignal signal) {
431 if (signal.getTrace() != fTrace) {
432 return;
433 }
434 TmfTimeRange fullRange = signal.getTrace().getTimeRange();
435 long traceStartTime = fullRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
436 long traceEndTime = fullRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
437
438 setStartTime(traceStartTime);
439 setEndTime(traceEndTime);
440 }
441
442 }
This page took 0.041231 seconds and 5 git commands to generate.