Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 3818:dc18457b1cef
merged flys-artifacts/pre2.7-2012-03-16
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:59 +0200 |
parents | 41037d51c8b6 |
children | 27cc95e65f18 |
comparison
equal
deleted
inserted
replaced
2456:60ab1054069d | 3818:dc18457b1cef |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 import org.apache.log4j.Logger; | |
4 | |
5 import java.awt.Color; | |
6 import java.awt.Font; | |
7 | |
8 import org.w3c.dom.Document; | |
9 | |
10 import de.intevation.artifacts.common.utils.XMLUtils; | |
11 | |
12 import de.intevation.flys.artifacts.model.MapserverStyle; | |
13 import de.intevation.flys.artifacts.model.MapserverStyle.Clazz; | |
14 import de.intevation.flys.artifacts.model.MapserverStyle.Style; | |
15 import de.intevation.flys.artifacts.model.MapserverStyle.Label; | |
16 | |
17 | |
18 /** | |
19 * Utility to deal with themes and their representations. | |
20 */ | |
21 public class ThemeUtil { | |
22 | |
23 private static Logger logger = | |
24 Logger.getLogger(ThemeUtil.class); | |
25 | |
26 public final static String XPATH_FILL_COLOR = | |
27 "/theme/field[@name='fillcolor']/@default"; | |
28 | |
29 public final static String XPATH_LINE_COLOR = | |
30 "/theme/field[@name='linecolor']/@default"; | |
31 | |
32 public static final String XPATH_LINE_SIZE = | |
33 "/theme/field[@name='linesize']/@default"; | |
34 | |
35 public static final String XPATH_LINE_STYLE = | |
36 "/theme/field[@name='linetype']/@default"; | |
37 | |
38 public static final String XPATH_POINT_SIZE = | |
39 "/theme/field[@name='pointsize']/@default"; | |
40 | |
41 public final static String XPATH_SHOW_BORDER = | |
42 "/theme/field[@name='showborder']/@default"; | |
43 | |
44 public final static String XPATH_SHOW_POINTS = | |
45 "/theme/field[@name='showpoints']/@default"; | |
46 | |
47 public final static String XPATH_SHOW_LINE = | |
48 "/theme/field[@name='showlines']/@default"; | |
49 | |
50 public final static String XPATH_TRANSPARENCY = | |
51 "/theme/field[@name='transparent']/@default"; | |
52 | |
53 public final static String XPATH_TEXT_COLOR = | |
54 "/theme/field[@name='textcolor']/@default"; | |
55 | |
56 public final static String XPATH_TEXT_SIZE = | |
57 "/theme/field[@name='textsize']/@default"; | |
58 | |
59 public final static String XPATH_TEXT_FONT = | |
60 "/theme/field[@name='font']/@default"; | |
61 | |
62 public final static String XPATH_TEXT_STYLE = | |
63 "/theme/field[@name='textstyle']/@default"; | |
64 | |
65 public final static String XPATH_TEXT_ORIENTATION = | |
66 "/theme/field[@name='textorientation']/@default"; | |
67 | |
68 public final static String XPATH_TEXT_BACKGROUND = | |
69 "/theme/field[@name='backgroundcolor']/@default"; | |
70 | |
71 public final static String XPATH_SHOW_BACKGROUND = | |
72 "/theme/field[@name='showbackground']/@default"; | |
73 | |
74 public final static String XPATH_SYMBOL = | |
75 "/theme/field[@name='symbol']/@default"; | |
76 | |
77 public final static String XPATH_SHOW_MINIMUM = | |
78 "/theme/field[@name='showminimum']/@default"; | |
79 | |
80 public final static String XPATH_SHOW_MAXIMUM = | |
81 "/theme/field[@name='showmaximum']/@default"; | |
82 | |
83 | |
84 /** Parse string to be boolean with default if empty or unrecognized. */ | |
85 public static boolean parseBoolean(String value, boolean defaultsTo) { | |
86 if (value == null || value.length() == 0) { | |
87 return defaultsTo; | |
88 } | |
89 if (value.equals("false")) { | |
90 return false; | |
91 } | |
92 else if (value.equals("true")) { | |
93 return true; | |
94 } | |
95 else { | |
96 return defaultsTo; | |
97 } | |
98 } | |
99 | |
100 | |
101 public static int parseInteger(String value, int defaultsTo) { | |
102 if (value == null || value.length() == 0) { | |
103 return defaultsTo; | |
104 } | |
105 | |
106 try { | |
107 return Integer.valueOf(value); | |
108 } | |
109 catch (NumberFormatException nfe) { | |
110 // do nothing | |
111 } | |
112 | |
113 return defaultsTo; | |
114 } | |
115 | |
116 | |
117 /** | |
118 * Parses line width, defaulting to 0. | |
119 * @param theme the theme | |
120 */ | |
121 public static int parseLineWidth(Document theme) { | |
122 String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null); | |
123 if (size == null || size.length() == 0) { | |
124 return 0; | |
125 } | |
126 | |
127 try { | |
128 return Integer.valueOf(size); | |
129 } | |
130 catch (NumberFormatException nfe) { | |
131 logger.warn("Unable to set line size from string: '" + size + "'"); | |
132 } | |
133 return 0; | |
134 } | |
135 | |
136 | |
137 public static int parsePointWidth(Document theme) { | |
138 String width = XMLUtils.xpathString(theme, XPATH_POINT_SIZE, null); | |
139 | |
140 return parseInteger(width, 3); | |
141 } | |
142 | |
143 | |
144 /** | |
145 * Parses the line style, defaulting to '10'. | |
146 * @param theme The theme. | |
147 */ | |
148 public static float[] parseLineStyle(Document theme) { | |
149 String dash = XMLUtils.xpathString(theme, XPATH_LINE_STYLE, null); | |
150 | |
151 float[] def = {10}; | |
152 if (dash == null || dash.length() == 0) { | |
153 return def; | |
154 } | |
155 | |
156 String[] pattern = dash.split(","); | |
157 if(pattern.length == 1) { | |
158 return def; | |
159 } | |
160 | |
161 try { | |
162 float[] dashes = new float[pattern.length]; | |
163 for (int i = 0; i < pattern.length; i++) { | |
164 dashes[i] = Float.parseFloat(pattern[i]); | |
165 } | |
166 return dashes; | |
167 } | |
168 catch(NumberFormatException nfe) { | |
169 logger.warn("Unable to set dash from string: '" + dash + "'"); | |
170 return def; | |
171 } | |
172 } | |
173 | |
174 | |
175 /** | |
176 * Parses text size, defaulting to 10. | |
177 * @param theme The theme. | |
178 */ | |
179 public static int parseTextSize(Document theme) { | |
180 String size = XMLUtils.xpathString(theme, XPATH_TEXT_SIZE, null); | |
181 if (size == null || size.length() == 0) { | |
182 return 10; | |
183 } | |
184 | |
185 try { | |
186 return Integer.valueOf(size); | |
187 } | |
188 catch (NumberFormatException nfe) { | |
189 } | |
190 return 10; | |
191 } | |
192 | |
193 | |
194 /** | |
195 * Parses the attribute 'showpoints', defaults to false. | |
196 * @param theme The theme. | |
197 */ | |
198 public static boolean parseShowPoints(Document theme) { | |
199 String show = XMLUtils.xpathString(theme, XPATH_SHOW_POINTS, null); | |
200 return parseBoolean(show, false); | |
201 } | |
202 | |
203 | |
204 /** | |
205 * Parses the attribute 'showlines', defaults to true. | |
206 * @param theme The theme. | |
207 */ | |
208 public static boolean parseShowLine(Document theme) { | |
209 String show = XMLUtils.xpathString(theme, XPATH_SHOW_LINE, null); | |
210 return parseBoolean(show, true); | |
211 } | |
212 | |
213 | |
214 /** | |
215 * Parses text color. | |
216 * @param theme The theme. | |
217 */ | |
218 public static Color parseTextColor(Document theme) { | |
219 return parseRGB(getTextColorString(theme)); | |
220 } | |
221 | |
222 | |
223 /** | |
224 * Parses the font. | |
225 * @param theme The theme. | |
226 */ | |
227 public static Font parseTextFont(Document theme) { | |
228 String font = XMLUtils.xpathString(theme, XPATH_TEXT_FONT, null); | |
229 if (font == null || font.length() == 0) { | |
230 return null; | |
231 } | |
232 | |
233 int size = parseTextSize(theme); | |
234 int style = parseTextStyle(theme); | |
235 Font f = new Font (font, style, size); | |
236 return f; | |
237 } | |
238 | |
239 | |
240 /** | |
241 * Parses the text style, defaults to 'Font.PLAIN'. | |
242 * @param theme The theme. | |
243 */ | |
244 public static int parseTextStyle(Document theme) { | |
245 String style = XMLUtils.xpathString(theme, XPATH_TEXT_STYLE, null); | |
246 if (style == null || style.length() == 0) { | |
247 return Font.PLAIN; | |
248 } | |
249 | |
250 if (style.equals("italic")) { | |
251 return Font.ITALIC; | |
252 } | |
253 else if (style.equals("bold")) { | |
254 return Font.BOLD; | |
255 } | |
256 else { | |
257 return Font.PLAIN; | |
258 } | |
259 } | |
260 | |
261 | |
262 /** | |
263 * Parses the textorientation, defaults to 'vertical'. | |
264 * @param theme The theme. | |
265 */ | |
266 public static String parseTextOrientation(Document theme) { | |
267 String o = XMLUtils.xpathString(theme, XPATH_TEXT_ORIENTATION, null); | |
268 if (o == null || o.length() == 0) { | |
269 return "vertical"; | |
270 } | |
271 if(o.equals("true")) { | |
272 return "horizontal"; | |
273 } | |
274 else { | |
275 return "vertical"; | |
276 } | |
277 } | |
278 | |
279 | |
280 /** | |
281 * Parses the text background color, defaults to white. | |
282 * @param theme The theme. | |
283 */ | |
284 public static Color parseTextBackground(Document theme) { | |
285 String color = getBackgroundColorString(theme); | |
286 if (color == null || color.length() == 0) { | |
287 return Color.WHITE; | |
288 } | |
289 return parseRGB(color); | |
290 } | |
291 | |
292 | |
293 /** | |
294 * Parses the attribute whether to show background or not, defaults to | |
295 * false. | |
296 * @param theme The theme. | |
297 */ | |
298 public static boolean parseShowTextBackground(Document theme) { | |
299 String show = XMLUtils.xpathString(theme, XPATH_SHOW_BACKGROUND, null); | |
300 return parseBoolean(show, false); | |
301 } | |
302 | |
303 | |
304 /** | |
305 * Parse a string like "103, 100, 0" and return a corresping color. | |
306 * @param rgbtext Color as string representation, e.g. "255,0,20". | |
307 * @return Color, null in case of issues. | |
308 */ | |
309 public static Color parseRGB(String rgbtext) { | |
310 if (rgbtext == null) { | |
311 return null; | |
312 } | |
313 String rgb[] = rgbtext.split(","); | |
314 Color c = null; | |
315 try { | |
316 c = new Color( | |
317 Integer.valueOf(rgb[0].trim()), | |
318 Integer.valueOf(rgb[1].trim()), | |
319 Integer.valueOf(rgb[2].trim())); | |
320 } | |
321 catch (NumberFormatException nfe) { | |
322 c = null; | |
323 } | |
324 return c; | |
325 } | |
326 | |
327 | |
328 public static String getLineColorString(Document theme) { | |
329 return XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null); | |
330 } | |
331 | |
332 /** Get show border as string. */ | |
333 public static String getShowBorderString(Document theme) { | |
334 return XMLUtils.xpathString(theme, XPATH_SHOW_BORDER, null); | |
335 } | |
336 | |
337 /** Get fill color as string. */ | |
338 public static String getFillColorString(Document theme) { | |
339 return XMLUtils.xpathString(theme, XPATH_FILL_COLOR, null); | |
340 } | |
341 | |
342 public static String getBackgroundColorString(Document theme) { | |
343 return XMLUtils.xpathString(theme, XPATH_TEXT_BACKGROUND, null); | |
344 } | |
345 | |
346 | |
347 public static String getTextColorString(Document theme) { | |
348 return XMLUtils.xpathString(theme, XPATH_TEXT_COLOR, null); | |
349 } | |
350 | |
351 | |
352 public static String getSymbol(Document theme) { | |
353 return XMLUtils.xpathString(theme, XPATH_SYMBOL, null); | |
354 } | |
355 | |
356 public static String getTransparencyString(Document theme) { | |
357 return XMLUtils.xpathString(theme, XPATH_TRANSPARENCY, null); | |
358 } | |
359 | |
360 | |
361 public static String getShowMinimum(Document theme) { | |
362 return XMLUtils.xpathString(theme, XPATH_SHOW_MINIMUM, null); | |
363 } | |
364 | |
365 | |
366 public static String getShowMaximum(Document theme) { | |
367 return XMLUtils.xpathString(theme, XPATH_SHOW_MAXIMUM, null); | |
368 } | |
369 | |
370 | |
371 /** | |
372 * Gets color from color field. | |
373 * @param theme the theme document. | |
374 * @return color. | |
375 */ | |
376 public static Color parseFillColorField(Document theme) { | |
377 return parseRGB(getFillColorString(theme)); | |
378 } | |
379 | |
380 public static boolean parseShowBorder(Document theme) { | |
381 return parseBoolean(getShowBorderString(theme), false); | |
382 } | |
383 | |
384 public static boolean parseTransparency(Document theme) { | |
385 return parseBoolean(getTransparencyString(theme), false); | |
386 } | |
387 | |
388 /** | |
389 * Gets color from color field. | |
390 * @param theme the theme document. | |
391 * @return color. | |
392 */ | |
393 public static Color parseLineColorField(Document theme) { | |
394 return parseRGB(getLineColorString(theme)); | |
395 } | |
396 | |
397 | |
398 public static boolean parseShowMinimum(Document theme) { | |
399 return parseBoolean(getShowMinimum(theme), false); | |
400 } | |
401 | |
402 public static boolean parseShowMaximum(Document theme) { | |
403 return parseBoolean(getShowMaximum(theme), false); | |
404 } | |
405 | |
406 | |
407 public static String createMapserverStyle(Document theme) { | |
408 String symbol = getSymbol(theme); | |
409 String backcolor = getBackgroundColorString(theme); | |
410 String linecolor = getLineColorString(theme); | |
411 | |
412 int linewidth = parseLineWidth(theme); | |
413 | |
414 MapserverStyle ms = new MapserverStyle(); | |
415 | |
416 Clazz c = new Clazz(" "); | |
417 | |
418 Style s = new Style(); | |
419 s.setOutlineColor(linecolor.replace(",", "")); | |
420 | |
421 if (backcolor != null && backcolor.length() > 0) { | |
422 s.setColor(backcolor.replace(",", "")); | |
423 } | |
424 | |
425 s.setSize(linewidth); | |
426 s.setSymbol(symbol); | |
427 c.addItem(s); | |
428 | |
429 String textcolor = getTextColorString(theme); | |
430 int textsize = parseTextSize(theme); | |
431 | |
432 if (textcolor != null && textcolor.length() > 0 && textsize > 0) { | |
433 Label l = new Label(); | |
434 l.setColor(textcolor.replace(",", "")); | |
435 l.setSize(textsize); | |
436 c.addItem(l); | |
437 } | |
438 | |
439 ms.addClazz(c); | |
440 | |
441 return ms.toString(); | |
442 } | |
443 } | |
444 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |