comparison flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 3814:8083f6384023

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

http://dive4elements.wald.intevation.org