Example #1
0
  /**
   * ************************************************************************ Insert String
   *
   * @param origOffset
   * @param string
   * @param attr
   * @throws BadLocationException
   */
  @Override
  public void insertString(int origOffset, String string, AttributeSet attr)
      throws BadLocationException {
    log.finest("Offset=" + origOffset + " String=" + string + " Length=" + string.length());
    if (origOffset < 0 || string == null) throw new IllegalArgumentException("Invalid argument");

    int offset = origOffset;
    int length = string.length();
    //	From DataBinder (assuming correct format)
    if (length != 1) {
      super.insertString(offset, string, attr);
      return;
    }

    /** Manual Entry */
    String content = getText();
    //	remove all Thousands
    if (content.indexOf(m_groupingSeparator) != -1) {
      StringBuffer result = new StringBuffer();
      for (int i = 0; i < content.length(); i++) {
        if (content.charAt(i) == m_groupingSeparator) {
          if (i < offset) offset--;
        } else result.append(content.charAt(i));
      }
      super.remove(0, content.length());
      super.insertString(0, result.toString(), attr);
      //
      m_tc.setCaretPosition(offset);
      //	ADebug.trace(ADebug.l6_Database, "Clear Thousands (" + m_format.toPattern() + ")" + content
      // + " -> " + result.toString());
      content = result.toString();
    } //	remove Thousands

    /**
     * ******************************************************************** Check Character entered
     */
    char c = string.charAt(0);
    if (Character.isDigit(c)) // c >= '0' && c <= '9')
    {
      //	ADebug.trace(ADebug.l6_Database, "Digit=" + c);
      super.insertString(offset, string, attr);
      return;
    }

    //	Decimal - remove other decimals
    //	Thousand - treat as Decimal
    else if (c == m_decimalSeparator || c == m_groupingSeparator || c == '.' || c == ',') {
      //	log.info("Decimal=" + c + " (ds=" + m_decimalSeparator + "; gs=" + m_groupingSeparator +
      // ")");
      //  no decimals on integers
      if (m_displayType == DisplayType.Integer) return;
      int pos = content.indexOf(m_decimalSeparator);

      //	put decimal in
      String decimal = String.valueOf(m_decimalSeparator);
      super.insertString(offset, decimal, attr);

      //	remove other decimals
      if (pos != 0) {
        content = getText();
        StringBuffer result = new StringBuffer();
        int correction = 0;
        for (int i = 0; i < content.length(); i++) {
          if (content.charAt(i) == m_decimalSeparator) {
            if (i == offset) result.append(content.charAt(i));
            else if (i < offset) correction++;
          } else result.append(content.charAt(i));
        }
        super.remove(0, content.length());
        super.insertString(0, result.toString(), attr);
        m_tc.setCaretPosition(offset - correction + 1);
      } //	remove other decimals
    } //	decimal or thousand

    //	something else
    else if (VNumber.AUTO_POPUP || "=+-/*%".indexOf(c) > -1) {

      //	Minus - put minus on start of string
      if (c == m_minusSign && offset == 0) {
        //	no minus possible
        if (m_displayType == DisplayType.Integer) return;
        //	add at start of string
        else super.insertString(0, "-", attr);
      } else {
        log.fine("Input=" + c + " (" + (int) c + ")");

        if (c == m_percentSign && offset > 0) {
          // don't convert integers to percent. 1% = 0?
          if (m_displayType == DisplayType.Integer) return;
          // divide by 100
          else {
            String value = getText();
            BigDecimal percentValue = new BigDecimal(0.0);
            try {
              if (value != null && value.length() > 0) {
                Number number = m_format.parse(value);
                percentValue = new BigDecimal(number.toString());
                percentValue =
                    percentValue.divide(
                        new BigDecimal(100.0),
                        m_format.getMaximumFractionDigits(),
                        BigDecimal.ROUND_HALF_UP);
                m_tc.setText(m_format.format(percentValue));
              }
            } catch (ParseException pe) {
              log.info("InvalidEntry - " + pe.getMessage());
            }
          }
        } else {

          String result =
              VNumber.startCalculator(m_tc, getText(), m_format, m_displayType, m_title, c);
          super.remove(0, content.length());

          // insertString(0, result, attr);
          m_tc.setText(result);
        }
      }
    } else ADialog.beep();
  } //	insertString
Example #2
0
  /**
   * Insert String
   *
   * @param offset offset
   * @param string string
   * @param attr attributes
   * @throws BadLocationException
   */
  @Override
  public void insertString(int offset, String string, AttributeSet attr)
      throws BadLocationException {
    log.finest(
        "Offset="
            + offset
            + ",String="
            + string
            + ",Attr="
            + attr
            + ",OldText="
            + getText()
            + ",OldLength="
            + getText().length());

    //	manual entry
    //	DBTextDataBinder.updateText sends stuff at once - length=8
    if (string != null && string.length() == 1) {
      //	ignore if too long
      if (offset >= m_mask.length()) return;

      //	is it an empty field?
      int length = getText().length();
      if (offset == 0 && length == 0) {
        Date today = new Date(System.currentTimeMillis());
        String dateStr = m_format.format(today);
        super.insertString(0, string + dateStr.substring(1), attr);
        m_tc.setCaretPosition(1);
        return;
      }

      //	is it a digit ?
      try {
        Integer.parseInt(string);
      } catch (Exception pe) {
        // hengsin, [ 1660175 ] Date field - anoying popup
        // startDateDialog();
        ADialog.beep();
        return;
      }

      //	try to get date in field, if invalid, get today's
      /*try
      {
      	char[] cc = getText().toCharArray();
      	cc[offset] = string.charAt(0);
      	m_format.parse(new String(cc));
      }
      catch (ParseException pe)
      {
      	startDateDialog();
      	return;
      }*/

      //	positioned before the delimiter - jump over delimiter
      if (offset != m_mask.length() - 1 && m_mask.charAt(offset + 1) == DELIMITER)
        m_tc.setCaretPosition(offset + 2);

      //	positioned at the delimiter
      if (m_mask.charAt(offset) == DELIMITER) {
        offset++;
        m_tc.setCaretPosition(offset + 1);
      }
      super.remove(offset, 1); // 	replace current position
    }

    //	Set new character
    super.insertString(offset, string, attr);
    //	New value set Cursor
    if (offset == 0 && string != null && string.length() > 1) m_tc.setCaretPosition(0);
  } //	insertString