-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLimitedDocument.java
More file actions
49 lines (36 loc) · 1.16 KB
/
LimitedDocument.java
File metadata and controls
49 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class LimitedDocument extends PlainDocument {
private int _maxLength = -1;
private String _allowCharAsString = null;
public LimitedDocument(){
super();
}
public LimitedDocument( int maxLength ){
super();
this._maxLength = maxLength;
}
public void insertString( int offset, String str, AttributeSet attrSet)
throws BadLocationException{
if(str == null) {
return;
}
if(_allowCharAsString != null && str.length() == 1) {
if(_allowCharAsString.indexOf(str) == -1){
return;
}
}
char[] charVal = str.toCharArray();
String strOldValue = getText(0, getLength());
byte[] tmp = strOldValue.getBytes();
if(_maxLength != -1 && (tmp.length + charVal.length > _maxLength)){
return;
}
super.insertString(offset, str, attrSet);
}
public void setAllowChar(String str) {
_allowCharAsString = str;
}
}