Commit | Line | Data |
---|---|---|
fbd124dd BH |
1 | /*******************************************************************************\r |
2 | * Copyright (c) 2010, 2011 Ericsson\r | |
3 | * \r | |
4 | * All rights reserved. This program and the accompanying materials are\r | |
5 | * made available under the terms of the Eclipse Public License v1.0 which\r | |
6 | * accompanies this distribution, and is available at\r | |
7 | * http://www.eclipse.org/legal/epl-v10.html\r | |
8 | * \r | |
9 | * Contributors:\r | |
10 | * Philippe Sawicki (INF4990.A2010@gmail.com) - Initial API and implementation\r | |
11 | * Mathieu Denis (mathieu.denis55@gmail.com) - Refactored code\r | |
12 | * Bernd Hufmann - Adapted to new model-view-controller design, display improvements\r | |
13 | *******************************************************************************/\r | |
638eac44 | 14 | package org.eclipse.linuxtools.internal.lttng.ui.views.latency.listeners;\r |
fbd124dd BH |
15 | \r |
16 | import java.text.DecimalFormat;\r | |
17 | import java.util.Collections;\r | |
18 | import java.util.Vector;\r | |
19 | \r | |
638eac44 FC |
20 | import org.eclipse.linuxtools.internal.lttng.ui.views.latency.AbstractViewer;\r |
21 | import org.eclipse.linuxtools.internal.lttng.ui.views.latency.HistogramViewer;\r | |
22 | import org.eclipse.linuxtools.internal.lttng.ui.views.latency.Messages;\r | |
e0752744 FC |
23 | import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramScaledData;\r |
24 | import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramUtils;\r | |
25 | import org.eclipse.linuxtools.tmf.ui.views.histogram.IHistogramDataModel;\r | |
fbd124dd BH |
26 | import org.eclipse.swt.graphics.Image;\r |
27 | import org.eclipse.swt.graphics.Point;\r | |
28 | import org.eclipse.swt.widgets.Display;\r | |
29 | import org.eclipse.ui.plugin.AbstractUIPlugin;\r | |
30 | \r | |
31 | /**\r | |
32 | * <b><u>HistogramPaintListener</u></b>\r | |
33 | * <p>\r | |
34 | * Histogram paint listener.\r | |
35 | * \r | |
36 | * @author Philippe Sawicki\r | |
37 | */\r | |
38 | public class HistogramPaintListener extends AbstractPaintListener {\r | |
39 | \r | |
40 | // ------------------------------------------------------------------------\r | |
41 | // Attributes\r | |
42 | // ------------------------------------------------------------------------\r | |
43 | \r | |
44 | /**\r | |
45 | * Is a histogram bar so high that it is clipped from the draw area ?\r | |
46 | */\r | |
47 | private boolean fBarIsClipped = false;\r | |
48 | \r | |
49 | /**\r | |
50 | * Scaled data from data model\r | |
51 | */\r | |
52 | protected HistogramScaledData fScaledData;\r | |
53 | \r | |
54 | /**\r | |
55 | * Warning Image\r | |
56 | */\r | |
57 | protected Image fWarningImage;\r | |
58 | \r | |
59 | // ------------------------------------------------------------------------\r | |
60 | // Constructors\r | |
61 | // ------------------------------------------------------------------------\r | |
62 | \r | |
63 | /**\r | |
64 | * Constructor.\r | |
65 | * @param view\r | |
66 | * A reference to the listener's viewer.\r | |
67 | */\r | |
68 | public HistogramPaintListener(AbstractViewer viewer) {\r | |
69 | super(viewer);\r | |
70 | fWarningImage = AbstractUIPlugin.imageDescriptorFromPlugin(Messages.LatencyView_tmf_UI, "icons/elcl16/warning.gif").createImage(Display.getCurrent()); //$NON-NLS-1$\r | |
71 | }\r | |
72 | \r | |
73 | // ------------------------------------------------------------------------\r | |
74 | // Accessors\r | |
75 | // ------------------------------------------------------------------------\r | |
76 | \r | |
77 | /**\r | |
78 | * Returns the histogram's bar Width.\r | |
79 | * @return The histogram's bar Width.\r | |
80 | */\r | |
81 | public int getBarWidth() {\r | |
82 | return fBarWith;\r | |
83 | }\r | |
84 | \r | |
85 | // ------------------------------------------------------------------------\r | |
86 | // Operations\r | |
87 | // ------------------------------------------------------------------------\r | |
88 | \r | |
89 | /*\r | |
90 | * (non-Javadoc)\r | |
91 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#dispose()\r | |
92 | */\r | |
93 | @Override\r | |
94 | public void dispose() {\r | |
95 | fWarningImage.dispose();\r | |
96 | super.dispose();\r | |
97 | }\r | |
98 | \r | |
99 | /*\r | |
100 | * (non-Javadoc)\r | |
101 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#scale()\r | |
102 | */\r | |
103 | @Override\r | |
104 | public void scale() {\r | |
105 | // width of the plot area\r | |
106 | double width = getWidth();\r | |
107 | // height of the plot area\r | |
108 | double height = getHeight();\r | |
109 | \r | |
110 | int barWidth = getBarWidth();\r | |
111 | \r | |
112 | IHistogramDataModel model = ((HistogramViewer)fViewer).getModel();\r | |
113 | fScaledData = model.scaleTo((int)width, (int)height, barWidth);\r | |
114 | \r | |
115 | fYMin = 0;\r | |
116 | fYMax = fScaledData.fMaxValue;\r | |
117 | \r | |
118 | fXMin = fScaledData.getFirstBucketTime();\r | |
119 | fXMin = fXMin > 0 ? fXMin : 0; \r | |
120 | fXMax = fScaledData.getBucketEndTime(fScaledData.fLastBucket - 1);\r | |
121 | \r | |
122 | // No data to display - set end time to 0 \r | |
123 | if (fYMax == 0) {\r | |
124 | fXMax = 0;\r | |
125 | }\r | |
126 | }\r | |
127 | \r | |
128 | /*\r | |
129 | * (non-Javadoc)\r | |
130 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#paintVerticalTicks(int)\r | |
131 | */\r | |
132 | @Override\r | |
133 | public void paintVerticalTicks(int x) {\r | |
134 | // done in method paintVerticalAxisValues()\r | |
135 | }\r | |
136 | \r | |
137 | /*\r | |
138 | * (non-Javadoc)\r | |
139 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#paintVerticalAxisValues(int)\r | |
140 | */\r | |
141 | @Override\r | |
142 | public void paintVerticalAxisValues(int x) {\r | |
143 | int zoomFactor = 1;\r | |
144 | \r | |
145 | zoomFactor = fViewer.getZoomFactor();\r | |
146 | \r | |
147 | if (fYMin >= 0L && fYMax != 0L) {\r | |
148 | fAxisImage.setForeground(fTextColor);\r | |
149 | fAxisImage.setBackground(fBackgroundColor);\r | |
150 | \r | |
151 | // Apply the zoom to the max value of the graph for the next calculations\r | |
152 | long yMax = fYMax / zoomFactor;\r | |
153 | \r | |
154 | int nbTicks = ((int)getHeight()) / MAX_HEIGHT_BETWEEN_TICKS + 1;\r | |
155 | \r | |
156 | Vector<Integer> values = new Vector<Integer>();\r | |
157 | boolean multipleSameValues = true;\r | |
158 | while (multipleSameValues) {\r | |
159 | double valueStep = (double) (yMax - fYMin) / (double) (nbTicks);\r | |
160 | \r | |
161 | for (int i = 0; i < nbTicks; i++) {\r | |
162 | double currentValue = (double) (fYMin + i * valueStep) / (Math.pow(10, fDelta));\r | |
163 | \r | |
164 | values.add((int) currentValue);\r | |
165 | }\r | |
166 | \r | |
167 | Collections.sort(values);\r | |
168 | boolean hasRepetition = false;\r | |
169 | for (int i = 1; i < values.size(); i++) {\r | |
170 | if (values.get(i) == values.get(i - 1)) {\r | |
171 | hasRepetition = true;\r | |
172 | break;\r | |
173 | }\r | |
174 | }\r | |
175 | \r | |
176 | if (hasRepetition) {\r | |
177 | nbTicks--;\r | |
178 | values.clear();\r | |
179 | } else {\r | |
180 | multipleSameValues = false;\r | |
181 | \r | |
182 | // Draw rectangle over the old values\r | |
183 | int height = fViewer.getBounds().height - 2 * fPadding - fPaddingTop - fHorizontalAxisYOffset;\r | |
184 | fAxisImage.fillRectangle(0, fPadding + fPaddingTop, fPadding + fVerticalAxisOffset, height);\r | |
185 | \r | |
186 | double pixelStep = (getHeight()) / values.size() + 1;\r | |
187 | \r | |
188 | for (int i = 0; i < values.size(); i++) {\r | |
189 | double currentValue = values.get(i);\r | |
190 | \r | |
191 | int y = (int) (fClientArea.height - fPadding - fHorizontalAxisYOffset - i * pixelStep);\r | |
192 | String currentLabel = formatStringForVerticalAxis((long) currentValue);\r | |
193 | \r | |
194 | fAxisImage.setFont(fValuesFont);\r | |
195 | \r | |
196 | Point textDimensions = fAxisImage.stringExtent(currentLabel);\r | |
197 | fAxisImage.drawText(currentLabel, x - textDimensions.x - 5, y - textDimensions.y / 2);\r | |
198 | fAxisImage.drawLine(x - 3, y, x, y);\r | |
199 | }\r | |
200 | }\r | |
201 | }\r | |
202 | }\r | |
203 | }\r | |
204 | \r | |
205 | /*\r | |
206 | * (non-Javadoc)\r | |
207 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#paintContent()\r | |
208 | */\r | |
209 | @Override\r | |
210 | public void paintContent() {\r | |
211 | double zoomFactor = fViewer.getZoomFactor();\r | |
212 | \r | |
213 | // Calculate the vertical axis factor and see if it has changed\r | |
214 | double tmpDelta = fDelta;\r | |
215 | fDelta = 0;\r | |
216 | if (Long.toString(fYMax / (long) zoomFactor).length() > MAX_CHAR_VERTICAL_DISPLAY) {\r | |
217 | fDelta = Long.toString(fYMax / (long) zoomFactor).length() - MAX_CHAR_VERTICAL_DISPLAY;\r | |
218 | }\r | |
219 | if (tmpDelta != fDelta) {\r | |
220 | fViewer.clearBackground();\r | |
221 | }\r | |
222 | \r | |
223 | paintBackground();\r | |
224 | paintVerticalAxis();\r | |
225 | paintHorizontalAxis();\r | |
226 | \r | |
227 | fAxisImage.setForeground(fDataColor);\r | |
228 | fAxisImage.setBackground(fDataColor);\r | |
229 | \r | |
230 | // height of the plot area\r | |
231 | double height = getHeight();\r | |
232 | \r | |
233 | int barWidth = getBarWidth();\r | |
234 | \r | |
235 | // axisImage_.setBackground(backgroundColor_);\r | |
236 | // 1.a Iterate over the points, from 0 to nbPoints\r | |
237 | // 1.b Find the max counter value\r | |
238 | // 2. Assign the max value to the "yMax_" class attribute\r | |
239 | // 3. Draw the histogram bars using "axisImage_.fillRectangle(...)"\r | |
240 | boolean oneBarIsClipped = false;\r | |
241 | \r | |
242 | for (int i = 0; i < fScaledData.fData.length; i++) {\r | |
243 | double pointY = fScaledData.fData[i];\r | |
244 | \r | |
245 | // in pixels\r | |
246 | double x = fPadding + i * barWidth + fVerticalAxisOffset + 1;\r | |
247 | \r | |
248 | if (i == fScaledData.fData.length - 1)\r | |
249 | x -= 1.0;\r | |
250 | double barHeight = zoomFactor * ((double)(pointY - fYMin) / (double)(fYMax - fYMin)) * height;\r | |
251 | \r | |
252 | if (barHeight > height + 1) {\r | |
253 | barHeight = height;\r | |
254 | oneBarIsClipped = true;\r | |
255 | \r | |
256 | fAxisImage.drawImage(fWarningImage, 5, 3);\r | |
257 | }\r | |
258 | \r | |
259 | // Only draw the bars that have a barHeight of more than 1 pixel\r | |
260 | if (barHeight > 0) {\r | |
261 | double y = fPadding + fPaddingTop + height - barHeight;\r | |
262 | fAxisImage.setBackground(fDataColor);\r | |
263 | \r | |
264 | if (barHeight > height - 1) {\r | |
265 | fAxisImage.fillRectangle((int) x, (int) y, (int) barWidth, (int) (barHeight + 1));\r | |
266 | } else {\r | |
267 | fAxisImage.fillRectangle((int) x, (int) y, (int) barWidth, (int) (barHeight + 2));\r | |
268 | }\r | |
269 | }\r | |
270 | }\r | |
271 | \r | |
272 | if (oneBarIsClipped)\r | |
273 | fBarIsClipped = true;\r | |
274 | else\r | |
275 | fBarIsClipped = false;\r | |
276 | }\r | |
277 | \r | |
278 | /**\r | |
279 | * Paints the histogram horizontal axis values in engineering notation in which the exponent is a multiple of three.\r | |
280 | * @param value\r | |
281 | * The numeric value to convert to engineering notation.\r | |
282 | * @return The given value formatted according to the engineering notation.\r | |
283 | */\r | |
284 | @Override\r | |
285 | public String formatStringForHorizontalAxis(long value) {\r | |
286 | DecimalFormat formatter = new DecimalFormat("##0.#E0"); //$NON-NLS-1$\r | |
287 | return formatter.format(value);\r | |
288 | }\r | |
289 | \r | |
290 | /**\r | |
291 | * Sets the bar width.\r | |
292 | * @param barWidth \r | |
293 | * bar width to set\r | |
294 | */\r | |
295 | public void setBarWidth(int barWidth) {\r | |
296 | fBarWith = barWidth;\r | |
297 | }\r | |
298 | \r | |
299 | /**\r | |
300 | * Returns "true" if a histogram bar is so high that it cannot be drawn in the draw area, "false" otherwise.\r | |
301 | * @return "true" if a histogram bar is so high that it cannot be drawn in the draw area, "false" otherwise.\r | |
302 | */\r | |
303 | public boolean barIsClipped() {\r | |
304 | return fBarIsClipped;\r | |
305 | }\r | |
306 | \r | |
307 | /*\r | |
308 | * (non-Javadoc)\r | |
309 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatToolTipLabel(int, int)\r | |
310 | */\r | |
311 | @Override\r | |
312 | public String formatToolTipLabel(int x, int y) {\r | |
313 | if (fScaledData != null) {\r | |
314 | \r | |
315 | double barWidth = getBarWidth();\r | |
316 | double height = getHeight(); // height of the plot area\r | |
317 | \r | |
318 | double zoomFactor = fViewer.getZoomFactor();\r | |
319 | \r | |
320 | int index = (int) ((x - fPadding - fVerticalAxisOffset - 1) / barWidth);\r | |
321 | \r | |
322 | double barHeight = 0.0;\r | |
323 | if (index >= 0 && index <= fScaledData.fLastBucket) {\r | |
324 | barHeight = (zoomFactor * height * (fScaledData.fData[index] - fYMin) / (fYMax - fYMin));\r | |
325 | }\r | |
326 | \r | |
327 | long fMouseY = (long) (height - (y - fPadding - fPaddingTop));\r | |
328 | \r | |
329 | // Verifying mouse pointer is over histogram bar\r | |
330 | if (index >= 0 && fScaledData.fLastBucket >= index && fMouseY >= 0 && fMouseY < barHeight && fMouseY < height && x >= (fVerticalAxisOffset + fPadding)) {\r | |
331 | \r | |
332 | fScaledData.fCurrentBucket = index;\r | |
333 | \r | |
334 | long startTime = fScaledData.getBucketStartTime(index);\r | |
335 | // negative values are possible if time values came into the model in decreasing order\r | |
336 | if (startTime < 0) {\r | |
337 | startTime = 0;\r | |
338 | }\r | |
339 | long endTime = fScaledData.getBucketEndTime(index);\r | |
340 | int nbEvents = fScaledData.fData[index];\r | |
341 | \r | |
342 | StringBuffer buffer = new StringBuffer();\r | |
343 | buffer.append("Latency Range in s = ["); //$NON-NLS-1$\r | |
344 | buffer.append(HistogramUtils.nanosecondsToString(startTime));\r | |
345 | buffer.append(","); //$NON-NLS-1$\r | |
346 | buffer.append(HistogramUtils.nanosecondsToString(endTime));\r | |
347 | buffer.append("]\n"); //$NON-NLS-1$\r | |
348 | buffer.append("Latency count = "); //$NON-NLS-1$\r | |
349 | buffer.append(nbEvents);\r | |
350 | return buffer.toString();\r | |
351 | }\r | |
352 | }\r | |
353 | return ""; //$NON-NLS-1$\r | |
354 | }\r | |
355 | } |