protected IAnswerData getCurrentAnswer() {
   IAnswerData current = mPrompt.getAnswerValue();
   if (current == null) {
     return null;
   }
   return getTemplate().cast(current.uncast());
 }
  public static Object getValue(String xpath, TreeReference context, FormInstance tree) {
    TreeElement node = tree.resolveReference(ref(xpath).contextualize(context));
    if (node == null) {
      throw new RuntimeException(
          "Could not find node [" + xpath + "] when parsing saved instance!");
    }

    if (node.isRelevant()) {
      IAnswerData val = node.getValue();
      return (val == null ? null : val.getValue());
    } else {
      return null;
    }
  }
Beispiel #3
0
 private Integer getIntegerAnswerValue() {
   IAnswerData dataHolder = mPrompt.getAnswerValue();
   Integer d = null;
   if (dataHolder != null) {
     Object dataValue = dataHolder.getValue();
     if (dataValue != null) {
       if (dataValue instanceof Double) {
         d = Integer.valueOf(((Double) dataValue).intValue());
       } else {
         d = (Integer) dataValue;
       }
     }
   }
   return d;
 }
 private Double getDoubleAnswerValue() {
   IAnswerData dataHolder = mPrompt.getAnswerValue();
   Double d = null;
   if (dataHolder != null) {
     Object dataValue = dataHolder.getValue();
     if (dataValue != null) {
       if (dataValue instanceof Integer) {
         d = Double.valueOf(((Integer) dataValue).intValue());
       } else {
         d = (Double) dataValue;
       }
     }
   }
   return d;
 }
Beispiel #5
0
  private void attachControlsToInstanceData(TreeElement node) {
    for (int i = 0; i < node.getNumChildren(); i++) {
      attachControlsToInstanceData(node.getChildAt(i));
    }

    IAnswerData val = node.getValue();
    Vector selections = null;
    if (val instanceof SelectOneData) {
      selections = new Vector();
      selections.addElement(val.getValue());
    } else if (val instanceof SelectMultiData) {
      selections = (Vector) 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 (int i = 0; i < selections.size(); i++) {
        Selection s = (Selection) selections.elementAt(i);
        s.attachChoice(q);
      }
    }
  }
 public String getMartusAnswerStringFromQuestion(
     IAnswerData answer, QuestionDef question, final int dataType) throws Exception {
   String answerAsString = answer.getDisplayText();
   if (dataType == Constants.DATATYPE_DATE) {
     answerAsString = formatDateToMartusDateFormat(answerAsString);
   } else if (shouldTreatSingleItemChoiceListAsBooleanField(dataType, question)
       && answerAsString.isEmpty()) {
     answerAsString = FieldSpec.FALSESTRING;
   } else if (dataType == Constants.DATATYPE_CHOICE) {
     List<SelectChoice> list = question.getChoices();
     for (int i = 0; i < list.size(); ++i) {
       SelectChoice choice = list.get(i);
       if (choice.getValue().equals(answerAsString)) {
         answerAsString = choice.getLabelInnerText();
         break;
       }
     }
   }
   return answerAsString;
 }
