Search in sources :

Example 11 with GroupDef

use of org.javarosa.core.model.GroupDef in project collect by opendatakit.

the class FormController method stepOverGroup.

/**
 * If using a view like HierarchyView that doesn't support multi-question per screen, step over
 * the group represented by the FormIndex.
 */
private int stepOverGroup() {
    GroupDef gd = (GroupDef) formEntryController.getModel().getForm().getChild(getFormIndex());
    List<FormIndex> indices = getIndicesForGroup(gd);
    // jump to the end of the group
    formEntryController.jumpToIndex(indices.get(indices.size() - 1));
    return stepToNextEvent(STEP_OVER_GROUP);
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) GroupDef(org.javarosa.core.model.GroupDef)

Example 12 with GroupDef

use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.

the class XFormParser method collapseRepeatGroups.

/**
 * Collapses groups whose only child is a repeat into a single repeat that uses the label of the wrapping group
 */
private static void collapseRepeatGroups(IFormElement fe) {
    if (fe.getChildren() == null)
        return;
    for (int i = 0; i < fe.getChildren().size(); i++) {
        IFormElement child = fe.getChild(i);
        GroupDef group = null;
        if (child instanceof GroupDef)
            group = (GroupDef) child;
        if (group != null) {
            if (!group.getRepeat() && group.getChildren().size() == 1) {
                IFormElement grandchild = group.getChildren().get(0);
                GroupDef repeat = null;
                if (grandchild instanceof GroupDef)
                    repeat = (GroupDef) grandchild;
                if (repeat != null && repeat.getRepeat()) {
                    // collapse the wrapping group
                    // merge group into repeat
                    // id - later
                    // name - later
                    repeat.setLabelInnerText(group.getLabelInnerText());
                    repeat.setTextID(group.getTextID());
                    // don't merge binding; repeat will always already have one
                    // replace group with repeat
                    fe.getChildren().set(i, repeat);
                    group = repeat;
                }
            }
            collapseRepeatGroups(group);
        }
    }
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) GroupDef(org.javarosa.core.model.GroupDef)

Example 13 with GroupDef

use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.

the class XFormParser method parseGroup.

private void parseGroup(IFormElement parent, Element e, int groupType) {
    GroupDef group = new GroupDef();
    // until we come up with a better scheme
    group.setID(serialQuestionID++);
    IDataReference dataRef = null;
    boolean refFromBind = false;
    List<String> usedAtts = new ArrayList<>();
    usedAtts.add(REF_ATTR);
    usedAtts.add(NODESET_ATTR);
    usedAtts.add(BIND_ATTR);
    usedAtts.add(APPEARANCE_ATTR);
    usedAtts.add("count");
    usedAtts.add("noAddRemove");
    if (groupType == CONTAINER_REPEAT) {
        group.setRepeat(true);
    }
    String ref = e.getAttributeValue(null, REF_ATTR);
    String nodeset = e.getAttributeValue(null, NODESET_ATTR);
    String bind = e.getAttributeValue(null, BIND_ATTR);
    group.setAppearanceAttr(e.getAttributeValue(null, APPEARANCE_ATTR));
    if (bind != null) {
        DataBinding binding = bindingsByID.get(bind);
        if (binding == null) {
            throw new XFormParseException("XForm Parse: invalid binding ID [" + bind + "]", e);
        }
        dataRef = binding.getReference();
        refFromBind = true;
    } else {
        if (group.getRepeat()) {
            if (nodeset != null) {
                dataRef = new XPathReference(nodeset);
            } else {
                throw new XFormParseException("XForm Parse: <repeat> with no binding ('bind' or 'nodeset')", e);
            }
        } else {
            if (ref != null) {
                dataRef = new XPathReference(ref);
            } else if (nodeset != null) {
                dataRef = new XPathReference(nodeset);
            }
        // <group> not required to have a binding so no exception thrown
        }
    }
    if (!refFromBind) {
        dataRef = getAbsRef(dataRef, parent);
    }
    group.setBind(dataRef);
    if (group.getRepeat()) {
        repeats.add((TreeReference) dataRef.getReference());
        String countRef = e.getAttributeValue(NAMESPACE_JAVAROSA, "count");
        if (countRef != null) {
            group.count = getAbsRef(new XPathReference(countRef), parent);
            group.noAddRemove = true;
        } else {
            group.noAddRemove = (e.getAttributeValue(NAMESPACE_JAVAROSA, "noAddRemove") != null);
        }
    }
    for (int i = 0; i < e.getChildCount(); i++) {
        int type = e.getType(i);
        Element child = (type == Node.ELEMENT ? e.getElement(i) : null);
        String childName = (child != null ? child.getName() : null);
        String childNamespace = (child != null ? child.getNamespace() : null);
        if (group.getRepeat() && NAMESPACE_JAVAROSA.equals(childNamespace)) {
            if ("chooseCaption".equals(childName)) {
                group.chooseCaption = getLabel(child);
            } else if ("addCaption".equals(childName)) {
                group.addCaption = getLabel(child);
            } else if ("delCaption".equals(childName)) {
                group.delCaption = getLabel(child);
            } else if ("doneCaption".equals(childName)) {
                group.doneCaption = getLabel(child);
            } else if ("addEmptyCaption".equals(childName)) {
                group.addEmptyCaption = getLabel(child);
            } else if ("doneEmptyCaption".equals(childName)) {
                group.doneEmptyCaption = getLabel(child);
            } else if ("entryHeader".equals(childName)) {
                group.entryHeader = getLabel(child);
            } else if ("delHeader".equals(childName)) {
                group.delHeader = getLabel(child);
            } else if ("mainHeader".equals(childName)) {
                group.mainHeader = getLabel(child);
            }
        }
    }
    for (int i = 0; i < e.getChildCount(); i++) {
        if (e.getType(i) == Element.ELEMENT) {
            parseElement(e.getElement(i), group, groupLevelHandlers);
        }
    }
    // save all the unused attributes verbatim...
    for (int i = 0; i < e.getAttributeCount(); i++) {
        String name = e.getAttributeName(i);
        if (usedAtts.contains(name))
            continue;
        group.setAdditionalAttribute(e.getAttributeNamespace(i), name, e.getAttributeValue(i));
    }
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
    parent.addChild(group);
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) DataBinding(org.javarosa.core.model.DataBinding) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Element(org.kxml2.kdom.Element) IFormElement(org.javarosa.core.model.IFormElement) ArrayList(java.util.ArrayList) XPathReference(org.javarosa.model.xform.XPathReference) GroupDef(org.javarosa.core.model.GroupDef)

