(no commit message)
[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 package org.eclipse.linuxtools.lttng.ui.views.histogram;
13
14 import org.eclipse.swt.events.PaintEvent;
15 import org.eclipse.swt.events.PaintListener;
16 import org.eclipse.swt.graphics.Rectangle;
17
18 /**
19 * <b><u>HistogramCanvasPaintListener</u></b>
20 * <p>
21 * Implementation of a PaintListener for the need of the HistogramCanvas
22 * <p>
23 */
24 public class HistogramCanvasPaintListener implements PaintListener
25 {
26 protected HistogramCanvas parentCanvas = null;
27
28 /**
29 * HistogramCanvasPaintListener constructor
30 *
31 * @param parentCanvas Related canvas
32 */
33 public HistogramCanvasPaintListener(HistogramCanvas newParentCanvas) {
34 parentCanvas = newParentCanvas;
35 }
36
37 /**
38 * Function called when the canvas need to redraw.<p>
39 *
40 * @param event The generated paint event when redraw is called.
41 */
42 public void paintControl(PaintEvent event) {
43
44 // First clear the whole canvas to have a clean section where to draw
45 clearDrawingSection(event);
46
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) ) {
49 return;
50 }
51
52 // Call the function that draw the bars
53 drawHistogram(event);
54
55 // Pinpoint a position if set
56 if (parentCanvas.getHistogramContent().getSelectedEventTimeInWindow() > 0 ) {
57 drawSelectedEventInWindow(event);
58 }
59
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);
63 }
64 }
65
66 /**
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.
69 *
70 * @param event The generated paint event when redraw is called.
71 */
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);
78 }
79
80 // *** VERIFY ***
81 // Is it good to put this synchronized?
82 //
83 /**
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.
87 *
88 * @param event The generated paint event when redraw is called.
89 */
90 public synchronized void drawHistogram(PaintEvent event) {
91 HistogramContent tmpContent = parentCanvas.getHistogramContent();
92 int tmpBarWidth = tmpContent.getBarsWidth();
93
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));
96
97 // *** NOTE ***
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?
100
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);
105 }
106
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);
111 }
112
113 /**
114 * Draw a certain event selected in the window.<p>
115 *
116 * @param event The generated paint event when redraw is called.
117 */
118 public synchronized void drawSelectedEventInWindow(PaintEvent event) {
119 HistogramContent tmpContent = parentCanvas.getHistogramContent();
120 int tmpBarWidth = tmpContent.getBarsWidth();
121
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));
124
125 int position = tmpContent.getClosestXPositionFromTimestamp(tmpContent.getSelectedEventTimeInWindow());
126
127 Rectangle rect = new Rectangle(tmpBarWidth*position, 0, tmpBarWidth, event.height);
128 event.gc.fillRectangle(rect);
129 }
130
131 /**
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"
135 *
136 * @param event The generated paint event when redraw is called.
137 */
138 public void drawSelectedWindow(PaintEvent event) {
139 HistogramSelectedWindow tmpWindow = parentCanvas.getCurrentWindow();
140
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);
144
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();
149
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);
154 }
155
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);
161
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);
165 }
166
167 }
This page took 0.035207 seconds and 5 git commands to generate.