Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.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 | c1f445b94d03 |
comparison
equal
deleted
inserted
replaced
2456:60ab1054069d | 3818:dc18457b1cef |
---|---|
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 @Override | |
45 public boolean validate(Artifact artifact) | |
46 throws IllegalArgumentException | |
47 { | |
48 logger.debug("LocationDistanceSelect.validate"); | |
49 | |
50 WINFOArtifact flys = (WINFOArtifact) artifact; | |
51 | |
52 if (flys.isRange()) { | |
53 return super.validate(flys); | |
54 } | |
55 else { | |
56 return validateLocations(flys); | |
57 } | |
58 } | |
59 | |
60 | |
61 protected boolean validateLocations(WINFOArtifact flys) | |
62 throws IllegalArgumentException | |
63 { | |
64 StateData dValues = getData(flys, LOCATIONS); | |
65 String values = dValues != null ? (String)dValues.getValue() : null; | |
66 | |
67 if (values == null || values.length() == 0) { | |
68 throw new IllegalArgumentException("error_empty_state"); | |
69 } | |
70 | |
71 double[] absMinMax = getMinMax(flys); | |
72 double[] relMinMax = getMinMaxFromString(values); | |
73 | |
74 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) { | |
75 throw new IllegalArgumentException("error_feed_from_out_of_range"); | |
76 } | |
77 | |
78 if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) { | |
79 throw new IllegalArgumentException("error_feed_to_out_of_range"); | |
80 } | |
81 | |
82 return true; | |
83 } | |
84 | |
85 | |
86 /** | |
87 * Extracts the min/max values from String <i>s</i>. An | |
88 * IllegalArgumentException is thrown if there is a value that throws a | |
89 * NumberFormatException. | |
90 * | |
91 * @param s String that contains whitespace separated double values. | |
92 * | |
93 * @return a 2dmin array [min,max]. | |
94 */ | |
95 public static double[] getMinMaxFromString(String s) | |
96 throws IllegalArgumentException | |
97 { | |
98 String[] values = s.split(" "); | |
99 | |
100 double[] minmax = new double[] { | |
101 Double.MAX_VALUE, | |
102 -Double.MAX_VALUE }; | |
103 | |
104 for (String v: values) { | |
105 try { | |
106 double value = Double.valueOf(v); | |
107 | |
108 minmax[0] = minmax[0] < value ? minmax[0] : value; | |
109 minmax[1] = minmax[1] > value ? minmax[1] : value; | |
110 } | |
111 catch (NumberFormatException nfe) { | |
112 throw new IllegalArgumentException( | |
113 "error_invalid_double_value"); | |
114 } | |
115 } | |
116 | |
117 return minmax; | |
118 } | |
119 | |
120 | |
121 public static double[] getLocations(WINFOArtifact flys) { | |
122 StateData data = flys.getData(LOCATIONS); | |
123 String value = data != null ? (String) data.getValue() : null; | |
124 | |
125 if (value == null || value.length() == 0) { | |
126 logger.warn("No location data given."); | |
127 return null; | |
128 } | |
129 | |
130 String[] splitted = value.split(" "); | |
131 TDoubleArrayList values = new TDoubleArrayList(); | |
132 | |
133 for (String split: splitted) { | |
134 try { | |
135 values.add(Double.valueOf(split)); | |
136 } | |
137 catch (NumberFormatException nfe) { | |
138 logger.warn(nfe, nfe); | |
139 } | |
140 } | |
141 | |
142 return values.toNativeArray(); | |
143 } | |
144 } | |
145 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |