Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / histogram / HistogramView.java
CommitLineData
6e512b93 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2009, 2015 Ericsson
1b055dfa 3 *
6e512b93
ASL
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
1b055dfa 8 *
6e512b93 9 * Contributors:
b59134e1 10 * William Bourque - Initial API and implementation
c392540b 11 * Yuriy Vashchuk - GUI reorganisation, simplification and some related code improvements.
1b055dfa 12 * Yuriy Vashchuk - Histograms optimisation.
c392540b
FC
13 * Yuriy Vashchuk - Histogram Canvas Heritage correction
14 * Francois Chouinard - Cleanup and refactoring
e0752744 15 * Francois Chouinard - Moved from LTTng to TMF
65cdf787 16 * Patrick Tasse - Update for mouse wheel zoom
2fc582d2 17 * Xavier Raynaud - Support multi-trace coloring
6e512b93 18 *******************************************************************************/
3e9fdb8b 19
2bdf0193 20package org.eclipse.tracecompass.tmf.ui.views.histogram;
6e512b93 21
c14c0757
GB
22import java.util.Collection;
23
dab5f596 24import org.eclipse.jdt.annotation.NonNull;
95aa81ef
JCK
25import org.eclipse.jface.action.Action;
26import org.eclipse.jface.action.IAction;
27import org.eclipse.jface.action.Separator;
b59134e1 28import org.eclipse.swt.SWT;
2fc582d2 29import org.eclipse.swt.custom.CLabel;
720d67cb
PT
30import org.eclipse.swt.events.MouseAdapter;
31import org.eclipse.swt.events.MouseEvent;
65cdf787 32import org.eclipse.swt.events.MouseWheelListener;
720d67cb
PT
33import org.eclipse.swt.events.PaintEvent;
34import org.eclipse.swt.events.PaintListener;
2fc582d2 35import org.eclipse.swt.graphics.GC;
720d67cb
PT
36import org.eclipse.swt.graphics.Image;
37import org.eclipse.swt.graphics.Rectangle;
b59134e1 38import org.eclipse.swt.layout.GridData;
252ae4bd 39import org.eclipse.swt.layout.GridLayout;
2fc582d2 40import org.eclipse.swt.layout.RowLayout;
6e512b93 41import org.eclipse.swt.widgets.Composite;
2fc582d2 42import org.eclipse.swt.widgets.Control;
80c930fa 43import org.eclipse.swt.widgets.Display;
720d67cb 44import org.eclipse.swt.widgets.Label;
2bdf0193
AM
45import org.eclipse.tracecompass.internal.tmf.ui.Activator;
46import org.eclipse.tracecompass.internal.tmf.ui.ITmfImageConstants;
47import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
48import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
49import org.eclipse.tracecompass.tmf.core.signal.TmfRangeSynchSignal;
50import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
51import org.eclipse.tracecompass.tmf.core.signal.TmfSignalThrottler;
52import org.eclipse.tracecompass.tmf.core.signal.TmfTimeSynchSignal;
53import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
54import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
55import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
56import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
57import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
58import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
59import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
60import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
61import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
62import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
63import org.eclipse.tracecompass.tmf.ui.views.TmfView;
95aa81ef 64import org.eclipse.ui.IActionBars;
6e512b93 65
544fe9b7 66/**
faa38350 67 * The purpose of this view is to provide graphical time distribution statistics about the trace events.
544fe9b7 68 * <p>
c392540b
FC
69 * The view is composed of two histograms and two controls:
70 * <ul>
faa38350 71 * <li>an event distribution histogram for the whole trace;
c392540b
FC
72 * <li>an event distribution histogram for current time window (window span);
73 * <li>the timestamp of the currently selected event;
74 * <li>the window span (size of the time window of the smaller histogram).
75 * </ul>
76 * The histograms x-axis show their respective time range.
abbdd66a 77 *
f8177ba2 78 * @version 2.0
2af7df97 79 * @author Francois Chouinard
544fe9b7 80 */
c392540b
FC
81public class HistogramView extends TmfView {
82
83 // ------------------------------------------------------------------------
84 // Constants
85 // ------------------------------------------------------------------------
86
b544077e
BH
87 /**
88 * The view ID as defined in plugin.xml
89 */
dab5f596 90 public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.ui.views.histogram"; //$NON-NLS-1$
c392540b 91
720d67cb
PT
92 private static final Image LINK_IMG = Activator.getDefault().getImageFromPath("/icons/etool16/link.gif"); //$NON-NLS-1$
93
c392540b
FC
94 // ------------------------------------------------------------------------
95 // Attributes
96 // ------------------------------------------------------------------------
97
98 // Parent widget
99 private Composite fParent;
100
faa38350
PT
101 // The current trace
102 private ITmfTrace fTrace;
c392540b 103
f8177ba2 104 // Current timestamp/time window - everything in the TIME_SCALE
faa38350
PT
105 private long fTraceStartTime;
106 private long fTraceEndTime;
c392540b
FC
107 private long fWindowStartTime;
108 private long fWindowEndTime;
d7ee91bb 109 private long fWindowSpan;
0fcf3b09
PT
110 private long fSelectionBeginTime;
111 private long fSelectionEndTime;
c392540b
FC
112
113 // Time controls
720d67cb
PT
114 private HistogramTextControl fSelectionStartControl;
115 private HistogramTextControl fSelectionEndControl;
c392540b
FC
116 private HistogramTextControl fTimeSpanControl;
117
720d67cb
PT
118 // Link
119 private Label fLinkButton;
120 private boolean fLinkState;
121
c392540b
FC
122 // Histogram/request for the full trace range
123 private static FullTraceHistogram fFullTraceHistogram;
124 private HistogramRequest fFullTraceRequest;
125
126 // Histogram/request for the selected time range
127 private static TimeRangeHistogram fTimeRangeHistogram;
128 private HistogramRequest fTimeRangeRequest;
129
2fc582d2
XR
130 // Legend area
131 private Composite fLegendArea;
132 private Image[] fLegendImages;
133
38df2c82
AM
134 // Throttlers for the time sync and time-range sync signals
135 private final TmfSignalThrottler fTimeSyncThrottle;
136 private final TmfSignalThrottler fTimeRangeSyncThrottle;
137
95aa81ef
JCK
138 // Action for toggle showing the lost events
139 private Action hideLostEventsAction;
2fc582d2
XR
140 // Action for toggle showing the traces
141 private Action showTraceAction;
95aa81ef 142
c392540b
FC
143 // ------------------------------------------------------------------------
144 // Constructor
145 // ------------------------------------------------------------------------
146
b544077e
BH
147 /**
148 * Default constructor
149 */
c392540b
FC
150 public HistogramView() {
151 super(ID);
38df2c82
AM
152 fTimeSyncThrottle = new TmfSignalThrottler(this, 200);
153 fTimeRangeSyncThrottle = new TmfSignalThrottler(this, 200);
c392540b
FC
154 }
155
6a13fa07 156 @Override
c392540b 157 public void dispose() {
1b055dfa 158 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
4dc47e28
FC
159 fTimeRangeRequest.cancel();
160 }
1b055dfa 161 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
4dc47e28
FC
162 fFullTraceRequest.cancel();
163 }
6a13fa07
FC
164 fFullTraceHistogram.dispose();
165 fTimeRangeHistogram.dispose();
720d67cb
PT
166 fSelectionStartControl.dispose();
167 fSelectionEndControl.dispose();
f8177ba2 168 fTimeSpanControl.dispose();
03ec1f6a
MAL
169 disposeLegendImages();
170
4dc47e28 171 super.dispose();
c392540b
FC
172 }
173
03ec1f6a
MAL
174 private void disposeLegendImages() {
175 if (fLegendImages != null) {
176 for (Image i: fLegendImages) {
177 i.dispose();
178 }
179 }
180 fLegendImages = null;
181 }
182
c392540b
FC
183 // ------------------------------------------------------------------------
184 // TmfView
185 // ------------------------------------------------------------------------
186
187 @Override
c392540b
FC
188 public void createPartControl(Composite parent) {
189
190 fParent = parent;
191
192 // Control labels
720d67cb
PT
193 final String selectionStartLabel = Messages.HistogramView_selectionStartLabel;
194 final String selectionEndLabel = Messages.HistogramView_selectionEndLabel;
c392540b
FC
195 final String windowSpanLabel = Messages.HistogramView_windowSpanLabel;
196
197 // --------------------------------------------------------------------
198 // Set the HistogramView layout
199 // --------------------------------------------------------------------
200
201 Composite viewComposite = new Composite(fParent, SWT.FILL);
202 GridLayout gridLayout = new GridLayout();
203 gridLayout.numColumns = 2;
204 gridLayout.horizontalSpacing = 5;
205 gridLayout.verticalSpacing = 0;
206 gridLayout.marginHeight = 0;
207 gridLayout.marginWidth = 0;
208 viewComposite.setLayout(gridLayout);
209
c392540b
FC
210 // --------------------------------------------------------------------
211 // Time controls
212 // --------------------------------------------------------------------
213
720d67cb 214 Composite controlsComposite = new Composite(viewComposite, SWT.NONE);
c392540b
FC
215 gridLayout = new GridLayout();
216 gridLayout.numColumns = 2;
217 gridLayout.marginHeight = 0;
218 gridLayout.marginWidth = 0;
219 gridLayout.horizontalSpacing = 5;
720d67cb 220 gridLayout.verticalSpacing = 1;
f8177ba2 221 gridLayout.makeColumnsEqualWidth = false;
c392540b 222 controlsComposite.setLayout(gridLayout);
720d67cb
PT
223 GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false, false);
224 controlsComposite.setLayoutData(gridData);
225
226 Composite selectionGroup = new Composite(controlsComposite, SWT.BORDER);
227 gridLayout = new GridLayout();
228 gridLayout.marginHeight = 0;
229 gridLayout.marginWidth = 0;
230 gridLayout.horizontalSpacing = 0;
231 gridLayout.verticalSpacing = 0;
232 selectionGroup.setLayout(gridLayout);
c392540b 233
720d67cb 234 // Selection start control
c392540b 235 gridData = new GridData();
720d67cb
PT
236 gridData.horizontalAlignment = SWT.FILL;
237 gridData.verticalAlignment = SWT.CENTER;
238 fSelectionStartControl = new HistogramSelectionStartControl(this, selectionGroup, selectionStartLabel, 0L);
239 fSelectionStartControl.setLayoutData(gridData);
240 fSelectionStartControl.setValue(Long.MIN_VALUE);
241
242 // Selection end control
243 gridData = new GridData();
244 gridData.horizontalAlignment = SWT.FILL;
c392540b 245 gridData.verticalAlignment = SWT.CENTER;
720d67cb
PT
246 fSelectionEndControl = new HistogramSelectionEndControl(this, selectionGroup, selectionEndLabel, 0L);
247 fSelectionEndControl.setLayoutData(gridData);
248 fSelectionEndControl.setValue(Long.MIN_VALUE);
249
250 // Link button
251 gridData = new GridData();
252 fLinkButton = new Label(controlsComposite, SWT.NONE);
253 fLinkButton.setImage(LINK_IMG);
254 fLinkButton.setLayoutData(gridData);
255 addLinkButtonListeners();
c392540b
FC
256
257 // Window span time control
258 gridData = new GridData();
720d67cb 259 gridData.horizontalAlignment = SWT.FILL;
c392540b 260 gridData.verticalAlignment = SWT.CENTER;
f8177ba2 261 fTimeSpanControl = new HistogramTimeRangeControl(this, controlsComposite, windowSpanLabel, 0L);
c392540b 262 fTimeSpanControl.setLayoutData(gridData);
da7bdcbc 263 fTimeSpanControl.setValue(Long.MIN_VALUE);
c392540b
FC
264
265 // --------------------------------------------------------------------
266 // Time range histogram
267 // --------------------------------------------------------------------
268
720d67cb 269 Composite timeRangeComposite = new Composite(viewComposite, SWT.NONE);
c392540b
FC
270 gridLayout = new GridLayout();
271 gridLayout.numColumns = 1;
272 gridLayout.marginHeight = 0;
273 gridLayout.marginWidth = 0;
274 gridLayout.marginTop = 5;
275 gridLayout.horizontalSpacing = 0;
276 gridLayout.verticalSpacing = 0;
277 gridLayout.marginLeft = 5;
278 gridLayout.marginRight = 5;
279 timeRangeComposite.setLayout(gridLayout);
280
281 // Use remaining horizontal space
282 gridData = new GridData();
283 gridData.horizontalAlignment = SWT.FILL;
284 gridData.verticalAlignment = SWT.FILL;
285 gridData.grabExcessHorizontalSpace = true;
3a790c10 286 gridData.grabExcessVerticalSpace = true;
c392540b
FC
287 timeRangeComposite.setLayoutData(gridData);
288
289 // Histogram
290 fTimeRangeHistogram = new TimeRangeHistogram(this, timeRangeComposite);
291
292 // --------------------------------------------------------------------
293 // Full range histogram
294 // --------------------------------------------------------------------
295
296 Composite fullRangeComposite = new Composite(viewComposite, SWT.FILL);
297 gridLayout = new GridLayout();
298 gridLayout.numColumns = 1;
299 gridLayout.marginHeight = 0;
300 gridLayout.marginWidth = 0;
301 gridLayout.marginTop = 5;
302 gridLayout.horizontalSpacing = 0;
303 gridLayout.verticalSpacing = 0;
304 gridLayout.marginLeft = 5;
305 gridLayout.marginRight = 5;
306 fullRangeComposite.setLayout(gridLayout);
307
308 // Use remaining horizontal space
309 gridData = new GridData();
310 gridData.horizontalAlignment = SWT.FILL;
311 gridData.verticalAlignment = SWT.FILL;
312 gridData.horizontalSpan = 2;
313 gridData.grabExcessHorizontalSpace = true;
3a790c10 314 gridData.grabExcessVerticalSpace = true;
c392540b
FC
315 fullRangeComposite.setLayoutData(gridData);
316
317 // Histogram
318 fFullTraceHistogram = new FullTraceHistogram(this, fullRangeComposite);
319
2fc582d2
XR
320 fLegendArea = new Composite(viewComposite, SWT.FILL);
321 fLegendArea.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false, 2, 1));
322 fLegendArea.setLayout(new RowLayout());
323
65cdf787
PT
324 // Add mouse wheel listener to time span control
325 MouseWheelListener listener = fFullTraceHistogram.getZoom();
326 fTimeSpanControl.addMouseWheelListener(listener);
327
95aa81ef
JCK
328
329 // View Action Handling
330 contributeToActionBars();
331
3ac5721a
AM
332 ITmfTrace trace = getActiveTrace();
333 if (trace != null) {
334 traceSelected(new TmfTraceSelectedSignal(this, trace));
1b055dfa 335 }
ecfd1d41 336 }
c392540b
FC
337
338 @Override
c392540b 339 public void setFocus() {
faa38350 340 fFullTraceHistogram.fCanvas.setFocus();
833a21aa 341 }
c392540b 342
f8177ba2
FC
343 void refresh() {
344 fParent.layout(true);
345 }
346
c392540b
FC
347 // ------------------------------------------------------------------------
348 // Accessors
349 // ------------------------------------------------------------------------
350
faa38350
PT
351 /**
352 * Returns the current trace handled by the view
353 *
354 * @return the current trace
355 * @since 2.0
356 */
357 public ITmfTrace getTrace() {
358 return fTrace;
359 }
360
b544077e
BH
361 /**
362 * Returns the time range of the current selected window (base on default time scale).
abbdd66a 363 *
b544077e 364 * @return the time range of current selected window.
3bd46eef 365 * @since 2.0
b544077e 366 */
c392540b 367 public TmfTimeRange getTimeRange() {
f8177ba2
FC
368 return new TmfTimeRange(
369 new TmfTimestamp(fWindowStartTime, ITmfTimestamp.NANOSECOND_SCALE),
370 new TmfTimestamp(fWindowEndTime, ITmfTimestamp.NANOSECOND_SCALE));
c392540b
FC
371 }
372
95aa81ef
JCK
373 /**
374 * get the show lost events action
375 *
376 * @return The action object
46a59db7 377 * @since 2.2
95aa81ef
JCK
378 */
379 public Action getShowLostEventsAction() {
380 if (hideLostEventsAction == null) {
381 /* show lost events */
382 hideLostEventsAction = new Action(Messages.HistogramView_hideLostEvents, IAction.AS_CHECK_BOX) {
383 @Override
384 public void run() {
385 HistogramScaledData.hideLostEvents = hideLostEventsAction.isChecked();
386 long maxNbEvents = HistogramScaledData.hideLostEvents ? fFullTraceHistogram.fScaledData.fMaxValue : fFullTraceHistogram.fScaledData.fMaxCombinedValue;
3311a6ca 387 fFullTraceHistogram.setMaxNbEvents(maxNbEvents);
95aa81ef 388 maxNbEvents = HistogramScaledData.hideLostEvents ? fTimeRangeHistogram.fScaledData.fMaxValue : fTimeRangeHistogram.fScaledData.fMaxCombinedValue;
3311a6ca 389 fTimeRangeHistogram.setMaxNbEvents(maxNbEvents);
95aa81ef
JCK
390 }
391 };
392 hideLostEventsAction.setText(Messages.HistogramView_hideLostEvents);
393 hideLostEventsAction.setToolTipText(Messages.HistogramView_hideLostEvents);
394 hideLostEventsAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SHOW_LOST_EVENTS));
395 }
396 return hideLostEventsAction;
397 }
398
2fc582d2
XR
399 /**
400 * get the show trace action
401 *
402 * @return The action object
403 * @since 3.0
404 */
405 public Action getShowTraceAction() {
406 if (showTraceAction == null) {
407 /* show lost events */
408 showTraceAction = new Action(Messages.HistogramView_showTraces, IAction.AS_CHECK_BOX) {
409 @Override
410 public void run() {
411 Histogram.showTraces = showTraceAction.isChecked();
412 fFullTraceHistogram.fCanvas.redraw();
413 fTimeRangeHistogram.fCanvas.redraw();
414 updateLegendArea();
415 }
416 };
417 showTraceAction.setChecked(true);
418 showTraceAction.setText(Messages.HistogramView_showTraces);
419 showTraceAction.setToolTipText(Messages.HistogramView_showTraces);
420 showTraceAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SHOW_HIST_TRACES));
421 }
422 return showTraceAction;
423 }
424
c392540b
FC
425 // ------------------------------------------------------------------------
426 // Operations
427 // ------------------------------------------------------------------------
428
b544077e 429 /**
0fcf3b09
PT
430 * Broadcast TmfSignal about new current selection time range.
431 * @param beginTime the begin time of current selection.
432 * @param endTime the end time of current selection.
b544077e 433 */
0fcf3b09
PT
434 void updateSelectionTime(long beginTime, long endTime) {
435 updateDisplayedSelectionTime(beginTime, endTime);
436 TmfTimestamp beginTs = new TmfTimestamp(beginTime, ITmfTimestamp.NANOSECOND_SCALE);
437 TmfTimestamp endTs = new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE);
438 TmfTimeSynchSignal signal = new TmfTimeSynchSignal(this, beginTs, endTs);
439 fTimeSyncThrottle.queue(signal);
c392540b
FC
440 }
441
720d67cb
PT
442 /**
443 * Get selection begin time
444 * @return the begin time of current selection
445 */
446 long getSelectionBegin() {
447 return fSelectionBeginTime;
448 }
449
450 /**
451 * Get selection end time
452 * @return the end time of current selection
453 */
454 long getSelectionEnd() {
455 return fSelectionEndTime;
456 }
457
458 /**
459 * Get the link state
460 * @return true if begin and end selection time should be linked
461 */
462 boolean getLinkState() {
463 return fLinkState;
464 }
465
b544077e 466 /**
0fcf3b09 467 * Broadcast TmfSignal about new selection time range.
b544077e
BH
468 * @param startTime the new start time
469 * @param endTime the new end time
470 */
f8177ba2 471 void updateTimeRange(long startTime, long endTime) {
faa38350 472 if (fTrace != null) {
c392540b 473 // Build the new time range; keep the current time
f8177ba2
FC
474 TmfTimeRange timeRange = new TmfTimeRange(
475 new TmfTimestamp(startTime, ITmfTimestamp.NANOSECOND_SCALE),
476 new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE));
c392540b
FC
477 fTimeSpanControl.setValue(endTime - startTime);
478
38df2c82
AM
479 updateDisplayedTimeRange(startTime, endTime);
480
c392540b 481 // Send the FW signal
0fcf3b09 482 TmfRangeSynchSignal signal = new TmfRangeSynchSignal(this, timeRange);
38df2c82 483 fTimeRangeSyncThrottle.queue(signal);
c392540b
FC
484 }
485 }
486
b544077e
BH
487 /**
488 * Broadcast TmfSignal about new selected time range.
489 * @param newDuration new duration (relative to current start time)
490 */
c392540b 491 public synchronized void updateTimeRange(long newDuration) {
faa38350 492 if (fTrace != null) {
6a13fa07 493 long delta = newDuration - fWindowSpan;
1c6a842a 494 long newStartTime = fWindowStartTime - (delta / 2);
6a13fa07 495 setNewRange(newStartTime, newDuration);
c392540b
FC
496 }
497 }
498
499 private void setNewRange(long startTime, long duration) {
41b5c37f
AM
500 long realStart = startTime;
501
502 if (realStart < fTraceStartTime) {
503 realStart = fTraceStartTime;
1b055dfa 504 }
c392540b 505
41b5c37f 506 long endTime = realStart + duration;
faa38350
PT
507 if (endTime > fTraceEndTime) {
508 endTime = fTraceEndTime;
1c6a842a 509 if ((endTime - duration) > fTraceStartTime) {
41b5c37f 510 realStart = endTime - duration;
1b055dfa 511 } else {
41b5c37f 512 realStart = fTraceStartTime;
6a13fa07 513 }
c392540b 514 }
41b5c37f 515 updateTimeRange(realStart, endTime);
833a21aa 516 }
c392540b
FC
517
518 // ------------------------------------------------------------------------
519 // Signal handlers
520 // ------------------------------------------------------------------------
521
b544077e 522 /**
faa38350 523 * Handles trace opened signal. Loads histogram if new trace time range is not
b544077e 524 * equal <code>TmfTimeRange.NULL_RANGE</code>
fec1ac0b 525 * @param signal the trace opened signal
faa38350 526 * @since 2.0
b544077e 527 */
1406f802 528 @TmfSignalHandler
faa38350 529 public void traceOpened(TmfTraceOpenedSignal signal) {
c392540b 530 assert (signal != null);
faa38350
PT
531 fTrace = signal.getTrace();
532 loadTrace();
ecfd1d41 533 }
550d787e 534
faa38350
PT
535 /**
536 * Handles trace selected signal. Loads histogram if new trace time range is not
537 * equal <code>TmfTimeRange.NULL_RANGE</code>
538 * @param signal the trace selected signal
539 * @since 2.0
540 */
541 @TmfSignalHandler
542 public void traceSelected(TmfTraceSelectedSignal signal) {
543 assert (signal != null);
544 if (fTrace != signal.getTrace()) {
545 fTrace = signal.getTrace();
546 loadTrace();
547 }
548 }
549
550 private void loadTrace() {
c392540b
FC
551 initializeHistograms();
552 fParent.redraw();
550d787e
FC
553 }
554
ea279a69 555 /**
faa38350
PT
556 * Handles trace closed signal. Clears the view and data model and cancels requests.
557 * @param signal the trace closed signal
ea279a69
FC
558 * @since 2.0
559 */
560 @TmfSignalHandler
faa38350
PT
561 public void traceClosed(TmfTraceClosedSignal signal) {
562
563 if (signal.getTrace() != fTrace) {
564 return;
565 }
ea279a69
FC
566
567 // Kill any running request
568 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
569 fTimeRangeRequest.cancel();
570 }
571 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
572 fFullTraceRequest.cancel();
573 }
574
575 // Initialize the internal data
faa38350
PT
576 fTrace = null;
577 fTraceStartTime = 0L;
578 fTraceEndTime = 0L;
f8177ba2
FC
579 fWindowStartTime = 0L;
580 fWindowEndTime = 0L;
d7ee91bb 581 fWindowSpan = 0L;
0fcf3b09
PT
582 fSelectionBeginTime = 0L;
583 fSelectionEndTime = 0L;
ea279a69
FC
584
585 // Clear the UI widgets
586 fFullTraceHistogram.clear();
587 fTimeRangeHistogram.clear();
720d67cb
PT
588 fSelectionStartControl.setValue(Long.MIN_VALUE);
589 fSelectionEndControl.setValue(Long.MIN_VALUE);
f8177ba2 590
da7bdcbc 591 fTimeSpanControl.setValue(Long.MIN_VALUE);
2fc582d2
XR
592
593 for (Control c: fLegendArea.getChildren()) {
594 c.dispose();
595 }
03ec1f6a 596 disposeLegendImages();
2fc582d2
XR
597 fLegendArea.layout();
598 fLegendArea.getParent().layout();
ea279a69
FC
599 }
600
b544077e 601 /**
faa38350 602 * Handles trace range updated signal. Extends histogram according to the new time range. If a
b544077e
BH
603 * HistogramRequest is already ongoing, it will be cancelled and a new request with the new range
604 * will be issued.
abbdd66a 605 *
faa38350
PT
606 * @param signal the trace range updated signal
607 * @since 2.0
b544077e 608 */
74237cc3 609 @TmfSignalHandler
faa38350 610 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
74237cc3 611
faa38350 612 if (signal.getTrace() != fTrace) {
09d11238
PT
613 return;
614 }
615
74237cc3
FC
616 TmfTimeRange fullRange = signal.getRange();
617
faa38350
PT
618 fTraceStartTime = fullRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
619 fTraceEndTime = fullRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
74237cc3 620
faa38350
PT
621 fFullTraceHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
622 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
74237cc3 623
74237cc3
FC
624 sendFullRangeRequest(fullRange);
625 }
c392540b 626
b544077e 627 /**
faa38350
PT
628 * Handles the trace updated signal. Used to update time limits (start and end time)
629 * @param signal the trace updated signal
630 * @since 2.0
b544077e 631 */
a28d503d 632 @TmfSignalHandler
faa38350
PT
633 public void traceUpdated(TmfTraceUpdatedSignal signal) {
634 if (signal.getTrace() != fTrace) {
a28d503d
PT
635 return;
636 }
faa38350
PT
637 TmfTimeRange fullRange = signal.getTrace().getTimeRange();
638 fTraceStartTime = fullRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
639 fTraceEndTime = fullRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
a28d503d 640
faa38350
PT
641 fFullTraceHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
642 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
643
644 if ((fFullTraceRequest != null) && fFullTraceRequest.getRange().getEndTime().compareTo(signal.getRange().getEndTime()) < 0) {
645 sendFullRangeRequest(fullRange);
646 }
647}
a28d503d 648
b544077e
BH
649 /**
650 * Handles the current time updated signal. Sets the current time in the time range
651 * histogram as well as the full histogram.
abbdd66a 652 *
b544077e
BH
653 * @param signal the signal to process
654 */
c392540b 655 @TmfSignalHandler
80c930fa
PT
656 public void currentTimeUpdated(final TmfTimeSynchSignal signal) {
657 if (Display.getCurrent() == null) {
658 // Make sure the signal is handled in the UI thread
659 Display.getDefault().asyncExec(new Runnable() {
660 @Override
661 public void run() {
662 if (fParent.isDisposed()) {
663 return;
664 }
665 currentTimeUpdated(signal);
666 }
667 });
668 return;
669 }
c392540b 670
0fcf3b09
PT
671 // Update the selected time range
672 ITmfTimestamp beginTime = signal.getBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
673 ITmfTimestamp endTime = signal.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
674 updateDisplayedSelectionTime(beginTime.getValue(), endTime.getValue());
ecfd1d41 675 }
f05aabed 676
b544077e
BH
677 /**
678 * Updates the current time range in the time range histogram and full range histogram.
679 * @param signal the signal to process
680 */
c392540b 681 @TmfSignalHandler
80c930fa
PT
682 public void timeRangeUpdated(final TmfRangeSynchSignal signal) {
683 if (Display.getCurrent() == null) {
684 // Make sure the signal is handled in the UI thread
685 Display.getDefault().asyncExec(new Runnable() {
686 @Override
687 public void run() {
688 if (fParent.isDisposed()) {
689 return;
690 }
691 timeRangeUpdated(signal);
692 }
693 });
694 return;
695 }
c392540b 696
faa38350 697 if (fTrace != null) {
1c6a842a
PT
698 // Validate the time range
699 TmfTimeRange range = signal.getCurrentRange().getIntersection(fTrace.getTimeRange());
700 if (range == null) {
701 return;
702 }
703
38df2c82
AM
704 updateDisplayedTimeRange(
705 range.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(),
706 range.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue());
c392540b 707
38df2c82 708 // Send the event request to populate the small histogram
c392540b 709 sendTimeRangeRequest(fWindowStartTime, fWindowEndTime);
f8177ba2 710
c392540b
FC
711 fTimeSpanControl.setValue(fWindowSpan);
712 }
b59134e1 713 }
c392540b
FC
714
715 // ------------------------------------------------------------------------
716 // Helper functions
717 // ------------------------------------------------------------------------
718
719 private void initializeHistograms() {
faa38350 720 TmfTimeRange fullRange = updateTraceTimeRange();
0fcf3b09
PT
721 long selectionBeginTime = fTraceManager.getSelectionBeginTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
722 long selectionEndTime = fTraceManager.getSelectionEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
248af329
AM
723 long startTime = fTraceManager.getCurrentRange().getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
724 long duration = fTraceManager.getCurrentRange().getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue() - startTime;
74237cc3 725
faa38350
PT
726 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
727 fTimeRangeRequest.cancel();
728 }
74237cc3 729 fTimeRangeHistogram.clear();
faa38350 730 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
d7ee91bb 731 fTimeRangeHistogram.setTimeRange(startTime, duration);
0fcf3b09 732 fTimeRangeHistogram.setSelection(selectionBeginTime, selectionEndTime);
2fc582d2 733 fTimeRangeHistogram.fDataModel.setTrace(fTrace);
c392540b 734
faa38350
PT
735 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
736 fFullTraceRequest.cancel();
737 }
74237cc3 738 fFullTraceHistogram.clear();
faa38350 739 fFullTraceHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
d7ee91bb 740 fFullTraceHistogram.setTimeRange(startTime, duration);
0fcf3b09 741 fFullTraceHistogram.setSelection(selectionBeginTime, selectionEndTime);
2fc582d2 742 fFullTraceHistogram.fDataModel.setTrace(fTrace);
c392540b 743
d7ee91bb
PT
744 fWindowStartTime = startTime;
745 fWindowSpan = duration;
746 fWindowEndTime = startTime + duration;
6a13fa07 747
0fcf3b09
PT
748 fSelectionBeginTime = selectionBeginTime;
749 fSelectionEndTime = selectionEndTime;
720d67cb
PT
750 fSelectionStartControl.setValue(fSelectionBeginTime);
751 fSelectionEndControl.setValue(fSelectionEndTime);
f8177ba2 752
d7ee91bb 753 fTimeSpanControl.setValue(duration);
6a13fa07 754
c14c0757
GB
755 Collection<ITmfTrace> traces = TmfTraceManager.getTraceSet(fTrace);
756 if (!traces.isEmpty()) {
757 this.showTraceAction.setEnabled(traces.size() < fFullTraceHistogram.getMaxNbTraces());
2fc582d2
XR
758 }
759 updateLegendArea();
760
2af7df97 761 if (!fullRange.equals(TmfTimeRange.NULL_RANGE)) {
d7ee91bb 762 sendTimeRangeRequest(startTime, startTime + duration);
2af7df97
FC
763 sendFullRangeRequest(fullRange);
764 }
74237cc3
FC
765 }
766
2fc582d2
XR
767 private void updateLegendArea() {
768 for (Control c: fLegendArea.getChildren()) {
769 c.dispose();
770 }
03ec1f6a 771 disposeLegendImages();
2fc582d2 772 if (fFullTraceHistogram.showTraces()) {
c14c0757
GB
773 Collection<ITmfTrace> traces = TmfTraceManager.getTraceSet(fTrace);
774 fLegendImages = new Image[traces.size()];
2fc582d2
XR
775 int traceIndex = 0;
776 for (ITmfTrace trace : traces) {
777 fLegendImages[traceIndex] = new Image(fLegendArea.getDisplay(), 16, 16);
778 GC gc = new GC(fLegendImages[traceIndex]);
779 gc.setBackground(fFullTraceHistogram.getTraceColor(traceIndex));
780 gc.fillRectangle(0, 0, 15, 15);
781 gc.setForeground(fLegendArea.getDisplay().getSystemColor(SWT.COLOR_BLACK));
782 gc.drawRectangle(0, 0, 15, 15);
783 gc.dispose();
784
785 CLabel label = new CLabel(fLegendArea, SWT.NONE);
786 label.setText(trace.getName());
787 label.setImage(fLegendImages[traceIndex]);
788 traceIndex++;
789 }
790 }
791 fLegendArea.layout();
792 fLegendArea.getParent().layout();
793 }
794
0fcf3b09
PT
795 private void updateDisplayedSelectionTime(long beginTime, long endTime) {
796 fSelectionBeginTime = beginTime;
797 fSelectionEndTime = endTime;
38df2c82 798
0fcf3b09
PT
799 fFullTraceHistogram.setSelection(fSelectionBeginTime, fSelectionEndTime);
800 fTimeRangeHistogram.setSelection(fSelectionBeginTime, fSelectionEndTime);
720d67cb
PT
801 fSelectionStartControl.setValue(fSelectionBeginTime);
802 fSelectionEndControl.setValue(fSelectionEndTime);
38df2c82
AM
803 }
804
805 private void updateDisplayedTimeRange(long start, long end) {
806 fWindowStartTime = start;
807 fWindowEndTime = end;
808 fWindowSpan = fWindowEndTime - fWindowStartTime;
809 fFullTraceHistogram.setTimeRange(fWindowStartTime, fWindowSpan);
810 }
811
faa38350
PT
812 private TmfTimeRange updateTraceTimeRange() {
813 fTraceStartTime = 0L;
814 fTraceEndTime = 0L;
74237cc3 815
faa38350 816 TmfTimeRange timeRange = fTrace.getTimeRange();
c6023803 817 if (!timeRange.equals(TmfTimeRange.NULL_RANGE)) {
faa38350
PT
818 fTraceStartTime = timeRange.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
819 fTraceEndTime = timeRange.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
74237cc3
FC
820 }
821 return timeRange;
b59134e1 822 }
c392540b
FC
823
824 private void sendTimeRangeRequest(long startTime, long endTime) {
1b055dfa 825 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
c392540b 826 fTimeRangeRequest.cancel();
088c1d4e 827 }
f8177ba2
FC
828 TmfTimestamp startTS = new TmfTimestamp(startTime, ITmfTimestamp.NANOSECOND_SCALE);
829 TmfTimestamp endTS = new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE);
c392540b
FC
830 TmfTimeRange timeRange = new TmfTimeRange(startTS, endTS);
831
832 fTimeRangeHistogram.clear();
15844a4e 833 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
c392540b 834 fTimeRangeHistogram.setTimeRange(startTime, endTime - startTime);
4cf201de 835
faa38350 836 int cacheSize = fTrace.getCacheSize();
2740e05c
AM
837 fTimeRangeRequest = new HistogramRequest(fTimeRangeHistogram.getDataModel(),
838 timeRange, 0, ITmfEventRequest.ALL_DATA, cacheSize, ExecutionType.FOREGROUND, false);
faa38350 839 fTrace.sendRequest(fTimeRangeRequest);
088c1d4e 840 }
c392540b 841
74237cc3 842 private void sendFullRangeRequest(TmfTimeRange fullRange) {
1b055dfa 843 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
c392540b
FC
844 fFullTraceRequest.cancel();
845 }
faa38350 846 int cacheSize = fTrace.getCacheSize();
2740e05c
AM
847 fFullTraceRequest = new HistogramRequest(fFullTraceHistogram.getDataModel(),
848 fullRange,
849 (int) fFullTraceHistogram.fDataModel.getNbEvents(),
850 ITmfEventRequest.ALL_DATA,
851 cacheSize,
852 ExecutionType.BACKGROUND, true);
faa38350 853 fTrace.sendRequest(fFullTraceRequest);
ed4b3b9f 854 }
c392540b 855
95aa81ef
JCK
856 private void contributeToActionBars() {
857 IActionBars bars = getViewSite().getActionBars();
858 bars.getToolBarManager().add(getShowLostEventsAction());
2fc582d2 859 bars.getToolBarManager().add(getShowTraceAction());
95aa81ef
JCK
860 bars.getToolBarManager().add(new Separator());
861 }
862
720d67cb
PT
863 private void addLinkButtonListeners() {
864 fLinkButton.addMouseListener(new MouseAdapter() {
865 @Override
866 public void mouseDown(MouseEvent e) {
867 fSelectionEndControl.setEnabled(fLinkState);
868 fLinkState = !fLinkState;
869 fLinkButton.redraw();
870 }
871 });
872
873 fLinkButton.addPaintListener(new PaintListener() {
874 @Override
875 public void paintControl(PaintEvent e) {
876 if (fLinkState) {
877 Rectangle r = fLinkButton.getBounds();
878 r.x = -1;
879 r.y = -1;
880 e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
881 e.gc.drawRectangle(r);
882 r.x = 0;
883 r.y = 0;
884 e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_DARK_GRAY));
885 e.gc.drawRectangle(r);
886 }
887 }
888 });
889 }
6e512b93 890}
This page took 0.13566 seconds and 5 git commands to generate.