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 | |
14 | package org.eclipse.linuxtools.lttng.ui.views.latency.listeners;\r | |
15 | \r | |
16 | import java.text.DecimalFormat;\r | |
17 | \r | |
fbd124dd BH |
18 | import org.eclipse.linuxtools.lttng.ui.views.latency.AbstractViewer;\r |
19 | import org.eclipse.linuxtools.lttng.ui.views.latency.GraphViewer;\r | |
20 | import org.eclipse.linuxtools.lttng.ui.views.latency.model.Config;\r | |
21 | import org.eclipse.linuxtools.lttng.ui.views.latency.model.GraphScaledData;\r | |
22 | import org.eclipse.linuxtools.lttng.ui.views.latency.model.IGraphDataModel;\r | |
e0752744 FC |
23 | import org.eclipse.linuxtools.tmf.ui.views.distribution.model.BaseDistributionData;\r |
24 | import org.eclipse.linuxtools.tmf.ui.views.histogram.HistogramUtils;\r | |
fbd124dd BH |
25 | \r |
26 | /**\r | |
27 | * <b><u>GraphPaintListener</u></b>\r | |
28 | * <p>\r | |
29 | * Graph paint listener.\r | |
30 | * \r | |
31 | * @author Philippe Sawicki\r | |
32 | */\r | |
33 | public class GraphPaintListener extends AbstractPaintListener {\r | |
34 | \r | |
35 | // ------------------------------------------------------------------------\r | |
36 | // Attributes\r | |
37 | // ------------------------------------------------------------------------\r | |
38 | \r | |
39 | /**\r | |
40 | * Scaled data from data model\r | |
41 | */\r | |
42 | protected GraphScaledData fScaledData;\r | |
43 | \r | |
44 | // ------------------------------------------------------------------------\r | |
45 | // Constructors\r | |
46 | // ------------------------------------------------------------------------\r | |
47 | \r | |
48 | /**\r | |
49 | * Constructor.\r | |
50 | * @param viewer\r | |
51 | * A reference to the listener's viewer.\r | |
52 | */\r | |
53 | public GraphPaintListener(AbstractViewer view) {\r | |
54 | super(view);\r | |
55 | }\r | |
56 | \r | |
57 | // ------------------------------------------------------------------------\r | |
58 | // Operations\r | |
59 | // ------------------------------------------------------------------------\r | |
60 | \r | |
61 | /*\r | |
62 | * (non-Javadoc)\r | |
63 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#scale()\r | |
64 | */\r | |
65 | @Override\r | |
66 | public void scale() {\r | |
67 | \r | |
68 | // width of the plot area\r | |
69 | double width = getWidth();\r | |
70 | // height of the plot area\r | |
71 | double height = getHeight();\r | |
72 | \r | |
73 | IGraphDataModel model = ((GraphViewer)fViewer).getModel();\r | |
74 | fScaledData = model.scaleTo((int)width, (int)height, fBarWith);\r | |
75 | \r | |
76 | fXMin = fScaledData.getHorFirstBucketTime() > 0 ? fScaledData.getHorFirstBucketTime() : 0;\r | |
77 | fXMax = 0;\r | |
78 | if (fScaledData.getHorLastBucket() > 0) {\r | |
79 | fXMax = fScaledData.getHorBucketEndTime(fScaledData.getHorNbBuckets() - 1);\r | |
80 | }\r | |
81 | \r | |
82 | fYMin = fScaledData.getVerFirstBucketTime() > 0 ? fScaledData.getVerFirstBucketTime() : 0;\r | |
83 | fYMax = 0;\r | |
84 | if (fScaledData.getVerLastBucket() > 0) {\r | |
85 | fYMax = fScaledData.getVerBucketEndTime(fScaledData.getVerNbBuckets() - 1);\r | |
86 | }\r | |
87 | }\r | |
88 | \r | |
89 | /*\r | |
90 | * (non-Javadoc)\r | |
91 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#paintContent()\r | |
92 | */\r | |
93 | @Override\r | |
94 | public void paintContent() {\r | |
95 | if (fXMin >= 0 && fXMax >= 0 && fYMin >= 0 && fYMax >= 0 && fScaledData != null) {\r | |
96 | \r | |
97 | fAxisImage.setForeground(fDataColor);\r | |
98 | fAxisImage.setBackground(fDataColor);\r | |
99 | \r | |
100 | double height = getHeight();\r | |
101 | \r | |
102 | int xLen = fScaledData.getHorNbBuckets();\r | |
103 | int yLen = fScaledData.getVerNbBuckets();\r | |
104 | \r | |
105 | int barWidth = fScaledData.getBarWidth();\r | |
106 | \r | |
107 | for (int i = 0; i < xLen; i++) {\r | |
108 | for (int j = 0; j < yLen; j++) {\r | |
109 | if (fScaledData.getEventCount(i, j) > 0) {\r | |
110 | \r | |
111 | double x = fPadding + i * barWidth + fVerticalAxisOffset + 1;\r | |
112 | double y = fPadding + fPaddingTop + height - j * barWidth;\r | |
113 | \r | |
114 | // Avoid over-drawing background area\r | |
115 | int yBarWidth = fBarWith;\r | |
116 | if (y - yBarWidth < fPadding + fPaddingTop) {\r | |
117 | yBarWidth = (int) (y - fPadding - fPaddingTop);\r | |
118 | }\r | |
119 | int xBarWidth = fBarWith;\r | |
120 | if(x + xBarWidth > fClientArea.width - fPadding - fPaddingRight) {\r | |
121 | xBarWidth = (int)(fClientArea.width - fPadding - fPaddingRight - x); \r | |
122 | }\r | |
123 | fAxisImage.fillRectangle((int) x, (int) y - (int) yBarWidth, (int) xBarWidth, (int) yBarWidth);\r | |
124 | }\r | |
125 | }\r | |
126 | }\r | |
127 | \r | |
128 | if (fScaledData.isCurrentEventTimeValid()) {\r | |
129 | // Draw vertical line\r | |
130 | int index = fScaledData.getHorBucketIndex(fScaledData.getCurrentEventTime());\r | |
131 | \r | |
132 | int x = fPadding + index * barWidth + fVerticalAxisOffset + 1;\r | |
133 | fAxisImage.setForeground(fCurrentEventColor);\r | |
134 | fAxisImage.setBackground(fCurrentEventColor);\r | |
135 | fAxisImage.drawLine(x, fPadding + fPaddingTop, x, fClientArea.height - fPadding - fHorizontalAxisYOffset);\r | |
136 | }\r | |
137 | }\r | |
138 | }\r | |
139 | \r | |
140 | /*\r | |
141 | * (non-Javadoc)\r | |
142 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatStringForVerticalAxis(long)\r | |
143 | */\r | |
144 | @Override\r | |
145 | public String formatStringForVerticalAxis(long value) {\r | |
146 | DecimalFormat formatter = new DecimalFormat("0.0E0"); //$NON-NLS-1$\r | |
147 | return formatter.format(value);\r | |
148 | }\r | |
149 | \r | |
150 | /*\r | |
151 | * (non-Javadoc)\r | |
152 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatStringForHorizontalAxis(long)\r | |
153 | */\r | |
154 | @Override\r | |
155 | public String formatStringForHorizontalAxis(long value) {\r | |
156 | return HistogramUtils.nanosecondsToString(value);\r | |
157 | }\r | |
158 | \r | |
159 | /*\r | |
160 | * (non-Javadoc)\r | |
161 | * @see org.eclipse.linuxtools.lttng.ui.views.latency.listeners.AbstractPaintListener#formatToolTipLabel(int, int)\r | |
162 | */\r | |
163 | @Override\r | |
164 | public String formatToolTipLabel(int x, int y) {\r | |
165 | \r | |
166 | int index = getIndexFromHorizontalValue(x);\r | |
167 | int yIndex = getIndexFromVerticalValue(y);\r | |
168 | \r | |
169 | if (index != BaseDistributionData.OUT_OF_RANGE_BUCKET && yIndex != BaseDistributionData.OUT_OF_RANGE_BUCKET) {\r | |
170 | if (fScaledData.getEventCount(index, yIndex) > 0) {\r | |
171 | StringBuffer buffer = new StringBuffer();\r | |
172 | buffer.append("Time Range in s = ["); //$NON-NLS-1$\r | |
173 | // TODO change Utility\r | |
174 | long startTime = fScaledData.getHorBucketStartTime(index) > 0 ? fScaledData.getHorBucketStartTime(index) : 0;\r | |
175 | buffer.append(HistogramUtils.nanosecondsToString(startTime));\r | |
176 | buffer.append(","); //$NON-NLS-1$\r | |
177 | buffer.append(HistogramUtils.nanosecondsToString(fScaledData.getHorBucketEndTime(index)));\r | |
178 | buffer.append("]\n"); //$NON-NLS-1$\r | |
179 | buffer.append("Latency Range in s = ["); //$NON-NLS-1$\r | |
180 | long yStartTime = fScaledData.getVerBucketStartTime(yIndex) > 0 ? fScaledData.getVerBucketStartTime(yIndex) : 0;\r | |
181 | buffer.append(HistogramUtils.nanosecondsToString(yStartTime));\r | |
182 | buffer.append(","); //$NON-NLS-1$\r | |
183 | buffer.append(HistogramUtils.nanosecondsToString(fScaledData.getVerBucketEndTime(yIndex)));\r | |
184 | buffer.append("]\n"); //$NON-NLS-1$\r | |
185 | buffer.append("Latency count = "); //$NON-NLS-1$\r | |
186 | buffer.append(fScaledData.getEventCount(index, yIndex));\r | |
187 | return buffer.toString();\r | |
188 | }\r | |
189 | }\r | |
190 | return ""; //$NON-NLS-1$\r | |
191 | }\r | |
192 | \r | |
193 | public int getIndexFromHorizontalValue(int x) {\r | |
194 | if (fScaledData != null) {\r | |
195 | double barWidth = fScaledData.getBarWidth();\r | |
196 | \r | |
197 | int index = (int) ((x - fPadding - fVerticalAxisOffset - 1) / barWidth);\r | |
198 | if ((index >= 0) && (fScaledData.getHorNbBuckets() > index)) {\r | |
199 | return index;\r | |
200 | }\r | |
201 | }\r | |
202 | return BaseDistributionData.OUT_OF_RANGE_BUCKET;\r | |
203 | }\r | |
204 | \r | |
205 | public int getIndexFromVerticalValue(int y) {\r | |
206 | if (fScaledData != null) {\r | |
207 | double barWidth = fScaledData.getBarWidth();\r | |
208 | double height = getHeight(); // height of the plot area\r | |
209 | \r | |
210 | int index = (int) ((height - (y - fPadding - fPaddingTop)) / barWidth);\r | |
211 | if (index >= 0 && fScaledData.getVerNbBuckets() > index) {\r | |
212 | return index;\r | |
213 | }\r | |
214 | }\r | |
215 | return BaseDistributionData.OUT_OF_RANGE_BUCKET;\r | |
216 | }\r | |
217 | \r | |
218 | public long getCurrentTimeFromHorizontalValue(int x) {\r | |
219 | if (fXMin >= 0 && fXMax >= 0) {\r | |
220 | int index = getIndexFromHorizontalValue(x);\r | |
221 | if (index != BaseDistributionData.OUT_OF_RANGE_BUCKET) {\r | |
222 | return fScaledData.getHorBucketStartTime(index);\r | |
223 | }\r | |
224 | }\r | |
225 | return Config.INVALID_EVENT_TIME;\r | |
226 | }\r | |
227 | } |