comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java @ 3318:dbe2f85bf160

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

http://dive4elements.wald.intevation.org