Пример #1
0
 /** {@inheritDoc} */
 public void insertString(
     DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr)
     throws BadLocationException {
   int previousLength = fb.getDocument().getLength();
   fb.insertString(offset, text.replaceAll("[^0-9]", ""), attr);
   trimPosition(fb, text, offset, previousLength);
 }
Пример #2
0
  private void updateCaretPosition(DocumentFilter.FilterBypass fb) throws BadLocationException {
    String allText = fb.getDocument().getText(0, fb.getDocument().getLength());
    int index = allText.indexOf(':');
    if (index != -1) {
      int minuteLength = allText.length() - index - 1;
      int hourLength = index;
      int caretPosition = tf.getCaretPosition();

      if (minuteLength >= 2 && caretPosition == allText.length()) {
        tf.setCaretPosition(0);
      } else if (hourLength == caretPosition) {
        if (hourLength >= 2) {
          tf.setCaretPosition(3);
        } else if (hourLength == 1) {
          char c = allText.charAt(0);
          if (c != '0' && c != '1' && c != '2') {
            tf.setCaretPosition(2);
          }
        }
      } else if (hourLength + 1 == caretPosition) {
        if (hourLength == 1) {
          char c = allText.charAt(0);
          if (c == '0' || c == '1' || c == '2') {
            tf.setCaretPosition(caretPosition - 1);
          }
        } else if (hourLength == 0) {
          tf.setCaretPosition(caretPosition - 1);
        }
      }
    }
    if (allText.length() == 1) {
      tf.setCaretPosition(0);
    }
  }
 public void replace(
     DocumentFilter.FilterBypass filtby, int ofs, int length, String text, AttributeSet attr)
     throws BadLocationException {
   if (filtby.getDocument().getLength() + text.length() - length <= maxLength) {
     filtby.replace(ofs, length, text, attr);
   } else {
     Toolkit.getDefaultToolkit().beep();
   }
 }
 public void insertString(
     DocumentFilter.FilterBypass filtby, int ofs, String text, AttributeSet attrSet)
     throws BadLocationException {
   if (filtby.getDocument().getLength() + text.length() <= maxLength) {
     filtby.insertString(ofs, text, attrSet);
   } else {
     Toolkit.getDefaultToolkit().beep();
   }
 }
 public void insertString(DocumentFilter.FilterBypass fb, int offs, String str, AttributeSet a)
     throws BadLocationException {
   if (isValidPostiveNumber(true, fb, offs, str, 0)) {
     fb.insertString(offs, str, a);
     doValueUpdate(fb);
   }
 } // End of insert
 public void remove(DocumentFilter.FilterBypass fb, int offs, int length)
     throws BadLocationException {
   String str = "";
   if (isValidPostiveNumber(false, fb, offs, str, length)) {
     fb.remove(offs, length);
     doValueUpdate(fb);
   }
 } // End of remove
 public void replace(
     DocumentFilter.FilterBypass fb, int offs, int length, String str, AttributeSet a)
     throws BadLocationException {
   if (isValidPostiveNumber(false, fb, offs, str, length)) {
     fb.replace(offs, length, str, a);
     doValueUpdate(fb);
   }
 } // End of replace
Пример #8
0
 /** {@inheritDoc} */
 public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
     throws BadLocationException {
   String text = fb.getDocument().getText(offset, length);
   int index = text.indexOf(":");
   if (index == -1) {
     fb.remove(offset, length);
   } else {
     // index value is relative to offset
     if (index > 0) {
       fb.remove(offset, index);
     }
     if (index < length - 1) {
       fb.remove(offset + index + 1, length - index - 1);
     }
   }
   updateCaretPosition(fb);
 }
Пример #9
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);
   }
 }
 /** Overriden to unconditionally allow the replace if ignoreDocumentMutate is true. */
 void replace(
     DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
     throws BadLocationException {
   if (ignoreDocumentMutate) {
     fb.replace(offset, length, text, attrs);
     return;
   }
   super.replace(fb, offset, length, text, attrs);
 }
Пример #11
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);
   }
 }
Пример #12
0
  private void trimPosition(
      DocumentFilter.FilterBypass fb, String newText, int offset, int previousLength)
      throws BadLocationException {
    String allText = fb.getDocument().getText(0, fb.getDocument().getLength());
    int index = allText.indexOf(':');
    if (index != -1 && newText.length() == 1) {
      int minuteLength = allText.length() - index - 1;
      int hourLength = index;

      if (minuteLength > 2 || hourLength > 2) {
        if (offset < previousLength) {
          fb.remove(offset + 1, 1);
        } else {
          fb.remove(previousLength, 1);
        }
      }
    }
    updateCaretPosition(fb);
  }
Пример #13
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);
  }
 public void remove(DocumentFilter.FilterBypass fb, int offs, int length)
     throws BadLocationException {
   // Insertion is just replacement of a 0 lenght string.
   // Delete is a kind of insertion.
   // It's just replacement of non-zero lenght string with a 0 lenght string.
   // So we will insert a 0 length text.
   String str = "";
   // And we pass in false for insertion which means replacement.
   if (isValidTemperature(false, fb, offs, str, length)) {
     fb.remove(offs, length);
     doValueUpdate(fb);
   }
 } // End of remove
  protected void updateByOpTree(javax.swing.text.DocumentFilter.FilterBypass fb) {
    String oldStr = JMathTextField.this.getText();
    String newStr = operatorTree.toString();
    int commonStart = Utils.equalStartLen(oldStr, newStr);
    int commonEnd = Utils.equalEndLen(oldStr, newStr);
    if (commonEnd + commonStart >= Math.min(oldStr.length(), newStr.length()))
      commonEnd = Math.min(oldStr.length(), newStr.length()) - commonStart;

    try {
      fb.replace(
          commonStart,
          oldStr.length() - commonEnd - commonStart,
          newStr.substring(commonStart, newStr.length() - commonEnd),
          null);
    } catch (BadLocationException e) {
      // this should not happen. we should have checked this. this whole function should be safe
      throw new AssertionError(e);
    }
  }
Пример #16
0
  /** {@inheritDoc} */
  public void replace(
      DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr)
      throws BadLocationException {
    int previousLength = fb.getDocument().getLength();

    String t = fb.getDocument().getText(offset, length);
    int index = t.indexOf(":");
    fb.replace(offset, length, text.replaceAll("[^0-9]", ""), attr);
    if (index != -1) {
      if (fb.getDocument().getLength() >= 2) {
        fb.insertString(2, ":", attr);
      } else {
        fb.insertString(fb.getDocument().getLength(), ":", attr);
      }
    }

    trimPosition(fb, text, offset, previousLength);
  }
  @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();
    }
  }
Пример #18
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());
    }
  }
Пример #19
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();
    }
Пример #20
0
 public void replace(
     DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr)
     throws BadLocationException {
   fb.replace(offset, length, text.toUpperCase(), attr);
 }
Пример #21
0
 public void insertString(
     DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr)
     throws BadLocationException {
   fb.insertString(offset, text.toUpperCase(), attr);
 }