view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/ModuleSelection.java @ 7602:c50dbbe17950

issue1596: Store table (cell) data twice: Once as (formatted) string as coming from server, once transformed into float (or string). The benefit is that now we can sort table data numerically, while keeping the formatted and i18ned display of values.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 27 Nov 2013 14:55:25 +0100
parents 21d92d54303e
children 4d74d09228f0
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.client.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import org.dive4elements.river.client.client.Config;
import org.dive4elements.river.client.client.FLYSConstants;
import org.dive4elements.river.client.client.services.ModuleService;
import org.dive4elements.river.client.client.services.ModuleServiceAsync;
import org.dive4elements.river.client.shared.model.Data;
import org.dive4elements.river.client.shared.model.DataItem;
import org.dive4elements.river.client.shared.model.DataList;
import org.dive4elements.river.client.shared.model.DefaultData;
import org.dive4elements.river.client.shared.model.DefaultDataItem;
import org.dive4elements.river.client.shared.model.Module;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * The ModuleSelection combines the river selection and the module selection in
 * one widget. It will display a vertical splitted widget - the upper part will
 * render checkboxes for each module, the lower one will display a combobox at
 * the left and a map panel on the right to choose the river.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ModuleSelection extends MapSelection {

    private static final long serialVersionUID = -5634831815175543328L;

    /** The message class that provides i18n strings.*/
    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);

    /** The module checkboxes.*/
    protected static RadioGroupItem radio;

    /** */
    protected Module[] modules;

    /** The ModuleService used to retrieve the available modules of a user.*/
    protected ModuleServiceAsync moduleService = GWT.create(ModuleService.class);

    private List<String> fixRivers;
    private List<String> minfoRivers;
    protected Map<String, HLayout> rivers;

    /**
     * The default constructor.
     */
    public ModuleSelection() {
        rivers = null;
        //TODO: put all the rivers into a config file, or something.
        fixRivers = new ArrayList<String>();
        fixRivers.add("Rhein");
        fixRivers.add("Elbe");
        fixRivers.add("Donau");
        fixRivers.add("Fulda (Sommer)");
        fixRivers.add("Havel");
        fixRivers.add("Lahn");
        fixRivers.add("Main");
        fixRivers.add("Mosel");
        fixRivers.add("Neckar");
        fixRivers.add("Oder");
        fixRivers.add("Saale");
        fixRivers.add("Saar");
        fixRivers.add("Saar (Wiltinger Bogen)");
        fixRivers.add("Werra (Sommer)");
        fixRivers.add("Weser");

        minfoRivers = new ArrayList<String>();
        minfoRivers.add("Elbe");
        minfoRivers.add("Rhein");
        minfoRivers.add("Oder");

        readModules();
    }


    /**
     * This method returns a widget that renders the checkboxes for each module
     * and the MapSelection that lets the user choose the river.
     *
     * @param data The provided rivers.
     *
     * @return the module selection combined with the river selection.
     */
    @Override
    public Canvas create(DataList data) {
        GWT.log("ModuleSelection - create()");
        createCallback();
        VLayout newLayout = new VLayout();
        newLayout.setMembersMargin(10);
        newLayout.setAlign(VerticalAlignment.TOP);
        Canvas moduleSelection = createWidget();

        moduleSelection.setHeight(100);
        newLayout.setHeight(70);
        newLayout.addMember(moduleSelection);

        return newLayout;
    }

    private void readModules() {
        Config config = Config.getInstance();
        String locale = config.getLocale();

        moduleService.list(locale, new AsyncCallback<Module[]>() {
            @Override
            public void onFailure(Throwable caught) {
                GWT.log("Could not recieve a list of modules.");
                SC.warn(MSG.getString(caught.getMessage()));
            }

            @Override
            public void onSuccess(Module[] newmodules) {
                GWT.log("Retrieved " + newmodules.length + " modules.");
                modules = newmodules;
                setModules();
            }
        });
    }

    private void setModules() {
        LinkedHashMap<String, String> values = new LinkedHashMap<String, String>();

        if (this.modules!= null) {
            for(Module module : this.modules) {
                values.put(module.getName(), module.getLocalizedName());
                if (module.isSelected()) {
                    GWT.log("Module " + module.getName() + " is selected.");
                    if (radio != null) {
                        radio.setDefaultValue(module.getName());
                        GWT.log("Setting " + module.getName() + " as selected.");
                    }
                }
            }
        }
        if (radio != null) {
            radio.setValueMap(values);
        }
    }

    /**
     * Creates a widget that displays a checkbox for each module.
     *
     * @return a widget with checkboxes.
     */
    protected Canvas createWidget() {
        HLayout layout = new HLayout();

        Label      label = new Label(MESSAGES.module_selection());
        DynamicForm form = new DynamicForm();

        radio = new RadioGroupItem("plugin");
        radio.addChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                String selected = (String)event.getValue();
                if (!rivers.isEmpty()) {
                    for (Map.Entry<String, HLayout> s: rivers.entrySet()) {
                        if (selected.equals("minfo") && !minfoRivers.contains(s.getKey())) {
                            s.getValue().hide();
                        }
                        else if (selected.equals("fixanalysis") && !fixRivers.contains(s.getKey())) {
                            s.getValue().hide();
                        }
                        else {
                            s.getValue().show();
                        }
                    }
                }
            }
        });

        label.setWidth(50);
        label.setHeight(25);


        radio.setShowTitle(false);
        radio.setVertical(true);

        setModules();

        form.setFields(radio);

        layout.addMember(label);
        layout.addMember(form);

        return layout;
    }


    /**
     * This method prepares the data of two widgets - the module selection and
     * the river selection. The returning field will contain the Data that
     * represents the module selection at first position, the second position
     * stores the Data object that represents the river selection.
     *
     * @return the Data that was chosen in this widget.
     */
    @Override
    protected Data[] getData() {

        String module = radio.getValueAsString();

        DataItem[] items = new DefaultDataItem[1];
        items[0]         = new DefaultDataItem(module, module, module);

        Data       data  = new DefaultData("module", null, null, items);

        return new Data[] {data};
    }

    public void setRivers(Map<String, HLayout> rivers) {
        this.rivers = rivers;
    }

    private native void createCallback() /*-{
        $wnd.getModule = @org.dive4elements.river.client.client.ui.ModuleSelection::getSelectedModule();
    }-*/;

    private static String getSelectedModule() {
        GWT.log("selected: " + radio.getValueAsString());
        return radio.getValueAsString();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org