6ad857d434eb2c9aa58f9dcb85d4261ae67cf14d
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramView.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011, 2012 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 * William Bourque - Initial API and implementation
11 * Yuriy Vashchuk - GUI reorganisation, simplification and some related code improvements.
12 * Yuriy Vashchuk - Histograms optimisation.
13 * Yuriy Vashchuk - Histogram Canvas Heritage correction
14 * Francois Chouinard - Cleanup and refactoring
15 * Francois Chouinard - Moved from LTTng to TMF
16 *******************************************************************************/
17
18 package org.eclipse.linuxtools.tmf.ui.views.histogram;
19
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
22 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
23 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
24 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
25 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
26 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
27 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
28 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;
29 import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
30 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
31 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
32 import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
34 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39
40 /**
41 * The purpose of this view is to provide graphical time distribution statistics about the experiment/trace events.
42 * <p>
43 * The view is composed of two histograms and two controls:
44 * <ul>
45 * <li>an event distribution histogram for the whole experiment;
46 * <li>an event distribution histogram for current time window (window span);
47 * <li>the timestamp of the currently selected event;
48 * <li>the window span (size of the time window of the smaller histogram).
49 * </ul>
50 * The histograms x-axis show their respective time range.
51 *
52 * @version 1.0
53 * @author Francois Chouinard
54 */
55 public class HistogramView extends TmfView {
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60
61 /**
62 * The view ID as defined in plugin.xml
63 */
64 public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.histogram"; //$NON-NLS-1$
65
66 /**
67 * The initial window span (in nanoseconds)
68 */
69 public static final long INITIAL_WINDOW_SPAN = (1L * 100 * 1000 * 1000); // .1sec
70
71 // Time scale
72 private final byte TIME_SCALE = Histogram.TIME_SCALE;
73
74 // ------------------------------------------------------------------------
75 // Attributes
76 // ------------------------------------------------------------------------
77
78 // Parent widget
79 private Composite fParent;
80
81 // The current experiment
82 private TmfExperiment<ITmfEvent> fCurrentExperiment;
83
84 // Current timestamp/time window
85 private long fExperimentStartTime;
86 private long fExperimentEndTime;
87 private long fWindowStartTime;
88 private long fWindowEndTime;
89 private long fWindowSpan = INITIAL_WINDOW_SPAN;
90 private long fCurrentTimestamp;
91
92 // Time controls
93 private HistogramTextControl fCurrentEventTimeControl;
94 private HistogramTextControl fTimeSpanControl;
95
96 // Histogram/request for the full trace range
97 private static FullTraceHistogram fFullTraceHistogram;
98 private HistogramRequest fFullTraceRequest;
99
100 // Histogram/request for the selected time range
101 private static TimeRangeHistogram fTimeRangeHistogram;
102 private HistogramRequest fTimeRangeRequest;
103
104 // ------------------------------------------------------------------------
105 // Constructor
106 // ------------------------------------------------------------------------
107
108 /**
109 * Default constructor
110 */
111 public HistogramView() {
112 super(ID);
113 }
114
115 @Override
116 public void dispose() {
117 if (fTimeRangeRequest != null && !fTimeRangeRequest.isCompleted()) {
118 fTimeRangeRequest.cancel();
119 }
120 if (fFullTraceRequest != null && !fFullTraceRequest.isCompleted()) {
121 fFullTraceRequest.cancel();
122 }
123 fFullTraceHistogram.dispose();
124 fTimeRangeHistogram.dispose();
125 super.dispose();
126 }
127
128 // ------------------------------------------------------------------------
129 // TmfView
130 // ------------------------------------------------------------------------
131
132 @Override
133 @SuppressWarnings("unchecked")
134 public void createPartControl(Composite parent) {
135
136 fParent = parent;
137
138 // Control labels
139 final String currentEventLabel = Messages.HistogramView_currentEventLabel;
140 final String windowSpanLabel = Messages.HistogramView_windowSpanLabel;
141
142 // --------------------------------------------------------------------
143 // Set the HistogramView layout
144 // --------------------------------------------------------------------
145
146 Composite viewComposite = new Composite(fParent, SWT.FILL);
147 GridLayout gridLayout = new GridLayout();
148 gridLayout.numColumns = 2;
149 gridLayout.horizontalSpacing = 5;
150 gridLayout.verticalSpacing = 0;
151 gridLayout.marginHeight = 0;
152 gridLayout.marginWidth = 0;
153 viewComposite.setLayout(gridLayout);
154
155 // Use all available space
156 GridData gridData = new GridData();
157 gridData.horizontalAlignment = SWT.FILL;
158 gridData.verticalAlignment = SWT.FILL;
159 gridData.grabExcessHorizontalSpace = true;
160 viewComposite.setLayoutData(gridData);
161
162 // --------------------------------------------------------------------
163 // Time controls
164 // --------------------------------------------------------------------
165
166 Composite controlsComposite = new Composite(viewComposite, SWT.FILL);
167 gridLayout = new GridLayout();
168 gridLayout.numColumns = 2;
169 gridLayout.marginHeight = 0;
170 gridLayout.marginWidth = 0;
171 gridLayout.horizontalSpacing = 5;
172 gridLayout.verticalSpacing = 0;
173 gridLayout.makeColumnsEqualWidth = true;
174 gridLayout.marginLeft = 5;
175 gridLayout.marginRight = 5;
176 controlsComposite.setLayout(gridLayout);
177
178 // Current event time control
179 gridData = new GridData();
180 gridData.horizontalAlignment = SWT.CENTER;
181 gridData.verticalAlignment = SWT.CENTER;
182 fCurrentEventTimeControl = new HistogramCurrentTimeControl(this, controlsComposite, SWT.BORDER, SWT.NONE,
183 currentEventLabel, HistogramUtils.nanosecondsToString(0L));
184 fCurrentEventTimeControl.setLayoutData(gridData);
185
186 // Window span time control
187 gridData = new GridData();
188 gridData.horizontalAlignment = SWT.CENTER;
189 gridData.verticalAlignment = SWT.CENTER;
190 fTimeSpanControl = new HistogramTimeRangeControl(this, controlsComposite, SWT.BORDER, SWT.NONE,
191 windowSpanLabel, HistogramUtils.nanosecondsToString(0L));
192 fTimeSpanControl.setLayoutData(gridData);
193
194 // --------------------------------------------------------------------
195 // Time range histogram
196 // --------------------------------------------------------------------
197
198 Composite timeRangeComposite = new Composite(viewComposite, SWT.FILL);
199 gridLayout = new GridLayout();
200 gridLayout.numColumns = 1;
201 gridLayout.marginHeight = 0;
202 gridLayout.marginWidth = 0;
203 gridLayout.marginTop = 5;
204 gridLayout.horizontalSpacing = 0;
205 gridLayout.verticalSpacing = 0;
206 gridLayout.marginLeft = 5;
207 gridLayout.marginRight = 5;
208 timeRangeComposite.setLayout(gridLayout);
209
210 // Use remaining horizontal space
211 gridData = new GridData();
212 gridData.horizontalAlignment = SWT.FILL;
213 gridData.verticalAlignment = SWT.FILL;
214 gridData.grabExcessHorizontalSpace = true;
215 timeRangeComposite.setLayoutData(gridData);
216
217 // Histogram
218 fTimeRangeHistogram = new TimeRangeHistogram(this, timeRangeComposite);
219
220 // --------------------------------------------------------------------
221 // Full range histogram
222 // --------------------------------------------------------------------
223
224 Composite fullRangeComposite = new Composite(viewComposite, SWT.FILL);
225 gridLayout = new GridLayout();
226 gridLayout.numColumns = 1;
227 gridLayout.marginHeight = 0;
228 gridLayout.marginWidth = 0;
229 gridLayout.marginTop = 5;
230 gridLayout.horizontalSpacing = 0;
231 gridLayout.verticalSpacing = 0;
232 gridLayout.marginLeft = 5;
233 gridLayout.marginRight = 5;
234 fullRangeComposite.setLayout(gridLayout);
235
236 // Use remaining horizontal space
237 gridData = new GridData();
238 gridData.horizontalAlignment = SWT.FILL;
239 gridData.verticalAlignment = SWT.FILL;
240 gridData.horizontalSpan = 2;
241 gridData.grabExcessHorizontalSpace = true;
242 fullRangeComposite.setLayoutData(gridData);
243
244 // Histogram
245 fFullTraceHistogram = new FullTraceHistogram(this, fullRangeComposite);
246
247 // Load the experiment if present
248 fCurrentExperiment = (TmfExperiment<ITmfEvent>) TmfExperiment.getCurrentExperiment();
249 if (fCurrentExperiment != null)
250 loadExperiment();
251 }
252
253 @Override
254 @SuppressWarnings("unchecked")
255 public void setFocus() {
256 TmfExperiment<ITmfEvent> experiment = (TmfExperiment<ITmfEvent>) TmfExperiment.getCurrentExperiment();
257 if ((experiment != null) && (experiment != fCurrentExperiment)) {
258 fCurrentExperiment = experiment;
259 initializeHistograms();
260 }
261 fParent.redraw();
262 }
263
264 // ------------------------------------------------------------------------
265 // Accessors
266 // ------------------------------------------------------------------------
267
268 /**
269 * Returns the time range of the current selected window (base on default time scale).
270 *
271 * @return the time range of current selected window.
272 */
273 public TmfTimeRange getTimeRange() {
274 return new TmfTimeRange(new TmfTimestamp(fWindowStartTime, TIME_SCALE), new TmfTimestamp(fWindowEndTime,
275 TIME_SCALE));
276 }
277
278 // ------------------------------------------------------------------------
279 // Operations
280 // ------------------------------------------------------------------------
281
282 /**
283 * Broadcast TmfSignal about new current time value.
284 * @param newTime the new current time.
285 */
286 public void updateCurrentEventTime(long newTime) {
287 if (fCurrentExperiment != null) {
288 TmfTimeRange timeRange = new TmfTimeRange(new TmfTimestamp(newTime, TIME_SCALE), TmfTimestamp.BIG_CRUNCH);
289 HistogramRequest request = new HistogramRequest(fTimeRangeHistogram.getDataModel(), timeRange, 0, 1, 0, ExecutionType.FOREGROUND) {
290 @Override
291 public void handleData(ITmfEvent event) {
292 if (event != null) {
293 TmfTimeSynchSignal signal = new TmfTimeSynchSignal(this, event.getTimestamp());
294 TmfSignalManager.dispatchSignal(signal);
295 }
296 }
297 };
298 fCurrentExperiment.sendRequest(request);
299 }
300 }
301
302 /**
303 * Broadcast TmfSignal about new selected time range.
304 * @param startTime the new start time
305 * @param endTime the new end time
306 */
307 public void updateTimeRange(long startTime, long endTime) {
308 if (fCurrentExperiment != null) {
309 // Build the new time range; keep the current time
310 TmfTimeRange timeRange = new TmfTimeRange(new TmfTimestamp(startTime, TIME_SCALE), new TmfTimestamp(
311 endTime, TIME_SCALE));
312 TmfTimestamp currentTime = new TmfTimestamp(fCurrentTimestamp, TIME_SCALE);
313
314 fTimeSpanControl.setValue(endTime - startTime);
315
316 // Send the FW signal
317 TmfRangeSynchSignal signal = new TmfRangeSynchSignal(this, timeRange, currentTime);
318 TmfSignalManager.dispatchSignal(signal);
319 }
320 }
321
322 /**
323 * Broadcast TmfSignal about new selected time range.
324 * @param newDuration new duration (relative to current start time)
325 */
326 public synchronized void updateTimeRange(long newDuration) {
327 if (fCurrentExperiment != null) {
328 long delta = newDuration - fWindowSpan;
329 long newStartTime = fWindowStartTime + delta / 2;
330 setNewRange(newStartTime, newDuration);
331 }
332 }
333
334 private void setNewRange(long startTime, long duration) {
335 if (startTime < fExperimentStartTime)
336 startTime = fExperimentStartTime;
337
338 long endTime = startTime + duration;
339 if (endTime > fExperimentEndTime) {
340 endTime = fExperimentEndTime;
341 if (endTime - duration > fExperimentStartTime)
342 startTime = endTime - duration;
343 else {
344 startTime = fExperimentStartTime;
345 }
346 }
347 updateTimeRange(startTime, endTime);
348 }
349
350 // ------------------------------------------------------------------------
351 // Signal handlers
352 // ------------------------------------------------------------------------
353
354 /**
355 * Handles experiment selected signal. Loads histogram if new experiment time range is not
356 * equal <code>TmfTimeRange.NULL_RANGE</code>
357 * @param signal the experiment selected signal
358 */
359 @TmfSignalHandler
360 @SuppressWarnings("unchecked")
361 public void experimentSelected(TmfExperimentSelectedSignal<ITmfEvent> signal) {
362 assert (signal != null);
363 fCurrentExperiment = (TmfExperiment<ITmfEvent>) signal.getExperiment();
364 loadExperiment();
365 }
366
367 private void loadExperiment() {
368 initializeHistograms();
369 fParent.redraw();
370 }
371
372 /**
373 * Handles experiment range updated signal. Extends histogram according to the new time range. If a
374 * HistogramRequest is already ongoing, it will be cancelled and a new request with the new range
375 * will be issued.
376 *
377 * @param signal the experiment range updated signal
378 */
379 @TmfSignalHandler
380 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal) {
381
382 if (signal.getExperiment() != fCurrentExperiment) {
383 return;
384 }
385
386 boolean drawTimeRangeHistogram = fExperimentStartTime == 0;
387 TmfTimeRange fullRange = signal.getRange();
388
389 fExperimentStartTime = fullRange.getStartTime().normalize(0, -9).getValue();
390 fExperimentEndTime = fullRange.getEndTime().normalize(0, -9).getValue();
391
392 fFullTraceHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
393 fTimeRangeHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
394
395 if (drawTimeRangeHistogram) {
396 fCurrentTimestamp = fExperimentStartTime;
397 fCurrentEventTimeControl.setValue(fCurrentTimestamp);
398 fFullTraceHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
399 fTimeRangeHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
400 sendTimeRangeRequest(fExperimentStartTime, fExperimentStartTime + INITIAL_WINDOW_SPAN);
401 }
402
403 sendFullRangeRequest(fullRange);
404 }
405
406 /**
407 * Handles the experiment updated signal. Used to update time limits (start and end time)
408 * @param signal the experiment updated signal
409 */
410 @TmfSignalHandler
411 public void experimentUpdated(TmfExperimentUpdatedSignal signal) {
412 if (signal.getExperiment() != fCurrentExperiment) {
413 return;
414 }
415 TmfTimeRange fullRange = signal.getExperiment().getTimeRange();
416 fExperimentStartTime = fullRange.getStartTime().normalize(0, -9).getValue();
417 fExperimentEndTime = fullRange.getEndTime().normalize(0, -9).getValue();
418
419 fFullTraceHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
420 fTimeRangeHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
421 }
422
423 /**
424 * Handles the current time updated signal. Sets the current time in the time range
425 * histogram as well as the full histogram.
426 *
427 * @param signal the signal to process
428 */
429 @TmfSignalHandler
430 public void currentTimeUpdated(TmfTimeSynchSignal signal) {
431 // Because this can't happen :-)
432 assert (signal != null);
433
434 // Update the selected event time
435 ITmfTimestamp currentTime = signal.getCurrentTime();
436 fCurrentTimestamp = currentTime.normalize(0, -9).getValue();
437
438 // Notify the relevant widgets
439 fFullTraceHistogram.setCurrentEvent(fCurrentTimestamp);
440 fTimeRangeHistogram.setCurrentEvent(fCurrentTimestamp);
441 fCurrentEventTimeControl.setValue(fCurrentTimestamp);
442 }
443
444 /**
445 * Updates the current time range in the time range histogram and full range histogram.
446 * @param signal the signal to process
447 */
448 @TmfSignalHandler
449 public void timeRangeUpdated(TmfRangeSynchSignal signal) {
450 // Because this can't happen :-)
451 assert (signal != null);
452
453 if (fCurrentExperiment != null) {
454 // Update the time range
455 fWindowStartTime = signal.getCurrentRange().getStartTime().normalize(0, -9).getValue();
456 fWindowEndTime = signal.getCurrentRange().getEndTime().normalize(0, -9).getValue();
457 fWindowSpan = fWindowEndTime - fWindowStartTime;
458
459 // Notify the relevant widgets
460 sendTimeRangeRequest(fWindowStartTime, fWindowEndTime);
461 fFullTraceHistogram.setTimeRange(fWindowStartTime, fWindowSpan);
462 fTimeSpanControl.setValue(fWindowSpan);
463 }
464 }
465
466 // ------------------------------------------------------------------------
467 // Helper functions
468 // ------------------------------------------------------------------------
469
470 private void initializeHistograms() {
471 TmfTimeRange fullRange = updateExperimentTimeRange(fCurrentExperiment);
472
473 fTimeRangeHistogram.clear();
474 fTimeRangeHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
475 fTimeRangeHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
476 fTimeRangeHistogram.setCurrentEvent(fExperimentStartTime);
477
478 fFullTraceHistogram.clear();
479 fFullTraceHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
480 fFullTraceHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
481 fFullTraceHistogram.setCurrentEvent(fExperimentStartTime);
482
483 fWindowStartTime = fExperimentStartTime;
484 fWindowSpan = INITIAL_WINDOW_SPAN;
485 fWindowEndTime = fWindowStartTime + fWindowSpan;
486
487 fCurrentEventTimeControl.setValue(fExperimentStartTime);
488 fTimeSpanControl.setValue(fWindowSpan);
489
490 if (!fullRange.equals(TmfTimeRange.NULL_RANGE)) {
491 sendTimeRangeRequest(fExperimentStartTime, fExperimentStartTime + fWindowSpan);
492 sendFullRangeRequest(fullRange);
493 }
494 }
495
496 private TmfTimeRange updateExperimentTimeRange(TmfExperiment<ITmfEvent> experiment) {
497 fExperimentStartTime = 0;
498 fExperimentEndTime = 0;
499 fCurrentTimestamp = 0;
500
501 TmfTimeRange timeRange = fCurrentExperiment.getTimeRange();
502 if (!timeRange.equals(TmfTimeRange.NULL_RANGE)) {
503 fExperimentStartTime = timeRange.getStartTime().normalize(0, -9).getValue();
504 fExperimentEndTime = timeRange.getEndTime().normalize(0, -9).getValue();
505 fCurrentTimestamp = fExperimentStartTime;
506 }
507 return timeRange;
508 }
509
510 private void sendTimeRangeRequest(long startTime, long endTime) {
511 if (fTimeRangeRequest != null && !fTimeRangeRequest.isCompleted()) {
512 fTimeRangeRequest.cancel();
513 }
514 TmfTimestamp startTS = new TmfTimestamp(startTime, TIME_SCALE);
515 TmfTimestamp endTS = new TmfTimestamp(endTime, TIME_SCALE);
516 TmfTimeRange timeRange = new TmfTimeRange(startTS, endTS);
517
518 fTimeRangeHistogram.clear();
519 fTimeRangeHistogram.setTimeRange(startTime, endTime - startTime);
520
521 int cacheSize = fCurrentExperiment.getCacheSize();
522 fTimeRangeRequest = new HistogramRequest(fTimeRangeHistogram.getDataModel(), timeRange, 0, TmfDataRequest.ALL_DATA, cacheSize, ExecutionType.FOREGROUND);
523 fCurrentExperiment.sendRequest(fTimeRangeRequest);
524 }
525
526 private void sendFullRangeRequest(TmfTimeRange fullRange) {
527 if (fFullTraceRequest != null && !fFullTraceRequest.isCompleted()) {
528 fFullTraceRequest.cancel();
529 }
530 int cacheSize = fCurrentExperiment.getCacheSize();
531 fFullTraceRequest = new HistogramRequest(fFullTraceHistogram.getDataModel(), fullRange, (int) fFullTraceHistogram.fDataModel.getNbEvents(),
532 TmfDataRequest.ALL_DATA, cacheSize, ExecutionType.BACKGROUND);
533 fCurrentExperiment.sendRequest(fFullTraceRequest);
534 }
535
536 }
This page took 0.053329 seconds and 4 git commands to generate.