KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > ValidationTest


1 /*
2  * ValidationTest.java
3  *
4  * Shows a demonstration of SwingWT's C#/WinForms like validation capability
5  *
6  * @author Robin Rawson-Tetley
7  */

8
9 package demo;
10
11 import swingwt.awt.*;
12 import swingwt.awt.event.*;
13 import swingwtx.swing.*;
14 import swingwtx.swing.event.*;
15 import swingwtx.custom.validation.*;
16
17 import java.util.*;
18
19 public class ValidationTest extends JFrame {
20     
21     ValidatableJTextField txtFullName;
22     ValidatableJTextField txtTown;
23     ValidatableJTextField txtAge;
24     ValidatableJTextField txtPhone;
25     ValidationGroup vg;
26     
27     public ValidationTest() {
28         
29         setTitle("ValidationGroup Sample");
30         setSize(new Dimension(300, 200));
31         
32         getContentPane().setLayout(new BorderLayout());
33         
34         vg = new ValidationGroup();
35         
36         // Create the body panel with our 4 validatable fields on
37
JPanel pnlMain = new JPanel();
38         pnlMain.setLayout(new TableLayout(0, 2));
39         
40         pnlMain.add(new JLabel("Full Name:"));
41         
42         txtFullName = new ValidatableJTextField();
43         txtFullName.setPreferredSize(new Dimension(200, 25));
44         // Validation of full name
45
txtFullName.addValidationListener(new ValidationListener() {
46             public void validating(ValidationEvent e) {
47                 if (txtFullName.getText().trim().length() == 0)
48                     e.setValid(false, "Please enter the full name of the customer.");
49                 else {
50                     if (txtFullName.getText().indexOf(" ") == -1)
51                         e.setValid(false, "Please enter at least the first and last name of the customer.");
52                     else
53                         e.setValid(true);
54                 }
55             }
56         });
57         pnlMain.add(txtFullName);
58         vg.add(txtFullName); // Add to the error provider
59

60         
61         pnlMain.add(new JLabel("Town:"));
62         
63         txtTown = new ValidatableJTextField();
64         txtTown.setPreferredSize(new Dimension(200, 25));
65         txtTown.addValidationListener(new ValidationListener() {
66             public void validating(ValidationEvent e) {
67                 if (txtTown.getText().trim().length() == 0)
68                     e.setValid(false, "Please enter the customer's town.");
69                 else
70                     e.setValid(true);
71             }
72         });
73         pnlMain.add(txtTown);
74         vg.add(txtTown); // Add to the error provider
75

76         pnlMain.add(new JLabel("Age:"));
77         
78         txtAge = new ValidatableJTextField();
79         txtAge.setPreferredSize(new Dimension(200, 25));
80         txtAge.addValidationListener(new ValidationListener() {
81             public void validating(ValidationEvent e) {
82                 if (!isNumber(txtAge.getText()))
83                     e.setValid(false, "Please enter the customer's age, as a whole number.");
84                 else
85                     e.setValid(true);
86             }
87             public boolean isNumber(String JavaDoc no) {
88                 try { Integer.parseInt(no); } catch (NumberFormatException JavaDoc e) { return false; } return true;
89             }
90         });
91         pnlMain.add(txtAge);
92         vg.add(txtAge); // Add to the error provider
93

94         pnlMain.add(new JLabel("Phone #"));
95         
96         txtPhone = new ValidatableJTextField();
97         txtPhone.setPreferredSize(new Dimension(200, 25));
98         txtPhone.addValidationListener(new ValidationListener() {
99             public void validating(ValidationEvent e) {
100                 if (
101                    (txtPhone.getText().trim().length() == 0) ||
102                    (txtPhone.getText().trim().length() != 8)
103                    )
104                     e.setValid(false, "Please enter the customer's phone number in the format nnn-nnnn.");
105                 else
106                     e.setValid(true);
107             }
108         });
109         pnlMain.add(txtPhone);
110         vg.add(txtPhone); // Add to the error provider
111

112         
113         // Buttons
114
JPanel pnlButtons = new JPanel();
115         pnlButtons.setLayout(new FlowLayout());
116         
117         JButton btnCancel = new JButton("Cancel");
118         btnCancel.setMnemonic('c');
119         btnCancel.addActionListener(new ActionListener() {
120             public void actionPerformed(ActionEvent e) {
121                 dispose();
122             }
123         });
124         pnlButtons.add(btnCancel);
125         
126         JButton btnOk = new JButton("Ok");
127         btnOk.setMnemonic('o');
128         final ValidationTest valTest = this;
129         btnOk.addActionListener(new ActionListener() {
130             public void actionPerformed(ActionEvent e) {
131                 if (!vg.checkValidation())
132                     JOptionPane.showMessageDialog(valTest, "Validation Failed.", "Error", JOptionPane.ERROR_MESSAGE);
133                 else
134                     dispose();
135             }
136         });
137         pnlButtons.add(btnOk);
138         setDefaultButton(btnOk);
139         
140         // Focus management - really just an example to show you have it flexibly can be done
141
ValidationFocusManager fm = new ValidationFocusManager();
142         Vector comps = new Vector();
143         comps.add(txtFullName.getComponent());
144         comps.add(txtTown.getComponent());
145         comps.add(txtAge.getComponent());
146         comps.add(txtPhone.getComponent());
147         comps.add(btnCancel);
148         comps.add(btnOk);
149         fm.setComponents(comps);
150         comps = null;
151         FocusManager.setCurrentManager(fm);
152         
153         // Add everything to screen
154
getContentPane().add(pnlMain, BorderLayout.CENTER);
155         getContentPane().add(pnlButtons, BorderLayout.SOUTH);
156         
157         // Check validation before display
158
//vg.checkValidation();
159

160         // Display the form
161
show();
162         while(true){
163             try {
164                 Thread.sleep(1000);
165             } catch (InterruptedException JavaDoc e) {
166                 // TODO ×Ô¶¯Éú³É catch ¿é
167
e.printStackTrace();
168             }
169         }
170     }
171     
172     public static void main(String JavaDoc[] args) {
173         new ValidationTest();
174     }
175     
176 }
177
178 /**
179  * Useful little class - allows you to pass a vector of
180  * components in the tab order you want.
181  */

182 class ValidationFocusManager extends FocusManager {
183     Vector comps = null;
184     public Vector getComponents() { return comps; }
185     public void setComponents(Vector v) { comps = v; }
186     public void focusNextComponent(Component c) {
187         for (int i = 0; i < comps.size(); i++) {
188             if (comps.get(i).equals(c))
189                 if (i < comps.size() -1) {
190                     ((Component) comps.get(i+1)).grabFocus();
191                     return;
192                 }
193         }
194     }
195     public void focusPreviousComponent(Component c) {
196         for (int i = 0; i < comps.size(); i++) {
197             if (comps.get(i).equals(c))
198                 if (i > 0) {
199                     ((Component) comps.get(i-1)).grabFocus();
200                     return;
201                 }
202         }
203     }
204 }
205
Popular Tags