ingo@1038: package de.intevation.gnv.state;
ingo@1038:
ingo@1038: import de.intevation.artifactdatabase.XMLUtils;
ingo@1038:
ingo@1038: import de.intevation.artifacts.CallContext;
ingo@1038: import de.intevation.artifacts.CallMeta;
ingo@1038: import de.intevation.artifacts.PreferredLocale;
ingo@1038:
ingo@1038: import de.intevation.gnv.artifacts.ressource.RessourceFactory;
ingo@1038:
ingo@1038: import org.apache.log4j.Logger;
ingo@1038:
ingo@1038: import org.w3c.dom.Document;
ingo@1038: import org.w3c.dom.Element;
ingo@1038: import org.w3c.dom.Node;
ingo@1038:
ingo@1038:
ingo@1038: /**
ingo@1038: * This task of this State
implementation is to switch between
ingo@1038: * working with vector or scalar data.
ingo@1038: *
ingo@1038: * @author Ingo Weinzierl
ingo@1038: */
ingo@1038: public class SwitchModeState extends StateBase {
ingo@1038:
ingo@1038: public static final String VECTOR_VALUE = "vector";
ingo@1038: public static final String SCALAR_VALUE = "scalar";
ingo@1038:
ingo@1038: public static final String RESSOURCE_VECTOR = "vectorvalues";
ingo@1038: public static final String RESSOURCE_SCALAR = "scalarvalues";
ingo@1038:
ingo@1038: private static Logger logger = Logger.getLogger(SwitchModeState.class);
ingo@1038:
ingo@1038: public SwitchModeState() {
ingo@1038: }
ingo@1038:
ingo@1038:
ingo@1038: @Override
ingo@1038: protected void describeDynamic(
ingo@1038: XMLUtils.ElementCreator artCreator,
ingo@1038: XMLUtils.ElementCreator creator,
ingo@1038: Document document,
ingo@1038: Node dynamic,
ingo@1038: CallContext context,
ingo@1038: String uuid)
ingo@1038: {
ingo@1038: RessourceFactory ressource = RessourceFactory.getInstance();
ingo@1038: CallMeta callMeta = context.getMeta();
ingo@1038: PreferredLocale[] locales = callMeta.getLanguages();
ingo@1038:
ingo@1038: Element selectNode = creator.create("select1");
ingo@1038: creator.addAttr(selectNode, "ref", dataName);
ingo@1038:
ingo@1038: Element labelNode = creator.create("label");
ingo@1038: labelNode.setTextContent(ressource.getRessource(
ingo@1038: locales, dataName, dataName));
ingo@1038: selectNode.appendChild(labelNode);
ingo@1038:
ingo@1038: selectNode.appendChild(createChoices(creator, context));
ingo@1038: dynamic.appendChild(selectNode);
ingo@1038: logger.debug("creating dynamic ui elements finished");
ingo@1038: }
ingo@1038:
ingo@1038:
ingo@1038: protected Node createChoices(
ingo@1038: XMLUtils.ElementCreator creator,
ingo@1038: CallContext context)
ingo@1038: {
ingo@1038: RessourceFactory ressource = RessourceFactory.getInstance();
ingo@1038: CallMeta callMeta = context.getMeta();
ingo@1038: PreferredLocale[] locales = callMeta.getLanguages();
ingo@1038:
ingo@1038: Element choiceNodes = creator.create("choices");
ingo@1038:
ingo@1038: // add choice for scalar values
ingo@1038: logger.debug("create choice for scalar values");
ingo@1038: Element scalar = creator.create("item");
ingo@1038: Element label = creator.create("label");
ingo@1038: label.setTextContent(ressource.getRessource(
ingo@1038: locales, RESSOURCE_SCALAR, RESSOURCE_SCALAR));
ingo@1038: scalar.appendChild(label);
ingo@1038:
ingo@1038: Element value = creator.create("value");
ingo@1038: value.setTextContent(SCALAR_VALUE);
ingo@1038: scalar.appendChild(value);
ingo@1038:
ingo@1038: // add choice for vector values
ingo@1038: logger.debug("create choice for vector values");
ingo@1038: Element vector = creator.create("item");
ingo@1038: label = creator.create("label");
ingo@1038: label.setTextContent(ressource.getRessource(
ingo@1038: locales, RESSOURCE_VECTOR, RESSOURCE_VECTOR));
ingo@1038: vector.appendChild(label);
ingo@1038:
ingo@1038: value = creator.create("value");
ingo@1038: value.setTextContent(VECTOR_VALUE);
ingo@1038: vector.appendChild(value);
ingo@1038:
ingo@1038: choiceNodes.appendChild(scalar);
ingo@1038: choiceNodes.appendChild(vector);
ingo@1038:
ingo@1038: return choiceNodes;
ingo@1038: }
ingo@1038:
ingo@1038:
ingo@1038: @Override
ingo@1038: protected String[] getDescriptionForInputData(
ingo@1038: InputData data, CallContext context, String uuid)
ingo@1038: {
ingo@1038: RessourceFactory ressource = RessourceFactory.getInstance();
ingo@1038: CallMeta callMeta = context.getMeta();
ingo@1038: PreferredLocale[] locales = callMeta.getLanguages();
ingo@1038:
ingo@1038: String value = data.getValue();
ingo@1038:
ingo@1038: if (value != null && value.equals(SCALAR_VALUE)) {
ingo@1038: return new String[] { ressource.getRessource(
ingo@1038: locales, RESSOURCE_SCALAR, RESSOURCE_SCALAR) };
ingo@1038: }
ingo@1038: else if (value != null && value.equals(VECTOR_VALUE)) {
ingo@1038: return new String[] { ressource.getRessource(
ingo@1038: locales, RESSOURCE_VECTOR, RESSOURCE_VECTOR) };
ingo@1038: }
ingo@1038:
ingo@1038: return null;
ingo@1038: }
ingo@1038: }
ingo@1038: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :