tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / Histogram.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Changed to updated histogram data model
12 * Francois Chouinard - Reformat histogram labels on format change
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.ui.views.histogram;
16
17 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
18 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
19 import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal;
20 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
21 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
22 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
23 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.ControlEvent;
26 import org.eclipse.swt.events.ControlListener;
27 import org.eclipse.swt.events.KeyEvent;
28 import org.eclipse.swt.events.KeyListener;
29 import org.eclipse.swt.events.MouseEvent;
30 import org.eclipse.swt.events.MouseListener;
31 import org.eclipse.swt.events.MouseTrackListener;
32 import org.eclipse.swt.events.PaintEvent;
33 import org.eclipse.swt.events.PaintListener;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.graphics.FontData;
37 import org.eclipse.swt.graphics.GC;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.FillLayout;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Canvas;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Display;
45 import org.eclipse.swt.widgets.Text;
46
47 /**
48 * Re-usable histogram widget.
49 *
50 * It has the following features:
51 * <ul>
52 * <li>Y-axis labels displaying min/max count values
53 * <li>X-axis labels displaying time range
54 * <li>a histogram displaying the distribution of values over time (note that
55 * the histogram might not necessarily fill the whole canvas)
56 * </ul>
57 * The widget also has 2 'markers' to identify:
58 * <ul>
59 * <li>a red dashed line over the bar that contains the currently selected event
60 * <li>a dark red dashed line that delimits the right end of the histogram (if
61 * it doesn't fill the canvas)
62 * </ul>
63 * Clicking on the histogram will select the current event at the mouse
64 * location.
65 * <p>
66 * Once the histogram is selected, there is some limited keyboard support:
67 * <ul>
68 * <li>Home: go to the first histogram bar
69 * <li>End: go to the last histogram bar
70 * <li>Left: go to the previous histogram
71 * <li>Right: go to the next histogram bar
72 * </ul>
73 * Finally, when the mouse hovers over the histogram, a tool tip showing the
74 * following information about the corresponding histogram bar time range:
75 * <ul>
76 * <li>start of the time range
77 * <li>end of the time range
78 * <li>number of events in that time range
79 * </ul>
80 *
81 * @version 1.1
82 * @author Francois Chouinard
83 */
84 public abstract class Histogram implements ControlListener, PaintListener, KeyListener, MouseListener, MouseTrackListener, IHistogramModelListener {
85
86 // ------------------------------------------------------------------------
87 // Constants
88 // ------------------------------------------------------------------------
89
90 // Histogram colors
91 private final Color fBackgroundColor = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
92 private final Color fCurrentEventColor = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
93 private final Color fLastEventColor = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
94 private final Color fHistoBarColor = new Color(Display.getDefault(), 74, 112, 139);
95
96 // ------------------------------------------------------------------------
97 // Attributes
98 // ------------------------------------------------------------------------
99
100 /**
101 * The parent TMF view.
102 */
103 protected TmfView fParentView;
104
105 private Composite fComposite;
106 private Font fFont;
107
108 // Histogram text fields
109 private Text fMaxNbEventsText;
110 private Text fMinNbEventsText;
111 private Text fTimeRangeStartText;
112 private Text fTimeRangeEndText;
113
114 /**
115 * Histogram drawing area
116 */
117 protected Canvas fCanvas;
118
119 /**
120 * The histogram data model.
121 */
122 protected final HistogramDataModel fDataModel;
123
124 /**
125 * The histogram data model scaled to current resolution and screen width.
126 */
127 protected HistogramScaledData fScaledData;
128
129 /**
130 * The current event value
131 */
132 protected long fCurrentEventTime = 0L;
133
134 // ------------------------------------------------------------------------
135 // Construction
136 // ------------------------------------------------------------------------
137
138 /**
139 * Full constructor.
140 *
141 * @param view A reference to the parent TMF view.
142 * @param parent A parent composite
143 */
144 public Histogram(final TmfView view, final Composite parent) {
145 fParentView = view;
146
147 fComposite = createWidget(parent);
148 fDataModel = new HistogramDataModel();
149 fDataModel.addHistogramListener(this);
150 clear();
151
152 fCanvas.addControlListener(this);
153 fCanvas.addPaintListener(this);
154 fCanvas.addKeyListener(this);
155 fCanvas.addMouseListener(this);
156 fCanvas.addMouseTrackListener(this);
157
158 TmfSignalManager.register(this);
159 }
160
161 /**
162 * Dispose resources and unregisters listeners.
163 */
164 public void dispose() {
165 TmfSignalManager.deregister(this);
166
167 fHistoBarColor.dispose();
168 fDataModel.removeHistogramListener(this);
169 }
170
171 private Composite createWidget(final Composite parent) {
172
173 final Color labelColor = parent.getBackground();
174 fFont = adjustFont(parent);
175
176 final int initalWidth = 10;
177
178 // --------------------------------------------------------------------
179 // Define the histogram
180 // --------------------------------------------------------------------
181
182 final GridLayout gridLayout = new GridLayout();
183 gridLayout.numColumns = 3;
184 gridLayout.marginHeight = 0;
185 gridLayout.marginWidth = 0;
186 gridLayout.marginTop = 0;
187 gridLayout.horizontalSpacing = 0;
188 gridLayout.verticalSpacing = 0;
189 gridLayout.marginLeft = 0;
190 gridLayout.marginRight = 0;
191 final Composite composite = new Composite(parent, SWT.FILL);
192 composite.setLayout(gridLayout);
193
194 // Use all the horizontal space
195 GridData gridData = new GridData();
196 gridData.horizontalAlignment = SWT.FILL;
197 gridData.verticalAlignment = SWT.FILL;
198 gridData.grabExcessHorizontalSpace = true;
199 composite.setLayoutData(gridData);
200
201 // Y-axis max event
202 gridData = new GridData();
203 gridData.horizontalAlignment = SWT.RIGHT;
204 gridData.verticalAlignment = SWT.TOP;
205 fMaxNbEventsText = new Text(composite, SWT.READ_ONLY | SWT.RIGHT);
206 fMaxNbEventsText.setFont(fFont);
207 fMaxNbEventsText.setBackground(labelColor);
208 fMaxNbEventsText.setEditable(false);
209 fMaxNbEventsText.setText("0"); //$NON-NLS-1$
210 fMaxNbEventsText.setLayoutData(gridData);
211
212 // Histogram itself
213 Composite canvasComposite = new Composite(composite, SWT.BORDER);
214 gridData = new GridData();
215 gridData.horizontalSpan = 2;
216 gridData.verticalSpan = 2;
217 gridData.horizontalAlignment = SWT.FILL;
218 gridData.verticalAlignment = SWT.FILL;
219 gridData.grabExcessHorizontalSpace = true;
220 canvasComposite.setLayoutData(gridData);
221 canvasComposite.setLayout(new FillLayout());
222 fCanvas = new Canvas(canvasComposite, SWT.DOUBLE_BUFFERED);
223
224 // Y-axis min event (always 0...)
225 gridData = new GridData();
226 gridData.horizontalAlignment = SWT.RIGHT;
227 gridData.verticalAlignment = SWT.BOTTOM;
228 fMinNbEventsText = new Text(composite, SWT.READ_ONLY | SWT.RIGHT);
229 fMinNbEventsText.setFont(fFont);
230 fMinNbEventsText.setBackground(labelColor);
231 fMinNbEventsText.setEditable(false);
232 fMinNbEventsText.setText("0"); //$NON-NLS-1$
233 fMinNbEventsText.setLayoutData(gridData);
234
235 // Dummy cell
236 gridData = new GridData(initalWidth, SWT.DEFAULT);
237 gridData.horizontalAlignment = SWT.RIGHT;
238 gridData.verticalAlignment = SWT.BOTTOM;
239 final Text dummyText = new Text(composite, SWT.READ_ONLY);
240 dummyText.setFont(fFont);
241 dummyText.setBackground(labelColor);
242 dummyText.setEditable(false);
243 dummyText.setText(""); //$NON-NLS-1$
244 dummyText.setLayoutData(gridData);
245
246 // Window range start time
247 gridData = new GridData();
248 gridData.horizontalAlignment = SWT.LEFT;
249 gridData.verticalAlignment = SWT.BOTTOM;
250 fTimeRangeStartText = new Text(composite, SWT.READ_ONLY);
251 fTimeRangeStartText.setFont(fFont);
252 fTimeRangeStartText.setBackground(labelColor);
253 fTimeRangeStartText.setLayoutData(gridData);
254
255 // Window range end time
256 gridData = new GridData();
257 gridData.horizontalAlignment = SWT.RIGHT;
258 gridData.verticalAlignment = SWT.BOTTOM;
259 fTimeRangeEndText = new Text(composite, SWT.READ_ONLY);
260 fTimeRangeEndText.setFont(fFont);
261 fTimeRangeEndText.setBackground(labelColor);
262 fTimeRangeEndText.setLayoutData(gridData);
263
264 return composite;
265 }
266
267 private static Font adjustFont(final Composite composite) {
268 // Reduce font size for a more pleasing rendering
269 final int fontSizeAdjustment = -2;
270 final Font font = composite.getFont();
271 final FontData fontData = font.getFontData()[0];
272 return new Font(font.getDevice(), fontData.getName(), fontData.getHeight() + fontSizeAdjustment, fontData.getStyle());
273 }
274
275 // ------------------------------------------------------------------------
276 // Accessors
277 // ------------------------------------------------------------------------
278
279 /**
280 * Returns the start time (equal first bucket time)
281 * @return the start time.
282 */
283 public long getStartTime() {
284 return fDataModel.getFirstBucketTime();
285 }
286
287 /**
288 * Returns the end time.
289 * @return the end time.
290 */
291 public long getEndTime() {
292 return fDataModel.getEndTime();
293 }
294
295 /**
296 * Returns the time limit (end of last bucket)
297 * @return the time limit.
298 */
299 public long getTimeLimit() {
300 return fDataModel.getTimeLimit();
301 }
302
303 /**
304 * Returns a data model reference.
305 * @return data model.
306 */
307 public HistogramDataModel getDataModel() {
308 return fDataModel;
309 }
310
311 // ------------------------------------------------------------------------
312 // Operations
313 // ------------------------------------------------------------------------
314 /**
315 * Updates the time range.
316 * @param startTime A start time
317 * @param endTime A end time.
318 */
319 public abstract void updateTimeRange(long startTime, long endTime);
320
321 /**
322 * Clear the histogram and reset the data
323 */
324 public void clear() {
325 fDataModel.clear();
326 synchronized (fDataModel) {
327 fScaledData = null;
328 }
329 }
330
331 /**
332 * Increase the histogram bucket corresponding to [timestamp]
333 *
334 * @param eventCount
335 * The new event count
336 * @param timestamp
337 * The latest timestamp
338 */
339 public void countEvent(final long eventCount, final long timestamp) {
340 fDataModel.countEvent(eventCount, timestamp);
341 }
342
343 /**
344 * Sets the current event time and refresh the display
345 *
346 * @param timestamp
347 * The time of the current event
348 */
349 public void setCurrentEvent(final long timestamp) {
350 fCurrentEventTime = (timestamp > 0) ? timestamp : 0;
351 fDataModel.setCurrentEventNotifyListeners(timestamp);
352 }
353
354 /**
355 * Computes the timestamp of the bucket at [offset]
356 *
357 * @param offset offset from the left on the histogram
358 * @return the start timestamp of the corresponding bucket
359 */
360 public synchronized long getTimestamp(final int offset) {
361 assert offset > 0 && offset < fScaledData.fWidth;
362 try {
363 return fDataModel.getFirstBucketTime() + fScaledData.fBucketDuration * offset;
364 } catch (final Exception e) {
365 return 0; // TODO: Fix that racing condition (NPE)
366 }
367 }
368
369 /**
370 * Computes the offset of the timestamp in the histogram
371 *
372 * @param timestamp the timestamp
373 * @return the offset of the corresponding bucket (-1 if invalid)
374 */
375 public synchronized int getOffset(final long timestamp) {
376 if (timestamp < fDataModel.getFirstBucketTime() || timestamp > fDataModel.getEndTime()) {
377 return -1;
378 }
379 return (int) ((timestamp - fDataModel.getFirstBucketTime()) / fScaledData.fBucketDuration);
380 }
381
382 /**
383 * Move the currently selected bar cursor to a non-empty bucket.
384 *
385 * @param keyCode the SWT key code
386 */
387 protected void moveCursor(final int keyCode) {
388
389 if (fScaledData.fCurrentBucket == HistogramScaledData.OUT_OF_RANGE_BUCKET) {
390 return;
391 }
392
393 int index;
394 switch (keyCode) {
395
396 case SWT.HOME:
397 index = 0;
398 while (index < fScaledData.fLastBucket && fScaledData.fData[index] == 0) {
399 index++;
400 }
401 if (index < fScaledData.fLastBucket) {
402 fScaledData.fCurrentBucket = index;
403 }
404 break;
405
406 case SWT.ARROW_RIGHT:
407 index = fScaledData.fCurrentBucket + 1;
408 while (index < fScaledData.fWidth && fScaledData.fData[index] == 0) {
409 index++;
410 }
411 if (index < fScaledData.fLastBucket) {
412 fScaledData.fCurrentBucket = index;
413 }
414 break;
415
416 case SWT.END:
417 index = fScaledData.fLastBucket;
418 while (index >= 0 && fScaledData.fData[index] == 0) {
419 index--;
420 }
421 if (index >= 0) {
422 fScaledData.fCurrentBucket = index;
423 }
424 break;
425
426 case SWT.ARROW_LEFT:
427 index = fScaledData.fCurrentBucket - 1;
428 while (index >= 0 && fScaledData.fData[index] == 0) {
429 index--;
430 }
431 if (index >= 0) {
432 fScaledData.fCurrentBucket = index;
433 }
434 break;
435
436 default:
437 return;
438 }
439
440 updateCurrentEventTime();
441 }
442
443 /**
444 * Refresh the histogram display
445 */
446 @Override
447 public void modelUpdated() {
448 if (!fCanvas.isDisposed() && fCanvas.getDisplay() != null) {
449 fCanvas.getDisplay().asyncExec(new Runnable() {
450 @Override
451 public void run() {
452 if (!fCanvas.isDisposed()) {
453 // Retrieve and normalize the data
454 final int canvasWidth = fCanvas.getBounds().width;
455 final int canvasHeight = fCanvas.getBounds().height;
456 if (canvasWidth <= 0 || canvasHeight <= 0) {
457 return;
458 }
459 fDataModel.setCurrentEvent(fCurrentEventTime);
460 fScaledData = fDataModel.scaleTo(canvasWidth, canvasHeight, 1);
461 synchronized(fDataModel) {
462 if (fScaledData != null) {
463 fCanvas.redraw();
464 if (fDataModel.getNbEvents() != 0) {
465 // Display histogram and update X-,Y-axis labels
466 fTimeRangeStartText.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getFirstBucketTime()));
467 fTimeRangeEndText.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getEndTime()));
468 } else {
469 fTimeRangeStartText.setText(""); //$NON-NLS-1$
470 fTimeRangeEndText.setText(""); //$NON-NLS-1$
471 }
472 fMaxNbEventsText.setText(Long.toString(fScaledData.fMaxValue));
473 // The Y-axis area might need to be re-sized
474 fMaxNbEventsText.getParent().layout();
475 }
476 }
477 }
478 }
479 });
480 }
481 }
482
483 // ------------------------------------------------------------------------
484 // Helper functions
485 // ------------------------------------------------------------------------
486
487 private void updateCurrentEventTime() {
488 final long bucketStartTime = getTimestamp(fScaledData.fCurrentBucket);
489 ((HistogramView) fParentView).updateCurrentEventTime(bucketStartTime);
490 }
491
492 // ------------------------------------------------------------------------
493 // PaintListener
494 // ------------------------------------------------------------------------
495 /**
496 * Image key string for the canvas.
497 */
498 protected final String IMAGE_KEY = "double-buffer-image"; //$NON-NLS-1$
499
500 @Override
501 public void paintControl(final PaintEvent event) {
502
503 // Get the geometry
504 final int canvasWidth = fCanvas.getBounds().width;
505 final int canvasHeight = fCanvas.getBounds().height;
506
507 // Make sure we have something to draw upon
508 if (canvasWidth <= 0 || canvasHeight <= 0) {
509 return;
510 }
511
512 // Retrieve image; re-create only if necessary
513 Image image = (Image) fCanvas.getData(IMAGE_KEY);
514 if (image == null || image.getBounds().width != canvasWidth || image.getBounds().height != canvasHeight) {
515 image = new Image(event.display, canvasWidth, canvasHeight);
516 fCanvas.setData(IMAGE_KEY, image);
517 }
518
519 // Draw the histogram on its canvas
520 final GC imageGC = new GC(image);
521 formatImage(imageGC, image);
522 event.gc.drawImage(image, 0, 0);
523 imageGC.dispose();
524 }
525
526 private void formatImage(final GC imageGC, final Image image) {
527
528 if (fScaledData == null) {
529 return;
530 }
531
532 final HistogramScaledData scaledData = new HistogramScaledData(fScaledData);
533
534 try {
535 // Get drawing boundaries
536 final int width = image.getBounds().width;
537 final int height = image.getBounds().height;
538
539 // Clear the drawing area
540 imageGC.setBackground(fBackgroundColor);
541 imageGC.fillRectangle(0, 0, image.getBounds().width + 1, image.getBounds().height + 1);
542
543 // Draw the histogram bars
544 imageGC.setBackground(fHistoBarColor);
545 final int limit = width < scaledData.fWidth ? width : scaledData.fWidth;
546 for (int i = 0; i < limit; i++) {
547 final int value = (int) Math.ceil(scaledData.fData[i] * scaledData.fScalingFactor);
548 imageGC.fillRectangle(i, height - value, 1, value);
549 }
550
551 // Draw the current event bar
552 final int currentBucket = scaledData.fCurrentBucket;
553 if (currentBucket >= 0 && currentBucket < limit) {
554 drawDelimiter(imageGC, fCurrentEventColor, height, currentBucket);
555 }
556
557 // Add a dashed line as a delimiter (at the right of the last bar)
558 int lastEventIndex = limit - 1;
559 while (lastEventIndex >= 0 && scaledData.fData[lastEventIndex] == 0) {
560 lastEventIndex--;
561 }
562 lastEventIndex += (lastEventIndex < limit - 1) ? 1 : 0;
563 drawDelimiter(imageGC, fLastEventColor, height, lastEventIndex);
564 } catch (final Exception e) {
565 // Do nothing
566 }
567 }
568
569 private static void drawDelimiter(final GC imageGC, final Color color,
570 final int height, final int index) {
571 imageGC.setBackground(color);
572 final int dash = height / 4;
573 imageGC.fillRectangle(index, 0 * dash, 1, dash - 1);
574 imageGC.fillRectangle(index, 1 * dash, 1, dash - 1);
575 imageGC.fillRectangle(index, 2 * dash, 1, dash - 1);
576 imageGC.fillRectangle(index, 3 * dash, 1, height - 3 * dash);
577 }
578
579 // ------------------------------------------------------------------------
580 // KeyListener
581 // ------------------------------------------------------------------------
582
583 @Override
584 public void keyPressed(final KeyEvent event) {
585 moveCursor(event.keyCode);
586 }
587
588 @Override
589 public void keyReleased(final KeyEvent event) {
590 }
591
592 // ------------------------------------------------------------------------
593 // MouseListener
594 // ------------------------------------------------------------------------
595
596 @Override
597 public void mouseDoubleClick(final MouseEvent event) {
598 }
599
600 @Override
601 public void mouseDown(final MouseEvent event) {
602 if (fDataModel.getNbEvents() > 0 && fScaledData.fLastBucket >= event.x) {
603 fScaledData.fCurrentBucket = event.x;
604 updateCurrentEventTime();
605 }
606 }
607
608 @Override
609 public void mouseUp(final MouseEvent event) {
610 }
611
612 // ------------------------------------------------------------------------
613 // MouseTrackListener
614 // ------------------------------------------------------------------------
615
616 @Override
617 public void mouseEnter(final MouseEvent event) {
618 }
619
620 @Override
621 public void mouseExit(final MouseEvent event) {
622 }
623
624 @Override
625 public void mouseHover(final MouseEvent event) {
626 if (fDataModel.getNbEvents() > 0 && fScaledData != null && fScaledData.fLastBucket >= event.x) {
627 final String tooltip = formatToolTipLabel(event.x);
628 fCanvas.setToolTipText(tooltip);
629 } else {
630 fCanvas.setToolTipText(null);
631 }
632 }
633
634 private String formatToolTipLabel(final int index) {
635 long startTime = fScaledData.getBucketStartTime(index);
636 // negative values are possible if time values came into the model in decreasing order
637 if (startTime < 0) {
638 startTime = 0;
639 }
640 final long endTime = fScaledData.getBucketEndTime(index);
641 final int nbEvents = (index >= 0) ? fScaledData.fData[index] : 0;
642
643 final StringBuffer buffer = new StringBuffer();
644 buffer.append("Range = ["); //$NON-NLS-1$
645 buffer.append(new TmfTimestamp(startTime, ITmfTimestamp.NANOSECOND_SCALE).toString());
646 buffer.append(","); //$NON-NLS-1$
647 buffer.append(new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE).toString());
648 buffer.append(")\n"); //$NON-NLS-1$
649 buffer.append("Event count = "); //$NON-NLS-1$
650 buffer.append(nbEvents);
651 return buffer.toString();
652 }
653
654 // ------------------------------------------------------------------------
655 // ControlListener
656 // ------------------------------------------------------------------------
657
658 @Override
659 public void controlMoved(final ControlEvent event) {
660 fDataModel.complete();
661 }
662
663 @Override
664 public void controlResized(final ControlEvent event) {
665 fDataModel.complete();
666 }
667
668 // ------------------------------------------------------------------------
669 // Signal Handlers
670 // ------------------------------------------------------------------------
671
672 /**
673 * Format the timestamp and update the display
674 *
675 * @param signal the incoming signal
676 * @since 2.0
677 */
678 @TmfSignalHandler
679 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
680 if (fDataModel.getNbEvents() == 0) {
681 return;
682 }
683
684 String newTS = TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getFirstBucketTime());
685 fTimeRangeStartText.setText(newTS);
686
687 newTS = TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getEndTime());
688 fTimeRangeEndText.setText(newTS);
689
690 fComposite.layout();
691 }
692
693 }
This page took 0.060614 seconds and 6 git commands to generate.