示例#1
0
    private void indentText(JTextComponent textComponent) throws BadLocationException {
      String selectedText = textComponent.getSelectedText();
      int newLineIndex = selectedText != null ? selectedText.indexOf('\n') : -1;

      if (newLineIndex >= 0) {
        int originalSelectionStart = textComponent.getSelectionStart();
        int selectionStart = originalSelectionStart;
        int selectionEnd = textComponent.getSelectionEnd();

        int lastNewLineBeforeSelection = textComponent.getText(0, selectionStart).lastIndexOf('\n');
        int begin = lastNewLineBeforeSelection >= 0 ? lastNewLineBeforeSelection : 0;
        int end = selectionEnd;

        String text = textComponent.getText(begin, end - begin);
        int len = text.length();
        StringBuffer out = new StringBuffer(len);
        if (lastNewLineBeforeSelection < 0) {
          out.insert(0, '\t');
          selectionStart++;
          selectionEnd++;
        }
        for (int i = 0; i < len; i++) {
          char ch = text.charAt(i);
          out.append(ch);
          if (ch == '\n' && i < len - 1) {
            out.append("\t");
            selectionEnd++;
            if (begin + i < originalSelectionStart) {
              selectionStart++;
            }
          }
        }

        textComponent.select(begin, end);
        textComponent.replaceSelection(out.toString());

        textComponent.select(selectionStart, selectionEnd);
      } else {
        textComponent.replaceSelection("\t");
      }
    }
  /** Handle string insertion. */
  public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
    // Store source data

    int sourceLength = getLength();
    String sourceText = getText(0, sourceLength);
    StringBuffer strBuffer = new StringBuffer(sourceText);

    // Check if old value is zero

    if (offs == 0 && sourceLength > 0 && str.length() > 0) {
      long oldValue;

      try {
        oldValue = myFormat.parse(strBuffer.toString()).longValue();

        if (oldValue == 0) {
          strBuffer.deleteCharAt(0);
        }
      } catch (Exception e) {
      }
    }

    // Now add new string

    strBuffer.insert(offs, str);

    BigDecimal value;

    try {
      value = new BigDecimal(myFormat.parse(strBuffer.toString()).doubleValue());
    } catch (Exception e) {
      if (sourceLength > 0) {
        if (sourceText.startsWith(",")) {
          sourceText = "0" + sourceText;
        }

        value = new BigDecimal(getRealString(sourceText));
      } else {
        value = new BigDecimal(0.0);
      }
    }

    // Set the new value

    if (textField == null) {
      return;
    }

    textField.setValue(new Money(value, textField.getValue().getCurrency()), false);

    super.remove(0, sourceLength);
    super.insertString(0, myFormat.format(value.doubleValue()), a);

    // Set caret to correct caret position

    if (!"".equals(sourceText)) // <=> initilized
    {
      int lengthDiff = getLength() - sourceLength;
      int caretPos = lengthDiff + offs;
      int caretDiff = sourceLength - offs;

      // Adjust for columns after centSperator (currently Diff < 3)

      if ((caretDiff > 0 && caretDiff < 3) || (value.abs().longValue() < 10 && caretPos == 0)) {
        caretPos += 1;
      }

      if (caretPos < 0) {
        caretPos = 0;
      }

      textField.setCaretPosition(caretPos);
    } else {
      textField.setCaretPosition(1);
    }
  }