# HG changeset patch # User Ingo Weinzierl # Date 1300292683 0 # Node ID 6bcd8e3f21a7a82392763c8837fc658a19dbeedf # Parent ba7df4a24ae0b331daed57c41158531acc432c8f Refactored the LocationDistancePanel, so that is uses the DoubleArrayPanel and DoubleRangePanel from the last commit as well. flys-client/trunk@1484 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r ba7df4a24ae0 -r 6bcd8e3f21a7 flys-client/ChangeLog --- a/flys-client/ChangeLog Wed Mar 16 15:52:13 2011 +0000 +++ b/flys-client/ChangeLog Wed Mar 16 16:24:43 2011 +0000 @@ -1,3 +1,9 @@ +2011-03-16 Ingo Weinzierl + + * src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java: + Some refactoring. This class now uses the DoubleRangePanel and + DoubleArrayPanel for the two different input modes as well. + 2011-03-16 Ingo Weinzierl * src/main/java/de/intevation/flys/client/client/FLYSMessages_en.properties, diff -r ba7df4a24ae0 -r 6bcd8e3f21a7 flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java --- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java Wed Mar 16 15:52:13 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java Wed Mar 16 16:24:43 2011 +0000 @@ -1,5 +1,6 @@ package de.intevation.flys.client.client.ui; +import java.util.LinkedHashMap; import java.util.Map; import com.google.gwt.core.client.GWT; @@ -40,6 +41,9 @@ /** The constant name of the input field to enter locations.*/ public static final String FIELD_LOCATION = "location"; + /** The constant name of the input field to enter distance.*/ + public static final String FIELD_DISTANCE = "distance"; + /** The constant name of the input field to enter the start of a distance.*/ public static final String FIELD_FROM = "from"; @@ -52,11 +56,23 @@ /** The radio group for input mode selection.*/ - protected RadioGroupItem radio; + protected DynamicForm mode; /** A container that will contain the location or the distance panel.*/ protected HLayout container; + /** The 'from' value entered in the distance mode.*/ + protected double from; + + /** The 'to' value entered in the distance mode.*/ + protected double to; + + /** The 'step' value entered in the distance mode.*/ + protected double step; + + /** The values entered in the location mode.*/ + protected double[] values; + /** * Creates a new LocationDistancePanel instance. @@ -117,15 +133,22 @@ return; } - if (value.equals(MESSAGES.location())) { - // TODO Insert correct values - Canvas locationPanel = createLocationPanel(new double[] {1.7, 2.5}); + if (value.equals(FIELD_LOCATION)) { + Canvas locationPanel = new DoubleArrayPanel( + MESSAGES.unitLocation(), + getLocationValues(), + this); + container.removeMembers(container.getMembers()); container.addMember(locationPanel); } - else if (value.equals(MESSAGES.distance())){ - // TODO Insert correct values - Canvas distancePanel = createDistancePanel(1.0, 3.4, 100); + else { + Canvas distancePanel = new DoubleRangePanel( + MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(), + getFrom(), getTo(), getStep(), + 250, + this); + container.removeMembers(container.getMembers()); container.addMember(distancePanel); } @@ -146,85 +169,22 @@ return; } - if (field.equals(FIELD_LOCATION)) { - validateLocation(event.getForm(), item); - } - else if (field.equals(FIELD_FROM)) { - validateDistance(event.getForm(), item); - } - else if (field.equals(FIELD_TO)) { - validateDistance(event.getForm(), item); - } - else if (field.equals(FIELD_WIDTH)) { - validateDistance(event.getForm(), item); - } - } - - - /** - * This method validates the entered text in the location input field. If - * there are values that doesn't represent a valid location, an error is - * displayed. - * - * @param form The DynamicForm. - * @param item The FormItem. - */ - protected void validateLocation(DynamicForm form, FormItem item) { - String value = (String) item.getValue(); - String[] parts = value.split(" "); - - if (parts == null) { - return; - } - - NumberFormat f = NumberFormat.getDecimalFormat(); - Map errors = form.getErrors(); - - try { - for (String part: parts) { - double location = f.parse(part); + if (field.equals(DoubleArrayPanel.FIELD_NAME)) { + DoubleArrayPanel p = (DoubleArrayPanel) event.getForm(); - // TODO save the location + if (p.validateForm(item)) { + setLocationValues(p.getInputValues(item)); } - - errors.remove(item.getFieldName()); } - catch (NumberFormatException nfe) { - errors.put(item.getFieldName(), MESSAGES.wrongFormat()); - } - - form.setErrors(errors, true); - } - + else { + DoubleRangePanel p = (DoubleRangePanel) event.getForm(); - /** - * This method validates the entered text in the distance input fields. If - * there are values that doesn't represent a valid float, an error is - * displayed. - * - * @param form The DynamicForm. - * @param item The FormItem. - */ - protected void validateDistance(DynamicForm form, FormItem item) { - String v = (String) item.getValue(); - - NumberFormat f = NumberFormat.getDecimalFormat(); - Map errors = form.getErrors(); - - try { - double value = f.parse(v); - - // TODO SAVE THE VALUE - - errors.remove(item.getFieldName()); + if (p.validateForm(item)) { + setFrom(p.getFrom()); + setTo(p.getTo()); + setStep(p.getStep()); + } } - catch (NumberFormatException nfe) { - errors.put(item.getFieldName(), MESSAGES.wrongFormat()); - - item.focusInItem(); - } - - form.setErrors(errors, true); } @@ -235,107 +195,62 @@ * @return the checkbox panel. */ protected Canvas createRadioButtonPanel() { - DynamicForm form = new DynamicForm(); + mode = new DynamicForm(); - radio = new RadioGroupItem("mode"); + RadioGroupItem radio = new RadioGroupItem("mode"); radio.setShowTitle(false); radio.setVertical(false); - radio.setValueMap(MESSAGES.location(), MESSAGES.distance()); + LinkedHashMap values = new LinkedHashMap(); + values.put(FIELD_LOCATION, MESSAGES.location()); + values.put(FIELD_DISTANCE, MESSAGES.distance()); + + radio.setValueMap(values); radio.addChangeHandler(this); - form.setFields(radio); + mode.setFields(radio); - return form; + return mode; } - /** - * This method creates the location panel. It contains just a single input - * field. - * - * @param locations For each location a double value - * - * @return a panel with an input field. - */ - protected Canvas createLocationPanel(double[] locations) { - TextItem ti = new TextItem(FIELD_LOCATION); - - ti.setTitle(MESSAGES.unitLocation()); - ti.addBlurHandler(this); - - NumberFormat f = NumberFormat.getDecimalFormat(); - - StringBuilder text = new StringBuilder(); - boolean firstItem = true; - - for (double loc: locations) { - if (!firstItem) { - text.append(" "); - } - - text.append(f.format(loc)); - - firstItem = false; - } - - ti.setValue(text.toString()); - - // TODO There is still a colon between the input field and the text - - // remove this colon! - - DynamicForm form = new DynamicForm(); - form.setFields(ti); - form.setTitleOrientation(TitleOrientation.RIGHT); - form.setNumCols(2); - - return form; + protected double getFrom() { + return from; } - /** - * This method creates the distance panel. It contains three input fields: - * one for a start, one for the end and one for a step width. - * - * @param start The start point. - * @param end The end point. - * @param sw The step width. - * - * @return a panel with three input fields. - */ - protected Canvas createDistancePanel(double start,double end,double sw) { - FloatItem from = new FloatItem(FIELD_FROM); - FloatItem to = new FloatItem(FIELD_TO); - FloatItem width = new FloatItem(FIELD_WIDTH); - - from.addBlurHandler(this); - to.addBlurHandler(this); - width.addBlurHandler(this); + protected void setFrom(double from) { + this.from = from; + } - // TODO There is still a colon between the input field and the text - - // remove this colon! - - NumberFormat f = NumberFormat.getDecimalFormat(); - - from.setValue(f.format(start)); - to.setValue(f.format(end)); - width.setValue(f.format(sw)); - from.setWidth(50); - to.setWidth(50); - width.setWidth(50); + protected double getTo() { + return to; + } - from.setTitle(MESSAGES.unitFrom()); - to.setTitle(MESSAGES.unitTo()); - width.setTitle(MESSAGES.unitWidth()); - DynamicForm form = new DynamicForm(); - form.setFields(from, to, width); - form.setTitleOrientation(TitleOrientation.RIGHT); - form.setNumCols(6); - form.setFixedColWidths(false); + protected void setTo(double to) { + this.to = to; + } - return form; + + protected double getStep() { + return step; + } + + + protected void setStep(double step) { + this.step = step; + } + + + protected double[] getLocationValues() { + return values; + } + + + protected void setLocationValues(double[] values) { + this.values = values; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :