ingo@2696: package de.intevation.flys.artifacts.states;
ingo@2696: 
ingo@2696: import org.w3c.dom.Element;
ingo@2696: 
ingo@2696: import org.apache.log4j.Logger;
ingo@2696: 
ingo@2696: import de.intevation.artifacts.Artifact;
ingo@2696: import de.intevation.artifacts.CallContext;
ingo@2696: 
ingo@2696: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@2696: import de.intevation.artifacts.common.model.KVP;
ingo@2696: 
ingo@2705: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@2705: import de.intevation.flys.artifacts.resources.Resources;
ingo@2705: import de.intevation.flys.utils.FLYSUtils;
ingo@2705: 
ingo@2696: 
ingo@2696: /**
ingo@2696:  * State that holds minimun and maximum (for validation).
ingo@2696:  *
ingo@2696:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@2696:  */
ingo@2696: public abstract class MultiIntArrayState extends DefaultState {
ingo@2696: 
ingo@2696:     private static final Logger logger =
ingo@2696:         Logger.getLogger(MultiIntArrayState.class);
ingo@2696: 
ingo@2696: 
ingo@2696:     @Override
ingo@2696:     protected void appendItems(
ingo@2696:         Artifact       artifact,
ingo@2696:         ElementCreator creator,
ingo@2696:         String         name,
ingo@2696:         CallContext    context,
ingo@2696:         Element        select
ingo@2696:     ) {
ingo@2696:         try {
ingo@2696:             creator.addAttr(select, "type", "intoptions", true);
ingo@2696: 
ingo@2696:             for (KVP kvp: getOptions(artifact, name)) {
ingo@2696:                 Element item = creator.create("item");
ingo@2696:                 creator.addAttr(item, "label", kvp.getValue().toString(), true);
ingo@2708:                 creator.addAttr(item, "value", kvp.getKey().toString(), true);
ingo@2696: 
ingo@2708:                 select.appendChild(item);
ingo@2696:             }
ingo@2696:         }
ingo@2696:         catch (IllegalArgumentException iae) {
ingo@2696:             logger.warn("Illegal argument", iae);
ingo@2696:         }
ingo@2696:     }
ingo@2696: 
ingo@2696: 
ingo@2705:     @Override
ingo@2705:     protected Element createStaticData(
ingo@2705:         FLYSArtifact   flys,
ingo@2705:         ElementCreator creator,
ingo@2705:         CallContext    cc,
ingo@2705:         String         name,
ingo@2705:         String         value,
ingo@2705:         String         type
ingo@2705:     ) {
ingo@2705:         Element data = creator.create("data");
ingo@2705:         creator.addAttr(data, "name",  name, true);
ingo@2705:         creator.addAttr(data, "type",  type, true);
ingo@2705:         creator.addAttr(data, "label",
ingo@2705:             Resources.getMsg(cc.getMeta(), name, name), true);
ingo@2705: 
ingo@2705:         int[] values = FLYSUtils.intArrayFromString(value);
ingo@2705: 
ingo@2705:         for (int val: values) {
ingo@2705:             try {
ingo@2705:                 Element item = creator.create("item");
ingo@2705:                 creator.addAttr(item, "value", String.valueOf(val), true);
ingo@2705:                 creator.addAttr(item, "label", getLabelFor(cc, name, val), true);
ingo@2705: 
ingo@2705:                 data.appendChild(item);
ingo@2705:             }
ingo@2705:             catch (IllegalArgumentException iae) {
ingo@2705:                 logger.warn("Cannot append item: " + val, iae);
ingo@2705:             }
ingo@2705:         }
ingo@2705: 
ingo@2705:         return data;
ingo@2705:     }
ingo@2705: 
ingo@2705: 
ingo@2696:     protected abstract KVP<Integer, String>[] getOptions(
ingo@2696:         Artifact artifact,
ingo@2696:         String   parameterName
ingo@2696:     )
ingo@2696:     throws IllegalArgumentException;
ingo@2705: 
ingo@2705: 
ingo@2705:     protected abstract String getLabelFor(
ingo@2705:         CallContext cc,
ingo@2705:         String      parameterName,
ingo@2705:         int         value)
ingo@2705:     throws IllegalArgumentException;
ingo@2696: }
sascha@3083: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :