tmf: Fix histogram label background to be theme-friendly
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / Histogram.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Patrick Tasse - Support selection range
14 * Xavier Raynaud - Support multi-trace coloring
15 *******************************************************************************/
16
17 package org.eclipse.linuxtools.tmf.ui.views.histogram;
18
19 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
20 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
21 import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal;
22 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
23 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
24 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampDelta;
25 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
26 import org.eclipse.linuxtools.tmf.ui.views.TmfView;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ControlEvent;
30 import org.eclipse.swt.events.ControlListener;
31 import org.eclipse.swt.events.DisposeEvent;
32 import org.eclipse.swt.events.DisposeListener;
33 import org.eclipse.swt.events.KeyEvent;
34 import org.eclipse.swt.events.KeyListener;
35 import org.eclipse.swt.events.MouseEvent;
36 import org.eclipse.swt.events.MouseListener;
37 import org.eclipse.swt.events.MouseMoveListener;
38 import org.eclipse.swt.events.MouseTrackListener;
39 import org.eclipse.swt.events.MouseWheelListener;
40 import org.eclipse.swt.events.PaintEvent;
41 import org.eclipse.swt.events.PaintListener;
42 import org.eclipse.swt.graphics.Color;
43 import org.eclipse.swt.graphics.Font;
44 import org.eclipse.swt.graphics.FontData;
45 import org.eclipse.swt.graphics.GC;
46 import org.eclipse.swt.graphics.Image;
47 import org.eclipse.swt.layout.FillLayout;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Canvas;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Display;
53 import org.eclipse.swt.widgets.Label;
54
55 /**
56 * Re-usable histogram widget.
57 *
58 * It has the following features:
59 * <ul>
60 * <li>Y-axis labels displaying min/max count values
61 * <li>X-axis labels displaying time range
62 * <li>a histogram displaying the distribution of values over time (note that
63 * the histogram might not necessarily fill the whole canvas)
64 * </ul>
65 * The widget also has 2 'markers' to identify:
66 * <ul>
67 * <li>a red dashed line over the bar that contains the currently selected event
68 * <li>a dark red dashed line that delimits the right end of the histogram (if
69 * it doesn't fill the canvas)
70 * </ul>
71 * Clicking on the histogram will select the current event at the mouse
72 * location.
73 * <p>
74 * Once the histogram is selected, there is some limited keyboard support:
75 * <ul>
76 * <li>Home: go to the first histogram bar
77 * <li>End: go to the last histogram bar
78 * <li>Left: go to the previous histogram
79 * <li>Right: go to the next histogram bar
80 * </ul>
81 * Finally, when the mouse hovers over the histogram, a tool tip showing the
82 * following information about the corresponding histogram bar time range:
83 * <ul>
84 * <li>start of the time range
85 * <li>end of the time range
86 * <li>number of events in that time range
87 * </ul>
88 *
89 * @version 1.1
90 * @author Francois Chouinard
91 */
92 public abstract class Histogram implements ControlListener, PaintListener, KeyListener, MouseListener, MouseMoveListener, MouseTrackListener, IHistogramModelListener {
93
94 // ------------------------------------------------------------------------
95 // Constants
96 // ------------------------------------------------------------------------
97
98 // Histogram colors
99
100 // System colors, they do not need to be disposed
101 private final Color fBackgroundColor = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
102 private final Color fSelectionForegroundColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
103 private final Color fSelectionBackgroundColor = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
104 private final Color fLastEventColor = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED);
105
106 // Application colors, they need to be disposed
107 private final Color[] fHistoBarColors = new Color[] {new Color(Display.getDefault(), 90, 90, 255), // blue
108 new Color(Display.getDefault(), 0, 240, 0), // green
109 new Color(Display.getDefault(), 255, 0, 0), // red
110 new Color(Display.getDefault(), 0, 255, 255), // cyan
111 new Color(Display.getDefault(), 255, 80, 255), // magenta
112 new Color(Display.getDefault(), 200, 200, 0), // yellow
113 new Color(Display.getDefault(), 200, 150, 0), // brown
114 new Color(Display.getDefault(), 150, 255, 150), // light green
115 new Color(Display.getDefault(), 200, 80, 80), // dark red
116 new Color(Display.getDefault(), 30, 150, 150), // dark cyan
117 new Color(Display.getDefault(), 200, 200, 255), // light blue
118 new Color(Display.getDefault(), 0, 120, 0), // dark green
119 new Color(Display.getDefault(), 255, 150, 150), // lighter red
120 new Color(Display.getDefault(), 140, 80, 140), // dark magenta
121 new Color(Display.getDefault(), 150, 100, 50), // brown
122 new Color(Display.getDefault(), 255, 80, 80), // light red
123 new Color(Display.getDefault(), 200, 200, 200), // light grey
124 new Color(Display.getDefault(), 255, 200, 80), // orange
125 new Color(Display.getDefault(), 255, 255, 80), // pale yellow
126 new Color(Display.getDefault(), 255, 200, 200), // pale red
127 new Color(Display.getDefault(), 255, 200, 255), // pale magenta
128 new Color(Display.getDefault(), 255, 255, 200), // pale pale yellow
129 new Color(Display.getDefault(), 200, 255, 255), // pale pale blue
130 };
131 private final Color fTimeRangeColor = new Color(Display.getCurrent(), 255, 128, 0);
132 private final Color fLostEventColor = new Color(Display.getCurrent(), 208, 62, 120);
133
134 // Drag states
135 /**
136 * No drag in progress
137 * @since 2.2
138 */
139 protected final int DRAG_NONE = 0;
140 /**
141 * Drag the selection
142 * @since 2.2
143 */
144 protected final int DRAG_SELECTION = 1;
145 /**
146 * Drag the time range
147 * @since 2.2
148 */
149 protected final int DRAG_RANGE = 2;
150 /**
151 * Drag the zoom range
152 * @since 2.2
153 */
154 protected final int DRAG_ZOOM = 3;
155
156 // ------------------------------------------------------------------------
157 // Attributes
158 // ------------------------------------------------------------------------
159
160 /**
161 * The parent TMF view.
162 */
163 protected TmfView fParentView;
164
165 private Composite fComposite;
166 private Font fFont;
167
168 // Histogram text fields
169 private Label fMaxNbEventsLabel;
170 private Label fMinNbEventsLabel;
171 private Label fTimeRangeStartLabel;
172 private Label fTimeRangeEndLabel;
173
174 /**
175 * Histogram drawing area
176 */
177 protected Canvas fCanvas;
178
179 /**
180 * The histogram data model.
181 */
182 protected final HistogramDataModel fDataModel;
183
184 /**
185 * The histogram data model scaled to current resolution and screen width.
186 */
187 protected HistogramScaledData fScaledData;
188
189 /**
190 * The current event value
191 */
192 protected long fCurrentEventTime = 0L;
193
194 /**
195 * The current selection begin time
196 */
197 private long fSelectionBegin = 0L;
198
199 /**
200 * The current selection end time
201 */
202 private long fSelectionEnd = 0L;
203
204 /**
205 * The drag state
206 * @see #DRAG_NONE
207 * @see #DRAG_SELECTION
208 * @see #DRAG_RANGE
209 * @see #DRAG_ZOOM
210 * @since 2.2
211 */
212 protected int fDragState = DRAG_NONE;
213
214 /**
215 * The button that started a mouse drag, or 0 if no drag in progress
216 * @since 2.2
217 */
218 protected int fDragButton = 0;
219
220 /**
221 * The bucket display offset
222 */
223 private int fOffset = 0;
224
225 /**
226 * show the traces or not
227 * @since 3.0
228 */
229 static boolean showTraces = true;
230
231 // ------------------------------------------------------------------------
232 // Construction
233 // ------------------------------------------------------------------------
234
235 /**
236 * Full constructor.
237 *
238 * @param view A reference to the parent TMF view.
239 * @param parent A parent composite
240 */
241 public Histogram(final TmfView view, final Composite parent) {
242 fParentView = view;
243
244 fComposite = createWidget(parent);
245 fDataModel = new HistogramDataModel();
246 fDataModel.addHistogramListener(this);
247 clear();
248
249 fCanvas.addControlListener(this);
250 fCanvas.addPaintListener(this);
251 fCanvas.addKeyListener(this);
252 fCanvas.addMouseListener(this);
253 fCanvas.addMouseTrackListener(this);
254 fCanvas.addMouseMoveListener(this);
255
256 TmfSignalManager.register(this);
257 }
258
259 /**
260 * Dispose resources and unregisters listeners.
261 */
262 public void dispose() {
263 TmfSignalManager.deregister(this);
264 fLostEventColor.dispose();
265 for (Color c : fHistoBarColors) {
266 c.dispose();
267 }
268 fTimeRangeColor.dispose();
269 fFont.dispose();
270 fDataModel.removeHistogramListener(this);
271 fDataModel.dispose();
272 }
273
274 private Composite createWidget(final Composite parent) {
275
276 fFont = adjustFont(parent);
277
278 final int initalWidth = 10;
279
280 // --------------------------------------------------------------------
281 // Define the histogram
282 // --------------------------------------------------------------------
283
284 final GridLayout gridLayout = new GridLayout();
285 gridLayout.numColumns = 3;
286 gridLayout.marginHeight = 0;
287 gridLayout.marginWidth = 0;
288 gridLayout.marginTop = 0;
289 gridLayout.horizontalSpacing = 0;
290 gridLayout.verticalSpacing = 0;
291 gridLayout.marginLeft = 0;
292 gridLayout.marginRight = 0;
293 final Composite composite = new Composite(parent, SWT.FILL);
294 composite.setLayout(gridLayout);
295
296 // Use all the horizontal space
297 GridData gridData = new GridData();
298 gridData.horizontalAlignment = SWT.FILL;
299 gridData.verticalAlignment = SWT.FILL;
300 gridData.grabExcessHorizontalSpace = true;
301 gridData.grabExcessVerticalSpace = true;
302 composite.setLayoutData(gridData);
303
304 // Y-axis max event
305 gridData = new GridData();
306 gridData.horizontalAlignment = SWT.RIGHT;
307 gridData.verticalAlignment = SWT.TOP;
308 fMaxNbEventsLabel = new Label(composite, SWT.RIGHT);
309 fMaxNbEventsLabel.setFont(fFont);
310 fMaxNbEventsLabel.setText("0"); //$NON-NLS-1$
311 fMaxNbEventsLabel.setLayoutData(gridData);
312
313 // Histogram itself
314 Composite canvasComposite = new Composite(composite, SWT.BORDER);
315 gridData = new GridData();
316 gridData.horizontalSpan = 2;
317 gridData.verticalSpan = 2;
318 gridData.horizontalAlignment = SWT.FILL;
319 gridData.verticalAlignment = SWT.FILL;
320 gridData.heightHint = 0;
321 gridData.widthHint = 0;
322 gridData.grabExcessHorizontalSpace = true;
323 gridData.grabExcessVerticalSpace = true;
324 canvasComposite.setLayoutData(gridData);
325 canvasComposite.setLayout(new FillLayout());
326 fCanvas = new Canvas(canvasComposite, SWT.DOUBLE_BUFFERED);
327 fCanvas.addDisposeListener(new DisposeListener() {
328 @Override
329 public void widgetDisposed(DisposeEvent e) {
330 Object image = fCanvas.getData(IMAGE_KEY);
331 if (image instanceof Image) {
332 ((Image) image).dispose();
333 }
334 }
335 });
336
337 // Y-axis min event (always 0...)
338 gridData = new GridData();
339 gridData.horizontalAlignment = SWT.RIGHT;
340 gridData.verticalAlignment = SWT.BOTTOM;
341 fMinNbEventsLabel = new Label(composite, SWT.RIGHT);
342 fMinNbEventsLabel.setFont(fFont);
343 fMinNbEventsLabel.setText("0"); //$NON-NLS-1$
344 fMinNbEventsLabel.setLayoutData(gridData);
345
346 // Dummy cell
347 gridData = new GridData(initalWidth, SWT.DEFAULT);
348 gridData.horizontalAlignment = SWT.RIGHT;
349 gridData.verticalAlignment = SWT.BOTTOM;
350 final Label dummyLabel = new Label(composite, SWT.NONE);
351 dummyLabel.setLayoutData(gridData);
352
353 // Window range start time
354 gridData = new GridData();
355 gridData.horizontalAlignment = SWT.LEFT;
356 gridData.verticalAlignment = SWT.BOTTOM;
357 fTimeRangeStartLabel = new Label(composite, SWT.NONE);
358 fTimeRangeStartLabel.setFont(fFont);
359 fTimeRangeStartLabel.setLayoutData(gridData);
360
361 // Window range end time
362 gridData = new GridData();
363 gridData.horizontalAlignment = SWT.RIGHT;
364 gridData.verticalAlignment = SWT.BOTTOM;
365 fTimeRangeEndLabel = new Label(composite, SWT.NONE);
366 fTimeRangeEndLabel.setFont(fFont);
367 fTimeRangeEndLabel.setLayoutData(gridData);
368
369 return composite;
370 }
371
372 private static Font adjustFont(final Composite composite) {
373 // Reduce font size for a more pleasing rendering
374 final int fontSizeAdjustment = -2;
375 final Font font = composite.getFont();
376 final FontData fontData = font.getFontData()[0];
377 return new Font(font.getDevice(), fontData.getName(), fontData.getHeight() + fontSizeAdjustment, fontData.getStyle());
378 }
379
380 // ------------------------------------------------------------------------
381 // Accessors
382 // ------------------------------------------------------------------------
383
384 /**
385 * Returns the start time (equal first bucket time)
386 * @return the start time.
387 */
388 public long getStartTime() {
389 return fDataModel.getFirstBucketTime();
390 }
391
392 /**
393 * Returns the end time.
394 * @return the end time.
395 */
396 public long getEndTime() {
397 return fDataModel.getEndTime();
398 }
399
400 /**
401 * Returns the time limit (end of last bucket)
402 * @return the time limit.
403 */
404 public long getTimeLimit() {
405 return fDataModel.getTimeLimit();
406 }
407
408 /**
409 * Returns a data model reference.
410 * @return data model.
411 */
412 public HistogramDataModel getDataModel() {
413 return fDataModel;
414 }
415
416 /**
417 * Set the max number events to be displayed
418 *
419 * @param maxNbEvents
420 * the maximum number of events
421 */
422 void setMaxNbEvents(long maxNbEvents) {
423 fMaxNbEventsLabel.setText(Long.toString(maxNbEvents));
424 fMaxNbEventsLabel.getParent().layout();
425 fCanvas.redraw();
426 }
427
428 /**
429 * Return <code>true</code> if the traces must be displayed in the histogram,
430 * <code>false</code> otherwise.
431 * @return whether the traces should be displayed
432 * @since 3.0
433 */
434 public boolean showTraces() {
435 return showTraces && fDataModel.getNbTraces() < getMaxNbTraces();
436 }
437
438 /**
439 * Returns the maximum number of traces the histogram can display with separate colors.
440 * If there is more traces, histogram will use only one color to display them.
441 * @return the maximum number of traces the histogram can display.
442 * @since 3.0
443 */
444 public int getMaxNbTraces() {
445 return fHistoBarColors.length;
446 }
447
448 /**
449 * Returns the color used to display the trace at the given index.
450 * @param traceIndex a trace index
451 * @return a {@link Color}
452 * @since 3.0
453 */
454 public Color getTraceColor(int traceIndex) {
455 return fHistoBarColors[traceIndex % fHistoBarColors.length];
456 }
457
458 // ------------------------------------------------------------------------
459 // Operations
460 // ------------------------------------------------------------------------
461 /**
462 * Updates the time range.
463 * @param startTime A start time
464 * @param endTime A end time.
465 */
466 public void updateTimeRange(long startTime, long endTime) {
467 if (fDragState == DRAG_NONE) {
468 ((HistogramView) fParentView).updateTimeRange(startTime, endTime);
469 }
470 }
471
472 /**
473 * Clear the histogram and reset the data
474 */
475 public void clear() {
476 fDataModel.clear();
477 if (fDragState == DRAG_SELECTION) {
478 updateSelectionTime();
479 }
480 fDragState = DRAG_NONE;
481 fDragButton = 0;
482 synchronized (fDataModel) {
483 fScaledData = null;
484 }
485 }
486
487 /**
488 * Sets the current selection time range and refresh the display
489 *
490 * @param beginTime The begin time of the current selection
491 * @param endTime The end time of the current selection
492 * @since 2.1
493 */
494 public void setSelection(final long beginTime, final long endTime) {
495 fSelectionBegin = (beginTime > 0) ? beginTime : 0;
496 fSelectionEnd = (endTime > 0) ? endTime : 0;
497 fDataModel.setSelectionNotifyListeners(beginTime, endTime);
498 }
499
500 /**
501 * Computes the timestamp of the bucket at [offset]
502 *
503 * @param offset offset from the left on the histogram
504 * @return the start timestamp of the corresponding bucket
505 */
506 public synchronized long getTimestamp(final int offset) {
507 assert offset > 0 && offset < fScaledData.fWidth;
508 try {
509 return fScaledData.fFirstBucketTime + fScaledData.fBucketDuration * offset;
510 } catch (final Exception e) {
511 return 0; // TODO: Fix that racing condition (NPE)
512 }
513 }
514
515 /**
516 * Computes the offset of the timestamp in the histogram
517 *
518 * @param timestamp the timestamp
519 * @return the offset of the corresponding bucket (-1 if invalid)
520 */
521 public synchronized int getOffset(final long timestamp) {
522 if (timestamp < fDataModel.getFirstBucketTime() || timestamp > fDataModel.getEndTime()) {
523 return -1;
524 }
525 return (int) ((timestamp - fDataModel.getFirstBucketTime()) / fScaledData.fBucketDuration);
526 }
527
528 /**
529 * Set the bucket display offset
530 *
531 * @param offset
532 * the bucket display offset
533 * @since 2.2
534 */
535 protected void setOffset(final int offset) {
536 fOffset = offset;
537 }
538
539 /**
540 * Move the currently selected bar cursor to a non-empty bucket.
541 *
542 * @param keyCode the SWT key code
543 */
544 protected void moveCursor(final int keyCode) {
545
546 int index;
547 switch (keyCode) {
548
549 case SWT.HOME:
550 index = 0;
551 while (index < fScaledData.fLastBucket && fScaledData.fData[index].isEmpty()) {
552 index++;
553 }
554 if (index < fScaledData.fLastBucket) {
555 fScaledData.fSelectionBeginBucket = index;
556 }
557 break;
558
559 case SWT.ARROW_RIGHT:
560 index = Math.max(0, fScaledData.fSelectionBeginBucket + 1);
561 while (index < fScaledData.fWidth && fScaledData.fData[index].isEmpty()) {
562 index++;
563 }
564 if (index < fScaledData.fLastBucket) {
565 fScaledData.fSelectionBeginBucket = index;
566 }
567 break;
568
569 case SWT.END:
570 index = fScaledData.fLastBucket;
571 while (index >= 0 && fScaledData.fData[index].isEmpty()) {
572 index--;
573 }
574 if (index >= 0) {
575 fScaledData.fSelectionBeginBucket = index;
576 }
577 break;
578
579 case SWT.ARROW_LEFT:
580 index = Math.min(fScaledData.fLastBucket - 1, fScaledData.fSelectionBeginBucket - 1);
581 while (index >= 0 && fScaledData.fData[index].isEmpty()) {
582 index--;
583 }
584 if (index >= 0) {
585 fScaledData.fSelectionBeginBucket = index;
586 }
587 break;
588
589 default:
590 return;
591 }
592
593 fScaledData.fSelectionEndBucket = fScaledData.fSelectionBeginBucket;
594 fSelectionBegin = getTimestamp(fScaledData.fSelectionBeginBucket);
595 fSelectionEnd = fSelectionBegin;
596 updateSelectionTime();
597 }
598
599 /**
600 * Refresh the histogram display
601 */
602 @Override
603 public void modelUpdated() {
604 if (!fCanvas.isDisposed() && fCanvas.getDisplay() != null) {
605 fCanvas.getDisplay().asyncExec(new Runnable() {
606 @Override
607 public void run() {
608 if (!fCanvas.isDisposed()) {
609 // Retrieve and normalize the data
610 final int canvasWidth = fCanvas.getBounds().width;
611 final int canvasHeight = fCanvas.getBounds().height;
612 if (canvasWidth <= 0 || canvasHeight <= 0) {
613 return;
614 }
615 fDataModel.setSelection(fSelectionBegin, fSelectionEnd);
616 fScaledData = fDataModel.scaleTo(canvasWidth, canvasHeight, 1);
617 synchronized (fDataModel) {
618 if (fScaledData != null) {
619 fCanvas.redraw();
620 // Display histogram and update X-,Y-axis labels
621 updateRangeTextControls();
622 long maxNbEvents = HistogramScaledData.hideLostEvents ? fScaledData.fMaxValue : fScaledData.fMaxCombinedValue;
623 fMaxNbEventsLabel.setText(Long.toString(maxNbEvents));
624 // The Y-axis area might need to be re-sized
625 GridData gd = (GridData) fMaxNbEventsLabel.getLayoutData();
626 gd.widthHint = Math.max(gd.widthHint, fMaxNbEventsLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
627 fMaxNbEventsLabel.getParent().layout();
628 }
629 }
630 }
631 }
632 });
633 }
634 }
635
636 /**
637 * Add a mouse wheel listener to the histogram
638 * @param listener the mouse wheel listener
639 * @since 2.0
640 */
641 public void addMouseWheelListener(MouseWheelListener listener) {
642 fCanvas.addMouseWheelListener(listener);
643 }
644
645 /**
646 * Remove a mouse wheel listener from the histogram
647 * @param listener the mouse wheel listener
648 * @since 2.0
649 */
650 public void removeMouseWheelListener(MouseWheelListener listener) {
651 fCanvas.removeMouseWheelListener(listener);
652 }
653
654 // ------------------------------------------------------------------------
655 // Helper functions
656 // ------------------------------------------------------------------------
657
658 private void updateSelectionTime() {
659 if (fSelectionBegin > fSelectionEnd) {
660 long end = fSelectionBegin;
661 fSelectionBegin = fSelectionEnd;
662 fSelectionEnd = end;
663 }
664 ((HistogramView) fParentView).updateSelectionTime(fSelectionBegin, fSelectionEnd);
665 }
666
667 /**
668 * Update the range text controls
669 */
670 private void updateRangeTextControls() {
671 if (fDataModel.getStartTime() < fDataModel.getEndTime()) {
672 fTimeRangeStartLabel.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getStartTime()));
673 fTimeRangeEndLabel.setText(TmfTimestampFormat.getDefaulTimeFormat().format(fDataModel.getEndTime()));
674 } else {
675 fTimeRangeStartLabel.setText(""); //$NON-NLS-1$
676 fTimeRangeEndLabel.setText(""); //$NON-NLS-1$
677 }
678 }
679
680 // ------------------------------------------------------------------------
681 // PaintListener
682 // ------------------------------------------------------------------------
683 /**
684 * Image key string for the canvas.
685 */
686 protected final String IMAGE_KEY = "double-buffer-image"; //$NON-NLS-1$
687
688 @Override
689 public void paintControl(final PaintEvent event) {
690
691 // Get the geometry
692 final int canvasWidth = fCanvas.getBounds().width;
693 final int canvasHeight = fCanvas.getBounds().height;
694
695 // Make sure we have something to draw upon
696 if (canvasWidth <= 0 || canvasHeight <= 0) {
697 return;
698 }
699
700 // Retrieve image; re-create only if necessary
701 Image image = (Image) fCanvas.getData(IMAGE_KEY);
702 if (image == null || image.getBounds().width != canvasWidth || image.getBounds().height != canvasHeight) {
703 if (image != null) {
704 image.dispose();
705 }
706 image = new Image(event.display, canvasWidth, canvasHeight);
707 fCanvas.setData(IMAGE_KEY, image);
708 }
709
710 // Draw the histogram on its canvas
711 final GC imageGC = new GC(image);
712 formatImage(imageGC, image);
713 event.gc.drawImage(image, 0, 0);
714 imageGC.dispose();
715 }
716
717 private void formatImage(final GC imageGC, final Image image) {
718
719 if (fScaledData == null) {
720 return;
721 }
722
723 final HistogramScaledData scaledData = new HistogramScaledData(fScaledData);
724
725 try {
726 // Get drawing boundaries
727 final int width = image.getBounds().width;
728 final int height = image.getBounds().height;
729
730 // Clear the drawing area
731 imageGC.setBackground(fBackgroundColor);
732 imageGC.fillRectangle(0, 0, image.getBounds().width + 1, image.getBounds().height + 1);
733
734 // Draw the histogram bars
735 final int limit = width < scaledData.fWidth ? width : scaledData.fWidth;
736 double factor = HistogramScaledData.hideLostEvents ? scaledData.fScalingFactor : scaledData.fScalingFactorCombined;
737 final boolean showTracesColors = showTraces();
738 for (int i = 0; i < limit; i++) {
739 HistogramBucket hb = scaledData.fData[i];
740 int totalNbEvents = hb.getNbEvents();
741 int value = (int) Math.ceil(totalNbEvents * factor);
742 int x = i + fOffset;
743
744 // in Linux, the last pixel in a line is not drawn,
745 // so draw lost events first, one pixel too far
746 if (!HistogramScaledData.hideLostEvents) {
747 imageGC.setForeground(fLostEventColor);
748 final int lostEventValue = (int) Math.ceil(scaledData.fLostEventsData[i] * factor);
749 if (lostEventValue != 0) {
750 // drawing a line is inclusive, so we should remove 1 from y2
751 // but we don't because Linux
752 imageGC.drawLine(x, height - value - lostEventValue, x, height - value);
753 }
754 }
755
756 // then draw normal events second, to overwrite that extra pixel
757 if (!hb.isEmpty()) {
758 if (showTracesColors) {
759 for (int traceIndex = 0; traceIndex < hb.getNbTraces(); traceIndex++) {
760 int nbEventsForTrace = hb.getNbEvent(traceIndex);
761 if (nbEventsForTrace > 0) {
762 Color c = fHistoBarColors[traceIndex % fHistoBarColors.length];
763 imageGC.setForeground(c);
764 imageGC.drawLine(x, height - value, x, height);
765 totalNbEvents -= nbEventsForTrace;
766 value = (int) Math.ceil(totalNbEvents * scaledData.fScalingFactor);
767 }
768 }
769 } else {
770 Color c = fHistoBarColors[0];
771 imageGC.setForeground(c);
772 imageGC.drawLine(x, height - value, x, height);
773 }
774 }
775 }
776
777 // Draw the selection bars
778 int alpha = imageGC.getAlpha();
779 imageGC.setAlpha(100);
780 imageGC.setForeground(fSelectionForegroundColor);
781 imageGC.setBackground(fSelectionBackgroundColor);
782 final int beginBucket = scaledData.fSelectionBeginBucket + fOffset;
783 if (beginBucket >= 0 && beginBucket < limit) {
784 imageGC.drawLine(beginBucket, 0, beginBucket, height);
785 }
786 final int endBucket = scaledData.fSelectionEndBucket + fOffset;
787 if (endBucket >= 0 && endBucket < limit && endBucket != beginBucket) {
788 imageGC.drawLine(endBucket, 0, endBucket, height);
789 }
790 if (Math.abs(endBucket - beginBucket) > 1) {
791 if (endBucket > beginBucket) {
792 imageGC.fillRectangle(beginBucket + 1, 0, endBucket - beginBucket - 1, height);
793 } else {
794 imageGC.fillRectangle(endBucket + 1, 0, beginBucket - endBucket - 1, height);
795 }
796 }
797 imageGC.setAlpha(alpha);
798
799 // Add a dashed line as a delimiter
800 int delimiterIndex = (int) ((getDataModel().getEndTime() - scaledData.getFirstBucketTime()) / scaledData.fBucketDuration) + 1;
801 drawDelimiter(imageGC, fLastEventColor, height, delimiterIndex);
802
803 // Fill the area to the right of delimiter with background color
804 imageGC.setBackground(fComposite.getBackground());
805 imageGC.fillRectangle(delimiterIndex + 1, 0, width - (delimiterIndex + 1), height);
806
807 } catch (final Exception e) {
808 // Do nothing
809 }
810 }
811
812 private static void drawDelimiter(final GC imageGC, final Color color,
813 final int height, final int index) {
814 imageGC.setBackground(color);
815 final int dash = height / 4;
816 imageGC.fillRectangle(index, 0 * dash, 1, dash - 1);
817 imageGC.fillRectangle(index, 1 * dash, 1, dash - 1);
818 imageGC.fillRectangle(index, 2 * dash, 1, dash - 1);
819 imageGC.fillRectangle(index, 3 * dash, 1, height - 3 * dash);
820 }
821
822 /**
823 * Draw a time range window
824 *
825 * @param imageGC
826 * the GC
827 * @param rangeStartTime
828 * the range start time
829 * @param rangeDuration
830 * the range duration
831 * @since 2.2
832 */
833 protected void drawTimeRangeWindow(GC imageGC, long rangeStartTime, long rangeDuration) {
834
835 if (fScaledData == null) {
836 return;
837 }
838
839 // Map times to histogram coordinates
840 long bucketSpan = Math.max(fScaledData.fBucketDuration, 1);
841 long startTime = Math.min(rangeStartTime, rangeStartTime + rangeDuration);
842 int rangeWidth = (int) (Math.abs(rangeDuration) / bucketSpan);
843
844 int left = (int) ((startTime - fDataModel.getFirstBucketTime()) / bucketSpan);
845 int right = left + rangeWidth;
846 int center = (left + right) / 2;
847 int height = fCanvas.getSize().y;
848 int arc = Math.min(15, rangeWidth);
849
850 // Draw the selection window
851 imageGC.setForeground(fTimeRangeColor);
852 imageGC.setLineWidth(1);
853 imageGC.setLineStyle(SWT.LINE_SOLID);
854 imageGC.drawRoundRectangle(left, 0, rangeWidth, height - 1, arc, arc);
855
856 // Fill the selection window
857 imageGC.setBackground(fTimeRangeColor);
858 imageGC.setAlpha(35);
859 imageGC.fillRoundRectangle(left + 1, 1, rangeWidth - 1, height - 2, arc, arc);
860 imageGC.setAlpha(255);
861
862 // Draw the cross hair
863 imageGC.setForeground(fTimeRangeColor);
864 imageGC.setLineWidth(1);
865 imageGC.setLineStyle(SWT.LINE_SOLID);
866
867 int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2;
868 imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
869 imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth);
870 }
871
872 // ------------------------------------------------------------------------
873 // KeyListener
874 // ------------------------------------------------------------------------
875
876 @Override
877 public void keyPressed(final KeyEvent event) {
878 moveCursor(event.keyCode);
879 }
880
881 @Override
882 public void keyReleased(final KeyEvent event) {
883 }
884
885 // ------------------------------------------------------------------------
886 // MouseListener
887 // ------------------------------------------------------------------------
888
889 @Override
890 public void mouseDoubleClick(final MouseEvent event) {
891 }
892
893 @Override
894 public void mouseDown(final MouseEvent event) {
895 if (fScaledData != null && event.button == 1 && fDragState == DRAG_NONE && fDataModel.getStartTime() < fDataModel.getEndTime()) {
896 fDragState = DRAG_SELECTION;
897 fDragButton = event.button;
898 if ((event.stateMask & SWT.MODIFIER_MASK) == SWT.SHIFT) {
899 if (Math.abs(event.x - fScaledData.fSelectionBeginBucket) < Math.abs(event.x - fScaledData.fSelectionEndBucket)) {
900 fScaledData.fSelectionBeginBucket = fScaledData.fSelectionEndBucket;
901 fSelectionBegin = fSelectionEnd;
902 }
903 fSelectionEnd = Math.min(getTimestamp(event.x), getEndTime());
904 fScaledData.fSelectionEndBucket = (int) ((fSelectionEnd - fScaledData.fFirstBucketTime) / fScaledData.fBucketDuration);
905 } else {
906 fSelectionBegin = Math.min(getTimestamp(event.x), getEndTime());
907 fScaledData.fSelectionBeginBucket = (int) ((fSelectionBegin - fScaledData.fFirstBucketTime) / fScaledData.fBucketDuration);
908 fSelectionEnd = fSelectionBegin;
909 fScaledData.fSelectionEndBucket = fScaledData.fSelectionBeginBucket;
910 }
911 fCanvas.redraw();
912 }
913 }
914
915 @Override
916 public void mouseUp(final MouseEvent event) {
917 if (fDragState == DRAG_SELECTION && event.button == fDragButton) {
918 fDragState = DRAG_NONE;
919 fDragButton = 0;
920 updateSelectionTime();
921 }
922 }
923
924 // ------------------------------------------------------------------------
925 // MouseMoveListener
926 // ------------------------------------------------------------------------
927
928 /**
929 * @since 2.2
930 */
931 @Override
932 public void mouseMove(MouseEvent event) {
933 if (fDragState == DRAG_SELECTION && fDataModel.getStartTime() < fDataModel.getEndTime()) {
934 fSelectionEnd = Math.max(getStartTime(), Math.min(getEndTime(), getTimestamp(event.x)));
935 fScaledData.fSelectionEndBucket = (int) ((fSelectionEnd - fScaledData.fFirstBucketTime) / fScaledData.fBucketDuration);
936 fCanvas.redraw();
937 }
938 }
939
940 // ------------------------------------------------------------------------
941 // MouseTrackListener
942 // ------------------------------------------------------------------------
943
944 @Override
945 public void mouseEnter(final MouseEvent event) {
946 }
947
948 @Override
949 public void mouseExit(final MouseEvent event) {
950 }
951
952 @Override
953 public void mouseHover(final MouseEvent event) {
954 if (fDataModel.getStartTime() < fDataModel.getEndTime() && fScaledData != null) {
955 int delimiterIndex = (int) ((fDataModel.getEndTime() - fScaledData.getFirstBucketTime()) / fScaledData.fBucketDuration) + 1;
956 if (event.x < delimiterIndex) {
957 final String tooltip = formatToolTipLabel(event.x - fOffset);
958 fCanvas.setToolTipText(tooltip);
959 return;
960 }
961 }
962 fCanvas.setToolTipText(null);
963 }
964
965 private String formatToolTipLabel(final int index) {
966 long startTime = fScaledData.getBucketStartTime(index);
967 // negative values are possible if time values came into the model in decreasing order
968 if (startTime < 0) {
969 startTime = 0;
970 }
971 final long endTime = fScaledData.getBucketEndTime(index);
972 final int nbEvents = (index >= 0) ? fScaledData.fData[index].getNbEvents() : 0;
973 final String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
974 final StringBuffer buffer = new StringBuffer();
975 int selectionBeginBucket = Math.min(fScaledData.fSelectionBeginBucket, fScaledData.fSelectionEndBucket);
976 int selectionEndBucket = Math.max(fScaledData.fSelectionBeginBucket, fScaledData.fSelectionEndBucket);
977 if (selectionBeginBucket <= index && index <= selectionEndBucket && fSelectionBegin != fSelectionEnd) {
978 TmfTimestampDelta delta = new TmfTimestampDelta(Math.abs(fSelectionEnd - fSelectionBegin), ITmfTimestamp.NANOSECOND_SCALE);
979 buffer.append(NLS.bind(Messages.Histogram_selectionSpanToolTip, delta.toString()));
980 buffer.append(newLine);
981 }
982 buffer.append(NLS.bind(Messages.Histogram_bucketRangeToolTip,
983 new TmfTimestamp(startTime, ITmfTimestamp.NANOSECOND_SCALE).toString(),
984 new TmfTimestamp(endTime, ITmfTimestamp.NANOSECOND_SCALE).toString()));
985 buffer.append(newLine);
986 buffer.append(NLS.bind(Messages.Histogram_eventCountToolTip, nbEvents));
987 if (!HistogramScaledData.hideLostEvents) {
988 final int nbLostEvents = (index >= 0) ? fScaledData.fLostEventsData[index] : 0;
989 buffer.append(newLine);
990 buffer.append(NLS.bind(Messages.Histogram_lostEventCountToolTip, nbLostEvents));
991 }
992 return buffer.toString();
993 }
994
995 // ------------------------------------------------------------------------
996 // ControlListener
997 // ------------------------------------------------------------------------
998
999 @Override
1000 public void controlMoved(final ControlEvent event) {
1001 fDataModel.complete();
1002 }
1003
1004 @Override
1005 public void controlResized(final ControlEvent event) {
1006 fDataModel.complete();
1007 }
1008
1009 // ------------------------------------------------------------------------
1010 // Signal Handlers
1011 // ------------------------------------------------------------------------
1012
1013 /**
1014 * Format the timestamp and update the display
1015 *
1016 * @param signal
1017 * the incoming signal
1018 * @since 2.0
1019 */
1020 @TmfSignalHandler
1021 public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) {
1022 updateRangeTextControls();
1023
1024 fComposite.layout();
1025 }
1026
1027 }
This page took 0.052952 seconds and 6 git commands to generate.