comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/LocationSelect.java @ 5831:bd047b71ab37

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

http://dive4elements.wald.intevation.org