Beispiel #7
0
  public void copyItemsetAnswer(QuestionDef q, TreeElement targetNode, IAnswerData data)
      throws InvalidReferenceException {
    ItemsetBinding itemset = q.getDynamicChoices();
    TreeReference targetRef = targetNode.getRef();
    TreeReference destRef = itemset.getDestRef().contextualize(targetRef);

    Vector<Selection> selections = null;
    Vector<String> selectedValues = new Vector<String>();
    if (data instanceof SelectMultiData) {
      selections = (Vector<Selection>) data.getValue();
    } else if (data instanceof SelectOneData) {
      selections = new Vector<Selection>();
      selections.addElement((Selection) data.getValue());
    }
    if (itemset.valueRef != null) {
      for (int i = 0; i < selections.size(); i++) {
        selectedValues.addElement(selections.elementAt(i).choice.getValue());
      }
    }

    // delete existing dest nodes that are not in the answer selection
    Hashtable<String, TreeElement> existingValues = new Hashtable<String, TreeElement>();
    Vector<TreeReference> existingNodes = getInstance().expandReference(destRef);
    for (int i = 0; i < existingNodes.size(); i++) {
      TreeElement node = getInstance().resolveReference(existingNodes.elementAt(i));

      if (itemset.valueRef != null) {
        String value =
            itemset
                .getRelativeValue()
                .evalReadable(
                    this.getInstance(), new EvaluationContext(exprEvalContext, node.getRef()));
        if (selectedValues.contains(value)) {
          existingValues.put(value, node); // cache node if in selection and already exists
        }
      }

      // delete from target
      targetNode.removeChild(node);
    }

    // copy in nodes for new answer; preserve ordering in answer
    for (int i = 0; i < selections.size(); i++) {
      Selection s = selections.elementAt(i);
      SelectChoice ch = s.choice;

      TreeElement cachedNode = null;
      if (itemset.valueRef != null) {
        String value = ch.getValue();
        if (existingValues.containsKey(value)) {
          cachedNode = existingValues.get(value);
        }
      }

      if (cachedNode != null) {
        cachedNode.setMult(i);
        targetNode.addChild(cachedNode);
      } else {
        getInstance().copyItemsetNode(ch.copyNode, destRef, this);
      }
    }

    triggerTriggerables(
        destRef); // trigger conditions that depend on the creation of these new nodes
    initializeTriggerables(destRef); // initialize conditions for the node (and sub-nodes)
    // not 100% sure this will work since destRef is ambiguous as the last step, but i think it's
    // supposed to work
  }
    @SuppressWarnings("unchecked")
    public GridMultiWidget(Context context, FormEntryPrompt prompt, int numColumns) {
        super(context, prompt);
        mItems = prompt.getSelectChoices();
        mPrompt = prompt;

        selected = new boolean[mItems.size()];
        choices = new String[mItems.size()];
        gridview = new GridView(context);
        imageViews = new ImageView[mItems.size()];
        maxColumnWidth = -1;
        this.numColumns = numColumns;
        for (int i = 0; i < mItems.size(); i++) {
            imageViews[i] = new ImageView(getContext());
        }

        // Build view
        for (int i = 0; i < mItems.size(); i++) {
            SelectChoice sc = mItems.get(i);
            // Read the image sizes and set maxColumnWidth. This allows us to make sure all of our
            // columns are going to fit
            String imageURI =
                prompt.getSpecialFormSelectChoiceText(sc, FormEntryCaption.TEXT_FORM_IMAGE);

            if (imageURI != null) {
                choices[i] = imageURI;

                String imageFilename;
                try {
                    imageFilename = ReferenceManager._().DeriveReference(imageURI).getLocalURI();
                    final File imageFile = new File(imageFilename);
                    if (imageFile.exists()) {
                        Display display =
                            ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
                                    .getDefaultDisplay();
                        int screenWidth = display.getWidth();
                        int screenHeight = display.getHeight();
                        Bitmap b =
                            FileUtils
                                    .getBitmapScaledToDisplay(imageFile, screenHeight, screenWidth);
                        if (b != null) {

                            if (b.getWidth() > maxColumnWidth) {
                                maxColumnWidth = b.getWidth();
                            }

                        }
                    }
                } catch (InvalidReferenceException e) {
                    Log.e("GridWidget", "image invalid reference exception");
                    e.printStackTrace();
                }

            } else {
                choices[i] = prompt.getSelectChoiceText(sc);
            }

        }

        // Use the custom image adapter and initialize the grid view
        ImageAdapter ia = new ImageAdapter(getContext(), choices);
        gridview.setAdapter(ia);
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                if (selected[position]) {
                    selected[position] = false;
                    imageViews[position].setBackgroundColor(Color.WHITE);
                   	Collect.getInstance().getActivityLogger().logInstanceAction(this, "onItemClick.deselect", 
                			mItems.get(position).getValue(), mPrompt.getIndex());

                } else {
                    selected[position] = true;
                    imageViews[position].setBackgroundColor(Color.rgb(orangeRedVal, orangeGreenVal,
                        orangeBlueVal));
                   	Collect.getInstance().getActivityLogger().logInstanceAction(this, "onItemClick.select", 
                			mItems.get(position).getValue(), mPrompt.getIndex());
                }

            }
        });

        // Read the screen dimensions and fit the grid view to them. It is important that the grid
        // view
        // knows how far out it can stretch.
        Display display =
            ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();
        GridView.LayoutParams params = new GridView.LayoutParams(screenWidth - 5, screenHeight - 5);
        gridview.setLayoutParams(params);

        // Use the user's choice for num columns, otherwise automatically decide.
        if (numColumns > 0) {
            gridview.setNumColumns(numColumns);
        } else {
            gridview.setNumColumns(GridView.AUTO_FIT);
        }

        gridview.setColumnWidth(maxColumnWidth);
        gridview.setHorizontalSpacing(2);
        gridview.setVerticalSpacing(2);
        gridview.setGravity(Gravity.LEFT);
        gridview.setStretchMode(GridView.NO_STRETCH);

        // Fill in answer
        IAnswerData answer = prompt.getAnswerValue();
        Vector<Selection> ve;
        if ((answer == null) || (answer.getValue() == null)) {
            ve = new Vector<Selection>();
        } else {
            ve = (Vector<Selection>) answer.getValue();
        }

        for (int i = 0; i < choices.length; ++i) {

            String value = mItems.get(i).getValue();
            boolean found = false;
            for (Selection s : ve) {
                if (value.equals(s.getValue())) {
                    found = true;
                    break;
                }
            }

            selected[i] = found;
            if (selected[i]) {
                imageViews[i].setBackgroundColor(Color.rgb(orangeRedVal, orangeGreenVal,
                    orangeBlueVal));
            } else {
                imageViews[i].setBackgroundColor(Color.WHITE);
            }
        }

        addView(gridview);
    }