DoRRadioButtonElement.java 8.05 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.addon.dor.DoRUtils
 *  com.adobe.xfa.Attribute
 *  com.adobe.xfa.Element
 *  com.adobe.xfa.Node
 *  com.adobe.xfa.StringAttr
 *  com.adobe.xfa.XFA
 *  com.adobe.xfa.content.Content
 *  com.adobe.xfa.content.IntegerValue
 *  com.adobe.xfa.content.TextValue
 *  com.adobe.xfa.template.Value
 *  com.adobe.xfa.template.containers.Draw
 *  com.adobe.xfa.template.containers.ExclGroup
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.addon.dor.elements;

import com.adobe.aemds.guide.addon.dor.DoRUtils;
import com.adobe.aemds.guide.addon.dor.elements.DoRElement;
import com.adobe.aemds.guide.addon.dor.elements.DoRElementUtils;
import com.adobe.xfa.Attribute;
import com.adobe.xfa.Element;
import com.adobe.xfa.Node;
import com.adobe.xfa.StringAttr;
import com.adobe.xfa.XFA;
import com.adobe.xfa.content.Content;
import com.adobe.xfa.content.IntegerValue;
import com.adobe.xfa.content.TextValue;
import com.adobe.xfa.template.Value;
import com.adobe.xfa.template.containers.Draw;
import com.adobe.xfa.template.containers.ExclGroup;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DoRRadioButtonElement
extends DoRElement {
    private static Logger logger = LoggerFactory.getLogger(DoRRadioButtonElement.class);

    public DoRRadioButtonElement(Element xfaElement) throws Exception {
        super(xfaElement);
        this.xPathExprs.put("jcr:title", "field/caption/value/text/text()");
        this.xPathExprs.put("assistPriority", "field/assist/speak/@priority");
        this.xPathExprs.put("custom", "field/assist/speak/text()");
        this.xPathExprs.put("shortDescription", "field/assist/toolTip/text()");
        this.xPathExprs.put("bindRef", "exclGroup/bind/@ref");
        this.xPathExprs.put("bindMatch", "exclGroup/bind/@match");
        this.xPathExprs.put("name", "exclGroup/@name");
    }

    @Override
    public String getCaptionValueSom() {
        return "#field.#caption.#value";
    }

    @Override
    public void applyProperties(Properties properties) throws Exception {
        this.adaptForBackwardCompatibility();
        Map map = (Map)properties.get("options");
        this.applyOptionsProperty(map);
        this.adaptToTable();
        this.applyDefaultValue(properties);
        this.applyProperties(properties, true);
        this.setWidth(this.xfaElement.getAttribute(XFA.WTAG).getAttrValue());
    }

    private void adaptForBackwardCompatibility() {
        Node titleNode = this.xfaElement.resolveNode("AF_RADIOBUTTONLISTTITLE");
        if (titleNode instanceof Draw) {
            this.xPathExprs.put("jcr:title", "draw/value/text/text()");
            this.xPathExprs.put("assistPriority", "draw/assist/speak/@priority");
            this.xPathExprs.put("custom", "draw/assist/speak/text()");
            this.xPathExprs.put("shortDescription", "draw/assist/toolTip/text()");
        }
    }

    private void adaptToTable() {
        Element parent = this.xfaElement.getXFAParent();
        try {
            if (parent.getAttribute(XFA.LAYOUTTAG).getAttrValue().equals("row")) {
                String width = DoRElementUtils.getColumnWidthFromParent(this.xfaElement);
                DoRElementUtils.adaptToTableCell(this.xfaElement, width);
                Element title = (Element)this.xfaElement.resolveNode("AF_RADIOBUTTONLISTTITLE");
                if (title != null) {
                    StringAttr attribute = new StringAttr("presence", "hidden");
                    title.setAttribute((Attribute)attribute, XFA.PRESENCETAG);
                    Node caption = title.resolveNode("#caption");
                    title.removeChild(caption);
                    Element para = (Element)title.resolveNode("#para");
                    para.setAttribute((Attribute)new StringAttr("hAlign", "center"), XFA.HALIGNTAG);
                    DoRElementUtils.adaptToTableCell(title, width);
                }
                Element exclusionGroup = (Element)this.xfaElement.resolveNode("#exclGroup");
                DoRElementUtils.adaptToTableCell(exclusionGroup, width);
                int childCount = exclusionGroup.getXFAChildCount();
                for (int i = 0; i < childCount; ++i) {
                    Element element = (Element)exclusionGroup.getXFAChild(i);
                    if (!element.getClassName().equals("field")) continue;
                    DoRElementUtils.adaptToTableCell(element, width);
                }
            }
        }
        catch (NullPointerException e) {
            logger.debug("Radio button is not inside table", (Throwable)e);
        }
    }

    private void applyDefaultValue(Properties properties) {
        String name;
        Node radioButtonNode;
        String key = "_value";
        if (properties.containsKey(key) && (radioButtonNode = this.xfaElement.resolveNode(name = "#exclGroup." + this.xfaElement.getName() + properties.getProperty(key))) != null) {
            this.xPathExprs.put(key, "exclGroup/field[@name=\"" + this.xfaElement.getName() + properties.getProperty(key) + "\"]" + "/value/text/text()");
        }
    }

    private void applyOptionsProperty(Map<String, String> map) {
        String name = this.xfaElement.getName();
        Element radioButton = (Element)this.xfaElement.resolveNode("#exclGroup.AF_RADIOBUTTON");
        LinkedList<Element> listOfChildren = new LinkedList<Element>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            IntegerValue integerValue;
            Element radioButtonElement = radioButton.clone(null, true);
            Value valueNode = (Value)radioButtonElement.resolveNode("#caption.#value");
            Node childValueNode = valueNode.getOneOfChild();
            DoRUtils.setValue((Content)((Content)childValueNode), (String)entry.getValue(), (String)null);
            TextValue itemsTextValue = (TextValue)radioButtonElement.resolveNode("#items.#text");
            if (itemsTextValue == null) {
                itemsTextValue = new TextValue((Element)radioButtonElement.resolveNode("#items"), null);
            }
            if ((integerValue = (IntegerValue)radioButtonElement.resolveNode("#items.#integer")) != null) {
                integerValue.remove();
            }
            radioButtonElement.setName(name + entry.getKey());
            itemsTextValue.setValue(entry.getKey());
            listOfChildren.add(radioButtonElement);
        }
        Element exclGroup = (Element)this.xfaElement.resolveNode("#exclGroup");
        for (int i = listOfChildren.size() - 1; i >= 0; --i) {
            exclGroup.appendChild((Node)listOfChildren.get(i));
        }
        listOfChildren.clear();
        radioButton.remove();
    }

    private void setWidth(String width) {
        Element exclusionGroup;
        Element title = (Element)this.xfaElement.resolveNode("AF_RADIOBUTTONLISTTITLE");
        if (title != null) {
            title.setAttribute((Attribute)new StringAttr("maxW", width), XFA.MAXWTAG);
        }
        if ((exclusionGroup = (Element)this.xfaElement.resolveNode("#exclGroup")) != null) {
            exclusionGroup.setAttribute((Attribute)new StringAttr("maxW", width), XFA.MAXWTAG);
            int childCount = exclusionGroup.getXFAChildCount();
            for (int i = 0; i < childCount; ++i) {
                Element element = (Element)exclusionGroup.getXFAChild(i);
                if (element == null || !element.getClassName().equals("field")) continue;
                element.setAttribute((Attribute)new StringAttr("maxW", width), XFA.MAXWTAG);
            }
        }
    }

    @Override
    protected void modifyCaptionAttributes(Element node, double scalingFactor) {
        int noOfChild = node.getXFAChildCount();
        for (int i = 0; i < noOfChild; ++i) {
            Element child = (Element)node.getXFAChild(i);
            if (child instanceof ExclGroup) {
                this.modifyCaptionAttributes(child, scalingFactor);
            }
            super.modifyCaptionAttributes(child, scalingFactor);
        }
    }
}