Search in sources :

Example 11 with FormIndex

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

the class ViewFormHierarchyActivity method onItemClick.

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    HierarchyElement h = (HierarchyElement) listView.getItemAtPosition(position);
    FormIndex index = h.getFormIndex();
    if (index == null) {
        goUpLevel();
        return;
    }
    switch(h.getType()) {
        case EXPANDED:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "COLLAPSED", h.getFormIndex());
            h.setType(COLLAPSED);
            ArrayList<HierarchyElement> children = h.getChildren();
            for (int i = 0; i < children.size(); i++) {
                formList.remove(position + 1);
            }
            h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_minimized));
            break;
        case COLLAPSED:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "EXPANDED", h.getFormIndex());
            h.setType(EXPANDED);
            ArrayList<HierarchyElement> children1 = h.getChildren();
            for (int i = 0; i < children1.size(); i++) {
                Timber.i("adding child: %s", children1.get(i).getFormIndex());
                formList.add(position + 1 + i, children1.get(i));
            }
            h.setIcon(ContextCompat.getDrawable(getApplicationContext(), R.drawable.expander_ic_maximized));
            break;
        case QUESTION:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "QUESTION-JUMP", index);
            Collect.getInstance().getFormController().jumpToIndex(index);
            if (Collect.getInstance().getFormController().indexIsInFieldList()) {
                try {
                    Collect.getInstance().getFormController().stepToPreviousScreenEvent();
                } catch (JavaRosaException e) {
                    Timber.d(e);
                    createErrorDialog(e.getCause().getMessage());
                    return;
                }
            }
            setResult(RESULT_OK);
            return;
        case CHILD:
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "onListItemClick", "REPEAT-JUMP", h.getFormIndex());
            Collect.getInstance().getFormController().jumpToIndex(h.getFormIndex());
            setResult(RESULT_OK);
            refreshView();
            return;
    }
    // Should only get here if we've expanded or collapsed a group
    HierarchyListAdapter itla = new HierarchyListAdapter(this);
    itla.setListItems(formList);
    listView.setAdapter(itla);
    listView.setSelection(position);
}
Also used : HierarchyElement(org.odk.collect.android.logic.HierarchyElement) HierarchyListAdapter(org.odk.collect.android.adapters.HierarchyListAdapter) FormIndex(org.javarosa.core.model.FormIndex) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 12 with FormIndex

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

the class FormIndexSerializationTest method deserializeFormIndex.

private static FormIndex deserializeFormIndex(byte[] serializedFormIndex) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedFormIndex);
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object o = ois.readObject();
    return (FormIndex) o;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormIndex(org.javarosa.core.model.FormIndex) ObjectInputStream(java.io.ObjectInputStream)

Example 13 with FormIndex

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

the class FormIndexSerializationTest method testEndOfForm.

@Test
public void testEndOfForm() throws IOException, ClassNotFoundException {
    FormIndex formIndexToSerialize = FormIndex.createEndOfFormIndex();
    byte[] serializedObject = serializeObject(formIndexToSerialize);
    FormIndex formIndexDeserialized = deserializeFormIndex(serializedObject);
    assertFormIndex(formIndexToSerialize, formIndexDeserialized);
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) Test(org.junit.Test)

Example 14 with FormIndex

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

the class FormIndexSerializationTest method testLocalAndInstanceNullReference.

@Test
public void testLocalAndInstanceNullReference() throws IOException, ClassNotFoundException {
    FormIndex formIndexToSerialize = new FormIndex(1, 2, null);
    byte[] serializedObject = serializeObject(formIndexToSerialize);
    FormIndex formIndexDeserialized = deserializeFormIndex(serializedObject);
    assertFormIndex(formIndexToSerialize, formIndexDeserialized);
}
Also used : FormIndex(org.javarosa.core.model.FormIndex) Test(org.junit.Test)

Example 15 with FormIndex

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

the class FormEntryModel method isIndexRelevant.

/**
 * Determine if the current FormIndex is relevant. Only relevant indexes
 * should be returned when filling out a form.
 *
 * @param index
 * @return true if current element at FormIndex is relevant
 */
public boolean isIndexRelevant(FormIndex index) {
    TreeReference ref = form.getChildInstanceRef(index);
    boolean isAskNewRepeat = (getEvent(index) == FormEntryController.EVENT_PROMPT_NEW_REPEAT);
    boolean isRepeatJuncture = (getEvent(index) == FormEntryController.EVENT_REPEAT_JUNCTURE);
    boolean relevant;
    if (isAskNewRepeat) {
        relevant = form.isRepeatRelevant(ref) && form.canCreateRepeat(ref, index);
    // repeat junctures are still relevant if no new repeat can be created; that option
    // is simply missing from the menu
    } else if (isRepeatJuncture) {
        relevant = form.isRepeatRelevant(ref);
    } else {
        TreeElement node = form.getMainInstance().resolveReference(ref);
        // check instance flag first
        relevant = node != null && node.isRelevant();
    }
    if (relevant) {
        // if instance flag/condition says relevant, we still
        // have to check the <group>/<repeat> hierarchy
        FormIndex ancestorIndex = index;
        while (!ancestorIndex.isTerminal()) {
            // This should be safe now that the TreeReference is contained
            // in the ancestor index itself
            TreeElement ancestorNode = form.getMainInstance().resolveReference(ancestorIndex.getLocalReference());
            if (!ancestorNode.isRelevant()) {
                relevant = false;
                break;
            }
            ancestorIndex = ancestorIndex.getNextLevel();
        }
    }
    return relevant;
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) FormIndex(org.javarosa.core.model.FormIndex) TreeElement(org.javarosa.core.model.instance.TreeElement)

Aggregations

FormIndex (org.javarosa.core.model.FormIndex)63 GroupDef (org.javarosa.core.model.GroupDef)11 JavaRosaException (org.odk.collect.android.exception.JavaRosaException)11 FormController (org.odk.collect.android.javarosawrapper.FormController)11 ArrayList (java.util.ArrayList)10 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)10 Test (org.junit.Test)10 IFormElement (org.javarosa.core.model.IFormElement)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)6 File (java.io.File)5 FormController (org.odk.collect.android.logic.FormController)5 HierarchyElement (org.odk.collect.android.logic.HierarchyElement)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 HierarchyListAdapter (org.odk.collect.android.adapters.HierarchyListAdapter)4 FormDef (org.javarosa.core.model.FormDef)3 FormEntryModel (org.javarosa.form.api.FormEntryModel)3 Intent (android.content.Intent)2 ObjectInputStream (java.io.ObjectInputStream)2 HashMap (java.util.HashMap)2