use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.
the class XFormAnswerDataParser method getSelection.
private static Selection getSelection(String choiceValue, QuestionDef q) {
Selection s;
if (q == null || q.getDynamicChoices() != null) {
s = new Selection(choiceValue);
} else {
SelectChoice choice = q.getChoiceForValue(choiceValue);
s = (choice != null ? choice.selection() : null);
}
return s;
}
use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.
the class FormEntryPrompt method getAnswerValue.
// note: code overlap with FormDef.copyItemsetAnswer
public IAnswerData getAnswerValue() {
QuestionDef q = getQuestion();
ItemsetBinding itemset = q.getDynamicChoices();
if (itemset != null) {
if (itemset.valueRef != null) {
List<SelectChoice> choices = getSelectChoices();
List<String> preselectedValues = new ArrayList<String>();
// determine which selections are already present in the answer
if (itemset.copyMode) {
TreeReference destRef = itemset.getDestRef().contextualize(mTreeElement.getRef());
List<TreeReference> subNodes = form.getEvaluationContext().expandReference(destRef);
for (int i = 0; i < subNodes.size(); i++) {
TreeElement node = form.getMainInstance().resolveReference(subNodes.get(i));
String value = itemset.getRelativeValue().evalReadable(form.getMainInstance(), new EvaluationContext(form.getEvaluationContext(), node.getRef()));
preselectedValues.add(value);
}
} else {
List<Selection> sels;
IAnswerData data = mTreeElement.getValue();
if (data instanceof SelectMultiData) {
sels = (List<Selection>) data.getValue();
} else if (data instanceof SelectOneData) {
sels = new ArrayList<Selection>(1);
sels.add((Selection) data.getValue());
} else {
sels = new ArrayList<Selection>(0);
}
for (int i = 0; i < sels.size(); i++) {
preselectedValues.add(sels.get(i).xmlValue);
}
}
// populate 'selection' with the corresponding choices (matching 'value') from the dynamic choiceset
List<Selection> selection = new ArrayList<Selection>();
for (int i = 0; i < preselectedValues.size(); i++) {
String value = preselectedValues.get(i);
SelectChoice choice = null;
for (int j = 0; j < choices.size(); j++) {
SelectChoice ch = choices.get(j);
if (value.equals(ch.getValue())) {
choice = ch;
break;
}
}
// will no longer be an option this go around
if (choice != null) {
selection.add(choice.selection());
}
}
// convert to IAnswerData
if (selection.size() == 0) {
return null;
} else if (q.getControlType() == Constants.CONTROL_SELECT_MULTI) {
return new SelectMultiData(selection);
} else if (q.getControlType() == Constants.CONTROL_SELECT_ONE) {
// do something if more than one selected?
return new SelectOneData(selection.get(0));
} else {
throw new RuntimeException("can't happen");
}
} else {
// cannot map up selections without <value>
return null;
}
} else {
// static choices
return mTreeElement.getValue();
}
}
use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.
the class SelectMultiDataTests method setUp.
/* (non-Javadoc)
* @see j2meunit.framework.TestCase#setUp()
*/
public void setUp() throws Exception {
super.setUp();
question = new QuestionDef();
for (int i = 0; i < 4; i++) {
question.addSelectChoice(new SelectChoice("", "Selection" + i, "Selection " + i, false));
}
one = new Selection("Selection 1");
one.attachChoice(question);
two = new Selection("Selection 2");
two.attachChoice(question);
three = new Selection("Selection 3");
three.attachChoice(question);
firstTwo = new ArrayList<Selection>();
firstTwo.add(one);
firstTwo.add(two);
lastTwo = new ArrayList<Selection>();
lastTwo.add(two);
lastTwo.add(three);
invalid = new ArrayList<Object>();
invalid.add(three);
invalid.add(new Integer(12));
invalid.add(one);
}
use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.
the class FormParseInit method printStuff.
/*
* Makes an 'extremely basic' print out of the xform model.
*/
public String printStuff() {
String stuff = "";
// go to the beginning of the form
fec.jumpToIndex(FormIndex.createBeginningOfFormIndex());
do {
FormEntryCaption fep = femodel.getCaptionPrompt();
boolean choiceFlag = false;
if (fep.getFormElement() instanceof QuestionDef) {
stuff += "\t[Type:QuestionDef, ";
List<SelectChoice> s = ((QuestionDef) fep.getFormElement()).getChoices();
stuff += "ContainsChoices: " + ((s != null && s.size() > 0) ? "true " : "false") + ", ";
if (s != null && s.size() > 0)
choiceFlag = true;
} else if (fep.getFormElement() instanceof FormDef) {
stuff += "\t[Type:FormDef, ";
} else if (fep.getFormElement() instanceof GroupDef) {
stuff += "\t[Type:GroupDef, ";
} else {
stuff += "\t[Type:Unknown]\n";
continue;
}
stuff += "ID:" + fep.getFormElement().getID() + ", TextID:" + fep.getFormElement().getTextID() + ",InnerText:" + fep.getFormElement().getLabelInnerText();
if (choiceFlag) {
stuff += "] \n\t\t---Choices:" + ((QuestionDef) fep.getFormElement()).getChoices().toString() + "\n";
} else {
stuff += "]\n";
}
} while (fec.stepToNextEvent() != fec.EVENT_END_OF_FORM);
return stuff;
}
Aggregations