Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/LocationPanel.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-client/src/main/java/org/dive4elements/river/client/client/ui/LocationPanel.java@821a02bbfb4e |
children | 172338b1407f |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.client.client.ui; | |
2 | |
3 import com.google.gwt.core.client.GWT; | |
4 import com.google.gwt.i18n.client.NumberFormat; | |
5 | |
6 import com.smartgwt.client.data.Record; | |
7 import com.smartgwt.client.util.SC; | |
8 import com.smartgwt.client.widgets.Canvas; | |
9 import com.smartgwt.client.widgets.Label; | |
10 import com.smartgwt.client.widgets.form.fields.FormItem; | |
11 import com.smartgwt.client.widgets.form.fields.events.BlurEvent; | |
12 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; | |
13 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; | |
14 import com.smartgwt.client.widgets.layout.HLayout; | |
15 import com.smartgwt.client.widgets.layout.VLayout; | |
16 | |
17 import org.dive4elements.river.client.shared.model.Data; | |
18 import org.dive4elements.river.client.shared.model.DataItem; | |
19 import org.dive4elements.river.client.shared.model.DataList; | |
20 | |
21 import java.util.ArrayList; | |
22 import java.util.List; | |
23 | |
24 /** | |
25 * This UIProvider serves as base for UI Providers to enter a single location (km). | |
26 */ | |
27 public abstract class LocationPanel | |
28 extends AbstractUIProvider | |
29 { | |
30 private static final long serialVersionUID = -5306604428440015046L; | |
31 | |
32 /** A container that will contain the location or the distance panel. */ | |
33 protected HLayout inputLayout; | |
34 | |
35 /** The minimal value that the user is allowed to enter. */ | |
36 protected double min; | |
37 | |
38 /** The maximal value that the user is allowed to enter. */ | |
39 protected double max; | |
40 | |
41 /** The values entered in the location mode. */ | |
42 protected double[] values; | |
43 | |
44 /** Name of the data item that keeps this location(s). */ | |
45 protected String dataItemName; | |
46 | |
47 /** The input panel for locations. */ | |
48 protected DoubleArrayPanel locationPanel; | |
49 | |
50 | |
51 /** | |
52 * Creates a new LocationDistancePanel instance. | |
53 */ | |
54 public LocationPanel() { | |
55 values = new double[0]; | |
56 } | |
57 | |
58 | |
59 /** | |
60 * This method creates a widget that contains a label, a panel with | |
61 * checkboxes to switch the input mode between location and distance input, | |
62 * and a mode specific panel. | |
63 * | |
64 * @param data The data that might be inserted. | |
65 * | |
66 * @return a panel. | |
67 */ | |
68 @Override | |
69 public Canvas create(DataList data) { | |
70 findDataItemName(data); | |
71 | |
72 VLayout layout = new VLayout(); | |
73 layout.setMembersMargin(10); | |
74 | |
75 // Subclass uses translated data items name as label. | |
76 Label label = new Label(MSG.location()); | |
77 Canvas widget = createWidget(data); | |
78 Canvas submit = getNextButton(); | |
79 | |
80 initDefaults(data); | |
81 | |
82 widget.setHeight(50); | |
83 label.setHeight(25); | |
84 | |
85 layout.addMember(label); | |
86 layout.addMember(widget); | |
87 layout.addMember(submit); | |
88 | |
89 return layout; | |
90 } | |
91 | |
92 | |
93 /** Store label of first data item in list. */ | |
94 public void findDataItemName(DataList list) { | |
95 this.dataItemName = list.getAll().get(0).getLabel(); | |
96 } | |
97 | |
98 | |
99 /** Get label of first data item that this uiprovider has seen. */ | |
100 public String getDataItemName() { | |
101 return this.dataItemName; | |
102 } | |
103 | |
104 | |
105 /** | |
106 * This method creates a Canvas element showing the old Data objects in the | |
107 * DataList <i>data</i>. | |
108 */ | |
109 @Override | |
110 public Canvas createOld(DataList dataList) { | |
111 findDataItemName(dataList); | |
112 | |
113 List<Data> items = dataList.getAll(); | |
114 Data dLocation = getData(items, getDataItemName()); | |
115 DataItem[] loc = dLocation.getItems(); | |
116 | |
117 HLayout layout = new HLayout(); | |
118 layout.setWidth("400px"); | |
119 | |
120 Label label = new Label(dataList.getLabel()); | |
121 label.setWidth("200px"); | |
122 | |
123 Canvas back = getBackButton(dataList.getState()); | |
124 | |
125 // TODO evaluate: isn't this what findDataItemName is doing? | |
126 Label selected = new Label(loc[0].getLabel()); | |
127 selected.setWidth("130px"); | |
128 | |
129 layout.addMember(label); | |
130 layout.addMember(selected); | |
131 layout.addMember(back); | |
132 | |
133 return layout; | |
134 } | |
135 | |
136 | |
137 /** | |
138 * This method reads the default values defined in the DataItems of the Data | |
139 * objects in <i>list</i>. | |
140 * | |
141 * @param list The DataList container that stores the Data objects. | |
142 */ | |
143 protected void initDefaults(DataList list) { | |
144 Data data = list.get(0); | |
145 | |
146 DataItem[] items = data.getItems(); | |
147 DataItem iMin = getDataItem(items, "min"); | |
148 DataItem iMax = getDataItem(items, "max"); | |
149 | |
150 try { | |
151 min = Double.parseDouble(iMin.getStringValue()); | |
152 max = Double.parseDouble(iMax.getStringValue()); | |
153 } | |
154 catch (NumberFormatException nfe) { | |
155 SC.warn(MSG.error_read_minmax_values()); | |
156 min = -Double.MAX_VALUE; | |
157 max = Double.MAX_VALUE; | |
158 } | |
159 | |
160 DataItem def = data.getDefault(); | |
161 String value = def.getStringValue(); | |
162 | |
163 try { | |
164 double d = Double.parseDouble(value); | |
165 setLocationValues(new double[] { d } ); | |
166 } | |
167 catch (NumberFormatException nfe) { | |
168 // could not parse, don't know what else to do | |
169 GWT.log("LocationPanel", nfe); | |
170 } | |
171 } | |
172 | |
173 | |
174 /** | |
175 * This method grabs the Data with name <i>name</i> from the list and | |
176 * returns it. | |
177 * | |
178 * @param items A list of Data. | |
179 * @param name The name of the Data that we are searching for. | |
180 * | |
181 * @return the Data with the name <i>name</i>. | |
182 */ | |
183 @Override | |
184 protected Data getData(List<Data> data, String name) { | |
185 for (Data d: data) { | |
186 if (name.equals(d.getLabel())) { | |
187 return d; | |
188 } | |
189 } | |
190 | |
191 return null; | |
192 } | |
193 | |
194 | |
195 protected Canvas createWidget(DataList data) { | |
196 VLayout layout = new VLayout(); | |
197 inputLayout = new HLayout(); | |
198 | |
199 // The initial view will display the location input mode. | |
200 locationPanel = new DoubleArrayPanel( | |
201 MSG.unitLocation(), | |
202 getLocationValues(), | |
203 new BlurHandler(){@Override | |
204 public void onBlur(BlurEvent be) {}}); | |
205 | |
206 // TODO Remove picker references, refactor such that subclasses can | |
207 // easily use their picker if they want. | |
208 //picker.getLocationTable().setAutoFetchData(true); | |
209 | |
210 inputLayout.addMember(locationPanel); | |
211 | |
212 layout.addMember(inputLayout); | |
213 | |
214 inputLayout.setMembersMargin(30); | |
215 | |
216 /* | |
217 //picker.prepareFilter(); | |
218 helperContainer.addMember(picker.getLocationTable()); | |
219 helperContainer.addMember(picker.getFilterLayout()); | |
220 helperContainer.addMember(picker.getResultCountForm()); | |
221 */ | |
222 return layout; | |
223 } | |
224 | |
225 | |
226 @Override | |
227 public List<String> validate() { | |
228 List<String> errors = new ArrayList<String>(); | |
229 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
230 | |
231 saveLocationValues(locationPanel); | |
232 | |
233 if (!locationPanel.validateForm()) { | |
234 errors.add(MSG.wrongFormat()); | |
235 return errors; | |
236 } | |
237 | |
238 double[] values = getLocationValues(); | |
239 double[] good = new double[values.length]; | |
240 int idx = 0; | |
241 | |
242 for (double value: values) { | |
243 if (value < min || value > max) { | |
244 String tmp = MSG.error_validate_range(); | |
245 tmp = tmp.replace("$1", nf.format(value)); | |
246 tmp = tmp.replace("$2", nf.format(min)); | |
247 tmp = tmp.replace("$3", nf.format(max)); | |
248 errors.add(tmp); | |
249 } | |
250 else { | |
251 good[idx++] = value; | |
252 } | |
253 } | |
254 | |
255 double[] justGood = new double[idx]; | |
256 for (int i = 0; i < justGood.length; i++) { | |
257 justGood[i] = good[i]; | |
258 } | |
259 | |
260 if (!errors.isEmpty()) { | |
261 locationPanel.setValues(justGood); | |
262 } | |
263 | |
264 return errors; | |
265 } | |
266 | |
267 | |
268 /** | |
269 * Validates and stores all values entered in the location mode. | |
270 * | |
271 * @param p The DoubleArrayPanel. | |
272 */ | |
273 protected void saveLocationValues(DoubleArrayPanel p) { | |
274 FormItem[] formItems = p.getFields(); | |
275 | |
276 for (FormItem item: formItems) { | |
277 if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) { | |
278 saveLocationValue(p, item); | |
279 } | |
280 } | |
281 } | |
282 | |
283 | |
284 /** | |
285 * Validates and stores a value entered in the location mode. | |
286 * | |
287 * @param p The DoubleArrayPanel. | |
288 * @param item The item that needs to be validated. | |
289 */ | |
290 protected void saveLocationValue(DoubleArrayPanel p, FormItem item) { | |
291 if (p.validateForm(item)) { | |
292 setLocationValues(p.getInputValues(item)); | |
293 } | |
294 } | |
295 | |
296 | |
297 /** Get the location values. */ | |
298 protected double[] getLocationValues() { | |
299 return values; | |
300 } | |
301 | |
302 | |
303 /** Sets Location values and updates the panel. */ | |
304 protected void setLocationValues(double[] values) { | |
305 this.values = values; | |
306 locationPanel.setValues(values); | |
307 } | |
308 | |
309 | |
310 /** | |
311 * Callback when an item from the input helper was clicked. | |
312 * Set the respective km-value in the location value field. | |
313 * @param e event passed. | |
314 */ | |
315 public void onRecordClick (RecordClickEvent e) { | |
316 Record record = e.getRecord(); | |
317 double[] selected = new double[1]; | |
318 try { | |
319 selected[0] = | |
320 Double.parseDouble(record.getAttribute("from")); | |
321 } | |
322 catch(NumberFormatException nfe) { | |
323 GWT.log("onRecordClick", nfe); | |
324 } | |
325 setLocationValues(selected); | |
326 } | |
327 } | |
328 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |