Esempio n. 1
0
  /**
   * Returns true if a string is a standard number, i.e not in scientific notation
   *
   * @param s
   * @return
   */
  private static boolean isStandardNumber(String s) {

    // return if empty string
    if (s == null || s.length() == 0) return false;

    // test the first char for a digit, sign or decimal point.
    Character c = s.charAt(0);
    if (!(StringUtil.isDigit(c) || c == '.' || c == '-' || c == '+' || c == '\u2212')) {
      return false;
    }

    // test the remaining chars for digits or decimal point
    int decimalCount = 0;
    for (int i = 1; i < s.length(); i++) {
      c = s.charAt(i);
      if (StringUtil.isDigit(c)) {
        continue;
      }
      if (c == '.' && decimalCount == 0) {
        decimalCount++;
        continue;
      }
      return false;
    }
    return true;
  }
  /*
   * auto-insert degree symbol when appropriate
   */
  public void onKeyUp(KeyUpEvent e) {

    // return unless digit typed (instead of !Character.isDigit)
    if (e.getNativeKeyCode() < 48
        || (e.getNativeKeyCode() > 57 && e.getNativeKeyCode() < 96)
        || e.getNativeKeyCode() > 105) return;

    AutoCompleteTextFieldW tc = inputPanel.getTextComponent();
    String text = tc.getText();

    // if text already contains degree symbol or variable
    for (int i = 0; i < text.length(); i++) {
      if (!StringUtil.isDigit(text.charAt(i))) return;
    }

    int caretPos = tc.getCaretPosition();

    tc.setText(tc.getText() + Unicode.degree);

    tc.setCaretPosition(caretPos);
  }