下面列出了javax.swing.text.DocumentFilter#FilterBypass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
@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);
}
}
@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);
}
@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);
}
@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);
}
@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);
}
@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();
}
}
@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);
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
String intToReplace;
if (!Utils.isInteger(text)){
intToReplace = "";
}
else{
intToReplace = text;
}
super.replace(fb, offset, length, intToReplace, attrs);
}
/**
* 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();
}
@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;
}
}
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
throws BadLocationException {
fb.remove(offset, length);
parseDocument();
}
public DocumentFilter.FilterBypass getProtectionBypass() {
return bypass;
}
@Override public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
replace(fb, offset, length, "", null);
}
private DocumentFilter.FilterBypass getFilterBypass() {
if (filterBypass == null) {
filterBypass = new FilterBypassImpl();
}
return filterBypass;
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
fb.replace(offset, length, text, attrs);
}
@Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
if (Objects.nonNull(text)) {
replace(fb, offset, 0, text, attr);
}
}
@Override public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
fb.remove(offset, length);
}
@Override public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
if (Objects.nonNull(text)) {
replace(fb, offset, 0, text, attr);
}
}
@Override public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
if (doc.getDefaultRootElement().getElementIndex(offset) >= maskRange) {
fb.replace(offset, length, text, attrs);
}
}