Example #1
0
 /**
  * ************************************************************************* Handle a text command
  * ************************************************************************
  */
 private void handleCommand() {
   String promptValue = m_textInput.getValue();
   if (promptValue.isEmpty()) {
     return;
   }
   m_parent.issueCommand(promptValue);
   m_textInput.reset();
 }
Example #2
0
  /**
   * ************************************************************************* Set the table font
   * ************************************************************************
   */
  private void updateFontFromSize() {
    if (m_myFont != null) {
      m_myFont.dispose();
    }
    setRedraw(false);
    m_myFont = new Font(Display.getDefault(), "Courier New", m_fontSize, SWT.NORMAL);
    m_promptText.setFont(m_myFont);
    m_textInput.setFont(m_myFont);

    try {
      if (m_optionContainer != null && !m_optionContainer.isDisposed()) {
        for (Button opt : m_optionsRadio) {
          opt.setFont(m_myFont);
        }
        m_optionContainer.pack();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    setRedraw(true);
    m_parent.layout();
  }
Example #3
0
  /**
   * ************************************************************************* Handle a text command
   * ************************************************************************
   */
  private void handlePromptAnswer() {
    Logger.debug("Handle prompt answer", Level.GUI, this);

    String answer = m_textInput.getValue();
    if (answer.length() == 0 && m_selectedOption < 0) {
      MessageDialog.openError(getShell(), "Prompt error", "Cannot commit, no value given");
      m_textInput.reset();
      m_textInput.promptStart();

      return;
    }
    if (m_numericInput) {
      try {
        // Is it an int?
        Integer.parseInt(answer);
        m_promptData.setReturnValue(answer);
      } catch (NumberFormatException ex) {
        try {
          // Is it a double?
          Double.parseDouble(answer);
          m_promptData.setReturnValue(answer);
        } catch (NumberFormatException ex2) {
          try {
            // Is it a hex?
            if (answer.startsWith("0x")) {
              int a = Integer.parseInt(answer.substring(2), 16);
              m_promptData.setReturnValue(a + "");
            } else {
              throw new NumberFormatException("Hex out of range");
            }
          } catch (NumberFormatException ex3) {
            MessageDialog.openError(
                getShell(), "Prompt error", "Cannot commit, expected a numeric value");

            m_textInput.reset();
            m_textInput.promptStart();

            return;
          }
        }
      }
    } else if (m_expected != null) {
      Logger.debug("Have expected values", Level.GUI, this);
      // If the input text field has an answer
      if (!answer.isEmpty()) {
        Logger.debug("Current text field answer: '" + answer + "'", Level.GUI, this);
        // Check if there is a selected option
        if (m_selectedOption >= 0) {
          // If there is a choice, it must be consistent with the text
          // input
          String optString = m_expected.get(m_selectedOption);

          Logger.debug("Option string: '" + optString + "'", Level.GUI, this);

          if (!optString.equals(answer)) {
            MessageDialog.openError(
                getShell(),
                "Prompt error",
                "Conflicting values found between text area and buttons");

            resetOptions();

            m_textInput.reset();
            m_textInput.promptStart();

            return;
          }
        }
        // If there is no selected option, check that the text input
        // matches any of the expected values
        else {
          boolean accept = false;
          for (String opt : m_expected) {
            if (opt.equals(answer)) {
              accept = true;
              break;
            }
          }
          if (!accept) {
            String values = "";
            for (String exp : m_expected) values += exp + "\n";
            MessageDialog.openError(
                getShell(), "Prompt error", "Must enter one of the expected values:\n" + values);
            m_textInput.reset();
            m_textInput.promptStart();
            return;
          }
        }

        // If we reach this point it is ok. Set the prompt answer, but
        // getting the corresponding index of the option
        int idx = -1;
        int count = 0;
        for (String expected : m_expected) {
          if (expected.equals(answer)) {
            idx = count;
            break;
          }
          count++;
        }
        m_promptData.setReturnValue(Integer.toString(idx));
      }
      // If the text input does not have a text, at least there must be a
      // selected option in the list
      else if (m_selectedOption == -1) {
        String values = "";
        for (String exp : m_expected) values += exp + "\n";
        MessageDialog.openError(
            getShell(), "Prompt error", "Must enter one of the expected values:\n" + values);
        m_textInput.reset();
        m_textInput.promptStart();
        return;
      }
      // We have a selected option
      else {
        // Set the option index as return value
        m_promptData.setReturnValue(Integer.toString(m_selectedOption));
      }
    } else {
      m_promptData.setReturnValue(answer);
    }
    s_proxy.answerPrompt(m_promptData);
    m_parent.resetPrompt();
    m_textInput.promptEnd();
  }