KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > custom > validation > ValidationGroup


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: ValidationGroup.java,v $
11    Revision 1.1 2004/01/20 07:38:05 bobintetley
12    Bug fixes and compatibility methods
13
14    Revision 1.1 2004/01/13 21:00:43 bobintetley
15    Renamed ErrorProvider to "ValidationGroup" as it describes it's function better and
16    is a bit more swing-y (like ButtonGroup, which it does bear a resemblance to).
17
18    Revision 1.2 2004/01/13 15:55:12 bobintetley
19    Fixed component sizing problem and added clearErrors() method
20
21    Revision 1.1 2004/01/13 11:14:25 bobintetley
22    ValidationGroup and validation implementation
23
24
25 */

26
27 package swingwtx.custom.validation;
28
29 import swingwtx.swing.*;
30
31 import java.util.*;
32
33 /**
34  * This isn't a real Swing class at all! This is a useful class that someone suggested
35  * we include from C# WinForms! After all, if we're going to make a new GUI API that matches Swing
36  * there's nothing wrong with including new bits and pieces if they're useful :)
37  *
38  * Besides, I hear the JGoodies people are charging for an implementation of this - only took
39  * me a couple of hours to implement!
40  *
41  * This class allows you to monitor validation errors. Each of the validatable components
42  * supports a new event listener called "ValidateListener", passing in a ValidateEvent - your
43  * code should decide whether the component is ok and set true or false with a message. The same
44  * component can be a member of different validation groups, as each validation group allows one stock icon.
45  *
46  * I had a long think about this and decided the best way to deal with this was to make some
47  * new versions of the regular components you might want to validate. They are all prefixed
48  * with the word "Validatable" - eg: ValidatableJTextField, ValidatableJTextArea.
49  *
50  * @author Robin Rawson-Tetley
51  */

52 public class ValidationGroup {
53     
54     protected ImageIcon pImage = null;
55     protected Vector components = new Vector();
56     
57     /** Last seen error message from a component */
58     protected String JavaDoc errorMessage = "";
59     
60     /** Creates an error provider with the default error icon */
61     public ValidationGroup() { pImage = new ImageIcon(getClass().getResource("/swingwtx/custom/validation/erroricon.gif")); }
62     public ValidationGroup(ImageIcon image) { if (image == null) throw new IllegalArgumentException JavaDoc("Image can't be null"); pImage = image; }
63     
64     public void add(ValidatableComponent c) { c.setValidationGroup(this); components.add(c); }
65     public void remove(ValidatableComponent c) { c.setValidationGroup(null); components.remove(c); }
66     
67     public ImageIcon getIcon() { return pImage; }
68     public String JavaDoc getErrorMessage() { return errorMessage; }
69     
70     /** Calls the validation routines for each component this ValidationGroup is managing
71      * @return true if validation is ok, otherwise returns false. Call getErrorMessage()
72      * for the last error message from a component.
73      */

74     public boolean checkValidation() {
75         boolean andCheck = true;
76         for (int i = 0; i < components.size(); i++) {
77             ValidatableComponent c = (ValidatableComponent) components.get(i);
78             c.fireValidation(false);
79             if (!c.isValid()) {
80                 errorMessage = c.getErrorMessage();
81             }
82             andCheck = andCheck && c.isValid();
83         }
84         return andCheck;
85     }
86
87     /** Clears all error messages and icons from all components this
88      * ValidationGroup is managing.
89      */

90     public void clearErrors() {
91         for (int i = 0; i < components.size(); i++) {
92         ValidatableComponent c = (ValidatableComponent) components.get(i);
93         c.fireValidation(true);
94     }
95     errorMessage = "";
96     }
97
98 }
99
Popular Tags