use of org.javarosa.core.model.data.SelectMultiData in project javarosa by opendatakit.
the class FormDef method attachControlsToInstanceData.
private void attachControlsToInstanceData(TreeElement node) {
for (int i = 0; i < node.getNumChildren(); i++) {
attachControlsToInstanceData(node.getChildAt(i));
}
IAnswerData val = node.getValue();
List<Selection> selections = null;
if (val instanceof SelectOneData) {
selections = new ArrayList<Selection>();
selections.add((Selection) val.getValue());
} else if (val instanceof SelectMultiData) {
selections = (List<Selection>) val.getValue();
}
if (selections != null) {
QuestionDef q = findQuestionByRef(node.getRef(), this);
if (q == null) {
throw new RuntimeException("FormDef.attachControlsToInstanceData: can't find question to link");
}
if (q.getDynamicChoices() != null) {
// droos: i think we should do something like initializing the
// itemset here, so that default answers
// can be linked to the selectchoices. however, there are
// complications. for example, the itemset might
// not be ready to be evaluated at form initialization; it may
// require certain questions to be answered
// first. e.g., if we evaluate an itemset and it has no choices, the
// xform engine will throw an error
// itemset TODO
}
for (Selection s : selections) {
s.attachChoice(q);
}
}
}
use of org.javarosa.core.model.data.SelectMultiData in project collect by opendatakit.
the class FormEntryPromptUtils method getAnswerText.
public static String getAnswerText(FormEntryPrompt fep, Context context, FormController formController) {
IAnswerData data = fep.getAnswerValue();
final String appearance = fep.getQuestion().getAppearanceAttr();
if (data instanceof SelectMultiData) {
StringBuilder b = new StringBuilder();
String sep = "";
for (Selection value : (List<Selection>) data.getValue()) {
b.append(sep);
sep = ", ";
b.append(fep.getSelectItemText(value));
}
return b.toString();
}
if (data instanceof DateTimeData) {
return DateTimeUtils.getDateTimeLabel((Date) data.getValue(), DateTimeUtils.getDatePickerDetails(appearance), true, context);
}
if (data instanceof DateData) {
return DateTimeUtils.getDateTimeLabel((Date) data.getValue(), DateTimeUtils.getDatePickerDetails(appearance), false, context);
}
if (data != null && appearance != null && appearance.contains("thousands-sep")) {
try {
final BigDecimal answerAsDecimal = new BigDecimal(fep.getAnswerText());
DecimalFormat df = new DecimalFormat();
df.setGroupingSize(3);
df.setGroupingUsed(true);
df.setMaximumFractionDigits(Integer.MAX_VALUE);
// Use . as decimal marker for consistency with DecimalWidget
DecimalFormatSymbols customFormat = new DecimalFormatSymbols();
customFormat.setDecimalSeparator('.');
if (df.getDecimalFormatSymbols().getGroupingSeparator() == '.') {
customFormat.setGroupingSeparator(' ');
}
df.setDecimalFormatSymbols(customFormat);
return df.format(answerAsDecimal);
} catch (NumberFormatException e) {
return fep.getAnswerText();
}
}
if (data != null && data.getValue() != null && fep.getDataType() == DATATYPE_TEXT && fep.getQuestion().getAdditionalAttribute(null, "query") != null) {
// ItemsetWidget
return new ItemsetDao().getItemLabel(fep.getAnswerValue().getDisplayText(), formController.getMediaFolder().getAbsolutePath(), formController.getLanguage());
}
return fep.getAnswerText();
}
use of org.javarosa.core.model.data.SelectMultiData in project collect by opendatakit.
the class GridMultiWidget method getAnswer.
@Override
public IAnswerData getAnswer() {
List<Selection> vc = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
if (selected[i]) {
SelectChoice sc = items.get(i);
vc.add(new Selection(sc));
}
}
if (vc.size() == 0) {
return null;
} else {
return new SelectMultiData(vc);
}
}
use of org.javarosa.core.model.data.SelectMultiData in project collect by opendatakit.
the class SpinnerMultiWidget method getAnswer.
@Override
public IAnswerData getAnswer() {
clearFocus();
List<Selection> vc = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
if (selections[i]) {
SelectChoice sc = items.get(i);
vc.add(new Selection(sc));
}
}
if (vc.size() == 0) {
return null;
} else {
return new SelectMultiData(vc);
}
}
use of org.javarosa.core.model.data.SelectMultiData in project collect by opendatakit.
the class ExternalAnswerResolver method resolveAnswer.
@Override
public IAnswerData resolveAnswer(String textVal, TreeElement treeElement, FormDef formDef) {
QuestionDef questionDef = XFormParser.ghettoGetQuestionDef(treeElement.getDataType(), formDef, treeElement.getRef());
if (questionDef != null && (questionDef.getControlType() == Constants.CONTROL_SELECT_ONE || questionDef.getControlType() == Constants.CONTROL_SELECT_MULTI)) {
boolean containsSearchExpression = false;
XPathFuncExpr xpathExpression = null;
try {
xpathExpression = ExternalDataUtil.getSearchXPathExpression(questionDef.getAppearanceAttr());
} catch (Exception e) {
Timber.e(e);
// there is a search expression, but has syntax errors
containsSearchExpression = true;
}
if (xpathExpression != null || containsSearchExpression) {
// that means that we have dynamic selects
// read the static choices from the options sheet
List<SelectChoice> staticChoices = questionDef.getChoices();
for (int index = 0; index < staticChoices.size(); index++) {
SelectChoice selectChoice = staticChoices.get(index);
String selectChoiceValue = selectChoice.getValue();
if (ExternalDataUtil.isAnInteger(selectChoiceValue)) {
Selection selection = selectChoice.selection();
switch(questionDef.getControlType()) {
case Constants.CONTROL_SELECT_ONE:
{
if (selectChoiceValue.equals(textVal)) {
// we just need to make sure, so we will override that.
if (questionDef.getControlType() == Constants.CONTROL_SELECT_ONE) {
// we don't need another, just return the static choice.
return new SelectOneData(selection);
}
}
break;
}
case Constants.CONTROL_SELECT_MULTI:
{
// we should search in a potential comma-separated string of
// values for a match
// copied from org.javarosa.xform.util.XFormAnswerDataParser
// .getSelections()
List<String> textValues = DateUtils.split(textVal, XFormAnswerDataSerializer.DELIMITER, true);
if (textValues.contains(textVal)) {
// choice.
if (selectChoiceValue.equals(textVal)) {
// this means that the user selected ONLY the static
// answer, so just return that
List<Selection> customSelections = new ArrayList<Selection>();
customSelections.add(selection);
return new SelectMultiData(customSelections);
} else {
// we will ignore it for now since we will return that
// selection together with the dynamic ones.
}
}
break;
}
default:
{
// There is a bug if we get here, so let's throw an Exception
throw createBugRuntimeException(treeElement, textVal);
}
}
} else {
switch(questionDef.getControlType()) {
case Constants.CONTROL_SELECT_ONE:
{
// the default implementation will search for the "textVal"
// (saved answer) inside the static choices.
// Since we know that there isn't such, we just wrap the textVal
// in a virtual choice in order to
// create a SelectOneData object to be used as the IAnswer to the
// TreeElement.
// (the caller of this function is searching for such an answer
// to populate the in-memory model.)
SelectChoice customSelectChoice = new SelectChoice(textVal, textVal, false);
customSelectChoice.setIndex(index);
return new SelectOneData(customSelectChoice.selection());
}
case Constants.CONTROL_SELECT_MULTI:
{
// we should create multiple selections and add them to the pile
List<SelectChoice> customSelectChoices = createCustomSelectChoices(textVal);
List<Selection> customSelections = new ArrayList<Selection>();
for (SelectChoice customSelectChoice : customSelectChoices) {
customSelections.add(customSelectChoice.selection());
}
return new SelectMultiData(customSelections);
}
default:
{
// There is a bug if we get here, so let's throw an Exception
throw createBugRuntimeException(treeElement, textVal);
}
}
}
}
// if we get there then that means that we have a bug
throw createBugRuntimeException(treeElement, textVal);
}
}
// default behavior matches original behavior (for static selects, etc.)
return super.resolveAnswer(textVal, treeElement, formDef);
}
Aggregations