Partial fix for bug345440 - tmf.core.component
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramView.java
CommitLineData
6e512b93 1/*******************************************************************************
c392540b 2 * Copyright (c) 2009, 2010, 2011 Ericsson
6e512b93
ASL
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:
b59134e1 10 * William Bourque - Initial API and implementation
c392540b
FC
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
6e512b93 15 *******************************************************************************/
3e9fdb8b 16
6e512b93
ASL
17package org.eclipse.linuxtools.lttng.ui.views.histogram;
18
6c13869b 19import org.eclipse.linuxtools.lttng.core.event.LttngEvent;
4df4581d 20import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b
FC
21import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
22import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
23import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
24import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
25import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
26import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
27import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal;
28import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
29import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
30import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal;
b59134e1
WB
31import org.eclipse.linuxtools.tmf.ui.views.TmfView;
32import org.eclipse.swt.SWT;
33import org.eclipse.swt.layout.GridData;
252ae4bd 34import org.eclipse.swt.layout.GridLayout;
6e512b93 35import org.eclipse.swt.widgets.Composite;
6e512b93 36
544fe9b7
WB
37/**
38 * <b><u>HistogramView</u></b>
39 * <p>
12c155f5 40 * The purpose of this view is to provide graphical time distribution statistics about the experiment/trace events.
544fe9b7 41 * <p>
c392540b
FC
42 * The view is composed of two histograms and two controls:
43 * <ul>
44 * <li>an event distribution histogram for the whole experiment;
45 * <li>an event distribution histogram for current time window (window span);
46 * <li>the timestamp of the currently selected event;
47 * <li>the window span (size of the time window of the smaller histogram).
48 * </ul>
49 * The histograms x-axis show their respective time range.
544fe9b7 50 */
c392540b
FC
51public class HistogramView extends TmfView {
52
53 // ------------------------------------------------------------------------
54 // Constants
55 // ------------------------------------------------------------------------
56
57 // The view ID as defined in plugin.xml
3b38ea61 58 public static final String ID = "org.eclipse.linuxtools.lttng.ui.views.histogram"; //$NON-NLS-1$
c392540b
FC
59
60 // The initial window span (in nanoseconds)
61 public static long INITIAL_WINDOW_SPAN = (1L * 100 * 1000 * 1000); // .1sec
62
63 // Time scale
64 private final byte TIME_SCALE = Histogram.TIME_SCALE;
65
66 // ------------------------------------------------------------------------
67 // Attributes
68 // ------------------------------------------------------------------------
69
70 // Parent widget
71 private Composite fParent;
72
73 // The current experiment
74 private TmfExperiment<LttngEvent> fCurrentExperiment;
75
76 // Current timestamp/time window
77 private long fExperimentStartTime;
78 private long fExperimentEndTime;
79 private long fWindowStartTime;
80 private long fWindowEndTime;
81 private long fWindowSpan = INITIAL_WINDOW_SPAN;
82 private long fCurrentTimestamp;
83
84 // Time controls
85 private HistogramTextControl fCurrentEventTimeControl;
86 private HistogramTextControl fTimeSpanControl;
87
88 // Histogram/request for the full trace range
89 private static FullTraceHistogram fFullTraceHistogram;
90 private HistogramRequest fFullTraceRequest;
91
92 // Histogram/request for the selected time range
93 private static TimeRangeHistogram fTimeRangeHistogram;
94 private HistogramRequest fTimeRangeRequest;
95
96 // ------------------------------------------------------------------------
97 // Constructor
98 // ------------------------------------------------------------------------
99
100 public HistogramView() {
101 super(ID);
102 }
103
6a13fa07 104 @Override
c392540b 105 public void dispose() {
4dc47e28
FC
106 if (fTimeRangeRequest != null && !fTimeRangeRequest.isCompleted()) {
107 fTimeRangeRequest.cancel();
108 }
109 if (fFullTraceRequest != null && !fFullTraceRequest.isCompleted()) {
110 fFullTraceRequest.cancel();
111 }
6a13fa07
FC
112 fFullTraceHistogram.dispose();
113 fTimeRangeHistogram.dispose();
4dc47e28 114 super.dispose();
c392540b
FC
115 }
116
117 // ------------------------------------------------------------------------
118 // TmfView
119 // ------------------------------------------------------------------------
120
121 @Override
b59134e1 122 @SuppressWarnings("unchecked")
c392540b
FC
123 public void createPartControl(Composite parent) {
124
125 fParent = parent;
126
127 // Control labels
128 final String currentEventLabel = Messages.HistogramView_currentEventLabel;
129 final String windowSpanLabel = Messages.HistogramView_windowSpanLabel;
130
131 // --------------------------------------------------------------------
132 // Set the HistogramView layout
133 // --------------------------------------------------------------------
134
135 Composite viewComposite = new Composite(fParent, SWT.FILL);
136 GridLayout gridLayout = new GridLayout();
137 gridLayout.numColumns = 2;
138 gridLayout.horizontalSpacing = 5;
139 gridLayout.verticalSpacing = 0;
140 gridLayout.marginHeight = 0;
141 gridLayout.marginWidth = 0;
142 viewComposite.setLayout(gridLayout);
143
144 // Use all available space
145 GridData gridData = new GridData();
146 gridData.horizontalAlignment = SWT.FILL;
147 gridData.verticalAlignment = SWT.FILL;
148 gridData.grabExcessHorizontalSpace = true;
149 viewComposite.setLayoutData(gridData);
150
151 // --------------------------------------------------------------------
152 // Time controls
153 // --------------------------------------------------------------------
154
155 Composite controlsComposite = new Composite(viewComposite, SWT.FILL);
156 gridLayout = new GridLayout();
157 gridLayout.numColumns = 2;
158 gridLayout.marginHeight = 0;
159 gridLayout.marginWidth = 0;
160 gridLayout.horizontalSpacing = 5;
161 gridLayout.verticalSpacing = 0;
162 gridLayout.makeColumnsEqualWidth = true;
163 gridLayout.marginLeft = 5;
164 gridLayout.marginRight = 5;
165 controlsComposite.setLayout(gridLayout);
166
167 // Current event time control
168 gridData = new GridData();
169 gridData.horizontalAlignment = SWT.CENTER;
170 gridData.verticalAlignment = SWT.CENTER;
12c155f5
FC
171 fCurrentEventTimeControl = new HistogramCurrentTimeControl(this, controlsComposite, SWT.BORDER, SWT.BORDER,
172 currentEventLabel, HistogramUtils.nanosecondsToString(0L));
c392540b
FC
173 fCurrentEventTimeControl.setLayoutData(gridData);
174
175 // Window span time control
176 gridData = new GridData();
177 gridData.horizontalAlignment = SWT.CENTER;
178 gridData.verticalAlignment = SWT.CENTER;
12c155f5
FC
179 fTimeSpanControl = new HistogramTimeRangeControl(this, controlsComposite, SWT.BORDER, SWT.BORDER,
180 windowSpanLabel, HistogramUtils.nanosecondsToString(0L));
c392540b
FC
181 fTimeSpanControl.setLayoutData(gridData);
182
183 // --------------------------------------------------------------------
184 // Time range histogram
185 // --------------------------------------------------------------------
186
187 Composite timeRangeComposite = new Composite(viewComposite, SWT.FILL);
188 gridLayout = new GridLayout();
189 gridLayout.numColumns = 1;
190 gridLayout.marginHeight = 0;
191 gridLayout.marginWidth = 0;
192 gridLayout.marginTop = 5;
193 gridLayout.horizontalSpacing = 0;
194 gridLayout.verticalSpacing = 0;
195 gridLayout.marginLeft = 5;
196 gridLayout.marginRight = 5;
197 timeRangeComposite.setLayout(gridLayout);
198
199 // Use remaining horizontal space
200 gridData = new GridData();
201 gridData.horizontalAlignment = SWT.FILL;
202 gridData.verticalAlignment = SWT.FILL;
203 gridData.grabExcessHorizontalSpace = true;
204 timeRangeComposite.setLayoutData(gridData);
205
206 // Histogram
207 fTimeRangeHistogram = new TimeRangeHistogram(this, timeRangeComposite);
208
209 // --------------------------------------------------------------------
210 // Full range histogram
211 // --------------------------------------------------------------------
212
213 Composite fullRangeComposite = new Composite(viewComposite, SWT.FILL);
214 gridLayout = new GridLayout();
215 gridLayout.numColumns = 1;
216 gridLayout.marginHeight = 0;
217 gridLayout.marginWidth = 0;
218 gridLayout.marginTop = 5;
219 gridLayout.horizontalSpacing = 0;
220 gridLayout.verticalSpacing = 0;
221 gridLayout.marginLeft = 5;
222 gridLayout.marginRight = 5;
223 fullRangeComposite.setLayout(gridLayout);
224
225 // Use remaining horizontal space
226 gridData = new GridData();
227 gridData.horizontalAlignment = SWT.FILL;
228 gridData.verticalAlignment = SWT.FILL;
229 gridData.horizontalSpan = 2;
230 gridData.grabExcessHorizontalSpace = true;
231 fullRangeComposite.setLayoutData(gridData);
232
233 // Histogram
234 fFullTraceHistogram = new FullTraceHistogram(this, fullRangeComposite);
235
236 // Load the experiment if present
237 fCurrentExperiment = (TmfExperiment<LttngEvent>) TmfExperiment.getCurrentExperiment();
238 if (fCurrentExperiment != null)
239 loadExperiment();
ecfd1d41 240 }
c392540b
FC
241
242 @Override
3e9fdb8b 243 @SuppressWarnings("unchecked")
c392540b
FC
244 public void setFocus() {
245 TmfExperiment<LttngEvent> experiment = (TmfExperiment<LttngEvent>) TmfExperiment.getCurrentExperiment();
246 if ((experiment != null) && (experiment != fCurrentExperiment)) {
247 fCurrentExperiment = experiment;
248 initializeHistograms();
249 }
250 fParent.redraw();
833a21aa 251 }
c392540b
FC
252
253 // ------------------------------------------------------------------------
254 // Accessors
255 // ------------------------------------------------------------------------
256
257 public TmfTimeRange getTimeRange() {
12c155f5
FC
258 return new TmfTimeRange(new TmfTimestamp(fWindowStartTime, TIME_SCALE), new TmfTimestamp(fWindowEndTime,
259 TIME_SCALE));
c392540b
FC
260 }
261
262 // ------------------------------------------------------------------------
263 // Operations
264 // ------------------------------------------------------------------------
265
266 public void updateCurrentEventTime(long newTime) {
267 if (fCurrentExperiment != null) {
268 TmfTimeRange timeRange = new TmfTimeRange(new TmfTimestamp(newTime, TIME_SCALE), TmfTimestamp.BigCrunch);
fbd124dd 269 HistogramRequest request = new HistogramRequest(fTimeRangeHistogram.getDataModel(), timeRange, 0, 1, ExecutionType.FOREGROUND) {
c392540b
FC
270 @Override
271 public void handleData(LttngEvent event) {
272 if (event != null) {
273 TmfTimeSynchSignal signal = new TmfTimeSynchSignal(this, event.getTimestamp());
274 TmfSignalManager.dispatchSignal(signal);
275 }
276 }
277 };
278 fCurrentExperiment.sendRequest(request);
279 }
280 }
281
282 public void updateTimeRange(long startTime, long endTime) {
283 if (fCurrentExperiment != null) {
284 // Build the new time range; keep the current time
12c155f5
FC
285 TmfTimeRange timeRange = new TmfTimeRange(new TmfTimestamp(startTime, TIME_SCALE), new TmfTimestamp(
286 endTime, TIME_SCALE));
c392540b 287 TmfTimestamp currentTime = new TmfTimestamp(fCurrentTimestamp, TIME_SCALE);
6a13fa07 288
c392540b
FC
289 fTimeSpanControl.setValue(endTime - startTime);
290
291 // Send the FW signal
292 TmfRangeSynchSignal signal = new TmfRangeSynchSignal(this, timeRange, currentTime);
293 TmfSignalManager.dispatchSignal(signal);
294 }
295 }
296
297 public synchronized void updateTimeRange(long newDuration) {
298 if (fCurrentExperiment != null) {
6a13fa07
FC
299 long delta = newDuration - fWindowSpan;
300 long newStartTime = fWindowStartTime + delta / 2;
301 setNewRange(newStartTime, newDuration);
c392540b
FC
302 }
303 }
304
305 private void setNewRange(long startTime, long duration) {
306 if (startTime < fExperimentStartTime)
6a13fa07 307 startTime = fExperimentStartTime;
c392540b
FC
308
309 long endTime = startTime + duration;
310 if (endTime > fExperimentEndTime) {
6a13fa07
FC
311 endTime = fExperimentEndTime;
312 if (endTime - duration > fExperimentStartTime)
313 startTime = endTime - duration;
314 else {
315 startTime = fExperimentStartTime;
316 }
c392540b
FC
317 }
318 updateTimeRange(startTime, endTime);
833a21aa 319 }
c392540b
FC
320
321 // ------------------------------------------------------------------------
322 // Signal handlers
323 // ------------------------------------------------------------------------
324
1406f802 325 @TmfSignalHandler
c392540b
FC
326 @SuppressWarnings("unchecked")
327 public void experimentSelected(TmfExperimentSelectedSignal<LttngEvent> signal) {
328 assert (signal != null);
329 fCurrentExperiment = (TmfExperiment<LttngEvent>) signal.getExperiment();
330 loadExperiment();
ecfd1d41 331 }
550d787e 332
c392540b 333 private void loadExperiment() {
c392540b
FC
334 initializeHistograms();
335 fParent.redraw();
550d787e
FC
336 }
337
74237cc3 338 @TmfSignalHandler
74237cc3
FC
339 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal) {
340
09d11238
PT
341 if (signal.getExperiment() != fCurrentExperiment) {
342 return;
343 }
344
74237cc3
FC
345 boolean drawTimeRangeHistogram = fExperimentStartTime == 0;
346 TmfTimeRange fullRange = signal.getRange();
347
348 fExperimentStartTime = fullRange.getStartTime().getValue();
349 fExperimentEndTime = fullRange.getEndTime().getValue();
350
351 fFullTraceHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
352 fTimeRangeHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
353
354 if (drawTimeRangeHistogram) {
355 fCurrentTimestamp = fExperimentStartTime;
356 fCurrentEventTimeControl.setValue(fCurrentTimestamp);
357 fFullTraceHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
358 fTimeRangeHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
359 sendTimeRangeRequest(fExperimentStartTime, fExperimentStartTime + INITIAL_WINDOW_SPAN);
360 }
361
362 sendFullRangeRequest(fullRange);
363 }
c392540b
FC
364
365 @TmfSignalHandler
366 public void currentTimeUpdated(TmfTimeSynchSignal signal) {
367 // Because this can't happen :-)
368 assert (signal != null);
369
370 // Update the selected event time
4df4581d 371 ITmfTimestamp currentTime = signal.getCurrentTime();
c392540b
FC
372 fCurrentTimestamp = currentTime.getValue();
373
374 // Notify the relevant widgets
375 fFullTraceHistogram.setCurrentEvent(fCurrentTimestamp);
376 fTimeRangeHistogram.setCurrentEvent(fCurrentTimestamp);
377 fCurrentEventTimeControl.setValue(fCurrentTimestamp);
ecfd1d41 378 }
f05aabed 379
c392540b
FC
380 @TmfSignalHandler
381 public void timeRangeUpdated(TmfRangeSynchSignal signal) {
382 // Because this can't happen :-)
383 assert (signal != null);
384
385 if (fCurrentExperiment != null) {
386 // Update the time range
387 fWindowStartTime = signal.getCurrentRange().getStartTime().getValue();
388 fWindowEndTime = signal.getCurrentRange().getEndTime().getValue();
389 fWindowSpan = fWindowEndTime - fWindowStartTime;
390
391 // Notify the relevant widgets
392 sendTimeRangeRequest(fWindowStartTime, fWindowEndTime);
393 fFullTraceHistogram.setTimeRange(fWindowStartTime, fWindowSpan);
394 fTimeSpanControl.setValue(fWindowSpan);
395 }
b59134e1 396 }
c392540b
FC
397
398 // ------------------------------------------------------------------------
399 // Helper functions
400 // ------------------------------------------------------------------------
401
402 private void initializeHistograms() {
74237cc3
FC
403 TmfTimeRange fullRange = updateExperimentTimeRange(fCurrentExperiment);
404
405 fTimeRangeHistogram.clear();
c392540b
FC
406 fTimeRangeHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
407 fTimeRangeHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
408 fTimeRangeHistogram.setCurrentEvent(fExperimentStartTime);
409
74237cc3 410 fFullTraceHistogram.clear();
6a13fa07 411 fFullTraceHistogram.setFullRange(fExperimentStartTime, fExperimentEndTime);
c392540b
FC
412 fFullTraceHistogram.setTimeRange(fExperimentStartTime, INITIAL_WINDOW_SPAN);
413 fFullTraceHistogram.setCurrentEvent(fExperimentStartTime);
414
415 fWindowStartTime = fExperimentStartTime;
416 fWindowSpan = INITIAL_WINDOW_SPAN;
417 fWindowEndTime = fWindowStartTime + fWindowSpan;
6a13fa07 418
c392540b
FC
419 fCurrentEventTimeControl.setValue(fExperimentStartTime);
420 fTimeSpanControl.setValue(fWindowSpan);
6a13fa07 421
c392540b 422 sendTimeRangeRequest(fExperimentStartTime, fExperimentStartTime + fWindowSpan);
74237cc3
FC
423 sendFullRangeRequest(fullRange);
424 }
425
426 private TmfTimeRange updateExperimentTimeRange(TmfExperiment<LttngEvent> experiment) {
427 fExperimentStartTime = 0;
428 fExperimentEndTime = 0;
429 fCurrentTimestamp = 0;
430
431 TmfTimeRange timeRange = fCurrentExperiment.getTimeRange();
432 if (timeRange != TmfTimeRange.Null) {
433 fExperimentStartTime = timeRange.getStartTime().getValue();
434 fExperimentEndTime = timeRange.getEndTime().getValue();
435 fCurrentTimestamp = fExperimentStartTime;
436 }
437 return timeRange;
b59134e1 438 }
c392540b
FC
439
440 private void sendTimeRangeRequest(long startTime, long endTime) {
441 if (fTimeRangeRequest != null && !fTimeRangeRequest.isCompleted()) {
442 fTimeRangeRequest.cancel();
088c1d4e 443 }
c392540b
FC
444 TmfTimestamp startTS = new TmfTimestamp(startTime, TIME_SCALE);
445 TmfTimestamp endTS = new TmfTimestamp(endTime, TIME_SCALE);
446 TmfTimeRange timeRange = new TmfTimeRange(startTS, endTS);
447
448 fTimeRangeHistogram.clear();
449 fTimeRangeHistogram.setTimeRange(startTime, endTime - startTime);
fbd124dd 450 fTimeRangeRequest = new HistogramRequest(fTimeRangeHistogram.getDataModel(), timeRange, ExecutionType.FOREGROUND);
c392540b 451 fCurrentExperiment.sendRequest(fTimeRangeRequest);
088c1d4e 452 }
c392540b 453
74237cc3 454 private void sendFullRangeRequest(TmfTimeRange fullRange) {
c392540b
FC
455 if (fFullTraceRequest != null && !fFullTraceRequest.isCompleted()) {
456 fFullTraceRequest.cancel();
457 }
fbd124dd
BH
458 fFullTraceRequest = new HistogramRequest(fFullTraceHistogram.getDataModel(), fullRange, (int) fFullTraceHistogram.fDataModel.getNbEvents(),
459 ExecutionType.BACKGROUND);
c392540b 460 fCurrentExperiment.sendRequest(fFullTraceRequest);
ed4b3b9f 461 }
c392540b 462
6e512b93 463}
This page took 0.061826 seconds and 5 git commands to generate.