comparison flys-client/src/main/java/org/dive4elements/river/client/client/ui/chart/ManualDatePointsEditor.java @ 5834:f507086aa94b

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

http://dive4elements.wald.intevation.org