-
Notifications
You must be signed in to change notification settings - Fork 98
/
MaskFormatter.java
343 lines (323 loc) · 9.35 KB
/
MaskFormatter.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*******************************************************************************
* Copyright (c) 2005, 2009 Eric Wuillai.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eric Wuillai (eric@wdev91.com) - initial API and implementation
*******************************************************************************/
package org.eclipse.nebula.widgets.formattedtext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
/**
* This class provides formatting of <code>String</code> values in a
* <code>FormattedText</code>.<p>
*
* <h4>Pattern Characters</h4>
* <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples.">
* <tr bgcolor="#ccccff">
* <th align=left>Symbol</th>
* <th align=left>Meaning</th>
* </tr>
* <tr>
* <td><code>#</code></td>
* <td>Digit</td>
* </tr>
* <tr bgcolor="#eeeeff">
* <td><code>A</code></td>
* <td>Alphanumeric</td>
* </tr>
* <tr>
* <td><code>U</code></td>
* <td>Alphanumeric converted in uppercase</td>
* </tr>
* <tr bgcolor="#eeeeff">
* <td><code>L</code></td>
* <td>Alphanumeric converted in lowercase</td>
* </tr>
* </table>
* All other characters are considered as separators.
*
* <h4>Examples</h4>
* <code>newFormatter("(###) UUU-AAAA");</code><br>
* <code>setValue("123aBcDeF");</code> -> will display as "(123) ABC-DeF".
*/
public class MaskFormatter extends AbstractFormatter {
private static final char P_DIGIT = '#';
private static final char P_ALPHANUM = 'A';
private static final char P_UPPERCASE = 'U';
private static final char P_LOWERCASE = 'L';
private static final char P_UHEXDIGIT = 'X';
private static final char P_LHEXDIGIT = 'x';
/** Edit mask */
protected String editPattern;
/** Buffer for the edit value */
protected StringBuffer editValue;
/** Number of editable positions in the mask */
protected int positions;
/** Current number of characters in the buffer (except mask characters) */
protected int count = 0;
/**
* Constructs a new instance with the given edit mask.
*
* @param editPattern edit mask
*/
public MaskFormatter(String editPattern) {
if (editPattern == null)
SWT.error(SWT.ERROR_NULL_ARGUMENT);
this.editPattern = editPattern;
// Initializes the buffer with the formatting characters
editValue = new StringBuffer(editPattern.length());
for (int i = 0; i < editPattern.length(); i++) {
char c = editPattern.charAt(i);
if (c != P_DIGIT && c != P_ALPHANUM && c != P_UPPERCASE
&& c != P_LOWERCASE && c != P_UHEXDIGIT && c != P_LHEXDIGIT) {
editValue.append(c);
} else {
editValue.append(SPACE);
positions++;
}
}
}
/**
* Clear a part of the edition cache. Mask characters are preserved in their
* positions.
*
* @param start
* beginning index
* @param len
* length of portion to clear
*/
protected void clearText(int start, int len) {
for (int i = start; i < start + len && i < editValue.length(); i++) {
char c = editPattern.charAt(i);
if (c == P_DIGIT || c == P_ALPHANUM || c == P_UPPERCASE
|| c == P_LOWERCASE || c == P_UHEXDIGIT || c == P_LHEXDIGIT) {
if (editValue.charAt(i) != SPACE) {
count--;
}
editValue.setCharAt(i, SPACE);
}
}
}
/**
* Returns the current value formatted for display.<p>
* There is no difference in this formatter between edit and display values.
* So this method returns the edit string.
*
* @return display string
* @see ITextFormatter#getDisplayString()
*/
public String getDisplayString() {
return getEditString();
}
/**
* Returns the current value formatted for editing. This method is called by
* <code>FormattedText</code> when the <code>Text</code> widget gains focus.
* The value returned is the content of the StringBuilder
* <code>editValue</code> used as cache.
*
* @return edit string
* @see ITextFormatter#getEditString()
*/
public String getEditString() {
return editValue.toString();
}
/**
* Returns the current value. The value returned is the content of the edit
* cache without any mask characters.
*
* @return current string value
* @see ITextFormatter#getValue()
*/
public Object getValue() {
StringBuffer value = new StringBuffer(editValue.length());
for (int i = 0; i < editValue.length(); i++) {
char c = editPattern.charAt(i);
if (c == P_DIGIT || c == P_ALPHANUM || c == P_UPPERCASE
|| c == P_LOWERCASE || c == P_UHEXDIGIT || c != P_LHEXDIGIT) {
value.append(editValue.charAt(i));
}
}
return value.toString();
}
/**
* Returns the type of value this {@link ITextFormatter} handles, i.e.
* returns in {@link #getValue()}.<br>
* A MaskFormatter always returns a String value.
*
* @return The value type.
*/
public Class<String> getValueType() {
return String.class;
}
/**
* Inserts a sequence of characters in the edit buffer. The current content
* of the buffer is override. The new position of the cursor is computed and
* returned. Mask characters are preserved in their positions.
*
* @param txt String of characters to insert
* @param start Starting position of insertion
* @return New position of the cursor
*/
protected int insertText(String txt, int start) {
int i = start, j = 0;
char p, c, o;
while (i < editPattern.length() && j < txt.length()) {
p = editPattern.charAt(i);
c = txt.charAt(j);
o = editValue.charAt(i);
switch (p) {
case P_DIGIT:
if (Character.isDigit(c)) {
editValue.setCharAt(i, c);
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
break;
case P_UPPERCASE:
if (Character.isLetterOrDigit(c)) {
editValue.setCharAt(i, Character.toUpperCase(c));
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
break;
case P_LOWERCASE:
if (Character.isLetterOrDigit(c)) {
editValue.setCharAt(i, Character.toLowerCase(c));
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
break;
case P_ALPHANUM:
if (Character.isLetterOrDigit(c)) {
editValue.setCharAt(i, c);
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
break;
case P_UHEXDIGIT:
if (isHexDigit(c)) {
editValue.setCharAt(i, Character.toUpperCase(c));
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
break;
case P_LHEXDIGIT:
if (isHexDigit(c)) {
editValue.setCharAt(i, Character.toLowerCase(c));
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
break;
default: // Separator
if (p != c) {
if (Character.isLetterOrDigit(c)) {
i++;
continue;
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
}
o = '*';
break;
}
if (o == SPACE && c != SPACE) {
count++;
}
i++;
j++;
}
return i;
}
/**
* Returns <code>true</code> if current edited value is empty, else returns
* <code>false</code>.<br>
* A value is considered as empty in a MaskFormatter if the edit buffer
* contains no characters except the mask characters.
*
* @return true if empty, else false
*/
public boolean isEmpty() {
return count == 0;
}
/**
* Returns <code>true</code> if current edited value is valid, else returns
* <code>false</code>. An empty value is considered as valid.
*
* @return true if valid, else false
* @see ITextFormatter#isValid()
*/
public boolean isValid() {
return (count == 0 || count == positions);
}
/**
* Sets the value to edit. The value provided must be a <code>String</code>.
*
* @param value string value
* @throws IllegalArgumentException if not a string
* @see ITextFormatter#setValue(java.lang.Object)
*/
public void setValue(Object value) {
if (value instanceof String) {
insertText((String) value, 0);
updateText(editValue.toString(), 0);
} else if (value == null) {
clearText(0, editValue.length());
} else {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
}
/**
* Handles a <code>VerifyEvent</code> sent when the text is about to be
* modified. This method is the entry point of all operations of formatting.
*
* @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent)
*/
public void verifyText(VerifyEvent e) {
if (ignore) {
return;
}
e.doit = false;
if (e.keyCode == SWT.BS || e.keyCode == SWT.DEL) {
// Clears
clearText(e.start, (e.end > e.start) ? e.end - e.start : 1);
updateText(editValue.toString(), e.start);
} else {
// Inserts
int p;
try {
p = insertText(e.text, e.start);
if (e.end - e.start > e.text.length()) {
clearText(e.start + e.text.length(), e.end - e.start - e.text.length());
}
} catch (IllegalArgumentException iae) {
beep();
p = e.start;
}
// Computes new position of the cursor
char c;
while (p < editPattern.length()
&& (c = editPattern.charAt(p)) != P_DIGIT
&& c != P_ALPHANUM && c != P_UPPERCASE && c != P_LOWERCASE
&& c != P_UHEXDIGIT && c != P_LHEXDIGIT) {
p++;
}
updateText(editValue.toString(), p);
}
}
protected boolean isHexDigit(char ch) {
boolean test = false;
try {
test = Character.digit(ch, 16) > -1.;
} catch (Exception e) {
return false;
}
return test;
}
}