Java 类javax.swing.text.DocumentFilter 实例源码

项目:incubator-netbeans    文件:BaseDocument.java   
/** Inserts string into document */
    public @Override void insertString(int offset, String text, AttributeSet attrs)
    throws BadLocationException {
//        if (LOG_EDT.isLoggable(Level.FINE)) { // Only permit operations in EDT
//            // Disabled due to failing OpenEditorEnablesEditMenuFactoryTest
//            if (!SwingUtilities.isEventDispatchThread()) {
//                throw new IllegalStateException("BaseDocument.insertString not in EDT: offset=" + // NOI18N
//                        offset + ", text=" + org.netbeans.lib.editor.util.CharSequenceUtilities.debugText(text)); // NOI18N
//            }
//        }

        // Always acquire atomic lock (it simplifies processing and improves readability)
        atomicLockImpl();
        try {
            checkModifiable(offset);
            DocumentFilter filter = getDocumentFilter();
            if (filter != null) {
                filter.insertString(getFilterBypass(), offset, text, attrs);
            } else {
                handleInsertString(offset, text, attrs);
            }
        } finally {
            atomicUnlockImpl(true);
        }
    }
项目:incubator-netbeans    文件:BaseDocument.java   
/** Removes portion of a document */
    public @Override void remove(int offset, int length) throws BadLocationException {
//        if (LOG_EDT.isLoggable(Level.FINE)) { // Only permit operations in EDT
//            if (!SwingUtilities.isEventDispatchThread()) {
//                throw new IllegalStateException("BaseDocument.insertString not in EDT: offset=" + // NOI18N
//                        offset + ", len=" + length); // NOI18N
//            }
//        }

        // Always acquire atomic lock (it simplifies processing and improves readability)
        atomicLockImpl();
        try {
            checkModifiable(offset);
            DocumentFilter filter = getDocumentFilter();
            if (filter != null) {
                filter.remove(getFilterBypass(), offset, length);
            } else {
                handleRemove(offset, length);
            }
        } finally {
            atomicUnlockImpl(true);
        }
    }
项目:incubator-netbeans    文件:BaseDocument.java   
public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
    // Always acquire atomic lock (it simplifies processing and improves readability)
    atomicLockImpl();
    try {
        checkModifiable(offset);
        DocumentFilter filter = getDocumentFilter();
        if (filter != null) {
            filter.replace(getFilterBypass(), offset, length, text, attrs);
        } else {
            handleRemove(offset, length);
            handleInsertString(offset, text, attrs);
        }
    } finally {
        atomicUnlockImpl(true);
    }
}
项目:LGoodDatePicker    文件:JIntegerTextField.java   
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
        throws BadLocationException {
    if (skipFiltersWhileTrue) {
        super.remove(fb, offset, length);
        return;
    }
    String oldText = fb.getDocument().getText(0, fb.getDocument().getLength());
    StringBuilder newTextBuilder = new StringBuilder(oldText);
    newTextBuilder.delete(offset, (offset + length));
    String newText = newTextBuilder.toString();
    if (newText.trim().isEmpty() || oldText.equals("-1")) {
        setFieldToDefaultValue();
    } else if (allowNegativeNumbers() && newText.trim().equals("-")) {
        setFieldToNegativeOne();
    } else if (isValidInteger(newText)) {
        super.remove(fb, offset, length);
    } else {
        Toolkit.getDefaultToolkit().beep();
    }
}
项目:Enigma-machine-simulator    文件:PlugboardPanel.java   
public PlugboardPanel() {

    // Create component
    pbField = new JTextField(95);
    pbField.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.gray), BorderFactory.createEmptyBorder(5, 5, 5, 5)));

    // Adding border
    Border outerBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
    innerBorder = BorderFactory.createTitledBorder("Plugboard (e.g. AB CD) - Press \"SAVE\"");
    setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));

    // Add component
    add(pbField);

    // Filter input to upper case only
    ( (AbstractDocument) pbField.getDocument() ).setDocumentFilter(new DocumentFilter(){
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            for (int i = 0; i < text.length(); i++) 
                if( (text.charAt(i) < 'a' || text.charAt(i) > 'z') && (text.charAt(i) < 'A' || text.charAt(i) > 'Z') && text.charAt(0) != ' ')
                    return;
            super.replace(fb, offset, length, text.toUpperCase(), attrs);
        }
    });
}
项目:groovy    文件:StructuredSyntaxDocumentFilter.java   
/**
 * Replace a string in the document, and then parse it if the parser has been
 * set.
 *
 * @param fb
 * @param offset
 * @param length
 * @param text
 * @param attrs
 * @throws BadLocationException
 */    
