DoRCheckBoxElement.java 7.8 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.template.Value
 *  com.adobe.xfa.template.containers.Draw
 *  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.template.Value;
import com.adobe.xfa.template.containers.Draw;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DoRCheckBoxElement
extends DoRElement {
    private static Logger logger = LoggerFactory.getLogger(DoRCheckBoxElement.class);
    protected String CHECKBOXTITLE_NAME;
    protected String CHECKBUTTON_NAME;

    public DoRCheckBoxElement(Element xfaElement) throws Exception {
        super(xfaElement);
        this.initNameIdentifiers();
        this.xPathExprs.put("jcr:title", "field[@name=\"" + this.CHECKBOXTITLE_NAME + "\"]/caption/value/text/text()");
        this.xPathExprs.put("assistPriority", "field[@name=\"" + this.CHECKBOXTITLE_NAME + "\"]/assist/speak/@priority");
        this.xPathExprs.put("custom", "field[@name=\"" + this.CHECKBOXTITLE_NAME + "\"]/assist/speak/text()");
        this.xPathExprs.put("shortDescription", "field[@name=\"" + this.CHECKBOXTITLE_NAME + "\"]/assist/toolTip/text()");
        this.xPathExprs.put("bindRef", "field[@name=\"HiddenTextField\"]/bind/@ref");
        this.xPathExprs.put("bindMatch", "field[@name=\"HiddenTextField\"]/bind/@match");
    }

    protected void initNameIdentifiers() {
        this.CHECKBOXTITLE_NAME = "AF_CHECKBOXTITLE";
        this.CHECKBUTTON_NAME = "AF_CHECKBUTTON";
    }

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

    @Override
    public void applyProperties(Properties properties) throws Exception {
        Node hiddenTextField;
        this.adaptForBackwardCompatibility();
        Map map = (Map)properties.get("options");
        this.applyOptionsProperty(map);
        this.adaptToTable();
        this.applyDefaultValues(properties);
        this.applyProperties(properties, true);
        String name = properties.getProperty("name");
        if (name != null && !name.isEmpty() && (hiddenTextField = this.xfaElement.resolveNode("HiddenTextField")) != null) {
            hiddenTextField.setName(name);
        }
        this.setWidth(this.xfaElement.getAttribute(XFA.WTAG).getAttrValue());
    }

    private void adaptForBackwardCompatibility() {
        Node titleNode = this.xfaElement.resolveNode(this.CHECKBOXTITLE_NAME);
        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()");
            this.xPathExprs.put("bindRef", "field[ui[textEdit]]/bind/@ref");
            this.xPathExprs.put("bindMatch", "field[ui[textEdit]]/bind/@match");
        }
    }

    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(this.CHECKBOXTITLE_NAME);
                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);
                }
                int childCount = this.xfaElement.getXFAChildCount();
                for (int i = 0; i < childCount; ++i) {
                    Element element = (Element)this.xfaElement.getXFAChild(i);
                    if (!element.getClassName().equals("field")) continue;
                    DoRElementUtils.adaptToTableCell(element, width);
                }
            }
        }
        catch (NullPointerException e) {
            logger.debug("Check box is not inside table", (Throwable)e);
        }
    }

    private void applyDefaultValues(Properties properties) {
        String key = "_value";
        if (properties.containsKey(key)) {
            String name = this.xfaElement.getName();
            String defaultValue = properties.getProperty("_value");
            StringTokenizer tokenizer = new StringTokenizer(defaultValue, ",");
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                if (this.xfaElement.resolveNode(name + token) == null) continue;
                this.xPathExprs.put(token, "field[@name=\"" + name + token + "\"" + "]" + "/" + "value/integer/text()");
                properties.put(token, "1");
            }
            properties.remove("_value");
        }
    }

    protected void applyOptionsProperty(Map<String, String> map) {
        String name = this.xfaElement.getName();
        Element checkButton = (Element)this.xfaElement.resolveNode(this.CHECKBUTTON_NAME);
        LinkedList<Element> listOfChildren = new LinkedList<Element>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            Element checkButtonElement = checkButton.clone(null, true);
            Value valueNode = (Value)checkButtonElement.resolveNode("#caption.#value");
            Node childNode = valueNode.getOneOfChild();
            DoRUtils.setValue((Content)((Content)childNode), (String)entry.getValue(), (String)null);
            checkButtonElement.setName(name + entry.getKey());
            listOfChildren.add(checkButtonElement);
        }
        for (int i = listOfChildren.size() - 1; i >= 0; --i) {
            this.xfaElement.appendChild((Node)listOfChildren.get(i));
        }
        listOfChildren.clear();
        checkButton.remove();
    }

    private void setWidth(String width) {
        Element title = (Element)this.xfaElement.resolveNode("AF_CHECKBOXTITLE");
        if (title != null) {
            title.setAttribute((Attribute)new StringAttr("maxW", width), XFA.MAXWTAG);
        }
        int childCount = this.xfaElement.getXFAChildCount();
        for (int i = 0; i < childCount; ++i) {
            Element element = (Element)this.xfaElement.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);
            super.modifyCaptionAttributes(child, scalingFactor);
        }
    }
}