01: // WebOnSwing - Web Application Framework
02: //Copyright (C) 2004 Fernando Damian Petrola
03: //
04: //This library is free software; you can redistribute it and/or
05: //modify it under the terms of the GNU Lesser General Public
06: //License as published by the Free Software Foundation; either
07: //version 2.1 of the License, or (at your option) any later version.
08: //
09: //This library is distributed in the hope that it will be useful,
10: //but WITHOUT ANY WARRANTY; without even the implied warranty of
11: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: //Lesser General Public License for more details.
13: //
14: //You should have received a copy of the GNU Lesser General Public
15: //License along with this library; if not, write to the Free Software
16: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17:
18: package net.ar.webonswing.swing.components.validators;
19:
20: import javax.swing.*;
21:
22: import net.ar.webonswing.*;
23: import net.ar.webonswing.helpers.*;
24: import net.ar.webonswing.swing.components.validators.contributors.*;
25:
26: public class JRequiredFieldValidator extends JValidator {
27: public JRequiredFieldValidator() {
28: super (null, "*", "", true);
29:
30: WosFramework.assignContributor(this ,
31: RequiredFieldValidatorUIContributor.class);
32: }
33:
34: public JRequiredFieldValidator(JComponent aComponentToValidate,
35: String aMessage, String aGroupMessage,
36: boolean hasRemoteValidation) {
37: super (aComponentToValidate, aMessage, aGroupMessage,
38: hasRemoteValidation);
39:
40: WosFramework.assignContributor(this ,
41: RequiredFieldValidatorUIContributor.class);
42: }
43:
44: public JRequiredFieldValidator(JComponent aComponentToValidate,
45: String aMessage, boolean hasRemoteValidation) {
46: this (aComponentToValidate, aMessage, "", hasRemoteValidation);
47: }
48:
49: public JRequiredFieldValidator(JComponent aComponentToValidate) {
50: this (aComponentToValidate, "*", "", true);
51: }
52:
53: public JRequiredFieldValidator(JComponent aComponentToValidate,
54: String aGroupMessage) {
55: this (aComponentToValidate, "*", aGroupMessage, true);
56: }
57:
58: protected boolean performValidation() {
59: if (componentToValidate instanceof JTextField
60: || componentToValidate instanceof JPasswordField
61: || componentToValidate instanceof JTextArea)
62: return getComponentValue(componentToValidate).length() > 0;
63: else if (componentToValidate instanceof JList)
64: return ((JList) componentToValidate).getSelectedIndex() != -1;
65: else if (componentToValidate instanceof AbstractButton) {
66: AbstractButton button = (AbstractButton) componentToValidate;
67: DefaultButtonModel buttonModel = (DefaultButtonModel) button
68: .getModel();
69:
70: return buttonModel.getGroup() != null ? buttonModel
71: .getGroup().getSelection() != null : button
72: .isSelected();
73: } else if (componentToValidate instanceof JComboBox) {
74: JComboBox comboBox = (JComboBox) componentToValidate;
75: int selectedIndex = comboBox.getSelectedIndex();
76:
77: if (selectedIndex != -1)
78: return !comboBox.getModel().getElementAt(selectedIndex)
79: .equals("");
80: else
81: return false;
82: }
83:
84: throw new WebOnSwingException(
85: "Component not supported by RequiredFieldValidator: "
86: + componentToValidate);
87: }
88: }
|