comparison flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 3812:f788d2d901d6

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

http://dive4elements.wald.intevation.org