Java Code Examples for javax.swing.text.DocumentFilter

The following examples show how to use javax.swing.text.DocumentFilter. These examples are extracted from open source projects. 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 Project: ghidra   Source File: HexIntegerFormatter.java    License: 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 2
Source Project: netbeans   Source File: BaseDocument.java    License: 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 3
Source Project: netbeans   Source File: BaseDocument.java    License: 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 4
Source Project: netbeans   Source File: BaseDocument.java    License: 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 5
Source Project: java-swing-tips   Source File: MainPanel.java    License: 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 6
Source Project: groovy   Source File: ConsoleTextEditor.java    License: 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 7
Source Project: opensim-gui   Source File: JConsole.java    License: 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 8
Source Project: opensim-gui   Source File: JConsole.java    License: 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 9
Source Project: Explvs-AIO   Source File: DoubleDocumentFilter.java    License: 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 = DOUBLE_REGEX.matcher(text);

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

    super.replace(fb, offset, length, text, attrs);
}
 
Example 10
Source Project: Explvs-AIO   Source File: IntegerDocumentFilter.java    License: 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 11
Source Project: Explvs-AIO   Source File: DoubleDocumentFilter.java    License: 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 = DOUBLE_REGEX.matcher(text);

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

    super.replace(fb, offset, length, text, attrs);
}
 
Example 12
Source Project: Explvs-AIO   Source File: IntegerDocumentFilter.java    License: 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 13
Source Project: netbeans   Source File: SimpleTestStepLocation.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source Project: netbeans   Source File: SimpleTestStepLocation.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source Project: netbeans   Source File: SimpleTestStepLocation.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void remove(DocumentFilter.FilterBypass bypass,
                   int offset,
                   int length) throws BadLocationException {
    if (!programmaticChange && srcGroupNameDisplayed) {
        removeSrcGroupName(bypass);
    }
    super.remove(bypass, offset, length);
}
 
Example 16
Source Project: netbeans   Source File: SimpleTestStepLocation.java    License: Apache License 2.0 5 votes vote down vote up
private void removeSrcGroupName(DocumentFilter.FilterBypass bypass)
                                        throws BadLocationException {
    bypass.remove(classNameLength,
                  tfClassToTest.getText().length() - classNameLength);
    srcGroupNameDisplayed = false;
    setNavigationFilterEnabled(false);
}
 
Example 17
Source Project: netbeans   Source File: ClassNameTextField.java    License: Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 18
Source Project: mts   Source File: FloatDocumentFilter.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
    String intToInsert;
    if (!Utils.isInteger(text) && !text.equals(".")){
        intToInsert = "";
    }
    else{
        intToInsert = text;
    }
    super.insertString(fb, offset, intToInsert, attr);
}
 
Example 19
Source Project: mts   Source File: FloatDocumentFilter.java    License: GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 20
Source Project: mts   Source File: JPanelNumber.java    License: GNU General Public License v3.0 5 votes vote down vote up
public JPanelNumber(PropertiesEnhanced config, String name, DocumentFilter filter){
    super(config, name);
    this.filter = filter;
    this.jTextFieldValue = new JTextField();
    ((AbstractDocument) this.jTextFieldValue.getDocument()).setDocumentFilter(this.filter);        
    this.add(this.jTextFieldValue);
    this.jTextFieldValue.setEditable(true);
    this.jTextFieldValue.setVisible(true);
    this.validate();
}
 
Example 21
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
@Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
  fb.insertString(offset, text, attr);
  Element root = fb.getDocument().getDefaultRootElement();
  if (root.getElementCount() > MAX_LINES) {
    fb.remove(0, root.getElement(0).getEndOffset());
  }
}
 
Example 22
Source Project: sql-developer-keepalive   Source File: PollIntervalDialog.java    License: 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 (isNumber(text))
        super.replace(fb, offset, length, text, attrs);
    else {
        java.awt.Toolkit.getDefaultToolkit().beep();
    }
}
 
Example 23
/**
 * 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();
}
 
Example 24
Source Project: java-swing-tips   Source File: MainPanel.java    License: 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 25
Source Project: binnavi   Source File: CDecFormatter.java    License: 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 Project: binnavi   Source File: CFilenameFormatter.java    License: 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 27
Source Project: binnavi   Source File: CFilenameFormatter.java    License: 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 28
Source Project: binnavi   Source File: CHexFormatter.java    License: 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 Project: binnavi   Source File: CHexFormatter.java    License: 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 30
Source Project: java-swing-tips   Source File: MainPanel.java    License: 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);
}