/**
  * If the resulting length of the insert will be less than or equal to the allowed length, then
  * allow it, otherwise ignore it.
  */
 @Override
 public void replace(
     DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
     throws BadLocationException {
   if ((fb.getDocument().getLength() + text.length() - length) <= maxLength) {
     super.replace(fb, offset, length, text, attrs);
   }
 }
 public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr)
     throws BadLocationException {
   if (string != null) {
     StringBuilder builder = new StringBuilder(string);
     // 过滤用户替换的所有字符
     filterInt(builder);
     string = builder.toString();
   }
   super.replace(fb, offset, length, string, attr);
 }
  public void replace(FilterBypass fb, int offset, int length, String str, AttributeSet a)
      throws BadLocationException {

    /* Queue a copy of the event and then modify the text */
    if (length > 0) {
      eventHistory.add(new TextRemoveEvent(offset, length));
    }
    eventHistory.add(new TextInsertEvent(offset, str));
    super.replace(fb, offset, length, str, a);
  }
  @Override
  public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
      throws BadLocationException {
    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder();
    sb.append(doc.getText(0, doc.getLength()));
    sb.replace(offset, offset + length, text);

    if (test(sb.toString())) super.replace(fb, offset, length, text, attrs);
    else Toolkit.getDefaultToolkit().beep();
  }
  /**
   * replace
   *
   * @param fb
   * @param offset
   * @param length
   * @param text
   * @param attrs
   * @throws BadLocationException
   */
  @Override
  public void replace(
      final FilterBypass fb,
      final int offset,
      final int length,
      final String text,
      final AttributeSet attrs)
      throws BadLocationException {

    if (offset >= promptPosition) {
      super.replace(fb, offset, length, text, attrs);
    }
  }
Exemple #6
0
  @Override
  public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
      throws BadLocationException {
    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder();
    sb.append(doc.getText(0, doc.getLength()));
    sb.replace(offset, offset + length, text);

    if (test(sb.toString())) {
      super.replace(fb, offset, length, text, attrs);
    } else {
      // warn the user and don't allow the insert
    }
  }
Exemple #7
0
 public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr)
     throws BadLocationException {
   if (string != null) {
     StringBuilder builder = new StringBuilder(string);
     for (int i = builder.length() - 1; i >= 0; i--) {
       int cp = builder.codePointAt(i);
       if (!Character.isDigit(cp) && cp != '-') {
         builder.deleteCharAt(i);
         if (Character.isSupplementaryCodePoint(cp)) {
           i--;
           builder.deleteCharAt(i);
         }
       }
     }
     string = builder.toString();
   }
   super.replace(fb, offset, length, string, attr);
 }
  @Override
  public void replace(
      DocumentFilter.FilterBypass filterBypass,
      int offset,
      int length,
      String text,
      AttributeSet attributeSet)
      throws BadLocationException {
    text = trimHexadecimal(text);
    Document document = filterBypass.getDocument();
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(document.getText(0, document.getLength()));
    stringBuilder.insert(offset, text);

    if (isValidInput(stringBuilder.toString(), length)) {
      super.replace(filterBypass, offset, length, text, attributeSet);
    } else {
      Toolkit.getDefaultToolkit().beep();
    }
  }
  public void replace(
      DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
      throws BadLocationException {

    // This is called when the user does any typing.
    Document theDoc = fb.getDocument();
    int docLength = theDoc.getLength();

    if (docLength != offset)
      // User trying to move the caret back and insert. This is not allowed.
      // Only allow typing at the end of a line.
      return;

    super.replace(fb, offset, length, text, attrs);

    // If the user just typed return, then tell theWindow.theListener what was typed.
    if ((theWindow.theListener != null) && (text.equals("\n") == true)) {
      int len = theWindow.theText.getText().length() - theWindow.getLastTake();
      String typed = theWindow.theText.getText(theWindow.getLastTake(), len);

      // Theoretically the call to takeLine() could return false (though it's very
      // unlikely), in which case this thread should hold typed and try again. This kind of
      // loop so deep in the document-handling process is not really a good thing because
      // it could prevent windows from updating while waiting for this to be resolved, but
      // it should be OK in this case.
      boolean tookIt = false;
      while (tookIt == false) {
        tookIt = theWindow.theListener.takeLine(typed);
        if (tookIt == false) {
          try {
            Thread.sleep(5);
          } catch (Exception e) {
            // Do nothing.
          }
        }
      }

      // Since theListener has heard about this text, move the lastTakePos.
      theWindow.updateLastTake(theWindow.theText.getText().length());
    }
  }
Exemple #10
0
    @Override
    public void replace(
        DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset)
        throws BadLocationException {
      Document doc = fp.getDocument();
      String oldText = doc.getText(0, doc.getLength());
      StringBuilder sb = new StringBuilder(oldText);
      sb.replace(offset, offset + length, oldText);

      int len = string.length();
      boolean isValidInteger = true;

      for (int i = 0; i < len; i++) {
        if (!Character.isDigit(string.charAt(i))) {
          isValidInteger = false;
          break;
        }
      }
      if (isValidInteger && verifyText(sb.toString())) {
        super.replace(fp, offset, length, string, aset);
      } else Toolkit.getDefaultToolkit().beep();
    }
    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
        throws BadLocationException {
      if ("\n".equals(str)) str = addWhiteSpace(fb.getDocument(), offs);

      super.replace(fb, offs, length, str, a);
    }
Exemple #12
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);
 }
 @Override
 public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
     throws BadLocationException {
   if (filter == null) super.replace(fb, offs, length, str, a);
   else filter.replace(fb, offs, length, str, a);
 }