Example 14 with GroupDef

use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.

the class FormInstanceParser method verifyControlBindings.

private void verifyControlBindings(IFormElement fe, FormInstance instance, List<String> errors) {
    // throws XmlPullParserException {
    if (fe.getChildren() == null)
        return;
    for (int i = 0; i < fe.getChildren().size(); i++) {
        IFormElement child = fe.getChildren().get(i);
        IDataReference ref = null;
        String type = null;
        if (child instanceof GroupDef) {
            ref = child.getBind();
            type = (((GroupDef) child).getRepeat() ? "Repeat" : "Group");
        } else if (child instanceof QuestionDef) {
            ref = child.getBind();
            type = "Question";
        }
        TreeReference tref = FormInstance.unpackReference(ref);
        if (child instanceof QuestionDef && tref.size() == 0) {
            // group can bind to '/'; repeat can't, but that's checked above
            reporter.warning(XFormParserReporter.TYPE_INVALID_STRUCTURE, "Cannot bind control to '/'", null);
        } else {
            List<TreeReference> nodes = new EvaluationContext(instance).expandReference(tref, true);
            if (nodes.size() == 0) {
                String error = type + " bound to non-existent node: [" + tref.toString() + "]";
                reporter.error(error);
                errors.add(error);
            }
        // we can't check whether questions map to the right kind of node ('data' node vs. 'sub-tree' node) as that depends
        // on the question's data type, which we don't know yet
        }
        verifyControlBindings(child, instance, errors);
    }
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) IDataReference(org.javarosa.core.model.IDataReference) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) QuestionDef(org.javarosa.core.model.QuestionDef) Constraint(org.javarosa.core.model.condition.Constraint) GroupDef(org.javarosa.core.model.GroupDef)

Example 15 with GroupDef

use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.

the class FormEntryCaption method getRepeatText.

// TODO: this is explicitly missing integration with the new multi-media support
// TODO: localize the default captions
public String getRepeatText(String typeKey) {
    GroupDef g = (GroupDef) element;
    if (!g.getRepeat()) {
        throw new RuntimeException("not a repeat");
    }
    String title = getLongText();
    int count = getNumRepetitions();
    String caption = null;
    if ("mainheader".equals(typeKey)) {
        caption = g.mainHeader;
        if (caption == null) {
            return title;
        }
    } else if ("add".equals(typeKey)) {
        caption = g.addCaption;
        if (caption == null) {
            return "Add another " + title;
        }
    } else if ("add-empty".equals(typeKey)) {
        caption = g.addEmptyCaption;
        if (caption == null) {
            caption = g.addCaption;
        }
        if (caption == null) {
            return "None - Add " + title;
        }
    } else if ("del".equals(typeKey)) {
        caption = g.delCaption;
        if (caption == null) {
            return "Delete " + title;
        }
    } else if ("done".equals(typeKey)) {
        caption = g.doneCaption;
        if (caption == null) {
            return "Done";
        }
    } else if ("done-empty".equals(typeKey)) {
        caption = g.doneEmptyCaption;
        if (caption == null) {
            caption = g.doneCaption;
        }
        if (caption == null) {
            return "Skip";
        }
    } else if ("delheader".equals(typeKey)) {
        caption = g.delHeader;
        if (caption == null) {
            return "Delete which " + title + "?";
        }
    }
    HashMap<String, Object> vars = new HashMap<String, Object>();
    vars.put("name", title);
    vars.put("n", Integer.valueOf(count));
    return form.fillTemplateString(caption, index.getReference(), vars);
}
Also used : HashMap(java.util.HashMap) GroupDef(org.javarosa.core.model.GroupDef)

Aggregations

GroupDef (org.javarosa.core.model.GroupDef)22 IFormElement (org.javarosa.core.model.IFormElement)15 ArrayList (java.util.ArrayList)6 FormIndex (org.javarosa.core.model.FormIndex)6 QuestionDef (org.javarosa.core.model.QuestionDef)5 TreeReference (org.javarosa.core.model.instance.TreeReference)5 TreeElement (org.javarosa.core.model.instance.TreeElement)4 IDataReference (org.javarosa.core.model.IDataReference)3 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)3 HashMap (java.util.HashMap)2 FormDef (org.javarosa.core.model.FormDef)2 Constraint (org.javarosa.core.model.condition.Constraint)2 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)2 XPathReference (org.javarosa.model.xform.XPathReference)2 DataBinding (org.javarosa.core.model.DataBinding)1 SelectChoice (org.javarosa.core.model.SelectChoice)1 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 InvalidReferenceException (org.javarosa.core.model.instance.InvalidReferenceException)1