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 |
| package com.gargoylesoftware.base.util; |
39 |
| |
40 |
| import javax.swing.event.DocumentListener; |
41 |
| import javax.swing.event.UndoableEditListener; |
42 |
| import javax.swing.text.AttributeSet; |
43 |
| import javax.swing.text.BadLocationException; |
44 |
| import javax.swing.text.Document; |
45 |
| import javax.swing.text.Element; |
46 |
| import javax.swing.text.PlainDocument; |
47 |
| import javax.swing.text.Position; |
48 |
| import javax.swing.text.Segment; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| public class DocumentUtil { |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
0
| private DocumentUtil() {
|
62 |
| } |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
2
| public static Document upperCaseDocument( final Document document ) {
|
70 |
2
| return new PassthroughDocument(document) {
|
71 |
2
| public void insertString(
|
72 |
| final int offset, |
73 |
| final String str, |
74 |
| final AttributeSet a) |
75 |
| throws |
76 |
| BadLocationException { |
77 |
| |
78 |
2
| super.insertString(offset, str.toUpperCase(), a);
|
79 |
| } |
80 |
| }; |
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
0
| public static Document upperCaseDocument() {
|
89 |
0
| return upperCaseDocument( new PlainDocument() );
|
90 |
| } |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
4
| public static Document lengthLimitedDocument( final Document document,
|
101 |
| final int maxLength ) { |
102 |
4
| return new LengthLimitedDocument(document,maxLength);
|
103 |
| } |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
0
| public static Document lengthLimitedDocument( final int maxLength ) {
|
112 |
0
| return lengthLimitedDocument( new PlainDocument(), maxLength );
|
113 |
| } |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| private static class LengthLimitedDocument extends PassthroughDocument { |
120 |
| private int maxLength_; |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
3
| public LengthLimitedDocument( final Document document,
|
128 |
| final int maxLength ) { |
129 |
3
| super(document);
|
130 |
3
| if( maxLength < 0 ) {
|
131 |
2
| throw new DetailedIllegalArgumentException(
|
132 |
| "maxLength", new Integer(maxLength), "May not be less than zero"); |
133 |
| } |
134 |
1
| maxLength_ = maxLength;
|
135 |
| } |
136 |
| |
137 |
5
| public void insertString(
|
138 |
| final int offset, |
139 |
| final String string, |
140 |
| final AttributeSet attributeSet) |
141 |
| throws |
142 |
| BadLocationException { |
143 |
| |
144 |
5
| final int documentLength = getLength();
|
145 |
5
| final int stringLength = string.length();
|
146 |
| |
147 |
5
| if( documentLength == maxLength_ ) {
|
148 |
1
| return;
|
149 |
| } |
150 |
4
| else if( documentLength + stringLength <= maxLength_ ) {
|
151 |
4
| super.insertString( offset, string, attributeSet );
|
152 |
| } |
153 |
| else { |
154 |
0
| super.insertString( offset,
|
155 |
| string.substring(0, maxLength_ - documentLength), |
156 |
| attributeSet ); |
157 |
| } |
158 |
| } |
159 |
| } |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| private abstract static class PassthroughDocument implements Document { |
166 |
| private final Document delegate_; |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
6
| public PassthroughDocument( final Document delegate ) {
|
173 |
6
| if( delegate == null ) {
|
174 |
2
| throw new DetailedNullPointerException("delegate");
|
175 |
| } |
176 |
4
| delegate_ = delegate;
|
177 |
| } |
178 |
| |
179 |
16
| public int getLength() {
|
180 |
16
| return delegate_.getLength();
|
181 |
| } |
182 |
| |
183 |
| |
184 |
0
| public void addDocumentListener(DocumentListener listener) {
|
185 |
0
| delegate_.addDocumentListener(listener);
|
186 |
| } |
187 |
| |
188 |
| |
189 |
0
| public void removeDocumentListener(DocumentListener listener) {
|
190 |
0
| delegate_.removeDocumentListener(listener);
|
191 |
| } |
192 |
| |
193 |
| |
194 |
0
| public void addUndoableEditListener(UndoableEditListener listener) {
|
195 |
0
| delegate_.addUndoableEditListener(listener);
|
196 |
| } |
197 |
| |
198 |
| |
199 |
0
| public void removeUndoableEditListener(UndoableEditListener listener) {
|
200 |
0
| delegate_.removeUndoableEditListener(listener);
|
201 |
| } |
202 |
| |
203 |
| |
204 |
0
| public Object getProperty(Object key) {
|
205 |
0
| return delegate_.getProperty(key);
|
206 |
| } |
207 |
| |
208 |
| |
209 |
0
| public void putProperty(Object key, Object value) {
|
210 |
0
| delegate_.putProperty(key,value);
|
211 |
| } |
212 |
| |
213 |
| |
214 |
0
| public void remove(int offs, int len) throws BadLocationException {
|
215 |
0
| delegate_.remove(offs, len);
|
216 |
| } |
217 |
| |
218 |
| |
219 |
6
| public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
|
220 |
6
| delegate_.insertString(offset, str, a);
|
221 |
| } |
222 |
| |
223 |
| |
224 |
4
| public String getText(int offset, int length) throws BadLocationException {
|
225 |
4
| return delegate_.getText(offset, length);
|
226 |
| } |
227 |
| |
228 |
| |
229 |
0
| public void getText(int offset, int length, Segment txt) throws BadLocationException {
|
230 |
0
| delegate_.getText(offset, length, txt);
|
231 |
| } |
232 |
| |
233 |
| |
234 |
0
| public Position getStartPosition() {
|
235 |
0
| return delegate_.getStartPosition();
|
236 |
| } |
237 |
| |
238 |
| |
239 |
0
| public Position getEndPosition() {
|
240 |
0
| return delegate_.getEndPosition();
|
241 |
| } |
242 |
| |
243 |
| |
244 |
0
| public Position createPosition(int offs) throws BadLocationException {
|
245 |
0
| return delegate_.createPosition(offs);
|
246 |
| } |
247 |
| |
248 |
| |
249 |
0
| public Element[] getRootElements() {
|
250 |
0
| return delegate_.getRootElements();
|
251 |
| } |
252 |
| |
253 |
| |
254 |
0
| public Element getDefaultRootElement() {
|
255 |
0
| return delegate_.getDefaultRootElement();
|
256 |
| } |
257 |
| |
258 |
| |
259 |
0
| public void render(Runnable r) {
|
260 |
0
| delegate_.render(r);
|
261 |
| } |
262 |
| } |
263 |
| } |
264 |
| |