Ejemplo n.º 1
0
  public static void main(String args[]) throws Exception {
    DialogCallbackHandler h = new DialogCallbackHandler();
    TextOutputCallback toc = new TextOutputCallback(TextOutputCallback.INFORMATION, "hello");
    TextOutputCallback toc2 = new TextOutputCallback(TextOutputCallback.INFORMATION, "world");
    ConfirmationCallback cc =
        new ConfirmationCallback(
            "Correct?",
            ConfirmationCallback.INFORMATION,
            ConfirmationCallback.YES_NO_OPTION,
            ConfirmationCallback.NO);

    Callback[] callbacks = {toc, toc2, cc};
    h.handle(callbacks);

    if (cc.getSelectedIndex() == ConfirmationCallback.YES) {
      System.out.println("yes");
    } else {
      System.out.println("no");
    }

    System.exit(0);
  }
  /** {@inheritDoc} */
  public ConfirmationCallback convertFromJson(ConfirmationCallback callback, JsonValue jsonCallback)
      throws RestAuthException {

    validateCallbackType(CALLBACK_NAME, jsonCallback);

    JsonValue input = jsonCallback.get("input");

    if (input.size() != 1) {
      throw new JsonException("JSON Callback does not include a input field");
    }

    JsonValue inputFieldValue = input.get(0).get("value").required();
    int value;
    if (inputFieldValue.isString()) {
      value = Integer.parseInt(inputFieldValue.asString());
    } else {
      value = inputFieldValue.asInteger();
    }
    callback.setSelectedIndex(value);

    return callback;
  }
  /** {@inheritDoc} */
  public JsonValue convertToJson(ConfirmationCallback callback, int index) {

    String prompt = callback.getPrompt();
    int messageType = callback.getMessageType();
    String[] options = callback.getOptions();
    int optionType = callback.getOptionType();
    int defaultOption = callback.getDefaultOption();
    int selectedIndex = callback.getSelectedIndex();

    JsonValue jsonValue =
        JsonValueBuilder.jsonValue()
            .put("type", CALLBACK_NAME)
            .array("output")
            .add(createOutputField("prompt", prompt))
            .add(createOutputField("messageType", messageType))
            .add(createOutputField("options", options))
            .add(createOutputField("optionType", optionType))
            .addLast(createOutputField("defaultOption", defaultOption))
            .array("input")
            .addLast(createInputField(index, selectedIndex))
            .build();

    return jsonValue;
  }