Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/SingleLocationPanel.java @ 238:234c78a91c15
Added new UI provider for single location selection.
flys-client/trunk@1806 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Tue, 03 May 2011 13:52:14 +0000 |
parents | |
children | f9ca49e59fb6 |
comparison
equal
deleted
inserted
replaced
237:cf25f235b7b6 | 238:234c78a91c15 |
---|---|
1 package de.intevation.flys.client.client.ui; | |
2 | |
3 import java.util.List; | |
4 | |
5 import com.google.gwt.core.client.GWT; | |
6 import com.google.gwt.user.client.rpc.AsyncCallback; | |
7 | |
8 import com.smartgwt.client.widgets.Canvas; | |
9 import com.smartgwt.client.widgets.Label; | |
10 import com.smartgwt.client.widgets.form.fields.events.BlurHandler; | |
11 import com.smartgwt.client.widgets.form.fields.events.BlurEvent; | |
12 import com.smartgwt.client.widgets.form.fields.FormItem; | |
13 import com.smartgwt.client.widgets.layout.HLayout; | |
14 import com.smartgwt.client.widgets.layout.VLayout; | |
15 import com.smartgwt.client.widgets.grid.ListGrid; | |
16 import com.smartgwt.client.widgets.grid.ListGridField; | |
17 import com.smartgwt.client.widgets.grid.ListGridRecord; | |
18 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; | |
19 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; | |
20 | |
21 import com.smartgwt.client.types.ListGridFieldType; | |
22 | |
23 import de.intevation.flys.client.shared.model.Data; | |
24 import de.intevation.flys.client.shared.model.DataItem; | |
25 import de.intevation.flys.client.shared.model.DataList; | |
26 import de.intevation.flys.client.shared.model.DefaultData; | |
27 import de.intevation.flys.client.shared.model.DefaultDataItem; | |
28 import de.intevation.flys.client.shared.model.DistanceInfoObject; | |
29 import de.intevation.flys.client.shared.model.DistanceInfoRecord; | |
30 import de.intevation.flys.client.shared.model.ArtifactDescription; | |
31 | |
32 import de.intevation.flys.client.client.services.DistanceInfoService; | |
33 import de.intevation.flys.client.client.services.DistanceInfoServiceAsync; | |
34 import de.intevation.flys.client.client.FLYSConstants; | |
35 import de.intevation.flys.client.client.FLYSImages; | |
36 import de.intevation.flys.client.client.Config; | |
37 | |
38 | |
39 /** | |
40 * This UIProvider creates a widget to enter locations. | |
41 * | |
42 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
43 */ | |
44 public class SingleLocationPanel | |
45 extends AbstractUIProvider | |
46 { | |
47 /** The message class that provides i18n strings.*/ | |
48 protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); | |
49 | |
50 /** The interface that provides the image resources. */ | |
51 private FLYSImages IMAGES = GWT.create(FLYSImages.class); | |
52 | |
53 /** The DistanceInfoService used to retrieve locations about rivers.*/ | |
54 protected DistanceInfoServiceAsync distanceInfoService = | |
55 GWT.create(DistanceInfoService.class); | |
56 | |
57 /** A container that will contain the location or the distance panel.*/ | |
58 protected HLayout container; | |
59 | |
60 /** The values entered in the location mode.*/ | |
61 protected double[] values; | |
62 | |
63 /** The input panel for locations */ | |
64 protected DoubleArrayPanel locationPanel; | |
65 | |
66 /** The locations table */ | |
67 protected ListGrid locationTable; | |
68 | |
69 /** The table data. */ | |
70 protected DistanceInfoObject[] tableData; | |
71 | |
72 /** | |
73 * Creates a new LocationDistancePanel instance. | |
74 */ | |
75 public SingleLocationPanel() { | |
76 locationTable = new ListGrid(); | |
77 } | |
78 | |
79 | |
80 /** | |
81 * This method creates a widget that contains a label, a panel with | |
82 * checkboxes to switch the input mode between location and distance input, | |
83 * and a the mode specific panel. | |
84 * | |
85 * @param data The data that might be inserted. | |
86 * | |
87 * @return a panel. | |
88 */ | |
89 public Canvas create(DataList data) { | |
90 VLayout layout = new VLayout(); | |
91 layout.setMembersMargin(10); | |
92 | |
93 initDefaults(data); | |
94 | |
95 Label label = new Label(MESSAGES.location ()); | |
96 Canvas widget = createWidget(data); | |
97 Canvas submit = getNextButton(); | |
98 | |
99 createLocationTable(); | |
100 | |
101 widget.setHeight(50); | |
102 label.setHeight(25); | |
103 | |
104 layout.addMember(label); | |
105 layout.addMember(widget); | |
106 layout.addMember(submit); | |
107 | |
108 return layout; | |
109 } | |
110 | |
111 | |
112 /** | |
113 * This method creates a table that contains the location values. | |
114 */ | |
115 protected void createLocationTable() { | |
116 locationTable.setWidth(450); | |
117 locationTable.setShowRecordComponents(true); | |
118 locationTable.setShowRecordComponentsByCell(true); | |
119 locationTable.setHeight(300); | |
120 | |
121 ListGridField addLocation = new ListGridField ("", ""); | |
122 addLocation.setType (ListGridFieldType.ICON); | |
123 addLocation.setWidth ("30"); | |
124 addLocation.addRecordClickHandler (new RecordClickHandler () { | |
125 public void onRecordClick (RecordClickEvent e) { | |
126 ListGridRecord[] records = locationTable.getSelection(); | |
127 double[] selected = new double[1]; | |
128 selected[0] = records[0].getAttributeAsDouble("from"); | |
129 setLocationValues(selected); | |
130 } | |
131 }); | |
132 addLocation.setCellIcon (IMAGES.markerGreen ().getURL ()); | |
133 | |
134 ListGridField ldescr = new ListGridField("description", | |
135 MESSAGES.description()); | |
136 ldescr.setType(ListGridFieldType.TEXT); | |
137 ldescr.setWidth("*"); | |
138 ListGridField lside = new ListGridField("riverside", | |
139 MESSAGES.riverside()); | |
140 lside.setType(ListGridFieldType.TEXT); | |
141 lside.setWidth(60); | |
142 ListGridField loc = new ListGridField("from", MESSAGES.location()); | |
143 loc.setType(ListGridFieldType.TEXT); | |
144 loc.setWidth(80); | |
145 locationTable.setFields(addLocation, ldescr, loc, lside); | |
146 } | |
147 | |
148 | |
149 public Canvas createOld(DataList dataList) { | |
150 List<Data> items = dataList.getAll(); | |
151 | |
152 HLayout layout = new HLayout(); | |
153 layout.setWidth("400px"); | |
154 | |
155 Label label = new Label(dataList.getLabel()); | |
156 label.setWidth("200px"); | |
157 | |
158 Canvas back = getBackButton(dataList.getState()); | |
159 | |
160 Label selected = new Label("testtext"); | |
161 selected.setWidth("130px"); | |
162 | |
163 layout.addMember(label); | |
164 layout.addMember(selected); | |
165 layout.addMember(back); | |
166 | |
167 return layout; | |
168 } | |
169 | |
170 | |
171 /** | |
172 * This method reads the default values defined in the DataItems of the Data | |
173 * objects in <i>list</i>. | |
174 * | |
175 * @param list The DataList container that stores the Data objects. | |
176 */ | |
177 protected void initDefaults(DataList list) { | |
178 } | |
179 | |
180 | |
181 /** | |
182 * This method greps the Data with name <i>name</i> from the list and | |
183 * returns it. | |
184 * | |
185 * @param items A list of Data. | |
186 * @param name The name of the Data that we are searching for. | |
187 * | |
188 * @return the Data with the name <i>name</i>. | |
189 */ | |
190 protected Data getData(List<Data> data, String name) { | |
191 for (Data d: data) { | |
192 if (name.equals(d.getLabel())) { | |
193 return d; | |
194 } | |
195 } | |
196 | |
197 return null; | |
198 } | |
199 | |
200 | |
201 protected Canvas createWidget(DataList data) { | |
202 VLayout layout = new VLayout(); | |
203 container = new HLayout(); | |
204 | |
205 // the initial view will display the location input mode | |
206 locationPanel = new DoubleArrayPanel( | |
207 MESSAGES.unitLocation(), | |
208 getLocationValues(), | |
209 new BlurHandler(){public void onBlur(BlurEvent be) {}}); | |
210 container.addMember(locationPanel); | |
211 | |
212 layout.addMember(container); | |
213 | |
214 container.setMembersMargin(30); | |
215 | |
216 container.addMember(locationTable); | |
217 createInputPanel(); | |
218 return layout; | |
219 } | |
220 | |
221 | |
222 /** | |
223 * This method returns the selected data. | |
224 * | |
225 * @return the selected/inserted data. | |
226 */ | |
227 public Data[] getData() { | |
228 double[] values = getLocationValues(); | |
229 Data[] data = new Data[values.length]; | |
230 DataItem item = new DefaultDataItem(); | |
231 for (int i = 0; i < values.length; i++) { | |
232 item = new DefaultDataItem( | |
233 "ld_locations", | |
234 "ld_locations", | |
235 Double.valueOf(values[i]).toString()); | |
236 data[i] = new DefaultData( | |
237 "ld_locations", | |
238 null, | |
239 null, | |
240 new DataItem[] {item}); | |
241 } | |
242 return data; | |
243 } | |
244 | |
245 | |
246 | |
247 | |
248 /** | |
249 * Validates and stores all values entered in the location mode. | |
250 * | |
251 * @param p The DoubleArrayPanel. | |
252 */ | |
253 protected void saveLocationValues(DoubleArrayPanel p) { | |
254 FormItem[] formItems = p.getFields(); | |
255 | |
256 for (FormItem item: formItems) { | |
257 if (item.getFieldName().equals(DoubleArrayPanel.FIELD_NAME)) { | |
258 saveLocationValue(p, item); | |
259 } | |
260 } | |
261 } | |
262 | |
263 | |
264 /** | |
265 * Validates and stores a value entered in the location mode. | |
266 * | |
267 * @param p The DoubleArrayPanel. | |
268 * @param item The item that needs to be validated. | |
269 */ | |
270 protected void saveLocationValue(DoubleArrayPanel p, FormItem item) { | |
271 if (p.validateForm(item)) { | |
272 setLocationValues(p.getInputValues(item)); | |
273 } | |
274 } | |
275 | |
276 | |
277 protected void createInputPanel() { | |
278 Config config = Config.getInstance(); | |
279 String url = config.getServerUrl(); | |
280 String locale = config.getLocale (); | |
281 String river = ""; | |
282 | |
283 ArtifactDescription adescr = artifact.getArtifactDescription(); | |
284 DataList[] data = adescr.getOldData(); | |
285 | |
286 if (data != null && data.length > 0) { | |
287 for (int i = 0; i < data.length; i++) { | |
288 DataList dl = data[i]; | |
289 if (dl.getState().equals("state.winfo.river")) { | |
290 for (int j = 0; j < dl.size(); j++) { | |
291 Data d = dl.get(j); | |
292 DataItem[] di = d.getItems(); | |
293 if (di != null && di.length == 1) { | |
294 river = d.getItems()[0].getStringValue(); | |
295 } | |
296 } | |
297 } | |
298 } | |
299 } | |
300 | |
301 distanceInfoService.getDistanceInfo(url, locale, river, | |
302 new AsyncCallback<DistanceInfoObject[]>() { | |
303 public void onFailure(Throwable caught) { | |
304 GWT.log("Could not recieve location informations."); | |
305 GWT.log(caught.getMessage()); | |
306 } | |
307 | |
308 public void onSuccess(DistanceInfoObject[] di) { | |
309 int num = di != null ? di.length :0; | |
310 GWT.log("Recieved " + num + " location informations."); | |
311 | |
312 if (num == 0) { | |
313 return; | |
314 } | |
315 tableData = di; | |
316 updateLocationInfo(di); | |
317 } | |
318 } | |
319 ); | |
320 } | |
321 | |
322 | |
323 protected void updateLocationInfo(DistanceInfoObject[] di) { | |
324 int i = 0; | |
325 for (DistanceInfoObject dio: di) { | |
326 if (dio.getTo() != null) { | |
327 locationTable.addData(new DistanceInfoRecord(dio)); | |
328 } | |
329 } | |
330 return; | |
331 } | |
332 | |
333 | |
334 protected double[] getLocationValues() { | |
335 return values; | |
336 } | |
337 | |
338 | |
339 protected void setLocationValues(double[] values) { | |
340 this.values = values; | |
341 locationPanel.setValues(values); | |
342 } | |
343 } | |
344 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |