Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.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 | c21fb8de54f8 |
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 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.ProtocolUtils; | |
15 import de.intevation.artifactdatabase.data.StateData; | |
16 | |
17 import de.intevation.flys.model.River; | |
18 | |
19 import de.intevation.flys.artifacts.FLYSArtifact; | |
20 import de.intevation.flys.artifacts.model.RiverFactory; | |
21 import de.intevation.flys.artifacts.resources.Resources; | |
22 | |
23 /** | |
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
25 */ | |
26 public class LocationDistanceSelect extends RangeState { | |
27 | |
28 /** The logger used in this class.*/ | |
29 private static Logger logger = Logger.getLogger(LocationDistanceSelect.class); | |
30 | |
31 | |
32 /** The default step width.*/ | |
33 public static final String DEFAULT_STEP = "100"; | |
34 | |
35 /** The name of the 'from' field. */ | |
36 public static final String FROM = "ld_from"; | |
37 | |
38 /** The name of the 'to' field. */ | |
39 public static final String TO = "ld_to"; | |
40 | |
41 /** The name of the 'step' field. */ | |
42 public static final String STEP = "ld_step"; | |
43 | |
44 /** | |
45 * The default constructor that initializes an empty State object. | |
46 */ | |
47 public LocationDistanceSelect() { | |
48 } | |
49 | |
50 protected Element createData( | |
51 XMLUtils.ElementCreator cr, | |
52 Artifact artifact, | |
53 StateData data, | |
54 CallContext context) | |
55 { | |
56 Element select = ProtocolUtils.createArtNode( | |
57 cr, "select", null, null); | |
58 | |
59 cr.addAttr(select, "name", data.getName(), true); | |
60 | |
61 Element label = ProtocolUtils.createArtNode( | |
62 cr, "label", null, null); | |
63 | |
64 Element choices = ProtocolUtils.createArtNode( | |
65 cr, "choices", null, null); | |
66 | |
67 label.setTextContent(Resources.getMsg( | |
68 context.getMeta(), | |
69 data.getName(), | |
70 data.getName())); | |
71 | |
72 select.appendChild(label); | |
73 | |
74 return select; | |
75 } | |
76 | |
77 | |
78 protected Element[] createItems( | |
79 XMLUtils.ElementCreator cr, | |
80 Artifact artifact, | |
81 String name, | |
82 CallContext context) | |
83 { | |
84 double[] minmax = getMinMaxDistance(artifact); | |
85 | |
86 double minVal = Double.MIN_VALUE; | |
87 double maxVal = Double.MAX_VALUE; | |
88 | |
89 if (minmax != null) { | |
90 minVal = minmax[0]; | |
91 maxVal = minmax[1]; | |
92 } | |
93 else { | |
94 logger.warn("Could not read min/max distance values!"); | |
95 } | |
96 | |
97 if (name.equals("ld_from")) { | |
98 Element min = createItem( | |
99 cr, | |
100 new String[] {"min", new Double(minVal).toString()}); | |
101 | |
102 return new Element[] { min }; | |
103 } | |
104 else if (name.equals("ld_to")) { | |
105 Element max = createItem( | |
106 cr, | |
107 new String[] {"max", new Double(maxVal).toString()}); | |
108 | |
109 return new Element[] { max }; | |
110 } | |
111 else { | |
112 Element step = createItem(cr, new String[] {"step", DEFAULT_STEP}); | |
113 return new Element[] { step }; | |
114 } | |
115 | |
116 } | |
117 | |
118 | |
119 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { | |
120 Element item = ProtocolUtils.createArtNode(cr, "item", null, null); | |
121 Element label = ProtocolUtils.createArtNode(cr, "label", null, null); | |
122 Element value = ProtocolUtils.createArtNode(cr, "value", null, null); | |
123 | |
124 String[] arr = (String[]) obj; | |
125 | |
126 label.setTextContent(arr[0]); | |
127 value.setTextContent(arr[1]); | |
128 | |
129 item.appendChild(label); | |
130 item.appendChild(value); | |
131 | |
132 return item; | |
133 } | |
134 | |
135 | |
136 protected String getUIProvider() { | |
137 return "location_distance_panel"; | |
138 } | |
139 | |
140 | |
141 protected double[] getMinMaxDistance(Artifact artifact) { | |
142 FLYSArtifact flysArtifact = (FLYSArtifact) artifact; | |
143 StateData data = flysArtifact.getData("river"); | |
144 | |
145 String name = (String) data.getValue(); | |
146 | |
147 logger.debug("Search for the min/max distances of '" + name + "'"); | |
148 | |
149 River river = RiverFactory.getRiver(name); | |
150 | |
151 return river != null ? river.determineMinMaxDistance() : null; | |
152 } | |
153 | |
154 | |
155 @Override | |
156 public boolean validate(Artifact artifact, CallContext context) | |
157 throws IllegalArgumentException | |
158 { | |
159 logger.debug("LocationDistanceSelect.validate"); | |
160 | |
161 Map<String, StateData> data = getData(); | |
162 | |
163 String fromStr = (String) data.get(FROM).getValue(); | |
164 String toStr = (String) data.get(TO).getValue(); | |
165 String stepStr = (String) data.get(STEP).getValue(); | |
166 | |
167 if (fromStr == null || toStr == null || stepStr == null) { | |
168 throw new IllegalArgumentException("error_empty_state"); | |
169 } | |
170 | |
171 try { | |
172 double from = Double.parseDouble(fromStr); | |
173 double to = Double.parseDouble(toStr); | |
174 double step = Double.parseDouble(stepStr); | |
175 | |
176 double[] minmaxDist = getMinMaxDistance(artifact); | |
177 | |
178 return validateBounds(minmaxDist[0], minmaxDist[1], from, to, step); | |
179 } | |
180 catch (NumberFormatException nfe) { | |
181 throw new IllegalArgumentException("error_feed_number_format"); | |
182 } | |
183 } | |
184 } | |
185 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |