teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
teichmann@5863: * Software engineering by Intevation GmbH
teichmann@5863: *
teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3)
teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the
teichmann@5994: * documentation coming with Dive4Elements River for details.
teichmann@5863: */
teichmann@5863:
teichmann@5831: package org.dive4elements.river.artifacts.states;
ingo@136:
ingo@136: import org.apache.log4j.Logger;
gernotbelger@9175: import org.dive4elements.artifactdatabase.data.StateData;
teichmann@5831: import org.dive4elements.artifacts.Artifact;
gernotbelger@9175: import org.dive4elements.artifacts.CallContext;
teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact;
teichmann@5831: import org.dive4elements.river.artifacts.WINFOArtifact;
andre@8451: import org.dive4elements.river.artifacts.access.RangeAccess;
gernotbelger@9175: import org.dive4elements.river.artifacts.resources.Resources;
ingo@136:
ingo@136: /**
ingo@136: * @author Ingo Weinzierl
ingo@136: */
gernotbelger@9175: public class LocationDistanceSelect extends ComputationRangeState {
ingo@136:
teichmann@8202: /** The log used in this class. */
teichmann@8202: private static Logger log = Logger.getLogger(LocationDistanceSelect.class);
ingo@136:
ingo@627: /** The name of the 'mode' field. */
ingo@627: public static final String MODE = "ld_mode";
ingo@627:
felix@2235: /** The name of the 'locations' field. */
ingo@627: public static final String LOCATIONS = "ld_locations";
ingo@627:
ingo@136: /**
ingo@136: * The default constructor that initializes an empty State object.
ingo@136: */
ingo@136: public LocationDistanceSelect() {
ingo@136: }
ingo@136:
sascha@660: @Override
ingo@136: protected String getUIProvider() {
ingo@136: return "location_distance_panel";
ingo@136: }
ingo@313:
felix@3265: /** Validates the range (or location). */
ingo@322: @Override
gernotbelger@9175: public boolean validate(final Artifact artifact) throws IllegalArgumentException {
teichmann@8202: log.debug("LocationDistanceSelect.validate");
ingo@322:
gernotbelger@9175: final D4EArtifact flys = (D4EArtifact) artifact;
gernotbelger@9175: final StateData mode = getData(flys, MODE);
gernotbelger@9175: final String mValue = mode != null ? (String) mode.getValue() : null;
raimund@3628: if (mValue != null) {
raimund@3628: if (mValue.equals("distance")) {
raimund@3628: return super.validate(flys);
gernotbelger@9175: } else {
raimund@3628: return validateLocations(flys);
raimund@3628: }
ingo@627: }
raimund@3628: return false;
ingo@627: }
ingo@627:
felix@4043: /** Validate selected locations. */
gernotbelger@9175: protected boolean validateLocations(final D4EArtifact flys) throws IllegalArgumentException {
gernotbelger@9175: final StateData dValues = getData(flys, LOCATIONS);
gernotbelger@9175: final String values = dValues != null ? (String) dValues.getValue() : null;
ingo@627:
ingo@627: if (values == null || values.length() == 0) {
ingo@627: throw new IllegalArgumentException("error_empty_state");
ingo@627: }
ingo@627:
gernotbelger@9175: final double[] absMinMax = getMinMax(flys);
gernotbelger@9175: final double[] relMinMax = getMinMaxFromString(values);
ingo@627:
ingo@627: if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) {
ingo@627: throw new IllegalArgumentException("error_feed_from_out_of_range");
ingo@627: }
ingo@627:
ingo@627: if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) {
ingo@627: throw new IllegalArgumentException("error_feed_to_out_of_range");
ingo@627: }
ingo@627:
ingo@627: return true;
ingo@627: }
ingo@627:
ingo@627: /**
ingo@627: * Extracts the min/max values from String s. An
ingo@627: * IllegalArgumentException is thrown if there is a value that throws a
ingo@627: * NumberFormatException.
ingo@627: *
gernotbelger@9175: * @param s
gernotbelger@9175: * String that contains whitespace separated double values.
ingo@627: *
ingo@627: * @return a 2dmin array [min,max].
ingo@627: */
gernotbelger@9175: public static double[] getMinMaxFromString(final String s) throws IllegalArgumentException {
gernotbelger@9175: final String[] values = s.split(" ");
ingo@627:
gernotbelger@9175: final double[] minmax = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
ingo@627:
gernotbelger@9175: for (final String v : values) {
ingo@627: try {
gernotbelger@9175: final double value = Double.valueOf(v);
ingo@627:
ingo@627: minmax[0] = minmax[0] < value ? minmax[0] : value;
ingo@627: minmax[1] = minmax[1] > value ? minmax[1] : value;
ingo@627: }
gernotbelger@9175: catch (final NumberFormatException nfe) {
gernotbelger@9175: throw new IllegalArgumentException("error_invalid_double_value");
ingo@627: }
ingo@627: }
ingo@627:
ingo@627: return minmax;
ingo@627: }
ingo@628:
gernotbelger@9175: public static double[] getLocations(final WINFOArtifact flys) {
gernotbelger@9175: final RangeAccess ra = new RangeAccess(flys);
andre@8451: return ra.getLocations();
ingo@628: }
gernotbelger@9175:
gernotbelger@9175: @Override
gernotbelger@9175: protected String getTitle(final CallContext context) {
gernotbelger@9175: // REMARK: that is how it should be: return Resources.getMsg(context.getMeta(), getID());
gernotbelger@9175: return Resources.getMsg(context.getMeta(), "state.title.location_distance_state");
gernotbelger@9175: }
gernotbelger@9175:
ingo@136: }
ingo@136: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :