view artifacts/src/main/java/org/dive4elements/river/artifacts/states/ComputationRangeState.java @ 9175:34dc0163ad2d

DistancePanel Berechnungsstrecke->Darstellungsbereich Refactoring
author gernotbelger
date Mon, 25 Jun 2018 17:58:11 +0200
parents 8c0d1542c1d8
children baef46792354
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.artifacts.states;

import org.apache.log4j.Logger;
import org.dive4elements.artifactdatabase.ProtocolUtils;
import org.dive4elements.artifactdatabase.data.StateData;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.RiverAccess;
import org.dive4elements.river.artifacts.model.FacetTypes;
import org.dive4elements.river.artifacts.resources.Resources;
import org.w3c.dom.Element;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ComputationRangeState extends RangeState implements FacetTypes {
    private static Logger log = Logger.getLogger(ComputationRangeState.class);

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

    public ComputationRangeState() {
    }

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

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

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

        final String msg = getTitle(context);

        label.setTextContent(msg);
        // VORHER: data.getName(), data.getName())) (entsprach "ld_from", "ld_to" - unterschied zu jetzt: description wird
        // anders gesetzt ("Wahl der Berechnungsstrecke")
        // label bleibt "ld_from", "ld_to"; unklar, wo das gesetzt wird. unklar, warum das Element "label" die Description
        // beeinflusst (und scheinbar nicht das Label)

        final Element description = ProtocolUtils.createArtNode(cr, "description", null, null);
        description.setTextContent("TEST");
        select.appendChild(description);
        select.appendChild(label);

        return select;
    }

    // REMARK: allows to overwrite the titel which is shown on the client side.
    // TODO: instead of a fixed string, we should translate the 'id', but in that case we need to change all old workflows
    // (reason is, that we have different texts for the title and the 'old' title, which in turn is the translation of
    // 'description').

    protected String getTitle(final CallContext context) {
        // REMARK: that is how it should be: return Resources.getMsg(context.getMeta(), getID());
        return Resources.getMsg(context.getMeta(), "state.title.distance_state");
    }

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

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

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

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

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

            return new Element[] { max };
        } else {
            final Element step = createItem(cr, new String[] { "step", String.valueOf(getDefaultStep()) });
            return new Element[] { step };
        }

    }

    @Override
    protected double[] getMinMax(final Artifact artifact) {
        return new RiverAccess((D4EArtifact) artifact).getRiver().determineMinMaxDistance();
    }

    protected double getDefaultStep() {
        return DEFAULT_STEP;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org