javax.swing.text.DocumentFilter Java Examples

The following examples show how to use javax.swing.text.DocumentFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseDocument.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** 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);
        }
    }
 
Example #2
Source File: BaseDocument.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** 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);
        }
    }
 
Example #3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override 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) {
    String str = text;
    if (LB.equals(str)) {
      String line = doc.getText(promptPosition, offset - promptPosition);
      // String[] args = line.split("\\s");
      String[] args = Stream.of(line.split(","))
        .map(String::trim)
        .filter(s -> !s.isEmpty())
        .toArray(String[]::new);
      String cmd = args[0];
      if (cmd.isEmpty()) {
        str = String.format("%n%s", PROMPT);
      } else {
        str = String.format("%n%s: command not found%n%s", cmd, PROMPT);
      }
    }
    fb.replace(offset, length, str, attrs);
  }
}
 
Example #4
Source File: JConsole.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #5
Source File: HexIntegerFormatter.java    From ghidra with Apache License 2.0 6 votes vote down vote up
HexAllowedPositiveValueIntgerDocumentFilterWrapper(Format format,
		DocumentFilter wrappedFilter) {
	super(format, wrappedFilter);

	hexCharacterSet.add('a');
	hexCharacterSet.add('A');
	hexCharacterSet.add('b');
	hexCharacterSet.add('B');
	hexCharacterSet.add('c');
	hexCharacterSet.add('C');
	hexCharacterSet.add('d');
	hexCharacterSet.add('D');
	hexCharacterSet.add('e');
	hexCharacterSet.add('E');
	hexCharacterSet.add('f');
	hexCharacterSet.add('F');
}
 
Example #6
Source File: JConsole.java    From opensim-gui with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #7
Source File: BaseDocument.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #8
Source File: ConsoleTextEditor.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void enableHighLighter(Class clazz) {
    DefaultStyledDocument doc = (DefaultStyledDocument) textEditor.getDocument();

    try {
        DocumentFilter documentFilter = (DocumentFilter) clazz.getConstructor(doc.getClass()).newInstance(doc);
        doc.setDocumentFilter(documentFilter);

        disableMatchingHighlighter();
        if (documentFilter instanceof SmartDocumentFilter) {
            final SmartDocumentFilter smartDocumentFilter = (SmartDocumentFilter) documentFilter;
            enableMatchingHighlighter(smartDocumentFilter);
        }
    } catch (ReflectiveOperationException e) {
        e.printStackTrace();
    }
}
 
Example #9
Source File: StructuredSyntaxDocumentFilter.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Remove a string from the document, and then parse it if the parser has been
 * set.
 *
 * @param fb
 * @param offset
 * @param length
 * @throws BadLocationException
 */    
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
    throws BadLocationException {
    // FRICKIN' HACK!!!!! For some reason, deleting a string at offset 0
    // does not get done properly, so first replace and remove after parsing
    if (offset == 0 && length != fb.getDocument().getLength()) {
        fb.replace(0, length, "\n", lexer.defaultStyle);
        
        // start on either side of the removed text
        parseDocument(offset, 2);
        fb.remove(offset, 1);
    }
    else {
        fb.remove(offset, length);
        
        // start on either side of the removed text
        if (offset + 1 < fb.getDocument().getLength()) {
            parseDocument(offset, 1);
        }
        else if (offset - 1 > 0) {
            parseDocument(offset - 1, 1);
        }
        else {
            // empty text
            mlTextRunSet.clear();
        }
    }

}
 
Example #10
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  Document doc = fb.getDocument();
  int currentLength = doc.getLength();
  String currentContent = doc.getText(0, currentLength);
  String before = currentContent.substring(0, offset);
  String after = currentContent.substring(length + offset, currentLength);
  String newValue = before + Objects.toString(text, "") + after;
  checkInput(newValue, offset);
  fb.replace(offset, length, text, attrs);
}
 
Example #11
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  Document doc = fb.getDocument();
  int currentLength = doc.getLength();
  String currentContent = doc.getText(0, currentLength);
  String before = currentContent.substring(0, offset);
  String after = currentContent.substring(length + offset, currentLength);
  String newValue = before + Objects.toString(text, "") + after;
  checkInput(newValue, offset);
  fb.replace(offset, length, text, attrs);
}
 
Example #12
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  if (length == 0) {
    fb.insertString(offset, text, attrs);
  } else {
    compoundEdit = new CompoundEdit();
    fb.replace(offset, length, text, attrs);
    compoundEdit.end();
    addEdit(compoundEdit);
    compoundEdit = null;
  }
}
 
