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@383: ingo@383: import org.apache.log4j.Logger; gernotbelger@9584: import org.dive4elements.artifactdatabase.data.StateData; gernotbelger@9584: import org.dive4elements.artifacts.Artifact; gernotbelger@9584: import org.dive4elements.artifacts.CallContext; gernotbelger@9584: import org.dive4elements.artifacts.common.utils.XMLUtils; gernotbelger@9584: import org.dive4elements.river.artifacts.D4EArtifact; ingo@679: import org.w3c.dom.Element; ingo@679: gernotbelger@9584: import gnu.trove.TDoubleArrayList; ingo@383: ingo@383: /** felix@3284: * This state is used to realize the input of multiple locations as string. felix@3284: * felix@3284: * The string should be a whitespace separated list of double values where each ingo@383: * double value represents a location. ingo@383: * ingo@383: * @author Ingo Weinzierl ingo@383: */ ingo@383: public class LocationSelect extends LocationDistanceSelect { ingo@383: gernotbelger@9584: private static final long serialVersionUID = 1L; gernotbelger@9584: /** The log used in this class. */ teichmann@8202: private static Logger log = Logger.getLogger(LocationSelect.class); ingo@383: ingo@383: public LocationSelect() { ingo@383: } ingo@383: felix@3284: /** UI Provider (which input method should the client provide to user. */ ingo@383: @Override ingo@383: protected String getUIProvider() { ingo@383: return "location_panel"; ingo@383: } ingo@383: ingo@383: @Override gernotbelger@9584: protected Element[] createItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) { gernotbelger@9584: final double[] minmax = getMinMax(artifact); ingo@679: ingo@679: double minVal = Double.MIN_VALUE; ingo@679: double maxVal = Double.MAX_VALUE; ingo@679: ingo@679: if (minmax != null) { ingo@679: minVal = minmax[0]; ingo@679: maxVal = minmax[1]; gernotbelger@9584: } else { teichmann@8202: log.warn("Could not read min/max distance values!"); ingo@679: } ingo@679: felix@2235: if (name.equals(LOCATIONS)) { gernotbelger@9584: final Element min = createItem(cr, new String[] { "min", new Double(minVal).toString() }); ingo@679: gernotbelger@9584: final Element max = createItem(cr, new String[] { "max", new Double(maxVal).toString() }); ingo@679: ingo@679: return new Element[] { min, max }; ingo@679: } ingo@679: ingo@679: return null; ingo@679: } ingo@679: felix@3284: /** Validates data from artifact. */ ingo@679: @Override gernotbelger@9584: public boolean validate(final Artifact artifact) throws IllegalArgumentException { teichmann@8202: log.debug("LocationSelect.validate"); ingo@383: gernotbelger@9584: final D4EArtifact flys = (D4EArtifact) artifact; gernotbelger@9584: final StateData data = getData(flys, LOCATIONS); ingo@383: gernotbelger@9584: final String locationStr = data != null ? (String) data.getValue() : null; ingo@383: ingo@383: if (locationStr == null || locationStr.length() == 0) { teichmann@8202: log.error("No locations given."); ingo@383: throw new IllegalArgumentException("error_empty_state"); ingo@383: } ingo@383: gernotbelger@9584: final double[] minmax = getMinMax(artifact); gernotbelger@9584: final double[] mm = extractLocations(locationStr); ingo@383: teichmann@8202: log.debug("Inserted min location: " + mm[0]); gernotbelger@9584: log.debug("Inserted max location: " + mm[mm.length - 1]); ingo@383: gernotbelger@9584: return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length - 1], 0d); ingo@383: } ingo@383: ingo@383: /** ingo@383: * This method takes a string that consist of whitespace separated double ingo@383: * values and returns the double values as array. ingo@383: * gernotbelger@9584: * @param locationStr gernotbelger@9584: * The locations inserted in this state. ingo@383: * ingo@383: * @return the locations as array. ingo@383: */ gernotbelger@9584: protected double[] extractLocations(final String locationStr) { gernotbelger@9584: final String[] tmp = locationStr.split(" "); gernotbelger@9584: final TDoubleArrayList locations = new TDoubleArrayList(); ingo@383: gernotbelger@9584: for (final String l : tmp) { ingo@383: try { ingo@383: locations.add(Double.parseDouble(l)); ingo@383: } gernotbelger@9584: catch (final NumberFormatException nfe) { teichmann@8202: log.warn(nfe, nfe); ingo@383: } ingo@383: } ingo@383: ingo@383: locations.sort(); ingo@383: ingo@383: return locations.toNativeArray(); ingo@383: } ingo@383: } ingo@383: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :