Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java @ 657:af3f56758f59
merged gnv-artifacts/0.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:53 +0200 |
parents | 3d13fa281a7e |
children | 79401c871da4 |
comparison
equal
deleted
inserted
replaced
590:5f5f273c8566 | 657:af3f56758f59 |
---|---|
1 package de.intevation.gnv.chart; | |
2 | |
3 import java.awt.Font; | |
4 import java.awt.Color; | |
5 import java.awt.Paint; | |
6 import java.awt.geom.Ellipse2D; | |
7 import java.lang.NumberFormatException; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import org.jfree.chart.StandardChartTheme; | |
12 import org.jfree.chart.plot.XYPlot; | |
13 import org.jfree.chart.renderer.xy.AbstractXYItemRenderer; | |
14 import org.jfree.chart.renderer.xy.XYBarRenderer; | |
15 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; | |
16 import org.jfree.ui.RectangleInsets; | |
17 import org.jfree.util.UnitType; | |
18 | |
19 | |
20 import org.w3c.dom.Document; | |
21 | |
22 import de.intevation.artifactdatabase.Config; | |
23 | |
24 /** | |
25 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de> | |
26 */ | |
27 public class XMLChartTheme | |
28 extends StandardChartTheme | |
29 { | |
30 private static final Color DEFAULT_COLOR = Color.BLACK; | |
31 | |
32 private Logger log = Logger.getLogger(XMLChartTheme.class); | |
33 | |
34 protected boolean domainCrosshairVisible; | |
35 protected boolean rangeCrosshairVisible; | |
36 protected boolean renderLines; | |
37 protected boolean renderShapes; | |
38 | |
39 protected int pointWidth; | |
40 protected int pointHeight; | |
41 | |
42 protected Paint histogramBasePaint; | |
43 | |
44 | |
45 public XMLChartTheme(String name) { | |
46 super(name); | |
47 } | |
48 | |
49 | |
50 public void setDomainCrosshairVisible(boolean visible) { | |
51 this.domainCrosshairVisible = visible; | |
52 } | |
53 | |
54 | |
55 public boolean getDomainCrosshairVisible() { | |
56 return domainCrosshairVisible; | |
57 } | |
58 | |
59 | |
60 public boolean getRangeCrosshairVisible() { | |
61 return rangeCrosshairVisible; | |
62 } | |
63 | |
64 | |
65 public void setRangeCrosshairVisible(boolean visible) { | |
66 this.rangeCrosshairVisible = visible; | |
67 } | |
68 | |
69 | |
70 public void setHistogramBasePaint(Color c) { | |
71 this.histogramBasePaint = c; | |
72 } | |
73 | |
74 | |
75 public Paint getHistogramBasePaint() { | |
76 return histogramBasePaint; | |
77 } | |
78 | |
79 | |
80 public void applyXMLConfiguration(Document document) { | |
81 log.debug("create XMLChartTheme"); | |
82 | |
83 init(document); | |
84 } | |
85 | |
86 | |
87 private void init(Document document) { | |
88 log.debug("init XMLChartTheme parameters"); | |
89 | |
90 initChartParameters(document); | |
91 initTitleParameters(document); | |
92 initSubtitleParameters(document); | |
93 initPlotParameters(document); | |
94 initAxisParameters(document); | |
95 initLegendParameters(document); | |
96 initRenderer(document); | |
97 initHistogramColor(document); | |
98 } | |
99 | |
100 | |
101 private void initTitleParameters(Document document) { | |
102 log.debug("init title parameters."); | |
103 | |
104 int size = getInt(document, "theme/title/font/size/@value"); | |
105 String type = getString(document, "theme/title/font/type/@value"); | |
106 boolean bold = getBool(document, "theme/title/font/bold/@value"); | |
107 | |
108 setExtraLargeFont(createFont(type, size, bold)); | |
109 | |
110 String color = getString(document, "theme/title/font/color/@value"); | |
111 Color c = decodeColor(color); | |
112 if (c != null) | |
113 setTitlePaint(c); | |
114 } | |
115 | |
116 | |
117 private void initSubtitleParameters(Document document) { | |
118 log.debug("init title parameters."); | |
119 | |
120 int size = getInt(document, "theme/subtitle/font/size/@value"); | |
121 String type = getString(document, "theme/subtitle/font/type/@value"); | |
122 boolean bold = getBool(document, "theme/subtitle/font/bold/@value"); | |
123 | |
124 setLargeFont(createFont(type, size, bold)); | |
125 | |
126 String col = getString(document, "theme/subtitle/font/color/@value"); | |
127 setSubtitlePaint(Color.decode(col)); | |
128 } | |
129 | |
130 | |
131 private void initChartParameters(Document document) { | |
132 log.debug("init chart parameters."); | |
133 | |
134 String bg = getString(document, "theme/chart/background/color/@value"); | |
135 Color c = decodeColor(bg); | |
136 if (c != null) | |
137 setChartBackgroundPaint(c); | |
138 } | |
139 | |
140 | |
141 private void initPlotParameters(Document document) { | |
142 log.debug("init plot parameters."); | |
143 | |
144 String tmp = null; | |
145 tmp = getString(document, "theme/plot/background/color/@value"); | |
146 Color c = decodeColor(tmp); | |
147 if (c != null) | |
148 setPlotBackgroundPaint(c); | |
149 | |
150 tmp = getString(document, "theme/plot/outline/color/@value"); | |
151 c = decodeColor(tmp); | |
152 if (c != null) | |
153 setPlotOutlinePaint(c); | |
154 | |
155 tmp = getString(document, "theme/plot/domaingridline/color/@value"); | |
156 c = decodeColor(tmp); | |
157 if (c != null) | |
158 setDomainGridlinePaint(c); | |
159 | |
160 tmp = getString(document, "theme/plot/rangegridline/color/@value"); | |
161 c = decodeColor(tmp); | |
162 if (c != null) | |
163 setRangeGridlinePaint(c); | |
164 | |
165 boolean rangeCrosshairVisible = getBool( | |
166 document, "theme/plot/rangecrosshair/visible/@value"); | |
167 setRangeCrosshairVisible(rangeCrosshairVisible); | |
168 | |
169 boolean domainCrosshairVisible = getBool( | |
170 document, "theme/plot/domaincrosshair/visible/@value"); | |
171 setDomainCrosshairVisible(domainCrosshairVisible); | |
172 | |
173 int top = getInt(document, "theme/plot/offset/top/@value"); | |
174 int bottom = getInt(document, "theme/plot/offset/bottom/@value"); | |
175 int left = getInt(document, "theme/plot/offset/left/@value"); | |
176 int right = getInt(document, "theme/plot/offset/right/@value"); | |
177 setAxisOffset(new RectangleInsets( | |
178 UnitType.RELATIVE, | |
179 top, left, bottom, right) | |
180 ); | |
181 } | |
182 | |
183 | |
184 private void initAxisParameters(Document document) { | |
185 log.debug("init axis parameters."); | |
186 | |
187 String tmp = null; | |
188 tmp = getString(document, "theme/axis/label/color/@value"); | |
189 Color c = decodeColor(tmp); | |
190 if (c != null) | |
191 setAxisLabelPaint(c); | |
192 | |
193 tmp = getString(document, "theme/axis/ticklabel/color/@value"); | |
194 c = decodeColor(tmp); | |
195 if (c != null) | |
196 setTickLabelPaint(c); | |
197 } | |
198 | |
199 | |
200 private void initLegendParameters(Document document) { | |
201 log.debug("init legend parameters."); | |
202 | |
203 String tmp = null; | |
204 tmp = getString(document, "theme/legend/font/color/@value"); | |
205 Color c = decodeColor(tmp); | |
206 if (c != null) | |
207 setLegendItemPaint(c); | |
208 | |
209 tmp = getString(document, "theme/legend/background/color/@value"); | |
210 c = decodeColor(tmp); | |
211 if (c != null) | |
212 setLegendBackgroundPaint(c); | |
213 } | |
214 | |
215 | |
216 private void initRenderer(Document document) { | |
217 log.debug("init renderer parameters."); | |
218 | |
219 pointWidth = getInt(document, "theme/plot/itemrenderer/width/@value"); | |
220 log.debug("Read point width of " + pointWidth); | |
221 pointHeight = getInt(document, "theme/plot/itemrenderer/height/@value"); | |
222 log.debug("Read point height of " + pointHeight); | |
223 renderLines = getBool( | |
224 document, "theme/plot/itemrenderer/renderLines/@value" | |
225 ); | |
226 renderShapes = getBool( | |
227 document, "theme/plot/itemrenderer/renderPoints/@value" | |
228 ); | |
229 } | |
230 | |
231 | |
232 private void initHistogramColor(Document document) { | |
233 log.debug("init histogram color"); | |
234 String tmp = getString(document, "theme/histogram/bar/color/@value"); | |
235 Color c = decodeColor(tmp); | |
236 | |
237 if (c != null) | |
238 setHistogramBasePaint(c); | |
239 } | |
240 | |
241 | |
242 private static String getString(Document document, String xpath) { | |
243 return Config.getStringXPath(document, xpath); | |
244 } | |
245 | |
246 | |
247 private static int getInt(Document document, String xpath) { | |
248 String tmp = getString(document, xpath); | |
249 | |
250 if (tmp != null) | |
251 return Integer.parseInt(tmp); | |
252 else | |
253 return 0; | |
254 } | |
255 | |
256 | |
257 private static boolean getBool(Document document, String xpath) { | |
258 String tmp = getString(document, xpath); | |
259 | |
260 if (tmp != null) | |
261 return Boolean.parseBoolean(tmp); | |
262 else | |
263 return false; | |
264 } | |
265 | |
266 | |
267 protected Color decodeColor(String color) { | |
268 try { | |
269 if (color == null) | |
270 return null; | |
271 | |
272 return Color.decode(color); | |
273 } | |
274 catch (NumberFormatException nfe) { | |
275 log.warn("Error while parsing color: " + color, nfe); | |
276 } | |
277 | |
278 return null; | |
279 } | |
280 | |
281 | |
282 protected Font createFont(String type, int size, boolean bold) { | |
283 Font font = null; | |
284 if (bold) | |
285 font = new Font(type, Font.BOLD, size); | |
286 else | |
287 font = new Font(type, Font.PLAIN, size); | |
288 | |
289 return font; | |
290 } | |
291 | |
292 | |
293 protected void applyToXYPlot(XYPlot plot) { | |
294 log.debug("apply theme parameter to XYPlot"); | |
295 | |
296 super.applyToXYPlot(plot); | |
297 plot.setDomainCrosshairVisible(this.domainCrosshairVisible); | |
298 plot.setRangeCrosshairVisible(this.rangeCrosshairVisible); | |
299 | |
300 AbstractXYItemRenderer renderer = (AbstractXYItemRenderer) | |
301 plot.getRenderer(); | |
302 | |
303 if (renderer instanceof XYLineAndShapeRenderer) | |
304 applyToXYLineAndShapeRenderer(plot); | |
305 | |
306 if (renderer instanceof XYBarRenderer) | |
307 applyToXYBarRenderer(plot); | |
308 } | |
309 | |
310 | |
311 protected void applyToXYLineAndShapeRenderer(XYPlot plot) { | |
312 if (plot == null) | |
313 return; | |
314 | |
315 XYLineAndShapeRenderer renderer = | |
316 (XYLineAndShapeRenderer) plot.getRenderer(); | |
317 | |
318 Ellipse2D.Double point = new Ellipse2D.Double( | |
319 -(pointWidth/2), -(pointHeight/2), pointWidth, pointHeight | |
320 ); | |
321 | |
322 renderer.setSeriesShape(0, point); | |
323 renderer.setSeriesShapesVisible(0, renderShapes); | |
324 renderer.setSeriesLinesVisible(0, renderLines); | |
325 | |
326 plot.setRenderer(renderer); | |
327 } | |
328 | |
329 | |
330 protected void applyToXYBarRenderer(XYPlot plot) { | |
331 if (plot == null) | |
332 return; | |
333 | |
334 XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer(); | |
335 | |
336 renderer.setSeriesPaint(0, histogramBasePaint); | |
337 } | |
338 } |