comparison flys-client/src/main/java/de/intevation/flys/client/client/ui/chart/CrossSectionChartThemePanel.java @ 2934:a5ebe2e2c772

issue457 flys-client/trunk@4860 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 03 Jul 2012 12:13:36 +0000
parents e8eca6eeeec0
children a30d77d86386
comparison
equal deleted inserted replaced
2933:d87730b89afa 2934:a5ebe2e2c772
280 * Returns a double from the list that has the smallest distance to the 280 * Returns a double from the list that has the smallest distance to the
281 * given to value. In case of multiple values with the same difference, 281 * given to value. In case of multiple values with the same difference,
282 * the last one is taken. 282 * the last one is taken.
283 * @param in possible return values. 283 * @param in possible return values.
284 * @param to the value to be as close to as possible. 284 * @param to the value to be as close to as possible.
285 * @param up if true, prefer numerically higher values in case of two
286 * values with equal distance to \param to.
285 * @return value from in that is closest to to, -1 if none. 287 * @return value from in that is closest to to, -1 if none.
286 */ 288 */
287 public static double closest(Double[] in, double to) { 289 public static double closest(Double[] in, double to, boolean up) {
288 if (in == null || in.length == 0) { 290 if (in == null || in.length == 0) {
289 return -1; 291 return -1;
290 } 292 }
291 if (in[0] == to) { 293 if (in[0] == to) {
292 return to; 294 return to;
293 } 295 }
296 for (int i = 0; i < in.length; i++) {
297 GWT.log ("Close? " + in[i]);
298 }
299
294 double minDiff = Math.abs(to - in[0]); 300 double minDiff = Math.abs(to - in[0]);
295 double bestMatch = in[0]; 301 double bestMatch = in[0];
296 for (int i = 1; i < in.length; i++) { 302 for (int i = 1; i < in.length; i++) {
297 if (in[i] == to) { 303 if (in[i] == to) {
298 return to; 304 return to;
299 } 305 }
306 if ((in[i] < to && up)
307 || (in[i] > to && !up)) {
308 continue;
309 }
300 double diff = Math.abs(to - in[i]); 310 double diff = Math.abs(to - in[i]);
301 if (diff < minDiff) { 311 if (diff <= minDiff) {
302 minDiff = diff; 312 minDiff = diff;
303 bestMatch = in[i]; 313 bestMatch = in[i];
304 } 314 }
305 } 315 }
306 return bestMatch; 316 return bestMatch;
401 * Callback for when a value has been accepted in the km-spinner 411 * Callback for when a value has been accepted in the km-spinner
402 * of a Cross-Section Profile theme. 412 * of a Cross-Section Profile theme.
403 * @param item The SpinnerItem which was manipulated. 413 * @param item The SpinnerItem which was manipulated.
404 * @param enteredKm The double-parsed value that has been entered. 414 * @param enteredKm The double-parsed value that has been entered.
405 * @param facetRecord The underlying datastores record. 415 * @param facetRecord The underlying datastores record.
416 * @param up If true, numerically higher values are preferred if
417 * two values in \param in are in the same distance to
418 * \param to.
406 */ 419 */
407 public void spinnerValueEntered(final SpinnerItem item, 420 public void spinnerValueEntered(final SpinnerItem item,
408 final double enteredKm, final FacetRecord facetRecord 421 final double enteredKm, final FacetRecord facetRecord, final boolean up
409 ) { 422 ) {
410 disable(); 423 disable();
411 Config config = Config.getInstance(); 424 Config config = Config.getInstance();
412 final String locale = config.getLocale(); 425 final String locale = config.getLocale();
413 426
439 } 452 }
440 @Override 453 @Override
441 public void onSuccess(Map<Integer, Double[]> obj) { 454 public void onSuccess(Map<Integer, Double[]> obj) {
442 Double[] kms = obj.get(dbid); 455 Double[] kms = obj.get(dbid);
443 double closest = 456 double closest =
444 CrossSectionChartThemePanel.closest(kms, enteredKm); 457 CrossSectionChartThemePanel.closest(kms, enteredKm, up);
445 GWT.log("Got single km close to " + enteredKm + " for " + dbid + ", it is " 458 GWT.log("Got single km close to " + enteredKm + " for " + dbid + ", it is "
446 + closest); 459 + closest);
447 460
448 // Do not set value, as it will trigger strange 461 // Do not set value, as it will trigger strange
449 // "javascript" bugs. /*item.setValue(closest);*/ 462 // "javascript" bugs. /*item.setValue(closest);*/
472 }); 485 });
473 } 486 }
474 487
475 488
476 /** 489 /**
477 * Create the handler for ChangeEvents on the Spinner in the
478 * facets that control km of cross section.
479 *
480 * @param facetRecord The FacetRecord (~row in table) where this
481 * handler is added to (to a child, to be exact).
482 */
483 // TODO obsolete?
484 public final ChangedHandler createSpinnerHandler(
485 final FacetRecord facetRecord)
486 {
487 ChangedHandler handler = new ChangedHandler()
488 {
489 @Override
490 public void onChanged(final ChangedEvent ce) {
491 if (ce.getValue() == null) {
492 return;
493 }
494 String value = ce.getValue().toString();
495
496 // Now, query the value with the kmService.
497 final double selected_km =
498 Double.parseDouble(value);
499 final SpinnerItem item = (SpinnerItem) ce.getItem();
500
501 spinnerValueEntered(item, selected_km, facetRecord);
502 }
503 };
504 return handler;
505 }
506
507
508 /**
509 * Create a "kilometer spinner" for CrossSection Facets. 490 * Create a "kilometer spinner" for CrossSection Facets.
510 * @param facetRecord The respective Facet/Theme. 491 * @param facetRecord The respective Facet/Theme.
511 * @return label, intialized SpinnerItem. 492 * @return label, intialized SpinnerItem.
512 */ 493 */
513 public SpinnerItem createSpinnerItem(FacetRecord facetRecord) { 494 public SpinnerItem createSpinnerItem(FacetRecord facetRecord) {
549 Button minusButton = new Button("-"); 530 Button minusButton = new Button("-");
550 minusButton.setWidth(10); 531 minusButton.setWidth(10);
551 minusButton.setHeight(height); 532 minusButton.setHeight(height);
552 minusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() { 533 minusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
553 public void onClick(ClickEvent evt) { 534 public void onClick(ClickEvent evt) {
554 spinnerValueEntered(null, currentValue - 0.1d, _facetRecord); 535 spinnerValueEntered(null, currentValue - 0.1d, _facetRecord, false);
555 } 536 }
556 }); 537 });
557 538
558 DynamicForm form = new DynamicForm(); 539 DynamicForm form = new DynamicForm();
559 final TextItem kmField = new TextItem(); 540 final TextItem kmField = new TextItem();
605 public void onBlur(BlurEvent be) { 586 public void onBlur(BlurEvent be) {
606 if (kmField.getValue() != null) { 587 if (kmField.getValue() != null) {
607 try { 588 try {
608 spinnerValueEntered(null, 589 spinnerValueEntered(null,
609 Double.parseDouble(kmField.getValue().toString()), 590 Double.parseDouble(kmField.getValue().toString()),
610 _facetRecord); 591 _facetRecord, true);
611 } 592 }
612 catch(NumberFormatException nfe) { 593 catch(NumberFormatException nfe) {
613 GWT.log("entered string cannot be parsed to double."); 594 GWT.log("entered string cannot be parsed to double.");
614 } 595 }
615 } 596 }
635 Button plusButton = new Button("+"); 616 Button plusButton = new Button("+");
636 plusButton.setWidth(10); 617 plusButton.setWidth(10);
637 plusButton.setHeight(height); 618 plusButton.setHeight(height);
638 plusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() { 619 plusButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
639 public void onClick(ClickEvent evt) { 620 public void onClick(ClickEvent evt) {
640 spinnerValueEntered(null, currentValue + 0.1d, _facetRecord); 621 spinnerValueEntered(null, currentValue + 0.1d, _facetRecord, true);
641 } 622 }
642 }); 623 });
643 this.addMember(minusButton); 624 this.addMember(minusButton);
644 this.addMember(form); 625 this.addMember(form);
645 this.addMember(plusButton); 626 this.addMember(plusButton);

http://dive4elements.wald.intevation.org