comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/ManualPointsEditor.java @ 3983:65e369ac4f99

ManualPointsEditor supports Date x axis values.
author Christian Lins <christian.lins@intevation.de>
date Sat, 29 Sep 2012 11:38:38 +0200
parents 62332fa199bf
children bbd82bd8e541
comparison
equal deleted inserted replaced
3982:f9729662f1be 3983:65e369ac4f99
1 package de.intevation.flys.client.client.ui.chart; 1 package de.intevation.flys.client.client.ui.chart;
2 2
3 import com.google.gwt.core.client.GWT; 3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.i18n.client.DateTimeFormat;
4 import com.google.gwt.i18n.client.NumberFormat; 5 import com.google.gwt.i18n.client.NumberFormat;
5 import com.google.gwt.json.client.JSONArray; 6 import com.google.gwt.json.client.JSONArray;
6 import com.google.gwt.json.client.JSONBoolean; 7 import com.google.gwt.json.client.JSONBoolean;
7 import com.google.gwt.json.client.JSONNumber; 8 import com.google.gwt.json.client.JSONNumber;
8 import com.google.gwt.json.client.JSONParser; 9 import com.google.gwt.json.client.JSONParser;
45 import de.intevation.flys.client.shared.model.PropertyGroup; 46 import de.intevation.flys.client.shared.model.PropertyGroup;
46 import de.intevation.flys.client.shared.model.Recommendation; 47 import de.intevation.flys.client.shared.model.Recommendation;
47 import de.intevation.flys.client.shared.model.Settings; 48 import de.intevation.flys.client.shared.model.Settings;
48 import de.intevation.flys.client.shared.model.StringProperty; 49 import de.intevation.flys.client.shared.model.StringProperty;
49 50
51 import java.util.Date;
50 import java.util.List; 52 import java.util.List;
51 53
52 54
53 /** 55 /**
54 * UI to enter point data and save it to an PointArtifact. 56 * UI to enter point data and save it to an PointArtifact.
151 Button accept = new Button(MSG.label_ok()); 153 Button accept = new Button(MSG.label_ok());
152 Button cancel = new Button(MSG.label_cancel()); 154 Button cancel = new Button(MSG.label_cancel());
153 cancel.addClickHandler(this); 155 cancel.addClickHandler(this);
154 156
155 accept.addClickHandler(new ClickHandler() { 157 accept.addClickHandler(new ClickHandler() {
158 @Override
156 public void onClick(ClickEvent e) { 159 public void onClick(ClickEvent e) {
157 okClicked(); 160 okClicked();
158 } 161 }
159 }); 162 });
160 163
174 177
175 // Use X and Y as default fallback. 178 // Use X and Y as default fallback.
176 String xAxis = "X"; 179 String xAxis = "X";
177 String yAxis = "Y"; 180 String yAxis = "Y";
178 181
179 // Get header text from collection settings. 182 // Get header text from collection settings
180 Settings settings = this.collection.getSettings(outputModeName); 183 Settings settings = this.collection.getSettings(outputModeName);
181 List<Property> axes = settings.getSettings("axes"); 184 List<Property> axes = settings.getSettings("axes");
182 if(axes != null) { 185 if(axes != null) {
183 for (Property p: axes) { 186 for (Property p: axes) {
184 PropertyGroup pg = (PropertyGroup)p; 187 PropertyGroup pg = (PropertyGroup)p;
203 CellEditValueFormatter cevf = createCellEditValueFormatter(); 206 CellEditValueFormatter cevf = createCellEditValueFormatter();
204 207
205 ListGridField xField = 208 ListGridField xField =
206 new ListGridField(PointRecord.ATTRIBUTE_X, xAxis); 209 new ListGridField(PointRecord.ATTRIBUTE_X, xAxis);
207 if(xAxis.equalsIgnoreCase("date") || xAxis.equalsIgnoreCase("Datum")) { 210 if(xAxis.equalsIgnoreCase("date") || xAxis.equalsIgnoreCase("Datum")) {
208 // FIXME: This is a hack for the special axis with Date type 211 // FIXME: This is a hack for Timeseries charts with Date types on the x axis
209 xField.setType(ListGridFieldType.DATE); 212 xField.setType(ListGridFieldType.DATE);
210 this.fieldTypeX = ListGridFieldType.DATE; 213 this.fieldTypeX = ListGridFieldType.DATE;
211 } 214 }
212 else { 215 else {
213 xField.setType(ListGridFieldType.FLOAT); 216 xField.setType(ListGridFieldType.FLOAT);
244 247
245 listGrid.setFields(new ListGridField[] {activeField, xField, yField, 248 listGrid.setFields(new ListGridField[] {activeField, xField, yField,
246 nameField, removeField}); 249 nameField, removeField});
247 250
248 listGrid.addRecordClickHandler(new RecordClickHandler() { 251 listGrid.addRecordClickHandler(new RecordClickHandler() {
252 @Override
249 public void onRecordClick(final RecordClickEvent event) { 253 public void onRecordClick(final RecordClickEvent event) {
250 // Just handle remove-clicks 254 // Just handle remove-clicks
251 if(!event.getField().getName().equals(removeField.getName())) { 255 if(!event.getField().getName().equals(removeField.getName())) {
252 return; 256 return;
253 } 257 }
254 event.getViewer().removeData(event.getRecord()); 258 event.getViewer().removeData(event.getRecord());
255 } 259 }
256 }); 260 });
257 261
258 // Find the artifacts uuid. 262 // Find the artifacts uuid
259 findManualPointsUUID(); 263 findManualPointsUUID();
260 CollectionItem item = collection.getItem(uuid); 264 CollectionItem item = collection.getItem(uuid);
261 265
262 // Add points to grid. 266 // Add points to grid
263 if (item != null) { 267 if (item != null) {
264 // TODO store this from findPointUUID instead (we touched these). 268 // TODO store this from findPointUUID instead (we touched these).
265 String jsonData = item.getData().get(pointDataItemName); 269 String jsonData = item.getData().get(pointDataItemName);
266 JSONArray jsonArray = (JSONArray) JSONParser.parse(jsonData); 270 JSONArray jsonArray = (JSONArray) JSONParser.parse(jsonData);
267 for (int i = 0; i < jsonArray.size(); i++) { 271 for (int i = 0; i < jsonArray.size(); i++) {
268 JSONArray point = (JSONArray) jsonArray.get(i); 272 JSONArray point = (JSONArray) jsonArray.get(i);
269 listGrid.addData(pointRecordFromJSON(point)); 273 listGrid.addData(PointRecord.fromJSON(point));
270 } 274 }
271 } 275 }
272 else { 276 else {
273 // TODO proper log 277 GWT.log("ManualPointsEditor: No item found for " + uuid);
274 System.out.println("No item found for " + uuid);
275 } 278 }
276 279
277 IButton button = new IButton(MSG.newpoint()); 280 IButton button = new IButton(MSG.newpoint());
278 button.setTop(250); 281 button.setTop(250);
279 button.addClickHandler(new ClickHandler() { 282 button.addClickHandler(new ClickHandler() {
283 @Override
280 public void onClick(ClickEvent event) { 284 public void onClick(ClickEvent event) {
281 listGrid.startEditingNew(); 285 listGrid.startEditingNew();
282 } 286 }
283 }); 287 });
284 288
294 } 298 }
295 299
296 300
297 protected CellFormatter createCellFormatter() { 301 protected CellFormatter createCellFormatter() {
298 return new CellFormatter() { 302 return new CellFormatter() {
303 @Override
299 public String format(Object value, ListGridRecord record, int rowNum, int colNum) { 304 public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
300 if(value != null) { 305 if(value != null) {
301 NumberFormat nf = NumberFormat.getDecimalFormat(); 306 NumberFormat nf = NumberFormat.getDecimalFormat();
302 try { 307 try {
303 double d = Double.valueOf(value.toString()).doubleValue(); 308 double d = Double.valueOf(value.toString()).doubleValue();
312 } 317 }
313 318
314 319
315 protected CellEditValueParser createCellEditValueParser() { 320 protected CellEditValueParser createCellEditValueParser() {
316 return new CellEditValueParser() { 321 return new CellEditValueParser() {
322 @Override
317 public Object parse(Object value, ListGridRecord record, int rowNum, int colNum) { 323 public Object parse(Object value, ListGridRecord record, int rowNum, int colNum) {
318 if (value == null) 324 if (value == null)
319 return null; 325 return null;
320 try { 326 try {
321 NumberFormat nf = NumberFormat.getDecimalFormat(); 327 NumberFormat nf = NumberFormat.getDecimalFormat();
330 } 336 }
331 337
332 338
333 protected CellEditValueFormatter createCellEditValueFormatter() { 339 protected CellEditValueFormatter createCellEditValueFormatter() {
334 return new CellEditValueFormatter() { 340 return new CellEditValueFormatter() {
341 @Override
335 public Object format(Object value, ListGridRecord record, int rowNum, int colNum) { 342 public Object format(Object value, ListGridRecord record, int rowNum, int colNum) {
336 if (value == null) { 343 if (value == null) {
337 return ""; 344 return "";
338 } 345 }
339 NumberFormat nf = NumberFormat.getDecimalFormat(); 346 NumberFormat nf = NumberFormat.getDecimalFormat();
346 } 353 }
347 } 354 }
348 }; 355 };
349 } 356 }
350 357
358 protected String getLocaleDateFormat() {
359 String loc = Config.getInstance().getLocale();
360 if ("de".equals(loc)) {
361 return "yy.MM.yyyy";
362 }
363 else {
364 return "MM/dd/yyyy";
365 }
366 }
367
368 protected String formatDate(Date date) {
369 DateTimeFormat dtf = DateTimeFormat.getFormat(getLocaleDateFormat());
370 return dtf.format(date);
371 }
351 372
352 /** Create JSON representation of the points present in the list grid. */ 373 /** Create JSON representation of the points present in the list grid. */
353 protected JSONArray jsonArrayFromListGrid() { 374 protected JSONArray jsonArrayFromListGrid() {
354 JSONArray list = new JSONArray(); 375 JSONArray list = new JSONArray();
355 int idx = 0; 376 int idx = 0;
357 for(ListGridRecord record : listGrid.getRecords()) { 378 for(ListGridRecord record : listGrid.getRecords()) {
358 if (record instanceof PointRecord) { 379 if (record instanceof PointRecord) {
359 JSONArray data = new JSONArray(); 380 JSONArray data = new JSONArray();
360 381
361 PointRecord point = (PointRecord) record; 382 PointRecord point = (PointRecord) record;
362 data.set(0, new JSONNumber(point.getX())); 383 if(point.isTimeseriesPoint()) {
384 data.set(0, new JSONString(point.getXAsDate()));
385 GWT.log("Date: " + point.getXAsDate());
386 }
387 else {
388 data.set(0, new JSONNumber(point.getX()));
389 }
363 data.set(1, new JSONNumber(point.getY())); 390 data.set(1, new JSONNumber(point.getY()));
364 data.set(2, new JSONString(point.getName())); 391 data.set(2, new JSONString(point.getName()));
365 data.set(3, JSONBoolean.getInstance(point.isActive())); 392 data.set(3, JSONBoolean.getInstance(point.isActive()));
366 393
367 list.set(idx, data); 394 list.set(idx, data);
379 PointRecord.ATTRIBUTE_Y); 406 PointRecord.ATTRIBUTE_Y);
380 nameString = xString + "/" + yString; 407 nameString = xString + "/" + yString;
381 } 408 }
382 409
383 if(fieldTypeX.equals(ListGridFieldType.DATE)) { 410 if(fieldTypeX.equals(ListGridFieldType.DATE)) {
384 data.set(0, new JSONString(record.getAttribute(PointRecord.ATTRIBUTE_X))); 411 Date date = record.getAttributeAsDate(PointRecord.ATTRIBUTE_X);
412 data.set(0, new JSONString(formatDate(date)));
413 GWT.log("Date: " + formatDate(date));
385 } 414 }
386 else { 415 else {
387 data.set(0, new JSONNumber(record. 416 data.set(0, new JSONNumber(record.
388 getAttributeAsDouble(PointRecord.ATTRIBUTE_X))); 417 getAttributeAsDouble(PointRecord.ATTRIBUTE_X)));
389 } 418 }
418 feedService.feed( 447 feedService.feed(
419 Config.getInstance().getLocale(), 448 Config.getInstance().getLocale(),
420 new DefaultArtifact(uuid, "TODO:hash"), 449 new DefaultArtifact(uuid, "TODO:hash"),
421 feedData, 450 feedData,
422 new AsyncCallback<Artifact>() { 451 new AsyncCallback<Artifact>() {
452 @Override
423 public void onFailure(Throwable caught) { 453 public void onFailure(Throwable caught) {
424 GWT.log("Could not feed artifact with points."); 454 GWT.log("Could not feed artifact with points.");
425 SC.warn(MSG.getString(caught.getMessage())); 455 SC.warn(MSG.getString(caught.getMessage()));
426 enable(); 456 enable();
427 } 457 }
458 @Override
428 public void onSuccess(Artifact fartifact) { 459 public void onSuccess(Artifact fartifact) {
429 GWT.log("Successfully set points"); 460 GWT.log("Successfully set points");
430 redrawRequestHandler.onRedrawRequest( 461 redrawRequestHandler.onRedrawRequest(
431 new RedrawRequestEvent()); 462 new RedrawRequestEvent());
432 destroy(); 463 destroy();
438 SC.warn(MSG.error_dialog_not_valid()); 469 SC.warn(MSG.error_dialog_not_valid());
439 } 470 }
440 } 471 }
441 472
442 473
443 /** From a JSON-encoded point, create a PointRecord. */
444 public PointRecord pointRecordFromJSON(JSONArray jsonArray) {
445 JSONNumber x = (JSONNumber) jsonArray.get(0);
446 JSONNumber y = (JSONNumber) jsonArray.get(1);
447 JSONString s = (JSONString) jsonArray.get(2);
448 JSONBoolean b = (JSONBoolean) jsonArray.get(3);
449
450 return new PointRecord(b.booleanValue(), x.doubleValue(),
451 y.doubleValue(), s.stringValue());
452 }
453
454
455 /** Add a ManualPointArtifact to Collection. */ 474 /** Add a ManualPointArtifact to Collection. */
456 public void addArtifactCreateUI() { 475 public void addArtifactCreateUI() {
457 final Label standByLabel = new Label(MSG.standby()); 476 final Label standByLabel = new Label(MSG.standby());
458 addItem(standByLabel); 477 addItem(standByLabel);
459 478
468 this.collection, 487 this.collection,
469 new Recommendation("manualpoints", ""), 488 new Recommendation("manualpoints", ""),
470 "manualpoints", 489 "manualpoints",
471 locale, 490 locale,
472 new AsyncCallback<Artifact>() { 491 new AsyncCallback<Artifact>() {
492 @Override
473 public void onFailure(Throwable caught) { 493 public void onFailure(Throwable caught) {
474 GWT.log("Creating manualpoint artifact failed!"); 494 GWT.log("Creating manualpoint artifact failed!");
475 } 495 }
496 @Override
476 public void onSuccess(Artifact artifact) { 497 public void onSuccess(Artifact artifact) {
477 GWT.log("Successfully created artifact."); 498 GWT.log("Successfully created artifact.");
478 removeItem(standByLabel); 499 removeItem(standByLabel);
479 uuid = artifact.getUuid(); 500 uuid = artifact.getUuid();
480 createUI(); 501 createUI();
485 506
486 /** 507 /**
487 * This method is called when the user aborts point editing. 508 * This method is called when the user aborts point editing.
488 * @param event The event. 509 * @param event The event.
489 */ 510 */
511 @Override
490 public void onClick(ClickEvent event) { 512 public void onClick(ClickEvent event) {
491 this.destroy(); 513 this.destroy();
492 }
493
494
495 /** Simple record to store points. */
496 public class PointRecord extends ListGridRecord {
497 protected static final String ATTRIBUTE_X = "X";
498 protected static final String ATTRIBUTE_Y = "Y";
499 protected static final String ATTRIBUTE_NAME = "name";
500 protected static final String ATTRIBUTE_ACTIVE = "active";
501
502 public PointRecord(boolean b, double x, double y, String name) {
503 setActive(b);
504 setName(name);
505 setX(x);
506 setY(y);
507 }
508
509 public void setActive(boolean b) {
510 setAttribute(ATTRIBUTE_ACTIVE, b);
511 }
512
513 public boolean isActive() {
514 return getAttributeAsBoolean(ATTRIBUTE_ACTIVE);
515 }
516
517 public void setName(String name) {
518 setAttribute(ATTRIBUTE_NAME, name);
519 }
520
521 public String getName() {
522 return getAttributeAsString(ATTRIBUTE_NAME);
523 }
524
525 public void setX(double x) {
526 setAttribute(ATTRIBUTE_X, x);
527 }
528
529 public void setY(double y) {
530 setAttribute(ATTRIBUTE_Y, y);
531 }
532
533 public double getX() {
534 return getAttributeAsDouble(ATTRIBUTE_X);
535 }
536
537 public double getY() {
538 return getAttributeAsDouble(ATTRIBUTE_Y);
539 }
540 } 514 }
541 515
542 516
543 /** Return false if x or y attribute is missing. */ 517 /** Return false if x or y attribute is missing. */
544 protected boolean isDialogValid() { 518 protected boolean isDialogValid() {

http://dive4elements.wald.intevation.org