public void replace(DocumentFilter.FilterBypass fb, int offset, 
                    int length, String text, AttributeSet attrs)
    throws BadLocationException
{
    // text might be null and indicates no replacement text
    if (text == null) text = "";

    // remove problem meta characters returns
    text = replaceMetaCharacters(text);

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

    // start on the text that was replaced
    parseDocument(offset, text.length());
}
项目:thingweb-gui    文件:AbstractDocumentFilter.java   
public void insertString(DocumentFilter.FilterBypass fb, int offset,
        String string, AttributeSet attr) throws BadLocationException {

    if (string == null) {
        return;
    } else {
        String newValue;
        Document doc = fb.getDocument();
        int length = doc.getLength();
        if (length == 0) {
            newValue = string;
        } else {
            String currentContent = doc.getText(0, length);
            StringBuilder currentBuffer = new StringBuilder(
                    currentContent);
            currentBuffer.insert(offset, string);
            newValue = currentBuffer.toString();
        }
        currentValue = checkInput(newValue, offset);
        fb.insertString(offset, string, attr);
    }
}
项目:THINK-VPL    文件:SpecialEditorPane.java   
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,
    AttributeSet attr) throws BadLocationException {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < text.length(); i++){
        char c = text.charAt(i);
        boolean isLegal = true;
        for(char illegal : illegalChars){
            if(c == illegal){
                isLegal = false;
                break;
            }
        }
        if(isLegal){
            sb.append(c);
        }
    }
    text = sb.toString();
    super.insertString(fb, offset, text, attr);
}
项目:THINK-VPL    文件:VBoolean.java   
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
    AttributeSet attrs) throws BadLocationException {
    //String curentText = doc.getText(0, Math.min(4, doc.getLength()));
    /*if(doc.getLength() >= 4 && curentText.equals("true")){
        text = "true";
        offset = 0;
        length = doc.getLength();
    }else if(doc.getLength() >= 5){
        text = "false";
        offset = 0;
        length = doc.getLength();
    }*/
    if(doc.getText(0,Math.min(1, doc.getLength())).equals("t") || doc.getText(0,Math.min(1, doc.getLength())).equals("T") || (text.equals("t")) || (text.equals("T")) || (text.equals("true"))){
        text = "true";
        offset = 0;
        length = doc.getLength();
    }else{
        text = "false";
        offset = 0;
        length = doc.getLength();
    }
    super.replace(fb, offset, length, text, attrs);
}
项目:MathEOS    文件:JMathTextPane.java   
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str, AttributeSet attrs) throws BadLocationException {
    //XXX Attention : par défaut, dans Swing, insertString et remove appellent tous les deux replace, et non l'inverse
    if(str!=null && str.length()!=0) {//y aura-t-il une insertion ?
        if(length>0) {//il y aura aussi un remove
            undo.valider();
            flagRemove=true; remove(fb, offset, length);//remove sans valider le groupe d'edit
            flagInsert=true; insertString(fb, offset, str, attrs);//insert sans valider le groupe d'edit
            flagInsert=false; flagRemove=false;
        } else {//il s'agissait d'un insert déguisé
            insertString(fb, offset, str, attrs);
        }
    } else {//il s'agissait d'un remove déguisé
        remove(fb, offset, length);
    }
}
项目:opensim-gui    文件:JConsole.java   
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
        throws BadLocationException {
    if (useFilters) {
        // determine if we can insert
        if (console.getSelectionStart() >= console.editStart) {
            // can insert
            fb.insertString(offset, string, attr);
        } else {
            // insert at the end of the document
            fb.insertString(console.getText().length(), string, attr);
            // move cursor to the end
            console.getCaret().setDot(console.getText().length());
            // console.setSelectionEnd(console.getText().length());
            // console.setSelectionStart(console.getText().length());
        }
    } else {
        fb.insertString(offset, string, attr);
    }
}
项目:opensim-gui    文件:JConsole.java   
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
        throws BadLocationException {
    if (useFilters) {
        // determine if we can replace
        if (console.getSelectionStart() >= console.editStart) {
            // can replace
            fb.replace(offset, length, text, attrs);
        } else {
            // insert at end
            fb.insertString(console.getText().length(), text, attrs);
            // move cursor to the end
            console.getCaret().setDot(console.getText().length());
            // console.setSelectionEnd(console.getText().length());
            // console.setSelectionStart(console.getText().length());
        }
    } else {
        fb.replace(offset, length, text, attrs);
    }
}
项目:opensim-gui    文件:JConsole.java   
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
    if (useFilters) {
        if (offset > console.editStart) {
            // can remove
            fb.remove(offset, length);
        } else {
            // only remove the portion that's editable
            fb.remove(console.editStart, length - (console.editStart - offset));
            // move selection to the start of the editable section
            console.getCaret().setDot(console.editStart);
            // console.setSelectionStart(console.editStart);
            // console.setSelectionEnd(console.editStart);
        }
    } else {
        fb.remove(offset, length);
    }
}
项目:academia_biometria    文件:DecimalNumberDocumentFilter.java   
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String string, AttributeSet attrs) throws BadLocationException {
    Document document = fb.getDocument();
    if (length > 0) {
        fb.remove(offset, length);
    }
    for (int i = 0; i < string.length(); i++) {
        char charNow = string.charAt(i);
        if (Character.isDigit(charNow)) {
            fb.replace(offset, 0, String.valueOf(charNow), attrs);
        } else if (charNow == '.' || charNow == ',') {
            if (!document.getText(0, document.getLength()).contains(".")) {
                fb.replace(offset, 0, ".", attrs);

            }
        }
        offset++;
    }
}
项目:tools4j-validator    文件:ValidationDocumentFilter.java   
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
    String content = fb.getDocument().getText(0, fb.getDocument().getLength());
    StringBuffer sb = new StringBuffer();
    if (offset < fb.getDocument().getLength()) {
        if (offset>0) sb.append(content.substring(0, offset));
        sb.append(string);
        sb.append(content.substring(offset, content.length()));
    }
    else {
        sb.append(content);
        sb.append(string);
    }

    super.insertString(fb, offset, string, attr);

    boolean success;
    if (!(success=validator.validateString(window, sb.toString())))
        super.remove(fb, offset, string.length());

    if (output!=null)
        notifyOutput(success);
}
项目:firebird-vizualization-tool    文件:DocumentFilterFactory.java   
public static DocumentFilter buildFilter(DocumentFilterType type, boolean notNull) {

    DocumentFilter filter = new DocumentFilter();

    switch(type) {

        case INT:
            filter = new IntFilter(notNull);
        break;

        case DATABASE_COLUMN_NAME_LENGTH:
            filter = new DatabaseColumnNameLengthFilter();
        break;
    }

    return filter;
}
项目:First-Fruits    文件:SimpleCurrencyFilter.java   
public void insertString(DocumentFilter.FilterBypass fb, int offset,
                String text, AttributeSet attr) throws BadLocationException
{
    final String currentText = fb.getDocument().getText(0, fb.getDocument().getLength());
    final StringBuilder sb = new StringBuilder(currentText);
    sb.insert(offset, text);
    {
        final Matcher positiveMatcher = positiveCurrencyPattern.matcher(sb.toString());
        if (positiveMatcher.matches()) {
            fb.insertString(offset, text, attr);
        } else if (allowNegative) {
            final Matcher negativeMatcher = negativeCurrencyPattern.matcher(sb.toString());
            if (negativeMatcher.matches() || startNegativeDecimal.equals(sb.toString())) {
                fb.insertString(offset, text, attr);
            }
        }
    }
}
项目:First-Fruits    文件:SimpleCurrencyFilter.java   
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
                String text, AttributeSet attr) throws BadLocationException
{
    final String currentText = fb.getDocument().getText(0, fb.getDocument().getLength());
    final StringBuilder sb = new StringBuilder(currentText);
    sb.replace(offset, offset + length, text);
    final Matcher positiveMatcher = positiveCurrencyPattern.matcher(sb.toString());
    if (positiveMatcher.matches()) {
        fb.replace(offset, length, text, attr);
    } else if (allowNegative) {
        final Matcher negativeMatcher = negativeCurrencyPattern.matcher(sb.toString());
        if (negativeMatcher.matches() || sb.toString().matches(startNegativeDecimal)) {
            fb.replace(offset, length, text, attr);
        }
    }
}
项目:cn1    文件:JFormattedTextField_AbstractFormatterTest.java   
public void testInstallUninstall_Filters() {
    NavigationFilter navFilter = new NavigationFilter();
    formatter.setNavigationFilter(navFilter);
    DocumentFilter docFilter = new DocumentFilter();
    formatter.setDocumentFilter(docFilter);
    AbstractDocument doc = (AbstractDocument) tf.getDocument();
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
    formatter.install(tf);
    assertEquals(navFilter, tf.getNavigationFilter());
    assertEquals(docFilter, doc.getDocumentFilter());
    formatter.uninstall();
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
    formatter.install(tf);
    assertEquals(navFilter, tf.getNavigationFilter());
    assertEquals(docFilter, doc.getDocumentFilter());
    formatter.install(null);
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
}
项目:freeVM    文件:JFormattedTextField_AbstractFormatterTest.java   
public void testInstallUninstall_Filters() {
    NavigationFilter navFilter = new NavigationFilter();
    formatter.setNavigationFilter(navFilter);
    DocumentFilter docFilter = new DocumentFilter();
    formatter.setDocumentFilter(docFilter);
    AbstractDocument doc = (AbstractDocument) tf.getDocument();
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
    formatter.install(tf);
    assertEquals(navFilter, tf.getNavigationFilter());
    assertEquals(docFilter, doc.getDocumentFilter());
    formatter.uninstall();
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
    formatter.install(tf);
    assertEquals(navFilter, tf.getNavigationFilter());
    assertEquals(docFilter, doc.getDocumentFilter());
    formatter.install(null);
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
}
项目:freeVM    文件:JFormattedTextField_AbstractFormatterTest.java   
public void testInstallUninstall_Filters() {
    NavigationFilter navFilter = new NavigationFilter();
    formatter.setNavigationFilter(navFilter);
    DocumentFilter docFilter = new DocumentFilter();
    formatter.setDocumentFilter(docFilter);
    AbstractDocument doc = (AbstractDocument) tf.getDocument();
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
    formatter.install(tf);
    assertEquals(navFilter, tf.getNavigationFilter());
    assertEquals(docFilter, doc.getDocumentFilter());
    formatter.uninstall();
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
    formatter.install(tf);
    assertEquals(navFilter, tf.getNavigationFilter());
    assertEquals(docFilter, doc.getDocumentFilter());
    formatter.install(null);
    assertNull(tf.getNavigationFilter());
    assertNull(doc.getDocumentFilter());
}
项目:lexml-swing-editorhtml    文件:AutoCurvedQuotesBehavior.java   
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
        String text, AttributeSet attrs, DocumentFilterChain chain)
        throws BadLocationException {

    if(text.equals("\"")) {
        fb.replace(offset, length, text, attrs);
        boolean open = shouldOpenQuote(doc, offset);
        String curvedQuote = open? "“": "”";
        if(open) {
            attrs = doc.getCharacterElement(offset + 1).getAttributes();
        }
        chain.replace(fb, offset, 1, curvedQuote, attrs);
        return;
    }

    chain.replace(fb, offset, length, text, attrs);
}
项目:incubator-netbeans    文件:ClassNameTextField.java   
@Override
public void insertString(DocumentFilter.FilterBypass fb,
                         int offset,
                         String string,
                         AttributeSet attr) throws BadLocationException {
    String strToAdd = removeSpaces(string);
    if (strToAdd != null) {
        super.insertString(fb, offset, strToAdd, null);
    }
}
项目:incubator-netbeans    文件:SimpleTestStepLocation.java   
@Override
public void replace(DocumentFilter.FilterBypass bypass,
                    int offset,
                    int length,
                    String text,
                    AttributeSet attrs) throws BadLocationException {
    if (!programmaticChange && srcGroupNameDisplayed) {
        removeSrcGroupName(bypass);
    }
    super.replace(bypass, offset, length, text, attrs);
}
项目:incubator-netbeans    文件:SimpleTestStepLocation.java   
@Override
public void insertString(
                    DocumentFilter.FilterBypass bypass,
                    int offset,
                    String string,
                    AttributeSet attr) throws BadLocationException {
    if (!programmaticChange && srcGroupNameDisplayed) {
        removeSrcGroupName(bypass);
    }
    super.insertString(bypass, offset, string, attr);
}
项目:incubator-netbeans    文件:SimpleTestStepLocation.java   
@Override
public void remove(DocumentFilter.FilterBypass bypass,
                   int offset,
                   int length) throws BadLocationException {
    if (!programmaticChange && srcGroupNameDisplayed) {
        removeSrcGroupName(bypass);
    }
    super.remove(bypass, offset, length);
}
项目:incubator-netbeans    文件:SimpleTestStepLocation.java   
private void removeSrcGroupName(DocumentFilter.FilterBypass bypass)
                                        throws BadLocationException {
    bypass.remove(classNameLength,
                  tfClassToTest.getText().length() - classNameLength);
    srcGroupNameDisplayed = false;
    setNavigationFilterEnabled(false);
}
项目:Mujeed-Arabic-Prolog    文件:NonEditableLineDocumentFilter.java   
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if(string == null) {
     return;
   }else{
     replace(fb, offset, 0, string, attr);
   }
 }
