comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationSelect.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 ee5310134463
children d9af29a4bb85
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
1 package de.intevation.flys.artifacts.states;
2
3 import gnu.trove.TDoubleArrayList;
4
5 import org.apache.log4j.Logger;
6
7 import org.w3c.dom.Element;
8
9 import de.intevation.artifacts.Artifact;
10 import de.intevation.artifacts.CallContext;
11
12 import de.intevation.artifacts.common.utils.XMLUtils;
13
14 import de.intevation.artifactdatabase.data.StateData;
15
16 import de.intevation.flys.artifacts.FLYSArtifact;
17
18
19 /**
20 * This state is used to realize the input of multiple locations as string. The
21 * string should be a whitespace separated list of double values where each
22 * double value represents a location.
23 *
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
25 */
26 public class LocationSelect extends LocationDistanceSelect {
27
28 /** The logger used in this class.*/
29 private static Logger logger = Logger.getLogger(LocationSelect.class);
30
31
32 public LocationSelect() {
33 }
34
35
36 @Override
37 protected String getUIProvider() {
38 return "location_panel";
39 }
40
41
42 @Override
43 protected Element[] createItems(
44 XMLUtils.ElementCreator cr,
45 Artifact artifact,
46 String name,
47 CallContext context)
48 {
49 double[] minmax = getMinMax(artifact);
50
51 double minVal = Double.MIN_VALUE;
52 double maxVal = Double.MAX_VALUE;
53
54 if (minmax != null) {
55 minVal = minmax[0];
56 maxVal = minmax[1];
57 }
58 else {
59 logger.warn("Could not read min/max distance values!");
60 }
61
62 if (name.equals(LOCATIONS)) {
63 Element min = createItem(
64 cr,
65 new String[] {"min", new Double(minVal).toString()});
66
67 Element max = createItem(
68 cr,
69 new String[] {"max", new Double(maxVal).toString()});
70
71 return new Element[] { min, max };
72 }
73
74 return null;
75 }
76
77
78 @Override
79 public boolean validate(Artifact artifact)
80 throws IllegalArgumentException
81 {
82 logger.debug("LocationSelect.validate");
83
84 FLYSArtifact flys = (FLYSArtifact) artifact;
85 StateData data = getData(flys, LOCATIONS);
86
87 String locationStr = data != null
88 ? (String) data.getValue()
89 : null;
90
91 if (locationStr == null || locationStr.length() == 0) {
92 logger.error("No locations given.");
93 throw new IllegalArgumentException("error_empty_state");
94 }
95
96 double[] minmax = getMinMax(artifact);
97 double[] mm = extractLocations(locationStr);
98
99 logger.debug("Inserted min location: " + mm[0]);
100 logger.debug("Inserted max location: " + mm[mm.length-1]);
101
102 return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length-1], 0d);
103 }
104
105
106 /**
107 * This method takes a string that consist of whitespace separated double
108 * values and returns the double values as array.
109 *
110 * @param locationStr The locations inserted in this state.
111 *
112 * @return the locations as array.
113 */
114 protected double[] extractLocations(String locationStr) {
115 String[] tmp = locationStr.split(" ");
116 TDoubleArrayList locations = new TDoubleArrayList();
117
118 for (String l: tmp) {
119 try {
120 locations.add(Double.parseDouble(l));
121 }
122 catch (NumberFormatException nfe) {
123 logger.warn(nfe, nfe);
124 }
125 }
126
127 locations.sort();
128
129 return locations.toNativeArray();
130 }
131 }
132 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org