view flys-client/src/main/java/de/intevation/flys/client/client/ui/LocationDistancePanel.java @ 45:f99c5f8e4672

Some GUI improvements in the Location/Distance, W/Q and Module panels. flys-client/trunk@1490 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 17 Mar 2011 09:54:05 +0000
parents 6bcd8e3f21a7
children 0d4795b4f284
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;

import com.smartgwt.client.types.TitleOrientation;
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.FloatItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
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.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() {
        // TODO Implement me

        return null;
    }


    /**
     * 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 :

http://dive4elements.wald.intevation.org