2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / HistogramCanvasPaintListener.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 * William Bourque - Initial API and implementation
11 *
12 * Modifications:
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;
17
18 import org.eclipse.swt.events.PaintEvent;
19 import org.eclipse.swt.events.PaintListener;
20
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Image;
24
25 /**
26 * <b><u>HistogramCanvasPaintListener</u></b>
27 * <p>
28 * Implementation of a PaintListener for the need of the HistogramCanvas
29 * <p>
30 */
31 public class HistogramCanvasPaintListener implements PaintListener
32 {
33 private static ChildrenHistogramCanvas childrenCanvas = null;
34 protected boolean isFinished = false;
35
36 /**
37 * HistogramCanvasPaintListener default constructor
38 */
39 public HistogramCanvasPaintListener() {
40 }
41
42 /**
43 * HistogramCanvasPaintListener constructor
44 *
45 * @param parentCanvas Related canvas
46 */
47 public HistogramCanvasPaintListener(ChildrenHistogramCanvas newCanvas) {
48 childrenCanvas = newCanvas;
49 }
50
51 /**
52 * Function called when the canvas need to redraw.<p>
53 *
54 * @param event The generated paint event when redraw is called.
55 */
56 private final String DATA_KEY = "double-buffer-image"; //$NON-NLS-1$
57 @Override
58 public void paintControl(PaintEvent event) {
59
60 if (childrenCanvas.getSize().x > 0 && childrenCanvas.getSize().y > 0) {
61 Image image = (Image) childrenCanvas.getData(DATA_KEY);
62
63 // Creates new image only absolutely necessary.
64 if (image == null
65 || image.getBounds().width != childrenCanvas.getBounds().width
66 || image.getBounds().height != childrenCanvas.getBounds().height) {
67
68 image = new Image(
69 event.display,
70 childrenCanvas.getBounds().width,
71 childrenCanvas.getBounds().height
72 );
73
74 childrenCanvas.setData(DATA_KEY, image);
75 }
76
77 // Initializes the graphics context of the image.
78 GC imageGC = new GC(image);
79
80 // First clear the whole canvas to have a clean section where to draw
81 clearDrawingSection(imageGC, image, childrenCanvas);
82
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) ) {
86
87 // Call the function that draw the bars
88 // if (!isFinished) {
89 drawHistogram(imageGC, image);
90 // }
91
92 // Pinpoint a position if set
93 if (childrenCanvas.getHistogramContent().getSelectedEventTimeInWindow() > 0 ) {
94 drawSelectedEventInWindow(imageGC, image);
95 }
96
97 // Draws the buffer image onto the canvas.
98 event.gc.drawImage(image, 0, 0);
99 }
100
101 imageGC.dispose();
102 }
103 }
104
105 /**
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.
108 *
109 * @param imageGC GC content.
110 * @param image Image content.
111 * @param ourCanvas Canvas to clean.
112 */
113 public void clearDrawingSection(GC imageGC, Image image, HistogramCanvas ourCanvas) {
114 // Fills background.
115 imageGC.setBackground(ourCanvas.getDisplay().getSystemColor(HistogramConstant.EMPTY_BACKGROUND_COLOR));
116 imageGC.fillRectangle(0, 0, image.getBounds().width + 1, image.getBounds().height + 1);
117 }
118
119 // *** VERIFY ***
120 // Is it good to put this synchronized?
121 //
122 /**
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.
126 *
127 * @param imageGC GC content.
128 * @param image image content.
129 */
130 public synchronized void drawHistogram(GC imageGC, Image image) {
131
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) );
134
135 // *** NOTE ***
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?
138
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
146 );
147 }
148
149 }
150
151 /**
152 * Draw a certain event selected in the window.<p>
153 *
154 * @param imageGC GC content.
155 * @param image image content.
156 */
157 public synchronized void drawSelectedEventInWindow(GC imageGC, Image image) {
158
159 final HistogramContent tmpContent = childrenCanvas.getHistogramContent();
160 final int tmpBarWidth = tmpContent.getBarsWidth();
161 final int position = tmpContent.getClosestXPositionFromTimestamp(tmpContent.getSelectedEventTimeInWindow());
162
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);
166 imageGC.drawLine(
167 tmpBarWidth * position,
168 0,
169 tmpBarWidth * position,
170 image.getBounds().height
171 );
172 }
173
174 /**
175 * @param isFinished the flag value
176 */
177 public void setIsFinished(boolean isFinished) {
178 this.isFinished = isFinished;
179 }
180 }
This page took 0.033895 seconds and 5 git commands to generate.