Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java @ 8597:ba2a34a4e440
(issue1051) Validate epochs before adding them to the list
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Thu, 19 Mar 2015 16:28:40 +0100 |
parents | 73a4c3c202e5 |
children | 582e970115b6 |
comparison
equal
deleted
inserted
replaced
8596:b486812f4f14 | 8597:ba2a34a4e440 |
---|---|
58 protected ListGrid elements; | 58 protected ListGrid elements; |
59 private TextItem start; | 59 private TextItem start; |
60 private TextItem end; | 60 private TextItem end; |
61 private ListGrid sedLoadTable; | 61 private ListGrid sedLoadTable; |
62 | 62 |
63 protected List<String> validYears; | |
64 | |
63 public Canvas createWidget(DataList data) { | 65 public Canvas createWidget(DataList data) { |
64 HLayout input = new HLayout(); | 66 HLayout input = new HLayout(); |
65 VLayout root = new VLayout(); | 67 VLayout root = new VLayout(); |
66 VLayout grid = new VLayout(); | 68 VLayout grid = new VLayout(); |
67 VLayout intFields = new VLayout(); | 69 VLayout intFields = new VLayout(); |
83 add.addClickHandler(new ClickHandler() { | 85 add.addClickHandler(new ClickHandler() { |
84 @Override | 86 @Override |
85 public void onClick(ClickEvent ce) { | 87 public void onClick(ClickEvent ce) { |
86 String v1 = start.getValueAsString(); | 88 String v1 = start.getValueAsString(); |
87 String v2 = end.getValueAsString(); | 89 String v2 = end.getValueAsString(); |
88 //TODO: better validation. | |
89 if (v1 == null || v2 == null) { | 90 if (v1 == null || v2 == null) { |
90 return; | 91 return; |
91 } | 92 } |
92 try { | 93 if (!isValidEpoch(v1, v2)) { |
93 int v1i = Integer.parseInt(v1); | |
94 int v2i = Integer.parseInt(v2); | |
95 } | |
96 catch(NumberFormatException nfe) { | |
97 return; | 94 return; |
98 } | 95 } |
99 ListGridRecord r = new ListGridRecord(); | 96 ListGridRecord r = new ListGridRecord(); |
100 r.setAttribute("from", v1); | 97 r.setAttribute("from", v1); |
101 r.setAttribute("to", v2); | 98 r.setAttribute("to", v2); |
284 | 281 |
285 double[] km = artifact.getArtifactDescription().getKMRange(); | 282 double[] km = artifact.getArtifactDescription().getKMRange(); |
286 String river = artifact.getArtifactDescription().getRiver(); | 283 String river = artifact.getArtifactDescription().getRiver(); |
287 | 284 |
288 String sq_ti_id = ""; | 285 String sq_ti_id = ""; |
286 validYears = new ArrayList<String>(data.length); | |
289 for (int i = 0; i < data.length; i++) { | 287 for (int i = 0; i < data.length; i++) { |
290 Data str = getData(data[i].getAll(), "sq_ti_id"); | 288 Data str = getData(data[i].getAll(), "sq_ti_id"); |
291 if (str != null) { | 289 if (str != null) { |
292 DataItem[] strItems = str.getItems(); | 290 DataItem[] strItems = str.getItems(); |
293 sq_ti_id = strItems[0].getStringValue(); | 291 sq_ti_id = strItems[0].getStringValue(); |
323 | 321 |
324 protected void addSedimentLoadInfo (SedimentLoadInfoObject[] sedLoad) { | 322 protected void addSedimentLoadInfo (SedimentLoadInfoObject[] sedLoad) { |
325 for(SedimentLoadInfoObject sl: sedLoad) { | 323 for(SedimentLoadInfoObject sl: sedLoad) { |
326 SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl); | 324 SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl); |
327 sedLoadTable.addData(rec); | 325 sedLoadTable.addData(rec); |
328 } | 326 validYears.add(rec.getDate()); |
329 } | 327 } |
330 | 328 } |
329 | |
330 /* Validate the epoch input. We do this here and not in an overridden | |
331 * validate method as we want to validate before an epoch is added | |
332 * to the list of epochs. */ | |
333 protected boolean isValidEpoch(String y1, String y2) { | |
334 // First check that both are integer | |
335 int iY1; | |
336 int iY2; | |
337 List<String> errors = new ArrayList<String>(); | |
338 try { | |
339 iY1 = Integer.parseInt(y1); | |
340 } catch (NumberFormatException e) { | |
341 errors.add(MESSAGES.wrongFormat() + ": " + y1); | |
342 } | |
343 try { | |
344 iY2 = Integer.parseInt(y2); | |
345 } catch (NumberFormatException e) { | |
346 errors.add(MESSAGES.wrongFormat() + ": " + y2); | |
347 } | |
348 if (!errors.isEmpty()) { | |
349 showErrors(errors); | |
350 return false; | |
351 } | |
352 boolean startIsGood = false; | |
353 boolean endIsGood = false; | |
354 for (String validYear: validYears) { | |
355 if (startIsGood || y1.equals(validYear)) { | |
356 startIsGood = true; | |
357 } | |
358 if (endIsGood || y2.equals(validYear)) { | |
359 endIsGood = true; | |
360 } | |
361 if (startIsGood && endIsGood) { | |
362 break; | |
363 } | |
364 /* alternative check if data lies in between | |
365 int aYear = Integer.parseInt(validYear); | |
366 if (aYear >= iY1 && aYear <= iY2) { | |
367 isGood = true; | |
368 break; | |
369 } | |
370 */ | |
371 } | |
372 if (!startIsGood) { | |
373 String tmp = MESSAGES.no_data_for_year(); | |
374 tmp = tmp.replace("$1", y1); | |
375 errors.add(tmp); | |
376 } | |
377 if (!endIsGood) { | |
378 String tmp = MESSAGES.no_data_for_year(); | |
379 tmp = tmp.replace("$1", y2); | |
380 errors.add(tmp); | |
381 } | |
382 if (!errors.isEmpty()) { | |
383 showErrors(errors); | |
384 return false; | |
385 } | |
386 return true; | |
387 } | |
331 } | 388 } |