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: import net.ar.webonswing.*;
024: import net.ar.webonswing.swing.components.validators.contributors.*;
025:
026: public class JGroupValidator extends JValidator {
027: protected String headMessage = "Please fix the following errors:";
028: protected List validators;
029:
030: public JGroupValidator() {
031: init();
032: }
033:
034: public JGroupValidator(JValidator[] aValidators,
035: boolean hasRemoteValidation) {
036: super (null, "", "", hasRemoteValidation);
037: init();
038: addValidators(aValidators);
039: }
040:
041: public JGroupValidator(JComponent aComponentToValidate,
042: String aMessage, String aGroupMessage,
043: boolean hasRemoteValidation) {
044: super (aComponentToValidate, aMessage, aGroupMessage,
045: hasRemoteValidation);
046: init();
047: }
048:
049: public JGroupValidator(String aHeadMessage,
050: boolean hasRemoteValidation) {
051: this (null, aHeadMessage, hasRemoteValidation);
052: }
053:
054: public JGroupValidator(JComponent aComponentToValidate,
055: String aHeadMessage, boolean hasRemoteValidation) {
056: this (aComponentToValidate, "", "", hasRemoteValidation);
057: headMessage = aHeadMessage;
058: }
059:
060: protected void init() {
061: validators = new Vector();
062: WosFramework.assignContributor(this ,
063: GroupValidatorUIContributor.class);
064: }
065:
066: public JValidator addValidator(JValidator aValidator) {
067: validators.add(aValidator);
068: aValidator.setGrouped(true);
069: return aValidator;
070: }
071:
072: public void addValidators(JValidator[] aValidators) {
073: addValidators(Arrays.asList(aValidators));
074: }
075:
076: public void addValidators(Collection aValidators) {
077: for (Iterator i = aValidators.iterator(); i.hasNext();)
078: addValidator((JValidator) i.next());
079: }
080:
081: public JValidator removeValidator(JValidator aValidator) {
082: validators.remove(aValidator);
083: return aValidator;
084: }
085:
086: protected boolean performValidation() {
087: StringBuffer message = new StringBuffer();
088: message.append("<html><b>").append(headMessage).append(
089: "</b><br>");
090: boolean result = true;
091:
092: for (Iterator i = validators.iterator(); i.hasNext();) {
093: JValidator validator = ((JValidator) i.next());
094: boolean isValid = validator.doValidation();
095: result &= isValid;
096: if (!isValid && validator.getGroupMessage().length() > 0)
097: message.append(validator.getGroupMessage()).append(
098: "<br>");
099: }
100:
101: if (result)
102: message = new StringBuffer();
103:
104: setGroupMessage(message.toString());
105: setOwnMessage(message.toString());
106: return result;
107: }
108:
109: public List getValidators() {
110: return validators;
111: }
112:
113: public void setValidators(List aList) {
114: validators = aList;
115: }
116:
117: public String getHeadMessage() {
118: return headMessage;
119: }
120:
121: public void setHeadMessage(String aString) {
122: headMessage = aString;
123: }
124: }
|