Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationSelect.java @ 430:7ab81ff32111 2.3
merged flys-artifacts/2.3
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:10 +0200 |
parents | 478940d06876 |
children | 929137ee8154 |
comparison
equal
deleted
inserted
replaced
290:a6f56ed9238b | 430:7ab81ff32111 |
---|---|
1 package de.intevation.flys.artifacts.states; | |
2 | |
3 import java.util.Map; | |
4 | |
5 import gnu.trove.TDoubleArrayList; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import de.intevation.artifacts.Artifact; | |
10 import de.intevation.artifacts.CallContext; | |
11 | |
12 import de.intevation.artifactdatabase.data.StateData; | |
13 | |
14 | |
15 /** | |
16 * This state is used to realize the input of multiple locations as string. The | |
17 * string should be a whitespace separated list of double values where each | |
18 * double value represents a location. | |
19 * | |
20 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
21 */ | |
22 public class LocationSelect extends LocationDistanceSelect { | |
23 | |
24 /** The logger used in this class.*/ | |
25 private static Logger logger = Logger.getLogger(LocationSelect.class); | |
26 | |
27 | |
28 /** The name of the StateData object that stores the location string.*/ | |
29 public static final String FIELD_LOCATIONS = "ld_locations"; | |
30 | |
31 | |
32 public LocationSelect() { | |
33 } | |
34 | |
35 | |
36 @Override | |
37 protected String getUIProvider() { | |
38 return "location_panel"; | |
39 } | |
40 | |
41 | |
42 @Override | |
43 public boolean validate(Artifact artifact, CallContext context) | |
44 throws IllegalArgumentException | |
45 { | |
46 logger.debug("LocationSelect.validate"); | |
47 | |
48 Map<String, StateData> data = getData(); | |
49 | |
50 String locationStr = (String) data.get(FIELD_LOCATIONS).getValue(); | |
51 | |
52 if (locationStr == null || locationStr.length() == 0) { | |
53 logger.error("No locations given."); | |
54 throw new IllegalArgumentException("error_empty_state"); | |
55 } | |
56 | |
57 double[] minmax = getMinMaxDistance(artifact); | |
58 double[] mm = extractLocations(locationStr); | |
59 | |
60 logger.debug("Inserted min location: " + mm[0]); | |
61 logger.debug("Inserted max location: " + mm[mm.length-1]); | |
62 | |
63 return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length-1], 0d); | |
64 } | |
65 | |
66 | |
67 /** | |
68 * This method takes a string that consist of whitespace separated double | |
69 * values and returns the double values as array. | |
70 * | |
71 * @param locationStr The locations inserted in this state. | |
72 * | |
73 * @return the locations as array. | |
74 */ | |
75 protected double[] extractLocations(String locationStr) { | |
76 String[] tmp = locationStr.split(" "); | |
77 TDoubleArrayList locations = new TDoubleArrayList(); | |
78 | |
79 for (String l: tmp) { | |
80 try { | |
81 locations.add(Double.parseDouble(l)); | |
82 } | |
83 catch (NumberFormatException nfe) { | |
84 logger.warn(nfe, nfe); | |
85 } | |
86 } | |
87 | |
88 locations.sort(); | |
89 | |
90 return locations.toNativeArray(); | |
91 } | |
92 } | |
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |