Пример #1
1
 public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
     throws BadLocationException {
   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);
       }
     }
   }
   super.insertString(fb, offset, builder.toString(), attr);
 }
 public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
     throws BadLocationException {
   StringBuilder builder = new StringBuilder(string);
   // 过滤用户输入的所有字符
   filterInt(builder);
   super.insertString(fb, offset, builder.toString(), attr);
 }
Пример #3
0
  public void insertString(FilterBypass fb, int offset, String str, AttributeSet a)
      throws BadLocationException {

    /* Queue a copy of the event and then modify the textarea */
    eventHistory.add(new TextInsertEvent(offset, str));
    super.insertString(fb, offset, str, a);
  }
  /**
   * remove
   *
   * @param fb
   * @param offset
   * @param length
   * @throws BadLocationException
   */
  @Override
  public void remove(final FilterBypass fb, final int offset, final int length)
      throws BadLocationException {

    if (offset >= promptPosition) {
      super.remove(fb, offset, length);
    }
  }
Пример #5
0
 /**
  * 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);
   }
 }
  /**
   * insertString
   *
   * @param fb
   * @param offset
   * @param string
   * @param attr
   * @throws BadLocationException
   */
  @Override
  public void insertString(
      final FilterBypass fb, final int offset, final String string, final AttributeSet attr)
      throws BadLocationException {

    if (offset >= promptPosition) {
      super.insertString(fb, offset, string, attr);
    }
  }
Пример #7
0
 /**
  * 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 insertString(
     DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
     throws BadLocationException {
   int length = fb.getDocument().getLength();
   if ((string.length() + length) <= maxLength) {
     super.insertString(fb, offset, string, attr);
   }
 }
Пример #8
0
  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);
  }
 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);
 }
Пример #10
0
  @Override
  public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder();
    sb.append(doc.getText(0, doc.getLength()));
    sb.delete(offset, offset + length);

    if (isInteger(sb.toString())) {
      super.remove(fb, offset, length);
    }
  }
Пример #11
0
  public void insertString(
      DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
      throws BadLocationException {

    // This is called by JTextArea.append() (for example).
    super.insertString(fb, offset, string, attr);

    // Make sure that the caret is at the end. For extra safety, this should probably be done
    // with EventQueue.invokeLater() just like as is done in CmdLineWindow.takeChar().
    theWindow.theText.setCaretPosition(theWindow.theText.getText().length());
  }
Пример #12
0
    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException {
      if ("\n".equals(str)) {
        str = addWhiteSpace(fb.getDocument(), offs);
        if (fb.getDocument().getText(offs - 2, 1).equals("{")) {
          str = str + "Meow";
        }
      }

      super.insertString(fb, offs, str, a);
    }
Пример #13
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 Toolkit.getDefaultToolkit().beep();
  }
Пример #14
0
  @Override
  public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
      throws BadLocationException {
    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder();
    sb.append(doc.getText(0, doc.getLength()));
    sb.insert(offset, string);

    if (test(sb.toString())) super.insertString(fb, offset, string, attr);
    else Toolkit.getDefaultToolkit().beep();
  }
 @Override
 public void replace(
     DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
     throws BadLocationException {
   String intToReplacet;
   if (!Utils.isInteger(text) && !text.equals(".")) {
     intToReplacet = "";
   } else {
     intToReplacet = text;
   }
   super.insertString(fb, offset, intToReplacet, attrs);
 }
Пример #16
0
  @Override
  public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder();
    sb.append(doc.getText(0, doc.getLength()));
    sb.delete(offset, offset + length);

    if (test(sb.toString())) {
      super.remove(fb, offset, length);
    } else {
      // warn the user and don't allow the insert
    }
  }
Пример #17
0
  public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
      throws BadLocationException {

    // Typically called for back-space.
    Document theDoc = fb.getDocument();
    int docLength = theDoc.getLength();

    if (docLength != offset + 1)
      // User trying to move the caret back and edit. Not allowed.
      return;

    super.remove(fb, offset, length);
  }
  /**
   * 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);
    }
  }
Пример #19
0
  @Override
  public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
      throws BadLocationException {
    Document doc = fb.getDocument();
    StringBuilder sb = new StringBuilder();
    sb.append(doc.getText(0, doc.getLength()));
    sb.insert(offset, string);

    if (test(sb.toString())) {
      super.insertString(fb, offset, string, attr);
    } else {
      // warn the user and don't allow the insert
    }
  }
  @Override
  public void insertString(
      FilterBypass filterBypass, int offset, 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(), 0)) {
      super.insertString(filterBypass, offset, text, attributeSet);
    } else {
      Toolkit.getDefaultToolkit().beep();
    }
  }
Пример #21
0
  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());
    }
  }
Пример #22
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();
    }
Пример #23
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);
 }
Пример #24
0
 @Override
 public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
     throws BadLocationException {
   if (filter == null) super.insertString(fb, offs, str, a);
   else filter.insertString(fb, offs, str, a);
 }
Пример #25
0
 @Override
 public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
     throws BadLocationException {
   if (filter == null) super.remove(fb, offset, length);
   else filter.remove(fb, offset, length);
 }
Пример #26
0
    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);
    }
Пример #27
0
 @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);
 }
Пример #28
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);
    }
Пример #29
0
 public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
   /* Queue a copy of the event and then modify the textarea */
   eventHistory.add(new TextRemoveEvent(offset, length));
   super.remove(fb, offset, length);
 }