1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.gui.fields;
25
26 import java.awt.Toolkit;
27 import javax.swing.*;
28 import javax.swing.text.*;
29 import javax.swing.event.*;
30
31
32 public class NameField extends JTextField implements DocumentListener {
33
34 public NameField() { super(); }
35 public NameField(int columns) { super(columns); }
36 public NameField(String text) { super(text); }
37 public NameField(String text, int columns) { super(text,columns); }
38
39 protected Document createDefaultModel() {
40 Document doc = new NameFieldDocument();
41 doc.addDocumentListener(this);
42 return doc;
43 }
44
45 public void insertUpdate(DocumentEvent e) {
46 fireChanged();
47 }
48 public void removeUpdate(DocumentEvent e) {
49 fireChanged();
50 }
51 public void changedUpdate(DocumentEvent e) {
52 fireChanged();
53 }
54
55 static char[] alphaSet = {
56 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
57 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
58 };
59
60 /*** MS 230101 v1.05
61 added '.' to otherSet
62 ST 210301 v1.2
63 added '-' to otherSet
64
65 */
66 static char[] otherSet = {
67 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '$', '.','-'
68 };
69
70 static class NameFieldDocument extends PlainDocument {
71 public void insertString(int offs, String str, AttributeSet a)
72 throws BadLocationException {
73
74 if ( str == null ) return;
75 str = str.trim();
76 str = str.replace(' ','_');
77
78 String buf = getText(0,offs) + str;
79 buf = buf.toLowerCase();
80 char[] array = buf.toCharArray();
81
82 if ( array.length > 0 && !member(array[0],alphaSet) ) {
83 Toolkit.getDefaultToolkit().beep();
84 return;
85 }
86 for(int i = 1; i < array.length; i++ ) {
87 if ( !member(array[i],alphaSet) && !member(array[i],otherSet) ) {
88 Toolkit.getDefaultToolkit().beep();
89 return;
90 }
91 }
92 super.insertString(offs,str,a);
93 }
94 }
95 static boolean member(char item, char[] array) {
96 for(int i = 0; i < array.length; i++ )
97 if ( array[i] == item ) return true;
98 return false;
99 }
100
101 protected EventListenerList changeListeners = new EventListenerList();
102 public void addChangeListener(ChangeListener x) {
103 changeListeners.add(ChangeListener.class, x);
104 }
105 public void removeChangeListener(ChangeListener x) {
106 changeListeners.remove(ChangeListener.class, x);
107 }
108
109 protected void fireChanged() {
110 ChangeEvent c = new ChangeEvent(this);
111 Object[] listeners = changeListeners.getListenerList();
112 for(int i= listeners.length-2; i >= 0; i -=2) {
113 if (listeners[i] == ChangeListener.class) {
114 ChangeListener cl = (ChangeListener)listeners[i+1];
115 cl.stateChanged(c);
116 }
117 }
118 }
119
120 }
121
122
123