Пример #1
0
    private void outdentText(JTextComponent textComponent) throws BadLocationException {
      int tabSize =
          ((Integer) textComponent.getDocument().getProperty(PlainDocument.tabSizeAttribute))
              .intValue();

      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);
        if (lastNewLineBeforeSelection < 0) {
          text = "\n" + text;
        }
        int len = text.length();
        StringBuffer out = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
          char ch = text.charAt(i);
          out.append(ch);
          if (ch == '\n' && i < len - 1) {
            char next = text.charAt(i + 1);
            int stripCount = 0;
            if (next == '\t') {
              stripCount = 1;
            } else {
              for (; stripCount < tabSize && i + 1 + stripCount < len; stripCount++) {
                next = text.charAt(i + 1 + stripCount);
                if (next != ' ' && next != '\t') {
                  break;
                }
              }
            }

            selectionEnd -= stripCount;
            if (i + begin < originalSelectionStart - 1) {
              selectionStart -= stripCount;
            }
            i += stripCount;
          }
        }

        textComponent.select(begin, end);
        textComponent.replaceSelection(
            lastNewLineBeforeSelection < 0 ? out.toString().substring(1) : out.toString());

        textComponent.select(selectionStart, selectionEnd);
      }
    }
Пример #2
0
  /** See if edit value is valid, put error message in buff. */
  protected boolean _validate(StringBuffer buff) {
    String editValue = tf.getText().trim();
    if (editValue.length() == 0) return true; // empty ok

    try {
      new TimeDuration(tf.getText());
      return true;
    } catch (java.text.ParseException e) {
      buff.append(label).append(": ").append(e.getMessage());
      return false;
    }
  }
Пример #3
0
  // get current value from editComponent
  protected Object getEditValue() {
    String editValue = tf.getText().trim();
    if (editValue.length() == 0) return null; // empty ok

    try {
      return new TimeDuration(editValue);
    } catch (java.text.ParseException e) {
      return null;
    }
  }
Пример #4
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");
      }
    }
  public static void setHexadecimalInputFilter(
      JTextComponent textComponent, int maximumInputLength) {
    PlainDocument plainDocument = (PlainDocument) textComponent.getDocument();
    plainDocument.setDocumentFilter(new HexadecimalInputFilter(maximumInputLength));

    String text = textComponent.getText();
    text = trimHexadecimal(text);

    if (text.length() > maximumInputLength) {
      textComponent.setText(text.substring(text.length() - maximumInputLength, text.length()));
    } else {
      while (text.length() < maximumInputLength) {
        text = "0" + text;
      }

      textComponent.setText(text);
    }
  }
 String getText() {
   return textComponent.getText();
 }
Пример #7
0
 @Override
 public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as)
     throws BadLocationException {
   for (int n = string.length(); n > 0; n--) {
     char c = string.charAt(n - 1);
     switch (filterType) {
       case ALPHA:
         if ((Character.isAlphabetic(c))
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case NUMERIC:
         if ((Character.isDigit(c))
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case ALPHANUMERIC:
         if ((Character.isAlphabetic(c) || Character.isDigit(c))
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case ALPHA_SPACE:
         if ((Character.isAlphabetic(c) || c == ' ')
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case NUMERIC_SPACE:
         if ((Character.isDigit(c) || c == ' ')
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case ALPHANUMERIC_SPACE:
         if ((Character.isAlphabetic(c) || Character.isDigit(c) || c == ' ')
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case NO_HTML:
         if ((!isOneOfChars(c, '<', '>', '&'))
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case EMAIL:
         if ((Character.isAlphabetic(c)
                 || Character.isDigit(c)
                 || isOneOfChars(
                     c, '.', '!', '@', '_', '-', '+', '#', '$', '?', '=', '%', '\'', '*', '^',
                     '`', '{', '|', '}', '~'))
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       case FILENAME:
         if ((Character.isAlphabetic(c) || Character.isDigit(c) || isOneOfChars(c, '_', '-'))
             && (maxLength < 0 || this.textComponent.getText().length() < maxLength))
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
       default:
         if (maxLength < 0 || this.textComponent.getText().length() < maxLength)
           super.replace(fb, i, i1, String.valueOf(c), as);
         break;
     }
   }
   if (listener != null) listener.lengthChanged(textComponent.getText().length(), textComponent);
 }
Пример #8
0
    @Override
    public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
      super.remove(fb, i, i1);

      if (listener != null) listener.lengthChanged(textComponent.getText().length(), textComponent);
    }