tmf.ui: Make TmfTimeViewer handlers parameters nullable
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / TmfTimeViewer.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others.
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.tracecompass.tmf.ui.viewers;
15
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;
33
34 /**
35 * Abstract class that extends {@link TmfViewer} that adds methods to
36 * synchronize with a trace's time information.
37 *
38 * This class will be extended by viewers who require time information to update
39 * their content.
40 *
41 * <pre>
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
46 * </pre>
47 *
48 * @author Bernd Hufmann
49 * @author Geneviève Bastien
50 */
51 public abstract class TmfTimeViewer extends TmfViewer implements ITmfTimeProvider {
52
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);
69
70 /**
71 * Default constructor.
72 */
73 public TmfTimeViewer() {
74 super();
75 }
76
77 /**
78 * Constructor that initializes the parent of the viewer
79 *
80 * @param parent
81 * The parent composite that holds this viewer
82 */
83 public TmfTimeViewer(Composite parent) {
84 this(parent, ""); //$NON-NLS-1$
85 }
86
87 /**
88 * Constructor that initializes the parent of the viewer and that sets the
89 * name of the viewer
90 *
91 * @param parent
92 * The parent composite that holds this viewer
93 * @param name
94 * The name of the viewer
95 */
96 public TmfTimeViewer(Composite parent, String name) {
97 init(parent, name);
98 }
99
100 // ------------------------------------------------------------------------
101 // Getter/Setters
102 // ------------------------------------------------------------------------
103
104 /**
105 * Sets the start time of the trace
106 *
107 * @param startTime
108 * The start time to set
109 */
110 protected void setStartTime(long startTime) {
111 fStartTime = startTime;
112 }
113
114 /**
115 * Sets the end time of the trace
116 *
117 * @param endTime
118 * The start time to set
119 */
120 protected void setEndTime(long endTime) {
121 fEndTime = endTime;
122 }
123
124 /**
125 * Sets the start time and end of the current time range window (visible
126 * range)
127 *
128 * @param windowStartTime
129 * The start time to set
130 * @param windowEndTime
131 * The start time to set
132 * @since 1.0
133 */
134 protected void setWindowRange(long windowStartTime, long windowEndTime) {
135 fWindowStartTime = windowStartTime;
136 fWindowEndTime = windowEndTime;
137 }
138
139 /**
140 * Sets the begin and end time of the selected range without sending the
141 * {@link TmfSelectionRangeUpdatedSignal} signal.
142 *
143 * @param selectionBeginTime
144 * The begin time to set
145 * @param selectionEndTime
146 * The end time to set
147 *
148 * @since 1.0
149 */
150 protected void setSelectionRange(long selectionBeginTime, long selectionEndTime) {
151 fSelectionBeginTime = selectionBeginTime;
152 fSelectionEndTime = selectionEndTime;
153 }
154
155 /**
156 * Sets the trace that is displayed by this viewer.
157 *
158 * @param trace
159 * The trace to set
160 */
161 protected void setTrace(ITmfTrace trace) {
162 fTrace = trace;
163 }
164
165 /**
166 * Gets the trace that is displayed by this viewer.
167 *
168 * @return the trace
169 */
170 protected ITmfTrace getTrace() {
171 return fTrace;
172 }
173
174 // ------------------------------------------------------------------------
175 // ITmfTimeProvider
176 // ------------------------------------------------------------------------
177
178 @Override
179 public long getStartTime() {
180 return fStartTime;
181 }
182
183 @Override
184 public long getEndTime() {
185 return fEndTime;
186 }
187
188 @Override
189 public long getWindowStartTime() {
190 return fWindowStartTime;
191 }
192
193 @Override
194 public long getWindowEndTime() {
195 return fWindowEndTime;
196 }
197
198 @Override
199 public long getWindowDuration() {
200 return getWindowEndTime() - getWindowStartTime();
201 }
202
203 @Override
204 public long getSelectionBeginTime() {
205 return fSelectionBeginTime;
206 }
207
208 @Override
209 public long getSelectionEndTime() {
210 return fSelectionEndTime;
211 }
212
213 @Override
214 public void updateSelectionRange(final long currentBeginTime, final long currentEndTime) {
215 if (fTrace != null) {
216 setSelectionRange(currentBeginTime, currentEndTime);
217
218 final ITmfTimestamp startTimestamp = TmfTimestamp.fromNanos(getSelectionBeginTime());
219 final ITmfTimestamp endTimestamp = TmfTimestamp.fromNanos(getSelectionEndTime());
220
221 TmfSelectionRangeUpdatedSignal signal = new TmfSelectionRangeUpdatedSignal(this, startTimestamp, endTimestamp);
222 broadcast(signal);
223 }
224 }
225
226 @Override
227 public void updateWindow(long windowStartTime, long windowEndTime) {
228
229 setWindowRange(windowStartTime, windowEndTime);
230
231 // Build the new time range; keep the current time
232 TmfTimeRange timeRange = new TmfTimeRange(
233 TmfTimestamp.fromNanos(getWindowStartTime()),
234 TmfTimestamp.fromNanos(getWindowEndTime()));
235
236 // Send the signal
237 TmfWindowRangeUpdatedSignal signal = new TmfWindowRangeUpdatedSignal(this, timeRange);
238 fTimeRangeSyncThrottle.queue(signal);
239 }
240
241 // ------------------------------------------------------------------------
242 // Operations
243 // ------------------------------------------------------------------------
244 /**
245 * A Method to load a trace into the viewer.
246 *
247 * @param trace
248 * A trace to apply in the viewer
249 */
250 public void loadTrace(ITmfTrace trace) {
251 fTrace = trace;
252
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();
257
258 long windowStartTime = windowRange.getStartTime().toNanos();
259 long windowEndTime = windowRange.getEndTime().toNanos();
260 long startTime = fTrace.getStartTime().toNanos();
261 long endTime = fTrace.getEndTime().toNanos();
262
263 setSelectionRange(selectionStart, selectionEnd);
264
265 setStartTime(startTime);
266 setWindowRange(windowStartTime, windowEndTime);
267 setEndTime(endTime);
268 }
269
270 /**
271 * Resets the content of the viewer
272 */
273 public void reset() {
274 // Reset the internal data
275 setSelectionRange(0, 0);
276 setStartTime(0);
277 setWindowRange(0, 0);
278 setEndTime(0);
279 setTrace(null);
280 }
281
282 // ------------------------------------------------------------------------
283 // Signal Handler
284 // ------------------------------------------------------------------------
285
286 /**
287 * Signal handler for handling of the trace opened signal.
288 *
289 * @param signal
290 * The trace opened signal {@link TmfTraceOpenedSignal}
291 */
292 @TmfSignalHandler
293 public void traceOpened(@Nullable TmfTraceOpenedSignal signal) {
294 if (signal == null) {
295 return;
296 }
297 fTrace = signal.getTrace();
298 loadTrace(getTrace());
299 }
300
301 /**
302 * Signal handler for handling of the trace selected signal.
303 *
304 * @param signal
305 * The trace selected signal {@link TmfTraceSelectedSignal}
306 */
307 @TmfSignalHandler
308 public void traceSelected(@Nullable TmfTraceSelectedSignal signal) {
309 if (signal != null && fTrace != signal.getTrace()) {
310 fTrace = signal.getTrace();
311 loadTrace(getTrace());
312 }
313 }
314
315 /**
316 * Signal handler for handling of the trace closed signal.
317 *
318 * @param signal
319 * The trace closed signal {@link TmfTraceClosedSignal}
320 */
321 @TmfSignalHandler
322 public void traceClosed(@Nullable TmfTraceClosedSignal signal) {
323
324 if (signal == null || signal.getTrace() != fTrace) {
325 return;
326 }
327
328 // Reset the internal data
329 fTrace = null;
330 reset();
331 }
332
333 /**
334 * Signal handler for handling of the selected range signal.
335 *
336 * @param signal
337 * The {@link TmfSelectionRangeUpdatedSignal}
338 * @since 1.0
339 */
340 @TmfSignalHandler
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);
346 }
347 }
348
349 /**
350 * Signal handler for handling of the window range signal.
351 *
352 * @param signal
353 * The {@link TmfWindowRangeUpdatedSignal}
354 * @since 1.0
355 */
356 @TmfSignalHandler
357 public void windowRangeUpdated(@Nullable TmfWindowRangeUpdatedSignal signal) {
358
359 if (signal != null && fTrace != null) {
360 // Validate the time range
361 TmfTimeRange range = signal.getCurrentRange().getIntersection(fTrace.getTimeRange());
362 if (range == null) {
363 return;
364 }
365
366 if (signal.getSource() != this) {
367 // Update the time range
368 long windowStartTime = range.getStartTime().toNanos();
369 long windowEndTime = range.getEndTime().toNanos();
370
371 setWindowRange(windowStartTime, windowEndTime);
372 }
373 }
374 }
375
376 /**
377 * Signal handler for handling of the trace range updated signal.
378 *
379 * @param signal
380 * The trace range signal {@link TmfTraceRangeUpdatedSignal}
381 */
382 @TmfSignalHandler
383 public void traceRangeUpdated(@Nullable TmfTraceRangeUpdatedSignal signal) {
384 if (signal == null || signal.getTrace() != fTrace) {
385 return;
386 }
387
388 TmfTimeRange fullRange = signal.getRange();
389
390 long traceStartTime = fullRange.getStartTime().toNanos();
391 long traceEndTime = fullRange.getEndTime().toNanos();
392
393 setStartTime(traceStartTime);
394 setEndTime(traceEndTime);
395 }
396
397 /**
398 * Signal handler for handling of the trace updated signal.
399 *
400 * @param signal
401 * The trace updated signal {@link TmfTraceUpdatedSignal}
402 */
403 @TmfSignalHandler
404 public void traceUpdated(@Nullable TmfTraceUpdatedSignal signal) {
405 if (signal == null || signal.getTrace() != fTrace) {
406 return;
407 }
408 TmfTimeRange fullRange = signal.getTrace().getTimeRange();
409
410 long traceStartTime = fullRange.getStartTime().toNanos();
411 long traceEndTime = fullRange.getEndTime().toNanos();
412
413 setStartTime(traceStartTime);
414 setEndTime(traceEndTime);
415 }
416
417 }
This page took 0.039175 seconds and 5 git commands to generate.