Mercurial > dive4elements > river
annotate gwt-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ManualDatePointsEditor.java @ 7715:9705e747e3ee
(issue1574) Add correction curves to W Area compatibility lists
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 20 Dec 2013 12:50:01 +0100 |
parents | ea9eef426962 |
children | 4ead7919e709 |
rev | line source |
---|---|
5861
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
2 * Software engineering by Intevation GmbH |
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
3 * |
5993
ea9eef426962
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5861
diff
changeset
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
5861
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
5993
ea9eef426962
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5861
diff
changeset
|
6 * documentation coming with Dive4Elements River for details. |
5861
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
7 */ |
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
8 |
5835
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
9 package org.dive4elements.river.client.client.ui.chart; |
2479 | 10 |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
11 import com.google.gwt.core.client.GWT; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
12 import com.google.gwt.i18n.client.DateTimeFormat; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
13 import com.google.gwt.i18n.client.NumberFormat; |
2479 | 14 import com.google.gwt.json.client.JSONArray; |
15 import com.google.gwt.json.client.JSONBoolean; | |
16 import com.google.gwt.json.client.JSONNumber; | |
17 import com.google.gwt.json.client.JSONParser; | |
18 import com.google.gwt.json.client.JSONString; | |
19 | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
20 import com.smartgwt.client.types.Alignment; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
21 import com.smartgwt.client.types.ListGridFieldType; |
2479 | 22 import com.smartgwt.client.util.SC; |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
23 import com.smartgwt.client.widgets.Button; |
2479 | 24 import com.smartgwt.client.widgets.IButton; |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
25 import com.smartgwt.client.widgets.events.ClickEvent; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
26 import com.smartgwt.client.widgets.events.ClickHandler; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
27 import com.smartgwt.client.widgets.grid.CellEditValueFormatter; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
28 import com.smartgwt.client.widgets.grid.CellEditValueParser; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
29 import com.smartgwt.client.widgets.grid.CellFormatter; |
2479 | 30 import com.smartgwt.client.widgets.grid.ListGrid; |
31 import com.smartgwt.client.widgets.grid.ListGridField; | |
32 import com.smartgwt.client.widgets.grid.ListGridRecord; | |
33 import com.smartgwt.client.widgets.grid.events.RecordClickEvent; | |
34 import com.smartgwt.client.widgets.grid.events.RecordClickHandler; | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
35 import com.smartgwt.client.widgets.layout.HLayout; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
36 import com.smartgwt.client.widgets.layout.VLayout; |
2479 | 37 |
5835
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
38 import org.dive4elements.river.client.client.event.RedrawRequestHandler; |
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
39 import org.dive4elements.river.client.shared.model.Collection; |
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
40 import org.dive4elements.river.client.shared.model.CollectionItem; |
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
41 import org.dive4elements.river.client.shared.model.Property; |
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
42 import org.dive4elements.river.client.shared.model.PropertyGroup; |
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
43 import org.dive4elements.river.client.shared.model.Settings; |
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
44 import org.dive4elements.river.client.shared.model.StringProperty; |
2479 | 45 |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
46 import java.util.Date; |
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
47 import java.util.List; |
2479 | 48 |
49 /** | |
50 * UI to enter point data and save it to an PointArtifact. | |
51 */ | |
52 public class ManualDatePointsEditor | |
53 extends ManualPointsEditor | |
54 { | |
55 | |
56 public ManualDatePointsEditor(Collection collection, | |
57 RedrawRequestHandler handler, String outputModeName | |
58 ) { | |
59 super (collection, handler, outputModeName); | |
60 } | |
61 | |
62 | |
63 /** Create and setup/add the ui. */ | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
64 @Override |
2479 | 65 public void createUI() { |
66 Button accept = new Button(MSG.label_ok()); | |
67 Button cancel = new Button(MSG.label_cancel()); | |
68 cancel.addClickHandler(this); | |
69 | |
70 accept.addClickHandler(new ClickHandler() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
71 @Override |
2479 | 72 public void onClick(ClickEvent e) { |
73 okClicked(); | |
74 } | |
75 }); | |
76 | |
77 HLayout buttons = new HLayout(); | |
78 buttons.addMember(accept); | |
79 buttons.addMember(cancel); | |
80 buttons.setAlign(Alignment.CENTER); | |
81 buttons.setHeight(30); | |
82 | |
83 VLayout layout = new VLayout(); | |
84 listGrid = new ListGrid(); | |
85 listGrid.setWidth100(); | |
86 listGrid.setHeight("*"); | |
87 listGrid.setCanSort(false); | |
88 listGrid.setCanEdit(true); | |
89 listGrid.setShowHeaderContextMenu(false); | |
90 | |
91 CellFormatter doubleFormat = new CellFormatter() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
92 @Override |
2479 | 93 public String format(Object value, ListGridRecord record, int rowNum, int colNum) { |
94 if(value != null) { | |
95 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
96 try { | |
97 double d = Double.valueOf(value.toString()).doubleValue(); | |
98 return nf.format(d); | |
99 } catch (Exception e) { | |
100 return value.toString(); | |
101 } | |
102 } else { | |
103 return null; | |
104 } | |
105 }}; | |
106 | |
107 CellFormatter dateFormat = new CellFormatter() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
108 @Override |
2479 | 109 public String format(Object value, ListGridRecord record, int rowNum, int colNum) { |
110 if(value != null && !value.toString().equals("")) { | |
111 try { | |
112 DateTimeFormat df = | |
113 DateTimeFormat.getFormat("dd.MM.yyyy"); | |
114 Date d = df.parse(value.toString()); | |
115 DateTimeFormat df2 = | |
116 DateTimeFormat.getFormat( | |
117 DateTimeFormat.PredefinedFormat.DATE_MEDIUM); | |
118 return df2.format(d); | |
119 | |
120 } | |
121 catch(IllegalArgumentException iae) { | |
122 SC.warn(MSG.error_invalid_date()); | |
123 record.setAttribute(DatePointRecord.ATTRIBUTE_X, ""); | |
124 return ""; | |
125 } | |
126 } | |
127 else { | |
128 return null; | |
129 } | |
130 }}; | |
131 | |
132 | |
133 CellEditValueParser cevp = new CellEditValueParser() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
134 @Override |
2479 | 135 public Object parse(Object value, ListGridRecord record, int rowNum, int colNum) { |
136 if (value == null) | |
137 return null; | |
138 try { | |
139 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
140 double d = nf.parse(value.toString()); | |
141 return (new Double(d)).toString(); | |
142 } | |
143 catch(NumberFormatException nfe) { | |
144 return value; | |
145 } | |
146 } | |
147 }; | |
148 | |
149 CellEditValueFormatter cevf = new CellEditValueFormatter() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
150 @Override |
2479 | 151 public Object format(Object value, ListGridRecord record, int rowNum, int colNum) { |
152 if (value != null) { | |
153 NumberFormat nf = NumberFormat.getDecimalFormat(); | |
154 try { | |
155 double d = Double.valueOf(value.toString()).doubleValue(); | |
156 return nf.format(d); | |
157 } catch (Exception e) { | |
158 return value.toString(); | |
159 } | |
160 } | |
161 return null; | |
162 } | |
163 }; | |
164 | |
165 // Use X and Y as default fallback. | |
166 String xAxis = "X"; | |
167 String yAxis = "Y"; | |
168 | |
169 // Get header text from collection settings. | |
170 Settings settings = this.collection.getSettings(outputModeName); | |
171 List<Property> axes = settings.getSettings("axes"); | |
172 if(axes != null) { | |
173 for (Property p: axes) { | |
174 PropertyGroup pg = (PropertyGroup)p; | |
175 StringProperty id = | |
176 (StringProperty)pg.getPropertyByName("id"); | |
177 if(id.getValue().equals("X")) { | |
178 StringProperty name = | |
179 (StringProperty)pg.getPropertyByName("label"); | |
180 xAxis = name.getValue(); | |
181 } | |
182 else if (yAxis.equals("Y")) { | |
183 StringProperty name = | |
184 (StringProperty)pg.getPropertyByName("label"); | |
185 yAxis = name.getValue(); | |
186 } | |
187 } | |
188 } | |
189 ListGridField xField = | |
190 new ListGridField(PointRecord.ATTRIBUTE_X, xAxis); | |
191 xField.setType(ListGridFieldType.TEXT); | |
192 xField.setCellFormatter(dateFormat); | |
193 | |
194 ListGridField yField = | |
195 new ListGridField(PointRecord.ATTRIBUTE_Y, yAxis); | |
196 yField.setType(ListGridFieldType.FLOAT); | |
197 yField.setCellFormatter(doubleFormat); | |
198 yField.setEditValueParser(cevp); | |
199 yField.setEditValueFormatter(cevf); | |
200 | |
201 ListGridField nameField = new ListGridField(PointRecord.ATTRIBUTE_NAME, | |
202 MSG.pointname()); | |
203 final ListGridField removeField = | |
204 new ListGridField("_removeRecord", MSG.removepoint()){{ | |
205 setType(ListGridFieldType.ICON); | |
206 setIcon(GWT.getHostPageBaseURL() + MSG.removeFeature()); | |
207 setCanEdit(false); | |
208 setCanFilter(false); | |
209 setCanSort(false); | |
210 setCanGroupBy(false); | |
211 setCanFreeze(false); | |
212 setWidth(25); | |
213 }}; | |
214 | |
215 ListGridField activeField = new ListGridField( | |
216 PointRecord.ATTRIBUTE_ACTIVE, MSG.selection()); | |
217 activeField.setType(ListGridFieldType.BOOLEAN); | |
218 activeField.setDefaultValue(true); | |
219 | |
220 listGrid.setFields(new ListGridField[] {activeField, xField, yField, | |
221 nameField, removeField}); | |
222 | |
223 listGrid.addRecordClickHandler(new RecordClickHandler() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
224 @Override |
2479 | 225 public void onRecordClick(final RecordClickEvent event) { |
226 // Just handle remove-clicks | |
227 if(!event.getField().getName().equals(removeField.getName())) { | |
228 return; | |
2905
51ed89b754ae
FLYS client: Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2486
diff
changeset
|
229 } |
2479 | 230 event.getViewer().removeData(event.getRecord()); |
231 } | |
232 }); | |
233 | |
234 // Find the artifacts uuid. | |
235 findManualPointsUUID(); | |
236 CollectionItem item = collection.getItem(uuid); | |
237 | |
238 // Add points to grid. | |
239 if (item != null) { | |
240 String jsonData = item.getData().get(outputModeName + "." + POINT_DATA); | |
241 JSONArray jsonArray = (JSONArray) JSONParser.parse(jsonData); | |
242 for (int i = 0; i < jsonArray.size(); i++) { | |
243 JSONArray point = (JSONArray) jsonArray.get(i); | |
244 listGrid.addData(datePointRecordFromJSON(point)); | |
245 } | |
246 } | |
247 else { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
248 GWT.log("No item found for " + uuid); |
2479 | 249 } |
250 | |
251 IButton button = new IButton(MSG.newpoint()); | |
252 button.setTop(250); | |
253 button.addClickHandler(new ClickHandler() { | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
254 @Override |
2479 | 255 public void onClick(ClickEvent event) { |
256 listGrid.startEditingNew(); | |
257 } | |
258 }); | |
259 | |
260 layout.addMember(listGrid); | |
261 layout.addMember(button); | |
262 | |
263 addItem(layout); | |
264 | |
265 addItem(buttons); | |
266 setWidth(380); | |
267 setHeight(470); | |
268 centerInPage(); | |
269 } | |
270 | |
271 | |
272 /** Create JSON representation of the points present in the list grid. */ | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
273 @Override |
2479 | 274 protected JSONArray jsonArrayFromListGrid() { |
275 JSONArray list = new JSONArray(); | |
276 int idx = 0; | |
277 | |
278 for(ListGridRecord record : listGrid.getRecords()) { | |
279 if (record instanceof DatePointRecord) { | |
280 JSONArray data = new JSONArray(); | |
281 | |
282 DatePointRecord point = (DatePointRecord) record; | |
283 String dateString = point.getX(); | |
284 DateTimeFormat df = DateTimeFormat.getFormat( | |
285 DateTimeFormat.PredefinedFormat.DATE_MEDIUM); | |
286 | |
287 Date d = df.parse(dateString); | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
288 double dv = d.getTime(); |
2479 | 289 |
290 data.set(0, new JSONNumber(dv)); | |
291 data.set(1, new JSONNumber(point.getY())); | |
292 data.set(2, new JSONString(point.getName())); | |
293 data.set(3, JSONBoolean.getInstance(point.isActive())); | |
294 | |
295 list.set(idx, data); | |
296 idx++; | |
297 } | |
298 else { | |
299 JSONArray data = new JSONArray(); | |
300 | |
301 String nameString = record.getAttributeAsString(PointRecord.ATTRIBUTE_NAME); | |
302 // Apply default name if none set. | |
303 if (nameString == null || nameString.equals("")) { | |
304 String xString = record.getAttributeAsString( | |
305 PointRecord.ATTRIBUTE_X); | |
306 String yString = record.getAttributeAsString( | |
307 PointRecord.ATTRIBUTE_Y); | |
308 nameString = xString + "/" + yString; | |
309 } | |
310 | |
311 String dateString = record.getAttributeAsString(PointRecord.ATTRIBUTE_X); | |
312 DateTimeFormat df = DateTimeFormat.getFormat( | |
313 DateTimeFormat.PredefinedFormat.DATE_MEDIUM); | |
314 | |
315 Date d = df.parse(dateString); | |
4568
bbd82bd8e541
flys-client: Cosmetics and warnings.
Christian Lins <christian.lins@intevation.de>
parents:
2905
diff
changeset
|
316 double dv = d.getTime(); |
2479 | 317 data.set(0, new JSONNumber(dv)); |
318 data.set(1, new JSONNumber(record. | |
319 getAttributeAsDouble(PointRecord.ATTRIBUTE_Y))); | |
320 data.set(2, new JSONString(nameString)); | |
321 data.set(3, JSONBoolean.getInstance(record.getAttributeAsBoolean( | |
322 PointRecord.ATTRIBUTE_ACTIVE))); | |
323 | |
324 list.set(idx, data); | |
325 idx++; | |
326 } | |
327 } | |
328 return list; | |
329 } | |
330 | |
331 /** From a JSON-encoded point, create a PointRecord. */ | |
332 public DatePointRecord datePointRecordFromJSON(JSONArray jsonArray) { | |
333 JSONNumber x = (JSONNumber) jsonArray.get(0); | |
334 JSONNumber y = (JSONNumber) jsonArray.get(1); | |
335 JSONString s = (JSONString) jsonArray.get(2); | |
336 JSONBoolean b = (JSONBoolean) jsonArray.get(3); | |
337 | |
338 Date d = new Date (Long.valueOf(x.toString()).longValue()); | |
339 DateTimeFormat df = DateTimeFormat.getFormat( | |
340 DateTimeFormat.PredefinedFormat.DATE_MEDIUM); | |
341 | |
342 return new DatePointRecord(b.booleanValue(), df.format(d), | |
343 y.doubleValue(), s.stringValue()); | |
344 } | |
345 | |
346 | |
347 /** Return false if x or y attribute is missing. */ | |
348 @Override | |
349 protected boolean isDialogValid() { | |
350 boolean valid = true; | |
351 for (ListGridRecord record : listGrid.getRecords()) { | |
352 if (record.getAttributeAsString(PointRecord.ATTRIBUTE_X) == null || | |
353 record.getAttributeAsString( | |
354 DatePointRecord.ATTRIBUTE_X).equals("") || | |
355 record.getAttributeAsDouble(PointRecord.ATTRIBUTE_Y) == null) { | |
356 return false; | |
357 } | |
358 } | |
359 if (listGrid.hasErrors()) { | |
360 valid = false; | |
361 } | |
362 return valid; | |
363 } | |
364 | |
365 | |
366 /** Simple record to store points. */ | |
367 public class DatePointRecord extends ListGridRecord { | |
368 protected static final String ATTRIBUTE_X = "X"; | |
369 protected static final String ATTRIBUTE_Y = "Y"; | |
370 protected static final String ATTRIBUTE_NAME = "name"; | |
371 protected static final String ATTRIBUTE_ACTIVE = "active"; | |
372 | |
373 private DatePointRecord() {;} | |
374 | |
375 public DatePointRecord(boolean b, String x, double y, String name) { | |
376 setActive(b); | |
377 setName(name); | |
378 setX(x); | |
379 setY(y); | |
380 } | |
381 | |
382 public void setActive(boolean b) { | |
383 setAttribute(ATTRIBUTE_ACTIVE, b); | |
384 } | |
385 | |
386 public boolean isActive() { | |
387 return getAttributeAsBoolean(ATTRIBUTE_ACTIVE); | |
388 } | |
389 | |
390 public void setName(String name) { | |
391 setAttribute(ATTRIBUTE_NAME, name); | |
392 } | |
393 | |
394 public String getName() { | |
395 return getAttributeAsString(ATTRIBUTE_NAME); | |
396 } | |
397 | |
398 public void setX(String x) { | |
399 setAttribute(ATTRIBUTE_X, x); | |
400 } | |
401 | |
402 public void setY(double y) { | |
403 setAttribute(ATTRIBUTE_Y, y); | |
404 } | |
405 | |
406 public String getX() { | |
407 return getAttributeAsString(ATTRIBUTE_X); | |
408 } | |
409 | |
410 public double getY() { | |
411 return getAttributeAsDouble(ATTRIBUTE_Y); | |
412 } | |
413 } | |
414 } | |
4980
e70ff0a600a3
flys-client: Whitespace and doc cosemtics.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
4568
diff
changeset
|
415 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |
e70ff0a600a3
flys-client: Whitespace and doc cosemtics.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
4568
diff
changeset
|
416 |