1 /*******************************************************************************
2 * Copyright (c) 2009 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 * William Bourque - Initial API and implementation
13 * 2010-07-16 Yuriy Vashchuk - Heritage corrections. Redraw bug correction.
14 * Double Buffering implementation.
15 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
18 import org
.eclipse
.swt
.events
.PaintEvent
;
19 import org
.eclipse
.swt
.events
.PaintListener
;
21 import org
.eclipse
.swt
.graphics
.Color
;
22 import org
.eclipse
.swt
.graphics
.GC
;
23 import org
.eclipse
.swt
.graphics
.Image
;
26 * <b><u>HistogramCanvasPaintListener</u></b>
28 * Implementation of a PaintListener for the need of the HistogramCanvas
31 public class HistogramCanvasPaintListener
implements PaintListener
33 private static ChildrenHistogramCanvas childrenCanvas
= null;
34 protected boolean isFinished
= false;
37 * HistogramCanvasPaintListener default constructor
39 public HistogramCanvasPaintListener() {
43 * HistogramCanvasPaintListener constructor
45 * @param parentCanvas Related canvas
47 public HistogramCanvasPaintListener(ChildrenHistogramCanvas newCanvas
) {
48 childrenCanvas
= newCanvas
;
52 * Function called when the canvas need to redraw.<p>
54 * @param event The generated paint event when redraw is called.
56 private final String DATA_KEY
= "double-buffer-image"; //$NON-NLS-1$
58 public void paintControl(PaintEvent event
) {
60 if (childrenCanvas
.getSize().x
> 0 && childrenCanvas
.getSize().y
> 0) {
61 Image image
= (Image
) childrenCanvas
.getData(DATA_KEY
);
63 // Creates new image only absolutely necessary.
65 || image
.getBounds().width
!= childrenCanvas
.getBounds().width
66 || image
.getBounds().height
!= childrenCanvas
.getBounds().height
) {
70 childrenCanvas
.getBounds().width
,
71 childrenCanvas
.getBounds().height
74 childrenCanvas
.setData(DATA_KEY
, image
);
77 // Initializes the graphics context of the image.
78 GC imageGC
= new GC(image
);
80 // First clear the whole canvas to have a clean section where to draw
81 clearDrawingSection(imageGC
, image
, childrenCanvas
);
83 // If the content is null or has rady to draw we quit the function here
84 if ( (childrenCanvas
.getHistogramContent() != null)
85 && (childrenCanvas
.getHistogramContent().getReadyUpToPosition() != 0) ) {
87 // Call the function that draw the bars
89 drawHistogram(imageGC
, image
);
92 // Pinpoint a position if set
93 if (childrenCanvas
.getHistogramContent().getSelectedEventTimeInWindow() > 0 ) {
94 drawSelectedEventInWindow(imageGC
, image
);
97 // Draws the buffer image onto the canvas.
98 event
.gc
.drawImage(image
, 0, 0);
106 * Clear the drawing section of the canvas<p>
107 * This paint the whole background in EMPTY_BACKGROUND_COLOR, so we have something clean to draw on.
109 * @param imageGC GC content.
110 * @param image Image content.
111 * @param ourCanvas Canvas to clean.
113 public void clearDrawingSection(GC imageGC
, Image image
, HistogramCanvas ourCanvas
) {
115 imageGC
.setBackground(ourCanvas
.getDisplay().getSystemColor(HistogramConstant
.EMPTY_BACKGROUND_COLOR
));
116 imageGC
.fillRectangle(0, 0, image
.getBounds().width
+ 1, image
.getBounds().height
+ 1);
120 // Is it good to put this synchronized?
123 * Draw the histogram bars in the canvas.<p>
124 * Use existing elements in HistogramContent to draw bars on the cancas;
125 * the element table in content need to be populated and have consistent value.
127 * @param imageGC GC content.
128 * @param image image content.
130 public synchronized void drawHistogram(GC imageGC
, Image image
) {
132 // This will be the bottom color for all the bars that wil be draw below.
133 imageGC
.setBackground( new Color( imageGC
.getDevice(), 74, 112, 139) );
136 // Y Position in a canvas is REVERSED, so "0" is on top of the screen and "MAX" is on bottom.
137 // Not very instinctive, isn't it?
139 // Draw a bar from the left (pos X=0) until the pos=(NbBars*barWidth). If space is left, it will be blanked after.
140 for ( int x
= 0; x
< childrenCanvas
.getHistogramContent().getReadyUpToPosition(); x
++) {
141 imageGC
.fillRectangle(
142 childrenCanvas
.getHistogramContent().getBarsWidth() * x
,
143 image
.getBounds().height
- childrenCanvas
.getHistogramContent().getElementByIndex(x
).intervalHeight
,
144 childrenCanvas
.getHistogramContent().getBarsWidth(),
145 childrenCanvas
.getHistogramContent().getElementByIndex(x
).intervalHeight
152 * Draw a certain event selected in the window.<p>
154 * @param imageGC GC content.
155 * @param image image content.
157 public synchronized void drawSelectedEventInWindow(GC imageGC
, Image image
) {
159 final HistogramContent tmpContent
= childrenCanvas
.getHistogramContent();
160 final int tmpBarWidth
= tmpContent
.getBarsWidth();
161 final int position
= tmpContent
.getClosestXPositionFromTimestamp(tmpContent
.getSelectedEventTimeInWindow());
163 // This will be the color for all the bars that will be draw below.
164 imageGC
.setForeground(childrenCanvas
.getDisplay().getSystemColor(HistogramConstant
.SELECTED_EVENT_COLOR
));
165 imageGC
.setLineWidth(HistogramConstant
.SELECTION_LINE_WIDTH
);
167 tmpBarWidth
* position
,
169 tmpBarWidth
* position
,
170 image
.getBounds().height
175 * @param isFinished the flag value
177 public void setIsFinished(boolean isFinished
) {
178 this.isFinished
= isFinished
;