1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
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 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
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
;
48 * Re-usable histogram widget.
50 * It has the following features:
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)
57 * The widget also has 2 'markers' to identify:
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)
63 * Clicking on the histogram will select the current event at the mouse
66 * Once the histogram is selected, there is some limited keyboard support:
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
73 * Finally, when the mouse hovers over the histogram, a tool tip showing the
74 * following information about the corresponding histogram bar time range:
76 * <li>start of the time range
77 * <li>end of the time range
78 * <li>number of events in that time range
82 * @author Francois Chouinard
84 public abstract class Histogram
implements ControlListener
, PaintListener
, KeyListener
, MouseListener
, MouseTrackListener
, IHistogramModelListener
{
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
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);
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
101 * The parent TMF view.
103 protected TmfView fParentView
;
105 private Composite fComposite
;
108 // Histogram text fields
109 private Text fMaxNbEventsText
;
110 private Text fMinNbEventsText
;
111 private Text fTimeRangeStartText
;
112 private Text fTimeRangeEndText
;
115 * Histogram drawing area
117 protected Canvas fCanvas
;
120 * The histogram data model.
122 protected final HistogramDataModel fDataModel
;
125 * The histogram data model scaled to current resolution and screen width.
127 protected HistogramScaledData fScaledData
;
130 * The current event value
132 protected long fCurrentEventTime
= 0L;
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
141 * @param view A reference to the parent TMF view.
142 * @param parent A parent composite
144 public Histogram(final TmfView view
, final Composite parent
) {
147 fComposite
= createWidget(parent
);
148 fDataModel
= new HistogramDataModel();
149 fDataModel
.addHistogramListener(this);
152 fCanvas
.addControlListener(this);
153 fCanvas
.addPaintListener(this);
154 fCanvas
.addKeyListener(this);
155 fCanvas
.addMouseListener(this);
156 fCanvas
.addMouseTrackListener(this);
158 TmfSignalManager
.register(this);
162 * Dispose resources and unregisters listeners.
164 public void dispose() {
165 TmfSignalManager
.deregister(this);
167 fHistoBarColor
.dispose();
168 fDataModel
.removeHistogramListener(this);
171 private Composite
createWidget(final Composite parent
) {
173 final Color labelColor
= parent
.getBackground();
174 fFont
= adjustFont(parent
);
176 final int initalWidth
= 10;
178 // --------------------------------------------------------------------
179 // Define the histogram
180 // --------------------------------------------------------------------
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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());
275 // ------------------------------------------------------------------------
277 // ------------------------------------------------------------------------
280 * Returns the start time (equal first bucket time)
281 * @return the start time.
283 public long getStartTime() {
284 return fDataModel
.getFirstBucketTime();
288 * Returns the end time.
289 * @return the end time.
291 public long getEndTime() {
292 return fDataModel
.getEndTime();
296 * Returns the time limit (end of last bucket)
297 * @return the time limit.
299 public long getTimeLimit() {
300 return fDataModel
.getTimeLimit();
304 * Returns a data model reference.
305 * @return data model.
307 public HistogramDataModel
getDataModel() {
311 // ------------------------------------------------------------------------
313 // ------------------------------------------------------------------------
315 * Updates the time range.
316 * @param startTime A start time
317 * @param endTime A end time.
319 public abstract void updateTimeRange(long startTime
, long endTime
);
322 * Clear the histogram and reset the data
324 public void clear() {
326 synchronized (fDataModel
) {
332 * Increase the histogram bucket corresponding to [timestamp]
335 * The new event count
337 * The latest timestamp
339 public void countEvent(final long eventCount
, final long timestamp
) {
340 fDataModel
.countEvent(eventCount
, timestamp
);
344 * Sets the current event time and refresh the display
347 * The time of the current event
349 public void setCurrentEvent(final long timestamp
) {
350 fCurrentEventTime
= (timestamp
> 0) ? timestamp
: 0;
351 fDataModel
.setCurrentEventNotifyListeners(timestamp
);
355 * Computes the timestamp of the bucket at [offset]
357 * @param offset offset from the left on the histogram
358 * @return the start timestamp of the corresponding bucket
360 public synchronized long getTimestamp(final int offset
) {
361 assert offset
> 0 && offset
< fScaledData
.fWidth
;
363 return fDataModel
.getFirstBucketTime() + fScaledData
.fBucketDuration
* offset
;
364 } catch (final Exception e
) {
365 return 0; // TODO: Fix that racing condition (NPE)
370 * Computes the offset of the timestamp in the histogram
372 * @param timestamp the timestamp
373 * @return the offset of the corresponding bucket (-1 if invalid)
375 public synchronized int getOffset(final long timestamp
) {
376 if (timestamp
< fDataModel
.getFirstBucketTime() || timestamp
> fDataModel
.getEndTime()) {
379 return (int) ((timestamp
- fDataModel
.getFirstBucketTime()) / fScaledData
.fBucketDuration
);
383 * Move the currently selected bar cursor to a non-empty bucket.
385 * @param keyCode the SWT key code
387 protected void moveCursor(final int keyCode
) {
389 if (fScaledData
.fCurrentBucket
== HistogramScaledData
.OUT_OF_RANGE_BUCKET
) {
398 while (index
< fScaledData
.fLastBucket
&& fScaledData
.fData
[index
] == 0) {
401 if (index
< fScaledData
.fLastBucket
) {
402 fScaledData
.fCurrentBucket
= index
;
406 case SWT
.ARROW_RIGHT
:
407 index
= fScaledData
.fCurrentBucket
+ 1;
408 while (index
< fScaledData
.fWidth
&& fScaledData
.fData
[index
] == 0) {
411 if (index
< fScaledData
.fLastBucket
) {
412 fScaledData
.fCurrentBucket
= index
;
417 index
= fScaledData
.fLastBucket
;
418 while (index
>= 0 && fScaledData
.fData
[index
] == 0) {
422 fScaledData
.fCurrentBucket
= index
;
427 index
= fScaledData
.fCurrentBucket
- 1;
428 while (index
>= 0 && fScaledData
.fData
[index
] == 0) {
432 fScaledData
.fCurrentBucket
= index
;
440 updateCurrentEventTime();
444 * Refresh the histogram display
447 public void modelUpdated() {
448 if (!fCanvas
.isDisposed() && fCanvas
.getDisplay() != null) {
449 fCanvas
.getDisplay().asyncExec(new Runnable() {
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) {
459 fDataModel
.setCurrentEvent(fCurrentEventTime
);
460 fScaledData
= fDataModel
.scaleTo(canvasWidth
, canvasHeight
, 1);
461 synchronized(fDataModel
) {
462 if (fScaledData
!= null) {
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()));
469 fTimeRangeStartText
.setText(""); //$NON-NLS-1$
470 fTimeRangeEndText
.setText(""); //$NON-NLS-1$
472 fMaxNbEventsText
.setText(Long
.toString(fScaledData
.fMaxValue
));
473 // The Y-axis area might need to be re-sized
474 fMaxNbEventsText
.getParent().layout();
483 // ------------------------------------------------------------------------
485 // ------------------------------------------------------------------------
487 private void updateCurrentEventTime() {
488 final long bucketStartTime
= getTimestamp(fScaledData
.fCurrentBucket
);
489 ((HistogramView
) fParentView
).updateCurrentEventTime(bucketStartTime
);
492 // ------------------------------------------------------------------------
494 // ------------------------------------------------------------------------
496 * Image key string for the canvas.
498 protected final String IMAGE_KEY
= "double-buffer-image"; //$NON-NLS-1$
501 public void paintControl(final PaintEvent event
) {
504 final int canvasWidth
= fCanvas
.getBounds().width
;
505 final int canvasHeight
= fCanvas
.getBounds().height
;
507 // Make sure we have something to draw upon
508 if (canvasWidth
<= 0 || canvasHeight
<= 0) {
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
);
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);
526 private void formatImage(final GC imageGC
, final Image image
) {
528 if (fScaledData
== null) {
532 final HistogramScaledData scaledData
= new HistogramScaledData(fScaledData
);
535 // Get drawing boundaries
536 final int width
= image
.getBounds().width
;
537 final int height
= image
.getBounds().height
;
539 // Clear the drawing area
540 imageGC
.setBackground(fBackgroundColor
);
541 imageGC
.fillRectangle(0, 0, image
.getBounds().width
+ 1, image
.getBounds().height
+ 1);
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
);
551 // Draw the current event bar
552 final int currentBucket
= scaledData
.fCurrentBucket
;
553 if (currentBucket
>= 0 && currentBucket
< limit
) {
554 drawDelimiter(imageGC
, fCurrentEventColor
, height
, currentBucket
);
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) {
562 lastEventIndex
+= (lastEventIndex
< limit
- 1) ?
1 : 0;
563 drawDelimiter(imageGC
, fLastEventColor
, height
, lastEventIndex
);
564 } catch (final Exception e
) {
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
);
579 // ------------------------------------------------------------------------
581 // ------------------------------------------------------------------------
584 public void keyPressed(final KeyEvent event
) {
585 moveCursor(event
.keyCode
);
589 public void keyReleased(final KeyEvent event
) {
592 // ------------------------------------------------------------------------
594 // ------------------------------------------------------------------------
597 public void mouseDoubleClick(final MouseEvent event
) {
601 public void mouseDown(final MouseEvent event
) {
602 if (fDataModel
.getNbEvents() > 0 && fScaledData
.fLastBucket
>= event
.x
) {
603 fScaledData
.fCurrentBucket
= event
.x
;
604 updateCurrentEventTime();
609 public void mouseUp(final MouseEvent event
) {
612 // ------------------------------------------------------------------------
613 // MouseTrackListener
614 // ------------------------------------------------------------------------
617 public void mouseEnter(final MouseEvent event
) {
621 public void mouseExit(final MouseEvent event
) {
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
);
630 fCanvas
.setToolTipText(null);
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
640 final long endTime
= fScaledData
.getBucketEndTime(index
);
641 final int nbEvents
= (index
>= 0) ? fScaledData
.fData
[index
] : 0;
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();
654 // ------------------------------------------------------------------------
656 // ------------------------------------------------------------------------
659 public void controlMoved(final ControlEvent event
) {
660 fDataModel
.complete();
664 public void controlResized(final ControlEvent event
) {
665 fDataModel
.complete();
668 // ------------------------------------------------------------------------
670 // ------------------------------------------------------------------------
673 * Format the timestamp and update the display
675 * @param signal the incoming signal
679 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal
) {
680 if (fDataModel
.getNbEvents() == 0) {
684 String newTS
= TmfTimestampFormat
.getDefaulTimeFormat().format(fDataModel
.getFirstBucketTime());
685 fTimeRangeStartText
.setText(newTS
);
687 newTS
= TmfTimestampFormat
.getDefaulTimeFormat().format(fDataModel
.getEndTime());
688 fTimeRangeEndText
.setText(newTS
);