KickJava   Java API By Example, From Geeks To Geeks.

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


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: ValidatableComponent.java,v $
11    Revision 1.1 2004/01/20 07:38:05 bobintetley
12    Bug fixes and compatibility methods
13
14    Revision 1.3 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 import swingwtx.swing.event.*;
31 import swingwt.awt.*;
32 import swingwt.awt.event.*;
33
34 import java.util.*;
35
36 /**
37  * This is the superclass of all components that can be validated through
38  * the <code>ValidationGroup</code> class. When a component fails validation,
39  * an icon is shown next to the component with a tooltip stating why
40  * the component failed validation.
41  *
42  * @author Robin Rawson-Tetley
43  */

44 public abstract class ValidatableComponent extends JPanel {
45     
46     /** The component we are holding */
47     protected JComponent comp = null;
48     /** The component that will display the icon/tooltip */
49     protected JLabel errorOutput = new JLabel();
50     /** The validation group containing this component */
51     protected ValidationGroup validationGroup = null;
52     
53     /** whether the component currently matches it's validation rules */
54     protected boolean isValid = true;
55     protected String JavaDoc errorMessage = "";
56     
57     /** Validation listeners */
58     protected Vector validationListeners = new Vector();
59     
60     public void dispose() {
61         comp.dispose();
62         errorOutput.dispose();
63         super.dispose();
64     }
65     
66     public JComponent getComponent() { return comp; }
67     
68     public void addValidationListener(ValidationListener l) {
69         validationListeners.add(l);
70     }
71     
72     public void removeValidationListener(ValidationListener l) {
73         validationListeners.remove(l);
74     }
75     
76     /** Should be called by subclasses after component creation */
77     public void setupComponent() {
78         
79         setLayout(new BorderLayout());
80         add(comp, BorderLayout.CENTER);
81         add(errorOutput, BorderLayout.EAST);
82        
83     // Blank icon
84
errorOutput.setIcon(new ImageIcon(getClass().getResource("/swingwtx/custom/validation/blankicon.gif")));
85         
86         // Work out whether the component is valid by firing the
87
// validation event when the component loses the focus
88
comp.addFocusListener(new FocusAdapter() {
89             public void focusLost(FocusEvent e) {
90                 fireValidation(false);
91             }
92         });
93         
94     }
95     
96     public void setIsValid(boolean b) { isValid = b; }
97     public boolean isValid() { return isValid; }
98     public void setErrorMessage(String JavaDoc s) { errorMessage = s; }
99     public String JavaDoc getErrorMessage() { return errorMessage; }
100     
101     /**
102      * Sends the validation event to the component listeners.
103      * Monitors the validation event as it comes back and
104      * updates the object accordingly.
105      * @param forcePass If set to true, it is assumed that validation
106      * passes - even if it wouldn't, and the event is not called.
107      * This is used by the clearErrors() call to reset fields.
108      */

109     protected void fireValidation(boolean forcePass) {
110         for (int i = 0; i < validationListeners.size(); i++) {
111             ValidationEvent e = new ValidationEvent(this);
112         if (!forcePass) {
113                  ((ValidationListener) validationListeners.get(i)).validating(e);
114                  isValid = e.isValid();
115          errorMessage = e.getErrorMessage();
116         }
117         else
118          isValid = true;
119             if (isValid) {
120         errorOutput.setIcon(new ImageIcon(getClass().getResource("/swingwtx/custom/validation/blankicon.gif")));
121                 errorOutput.setToolTipText("");
122             }
123             else {
124                 errorOutput.setIcon(validationGroup.getIcon());
125                 errorOutput.setToolTipText(errorMessage);
126             }
127             // Relayout our container
128
invalidate();
129         }
130     }
131     
132     public void setVisible(final boolean b) { comp.setVisible(b); }
133     public boolean isVisible() { return comp.isVisible(); }
134     public void setEnabled(final boolean b) { comp.setEnabled(b); }
135     public boolean isEnabled() { return comp.isEnabled(); }
136     public void requestFocus() { comp.requestFocus(); }
137     public void grabFocus() { comp.grabFocus(); }
138     public swingwt.awt.Color getBackground() { return comp.getBackground(); }
139     public void setBackground(swingwt.awt.Color c) { comp.setBackground(c); }
140     public swingwt.awt.Color getForeground() { return comp.getForeground(); }
141     public void setForeground(swingwt.awt.Color c) { comp.setForeground(c); }
142     public swingwt.awt.Font getFont() { return comp.getFont(); }
143     public void setFont(swingwt.awt.Font f) { comp.setFont(f); }
144     public String JavaDoc getToolTipText() { return comp.getToolTipText(); }
145     public void setToolTipText(final String JavaDoc text) { comp.setToolTipText(text); }
146     public void addActionListener(ActionListener l) { comp.addActionListener(l); }
147     public void removeActionListener(ActionListener l) { comp.removeActionListener(l); }
148     public void addMouseListener(MouseListener l) { comp.addMouseListener(l); }
149     public void removeMouseListener(MouseListener l) { comp.removeMouseListener(l); }
150     public void addKeyListener(KeyListener l) { comp.addKeyListener(l); }
151     public void removeKeyListener(KeyListener l) { comp.removeKeyListener(l); }
152     public void addFocusListener(FocusListener l) { comp.addFocusListener(l); }
153     public void removeFocusListener(FocusListener l) { comp.removeFocusListener(l); }
154
155     /** Getter for property validationGroup.
156      * @return Value of property validationGroup.
157      *
158      */

159     public swingwtx.custom.validation.ValidationGroup getValidationGroup() {
160         return validationGroup;
161     }
162     
163     /** Setter for property validationGroup.
164      * @param validationGroup New value of property validationGroup.
165      *
166      */

167     public void setValidationGroup(swingwtx.custom.validation.ValidationGroup validationGroup) {
168         this.validationGroup = validationGroup;
169     }
170     
171 }
172
Popular Tags