use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.
the class FormEntryCaption method getRepetitionsText.
public List<String> getRepetitionsText() {
GroupDef g = (GroupDef) element;
if (!g.getRepeat()) {
throw new RuntimeException("not a repeat");
}
int numRepetitions = getNumRepetitions();
List<String> reps = new ArrayList<String>(numRepetitions);
for (int i = 0; i < numRepetitions; i++) {
reps.add(getRepetitionText("choose", form.descendIntoRepeat(index, i), false));
}
return reps;
}
use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.
the class FormEntryModel method getCaptionHierarchy.
/**
* Returns a hierarchical list of FormEntryCaption objects for the given
* FormIndex
*
* @param index
* @return list of FormEntryCaptions in hierarchical order
*/
public FormEntryCaption[] getCaptionHierarchy(FormIndex index) {
List<FormEntryCaption> captions = new ArrayList<FormEntryCaption>();
FormIndex remaining = index;
while (remaining != null) {
remaining = remaining.getNextLevel();
FormIndex localIndex = index.diff(remaining);
IFormElement element = form.getChild(localIndex);
if (element != null) {
FormEntryCaption caption = null;
if (element instanceof GroupDef)
caption = new FormEntryCaption(getForm(), localIndex);
else if (element instanceof QuestionDef)
caption = new FormEntryPrompt(getForm(), localIndex);
if (caption != null) {
captions.add(caption);
}
}
}
FormEntryCaption[] captionArray = new FormEntryCaption[captions.size()];
return captions.toArray(captionArray);
}
use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.
the class FormEntryModel method createModelIfNecessary.
/**
* For the current index: Checks whether the index represents a node which
* should exist given a non-interactive repeat, along with a count for that
* repeat which is beneath the dynamic level specified.
*
* If this index does represent such a node, the new model for the repeat is
* created behind the scenes and the index for the initial question is
* returned.
*
* Note: This method will not prevent the addition of new repeat elements in
* the interface, it will merely use the xforms repeat hint to create new
* nodes that are assumed to exist
*
* @param index The index to be evaluated as to whether the underlying model is
* hinted to exist
*/
private void createModelIfNecessary(FormIndex index) {
if (index.isInForm()) {
IFormElement e = getForm().getChild(index);
if (e instanceof GroupDef) {
GroupDef g = (GroupDef) e;
if (g.getRepeat() && g.getCountReference() != null) {
// Lu Gram: repeat count XPath needs to be contextualized for nested repeat groups
TreeReference countRef = FormInstance.unpackReference(g.getCountReference());
TreeReference contextualized = countRef.contextualize(index.getReference());
IAnswerData count = getForm().getMainInstance().resolveReference(contextualized).getValue();
if (count != null) {
long fullcount = ((Integer) count.getValue()).intValue();
TreeReference ref = getForm().getChildInstanceRef(index);
TreeElement element = getForm().getMainInstance().resolveReference(ref);
if (element == null) {
if (index.getTerminal().getInstanceIndex() < fullcount) {
try {
getForm().createNewRepeat(index);
} catch (InvalidReferenceException ire) {
Std.printStack(ire);
throw new RuntimeException("Invalid Reference while creting new repeat!" + ire.getMessage());
}
}
}
}
}
}
}
}
use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.
the class FormEntryModel method setRepeatNextMultiplicity.
private boolean setRepeatNextMultiplicity(List<IFormElement> elements, List<Integer> multiplicities) {
// find out if node is repeatable
TreeReference nodeRef = form.getChildInstanceRef(elements, multiplicities);
TreeElement node = form.getMainInstance().resolveReference(nodeRef);
if (node == null || node.isRepeatable()) {
// node == null if there are no instances of the repeat
IFormElement lastElement = elements.get(elements.size() - 1);
if (lastElement instanceof GroupDef && !((GroupDef) lastElement).getRepeat()) {
// it's a regular group inside a repeatable group
return false;
}
int mult;
if (node == null) {
// no repeats; next is 0
mult = 0;
} else {
String name = node.getName();
TreeElement parentNode = form.getMainInstance().resolveReference(nodeRef.getParentRef());
mult = parentNode.getChildMultiplicity(name);
}
multiplicities.set(multiplicities.size() - 1, repeatStructure == REPEAT_STRUCTURE_NON_LINEAR ? TreeReference.INDEX_REPEAT_JUNCTURE : mult);
return true;
} else {
return false;
}
}
use of org.javarosa.core.model.GroupDef in project javarosa by opendatakit.
the class FormEntryModel method incrementHelper.
private void incrementHelper(List<Integer> indexes, List<Integer> multiplicities, List<IFormElement> elements, boolean descend) {
int i = indexes.size() - 1;
// if exiting a repetition? (i.e., go to next repetition instead of one level up)
boolean exitRepeat = false;
if (i == -1 || elements.get(i) instanceof GroupDef) {
if (i >= 0) {
// find out whether we're on a repeat, and if so, whether the
// specified instance actually exists
GroupDef group = (GroupDef) elements.get(i);
if (group.getRepeat()) {
if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
if (multiplicities.get(multiplicities.size() - 1) == TreeReference.INDEX_REPEAT_JUNCTURE) {
descend = false;
exitRepeat = true;
}
} else {
if (form.getMainInstance().resolveReference(form.getChildInstanceRef(elements, multiplicities)) == null) {
// repeat instance does not exist; do not descend into it
descend = false;
exitRepeat = true;
}
}
}
}
if (descend) {
IFormElement ife = (i == -1) ? null : elements.get(i);
if ((i == -1) || (ife != null && ife.getChildren() != null && ife.getChildren().size() > 0)) {
indexes.add(0);
multiplicities.add(0);
elements.add((i == -1 ? form : elements.get(i)).getChild(0));
if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
if (elements.get(elements.size() - 1) instanceof GroupDef && ((GroupDef) elements.get(elements.size() - 1)).getRepeat()) {
multiplicities.set(multiplicities.size() - 1, TreeReference.INDEX_REPEAT_JUNCTURE);
}
}
return;
}
}
}
while (i >= 0) {
// will be true)
if (!exitRepeat && elements.get(i) instanceof GroupDef && ((GroupDef) elements.get(i)).getRepeat()) {
if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
multiplicities.set(i, TreeReference.INDEX_REPEAT_JUNCTURE);
} else {
multiplicities.set(i, multiplicities.get(i) + 1);
}
return;
}
IFormElement parent = (i == 0 ? form : elements.get(i - 1));
int curIndex = indexes.get(i);
// increment to the next element on the current level
if (curIndex + 1 >= parent.getChildren().size()) {
// at the end of the current level; move up one level and start
// over
indexes.remove(i);
multiplicities.remove(i);
elements.remove(i);
i--;
exitRepeat = false;
} else {
indexes.set(i, curIndex + 1);
multiplicities.set(i, 0);
elements.set(i, parent.getChild(curIndex + 1));
if (repeatStructure == REPEAT_STRUCTURE_NON_LINEAR) {
if (elements.get(elements.size() - 1) instanceof GroupDef && ((GroupDef) elements.get(elements.size() - 1)).getRepeat()) {
multiplicities.set(multiplicities.size() - 1, TreeReference.INDEX_REPEAT_JUNCTURE);
}
}
return;
}
}
}
Aggregations