comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/LocationDistanceSelect.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/LocationDistanceSelect.java@eb5564662e19
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.artifacts.states;
2
3 import org.apache.log4j.Logger;
4
5 import gnu.trove.TDoubleArrayList;
6
7 import org.dive4elements.artifacts.Artifact;
8
9 import org.dive4elements.artifactdatabase.data.StateData;
10
11 import org.dive4elements.river.artifacts.FLYSArtifact;
12 import org.dive4elements.river.artifacts.WINFOArtifact;
13
14
15 /**
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
17 */
18 public class LocationDistanceSelect
19 extends ComputationRangeState
20 {
21
22 /** The logger used in this class. */
23 private static Logger logger = Logger.getLogger(LocationDistanceSelect.class);
24
25 /** The name of the 'mode' field. */
26 public static final String MODE = "ld_mode";
27
28 /** The name of the 'locations' field. */
29 public static final String LOCATIONS = "ld_locations";
30
31
32 /**
33 * The default constructor that initializes an empty State object.
34 */
35 public LocationDistanceSelect() {
36 }
37
38
39 @Override
40 protected String getUIProvider() {
41 return "location_distance_panel";
42 }
43
44
45 /** Validates the range (or location). */
46 @Override
47 public boolean validate(Artifact artifact)
48 throws IllegalArgumentException
49 {
50 logger.debug("LocationDistanceSelect.validate");
51
52 FLYSArtifact flys = (FLYSArtifact)artifact;
53 StateData mode = getData(flys, MODE);
54 String mValue = mode != null ? (String)mode.getValue() : null;
55 if (mValue != null) {
56 if (mValue.equals("distance")) {
57 return super.validate(flys);
58 }
59 else {
60 return validateLocations(flys);
61 }
62 }
63 return false;
64 }
65
66
67 /** Validate selected locations. */
68 protected boolean validateLocations(FLYSArtifact flys)
69 throws IllegalArgumentException
70 {
71 StateData dValues = getData(flys, LOCATIONS);
72 String values = dValues != null ? (String)dValues.getValue() : null;
73
74 if (values == null || values.length() == 0) {
75 throw new IllegalArgumentException("error_empty_state");
76 }
77
78 double[] absMinMax = getMinMax(flys);
79 double[] relMinMax = getMinMaxFromString(values);
80
81 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) {
82 throw new IllegalArgumentException("error_feed_from_out_of_range");
83 }
84
85 if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) {
86 throw new IllegalArgumentException("error_feed_to_out_of_range");
87 }
88
89 return true;
90 }
91
92
93 /**
94 * Extracts the min/max values from String <i>s</i>. An
95 * IllegalArgumentException is thrown if there is a value that throws a
96 * NumberFormatException.
97 *
98 * @param s String that contains whitespace separated double values.
99 *
100 * @return a 2dmin array [min,max].
101 */
102 public static double[] getMinMaxFromString(String s)
103 throws IllegalArgumentException
104 {
105 String[] values = s.split(" ");
106
107 double[] minmax = new double[] {
108 Double.MAX_VALUE,
109 -Double.MAX_VALUE };
110
111 for (String v: values) {
112 try {
113 double value = Double.valueOf(v);
114
115 minmax[0] = minmax[0] < value ? minmax[0] : value;
116 minmax[1] = minmax[1] > value ? minmax[1] : value;
117 }
118 catch (NumberFormatException nfe) {
119 throw new IllegalArgumentException(
120 "error_invalid_double_value");
121 }
122 }
123
124 return minmax;
125 }
126
127
128 public static double[] getLocations(WINFOArtifact flys) {
129 StateData data = flys.getData(LOCATIONS);
130 String value = data != null ? (String) data.getValue() : null;
131
132 if (value == null || value.length() == 0) {
133 logger.warn("No location data given.");
134 return null;
135 }
136
137 String[] splitted = value.split(" ");
138 TDoubleArrayList values = new TDoubleArrayList();
139
140 for (String split: splitted) {
141 try {
142 values.add(Double.valueOf(split));
143 }
144 catch (NumberFormatException nfe) {
145 logger.warn(nfe, nfe);
146 }
147 }
148
149 return values.toNativeArray();
150 }
151 }
152 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org