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