001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2004 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.swing.components.validators;
019:
020: import java.util.*;
021:
022: import javax.swing.*;
023:
024: import net.ar.webonswing.*;
025: import net.ar.webonswing.helpers.*;
026: import net.ar.webonswing.swing.components.validators.contributors.*;
027:
028: public abstract class JValidator extends JLabel {
029: protected JComponent componentToValidate;
030: protected String ownMessage = "";
031: protected String groupMessage = "";
032: protected boolean remoteValidation;
033: protected boolean grouped;
034: protected List validationShooters = new Vector();
035:
036: public JValidator() {
037: remoteValidation = true;
038: WosFramework.assignContributor(this ,
039: ValidatorUIContributor.class);
040: }
041:
042: public JValidator(JComponent aComponentToValidate, String aMessage,
043: String aGroupMessage, boolean hasRemoteValidation) {
044: ownMessage = aMessage;
045: groupMessage = aGroupMessage;
046: componentToValidate = aComponentToValidate;
047: remoteValidation = hasRemoteValidation;
048: grouped = false;
049: WosFramework.assignContributor(this ,
050: ValidatorUIContributor.class);
051: }
052:
053: public boolean doValidation() {
054: boolean valid = !isEnabled() || performValidation();
055:
056: if (!valid)
057: setText(getOwnMessage());
058: else
059: setText("");
060:
061: return valid;
062: }
063:
064: protected String getComponentValue(JComponent aComponent) {
065: if (aComponent instanceof JTextField)
066: return ((JTextField) aComponent).getText();
067: if (aComponent instanceof JPasswordField)
068: return new String(((JPasswordField) aComponent)
069: .getPassword());
070: else if (aComponent instanceof JTextArea)
071: return ((JTextArea) aComponent).getText();
072: else if (aComponent instanceof JList)
073: return ((JList) aComponent).getSelectedValue().toString();
074: else if (aComponent instanceof JComboBox)
075: return ((JComboBox) aComponent).getSelectedItem()
076: .toString();
077: else if (aComponent instanceof AbstractButton) {
078: AbstractButton button = (AbstractButton) componentToValidate;
079: DefaultButtonModel buttonModel = (DefaultButtonModel) button
080: .getModel();
081: if (buttonModel.getGroup() != null
082: && buttonModel.getGroup().getSelection() != null)
083: buttonModel = (DefaultButtonModel) buttonModel
084: .getGroup().getSelection();
085:
086: return buttonModel.getActionCommand();
087: }
088:
089: throw new WebOnSwingException(
090: "Component not supported by this validator: "
091: + componentToValidate);
092: }
093:
094: protected abstract boolean performValidation();
095:
096: public void addShooter(JComponent aComponent) {
097: validationShooters.add(aComponent);
098: }
099:
100: public boolean isRemoteValidation() {
101: return remoteValidation;
102: }
103:
104: public void setRemoteValidation(boolean b) {
105: remoteValidation = b;
106: }
107:
108: public JComponent getComponentToValidate() {
109: return componentToValidate;
110: }
111:
112: public void setComponentToValidate(JComponent component) {
113: componentToValidate = component;
114: }
115:
116: public String getGroupMessage() {
117: return groupMessage;
118: }
119:
120: public void setGroupMessage(String aString) {
121: groupMessage = aString;
122: }
123:
124: public String getOwnMessage() {
125: return ownMessage;
126: }
127:
128: public void setOwnMessage(String aString) {
129: ownMessage = aString;
130: }
131:
132: public boolean isGrouped() {
133: return grouped;
134: }
135:
136: public void setGrouped(boolean b) {
137: grouped = b;
138: }
139:
140: public List getValidationShooters() {
141: return validationShooters;
142: }
143:
144: public void setValidationShooters(List aValidationShooters) {
145: this.validationShooters = aValidationShooters;
146: }
147: }
|