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();
}
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);
}
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;
}
Aggregations