Search in sources :

Example 1 with RangeQuestion

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

the class RangeWidget method setUpWidgetParameters.

private void setUpWidgetParameters() {
    RangeQuestion rangeQuestion = (RangeQuestion) getFormEntryPrompt().getQuestion();
    rangeStart = rangeQuestion.getRangeStart();
    rangeEnd = rangeQuestion.getRangeEnd();
    rangeStep = rangeQuestion.getRangeStep().abs();
}
Also used : RangeQuestion(org.javarosa.core.model.RangeQuestion)

Example 2 with RangeQuestion

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

the class XFormParserTest method parsesRangeForm.

@Test
public void parsesRangeForm() throws IOException {
    FormDef formDef = parse(r("range-form.xml")).formDef;
    RangeQuestion question = (RangeQuestion) formDef.getChild(0);
    assertEquals(CONTROL_RANGE, question.getControlType());
    assertEquals(-2.0d, question.getRangeStart().doubleValue(), 0);
    assertEquals(2.0d, question.getRangeEnd().doubleValue(), 0);
    assertEquals(0.5d, question.getRangeStep().doubleValue(), 0);
}
Also used : FormDef(org.javarosa.core.model.FormDef) RangeQuestion(org.javarosa.core.model.RangeQuestion) Test(org.junit.Test)

Example 3 with RangeQuestion

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

the class XFormParser method parseControl.

/**
 * Parses a form control element into a {@link org.javarosa.core.model.QuestionDef} and attaches it to its parent.
 *
 * @param parent                the form control element's parent
 * @param e                     the form control element to parse
 * @param controlType           one of the control types defined in {@link org.javarosa.core.model.Constants}
 * @param additionalUsedAtts    attributes specific to the control type
 * @param passedThroughAtts     attributes specific to the control type that should be passed through to
 *                              additionalAttributes for historical reasons
 * @return                      a {@link org.javarosa.core.model.QuestionDef} representing the form control element
 */
private QuestionDef parseControl(IFormElement parent, Element e, int controlType, List<String> additionalUsedAtts, List<String> passedThroughAtts) {
    final QuestionDef question = questionForControlType(controlType);
    // until we come up with a better scheme
    question.setID(serialQuestionID++);
    final List<String> usedAtts = new ArrayList<>(Arrays.asList(REF_ATTR, BIND_ATTR, APPEARANCE_ATTR));
    if (additionalUsedAtts != null) {
        usedAtts.addAll(additionalUsedAtts);
    }
    IDataReference dataRef = null;
    boolean refFromBind = false;
    String ref = e.getAttributeValue(null, REF_ATTR);
    String bind = e.getAttributeValue(null, BIND_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 (ref != null) {
        try {
            dataRef = new XPathReference(ref);
        } catch (RuntimeException el) {
            Std.out.println(XFormParser.getVagueLocation(e));
            throw el;
        }
    } else {
        // noinspection StatementWithEmptyBody
        if (controlType == Constants.CONTROL_TRIGGER) {
        // TODO: special handling for triggers? also, not all triggers created equal
        } else {
            throw new XFormParseException("XForm Parse: input control with neither 'ref' nor 'bind'", e);
        }
    }
    if (dataRef != null) {
        if (!refFromBind) {
            dataRef = getAbsRef(dataRef, parent);
        }
        question.setBind(dataRef);
        if (controlType == Constants.CONTROL_SELECT_ONE) {
            selectOnes.add((TreeReference) dataRef.getReference());
        } else if (controlType == Constants.CONTROL_SELECT_MULTI) {
            selectMultis.add((TreeReference) dataRef.getReference());
        }
    }
    boolean isSelect = (controlType == Constants.CONTROL_SELECT_MULTI || controlType == Constants.CONTROL_SELECT_ONE);
    question.setControlType(controlType);
    question.setAppearanceAttr(e.getAttributeValue(null, APPEARANCE_ATTR));
    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);
        if (LABEL_ELEMENT.equals(childName)) {
            parseQuestionLabel(question, child);
        } else if ("hint".equals(childName)) {
            parseHint(question, child);
        } else if (isSelect && "item".equals(childName)) {
            parseItem(question, child);
        } else if (isSelect && "itemset".equals(childName)) {
            parseItemset(question, child, parent);
        }
    }
    if (isSelect) {
        if (question.getNumChoices() > 0 && question.getDynamicChoices() != null) {
            throw new XFormParseException("Select question contains both literal choices and <itemset>");
        } else if (question.getNumChoices() == 0 && question.getDynamicChoices() == null) {
            throw new XFormParseException("Select question has no choices");
        }
    }
    if (question instanceof RangeQuestion) {
        populateQuestionWithRangeAttributes((RangeQuestion) question, e);
    }
    parent.addChild(question);
    processAdditionalAttributes(question, e, usedAtts, passedThroughAtts);
    return question;
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) 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) TreeReference(org.javarosa.core.model.instance.TreeReference) RangeQuestion(org.javarosa.core.model.RangeQuestion) DataBinding(org.javarosa.core.model.DataBinding) QuestionDef(org.javarosa.core.model.QuestionDef)

Aggregations

RangeQuestion (org.javarosa.core.model.RangeQuestion)3 ArrayList (java.util.ArrayList)1 DataBinding (org.javarosa.core.model.DataBinding)1 FormDef (org.javarosa.core.model.FormDef)1 IDataReference (org.javarosa.core.model.IDataReference)1 IFormElement (org.javarosa.core.model.IFormElement)1 QuestionDef (org.javarosa.core.model.QuestionDef)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 TreeElement (org.javarosa.core.model.instance.TreeElement)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 XPathReference (org.javarosa.model.xform.XPathReference)1 Test (org.junit.Test)1 Element (org.kxml2.kdom.Element)1