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
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
14 import org
.eclipse
.swt
.events
.PaintEvent
;
15 import org
.eclipse
.swt
.events
.PaintListener
;
16 import org
.eclipse
.swt
.graphics
.Rectangle
;
19 * <b><u>HistogramCanvasPaintListener</u></b>
21 * Implementation of a PaintListener for the need of the HistogramCanvas
24 public class HistogramCanvasPaintListener
implements PaintListener
26 protected HistogramCanvas parentCanvas
= null;
29 * HistogramCanvasPaintListener constructor
31 * @param parentCanvas Related canvas
33 public HistogramCanvasPaintListener(HistogramCanvas newParentCanvas
) {
34 parentCanvas
= newParentCanvas
;
38 * Function called when the canvas need to redraw.<p>
40 * @param event The generated paint event when redraw is called.
42 public void paintControl(PaintEvent event
) {
44 // First clear the whole canvas to have a clean section where to draw
45 clearDrawingSection(event
);
47 // If the content is null or has rady to draw we quit the function here
48 if ( (parentCanvas
.getHistogramContent() == null) || (parentCanvas
.getHistogramContent().getReadyUpToPosition() == 0) ) {
52 // Call the function that draw the bars
55 // Pinpoint a position if set
56 if (parentCanvas
.getHistogramContent().getSelectedEventTimeInWindow() > 0 ) {
57 drawSelectedEventInWindow(event
);
60 // If we have a selected window set to visible, call the function to draw it
61 if ( (parentCanvas
.getCurrentWindow() != null) && (parentCanvas
.getCurrentWindow().getSelectedWindowVisible() == true) ) {
62 drawSelectedWindow(event
);
67 * Clear the drawing section of the canvas<p>
68 * This paint the whole background in EMPTY_BACKGROUND_COLOR, so we have something clean to draw on.
70 * @param event The generated paint event when redraw is called.
72 public void clearDrawingSection(PaintEvent event
) {
73 event
.gc
.setForeground(event
.display
.getSystemColor(HistogramConstant
.EMPTY_BACKGROUND_COLOR
));
74 event
.gc
.setBackground(event
.display
.getSystemColor(HistogramConstant
.EMPTY_BACKGROUND_COLOR
));
75 Rectangle allSection
= new Rectangle(0, 0, event
.width
, event
.height
);
76 event
.gc
.fillRectangle(allSection
);
77 event
.gc
.drawRectangle(allSection
);
81 // Is it good to put this synchronized?
84 * Draw the histogram bars in the canvas.<p>
85 * Use existing elements in HistogramContent to draw bars on the cancas;
86 * the element table in content need to be populated and have consistent value.
88 * @param event The generated paint event when redraw is called.
90 public synchronized void drawHistogram(PaintEvent event
) {
91 HistogramContent tmpContent
= parentCanvas
.getHistogramContent();
92 int tmpBarWidth
= tmpContent
.getBarsWidth();
94 // This will be the color for all the bars that wil be draw below.
95 event
.gc
.setBackground(event
.display
.getSystemColor(HistogramConstant
.HISTOGRAM_BARS_COLOR
));
98 // Y Position in a canvas is REVERSED, so "0" is on top of the screen and "MAX" is on bottom.
99 // Not very instinctive, isn't it?
101 // Draw a bar from the left (pos X=0) until the pos=(NbBars*barWidth). If space is left, it will be blanked after.
102 for ( int x
=0; x
<tmpContent
.getReadyUpToPosition(); x
++) {
103 Rectangle rect
= new Rectangle(tmpBarWidth
*x
, event
.height
- tmpContent
.getElementByIndex(x
).intervalHeight
, tmpBarWidth
, tmpContent
.getElementByIndex(x
).intervalHeight
);
104 event
.gc
.fillRectangle(rect
);
107 // Clear the remaining space in the canvas (if any) so it appears clean.
108 event
.gc
.setBackground(event
.display
.getSystemColor(HistogramConstant
.EMPTY_BACKGROUND_COLOR
));
109 Rectangle rect
= new Rectangle(tmpBarWidth
*tmpContent
.getNbElement(), 0, event
.width
, event
.height
);
110 event
.gc
.fillRectangle(rect
);
114 * Draw a certain event selected in the window.<p>
116 * @param event The generated paint event when redraw is called.
118 public synchronized void drawSelectedEventInWindow(PaintEvent event
) {
119 HistogramContent tmpContent
= parentCanvas
.getHistogramContent();
120 int tmpBarWidth
= tmpContent
.getBarsWidth();
122 // This will be the color for all the bars that wil be draw below.
123 event
.gc
.setBackground(event
.display
.getSystemColor(HistogramConstant
.SELECTED_EVENT_COLOR
));
125 int position
= tmpContent
.getClosestXPositionFromTimestamp(tmpContent
.getSelectedEventTimeInWindow());
127 Rectangle rect
= new Rectangle(tmpBarWidth
*position
, 0, tmpBarWidth
, event
.height
);
128 event
.gc
.fillRectangle(rect
);
132 * Draw the selection window in the canvas.<p>
133 * This draw a square around the selected section with a crosshair in the middle.
134 * The square cannot be smaller than "MINIMUM_WINDOW_WIDTH"
136 * @param event The generated paint event when redraw is called.
138 public void drawSelectedWindow(PaintEvent event
) {
139 HistogramSelectedWindow tmpWindow
= parentCanvas
.getCurrentWindow();
141 // Attributes (color and width) of the lines
142 event
.gc
.setForeground(event
.display
.getSystemColor(HistogramConstant
.SELECTION_WINDOW_COLOR
));
143 event
.gc
.setLineWidth(HistogramConstant
.SELECTION_LINE_WIDTH
);
145 // Get the window position... this would fail if the window is not initialized yet
146 int positionCenter
= tmpWindow
.getWindowXPositionCenter();
147 int positionLeft
= tmpWindow
.getWindowXPositionLeft();
148 int positionRight
= tmpWindow
.getWindowXPositionRight();
150 // Minimal size verification.
151 if ( (positionRight
- positionLeft
) < HistogramConstant
.MINIMUM_WINDOW_WIDTH
) {
152 positionLeft
= positionCenter
- (HistogramConstant
.MINIMUM_WINDOW_WIDTH
/2);
153 positionRight
= positionCenter
+ (HistogramConstant
.MINIMUM_WINDOW_WIDTH
/2);
156 // Draw the selection window square
157 event
.gc
.drawLine(positionLeft
, 0 , positionLeft
, event
.height
);
158 event
.gc
.drawLine(positionLeft
, event
.height
, positionRight
, event
.height
);
159 event
.gc
.drawLine(positionRight
, event
.height
, positionRight
, 0);
160 event
.gc
.drawLine(positionLeft
, 0 , positionRight
, 0);
162 // Draw the crosshair section
163 event
.gc
.drawLine(positionCenter
+ HistogramConstant
.SELECTION_CROSSHAIR_LENGTH
, event
.height
/2, positionCenter
- HistogramConstant
.SELECTION_CROSSHAIR_LENGTH
, event
.height
/2);
164 event
.gc
.drawLine(positionCenter
, (event
.height
/2) + HistogramConstant
.SELECTION_CROSSHAIR_LENGTH
, positionCenter
, (event
.height
/2) - HistogramConstant
.SELECTION_CROSSHAIR_LENGTH
);