项目:Mujeed-Arabic-Prolog    文件:NonEditableLineDocumentFilter.java   
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,String text, AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
   Element root = doc.getDefaultRootElement();
   int count = root.getElementCount();
   int index = root.getElementIndex(offset);
   Element cur = root.getElement(index);
   int promptPosition = cur.getStartOffset()+PROMPT.length();
   if(index==count-1 && offset-promptPosition>=0) {
     if(text.equals("\n")) {
       text = "\n"+PROMPT;
     }
     fb.replace(offset, length, text, attrs);
   }
}
项目:balloonist    文件:FirstCharacterHelpingDocumentFilter.java   
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, javax.swing.text.AttributeSet attr) throws BadLocationException
{
    // System.out.println("inserting \""+string+"\" w. attr: "+attr + " @ "+offset);

    attr = fix(attr, offset);
    super.insertString(fb, offset, string, attr);
}
项目:balloonist    文件:FirstCharacterHelpingDocumentFilter.java   
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
{
    // System.out.println("replacing \""+text+"\" w. attrs: "+attrs + " @ "+offset);

    attrs = fix(attrs, offset);
    super.replace(fb, offset, length, text, attrs);
}
项目:j2se_for_android    文件:JFormattedTextField.java   
/**
 * Installs the <code>DocumentFilter</code> <code>filter</code> onto the
 * current <code>JFormattedTextField</code>.
 * 
 * @param filter
 *            DocumentFilter to install on the Document.
 */
