view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java @ 321:9581b88f2920

Removed testing code that has been comitted by accident in the last revision. flys-artifacts/trunk@1714 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 18 Apr 2011 09:34:18 +0000
parents 89bd0417418f
children 448d0dc64357
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import org.apache.log4j.Logger;

import org.w3c.dom.Element;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.data.StateData;

import de.intevation.flys.model.River;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.RiverFactory;
import de.intevation.flys.artifacts.resources.Resources;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class LocationDistanceSelect extends DefaultState {

    /** The logger used in this class.*/
    private static Logger logger = Logger.getLogger(LocationDistanceSelect.class);


    /** The default step width.*/
    public static final String DEFAULT_STEP = "100";

    /**
     * The default constructor that initializes an empty State object.
     */
    public LocationDistanceSelect() {
    }

    protected Element createData(
        XMLUtils.ElementCreator cr,
        Artifact    artifact,
        StateData   data,
        CallContext context)
    {
        Element select = ProtocolUtils.createArtNode(
            cr, "select", null, null);

        cr.addAttr(select, "name", data.getName(), true);

        Element label = ProtocolUtils.createArtNode(
            cr, "label", null, null);

        Element choices = ProtocolUtils.createArtNode(
            cr, "choices", null, null);

        label.setTextContent(Resources.getMsg(
            context.getMeta(),
            data.getName(),
            data.getName()));

        select.appendChild(label);

        return select;
    }


    protected Element[] createItems(
        XMLUtils.ElementCreator cr,
        Artifact    artifact,
        String      name,
        CallContext context)
    {
        double[] minmax = getMinMaxDistance(artifact);

        double minVal = Double.MIN_VALUE;
        double maxVal = Double.MAX_VALUE;

        if (minmax != null) {
            minVal = minmax[0];
            maxVal = minmax[1];
        }
        else {
            logger.warn("Could not read min/max distance values!");
        }

        if (name.equals("ld_from")) {
            Element min = createItem(
                cr,
                new String[] {"min", new Double(minVal).toString()});

            return new Element[] { min };
        }
        else if (name.equals("ld_to")) {
            Element max = createItem(
                cr,
                new String[] {"max", new Double(maxVal).toString()});

            return new Element[] { max };
        }
        else {
            Element step = createItem(cr, new String[] {"step", DEFAULT_STEP});
            return new Element[] { step };
        }

    }


    protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
        Element item  = ProtocolUtils.createArtNode(cr, "item", null, null);
        Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
        Element value = ProtocolUtils.createArtNode(cr, "value", null, null);

        String[] arr = (String[]) obj;

        label.setTextContent(arr[0]);
        value.setTextContent(arr[1]);

        item.appendChild(label);
        item.appendChild(value);

        return item;
    }


    protected String getUIProvider() {
        return "location_distance_panel";
    }


    protected double[] getMinMaxDistance(Artifact artifact) {
        FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
        StateData    data         = flysArtifact.getData("river");

        String name = (String) data.getValue();

        logger.debug("Search for the min/max distances of '" + name + "'");

        River river = RiverFactory.getRiver(name);

        return river != null ? river.determineMinMaxDistance() : null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org