Source code: org/schooltool/client/gui/widgets/IDVerifierDocument.java
1 /*
2 * IDVerifierDocument.java
3 *
4 * Created on April 30, 2002, 10:12 AM
5 */
6
7 package org.schooltool.client.gui.widgets;
8 import javax.swing.text.*;
9
10 /**
11 * Usage: new JTextField(new IDVerifierDocument(), text, cols);
12 * validates user input for digits only AND max 13 chars
13 * @author Antonio Bigazzi - bigazzis@attbi.com
14 * @version 1.0
15 * modified: 30.4.2002 by Enrico - added offset constraint line 27
16 */
17 public class IDVerifierDocument extends PlainDocument {
18
19 /** Creates new NumericDocument */
20 public IDVerifierDocument() {
21 }
22
23 public void insertString(int offset, String text, AttributeSet a)
24 throws BadLocationException {
25 StringBuffer buf = new StringBuffer(text.length());
26 for (int i = 0; i < text.length(); i++) {
27 if (Character.isDigit(text.charAt(i)) && offset < 13) {
28 buf.append(text.charAt(i));
29 } else {
30 java.awt.Toolkit.getDefaultToolkit().beep();
31 }
32 }
33 super.insertString(offset, buf.toString(), a);
34 }
35
36 }