view artifacts/src/main/java/org/dive4elements/river/artifacts/states/RadioSelect.java @ 9369:1a4d2ce77423

radio choice fix
author gernotbelger
date Thu, 02 Aug 2018 17:39:13 +0200
parents c7e5285d434f
children
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * 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.artifacts.states;

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

import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.resources.Resources;
import org.w3c.dom.Element;

/**
 * @author Domenico Nardi Tironi
 *
 */
public abstract class RadioSelect extends DefaultState {

    private static final long serialVersionUID = 1L;
    private Artifact artifact;

    protected abstract String getDatakey();

    @Override
    protected abstract String getUIProvider(); // force override in subs!
    // {
    // return "radio_panel"; //no generic radiopanel
    // }

    protected abstract LinkedHashMap<String, String> makeEntries(CallMeta meta, Artifact artifact); // AUSNAHMSWEISE EXPLIZITER TYP, damit Reihenfolge nicht
                                                                                                    // verrutscht

    @Override
    protected Element[] createItems(final XMLUtils.ElementCreator ec, final Artifact artifact, final String name, final CallContext context) {

        // REMEBER: we need it for getLabelFor later

        if (name.equals(getDatakey())) {
            this.artifact = artifact;

            final CallMeta meta = context.getMeta();
            final Map<String, String> entries = makeEntries(meta, artifact);

            final Element[] elements = new Element[entries.size()];
            int i = 0;
            for (final String key : entries.keySet()) {
                final String label = entries.get(key);
                final String labelToSet = label != null ? label : Resources.getMsg(meta, key);
                elements[i] = createItem(ec, new String[] { labelToSet, key });
                i++;
            }
            return elements;
        }
        return super.createItems(ec, artifact, name, context);
    }

    @Override
    protected String getLabelFor(final CallContext cc, final String name, final String value, final String type) {
        // artifacts must be set in "createItems" -> createItems has to be called first
        final Map<String, String> entries = makeEntries(cc.getMeta(), this.artifact);

        final String valueToSet = entries.get(value) != null ? entries.get(value) : value;
        return super.getLabelFor(cc, name, valueToSet, type);
    }
}

http://dive4elements.wald.intevation.org