tmf.all: use ITmfTimestamp#toNanos when possible
[deliverable/tracecompass.git] / tmf / 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;
f7d9ea05
BH
30import org.eclipse.swt.custom.SashForm;
31import org.eclipse.swt.custom.ScrolledComposite;
720d67cb
PT
32import org.eclipse.swt.events.MouseAdapter;
33import org.eclipse.swt.events.MouseEvent;
65cdf787 34import org.eclipse.swt.events.MouseWheelListener;
720d67cb
PT
35import org.eclipse.swt.events.PaintEvent;
36import org.eclipse.swt.events.PaintListener;
2fc582d2 37import org.eclipse.swt.graphics.GC;
720d67cb 38import org.eclipse.swt.graphics.Image;
f7d9ea05 39import org.eclipse.swt.graphics.Point;
720d67cb 40import org.eclipse.swt.graphics.Rectangle;
b59134e1 41import org.eclipse.swt.layout.GridData;
252ae4bd 42import org.eclipse.swt.layout.GridLayout;
2fc582d2 43import org.eclipse.swt.layout.RowLayout;
6e512b93 44import org.eclipse.swt.widgets.Composite;
2fc582d2 45import org.eclipse.swt.widgets.Control;
80c930fa 46import org.eclipse.swt.widgets.Display;
e8c79054 47import org.eclipse.swt.widgets.Event;
720d67cb 48import org.eclipse.swt.widgets.Label;
e8c79054
BH
49import org.eclipse.swt.widgets.Listener;
50import org.eclipse.swt.widgets.Sash;
2bdf0193
AM
51import org.eclipse.tracecompass.internal.tmf.ui.Activator;
52import org.eclipse.tracecompass.internal.tmf.ui.ITmfImageConstants;
53import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
54import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType;
e8c79054 55import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
2bdf0193 56import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
e8c79054 57import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
2bdf0193 58import org.eclipse.tracecompass.tmf.core.signal.TmfSignalThrottler;
2bdf0193
AM
59import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
60import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
61import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
62import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
63import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
e8c79054 64import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
2bdf0193
AM
65import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
66import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
67import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
68import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
21852dfa 69import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext;
2bdf0193 70import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
e8c79054
BH
71import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentInfo;
72import org.eclipse.tracecompass.tmf.ui.signal.TmfTimeViewAlignmentSignal;
73import org.eclipse.tracecompass.tmf.ui.views.ITmfTimeAligned;
2bdf0193 74import org.eclipse.tracecompass.tmf.ui.views.TmfView;
95aa81ef 75import org.eclipse.ui.IActionBars;
6e512b93 76
544fe9b7 77/**
faa38350 78 * The purpose of this view is to provide graphical time distribution statistics about the trace events.
544fe9b7 79 * <p>
c392540b
FC
80 * The view is composed of two histograms and two controls:
81 * <ul>
faa38350 82 * <li>an event distribution histogram for the whole trace;
c392540b
FC
83 * <li>an event distribution histogram for current time window (window span);
84 * <li>the timestamp of the currently selected event;
85 * <li>the window span (size of the time window of the smaller histogram).
86 * </ul>
87 * The histograms x-axis show their respective time range.
abbdd66a 88 *
f8177ba2 89 * @version 2.0
2af7df97 90 * @author Francois Chouinard
544fe9b7 91 */
e8c79054 92public class HistogramView extends TmfView implements ITmfTimeAligned {
c392540b
FC
93
94 // ------------------------------------------------------------------------
95 // Constants
96 // ------------------------------------------------------------------------
97
b544077e
BH
98 /**
99 * The view ID as defined in plugin.xml
100 */
dab5f596 101 public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.ui.views.histogram"; //$NON-NLS-1$
c392540b 102
d2e4afa7 103 private static final Image LINK_IMG = Activator.getDefault().getImageFromPath(ITmfImageConstants.IMG_UI_LINK);
720d67cb 104
f7d9ea05
BH
105 private static final int[] DEFAULT_WEIGHTS = {1, 3};
106
c392540b
FC
107 // ------------------------------------------------------------------------
108 // Attributes
109 // ------------------------------------------------------------------------
110
faa38350
PT
111 // The current trace
112 private ITmfTrace fTrace;
c392540b 113
f8177ba2 114 // Current timestamp/time window - everything in the TIME_SCALE
faa38350
PT
115 private long fTraceStartTime;
116 private long fTraceEndTime;
c392540b
FC
117 private long fWindowStartTime;
118 private long fWindowEndTime;
d7ee91bb 119 private long fWindowSpan;
0fcf3b09
PT
120 private long fSelectionBeginTime;
121 private long fSelectionEndTime;
c392540b 122
e8c79054
BH
123 // SashForm
124 private SashForm fSashForm;
f7d9ea05
BH
125 private ScrolledComposite fScrollComposite;
126 private Composite fTimeControlsComposite;
e8c79054
BH
127 private Composite fTimeRangeComposite;
128 private Listener fSashDragListener;
f7d9ea05 129
c392540b 130 // Time controls
720d67cb
PT
131 private HistogramTextControl fSelectionStartControl;
132 private HistogramTextControl fSelectionEndControl;
c392540b
FC
133 private HistogramTextControl fTimeSpanControl;
134
720d67cb
PT
135 // Link
136 private Label fLinkButton;
137 private boolean fLinkState;
138
c392540b
FC
139 // Histogram/request for the full trace range
140 private static FullTraceHistogram fFullTraceHistogram;
141 private HistogramRequest fFullTraceRequest;
142
143 // Histogram/request for the selected time range
144 private static TimeRangeHistogram fTimeRangeHistogram;
145 private HistogramRequest fTimeRangeRequest;
146
2fc582d2
XR
147 // Legend area
148 private Composite fLegendArea;
149 private Image[] fLegendImages;
150
38df2c82
AM
151 // Throttlers for the time sync and time-range sync signals
152 private final TmfSignalThrottler fTimeSyncThrottle;
153 private final TmfSignalThrottler fTimeRangeSyncThrottle;
154
95aa81ef
JCK
155 // Action for toggle showing the lost events
156 private Action hideLostEventsAction;
2fc582d2
XR
157 // Action for toggle showing the traces
158 private Action showTraceAction;
95aa81ef 159
c392540b
FC
160 // ------------------------------------------------------------------------
161 // Constructor
162 // ------------------------------------------------------------------------
163
b544077e
BH
164 /**
165 * Default constructor
166 */
c392540b
FC
167 public HistogramView() {
168 super(ID);
38df2c82
AM
169 fTimeSyncThrottle = new TmfSignalThrottler(this, 200);
170 fTimeRangeSyncThrottle = new TmfSignalThrottler(this, 200);
c392540b
FC
171 }
172
6a13fa07 173 @Override
c392540b 174 public void dispose() {
1b055dfa 175 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
4dc47e28
FC
176 fTimeRangeRequest.cancel();
177 }
1b055dfa 178 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
4dc47e28
FC
179 fFullTraceRequest.cancel();
180 }
6a13fa07
FC
181 fFullTraceHistogram.dispose();
182 fTimeRangeHistogram.dispose();
720d67cb
PT
183 fSelectionStartControl.dispose();
184 fSelectionEndControl.dispose();
f8177ba2 185 fTimeSpanControl.dispose();
03ec1f6a
MAL
186 disposeLegendImages();
187
4dc47e28 188 super.dispose();
c392540b
FC
189 }
190
03ec1f6a
MAL
191 private void disposeLegendImages() {
192 if (fLegendImages != null) {
193 for (Image i: fLegendImages) {
194 i.dispose();
195 }
196 }
197 fLegendImages = null;
198 }
199
c392540b
FC
200 // ------------------------------------------------------------------------
201 // TmfView
202 // ------------------------------------------------------------------------
203
204 @Override
c392540b 205 public void createPartControl(Composite parent) {
e8c79054 206 super.createPartControl(parent);
c392540b
FC
207
208 // Control labels
720d67cb
PT
209 final String selectionStartLabel = Messages.HistogramView_selectionStartLabel;
210 final String selectionEndLabel = Messages.HistogramView_selectionEndLabel;
c392540b
FC
211 final String windowSpanLabel = Messages.HistogramView_windowSpanLabel;
212
213 // --------------------------------------------------------------------
214 // Set the HistogramView layout
215 // --------------------------------------------------------------------
e8c79054 216 Composite viewComposite = new Composite(getParentComposite(), SWT.FILL);
f7d9ea05 217 GridLayout gridLayout = new GridLayout(1, false);
c392540b
FC
218 gridLayout.verticalSpacing = 0;
219 gridLayout.marginHeight = 0;
220 gridLayout.marginWidth = 0;
221 viewComposite.setLayout(gridLayout);
222
f7d9ea05
BH
223 // --------------------------------------------------------------------
224 // Add a sash for time controls and time range histogram
225 // --------------------------------------------------------------------
e8c79054
BH
226
227 /*
228 * The ScrolledComposite preferred size can be larger than its visible
229 * width. This affects the preferred width of the SashForm. Set the
230 * preferred width to 1 to prevent it from affecting the preferred width
231 * of the view composite.
232 */
233 fSashForm = new SashForm(viewComposite, SWT.NONE) {
234 @Override
235 public Point computeSize(int wHint, int hHint) {
236 Point computedSize = super.computeSize(wHint, hHint);
237 if (wHint == SWT.DEFAULT) {
238 return new Point(1, computedSize.y);
239 }
240 return computedSize;
241 }
242 @Override
243 public Point computeSize(int wHint, int hHint, boolean changed) {
244 Point computedSize = super.computeSize(wHint, hHint, changed);
245 if (wHint == SWT.DEFAULT) {
246 return new Point(1, computedSize.y);
247 }
248 return computedSize;
249 }
250 };
f7d9ea05 251 GridData gridData = new GridData(GridData.FILL, GridData.FILL, false, true);
e8c79054 252 fSashForm.setLayoutData(gridData);
f7d9ea05 253
c392540b
FC
254 // --------------------------------------------------------------------
255 // Time controls
256 // --------------------------------------------------------------------
e8c79054 257 fScrollComposite = new PackedScrolledComposite(fSashForm, SWT.H_SCROLL | SWT.V_SCROLL);
f7d9ea05
BH
258 fTimeControlsComposite = new Composite(fScrollComposite, SWT.NONE);
259 fScrollComposite.setContent(fTimeControlsComposite);
260 gridLayout = new GridLayout(1, false);
261 gridLayout.marginHeight = 0;
262 gridLayout.marginWidth = 0;
263 fScrollComposite.setLayout(gridLayout);
264 fScrollComposite.setExpandHorizontal(true);
265 fScrollComposite.setExpandVertical(true);
c392540b 266
f7d9ea05
BH
267 gridLayout = new GridLayout(1, false);
268 gridLayout.marginHeight = 0;
269 gridLayout.marginWidth = 0;
270 fTimeControlsComposite.setLayout(gridLayout);
271 gridData = new GridData(GridData.FILL, GridData.CENTER, false, true);
272 fTimeControlsComposite.setLayoutData(gridData);
273
274 Composite innerComp = new Composite(fTimeControlsComposite, SWT.NONE);
275
276 gridLayout = new GridLayout(2, false);
277 innerComp.setLayout(gridLayout);
c392540b
FC
278 gridLayout.marginHeight = 0;
279 gridLayout.marginWidth = 0;
280 gridLayout.horizontalSpacing = 5;
720d67cb 281 gridLayout.verticalSpacing = 1;
f7d9ea05
BH
282 gridData = new GridData(GridData.FILL, GridData.CENTER, false, true);
283 innerComp.setLayoutData(gridData);
720d67cb 284
f7d9ea05
BH
285 Composite selectionGroup = new Composite(innerComp, SWT.BORDER);
286 gridLayout = new GridLayout(1, false);
720d67cb
PT
287 gridLayout.marginHeight = 0;
288 gridLayout.marginWidth = 0;
720d67cb 289 selectionGroup.setLayout(gridLayout);
f7d9ea05
BH
290 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
291 selectionGroup.setLayoutData(gridData);
c392540b 292
720d67cb 293 // Selection start control
f7d9ea05 294 gridData = new GridData(GridData.FILL, GridData.CENTER, false, false);
720d67cb
PT
295 fSelectionStartControl = new HistogramSelectionStartControl(this, selectionGroup, selectionStartLabel, 0L);
296 fSelectionStartControl.setLayoutData(gridData);
297 fSelectionStartControl.setValue(Long.MIN_VALUE);
298
299 // Selection end control
f7d9ea05 300 gridData = new GridData(GridData.FILL, GridData.CENTER, false, false);
720d67cb
PT
301 fSelectionEndControl = new HistogramSelectionEndControl(this, selectionGroup, selectionEndLabel, 0L);
302 fSelectionEndControl.setLayoutData(gridData);
303 fSelectionEndControl.setValue(Long.MIN_VALUE);
304
305 // Link button
f7d9ea05
BH
306 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
307 fLinkButton = new Label(innerComp, SWT.NONE);
720d67cb
PT
308 fLinkButton.setImage(LINK_IMG);
309 fLinkButton.setLayoutData(gridData);
310 addLinkButtonListeners();
c392540b
FC
311
312 // Window span time control
f7d9ea05
BH
313 gridData = new GridData(GridData.FILL, GridData.CENTER, false, false);
314 fTimeSpanControl = new HistogramTimeRangeControl(this, innerComp, windowSpanLabel, 0L);
c392540b 315 fTimeSpanControl.setLayoutData(gridData);
da7bdcbc 316 fTimeSpanControl.setValue(Long.MIN_VALUE);
c392540b
FC
317
318 // --------------------------------------------------------------------
319 // Time range histogram
320 // --------------------------------------------------------------------
e8c79054 321 fTimeRangeComposite = new Composite(fSashForm, SWT.NONE);
f7d9ea05
BH
322 gridLayout = new GridLayout(1, true);
323 gridLayout.marginTop = 0;
e8c79054
BH
324 gridLayout.marginWidth = 0;
325 fTimeRangeComposite.setLayout(gridLayout);
c392540b
FC
326
327 // Use remaining horizontal space
f7d9ea05 328 gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
e8c79054 329 fTimeRangeComposite.setLayoutData(gridData);
c392540b
FC
330
331 // Histogram
e8c79054 332 fTimeRangeHistogram = new TimeRangeHistogram(this, fTimeRangeComposite, true);
c392540b
FC
333
334 // --------------------------------------------------------------------
335 // Full range histogram
336 // --------------------------------------------------------------------
f7d9ea05
BH
337 final Composite fullRangeComposite = new Composite(viewComposite, SWT.FILL);
338 gridLayout = new GridLayout(1, true);
c392540b
FC
339 fullRangeComposite.setLayout(gridLayout);
340
341 // Use remaining horizontal space
f7d9ea05 342 gridData = new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1);
c392540b
FC
343 fullRangeComposite.setLayoutData(gridData);
344
345 // Histogram
346 fFullTraceHistogram = new FullTraceHistogram(this, fullRangeComposite);
347
2fc582d2
XR
348 fLegendArea = new Composite(viewComposite, SWT.FILL);
349 fLegendArea.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false, 2, 1));
350 fLegendArea.setLayout(new RowLayout());
351
65cdf787
PT
352 // Add mouse wheel listener to time span control
353 MouseWheelListener listener = fFullTraceHistogram.getZoom();
354 fTimeSpanControl.addMouseWheelListener(listener);
355
95aa81ef
JCK
356 // View Action Handling
357 contributeToActionBars();
358
21852dfa 359 ITmfTrace trace = TmfTraceManager.getInstance().getActiveTrace();
3ac5721a
AM
360 if (trace != null) {
361 traceSelected(new TmfTraceSelectedSignal(this, trace));
1b055dfa 362 }
f7d9ea05 363
e8c79054
BH
364 fSashForm.setVisible(true);
365 fSashForm.setWeights(DEFAULT_WEIGHTS);
366
367 fTimeControlsComposite.addPaintListener(new PaintListener() {
368 @Override
369 public void paintControl(PaintEvent e) {
370 // Sashes in a SashForm are being created on layout so add the
371 // drag listener here
372 if (fSashDragListener == null) {
373 for (Control control : fSashForm.getChildren()) {
374 if (control instanceof Sash) {
375 fSashDragListener = new Listener() {
376 @Override
377 public void handleEvent(Event event) {
378 TmfSignalManager.dispatchSignal(new TmfTimeViewAlignmentSignal(fSashForm, getTimeViewAlignmentInfo()));
379 }
380 };
381 control.removePaintListener(this);
382 control.addListener(SWT.Selection, fSashDragListener);
383 // There should be only one sash
384 break;
385 }
386 }
387 }
388 }
389 });
ecfd1d41 390 }
c392540b
FC
391
392 @Override
c392540b 393 public void setFocus() {
faa38350 394 fFullTraceHistogram.fCanvas.setFocus();
833a21aa 395 }
c392540b 396
f8177ba2 397 void refresh() {
e8c79054
BH
398 getParentComposite().layout(true);
399 }
400
401 /**
402 * @since 1.0
403 */
404 @Override
405 public TmfTimeViewAlignmentInfo getTimeViewAlignmentInfo() {
406 if (fSashForm == null) {
407 return null;
408 }
409 return new TmfTimeViewAlignmentInfo(fSashForm.getShell(), fSashForm.toDisplay(0, 0), getTimeAxisOffset());
410 }
411
412 private int getTimeAxisOffset() {
2e23a015 413 return fScrollComposite.getSize().x + fSashForm.getSashWidth() + fTimeRangeHistogram.getPointAreaOffset();
e8c79054
BH
414 }
415
416 /**
417 * @since 1.0
418 */
419 @Override
420 public int getAvailableWidth(int requestedOffset) {
421 int pointAreaWidth = fTimeRangeHistogram.getPointAreaWidth();
422 int curTimeAxisOffset = getTimeAxisOffset();
423 if (pointAreaWidth <= 0) {
424 pointAreaWidth = fSashForm.getBounds().width - curTimeAxisOffset;
425 }
e8c79054
BH
426 int endOffset = curTimeAxisOffset + pointAreaWidth;
427 GridLayout layout = (GridLayout) fTimeRangeComposite.getLayout();
428 int endOffsetWithoutMargin = endOffset + layout.marginRight;
429 int availableWidth = endOffsetWithoutMargin - requestedOffset;
430 availableWidth = Math.min(fSashForm.getBounds().width, Math.max(0, availableWidth));
431
432 return availableWidth;
433 }
434
435 /**
436 * @since 1.0
437 */
438 @Override
439 public void performAlign(int offset, int width) {
e8c79054 440 int total = fSashForm.getBounds().width;
c34ab48a
PT
441 int plotAreaOffset = fTimeRangeHistogram.getPointAreaOffset();
442 int width1 = Math.max(0, offset - plotAreaOffset - fSashForm.getSashWidth());
443 int width2 = Math.max(0, total - width1 - fSashForm.getSashWidth());
40f88571
MAL
444 if (width1 >= 0 && width2 > 0 || width1 > 0 && width2 >= 0) {
445 fSashForm.setWeights(new int[] { width1, width2 });
446 fSashForm.layout();
447 }
e8c79054
BH
448
449 // calculate right margin
450 GridLayout layout = (GridLayout) fTimeRangeComposite.getLayout();
451 int timeBasedControlsWidth = fTimeRangeComposite.getSize().x;
452 int marginSize = timeBasedControlsWidth - width - plotAreaOffset;
453 layout.marginRight = Math.max(0, marginSize);
454 fTimeRangeComposite.layout();
f8177ba2
FC
455 }
456
c392540b
FC
457 // ------------------------------------------------------------------------
458 // Accessors
459 // ------------------------------------------------------------------------
460
faa38350
PT
461 /**
462 * Returns the current trace handled by the view
463 *
464 * @return the current trace
faa38350
PT
465 */
466 public ITmfTrace getTrace() {
467 return fTrace;
468 }
469
b544077e
BH
470 /**
471 * Returns the time range of the current selected window (base on default time scale).
abbdd66a 472 *
b544077e
BH
473 * @return the time range of current selected window.
474 */
c392540b 475 public TmfTimeRange getTimeRange() {
f8177ba2
FC
476 return new TmfTimeRange(
477 new TmfTimestamp(fWindowStartTime, ITmfTimestamp.NANOSECOND_SCALE),
478 new TmfTimestamp(fWindowEndTime, ITmfTimestamp.NANOSECOND_SCALE));
c392540b
FC
479 }
480
95aa81ef
JCK
481 /**
482 * get the show lost events action
483 *
484 * @return The action object
95aa81ef
JCK
485 */
486 public Action getShowLostEventsAction() {
487 if (hideLostEventsAction == null) {
488 /* show lost events */
489 hideLostEventsAction = new Action(Messages.HistogramView_hideLostEvents, IAction.AS_CHECK_BOX) {
490 @Override
491 public void run() {
492 HistogramScaledData.hideLostEvents = hideLostEventsAction.isChecked();
493 long maxNbEvents = HistogramScaledData.hideLostEvents ? fFullTraceHistogram.fScaledData.fMaxValue : fFullTraceHistogram.fScaledData.fMaxCombinedValue;
3311a6ca 494 fFullTraceHistogram.setMaxNbEvents(maxNbEvents);
95aa81ef 495 maxNbEvents = HistogramScaledData.hideLostEvents ? fTimeRangeHistogram.fScaledData.fMaxValue : fTimeRangeHistogram.fScaledData.fMaxCombinedValue;
3311a6ca 496 fTimeRangeHistogram.setMaxNbEvents(maxNbEvents);
95aa81ef
JCK
497 }
498 };
499 hideLostEventsAction.setText(Messages.HistogramView_hideLostEvents);
500 hideLostEventsAction.setToolTipText(Messages.HistogramView_hideLostEvents);
501 hideLostEventsAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SHOW_LOST_EVENTS));
502 }
503 return hideLostEventsAction;
504 }
505
2fc582d2
XR
506 /**
507 * get the show trace action
508 *
509 * @return The action object
2fc582d2
XR
510 */
511 public Action getShowTraceAction() {
512 if (showTraceAction == null) {
513 /* show lost events */
514 showTraceAction = new Action(Messages.HistogramView_showTraces, IAction.AS_CHECK_BOX) {
515 @Override
516 public void run() {
517 Histogram.showTraces = showTraceAction.isChecked();
518 fFullTraceHistogram.fCanvas.redraw();
519 fTimeRangeHistogram.fCanvas.redraw();
520 updateLegendArea();
521 }
522 };
523 showTraceAction.setChecked(true);
524 showTraceAction.setText(Messages.HistogramView_showTraces);
525 showTraceAction.setToolTipText(Messages.HistogramView_showTraces);
526 showTraceAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(ITmfImageConstants.IMG_UI_SHOW_HIST_TRACES));
527 }
528 return showTraceAction;
529 }
530
c392540b
FC
531 // ------------------------------------------------------------------------
532 // Operations
533 // ------------------------------------------------------------------------
534
b544077e 535 /**
0fcf3b09
PT
536 * Broadcast TmfSignal about new current selection time range.
537 * @param beginTime the begin time of current selection.
538 * @param endTime the end time of current selection.
b544077e 539 */
0fcf3b09
PT
540 void updateSelectionTime(long beginTime, long endTime) {
541 updateDisplayedSelectionTime(beginTime, endTime);
542 TmfTimestamp beginTs = new TmfTimestamp(beginTime, ITmfTimestamp.NANOSECOND_SCALE);
543 TmfTimestamp endTs = new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE);
97c71024 544 TmfSelectionRangeUpdatedSignal signal = new TmfSelectionRangeUpdatedSignal(this, beginTs, endTs);
0fcf3b09 545 fTimeSyncThrottle.queue(signal);
c392540b
FC
546 }
547
720d67cb
PT
548 /**
549 * Get selection begin time
550 * @return the begin time of current selection
551 */
552 long getSelectionBegin() {
553 return fSelectionBeginTime;
554 }
555
556 /**
557 * Get selection end time
558 * @return the end time of current selection
559 */
560 long getSelectionEnd() {
561 return fSelectionEndTime;
562 }
563
564 /**
565 * Get the link state
566 * @return true if begin and end selection time should be linked
567 */
568 boolean getLinkState() {
569 return fLinkState;
570 }
571
b544077e 572 /**
0fcf3b09 573 * Broadcast TmfSignal about new selection time range.
b544077e
BH
574 * @param startTime the new start time
575 * @param endTime the new end time
576 */
f8177ba2 577 void updateTimeRange(long startTime, long endTime) {
faa38350 578 if (fTrace != null) {
c392540b 579 // Build the new time range; keep the current time
f8177ba2
FC
580 TmfTimeRange timeRange = new TmfTimeRange(
581 new TmfTimestamp(startTime, ITmfTimestamp.NANOSECOND_SCALE),
582 new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE));
c392540b
FC
583 fTimeSpanControl.setValue(endTime - startTime);
584
38df2c82
AM
585 updateDisplayedTimeRange(startTime, endTime);
586
c392540b 587 // Send the FW signal
97c71024 588 TmfWindowRangeUpdatedSignal signal = new TmfWindowRangeUpdatedSignal(this, timeRange);
38df2c82 589 fTimeRangeSyncThrottle.queue(signal);
c392540b
FC
590 }
591 }
592
b544077e
BH
593 /**
594 * Broadcast TmfSignal about new selected time range.
595 * @param newDuration new duration (relative to current start time)
596 */
c392540b 597 public synchronized void updateTimeRange(long newDuration) {
faa38350 598 if (fTrace != null) {
6a13fa07 599 long delta = newDuration - fWindowSpan;
1c6a842a 600 long newStartTime = fWindowStartTime - (delta / 2);
6a13fa07 601 setNewRange(newStartTime, newDuration);
c392540b
FC
602 }
603 }
604
605 private void setNewRange(long startTime, long duration) {
41b5c37f
AM
606 long realStart = startTime;
607
608 if (realStart < fTraceStartTime) {
609 realStart = fTraceStartTime;
1b055dfa 610 }
c392540b 611
41b5c37f 612 long endTime = realStart + duration;
faa38350
PT
613 if (endTime > fTraceEndTime) {
614 endTime = fTraceEndTime;
1c6a842a 615 if ((endTime - duration) > fTraceStartTime) {
41b5c37f 616 realStart = endTime - duration;
1b055dfa 617 } else {
41b5c37f 618 realStart = fTraceStartTime;
6a13fa07 619 }
c392540b 620 }
41b5c37f 621 updateTimeRange(realStart, endTime);
833a21aa 622 }
c392540b
FC
623
624 // ------------------------------------------------------------------------
625 // Signal handlers
626 // ------------------------------------------------------------------------
627
b544077e 628 /**
faa38350 629 * Handles trace opened signal. Loads histogram if new trace time range is not
b544077e 630 * equal <code>TmfTimeRange.NULL_RANGE</code>
fec1ac0b 631 * @param signal the trace opened signal
b544077e 632 */
1406f802 633 @TmfSignalHandler
faa38350 634 public void traceOpened(TmfTraceOpenedSignal signal) {
c392540b 635 assert (signal != null);
faa38350
PT
636 fTrace = signal.getTrace();
637 loadTrace();
ecfd1d41 638 }
550d787e 639
faa38350
PT
640 /**
641 * Handles trace selected signal. Loads histogram if new trace time range is not
642 * equal <code>TmfTimeRange.NULL_RANGE</code>
643 * @param signal the trace selected signal
faa38350
PT
644 */
645 @TmfSignalHandler
646 public void traceSelected(TmfTraceSelectedSignal signal) {
647 assert (signal != null);
648 if (fTrace != signal.getTrace()) {
649 fTrace = signal.getTrace();
650 loadTrace();
651 }
652 }
653
654 private void loadTrace() {
c392540b 655 initializeHistograms();
e8c79054 656 getParentComposite().redraw();
550d787e
FC
657 }
658
ea279a69 659 /**
faa38350
PT
660 * Handles trace closed signal. Clears the view and data model and cancels requests.
661 * @param signal the trace closed signal
ea279a69
FC
662 */
663 @TmfSignalHandler
faa38350
PT
664 public void traceClosed(TmfTraceClosedSignal signal) {
665
666 if (signal.getTrace() != fTrace) {
667 return;
668 }
ea279a69
FC
669
670 // Kill any running request
671 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
672 fTimeRangeRequest.cancel();
673 }
674 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
675 fFullTraceRequest.cancel();
676 }
677
678 // Initialize the internal data
faa38350
PT
679 fTrace = null;
680 fTraceStartTime = 0L;
681 fTraceEndTime = 0L;
f8177ba2
FC
682 fWindowStartTime = 0L;
683 fWindowEndTime = 0L;
d7ee91bb 684 fWindowSpan = 0L;
0fcf3b09
PT
685 fSelectionBeginTime = 0L;
686 fSelectionEndTime = 0L;
ea279a69
FC
687
688 // Clear the UI widgets
689 fFullTraceHistogram.clear();
690 fTimeRangeHistogram.clear();
720d67cb
PT
691 fSelectionStartControl.setValue(Long.MIN_VALUE);
692 fSelectionEndControl.setValue(Long.MIN_VALUE);
f8177ba2 693
da7bdcbc 694 fTimeSpanControl.setValue(Long.MIN_VALUE);
2fc582d2
XR
695
696 for (Control c: fLegendArea.getChildren()) {
697 c.dispose();
698 }
03ec1f6a 699 disposeLegendImages();
2fc582d2
XR
700 fLegendArea.layout();
701 fLegendArea.getParent().layout();
ea279a69
FC
702 }
703
b544077e 704 /**
faa38350 705 * Handles trace range updated signal. Extends histogram according to the new time range. If a
b544077e
BH
706 * HistogramRequest is already ongoing, it will be cancelled and a new request with the new range
707 * will be issued.
abbdd66a 708 *
faa38350 709 * @param signal the trace range updated signal
b544077e 710 */
74237cc3 711 @TmfSignalHandler
faa38350 712 public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) {
74237cc3 713
faa38350 714 if (signal.getTrace() != fTrace) {
09d11238
PT
715 return;
716 }
717
74237cc3
FC
718 TmfTimeRange fullRange = signal.getRange();
719
16801c72
MK
720 fTraceStartTime = fullRange.getStartTime().toNanos();
721 fTraceEndTime = fullRange.getEndTime().toNanos();
74237cc3 722
faa38350
PT
723 fFullTraceHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
724 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
74237cc3 725
74237cc3
FC
726 sendFullRangeRequest(fullRange);
727 }
c392540b 728
b544077e 729 /**
faa38350
PT
730 * Handles the trace updated signal. Used to update time limits (start and end time)
731 * @param signal the trace updated signal
b544077e 732 */
a28d503d 733 @TmfSignalHandler
faa38350
PT
734 public void traceUpdated(TmfTraceUpdatedSignal signal) {
735 if (signal.getTrace() != fTrace) {
a28d503d
PT
736 return;
737 }
faa38350 738 TmfTimeRange fullRange = signal.getTrace().getTimeRange();
16801c72
MK
739 fTraceStartTime = fullRange.getStartTime().toNanos();
740 fTraceEndTime = fullRange.getEndTime().toNanos();
a28d503d 741
faa38350
PT
742 fFullTraceHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
743 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
744
745 if ((fFullTraceRequest != null) && fFullTraceRequest.getRange().getEndTime().compareTo(signal.getRange().getEndTime()) < 0) {
746 sendFullRangeRequest(fullRange);
747 }
748}
a28d503d 749
b544077e 750 /**
97c71024
AM
751 * Handles the selection range updated signal. Sets the current time
752 * selection in the time range histogram as well as the full histogram.
abbdd66a 753 *
97c71024
AM
754 * @param signal
755 * the signal to process
756 * @since 1.0
b544077e 757 */
c392540b 758 @TmfSignalHandler
97c71024 759 public void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal) {
80c930fa
PT
760 if (Display.getCurrent() == null) {
761 // Make sure the signal is handled in the UI thread
762 Display.getDefault().asyncExec(new Runnable() {
763 @Override
764 public void run() {
e8c79054 765 if (getParentComposite().isDisposed()) {
80c930fa
PT
766 return;
767 }
97c71024 768 selectionRangeUpdated(signal);
80c930fa
PT
769 }
770 });
771 return;
772 }
c392540b 773
0fcf3b09 774 // Update the selected time range
16801c72
MK
775 long beginTime = signal.getBeginTime().toNanos();
776 long endTime = signal.getEndTime().toNanos();
777
778 updateDisplayedSelectionTime(beginTime, endTime);
ecfd1d41 779 }
f05aabed 780
b544077e 781 /**
97c71024
AM
782 * Updates the current window time range in the time range histogram and
783 * full range histogram.
784 *
785 * @param signal
786 * the signal to process
787 * @since 1.0
b544077e 788 */
c392540b 789 @TmfSignalHandler
97c71024 790 public void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal) {
80c930fa
PT
791 if (Display.getCurrent() == null) {
792 // Make sure the signal is handled in the UI thread
793 Display.getDefault().asyncExec(new Runnable() {
794 @Override
795 public void run() {
e8c79054 796 if (getParentComposite().isDisposed()) {
80c930fa
PT
797 return;
798 }
97c71024 799 windowRangeUpdated(signal);
80c930fa
PT
800 }
801 });
802 return;
803 }
c392540b 804
faa38350 805 if (fTrace != null) {
1c6a842a
PT
806 // Validate the time range
807 TmfTimeRange range = signal.getCurrentRange().getIntersection(fTrace.getTimeRange());
808 if (range == null) {
809 return;
810 }
811
38df2c82 812 updateDisplayedTimeRange(
16801c72
MK
813 range.getStartTime().toNanos(),
814 range.getEndTime().toNanos());
c392540b 815
38df2c82 816 // Send the event request to populate the small histogram
c392540b 817 sendTimeRangeRequest(fWindowStartTime, fWindowEndTime);
f8177ba2 818
c392540b
FC
819 fTimeSpanControl.setValue(fWindowSpan);
820 }
b59134e1 821 }
c392540b
FC
822
823 // ------------------------------------------------------------------------
824 // Helper functions
825 // ------------------------------------------------------------------------
826
827 private void initializeHistograms() {
faa38350 828 TmfTimeRange fullRange = updateTraceTimeRange();
21852dfa
AM
829
830 TmfTraceContext ctx = TmfTraceManager.getInstance().getCurrentTraceContext();
16801c72
MK
831 long selectionBeginTime = ctx.getSelectionRange().getStartTime().toNanos();
832 long selectionEndTime = ctx.getSelectionRange().getEndTime().toNanos();
833 long startTime = ctx.getWindowRange().getStartTime().toNanos();
834 long duration = ctx.getWindowRange().getEndTime().toNanos() - startTime;
74237cc3 835
faa38350
PT
836 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
837 fTimeRangeRequest.cancel();
838 }
74237cc3 839 fTimeRangeHistogram.clear();
faa38350 840 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
d7ee91bb 841 fTimeRangeHistogram.setTimeRange(startTime, duration);
0fcf3b09 842 fTimeRangeHistogram.setSelection(selectionBeginTime, selectionEndTime);
2fc582d2 843 fTimeRangeHistogram.fDataModel.setTrace(fTrace);
c392540b 844
faa38350
PT
845 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
846 fFullTraceRequest.cancel();
847 }
74237cc3 848 fFullTraceHistogram.clear();
faa38350 849 fFullTraceHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
d7ee91bb 850 fFullTraceHistogram.setTimeRange(startTime, duration);
0fcf3b09 851 fFullTraceHistogram.setSelection(selectionBeginTime, selectionEndTime);
2fc582d2 852 fFullTraceHistogram.fDataModel.setTrace(fTrace);
c392540b 853
d7ee91bb
PT
854 fWindowStartTime = startTime;
855 fWindowSpan = duration;
856 fWindowEndTime = startTime + duration;
6a13fa07 857
0fcf3b09
PT
858 fSelectionBeginTime = selectionBeginTime;
859 fSelectionEndTime = selectionEndTime;
720d67cb
PT
860 fSelectionStartControl.setValue(fSelectionBeginTime);
861 fSelectionEndControl.setValue(fSelectionEndTime);
f8177ba2 862
f7d9ea05
BH
863 // make sure that the scrollbar is setup properly
864 fScrollComposite.setMinSize(fTimeControlsComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
d7ee91bb 865 fTimeSpanControl.setValue(duration);
6a13fa07 866
c14c0757
GB
867 Collection<ITmfTrace> traces = TmfTraceManager.getTraceSet(fTrace);
868 if (!traces.isEmpty()) {
869 this.showTraceAction.setEnabled(traces.size() < fFullTraceHistogram.getMaxNbTraces());
2fc582d2
XR
870 }
871 updateLegendArea();
872
2af7df97 873 if (!fullRange.equals(TmfTimeRange.NULL_RANGE)) {
d7ee91bb 874 sendTimeRangeRequest(startTime, startTime + duration);
2af7df97
FC
875 sendFullRangeRequest(fullRange);
876 }
74237cc3
FC
877 }
878
2fc582d2
XR
879 private void updateLegendArea() {
880 for (Control c: fLegendArea.getChildren()) {
881 c.dispose();
882 }
03ec1f6a 883 disposeLegendImages();
2fc582d2 884 if (fFullTraceHistogram.showTraces()) {
c14c0757
GB
885 Collection<ITmfTrace> traces = TmfTraceManager.getTraceSet(fTrace);
886 fLegendImages = new Image[traces.size()];
2fc582d2
XR
887 int traceIndex = 0;
888 for (ITmfTrace trace : traces) {
889 fLegendImages[traceIndex] = new Image(fLegendArea.getDisplay(), 16, 16);
890 GC gc = new GC(fLegendImages[traceIndex]);
891 gc.setBackground(fFullTraceHistogram.getTraceColor(traceIndex));
892 gc.fillRectangle(0, 0, 15, 15);
893 gc.setForeground(fLegendArea.getDisplay().getSystemColor(SWT.COLOR_BLACK));
894 gc.drawRectangle(0, 0, 15, 15);
895 gc.dispose();
896
897 CLabel label = new CLabel(fLegendArea, SWT.NONE);
898 label.setText(trace.getName());
899 label.setImage(fLegendImages[traceIndex]);
900 traceIndex++;
901 }
902 }
903 fLegendArea.layout();
904 fLegendArea.getParent().layout();
905 }
906
0fcf3b09
PT
907 private void updateDisplayedSelectionTime(long beginTime, long endTime) {
908 fSelectionBeginTime = beginTime;
909 fSelectionEndTime = endTime;
38df2c82 910
0fcf3b09
PT
911 fFullTraceHistogram.setSelection(fSelectionBeginTime, fSelectionEndTime);
912 fTimeRangeHistogram.setSelection(fSelectionBeginTime, fSelectionEndTime);
720d67cb
PT
913 fSelectionStartControl.setValue(fSelectionBeginTime);
914 fSelectionEndControl.setValue(fSelectionEndTime);
38df2c82
AM
915 }
916
917 private void updateDisplayedTimeRange(long start, long end) {
918 fWindowStartTime = start;
919 fWindowEndTime = end;
920 fWindowSpan = fWindowEndTime - fWindowStartTime;
921 fFullTraceHistogram.setTimeRange(fWindowStartTime, fWindowSpan);
922 }
923
faa38350
PT
924 private TmfTimeRange updateTraceTimeRange() {
925 fTraceStartTime = 0L;
926 fTraceEndTime = 0L;
74237cc3 927
faa38350 928 TmfTimeRange timeRange = fTrace.getTimeRange();
c6023803 929 if (!timeRange.equals(TmfTimeRange.NULL_RANGE)) {
16801c72
MK
930 fTraceStartTime = timeRange.getStartTime().toNanos();
931 fTraceEndTime = timeRange.getEndTime().toNanos();
74237cc3
FC
932 }
933 return timeRange;
b59134e1 934 }
c392540b
FC
935
936 private void sendTimeRangeRequest(long startTime, long endTime) {
1b055dfa 937 if ((fTimeRangeRequest != null) && !fTimeRangeRequest.isCompleted()) {
c392540b 938 fTimeRangeRequest.cancel();
088c1d4e 939 }
f8177ba2
FC
940 TmfTimestamp startTS = new TmfTimestamp(startTime, ITmfTimestamp.NANOSECOND_SCALE);
941 TmfTimestamp endTS = new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE);
c392540b
FC
942 TmfTimeRange timeRange = new TmfTimeRange(startTS, endTS);
943
944 fTimeRangeHistogram.clear();
15844a4e 945 fTimeRangeHistogram.setFullRange(fTraceStartTime, fTraceEndTime);
c392540b 946 fTimeRangeHistogram.setTimeRange(startTime, endTime - startTime);
4cf201de 947
faa38350 948 int cacheSize = fTrace.getCacheSize();
2740e05c
AM
949 fTimeRangeRequest = new HistogramRequest(fTimeRangeHistogram.getDataModel(),
950 timeRange, 0, ITmfEventRequest.ALL_DATA, cacheSize, ExecutionType.FOREGROUND, false);
faa38350 951 fTrace.sendRequest(fTimeRangeRequest);
088c1d4e 952 }
c392540b 953
74237cc3 954 private void sendFullRangeRequest(TmfTimeRange fullRange) {
1b055dfa 955 if ((fFullTraceRequest != null) && !fFullTraceRequest.isCompleted()) {
c392540b
FC
956 fFullTraceRequest.cancel();
957 }
faa38350 958 int cacheSize = fTrace.getCacheSize();
2740e05c
AM
959 fFullTraceRequest = new HistogramRequest(fFullTraceHistogram.getDataModel(),
960 fullRange,
961 (int) fFullTraceHistogram.fDataModel.getNbEvents(),
962 ITmfEventRequest.ALL_DATA,
963 cacheSize,
964 ExecutionType.BACKGROUND, true);
faa38350 965 fTrace.sendRequest(fFullTraceRequest);
ed4b3b9f 966 }
c392540b 967
95aa81ef
JCK
968 private void contributeToActionBars() {
969 IActionBars bars = getViewSite().getActionBars();
970 bars.getToolBarManager().add(getShowLostEventsAction());
2fc582d2 971 bars.getToolBarManager().add(getShowTraceAction());
95aa81ef
JCK
972 bars.getToolBarManager().add(new Separator());
973 }
974
720d67cb
PT
975 private void addLinkButtonListeners() {
976 fLinkButton.addMouseListener(new MouseAdapter() {
977 @Override
978 public void mouseDown(MouseEvent e) {
979 fSelectionEndControl.setEnabled(fLinkState);
980 fLinkState = !fLinkState;
981 fLinkButton.redraw();
982 }
983 });
984
985 fLinkButton.addPaintListener(new PaintListener() {
986 @Override
987 public void paintControl(PaintEvent e) {
988 if (fLinkState) {
989 Rectangle r = fLinkButton.getBounds();
990 r.x = -1;
991 r.y = -1;
992 e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
993 e.gc.drawRectangle(r);
994 r.x = 0;
995 r.y = 0;
996 e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_DARK_GRAY));
997 e.gc.drawRectangle(r);
998 }
999 }
1000 });
1001 }
f7d9ea05
BH
1002
1003 private static class PackedScrolledComposite extends ScrolledComposite {
1004 Point fScrollBarSize; // Size of OS-specific scrollbar
1005
1006 public PackedScrolledComposite(Composite parent, int style) {
1007 super(parent, style);
1008 Composite composite = new Composite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
1009 composite.setSize(1, 1);
1010 fScrollBarSize = composite.computeSize(0, 0);
1011 composite.dispose();
1012 }
1013
1014 @Override
1015 public Point computeSize(int wHint, int hHint, boolean changed) {
1016 Point point = super.computeSize(wHint, hHint, changed);
1017 // Remove scrollbar size if applicable
1018 point.x += ((getStyle() & SWT.V_SCROLL) != 0) ? -fScrollBarSize.x : 0;
1019 point.y += ((getStyle() & SWT.H_SCROLL) != 0) ? -fScrollBarSize.y : 0;
1020 return point;
1021 }
1022 }
6e512b93 1023}
This page took 0.203028 seconds and 5 git commands to generate.