private void installDocumentFilter(DocumentFilter filter) {
    JFormattedTextField ftf = getFormattedTextField();

    if (ftf != null) {
        Document doc = ftf.getDocument();

        if (doc instanceof AbstractDocument) {
            ((AbstractDocument) doc).setDocumentFilter(filter);
        }
        doc.putProperty(DocumentFilter.class, null);
    }
}
项目:javify    文件:DocumentFilterDemo.java   
public void insertString(DocumentFilter.FilterBypass fb,
                         int offset, String string,
                         AttributeSet attr)
throws BadLocationException
{
  filterString(fb, offset, 0, string, attr, true);
}
项目:javify    文件:DocumentFilterDemo.java   
public void replace(DocumentFilter.FilterBypass fb,
                          int offset, int length,
                          String string,
                          AttributeSet attr)
throws BadLocationException
{
  filterString(fb, offset, length, string, attr, false);
}
项目:javify    文件:AbstractDocument.java   
/** Returns the DocumentFilter.FilterBypass instance for this
 * document and create it if it does not exist yet.
 *
 * @return This document's DocumentFilter.FilterBypass instance.
 */
private DocumentFilter.FilterBypass getBypass()
{
  if (bypass == null)
    bypass = new Bypass();

  return bypass;
}
项目:binnavi    文件:CDecFormatter.java   
@Override
public void insertString(final DocumentFilter.FilterBypass fb, final int offset,
    final String string, final AttributeSet attr) throws BadLocationException {
  if (isValid(string, 0)) {
    super.insertString(fb, offset, string, attr);
  }
}
项目:binnavi    文件:CDecFormatter.java   
@Override
public void replace(final DocumentFilter.FilterBypass fb, final int offset, final int length,
    final String string, final AttributeSet attr) throws BadLocationException {
  if (isValid(string, length)) {
    super.replace(fb, offset, length, string, attr);
  }
}
项目:binnavi    文件:CFilenameFormatter.java   
@Override
public void insertString(final DocumentFilter.FilterBypass fb, final int offset,
    final String string, final AttributeSet attr) throws BadLocationException {
  if (isValid(string, 0)) {
    super.insertString(fb, offset, string, attr);
  }
}
项目:binnavi    文件:CFilenameFormatter.java   
@Override
public void replace(final DocumentFilter.FilterBypass fb, final int offset, final int length,
    final String string, final AttributeSet attr) throws BadLocationException {
  if (isValid(string, length)) {
    super.replace(fb, offset, length, string, attr);
  }
}
项目:binnavi    文件:CHexFormatter.java   
@Override
public void insertString(final DocumentFilter.FilterBypass fb, final int offset,
    final String string, final AttributeSet attr) throws BadLocationException {
  if (isValid(string, 0)) {
    super.insertString(fb, offset, string, attr);
  }
}