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 gnu.trove.TDoubleArrayList; ingo@383: ingo@383: import org.apache.log4j.Logger; ingo@383: ingo@679: import org.w3c.dom.Element; ingo@679: teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; ingo@383: teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; ingo@679: teichmann@5831: import org.dive4elements.artifactdatabase.data.StateData; ingo@383: teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; ingo@624: 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: teichmann@8202: /** The log used in this class.*/ teichmann@8202: private static Logger log = Logger.getLogger(LocationSelect.class); ingo@383: ingo@383: ingo@383: public LocationSelect() { ingo@383: } 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: ingo@383: @Override ingo@679: protected Element[] createItems( ingo@679: XMLUtils.ElementCreator cr, ingo@679: Artifact artifact, ingo@679: String name, ingo@679: CallContext context) ingo@679: { ingo@921: 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]; ingo@679: } ingo@679: else { teichmann@8202: log.warn("Could not read min/max distance values!"); ingo@679: } ingo@679: felix@2235: if (name.equals(LOCATIONS)) { ingo@679: Element min = createItem( ingo@679: cr, ingo@679: new String[] {"min", new Double(minVal).toString()}); ingo@679: ingo@679: Element max = createItem( ingo@679: cr, ingo@679: 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: ingo@679: felix@3284: /** Validates data from artifact. */ ingo@679: @Override sascha@1050: public boolean validate(Artifact artifact) ingo@383: throws IllegalArgumentException ingo@383: { teichmann@8202: log.debug("LocationSelect.validate"); ingo@383: teichmann@5867: D4EArtifact flys = (D4EArtifact) artifact; felix@2235: StateData data = getData(flys, LOCATIONS); ingo@383: felix@2223: String locationStr = data != null felix@2223: ? (String) data.getValue() felix@2223: : 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: ingo@921: double[] minmax = getMinMax(artifact); ingo@383: double[] mm = extractLocations(locationStr); ingo@383: teichmann@8202: log.debug("Inserted min location: " + mm[0]); teichmann@8202: log.debug("Inserted max location: " + mm[mm.length-1]); ingo@383: ingo@385: return validateBounds(minmax[0], minmax[1], mm[0], mm[mm.length-1], 0d); ingo@383: } 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: * ingo@383: * @param locationStr The locations inserted in this state. ingo@383: * ingo@383: * @return the locations as array. ingo@383: */ ingo@383: protected double[] extractLocations(String locationStr) { ingo@383: String[] tmp = locationStr.split(" "); ingo@383: TDoubleArrayList locations = new TDoubleArrayList(); ingo@383: ingo@383: for (String l: tmp) { ingo@383: try { ingo@383: locations.add(Double.parseDouble(l)); ingo@383: } ingo@383: catch (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 :