Search in sources :

Example 11 with SelectChoice

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

the class SelectImageMapWidget method setUpWebView.

@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
private void setUpWebView() {
    String svgMap = getParsedSVGFile();
    if (svgMap != null) {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.addJavascriptInterface(new JavaScriptInterface(), "imageMapInterface");
        webView.loadDataWithBaseURL(null, String.format(WEB_VIEW_CONTENT, svgMap), "text/html", "UTF-8", null);
        webView.setInitialScale(1);
        webView.getSettings().setUseWideViewPort(true);
        // about 60% of a screen
        int height = (int) (getResources().getDisplayMetrics().heightPixels / 1.7);
        webView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                view.loadUrl("javascript:setSelectMode(" + isSingleSelect + ")");
                for (SelectChoice selectChoice : items) {
                    view.loadUrl("javascript:addArea('" + selectChoice.getValue() + "')");
                }
                highlightSelections(view);
            }
        });
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) WebView(android.webkit.WebView) CustomWebView(org.odk.collect.android.views.CustomWebView) SuppressLint(android.annotation.SuppressLint) LinearLayout(android.widget.LinearLayout) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 12 with SelectChoice

use of org.javarosa.core.model.SelectChoice 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);
    }
}
Also used : SelectMultiData(org.javarosa.core.model.data.SelectMultiData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 13 with SelectChoice

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

the class ExternalAnswerResolver method createCustomSelectChoices.

protected List<SelectChoice> createCustomSelectChoices(String completeTextValue) {
    // copied from org.javarosa.xform.util.XFormAnswerDataParser.getSelections()
    List<String> textValues = DateUtils.split(completeTextValue, XFormAnswerDataSerializer.DELIMITER, true);
    int index = 0;
    List<SelectChoice> customSelectChoices = new ArrayList<SelectChoice>();
    for (String textValue : textValues) {
        SelectChoice selectChoice = new SelectChoice(textValue, textValue, false);
        selectChoice.setIndex(index++);
        customSelectChoices.add(selectChoice);
    }
    return customSelectChoices;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) ArrayList(java.util.ArrayList)

Example 14 with SelectChoice

use of org.javarosa.core.model.SelectChoice 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);
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) XPathFuncExpr(org.javarosa.xpath.expr.XPathFuncExpr) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) ArrayList(java.util.ArrayList) List(java.util.List) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 15 with SelectChoice

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

the class ExternalDataUtil method populateExternalChoices.

public static ArrayList<SelectChoice> populateExternalChoices(FormEntryPrompt formEntryPrompt, XPathFuncExpr xpathfuncexpr) {
    try {
        List<SelectChoice> selectChoices = formEntryPrompt.getSelectChoices();
        ArrayList<SelectChoice> returnedChoices = new ArrayList<SelectChoice>();
        for (SelectChoice selectChoice : selectChoices) {
            String value = selectChoice.getValue();
            if (isAnInteger(value)) {
                // treat this as a static choice
                returnedChoices.add(selectChoice);
            } else {
                String displayColumns = formEntryPrompt.getSelectChoiceText(selectChoice);
                String imageColumn = formEntryPrompt.getSpecialFormSelectChoiceText(selectChoice, FormEntryCaption.TEXT_FORM_IMAGE);
                if (imageColumn != null && imageColumn.startsWith(JR_IMAGES_PREFIX)) {
                    imageColumn = imageColumn.substring(JR_IMAGES_PREFIX.length());
                }
                // if (displayColumns == null || displayColumns.trim().length() == 0) {
                // throw new InvalidSyntaxException("The label column in the choices sheet
                // appears to be empty (or has been calculated as empty).");
                // }
                ExternalDataManager externalDataManager = Collect.getInstance().getExternalDataManager();
                FormInstance formInstance = Collect.getInstance().getFormController().getFormDef().getInstance();
                EvaluationContext baseEvaluationContext = new EvaluationContext(formInstance);
                EvaluationContext evaluationContext = new EvaluationContext(baseEvaluationContext, formEntryPrompt.getIndex().getReference());
                // we can only add only the appropriate by querying the xPathFuncExpr.id.name
                evaluationContext.addFunctionHandler(new ExternalDataHandlerSearch(externalDataManager, displayColumns, value, imageColumn));
                Object eval = xpathfuncexpr.eval(formInstance, evaluationContext);
                if (eval.getClass().isAssignableFrom(ArrayList.class)) {
                    @SuppressWarnings("unchecked") List<SelectChoice> dynamicChoices = (ArrayList<SelectChoice>) eval;
                    for (SelectChoice dynamicChoice : dynamicChoices) {
                        returnedChoices.add(dynamicChoice);
                    }
                } else {
                    throw new ExternalDataException(Collect.getInstance().getString(R.string.ext_search_return_error, eval.getClass().getName()));
                }
            }
        }
        return returnedChoices;
    } catch (Exception e) {
        throw new ExternalDataException(e.getMessage(), e);
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) ArrayList(java.util.ArrayList) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) ExternalDataHandlerSearch(org.odk.collect.android.external.handler.ExternalDataHandlerSearch) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance)

Aggregations

SelectChoice (org.javarosa.core.model.SelectChoice)24 ArrayList (java.util.ArrayList)12 Selection (org.javarosa.core.model.data.helper.Selection)12 QuestionDef (org.javarosa.core.model.QuestionDef)7 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)7 SuppressLint (android.annotation.SuppressLint)5 SelectOneData (org.javarosa.core.model.data.SelectOneData)4 List (java.util.List)3 Test (org.junit.Test)3 NonNull (android.support.annotation.NonNull)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 IAnswerData (org.javarosa.core.model.data.IAnswerData)2 TreeElement (org.javarosa.core.model.instance.TreeElement)2 ExternalSelectChoice (org.odk.collect.android.external.ExternalSelectChoice)2 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1