ingo@383: package de.intevation.flys.artifacts.states;
ingo@383:
ingo@383: import java.util.Map;
ingo@383:
ingo@383: import gnu.trove.TDoubleArrayList;
ingo@383:
ingo@383: import org.apache.log4j.Logger;
ingo@383:
ingo@383: import de.intevation.artifacts.Artifact;
ingo@383: import de.intevation.artifacts.CallContext;
ingo@383:
ingo@383: import de.intevation.artifactdatabase.data.StateData;
ingo@383:
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@383: public boolean validate(Artifact artifact, CallContext context)
ingo@383: throws IllegalArgumentException
ingo@383: {
ingo@383: logger.debug("LocationSelect.validate");
ingo@383:
ingo@383: Map data = getData();
ingo@383:
ingo@383: String locationStr = (String) data.get(FIELD_LOCATIONS).getValue();
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@383: double[] minmax = getMinMaxDistance(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 :