1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: public abstract class FormValidator
48: {
49:
50: private class FormTextfieldListener
51: implements DocumentListener, PropertyChangeListener
52: {
53: private FormTextfieldListener ()
54: {
55: }
56:
57:
63: public void propertyChange (final PropertyChangeEvent evt)
64: {
65: if (DOCUMENT_PROPERTY_NAME.equals(evt.getPropertyName()))
66: {
67: final Document olddoc = (Document) evt.getOldValue();
68: olddoc.removeDocumentListener(this);
69: final Document newdoc = (Document) evt.getOldValue();
70: newdoc.addDocumentListener(this);
71: }
72: }
73:
74:
79: public void changedUpdate (final DocumentEvent e)
80: {
81: handleValidate();
82: }
83:
84:
90: public void insertUpdate (final DocumentEvent e)
91: {
92: handleValidate();
93: }
94:
95:
102: public void removeUpdate (final DocumentEvent e)
103: {
104: handleValidate();
105: }
106: }
107:
108: private class FormActionListener implements ActionListener
109: {
110: private FormActionListener ()
111: {
112: }
113:
114:
117: public void actionPerformed (final ActionEvent e)
118: {
119: handleValidate();
120: }
121: }
122:
123: private class FormItemListener implements ItemListener
124: {
125: private FormItemListener ()
126: {
127: }
128:
129:
134: public void itemStateChanged (final ItemEvent e)
135: {
136: handleValidate();
137: }
138: }
139:
140: private FormTextfieldListener formTextfieldListener;
141: private FormActionListener actionListener;
142: private static final String DOCUMENT_PROPERTY_NAME = "document";
143: private FormItemListener itemListener;
144: private boolean enabled;
145:
146: protected FormValidator ()
147: {
148: this.formTextfieldListener = new FormTextfieldListener();
149: this.actionListener = new FormActionListener();
150: this.itemListener = new FormItemListener();
151: }
152:
153: public void registerTextField (final JTextComponent textField)
154: {
155: textField.getDocument().addDocumentListener(formTextfieldListener);
156: textField.addPropertyChangeListener(DOCUMENT_PROPERTY_NAME, formTextfieldListener);
157: }
158:
159: public void registerButton (final AbstractButton bt)
160: {
161: bt.addActionListener(actionListener);
162: }
163:
164: public void registerComboBox (final JComboBox bt)
165: {
166: bt.addItemListener(itemListener);
167: }
168:
169: public abstract Action getConfirmAction ();
170:
171: protected final void handleValidate ()
172: {
173: final Action confirmAction = getConfirmAction();
174: if (confirmAction == null || enabled == false)
175: {
176: return;
177: }
178:
179: if (performValidate() == false)
180: {
181: confirmAction.setEnabled(false);
182: }
183: else
184: {
185: confirmAction.setEnabled(true);
186: }
187: }
188:
189: public boolean isEnabled ()
190: {
191: return enabled;
192: }
193:
194: public void setEnabled (final boolean enabled)
195: {
196: this.enabled = enabled;
197: }
198:
199: public abstract boolean performValidate ();
200: }