Search in sources :

Example 6 with SelectChoice

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

the class TextFormTests method testSelectChoicesNoLocalizer.

public void testSelectChoicesNoLocalizer() {
    QuestionDef q = fpi.getFirstQuestionDef();
    if (q.getNumChoices() != 0) {
        fail("Select choices not empty on init");
    }
    // fpi.getNextQuestion();
    String onetext = "choice";
    String twotext = "stacey's";
    SelectChoice one = new SelectChoice(null, onetext, "val", false);
    q.addSelectChoice(one);
    SelectChoice two = new SelectChoice(null, twotext, "mom", false);
    q.addSelectChoice(two);
    if (!fep.getSelectChoices().toString().equals("[choice => val, stacey's => mom]")) {
        fail("Could not add individual select choice" + fep.getSelectChoices().toString());
    }
    Object a = onetext;
    Object b = fep.getSelectChoiceText(one);
    assertEquals("Invalid select choice text returned", a, b);
    assertEquals("Invalid select choice text returned", twotext, fep.getSelectChoiceText(two));
    assertNull("Form Entry Caption incorrectly contains Image Text", fep.getSpecialFormSelectChoiceText(one, FormEntryCaption.TEXT_FORM_IMAGE));
    assertNull("Form Entry Caption incorrectly contains Audio Text", fep.getSpecialFormSelectChoiceText(one, FormEntryCaption.TEXT_FORM_AUDIO));
    q.removeSelectChoice(q.getChoice(0));
    q.removeSelectChoice(q.getChoice(0));
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 7 with SelectChoice

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

the class TextFormTests method testSelectChoiceIDsNoLocalizer.

public void testSelectChoiceIDsNoLocalizer() {
    QuestionDef q = fpi.getFirstQuestionDef();
    q.addSelectChoice(new SelectChoice("choice1 id", "val1"));
    q.addSelectChoice(new SelectChoice("loc: choice2", "val2", false));
    if (!fep.getSelectChoices().toString().equals("[{choice1 id} => val1, loc: choice2 => val2]")) {
        fail("Could not add individual select choice ID" + fep.getSelectChoices().toString());
    }
    // clean up
    q.removeSelectChoice(q.getChoices().get(0));
    q.removeSelectChoice(q.getChoices().get(0));
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 8 with SelectChoice

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

the class XFormParser method parseItem.

private void parseItem(QuestionDef q, Element e) {
    final int MAX_VALUE_LEN = 32;
    // catalogue of used attributes in this method/element
    List<String> usedAtts = new ArrayList<>();
    List<String> labelUA = new ArrayList<>();
    List<String> valueUA = new ArrayList<>();
    labelUA.add(REF_ATTR);
    valueUA.add(FORM_ATTR);
    String labelInnerText = null;
    String textRef = null;
    String value = null;
    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)) {
            // print attribute warning for child element
            if (XFormUtils.showUnusedAttributeWarning(child, labelUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, labelUA), getVagueLocation(child));
            }
            labelInnerText = getLabel(child);
            String ref = child.getAttributeValue("", REF_ATTR);
            if (ref != null) {
                if (ref.startsWith(ITEXT_OPEN) && ref.endsWith(ITEXT_CLOSE)) {
                    textRef = ref.substring(ITEXT_OPEN.length(), ref.lastIndexOf(ITEXT_CLOSE));
                    verifyTextMappings(textRef, "Item <label>", true);
                } else {
                    throw new XFormParseException("malformed ref [" + ref + "] for <item>", child);
                }
            }
        } else if (VALUE.equals(childName)) {
            value = getXMLText(child, true);
            // print attribute warning for child element
            if (XFormUtils.showUnusedAttributeWarning(child, valueUA)) {
                reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(child, valueUA), getVagueLocation(child));
            }
            if (value != null) {
                if (value.length() > MAX_VALUE_LEN) {
                    reporter.warning(XFormParserReporter.TYPE_ERROR_PRONE, "choice value [" + value + "] is too long; max. suggested length " + MAX_VALUE_LEN + " chars", getVagueLocation(child));
                }
                // validate
                for (int k = 0; k < value.length(); k++) {
                    char c = value.charAt(k);
                    if (" \n\t\f\r\'\"`".indexOf(c) >= 0) {
                        boolean isMultiSelect = (q.getControlType() == Constants.CONTROL_SELECT_MULTI);
                        reporter.warning(XFormParserReporter.TYPE_ERROR_PRONE, (isMultiSelect ? SELECT : SELECTONE) + " question <value>s [" + value + "] " + (isMultiSelect ? "cannot" : "should not") + " contain spaces, and are recommended not to contain apostraphes/quotation marks", getVagueLocation(child));
                        break;
                    }
                }
            }
        }
    }
    if (textRef == null && labelInnerText == null) {
        throw new XFormParseException("<item> without proper <label>", e);
    }
    if (value == null) {
        throw new XFormParseException("<item> without proper <value>", e);
    }
    if (textRef != null) {
        q.addSelectChoice(new SelectChoice(textRef, value));
    } else {
        q.addSelectChoice(new SelectChoice(null, labelInnerText, value, false));
    }
    // print unused attribute warning message for parent element
    if (XFormUtils.showUnusedAttributeWarning(e, usedAtts)) {
        reporter.warning(XFormParserReporter.TYPE_UNKNOWN_MARKUP, XFormUtils.unusedAttWarning(e, usedAtts), getVagueLocation(e));
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) 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)

Example 9 with SelectChoice

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

Example 10 with SelectChoice

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

the class ItemsetWidget method getItemsFromDatabase.

private List<SelectChoice> getItemsFromDatabase(String selection, String[] selectionArgs, FormController formController) {
    List<SelectChoice> items = new ArrayList<>();
    File itemsetFile = fileUtil.getItemsetFile(formController.getMediaFolder().getAbsolutePath());
    if (itemsetFile.exists()) {
        adapter.open();
        // name of the itemset table for this form
        String pathHash = ItemsetDbAdapter.getMd5FromString(itemsetFile.getAbsolutePath());
        try {
            Cursor c = adapter.query(pathHash, selection, selectionArgs);
            if (c != null) {
                c.move(-1);
                int index = 0;
                while (c.moveToNext()) {
                    String label;
                    String val;
                    // try to get the value associated with the label:lang
                    // string if that doen't exist, then just use label
                    String lang = "";
                    if (formController.getLanguages() != null && formController.getLanguages().length > 0) {
                        lang = formController.getLanguage();
                    }
                    // apparently you only need the double quotes in the
                    // column name when creating the column with a : included
                    String labelLang = "label" + "::" + lang;
                    int langCol = c.getColumnIndex(labelLang);
                    if (langCol == -1) {
                        label = c.getString(c.getColumnIndex("label"));
                    } else {
                        label = c.getString(c.getColumnIndex(labelLang));
                    }
                    val = c.getString(c.getColumnIndex("name"));
                    SelectChoice selectChoice = new SelectChoice(null, label, val, false);
                    selectChoice.setIndex(index);
                    items.add(selectChoice);
                    index++;
                }
                c.close();
            }
        } catch (SQLiteException e) {
            Timber.i(e);
        } finally {
            adapter.close();
        }
    } else {
        TextView error = new TextView(getContext());
        error.setText(getContext().getString(R.string.file_missing, itemsetFile.getAbsolutePath()));
        addAnswerView(error);
    }
    return items;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) Cursor(android.database.Cursor) SQLiteException(android.database.sqlite.SQLiteException) File(java.io.File) SuppressLint(android.annotation.SuppressLint)

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