Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java @ 7143:053e39436ba3
Diagrams: Determine if axis should be inverted dynamically.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Wed, 25 Sep 2013 12:36:19 +0200 |
parents | 038a04e001d7 |
children | f2ffa631c2ed |
comparison
equal
deleted
inserted
replaced
7142:8c70767028e1 | 7143:053e39436ba3 |
---|---|
27 | 27 |
28 public class DiagramAttributes | 28 public class DiagramAttributes |
29 implements ElementConverter | 29 implements ElementConverter |
30 { | 30 { |
31 private static Logger log = Logger.getLogger(DiagramAttributes.class); | 31 private static Logger log = Logger.getLogger(DiagramAttributes.class); |
32 | |
33 public interface Evaluator { | |
34 Object evaluate(D4EArtifact artifact, CallContext context); | |
35 } // interface Evaluator | |
36 | |
37 public static final Evaluator TRUE = new Evaluator() { | |
38 @Override | |
39 public Object evaluate(D4EArtifact artifact, CallContext context) { | |
40 return Boolean.TRUE; | |
41 } | |
42 }; | |
43 | |
44 public static final Evaluator FALSE = new Evaluator() { | |
45 @Override | |
46 public Object evaluate(D4EArtifact artifact, CallContext context) { | |
47 return Boolean.TRUE; | |
48 } | |
49 }; | |
32 | 50 |
33 public class Instance { | 51 public class Instance { |
34 | 52 |
35 private List<Processor> processors; | 53 private List<Processor> processors; |
36 | 54 |
71 | 89 |
72 public Title getSubtitle() { | 90 public Title getSubtitle() { |
73 return DiagramAttributes.this.getSubtitle(); | 91 return DiagramAttributes.this.getSubtitle(); |
74 } | 92 } |
75 | 93 |
76 public Title getDomainAxisTitle() { | 94 public DomainAxisAttributes getDomainAxis() { |
77 return DiagramAttributes.this.getDomainAxisTitle(); | 95 return DiagramAttributes.this.getDomainAxis(); |
78 } | 96 } |
79 | 97 |
80 public int getAxisIndex(String axisName) { | 98 public int getAxisIndex(String axisName) { |
81 return DiagramAttributes.this.getAxisIndex(axisName); | 99 return DiagramAttributes.this.getAxisIndex(axisName); |
82 } | 100 } |
94 private String name; | 112 private String name; |
95 private boolean isLeftAlign; | 113 private boolean isLeftAlign; |
96 private boolean forceAlign; | 114 private boolean forceAlign; |
97 private boolean includeZero; | 115 private boolean includeZero; |
98 | 116 |
117 private Evaluator isInverted; | |
118 | |
99 public AxisAttributes() { | 119 public AxisAttributes() { |
100 } | 120 } |
101 | 121 |
102 public AxisAttributes( | 122 public AxisAttributes( |
103 String name, | 123 String name, |
104 boolean isLeftAlign, | 124 boolean isLeftAlign, |
105 boolean forceAlign, | 125 boolean forceAlign, |
106 boolean includeZero | 126 boolean includeZero, |
127 Evaluator isInverted | |
107 ) { | 128 ) { |
108 this.name = name; | 129 this.name = name; |
109 this.isLeftAlign = isLeftAlign; | 130 this.isLeftAlign = isLeftAlign; |
110 this.forceAlign = forceAlign; | 131 this.forceAlign = forceAlign; |
111 this.includeZero = includeZero; | 132 this.includeZero = includeZero; |
133 this.isInverted = isInverted; | |
112 } | 134 } |
113 | 135 |
114 public String getName() { | 136 public String getName() { |
115 return name; | 137 return name; |
116 } | 138 } |
124 } | 146 } |
125 | 147 |
126 public boolean includeZero() { | 148 public boolean includeZero() { |
127 return includeZero; | 149 return includeZero; |
128 } | 150 } |
151 | |
152 public Evaluator isInverted() { | |
153 return isInverted; | |
154 } | |
129 } // class AxisAttributes | 155 } // class AxisAttributes |
156 | |
157 public class DomainAxisAttributes extends AxisAttributes { | |
158 | |
159 private Title title; | |
160 | |
161 public DomainAxisAttributes() { | |
162 } | |
163 | |
164 public DomainAxisAttributes( | |
165 String name, | |
166 boolean isLeftAlign, | |
167 boolean forceAlign, | |
168 boolean includeZero, | |
169 Evaluator isInverted, | |
170 Title title | |
171 ) { | |
172 super(name, isLeftAlign, forceAlign, includeZero, isInverted); | |
173 this.title = title; | |
174 } | |
175 | |
176 public Title getTitle() { | |
177 return title; | |
178 } | |
179 } // class DomainAxisAttributes | |
130 | 180 |
131 public static class AxisProcessor { | 181 public static class AxisProcessor { |
132 | 182 |
133 private Class<Processor> processorClass; | 183 private Class<Processor> processorClass; |
134 private String axisName; | 184 private String axisName; |
238 private List<AxisAttributes> axesAttrs; | 288 private List<AxisAttributes> axesAttrs; |
239 private List<AxisProcessor> axesProcessors; | 289 private List<AxisProcessor> axesProcessors; |
240 | 290 |
241 private Title title; | 291 private Title title; |
242 private Title subtitle; | 292 private Title subtitle; |
243 private Title domainAxisTitle; | 293 |
294 private DomainAxisAttributes domainAxis; | |
244 | 295 |
245 public DiagramAttributes() { | 296 public DiagramAttributes() { |
246 axesAttrs = new ArrayList<AxisAttributes>(5); | 297 axesAttrs = new ArrayList<AxisAttributes>(5); |
247 axesProcessors = new ArrayList<AxisProcessor>(5); | 298 axesProcessors = new ArrayList<AxisProcessor>(5); |
248 } | 299 } |
251 public Object convert(Element config) { | 302 public Object convert(Element config) { |
252 parseAxis(config); | 303 parseAxis(config); |
253 parseProcessors(config); | 304 parseProcessors(config); |
254 parseTitle(config); | 305 parseTitle(config); |
255 parseSubtitle(config); | 306 parseSubtitle(config); |
256 parseDomainAxisTitle(config); | 307 parseDomainAxis(config); |
257 return this; | 308 return this; |
258 } | 309 } |
259 | 310 |
260 public List<AxisAttributes> getAxesAttributes() { | 311 public List<AxisAttributes> getAxesAttributes() { |
261 return axesAttrs; | 312 return axesAttrs; |
268 Element axisElement = (Element)axisNodes.item(i); | 319 Element axisElement = (Element)axisNodes.item(i); |
269 String name = axisElement.getAttribute("name").trim(); | 320 String name = axisElement.getAttribute("name").trim(); |
270 String align = axisElement.getAttribute("align").trim(); | 321 String align = axisElement.getAttribute("align").trim(); |
271 String includeZero = | 322 String includeZero = |
272 axisElement.getAttribute("include-zero").trim(); | 323 axisElement.getAttribute("include-zero").trim(); |
324 | |
325 String isInverted = axisElement.getAttribute("inverted"); | |
326 | |
273 if (name.isEmpty()) { | 327 if (name.isEmpty()) { |
274 continue; | 328 continue; |
275 } | 329 } |
276 boolean isleftAlign = false; | 330 boolean isleftAlign = false; |
277 boolean forceAlign = false; | 331 boolean forceAlign = false; |
280 if ("left" .equals(part)) isleftAlign = true; | 334 if ("left" .equals(part)) isleftAlign = true; |
281 else if ("right".equals(part)) isleftAlign = false; | 335 else if ("right".equals(part)) isleftAlign = false; |
282 else if ("force".equals(part)) forceAlign = true; | 336 else if ("force".equals(part)) forceAlign = true; |
283 } | 337 } |
284 | 338 |
339 Evaluator isInvertedE = parseEvaluator(isInverted, FALSE); | |
340 | |
285 axesAttrs.add(new AxisAttributes( | 341 axesAttrs.add(new AxisAttributes( |
286 name, isleftAlign, forceAlign, | 342 name, isleftAlign, forceAlign, |
287 includeZero.equals("true"))); | 343 includeZero.equals("true"), |
288 } | 344 isInvertedE)); |
345 } | |
346 } | |
347 | |
348 private Evaluator parseEvaluator(String s, Evaluator def) { | |
349 if ((s = s.trim()).isEmpty()) return def; | |
350 if ("true".equals(s)) return TRUE; | |
351 if ("false".equals(s)) return FALSE; | |
352 if (s.endsWith("()")) { | |
353 s = s.substring(0, s.length()-2).trim(); | |
354 try { | |
355 Class<Evaluator> clazz = (Class<Evaluator>)Class.forName(s); | |
356 return clazz.newInstance(); | |
357 } | |
358 catch (ClassNotFoundException cnfe) { | |
359 log.error(cnfe, cnfe); | |
360 } | |
361 catch (InstantiationException ie) { | |
362 log.error(ie, ie); | |
363 } | |
364 catch (IllegalAccessException iae) { | |
365 log.error(iae, iae); | |
366 } | |
367 } | |
368 return def; | |
289 } | 369 } |
290 | 370 |
291 public List<AxisProcessor> getAxesProcessors() { | 371 public List<AxisProcessor> getAxesProcessors() { |
292 return axesProcessors; | 372 return axesProcessors; |
293 } | 373 } |
298 | 378 |
299 public Title getSubtitle() { | 379 public Title getSubtitle() { |
300 return subtitle; | 380 return subtitle; |
301 } | 381 } |
302 | 382 |
303 public Title getDomainAxisTitle() { | 383 public DomainAxisAttributes getDomainAxis() { |
304 return domainAxisTitle; | 384 return domainAxis; |
305 } | 385 } |
306 | 386 |
307 private void parseProcessors(Element config) { | 387 private void parseProcessors(Element config) { |
308 NodeList processorNodes = config.getElementsByTagName("processor"); | 388 NodeList processorNodes = config.getElementsByTagName("processor"); |
309 | 389 |
332 | 412 |
333 private void parseSubtitle(Element config) { | 413 private void parseSubtitle(Element config) { |
334 subtitle = extractTitle(config, "subtitle"); | 414 subtitle = extractTitle(config, "subtitle"); |
335 } | 415 } |
336 | 416 |
337 private void parseDomainAxisTitle(Element config) { | 417 private void parseDomainAxis(Element config) { |
338 domainAxisTitle = extractTitle(config, "domain-axis"); | 418 Title title = extractTitle(config, "domain-axis"); |
419 String includeZero = config.getAttribute("include-zero"); | |
420 String isInverted = config.getAttribute("inverted"); | |
421 | |
422 domainAxis = new DomainAxisAttributes( | |
423 "X", | |
424 false, | |
425 false, | |
426 false, | |
427 parseEvaluator(isInverted, FALSE), | |
428 title); | |
339 } | 429 } |
340 | 430 |
341 private static Title extractTitle(Element config, String tagName) { | 431 private static Title extractTitle(Element config, String tagName) { |
342 NodeList titleNodes = config.getElementsByTagName(tagName); | 432 NodeList titleNodes = config.getElementsByTagName(tagName); |
343 if (titleNodes.getLength() < 1) { | 433 if (titleNodes.getLength() < 1) { |