001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.x.form;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.HashSet;
018: import java.util.List;
019: import java.util.Set;
020:
021: import com.eviware.x.form.validators.RequiredValidator;
022:
023: public abstract class AbstractXFormField<T> implements XFormField {
024: private Set<XFormFieldListener> listeners;
025: private List<XFormFieldValidator> validators;
026: private RequiredValidator requiredValidator;
027: private ComponentEnabler enabler = null;
028:
029: public AbstractXFormField() {
030: }
031:
032: public abstract T getComponent();
033:
034: public void addFormFieldListener(XFormFieldListener listener) {
035: if (listeners == null)
036: listeners = new HashSet<XFormFieldListener>();
037:
038: listeners.add(listener);
039: }
040:
041: public void addFormFieldValidator(XFormFieldValidator validator) {
042: if (validators == null)
043: validators = new ArrayList<XFormFieldValidator>();
044:
045: validators.add(validator);
046: }
047:
048: public void addComponentEnabler(XFormField tf, String value) {
049: if (enabler == null) {
050: enabler = new ComponentEnabler(this );
051: }
052: enabler.add(tf, value);
053: }
054:
055: public boolean isRequired() {
056: return requiredValidator != null;
057: }
058:
059: public void removeFieldListener(XFormFieldListener listener) {
060: if (listeners != null)
061: listeners.remove(listener);
062: }
063:
064: public void removeFormFieldValidator(XFormFieldValidator validator) {
065: if (validators != null)
066: validators.remove(validator);
067: }
068:
069: public void setRequired(boolean required, String message) {
070: if (requiredValidator != null)
071: removeFormFieldValidator(requiredValidator);
072:
073: if (required) {
074: requiredValidator = new RequiredValidator(message);
075: addFormFieldValidator(requiredValidator);
076: }
077: }
078:
079: public ValidationMessage[] validate() {
080: if (validators == null || validators.isEmpty())
081: return null;
082:
083: ArrayList<ValidationMessage> messages = new ArrayList<ValidationMessage>();
084:
085: for (XFormFieldValidator validator : validators) {
086: ValidationMessage[] validateField = validator
087: .validateField(this );
088: if (validateField != null && validateField.length > 0)
089: messages.addAll(Arrays.asList(validateField));
090: }
091:
092: return messages.toArray(new ValidationMessage[messages.size()]);
093: }
094:
095: protected void fireValueChanged(String newValue, String oldValue) {
096: if (listeners == null)
097: return;
098:
099: for (XFormFieldListener listener : listeners) {
100: listener.valueChanged(this , newValue, oldValue);
101: }
102: }
103:
104: public Object getProperty(String name) {
105: return null;
106: }
107:
108: public void setProperty(String name, Object value) {
109: }
110:
111: public boolean isMultiRow() {
112: return false;
113: }
114: }
|