ingo@383: package de.intevation.flys.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: ingo@383: import de.intevation.artifacts.Artifact; ingo@383: import de.intevation.artifacts.CallContext; ingo@383: ingo@679: import de.intevation.artifacts.common.utils.XMLUtils; ingo@679: ingo@383: import de.intevation.artifactdatabase.data.StateData; ingo@383: ingo@624: import de.intevation.flys.artifacts.FLYSArtifact; ingo@624: ingo@383: ingo@383: /** ingo@383: * This state is used to realize the input of multiple locations as string. The ingo@383: * 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: ingo@383: /** The logger used in this class.*/ ingo@383: private static Logger logger = Logger.getLogger(LocationSelect.class); ingo@383: ingo@383: ingo@383: /** The name of the StateData object that stores the location string.*/ ingo@383: public static final String FIELD_LOCATIONS = "ld_locations"; ingo@383: ingo@383: ingo@383: public LocationSelect() { ingo@383: } ingo@383: ingo@383: 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 { ingo@679: logger.warn("Could not read min/max distance values!"); ingo@679: } ingo@679: ingo@679: if (name.equals(FIELD_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: ingo@679: @Override sascha@1050: public boolean validate(Artifact artifact) ingo@383: throws IllegalArgumentException ingo@383: { ingo@383: logger.debug("LocationSelect.validate"); ingo@383: ingo@624: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@624: StateData data = getData(flys, FIELD_LOCATIONS); ingo@383: ingo@624: String locationStr = data != null ? (String) data.getValue() : null; ingo@383: ingo@383: if (locationStr == null || locationStr.length() == 0) { ingo@383: logger.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: ingo@383: logger.debug("Inserted min location: " + mm[0]); ingo@383: logger.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) { ingo@383: logger.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 :