comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationSelect.java @ 1190:f514894ec2fd

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

http://dive4elements.wald.intevation.org