Example #13
Source File: BorderEditor.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
                    AttributeSet attrs) throws BadLocationException {
    if(fb.getDocument().getLength() - length + text.length() > 6) {
        return;
    }
    for(int iter = 0 ; iter < text.length() ; iter++) {
        char c = text.charAt(iter);
        if(!(Character.isDigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) {
            return;
        }
    }
    fb.replace(offset, length, text, attrs);
    updateBorder(false);
}
 
Example #14
Source File: BorderEditor.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string,
                         AttributeSet attr) throws BadLocationException {
    if(fb.getDocument().getLength() + string.length() > 6) {
        return;
    }
    for(int iter = 0 ; iter < string.length() ; iter++) {
        char c = string.charAt(iter);
        if(!(Character.isDigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) {
            return;
        }
    }
    fb.insertString(offset, string, attr);
    updateBorder(false);
}
 
Example #15
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  Document doc = fb.getDocument();
  int currentLength = doc.getLength();
  String currentContent = doc.getText(0, currentLength);
  String before = currentContent.substring(0, offset);
  String after = currentContent.substring(length + offset, currentLength);
  String newValue = before + Objects.toString(text, "") + after;
  checkInput(newValue, offset);
  fb.replace(offset, length, text, attrs);
}
 
Example #16
Source File: SmartDocumentFilter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
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);

    parseDocument();
}
 
Example #17
Source File: IntegerDocumentFilter.java    From Explvs-AIO with MIT License 5 votes vote down vote up
@Override
public void replace(DocumentFilter.FilterBypass fb,
                    int offset,
                    int length,
                    String text,
                    AttributeSet attrs) throws BadLocationException {

    Matcher matcher = NUMBER_REGEX.matcher(text);

    if (!matcher.matches()) {
        return;
    }

    super.replace(fb, offset, length, text, attrs);
}
 
Example #18
Source File: SmartDocumentFilter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
        throws BadLocationException {

    fb.remove(offset, length);
    parseDocument();
}
 
Example #19
Source File: StructuredSyntaxDocumentFilter.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Insert a string into the document, and then parse it if the parser has been
 * set.
 *
 * @param fb
 * @param offset
 * @param text
 * @param attrs
 * @throws BadLocationException
 */    
public void insertString(DocumentFilter.FilterBypass fb, int offset,
                         String text, AttributeSet attrs)
    throws BadLocationException {
    // remove problem meta characters returns
    text = replaceMetaCharacters(text);
    
    fb.insertString(offset, text, attrs);
    
    // start on the string that was inserted
    parseDocument(offset, text.length());
}
 
Example #20
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
  int len = fb.getDocument().getLength();
  if (len + text.length() > MAX) {
    Toolkit.getDefaultToolkit().beep();
    return;
  }
  fb.insertString(offset, text, attr);
}
 
Example #21
Source File: BorderEditor.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws
                   BadLocationException {
    if(fb.getDocument().getLength() > length) {
        fb.remove(offset, length);
    }
    updateBorder(false);
}
 
Example #22
Source File: TextEditor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void setRenderRange(int start, int stop) {
    DocumentFilter documentFilter = ((DefaultStyledDocument) TextEditor.this.getDocument()).getDocumentFilter();
    if (documentFilter instanceof SmartDocumentFilter) {
        SmartDocumentFilter smartDocumentFilter = (SmartDocumentFilter) documentFilter;
        smartDocumentFilter.setRenderRange(Tuple.tuple(start, stop));
    }
}
 
Example #23
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  int len = fb.getDocument().getLength();
  if (len - length + text.length() > MAX) {
    Toolkit.getDefaultToolkit().beep();
    return;
  }
  fb.replace(offset, length, text, attrs);
}
 
Example #24
Source File: CHexFormatter.java    From binnavi with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #25
Source File: CHexFormatter.java    From binnavi with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #26
Source File: CFilenameFormatter.java    From binnavi with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #27
Source File: CFilenameFormatter.java    From binnavi with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #28
Source File: CDecFormatter.java    From binnavi with Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example #29
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  String str = text;
  if (offset == 0 && Objects.nonNull(text) && !text.isEmpty()) {
    str = text.substring(0, 1).toUpperCase(Locale.ENGLISH) + text.substring(1);
  }
  fb.replace(offset, length, str, attrs);
}
 
Example #30
Source File: RuneliteColorPicker.java    From runelite with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Gets the whole string from the passed DocumentFilter replace.
 */
static String getReplacedText(DocumentFilter.FilterBypass fb, int offset, int length, String str)
	throws BadLocationException
{
	Document doc = fb.getDocument();
	StringBuilder sb = new StringBuilder(doc.getText(0, doc.getLength()));
	sb.replace(offset, offset + length, str);

	return sb.toString();
}