Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java @ 49:d573ae975330
Some refactoring (imports, etc).
flys-client/trunk@1497 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 17 Mar 2011 11:39:27 +0000 |
parents | 0d4795b4f284 |
children | a2923d63f530 |
line wrap: on
line source
package de.intevation.flys.client.client.ui; import java.util.LinkedHashMap; import com.google.gwt.core.client.GWT; import com.smartgwt.client.widgets.Canvas; import com.smartgwt.client.widgets.IButton; import com.smartgwt.client.widgets.Label; import com.smartgwt.client.widgets.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.events.BlurHandler; import com.smartgwt.client.widgets.form.fields.events.BlurEvent; import com.smartgwt.client.widgets.form.fields.events.ChangeHandler; import com.smartgwt.client.widgets.form.fields.events.ChangeEvent; import com.smartgwt.client.widgets.form.fields.FormItem; import com.smartgwt.client.widgets.form.fields.RadioGroupItem; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.layout.VLayout; import de.intevation.flys.client.shared.model.Data; import de.intevation.flys.client.shared.model.DataItem; import de.intevation.flys.client.shared.model.DefaultData; import de.intevation.flys.client.shared.model.DefaultDataItem; import de.intevation.flys.client.client.FLYSMessages; /** * This UIProvider creates a widget to enter locations or a distance. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class LocationDistancePanel extends AbstractUIProvider implements ChangeHandler, BlurHandler { /** The message class that provides i18n strings.*/ protected FLYSMessages MESSAGES = GWT.create(FLYSMessages.class); /** 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"; /** The constant name of the input field to enter the end of a distance.*/ public static final String FIELD_TO = "to"; /** The constant name of the input field to enter the step width of a * distance.*/ public static final String FIELD_WIDTH = "width"; /** The radio group for input mode selection.*/ 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. */ public LocationDistancePanel() { } /** * This method creates a widget that contains a label, a panel with * checkboxes to switch the input mode between location and distance input, * and a the mode specific panel. * * @param data The data that might be inserted. * * @return a panel. */ public Canvas create(Data data) { VLayout layout = new VLayout(); Label label = new Label(MESSAGES.location_distance_state()); Canvas widget = createWidget(data); IButton submit = new IButton(MESSAGES.next(), this); label.setHeight(25); layout.addMember(label); layout.addMember(widget); layout.addMember(submit); return layout; } protected Canvas createWidget(Data data) { VLayout layout = new VLayout(); container = new HLayout(); Canvas checkboxPanel = createRadioButtonPanel(); // the initial view will display the location input mode Canvas locationPanel = new DoubleArrayPanel( MESSAGES.unitLocation(), getLocationValues(), this); container.addMember(locationPanel); layout.addMember(checkboxPanel); layout.addMember(container); // TODO Add a table on the right to select locations by mouse click return layout; } /** * This method returns the selected data. * * @return the selected/inserted data. */ public Data[] getData() { return new Data[] { getDataFrom(), getDataTo(), getDataStep() }; } /** * Returns the Data object for the 'from' attribute. * * @return the Data object for the 'from' attribute. */ protected Data getDataFrom() { String value = Double.valueOf(getFinalFrom()).toString(); DataItem item = new DefaultDataItem("ld_from", "ld_from", value); return new DefaultData( "ld_from", null, null, new DataItem[] { item }, null); } /** * Returns the Data object for the 'to' attribute. * * @return the Data object for the 'to' attribute. */ protected Data getDataTo() { String value = Double.valueOf(getFinalTo()).toString(); DataItem item = new DefaultDataItem("ld_to", "ld_to", value); return new DefaultData( "ld_to", null, null, new DataItem[] { item }, null); } /** * Returns the Data object for the 'step' attribute. * * @return the Data object for the 'step' attribute. */ protected Data getDataStep() { String value = Double.valueOf(getFinalStep()).toString(); DataItem item = new DefaultDataItem("ld_step","ld_step", value); return new DefaultData( "ld_step", null, null, new DataItem[] { item }, null); } /** * Returns the value of 'from' depending on the selected input mode. * * @return the value of 'from' depending on the selected input mode. */ protected double getFinalFrom() { if (isLocationMode()) { double[] values = getLocationValues(); double value = Double.MAX_VALUE; for (double v: values) { value = value < v ? value : v; } return value; } else { return getFrom(); } } /** * Returns the value of 'to' depending on the selected input mode. * * @return the value of 'to' depending on the selected input mode. */ protected double getFinalTo() { if (isLocationMode()) { double[] values = getLocationValues(); double value = Double.MIN_VALUE; for (double v: values) { value = value > v ? value : v; } return value; } else { return getTo(); } } /** * Returns the value of 'step' depending on the selected input mode. * * @return the value of 'step' depending on the selected input mode. */ protected double getFinalStep() { if (isLocationMode()) { // we have no field to enter the 'step' attribute in the location // mode return 0.0; } else { return getStep(); } } /** * Determines the current input mode. * * @return true, if 'location' is the current input mode, otherwise false. */ public boolean isLocationMode() { String inputMode = mode.getValueAsString("mode"); return inputMode.equals(FIELD_LOCATION) ? true : false; } /** * This method switches the input mode between location and distance input. * * @param event The click event fired by a RadioButtonGroupItem. */ public void onChange(ChangeEvent event) { String value = (String) event.getValue(); if (value == null) { return; } if (value.equals(FIELD_LOCATION)) { Canvas locationPanel = new DoubleArrayPanel( MESSAGES.unitLocation(), getLocationValues(), this); container.removeMembers(container.getMembers()); container.addMember(locationPanel); } else { Canvas distancePanel = new DoubleRangePanel( MESSAGES.unitFrom(), MESSAGES.unitTo(), MESSAGES.unitWidth(), getFrom(), getTo(), getStep(), 250, this); container.removeMembers(container.getMembers()); container.addMember(distancePanel); } } /** * This method is used to validate the inserted data in the form fields. * * @param event The BlurEvent that gives information about the FormItem that * has been modified and its value. */ public void onBlur(BlurEvent event) { FormItem item = event.getItem(); String field = item.getFieldName(); if (field == null) { return; } if (field.equals(DoubleArrayPanel.FIELD_NAME)) { DoubleArrayPanel p = (DoubleArrayPanel) event.getForm(); if (p.validateForm(item)) { setLocationValues(p.getInputValues(item)); } } else { DoubleRangePanel p = (DoubleRangePanel) event.getForm(); if (p.validateForm(item)) { setFrom(p.getFrom()); setTo(p.getTo()); setStep(p.getStep()); } } } /** * This method creates the panel that contains the checkboxes to switch * between the input mode 'location' and 'distance'. * * @return the checkbox panel. */ protected Canvas createRadioButtonPanel() { mode = new DynamicForm(); RadioGroupItem radio = new RadioGroupItem("mode"); radio.setShowTitle(false); radio.setVertical(false); LinkedHashMap values = new LinkedHashMap(); values.put(FIELD_LOCATION, MESSAGES.location()); values.put(FIELD_DISTANCE, MESSAGES.distance()); LinkedHashMap initial = new LinkedHashMap(); initial.put("mode", FIELD_LOCATION); radio.setValueMap(values); radio.addChangeHandler(this); mode.setFields(radio); mode.setValues(initial); return mode; } protected double getFrom() { return from; } protected void setFrom(double from) { this.from = from; } protected double getTo() { return to; } protected void setTo(double to) { this.to = to; } 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 :