r1792 - in trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator: . swing swing/ui
Author: tchemit Date: 2010-03-18 10:52:42 +0100 (Thu, 18 Mar 2010) New Revision: 1792 Log: reformat code Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorDetector.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorEvent.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorField.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorMessage.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorScope.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorUtil.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/XWorkBeanValidator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidator.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessage.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListModel.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListMouseListener.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListRenderer.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableModel.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableMouseListener.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableRenderer.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/AbstractBeanValidatorUI.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/IconValidationUI.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/ImageValidationUI.java trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/TranslucentValidationUI.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidator.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidator.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,76 +20,83 @@ */ package jaxx.runtime.validator; -import java.beans.EventSetDescriptor; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.util.ConverterUtil; -import java.beans.Introspector; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.util.ArrayList; -import java.util.Collections; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; +import javax.swing.event.EventListenerList; +import java.beans.*; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeMap; -import javax.swing.event.EventListenerList; /** - * * A customized validator for a given bean. + * <p/> + * <b>Note:</b> The bean must be listenable on properyChange events (means must + * have public addPropertychangeListener and removePropertyChangeListener + * methods). * - * <b>Note:</b> The bean must be listenable on properyChange events (means - * must have public addPropertychangeListener and removePropertyChangeListener methods). - * + * @author tchemit <chemit@codelutin.com> * @param <B> type of the bean to validate. - * - * @author tchemit <chemit@codelutin.com> */ public class BeanValidator<B> { /** la nom de la propriété bean */ static public final String BEAN_PROERTY = "bean"; + /** la nom de la propriété contextName */ static public final String CONTEXT_NAME_PROPERTY = "contextName"; + /** la nom de l'état valid */ static public final String VALID_PROERTY = "valid"; + /** la nom de l'état changed */ static public final String CHANGED_PROERTY = "changed"; + /** to use log facility, just put in your code: log.info(\"...\"); */ static protected final Log log = LogFactory.getLog(BeanValidator.class); + /** the type of bean to watch */ protected final Class<B> beanClass; + /** the validation named context (can be null) */ protected String contextName; + /** to chain to a prent validator */ protected BeanValidator<?> parentValidator; - /** state to indicate that validator has changed since the last time bean was setted */ + + /** + * state to indicate that validator has changed since the last time bean was + * setted + */ protected boolean changed; + /** state of the validator (is true if no errors of error scope is found) */ protected boolean valid = true; + /** bean to be watched */ protected B bean; + /** to add and remove PropertyChangeListener on watched beans */ protected EventSetDescriptor beanEventDescriptor; + /** list of fields watched by this validator */ protected Set<BeanValidatorField<B>> fields; + /** map of conversion errors detected by this validator */ protected Map<String, String> conversionErrors; - /** xworks scope validator **/ + + /** xworks scope validator * */ protected EnumMap<BeanValidatorScope, XWorkBeanValidator<B>> validators; + /** listener that listens on bean modification */ protected PropertyChangeListener l; + /** delegate property change support */ protected PropertyChangeSupport pcs; + /** A list of event listeners for this validators */ protected EventListenerList listenerList = new EventListenerList(); @@ -97,7 +104,8 @@ this.beanClass = beanClass; pcs = new PropertyChangeSupport(this); conversionErrors = new TreeMap<String, String>(); - validators = new EnumMap<BeanValidatorScope, XWorkBeanValidator<B>>(BeanValidatorScope.class); + validators = new EnumMap<BeanValidatorScope, XWorkBeanValidator<B>>( + BeanValidatorScope.class); setContextName(contextName); @@ -105,6 +113,7 @@ @Override public void propertyChange(PropertyChangeEvent evt) { + // chaque modification lance la validation doValidate(); } }; @@ -131,10 +140,11 @@ } /** - * Retourne vrai si l'objet bean a ete modifie depuis le dernier - * {@link #setBean} + * Retourne vrai si l'objet bean a ete modifie depuis le dernier {@link + * #setBean} * - * @return <code>true</code> if bean was modify since last {@link #setBean(Object)} invocation + * @return <code>true</code> if bean was modify since last {@link + * #setBean(Object)} invocation */ public boolean isChanged() { return changed; @@ -188,7 +198,8 @@ * Test a the validator contains the field given his name * * @param fieldName the name of the searched field - * @return <code>true</code> if validator contaisn this field, <code>false</code> otherwise + * @return <code>true</code> if validator contaisn this field, + * <code>false</code> otherwise */ public boolean containsField(String fieldName) { BeanValidatorField<B> field = getField(fieldName); @@ -198,7 +209,8 @@ public boolean isValid(String fieldName) { BeanValidatorField<B> field = getField(fieldName); if (field == null) { - throw new IllegalArgumentException("could not find a validator field " + fieldName); + throw new IllegalArgumentException( + "could not find a validator field " + fieldName); } return field.isValid(); } @@ -231,9 +243,11 @@ if (oldBean != null) { try { - getBeanEventDescriptor(oldBean).getRemoveListenerMethod().invoke(oldBean, l); + EventSetDescriptor descriptor = getBeanEventDescriptor(oldBean); + descriptor.getRemoveListenerMethod().invoke(oldBean, l); } catch (Exception eee) { - log.info("Can't register as listener for bean " + beanClass + " for reason " + eee.getMessage(), eee); + log.info("Can't register as listener for bean " + beanClass + + " for reason " + eee.getMessage(), eee); } } this.bean = bean; @@ -249,9 +263,11 @@ } else { try { - getBeanEventDescriptor(bean).getAddListenerMethod().invoke(bean, l); + EventSetDescriptor descriptor = getBeanEventDescriptor(bean); + descriptor.getAddListenerMethod().invoke(bean, l); } catch (Exception eee) { - log.info("Can't register as listener for bean " + beanClass + " for reason " + eee.getMessage(), eee); + log.info("Can't register as listener for bean " + beanClass + + " for reason " + eee.getMessage(), eee); } validate(); } @@ -270,7 +286,10 @@ } // rebuild the fields initFields(); - pcs.firePropertyChange(CONTEXT_NAME_PROPERTY, oldValidationContextName, contextName); + pcs.firePropertyChange(CONTEXT_NAME_PROPERTY, + oldValidationContextName, + contextName + ); } public void setParentValidator(BeanValidator<?> parentValidator) { @@ -282,7 +301,7 @@ * <p/> * If an error occurs, then add an error in validator. * - * @param <T> the type of conversion + * @param <T> the type of conversion * @param fieldName the name of the bean property * @param value the value to convert * @param valueClass the type of converted value @@ -297,8 +316,8 @@ throw new IllegalArgumentException("valueClass can not be null"); } - // on ne convertit pas si il y a un bean et que le resultat de la validation - // pourra etre affiche quelque part + // on ne convertit pas si il y a un bean et que le resultat de la + // validation pourra etre affiche quelque part if (!canValidate() || value == null) { return null; } @@ -310,7 +329,8 @@ try { Converter converter = ConverterUtil.getConverter(valueClass); if (converter == null) { - throw new RuntimeException("could not find converter for the type " + valueClass); + throw new RuntimeException( + "could not find converter for the type " + valueClass); } result = (T) converter.convert(valueClass, value); /* Why this test ? if (result != null && !value.equals(result.toString())) { @@ -320,7 +340,8 @@ }*/ } catch (ConversionException e) { // get - conversionErrors.put(fieldName, "error.convertor." + Introspector.decapitalize(valueClass.getSimpleName())); + String s = Introspector.decapitalize(valueClass.getSimpleName()); + conversionErrors.put(fieldName, "error.convertor." + s); result = null; validate(); } @@ -330,10 +351,10 @@ /** * Methode pour forcer la revalidation d'un bean en mettant a jour les etats * internes. - * + * <p/> * La méthode appelle {@link #validate()} puis met à jour les etats internes * {@link #valid} et {@link #changed}. - * + * * @since 1.5 */ public void doValidate() { @@ -344,17 +365,16 @@ /** * il faut eviter le code re-intrant (durant une validation, une autre est - * demandee). Pour cela on fait la validation dans un thread, et tant - * que la premiere validation n'est pas fini, on ne repond pas aux - * solicitations. + * demandee). Pour cela on fait la validation dans un thread, et tant que la + * premiere validation n'est pas fini, on ne repond pas aux solicitations. * Cette method est public pour permettre de force une validation par - * programmation, ce qui est utile par exemple si le bean ne supporte - * pas les {@link PropertyChangeListener} - * - * TODO la methode devra repasser en protected et on utilise la - * methode {@link #doValidate()} car {@link #validate()} ne modifie pas - * les etats internes et cela en rend son utilisation delicate (le - * validateur entre dans un etat incoherent par rapport aux messages envoyés). + * programmation, ce qui est utile par exemple si le bean ne supporte pas + * les {@link PropertyChangeListener} + * <p/> + * TODO la methode devra repasser en protected et on utilise la methode + * {@link #doValidate()} car {@link #validate()} ne modifie pas les etats + * internes et cela en rend son utilisation delicate (le validateur entre + * dans un etat incoherent par rapport aux messages envoyés). */ public void validate() { @@ -415,7 +435,8 @@ @Override public String toString() { - return super.toString() + "<beanClass:" + beanClass + ", contextName:" + contextName + ">"; + return super.toString() + "<beanClass:" + beanClass + + ", contextName:" + contextName + ">"; } public void addBeanValidatorListener(BeanValidatorListener listener) { @@ -434,7 +455,8 @@ pcs.addPropertyChangeListener(listener); } - public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + public void addPropertyChangeListener(String propertyName, + PropertyChangeListener listener) { pcs.addPropertyChangeListener(propertyName, listener); } @@ -442,20 +464,34 @@ pcs.removePropertyChangeListener(listener); } - public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + public void removePropertyChangeListener(String propertyName, + PropertyChangeListener listener) { pcs.removePropertyChangeListener(propertyName, listener); } - /** @return <code>true</code> if validation is enabled, <code>false</code> otherwise. */ + /** + * @return <code>true</code> if validation is enabled, <code>false</code> + * otherwise. + */ protected boolean canValidate() { return !(bean == null || fields.isEmpty()); } - protected void fireFieldChanged(BeanValidatorField<B> field, BeanValidatorScope scope, String[] toAdd, String[] toDelete) { + protected void fireFieldChanged(BeanValidatorField<B> field, + BeanValidatorScope scope, + String[] toAdd, + String[] toDelete) { - BeanValidatorEvent evt = new BeanValidatorEvent(this, field, scope, toAdd, toDelete); + BeanValidatorEvent evt = new BeanValidatorEvent( + this, + field, + scope, + toAdd, + toDelete + ); - for (BeanValidatorListener listener : listenerList.getListeners(BeanValidatorListener.class)) { + for (BeanValidatorListener listener : + listenerList.getListeners(BeanValidatorListener.class)) { listener.onFieldChanged(evt); } } @@ -463,18 +499,26 @@ protected synchronized void initFields() { Set<String> detectedFieldNames = new HashSet<String>(); - EnumMap<BeanValidatorScope, Set<String>> tmp = new EnumMap<BeanValidatorScope, Set<String>>(BeanValidatorScope.class); - Set<BeanValidatorField<B>> detectedFields = new HashSet<BeanValidatorField<B>>(); + EnumMap<BeanValidatorScope, Set<String>> tmp; + tmp = new EnumMap<BeanValidatorScope, Set<String>>( + BeanValidatorScope.class + ); + Set<BeanValidatorField<B>> detectedFields = + new HashSet<BeanValidatorField<B>>(); validators.clear(); for (BeanValidatorScope scope : BeanValidatorScope.values()) { - String scopeContext = (contextName == null ? "" : contextName + "-") + scope.name().toLowerCase(); + String scopeContext = + (contextName == null ? "" : contextName + "-") + + scope.name().toLowerCase(); - XWorkBeanValidator<B> newValidator = new XWorkBeanValidator<B>(beanClass, scopeContext, false); + XWorkBeanValidator<B> newValidator = + new XWorkBeanValidator<B>(beanClass, scopeContext, false); Set<String> fieldNames = newValidator.getFieldNames(); if (log.isDebugEnabled()) { - log.debug("detected validators for scope " + scopeContext + " : " + fieldNames); + log.debug("detected validators for scope " + scopeContext + + " : " + fieldNames); } if (!fieldNames.isEmpty()) { // fields detected in this validator, keep it @@ -489,11 +533,13 @@ scopes.clear(); // detect scopes for the field for (BeanValidatorScope scope : BeanValidatorScope.values()) { - if (tmp.containsKey(scope) && tmp.get(scope).contains(fieldName)) { + if (tmp.containsKey(scope) && + tmp.get(scope).contains(fieldName)) { scopes.add(scope); } } - BeanValidatorField<B> f = new BeanValidatorField<B>(beanClass, fieldName, scopes); + BeanValidatorField<B> f = + new BeanValidatorField<B>(beanClass, fieldName, scopes); detectedFields.add(f); } tmp.clear(); @@ -504,8 +550,12 @@ protected EventSetDescriptor getBeanEventDescriptor(B bean) { if (beanEventDescriptor == null) { - // check that the bean is listenable, otherwise, can't use the validator on it - beanEventDescriptor = BeanValidatorUtil.getPropertyChangeListenerDescriptor(bean.getClass()); + // check that the bean is listenable, otherwise, can't use the + // validator on it + beanEventDescriptor = + BeanValidatorUtil.getPropertyChangeListenerDescriptor( + bean.getClass() + ); } return beanEventDescriptor; } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorDetector.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorDetector.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorDetector.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -45,16 +45,23 @@ public class BeanValidatorDetector { /** to use log facility, just put in your code: log.info(\"...\"); */ - private static final Log log = LogFactory.getLog(BeanValidatorDetector.class); + private static final Log log = + LogFactory.getLog(BeanValidatorDetector.class); - public SortedSet<BeanValidator<?>> detect(File sourceRoot, Class<?>... types) { - SortedSet<BeanValidator<?>> result = detect(BeanValidator.class, sourceRoot, null, types); + public SortedSet<BeanValidator<?>> detect(File sourceRoot, + Class<?>... types) { + SortedSet<BeanValidator<?>> result = + detect(BeanValidator.class, sourceRoot, null, types); return result; } - public SortedSet<BeanValidator<?>> detect(Class<?> validatorClass, File sourceRoot, Pattern contextFilter, Class<?>... types) { + public SortedSet<BeanValidator<?>> detect(Class<?> validatorClass, + File sourceRoot, + Pattern contextFilter, + Class<?>... types) { - SortedSet<BeanValidator<?>> result = new TreeSet<BeanValidator<?>>(new BeanValidatorComparator()); + SortedSet<BeanValidator<?>> result = + new TreeSet<BeanValidator<?>>(new BeanValidatorComparator()); for (Class<?> c : types) { File dir = getClassDir(sourceRoot, c); @@ -72,21 +79,29 @@ String[] realContexts = getContextsWithoutScopes(contexts); if (log.isDebugEnabled()) { - log.debug("realContexts : " + Arrays.toString(realContexts)); + log.debug("realContexts : " + + Arrays.toString(realContexts)); } if (contextFilter != null) { // filter contexts - realContexts = getFilterContexts(contextFilter, realContexts); + realContexts = getFilterContexts(contextFilter, + realContexts + ); if (log.isDebugEnabled()) { - log.debug("filterContexts : " + Arrays.toString(realContexts)); + log.debug("filterContexts : " + + Arrays.toString(realContexts)); } } for (String context : realContexts) { // on cherche le validateur - BeanValidator<?> validator = getValidator(validatorClass, c, context.isEmpty() ? null : context); + BeanValidator<?> validator = getValidator( + validatorClass, + c, + context.isEmpty() ? null : context + ); if (validator != null) { // on enregistre le validateur result.add(validator); @@ -111,14 +126,19 @@ * @return le validateur initialisé, ou <code>null</code> si aucun scope * détecté dans le validateur. */ - protected <B> BeanValidator<B> getValidator(Class<?> validatorClass, Class<B> klass, String context) { + protected <B> BeanValidator<B> getValidator(Class<?> validatorClass, + Class<B> klass, + String context) { // BeanValidator<B> valitator = new BeanValidator<B>(klass, context); BeanValidator<B> valitator; try { - valitator = (BeanValidator<B>) validatorClass.getConstructor(Class.class, String.class).newInstance(klass, context); + valitator = (BeanValidator<B>) validatorClass.getConstructor( + Class.class, String.class).newInstance(klass, context); } catch (Exception ex) { - throw new RuntimeException("could not instanciate validator " + validatorClass + " for reason " + ex.getMessage(), ex); + throw new RuntimeException( + "could not instanciate validator " + validatorClass + + " for reason " + ex.getMessage(), ex); } Set<BeanValidatorScope> scopes = valitator.getScopes(); @@ -136,7 +156,8 @@ } protected File getClassDir(File sourceRoot, Class<?> clazz) { - String path = clazz.getPackage().getName().replaceAll("\\.", File.separator); + String path = clazz.getPackage().getName(); + path = path.replaceAll("\\.", File.separator); File dir = new File(sourceRoot, path); return dir; } @@ -152,9 +173,13 @@ if (log.isDebugEnabled()) { log.debug("file " + file); } - String context = file.substring(filter.prefix.length(), file.length() - ValidatorFilenameFilter.SUFFIX.length()); + String context = file.substring( + filter.prefix.length(), + file.length() - ValidatorFilenameFilter.SUFFIX.length() + ); if (log.isDebugEnabled()) { - log.debug("detect " + clazz.getSimpleName() + " context [" + context + "]"); + log.debug("detect " + clazz.getSimpleName() + + " context [" + context + "]"); } result.add(context); } @@ -174,9 +199,15 @@ if (log.isDebugEnabled()) { log.debug("detect context : " + context); } - String realContext = context.substring(0, context.length() - scopeName.length()); + String realContext = context.substring( + 0, + context.length() - scopeName.length() + ); if (realContext.endsWith("-")) { - realContext = realContext.substring(0, realContext.length() - 1); + realContext = realContext.substring( + 0, + realContext.length() - 1 + ); } result.add(realContext); } @@ -184,7 +215,8 @@ return result.toArray(new String[result.size()]); } - protected String[] getFilterContexts(Pattern contextFilter, String[] realContexts) { + protected String[] getFilterContexts(Pattern contextFilter, + String[] realContexts) { List<String> result = new ArrayList<String>(); for (String c : realContexts) { Matcher m = contextFilter.matcher(c); @@ -220,8 +252,12 @@ @Override public int compare(BeanValidator<?> o1, BeanValidator<?> o2) { - String contextName1 = o1.getBeanClass().getSimpleName() + "-" + (o1.getContextName() == null ? "" : o1.getContextName()); - String contextName2 = o2.getBeanClass().getSimpleName() + "-" + (o2.getContextName() == null ? "" : o2.getContextName()); + String contextName1 = + o1.getBeanClass().getSimpleName() + "-" + + (o1.getContextName() == null ? "" : o1.getContextName()); + String contextName2 = + o2.getBeanClass().getSimpleName() + "-" + + (o2.getContextName() == null ? "" : o2.getContextName()); return contextName1.compareTo(contextName2); } } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorEvent.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorEvent.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorEvent.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -32,14 +32,22 @@ public class BeanValidatorEvent extends EventObject { private static final long serialVersionUID = 1L; + /** the field impacted by the validator */ BeanValidatorField<?> field; + /** the scope impacted by the event */ BeanValidatorScope scope; + String[] messagestoAdd; + String[] messagestoDelete; - public BeanValidatorEvent(BeanValidator<?> source, BeanValidatorField<?> field, BeanValidatorScope scope, String[] toAdd, String[] toDelete) { + public BeanValidatorEvent(BeanValidator<?> source, + BeanValidatorField<?> field, + BeanValidatorScope scope, + String[] toAdd, + String[] toDelete) { super(source); this.field = field; this.scope = scope; Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorField.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorField.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorField.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -22,43 +22,43 @@ import com.opensymphony.xwork2.validator.FieldValidator; +import java.util.*; + import static org.nuiton.i18n.I18n._; -import javax.swing.event.EventListenerList; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.StringTokenizer; - /** * Definition of a field to be handled in a {@link BeanValidator}. * <p/> - * A such class is only registred in {@link BeanValidator } when the field of the bean - * was found in validator xml configuration file for a {@link FieldValidator} only. + * A such class is only registred in {@link BeanValidator } when the field of + * the bean was found in validator xml configuration file for a {@link + * FieldValidator} only. * <p/> - * This class use properties {@link #beanClass}, {@link #name} to define - * his naturel order. + * This class use properties {@link #beanClass}, {@link #name} to define his + * naturel order. * * @author tchemit <chemit@codelutin.com> - * @param <B> the type of the bean handled by the validator and this field of validation. + * @param <B> the type of the bean handled by the validator and this field of + * validation. * @since 1.3 */ public class BeanValidatorField<B> { /** the class of bean */ protected final Class<B> beanClass; + /** name of field in bean */ protected final String name; + protected EnumMap<BeanValidatorScope, Set<String>> messages; - /** A list of event listeners for this validators */ - protected EventListenerList listenerList = new EventListenerList(); - public BeanValidatorField(Class<B> beanClass, String name, List<BeanValidatorScope> scopes) { + public BeanValidatorField(Class<B> beanClass, + String name, + List<BeanValidatorScope> scopes) { this.beanClass = beanClass; this.name = name; - messages = new EnumMap<BeanValidatorScope, Set<String>>(BeanValidatorScope.class); + messages = new EnumMap<BeanValidatorScope, Set<String>>( + BeanValidatorScope.class + ); for (BeanValidatorScope scope : scopes) { messages.put(scope, new HashSet<String>()); } @@ -73,8 +73,8 @@ } /** - * @return <code>true</code> if this field is valid (says is in error - * scope and has errors), <code>false</code> otherwise. + * @return <code>true</code> if this field is valid (says is in error scope + * and has errors), <code>false</code> otherwise. */ public boolean isValid() { return !hasErrors(); @@ -129,7 +129,9 @@ return messages.get(scope); } - public void updateMessages(BeanValidator<B> validator, BeanValidatorScope scope, List<String> messages) { + public void updateMessages(BeanValidator<B> validator, + BeanValidatorScope scope, + List<String> messages) { if (scope == null) { @@ -143,7 +145,8 @@ } if (!this.messages.containsKey(scope)) { - throw new IllegalArgumentException("the scope " + scope + " was not registred for " + this); + throw new IllegalArgumentException( + "the scope " + scope + " was not registred for " + this); } if (messages == null || messages.isEmpty()) { @@ -235,14 +238,16 @@ sb.append("<").append(super.toString()); sb.append(" beanClass:").append(beanClass); sb.append(", name:").append(name); - sb.append(", scopes:").append(messages == null ? "[]" : messages.keySet()); + sb.append(", scopes:"); + sb.append(messages == null ? "[]" : messages.keySet()); sb.append(", scope:").append(getScope()); //sb.append(", errors:").append(errors); sb.append('>'); return sb.toString(); } - protected void clearMessages(BeanValidatorScope scope, BeanValidator<B> validator) { + protected void clearMessages(BeanValidatorScope scope, + BeanValidator<B> validator) { // remove all messages Set<String> toDelete = getMessages(scope); Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorMessage.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorMessage.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorMessage.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -23,31 +23,29 @@ /** * The object to box a validation message. * - * @param <E> type of message (use for override {@link #compareTo(Object)} method. - * * @author tchemit <chemit@codelutin.com> + * @param <E> type of message (use for override {@link #compareTo(Object)} + * method. * @since 1.3 */ public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E> { - /** - * the validator that produce the message - */ + /** the validator that produce the message */ protected BeanValidator<?> validator; - /** - * the field that produce the message - */ + + /** the field that produce the message */ protected BeanValidatorField<?> field; - /** - * the label of the message (to be displayed somewhere) - */ + + /** the label of the message (to be displayed somewhere) */ protected String message; - /** - * the scope of the message - */ + + /** the scope of the message */ protected BeanValidatorScope scope; - public BeanValidatorMessage(BeanValidator<?> validator, BeanValidatorField<?> field, String message, BeanValidatorScope scope) { + public BeanValidatorMessage(BeanValidator<?> validator, + BeanValidatorField<?> field, + String message, + BeanValidatorScope scope) { this.field = field; this.validator = validator; this.message = message; @@ -86,6 +84,30 @@ } @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + BeanValidatorMessage<?> that = (BeanValidatorMessage<?>) o; + + return field.equals(that.field) && + message.equals(that.message) && + scope == that.scope; + } + + @Override + public int hashCode() { + int result = field.hashCode(); + result = 31 * result + message.hashCode(); + result = 31 * result + scope.hashCode(); + return result; + } + + @Override public String toString() { return scope + " - " + field.getI18nError(message); } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorScope.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorScope.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorScope.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -35,25 +35,26 @@ /** * the error scope level. - * - * When a message of a such scope is found on a validator, then the validator - * is invalid and modified. + * <p/> + * When a message of a such scope is found on a validator, then the + * validator is invalid and modified. */ ERROR(n_("validator.scope.error.label")), /** * the warning scope level. - * - * When a message of a such scope is found on a validator, then the validator - * is still valid but modified. + * <p/> + * When a message of a such scope is found on a validator, then the + * validator is still valid but modified. */ WARNING(n_("validator.scope.warning.label")), /** * the information scope level. - * + * <p/> * When a message of a sucg scope is found on a validator, then the * validator is still valid and not modified. */ INFO(n_("validator.scope.info.label")); + private final String label; BeanValidatorScope(String label) { Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorUtil.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/BeanValidatorUtil.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,20 +20,17 @@ */ package jaxx.runtime.validator; -import jaxx.runtime.*; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationManager; +import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; +import jaxx.runtime.JAXXObject; +import jaxx.runtime.JAXXValidator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.beans.BeanInfo; -import java.beans.EventSetDescriptor; -import java.beans.IntrospectionException; -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.reflect.InvocationTargetException; +import java.beans.*; import java.util.ArrayList; import java.util.List; @@ -46,9 +43,10 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(BeanValidatorUtil.class); + /** - * a shared value stack to allow external operations on it (for example - * add some datas in stack to be usedby validators + * a shared value stack to allow external operations on it (for example add + * some datas in stack to be usedby validators */ static private ValueStack sharedValueStack; @@ -59,7 +57,10 @@ ConfigurationManager confManager = new ConfigurationManager(); Configuration conf = confManager.getConfiguration(); - sharedValueStack = conf.getContainer().getInstance(ValueStackFactory.class).createValueStack(); + Container container = conf.getContainer(); + ValueStackFactory stackFactory = container.getInstance( + ValueStackFactory.class); + sharedValueStack = stackFactory.createValueStack(); if (log.isDebugEnabled()) { log.debug("init shared value stack " + sharedValueStack); } @@ -81,7 +82,9 @@ * @param excludeIds the list of validator id to exclude */ @SuppressWarnings({"unchecked"}) - public static void setValidatorBean(JAXXObject ui, Object bean, String... excludeIds) { + public static void setValidatorBean(JAXXObject ui, + Object bean, + String... excludeIds) { if (!JAXXValidator.class.isAssignableFrom(ui.getClass())) { return; } @@ -94,8 +97,10 @@ } } for (String validatorId : validatorIds) { - BeanValidator beanValidator = jaxxValidator.getValidator(validatorId); - if (bean == null || beanValidator.getBeanClass().isAssignableFrom(bean.getClass())) { + BeanValidator beanValidator = + jaxxValidator.getValidator(validatorId); + if (bean == null || beanValidator.getBeanClass().isAssignableFrom( + bean.getClass())) { // touch validator, only if fits the bean type (or bean is null) beanValidator.setBean(bean); } @@ -103,7 +108,8 @@ } /** - * Convinient method to set the changed property to all validators of an JAXXObject. + * Convinient method to set the changed property to all validators of an + * JAXXObject. * <p/> * It is possible to exclude some validator to be treated. * @@ -112,7 +118,9 @@ * @param excludeIds the list of validator id to exclude */ @SuppressWarnings({"unchecked"}) - public static void setValidatorChanged(JAXXObject ui, boolean newValue, String... excludeIds) { + public static void setValidatorChanged(JAXXObject ui, + boolean newValue, + String... excludeIds) { if (!JAXXValidator.class.isAssignableFrom(ui.getClass())) { return; } @@ -125,42 +133,52 @@ } } for (String validatorId : validatorIds) { - BeanValidator<?> beanValidator = jaxxValidator.getValidator(validatorId); + BeanValidator<?> beanValidator = + jaxxValidator.getValidator(validatorId); beanValidator.setChanged(newValue); } } /** - * Convert a value to a given type and then if was succesffull try to set it in the bean manage by the validator. + * Convert a value to a given type and then if was succesffull try to set it + * in the bean manage by the validator. * * @param validator validator to be involved * @param fieldName the name of the bean property * @param value the actual value to convert * @param valueClass the type of the conversion */ - public static void convert(BeanValidator<?> validator, String fieldName, String value, Class<?> valueClass) { + public static void convert(BeanValidator<?> validator, + String fieldName, + String value, + Class<?> valueClass) { Object result = validator.convert(fieldName, value, valueClass); if (result != null) { try { - BeanInfo info = Introspector.getBeanInfo(validator.getBean().getClass()); + BeanInfo info = + Introspector.getBeanInfo(validator.getBean().getClass()); - for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { - if (fieldName.equals(descriptor.getName()) && descriptor.getWriteMethod() != null) { + for (PropertyDescriptor descriptor : + info.getPropertyDescriptors()) { + if (fieldName.equals(descriptor.getName()) && + descriptor.getWriteMethod() != null) { - descriptor.getWriteMethod().invoke(validator.getBean(), result); + descriptor.getWriteMethod().invoke( + validator.getBean(), + result + ); break; } } - } catch (IntrospectionException e) { - log.error("could not obtain beanInfo for " + valueClass.getClass() + ", reason : " + e.getMessage(), e); - } catch (InvocationTargetException e) { - log.error("could not obtain beanInfo for " + valueClass.getClass() + ", reason : " + e.getMessage(), e); - } catch (IllegalAccessException e) { - log.error("could not obtain beanInfo for " + valueClass.getClass() + ", reason : " + e.getMessage(), e); + } catch (Exception e) { + log.error("could not obtain beanInfo for " + + valueClass.getClass() + ", reason : " + + e.getMessage(), e); } } else { - //fixme : conversion failed, we should be able to notify ui that values has changed ? + //fixme : conversion failed, we should be able to notify ui + // that values has changed ? // otherwise, bean value has not changed,... } } @@ -176,20 +194,28 @@ if (event.getAddListenerMethod() == null) { // no property event listener, so can not use the validator - throw new IllegalStateException("no addPropertyChangeListener method found for " + beanClass); + throw new IllegalStateException( + "no addPropertyChangeListener method found " + + "for " + beanClass); } if (event.getRemoveListenerMethod() == null) { // no property event listener, so can not use the validator - throw new IllegalStateException("no removePropertyChangeListener method found for " + beanClass); + throw new IllegalStateException( + "no removePropertyChangeListener method found" + + " for " + beanClass); } return event; } } // no property event listener, so can not use the validator - throw new IllegalStateException("no PropertyChangeListener access method found for " + beanClass); + throw new IllegalStateException( + "no PropertyChangeListener access method found for " + + beanClass); } catch (IntrospectionException ex) { - throw new IllegalStateException("could not acquire PropertyChangeListener bean info for " + beanClass + " for reason " + ex.getMessage(), ex); + throw new IllegalStateException( + "could not acquire PropertyChangeListener bean info for " + + beanClass + " for reason " + ex.getMessage(), ex); } } } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/XWorkBeanValidator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/XWorkBeanValidator.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/XWorkBeanValidator.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -24,72 +24,85 @@ import com.opensymphony.xwork2.ValidationAwareSupport; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationManager; +import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; -import com.opensymphony.xwork2.validator.ActionValidatorManager; -import com.opensymphony.xwork2.validator.DelegatingValidatorContext; -import com.opensymphony.xwork2.validator.FieldValidator; -import com.opensymphony.xwork2.validator.ValidationException; -import com.opensymphony.xwork2.validator.Validator; - -import java.util.ArrayList; -import java.util.Collection; +import com.opensymphony.xwork2.validator.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; /** - * * A customized validator for a given bean. + * <p/> + * Use the method {@link #validate(Object)} to obtain the messages detected by + * the validator for the given bean. * - * Use the method {@link #validate(Object)} to obtain the messages - * detected by the validator for the given bean. - * + * @author tchemit <chemit@codelutin.com> * @param <B> type of the bean to validate. - * - * @author tchemit <chemit@codelutin.com> * @since 1.3 */ public class XWorkBeanValidator<B> { /** to use log facility, just put in your code: log.info(\"...\"); */ private static final Log log = LogFactory.getLog(XWorkBeanValidator.class); - protected final static Map<String, List<String>> EMPTY_RESULT = Collections.unmodifiableMap(new HashMap<String, List<String>>()); + + protected final static Map<String, List<String>> EMPTY_RESULT = + Collections.unmodifiableMap(new HashMap<String, List<String>>()); + /** the type of bean to validate */ protected final Class<B> beanClass; + /** the validation named context (can be null) */ protected String contextName; + /** the list of field names detected for this validator */ protected Set<String> fieldNames; + /** a flag to include or not the default context validators */ protected boolean includeDefaultContext; + // -- // XWorks fields // -- + protected ValidationAwareSupport validationSupport; + protected DelegatingValidatorContext validationContext; + protected ActionValidatorManager validator; + protected ActionContext context; public XWorkBeanValidator(Class<B> beanClass, String contextName) { - this(beanClass, contextName, true, BeanValidatorUtil.getSharedValueStack()); + this(beanClass, + contextName, + true, + BeanValidatorUtil.getSharedValueStack() + ); } - public XWorkBeanValidator(Class<B> beanClass, String contextName, ValueStack vs) { + public XWorkBeanValidator(Class<B> beanClass, + String contextName, + ValueStack vs) { this(beanClass, contextName, true, vs); } - public XWorkBeanValidator(Class<B> beanClass, String contextName, boolean includeDefaultContext) { - this(beanClass, contextName, includeDefaultContext, BeanValidatorUtil.getSharedValueStack()); + public XWorkBeanValidator(Class<B> beanClass, + String contextName, + boolean includeDefaultContext) { + this(beanClass, + contextName, + includeDefaultContext, + BeanValidatorUtil.getSharedValueStack() + ); } - public XWorkBeanValidator(Class<B> beanClass, String contextName, boolean includeDefaultContext, ValueStack vs) { + public XWorkBeanValidator(Class<B> beanClass, + String contextName, + boolean includeDefaultContext, + ValueStack vs) { this.beanClass = beanClass; this.includeDefaultContext = includeDefaultContext; @@ -100,7 +113,10 @@ // create a standalone value stack ConfigurationManager confManager = new ConfigurationManager(); Configuration conf = confManager.getConfiguration(); - vs = conf.getContainer().getInstance(ValueStackFactory.class).createValueStack(); + Container container = conf.getContainer(); + ValueStackFactory stackFactory = + container.getInstance(ValueStackFactory.class); + vs = stackFactory.createValueStack(); if (log.isDebugEnabled()) { log.info("create a standalone value stack " + vs); } @@ -114,7 +130,10 @@ ActionContext.setContext(context); // init validator - validator = context.getContainer().getInstance(ActionValidatorManager.class, "no-annotations"); + Container container = context.getContainer(); + validator = container.getInstance(ActionValidatorManager.class, + "no-annotations" + ); // init context setContextName(contextName); @@ -144,7 +163,8 @@ * Test a the validator contains the field given his name * * @param fieldName the name of the searched field - * @return <code>true</code> if validator contaisn this field, <code>false</code> otherwise + * @return <code>true</code> if validator contaisn this field, + * <code>false</code> otherwise */ public boolean containsField(String fieldName) { return fieldNames.contains(fieldName); @@ -169,14 +189,14 @@ * Valide le bean donné et retourne les messages produits. * * @param bean le bean a valider (il doit etre non null) - * * @return le dictionnaire des messages produits par la validation indexées * par le nom du champs du bean impacté. */ public Map<String, List<String>> validate(B bean) { if (bean == null) { - throw new NullPointerException("bean can not be null in method validate"); + throw new NullPointerException( + "bean can not be null in method validate"); } Map<String, List<String>> result = EMPTY_RESULT; @@ -186,7 +206,8 @@ try { - //TC - 20081024 : since context is in a ThreadLocal variable, we must do the check + //TC - 20081024 : since context is in a ThreadLocal variable, + // we must do the check if (ActionContext.getContext() == null) { ActionContext.setContext(context); } @@ -194,20 +215,25 @@ validator.validate(bean, contextName, validationContext); if (log.isTraceEnabled()) { - log.trace("Action errors: " + validationContext.getActionErrors()); - log.trace("Action messages: " + validationContext.getActionMessages()); - log.trace("Field errors: " + validationContext.getFieldErrors()); + log.trace("Action errors: " + + validationContext.getActionErrors()); + log.trace("Action messages: " + + validationContext.getActionMessages()); + log.trace("Field errors: " + + validationContext.getFieldErrors()); } if (log.isDebugEnabled()) { - log.debug(this + " : " + validationContext.getFieldErrors()); + log.debug(this + " : " + + validationContext.getFieldErrors()); } if (validationContext.hasFieldErrors()) { - Map<?,?> messages = validationContext.getFieldErrors(); + Map<?, ?> messages = validationContext.getFieldErrors(); result = new HashMap<String, List<String>>(messages.size()); for (Object fieldName : messages.keySet()) { - Collection<?> c = (Collection<?>) messages.get(fieldName); + Collection<?> c = + (Collection<?>) messages.get(fieldName); List<String> mm = new ArrayList<String>(c.size()); for (Object message : c) { mm.add(message + ""); @@ -217,7 +243,8 @@ } } catch (ValidationException eee) { - log.warn("Error during validation on " + beanClass + " for reason : " + eee.getMessage(), eee); + log.warn("Error during validation on " + beanClass + + " for reason : " + eee.getMessage(), eee); } finally { // on nettoye toujours le validateur apres operation @@ -230,12 +257,11 @@ @Override public String toString() { - return super.toString() + "<beanClass:" + beanClass + ", contextName:" + contextName + ">"; + return super.toString() + "<beanClass:" + beanClass + + ", contextName:" + contextName + ">"; } - /** - * update the property {@link #fieldNames}, says search in XWorks - */ + /** update the property {@link #fieldNames}, says search in XWorks */ protected synchronized void initFields() { if (fieldNames != null) { @@ -263,7 +289,10 @@ continue; } FieldValidator fieldValidator = (FieldValidator) v; - log.debug("context " + contextName + " - field " + fieldValidator.getFieldName()); + if (log.isDebugEnabled()) { + log.debug("context " + contextName + " - field " + + fieldValidator.getFieldName()); + } String fName = fieldValidator.getFieldName(); detectedFieldNames.add(fName); } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidator.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidator.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidator.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,31 +20,30 @@ */ package jaxx.runtime.validator.swing; +import jaxx.runtime.validator.BeanValidator; +import jaxx.runtime.validator.BeanValidatorField; import jaxx.runtime.validator.swing.ui.AbstractBeanValidatorUI; import jaxx.runtime.validator.swing.ui.IconValidationUI; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jdesktop.jxlayer.JXLayer; -import javax.swing.JComponent; -import javax.swing.SwingUtilities; -import java.awt.Container; +import javax.swing.*; +import java.awt.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import jaxx.runtime.validator.BeanValidator; -import jaxx.runtime.validator.BeanValidatorField; /** * La surcharge de {@link BeanValidator} pour les ui swing * <p/> * /** * <p/> - * Permet d'ajouter facilement le support de la validation des champs d'un - * bean et de le relier a une interface graphique. - * Utilise xwork pour la validation et JXLayer pour la visualisation. + * Permet d'ajouter facilement le support de la validation des champs d'un bean + * et de le relier a une interface graphique. Utilise xwork pour la validation + * et JXLayer pour la visualisation. * <p/> * <p/> * Le mieux pour son integration dans Jaxx est de faire de la generation pour @@ -54,30 +53,32 @@ * myValidor.getBean().get<field>(); * </pre> * <p/> - * et ceci pour chaque field ajoute a la map fieldRepresentation. De cette - * facon meme si le champs field est en texte on a une verification de son - * existance a la compilation. + * et ceci pour chaque field ajoute a la map fieldRepresentation. De cette facon + * meme si le champs field est en texte on a une verification de son existance a + * la compilation. * <p/> * <p/> * La representation en tag pourrait etre * <pre> - * <validator id="myValidator" beanClass="{Personne.class}" errorList="$list"> + * <validator id="myValidator" beanClass="{Personne.class}" + * errorList="$list"> * <field name="name" component="$name"/> * <field name="firstName" component="$firstName"/> * <field name="birthDate" component="$birthDate"/> * </validator> - * <validator beanClass="{Personne.class}" autoField="true" errorList="$list"> + * <validator beanClass="{Personne.class}" autoField="true" + * errorList="$list"> * <fieldRepresentation name="name" component="$lastName"/> * </validator> * </pre> * <p/> * dans le premier exemple on fait un mapping explicite des champs, mais on voit - * que le nom du composant graphique est le meme que celui du champs. Pour eviter - * de longue saisie, il est possible d'utiliser le flag <b>autoField</b> - * qui pour chaque champs du ayant une methode get du bean recherche un composant - * avec cet Id. Il est aussi possible de surcharge un champs explicitement - * comme ici name, dans le cas ou le composant qui porterait ce nom serait - * utilise pour autre chose. + * que le nom du composant graphique est le meme que celui du champs. Pour + * eviter de longue saisie, il est possible d'utiliser le flag <b>autoField</b> + * qui pour chaque champs du ayant une methode get du bean recherche un + * composant avec cet Id. Il est aussi possible de surcharge un champs + * explicitement comme ici name, dans le cas ou le composant qui porterait ce + * nom serait utilise pour autre chose. * <p/> * <p/> * Il faut un handler particulier pour ce composant car les attributs @@ -97,34 +98,45 @@ * for (Method m : beanClass.getMethod()) { * if (m.getName().startsWith("get")) { * String fieldName = m.getName().substring(3).toLowerCase(); - * $myValidator.setFieldRepresentation(fieldName, $objectMap.get(fieldName)); + * $myValidator.setFieldRepresentation(fieldName, + * $objectMap.get(fieldName)); * } * } * // pour chaque tag fieldRepresentation * myValidator.setFieldRepresentation("name", $lastName); - * // si beanClass est specifie et n'est pas Object, on force l'acces au champs + * // si beanClass est specifie et n'est pas Object, on force l'acces au + * champs * // pour validation a la compilation * $myValidator.getBean().getName(); * $objectMap.put("myValidator", $myValidator); * } * </pre> * - * @param <B> le type de bean a valider * @author poussin <poussin@codelutin.com> * @author tchemit <chemit@codelutin.com> - * @version 1.0 + * @version 1.0 + * @param <B> le type de bean a valider */ public class SwingValidator<B> extends BeanValidator<B> { /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(SwingValidator.class); - static private final Class<? extends AbstractBeanValidatorUI> DEFAULT_UI_CLASS = IconValidationUI.class; - /** permet de faire le lien en un champs du bean et l'objet qui permet de l'editer */ + + static private final Class<? extends AbstractBeanValidatorUI> + DEFAULT_UI_CLASS = IconValidationUI.class; + + /** + * permet de faire le lien en un champs du bean et l'objet qui permet de + * l'editer + */ protected Map<String, JComponent> fieldRepresentation; + /** Object servant a contenir la liste des erreurs */ protected SwingValidatorMessageListModel errorListModel; + /** Object servant a contenir la liste des erreurs */ protected SwingValidatorMessageTableModel errorTableModel; + /** ui renderer class */ protected Class<? extends AbstractBeanValidatorUI> uiClass; @@ -135,10 +147,9 @@ /** * To reload a bean in the validator. - * - * This method is used to reload ui, since some editors - * could not exist when validator is init, so some messages - * should not be attached to an editor. + * <p/> + * This method is used to reload ui, since some editors could not exist when + * validator is init, so some messages should not be attached to an editor. */ public void reloadBean() { B b = getBean(); @@ -156,7 +167,8 @@ return uiClass; } - public void setErrorListModel(SwingValidatorMessageListModel errorListModel) { + public void setErrorListModel( + SwingValidatorMessageListModel errorListModel) { this.errorListModel = errorListModel; if (errorListModel != null) { // register the validator in the model list @@ -164,7 +176,8 @@ } } - public void setErrorTableModel(SwingValidatorMessageTableModel errorTableModel) { + public void setErrorTableModel( + SwingValidatorMessageTableModel errorTableModel) { this.errorTableModel = errorTableModel; if (errorTableModel != null) { // register the validator in the model table @@ -206,8 +219,8 @@ } /** - * Permet d'indiquer le composant graphique responsable de l'affichage - * d'un attribut du bean + * Permet d'indiquer le composant graphique responsable de l'affichage d'un + * attribut du bean * * @param fieldname the field name in the bean * @param c the editor component for the field @@ -216,13 +229,15 @@ BeanValidatorField<B> field = getField(fieldname); if (field == null) { // no field registred in the validator - log.warn("the field '" + fieldname + "' is not defined in validator (no rules on it)"); + log.warn("the field '" + fieldname + + "' is not defined in validator (no rules on it)"); return; } fieldRepresentation.put(fieldname, c); } - public void setFieldRepresentation(Map<String, JComponent> fieldRepresentation) { + public void setFieldRepresentation( + Map<String, JComponent> fieldRepresentation) { for (Map.Entry<String, JComponent> e : fieldRepresentation.entrySet()) { setFieldRepresentation(e.getKey(), e.getValue()); } @@ -247,9 +262,15 @@ // use the default one uiClass = DEFAULT_UI_CLASS; } - for (Entry<String, JComponent> entry : fieldRepresentation.entrySet()) { + for (Entry<String, JComponent> entry : + fieldRepresentation.entrySet()) { try { - setMessageRepresentation(entry.getKey(), null, entry.getValue(), uiClass); + setMessageRepresentation( + entry.getKey(), + null, + entry.getValue(), + uiClass + ); } catch (Exception e) { throw new RuntimeException(e); } @@ -258,8 +279,15 @@ }); } - protected void setMessageRepresentation(String fieldname, JComponent old, JComponent c, Class<? extends AbstractBeanValidatorUI> uiClass) - throws InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException { + protected void setMessageRepresentation( + String fieldname, + JComponent old, + JComponent c, + Class<? extends AbstractBeanValidatorUI> uiClass) + throws InvocationTargetException, + IllegalAccessException, + InstantiationException, + NoSuchMethodException { if (old == c) { // same component, nothing to do return; @@ -267,7 +295,8 @@ BeanValidatorField<B> field = getField(fieldname); if (field == null) { - // this case should not appear since fieldName has already been check in method addFieldRepresentation + // this case should not appear since fieldName has already been + // check in method addFieldRepresentation return; } if (old != null) { @@ -287,7 +316,8 @@ // ajout du jxlayer sous ce composant Container container = c.getParent(); if (container instanceof JXLayer<?>) { - Constructor<? extends AbstractBeanValidatorUI> cons = uiClass.getConstructor(BeanValidatorField.class); + Constructor<? extends AbstractBeanValidatorUI> cons = + uiClass.getConstructor(BeanValidatorField.class); AbstractBeanValidatorUI ui = cons.newInstance(field); ui.setEnabled(true); JXLayer<JComponent> jx = (JXLayer<JComponent>) container; Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessage.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessage.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessage.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,33 +20,41 @@ */ package jaxx.runtime.validator.swing; -import javax.swing.JComponent; import jaxx.runtime.validator.BeanValidatorField; import jaxx.runtime.validator.BeanValidatorMessage; import jaxx.runtime.validator.BeanValidatorScope; +import javax.swing.*; + /** * The object to box a validation message within an u. * * @author tchemit <chemit@codelutin.com> + * @see BeanValidatorMessage * @since 1.3 - * @see BeanValidatorMessage */ public class SwingValidatorMessage extends BeanValidatorMessage<SwingValidatorMessage> { - /** - * the optional field's editor - */ + /** the optional field's editor */ protected JComponent editor; + protected String fieldName; - public SwingValidatorMessage(SwingValidator<?> validator, BeanValidatorField<?> field, String message, BeanValidatorScope scope, JComponent editor) { + public SwingValidatorMessage(SwingValidator<?> validator, + BeanValidatorField<?> field, + String message, + BeanValidatorScope scope, + JComponent editor) { super(validator, field, message, scope); fieldName = field.getName(); this.editor = editor; } - public SwingValidatorMessage(SwingValidator<?> validator, String fieldName, String message, BeanValidatorScope scope, JComponent editor) { + public SwingValidatorMessage(SwingValidator<?> validator, + String fieldName, + String message, + BeanValidatorScope scope, + JComponent editor) { super(validator, null, message, scope); this.fieldName = fieldName; this.editor = editor; @@ -76,8 +84,33 @@ } @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + SwingValidatorMessage that = (SwingValidatorMessage) o; + + return fieldName.equals(that.fieldName) && + message.equals(that.message) && + scope == that.scope; + } + + @Override + public int hashCode() { + int result = fieldName.hashCode(); + result = 31 * result + message.hashCode(); + result = 31 * result + scope.hashCode(); + return result; + } + + @Override public String toString() { - String s = scope + " - " + (field == null ? message : field.getI18nError(message)); + String s = scope + " - " + + (field == null ? message : field.getI18nError(message)); if (editor == null) { return s; } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListModel.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListModel.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListModel.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -21,19 +21,18 @@ package jaxx.runtime.validator.swing; import jaxx.runtime.validator.BeanValidatorEvent; - -import javax.swing.AbstractListModel; -import javax.swing.JComponent; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import jaxx.runtime.validator.BeanValidatorField; import jaxx.runtime.validator.BeanValidatorListener; import jaxx.runtime.validator.BeanValidatorScope; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import javax.swing.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + /** * The model of the list of validation's messages * @@ -44,10 +43,13 @@ implements BeanValidatorListener { private static final long serialVersionUID = 1L; + /** to use log facility, just put in your code: log.info(\"...\"); */ private static Log log = LogFactory.getLog(SwingValidatorMessageListModel.class); + /** list of registred validators */ protected transient List<SwingValidator<?>> validators; + /** list of messages actual displayed */ protected List<SwingValidatorMessage> data; @@ -62,7 +64,9 @@ public void registerValidator(SwingValidator<?> validator) { if (validators.contains(validator)) { - throw new IllegalArgumentException("the validator " + validator + " is already registred in " + this); + throw new IllegalArgumentException( + "the validator " + validator + " is already registred in " + + this); } validators.add(validator); validator.addBeanValidatorListener(this); @@ -119,16 +123,28 @@ protected void ensureRowIndex(int index) throws ArrayIndexOutOfBoundsException { if (index < -1 || index >= getSize()) { - throw new ArrayIndexOutOfBoundsException("the rowIndex was " + index + ", but should be int [0," + (getSize() - 1) + "]"); + throw new ArrayIndexOutOfBoundsException( + "the rowIndex was " + index + ", but should be int [0," + + (getSize() - 1) + "]"); } } - protected void addMessages(SwingValidator<?> validator, BeanValidatorField<?> field, BeanValidatorScope scope, boolean sort, String... messages) { + protected void addMessages(SwingValidator<?> validator, + BeanValidatorField<?> field, + BeanValidatorScope scope, + boolean sort, + String... messages) { JComponent editor = validator.getFieldRepresentation(field.getName()); // add new errors for (String error : messages) { - SwingValidatorMessage row = new SwingValidatorMessage(validator, field, error, scope, editor); + SwingValidatorMessage row = new SwingValidatorMessage( + validator, + field, + error, + scope, + editor + ); data.add(row); if (!sort) { fireIntervalAdded(this, data.size() - 1, data.size() - 1); @@ -145,16 +161,24 @@ } } - protected void removeMessages(SwingValidator<?> validator, BeanValidatorField<?> field, BeanValidatorScope scope, boolean notify, String... messages) { + protected void removeMessages(SwingValidator<?> validator, + BeanValidatorField<?> field, + BeanValidatorScope scope, + boolean notify, + String... messages) { - List<String> messagesToDel = new ArrayList<String>(Arrays.asList(messages)); + List<String> messagesToDel = + new ArrayList<String>(Arrays.asList(messages)); // do it in reverse mode (only one pass in that way since index // will stay coherent while removing them) for (int i = getSize() - 1; i > -1; i--) { SwingValidatorMessage error = data.get(i); - if (error.getValidator() == validator && error.getScope() == scope && error.getField() == field && messagesToDel.contains(error.getMessage())) { + if (error.getValidator() == validator && + error.getScope() == scope && + error.getField() == field && + messagesToDel.contains(error.getMessage())) { // remove the message data.remove(i); if (notify) { Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListMouseListener.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListMouseListener.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListMouseListener.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -23,22 +23,24 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import javax.swing.JComponent; -import javax.swing.JList; +import javax.swing.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; /** - * A mouse listener to put on a {@link JList} with a {@link SwingValidatorMessageListModel} as a model. + * A mouse listener to put on a {@link JList} with a {@link + * SwingValidatorMessageListModel} as a model. * <p/> - * When a double click occurs, find the selected error in model and then focus to the associated component of error. + * When a double click occurs, find the selected error in model and then focus + * to the associated component of error. * * @author tchemit <chemit@codelutin.com> */ public class SwingValidatorMessageListMouseListener extends MouseAdapter { /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(SwingValidatorMessageListMouseListener.class); + static private Log log = + LogFactory.getLog(SwingValidatorMessageListMouseListener.class); @Override public void mouseClicked(MouseEvent e) { @@ -60,17 +62,21 @@ protected SwingValidatorMessage getSelectedMessage(MouseEvent e) { JList list = (JList) e.getSource(); if (!(list.getModel() instanceof SwingValidatorMessageListModel)) { - log.warn("model must be a " + SwingValidatorMessageListModel.class + ", but was " + list.getModel()); + log.warn("model must be a " + + SwingValidatorMessageListModel.class + ", but was " + + list.getModel()); return null; } - SwingValidatorMessageListModel model = (SwingValidatorMessageListModel) list.getModel(); + SwingValidatorMessageListModel model = + (SwingValidatorMessageListModel) list.getModel(); int index = list.getSelectionModel().getMinSelectionIndex(); if (index == -1) { // nothing is selected return null; } - SwingValidatorMessage entry = (SwingValidatorMessage) model.getElementAt(index); + SwingValidatorMessage entry = + (SwingValidatorMessage) model.getElementAt(index); if (log.isDebugEnabled()) { log.debug("selected index: " + index + " : error: " + entry); } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListRenderer.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListRenderer.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageListRenderer.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,26 +20,27 @@ */ package jaxx.runtime.validator.swing; -import javax.swing.ImageIcon; -import javax.swing.JLabel; -import javax.swing.JList; -import java.awt.Component; -import javax.swing.DefaultListCellRenderer; import jaxx.runtime.validator.BeanValidatorScope; + +import javax.swing.*; +import java.awt.*; + import static org.nuiton.i18n.I18n._; /** - * A simple render of a table of validator's messages, says a table that use - * a {@link SwingValidatorMessageTableModel} model. + * A simple render of a table of validator's messages, says a table that use a + * {@link SwingValidatorMessageTableModel} model. * * @author tchemit <chemit@codelutin.com> + * @see SwingValidatorMessageTableModel * @since 1.3 - * @see SwingValidatorMessageTableModel */ public class SwingValidatorMessageListRenderer extends DefaultListCellRenderer { private static final long serialVersionUID = 1L; + protected String format = "%1$-20s - %2$s"; + protected String formatTip = "%1$-20s - %2$-20s : %3$s"; public SwingValidatorMessageListRenderer() { @@ -58,9 +59,20 @@ } @Override - public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + public Component getListCellRendererComponent(JList list, + Object value, + int index, + boolean isSelected, + boolean cellHasFocus) { - JLabel rendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + JLabel rendererComponent = (JLabel) + super.getListCellRendererComponent( + list, + value, + index, + isSelected, + cellHasFocus + ); SwingValidatorMessage model = (SwingValidatorMessage) value; @@ -68,7 +80,11 @@ ImageIcon icon = SwingValidatorUtil.getIcon(model.getScope()); // field name - String fieldName = getFieldName(list, model.getField().getName(), index); + String fieldName = getFieldName( + list, + model.getField().getName(), + index + ); // message String message = getMessage(model); @@ -101,8 +117,10 @@ } public String getFieldName(JList list, String value, int row) { - SwingValidatorMessageListModel tableModel = (SwingValidatorMessageListModel) list.getModel(); - SwingValidatorMessage model = (SwingValidatorMessage) tableModel.getElementAt(row); + SwingValidatorMessageListModel tableModel = + (SwingValidatorMessageListModel) list.getModel(); + SwingValidatorMessage model = + (SwingValidatorMessage) tableModel.getElementAt(row); String fieldName = SwingValidatorUtil.getFieldName(model, value); return fieldName; } Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableModel.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableModel.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableModel.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -21,18 +21,18 @@ package jaxx.runtime.validator.swing; import jaxx.runtime.validator.BeanValidatorEvent; +import jaxx.runtime.validator.BeanValidatorField; +import jaxx.runtime.validator.BeanValidatorListener; +import jaxx.runtime.validator.BeanValidatorScope; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; -import javax.swing.JComponent; +import javax.swing.*; import javax.swing.table.AbstractTableModel; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import jaxx.runtime.validator.BeanValidatorField; -import jaxx.runtime.validator.BeanValidatorListener; -import jaxx.runtime.validator.BeanValidatorScope; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; /** * The model of the table of errors. @@ -47,23 +47,24 @@ implements BeanValidatorListener { private static final long serialVersionUID = 1L; - /** - * to use log facility, just put in your code: log.info(\"...\"); - */ - private static Log log = LogFactory.getLog(SwingValidatorMessageTableMouseListener.class); - public static final String[] columnNames = {"validator.scope", "validator.field", "validator.message"}; - public static final Class<?>[] columnClasses = {BeanValidatorScope.class, String.class, String.class}; - /** - * list of registred validators - */ + + /** to use log facility, just put in your code: log.info(\"...\"); */ + private static Log log = + LogFactory.getLog(SwingValidatorMessageTableMouseListener.class); + + public static final String[] columnNames = + {"validator.scope", "validator.field", "validator.message"}; + + public static final Class<?>[] columnClasses = + {BeanValidatorScope.class, String.class, String.class}; + + /** list of registred validators */ protected transient List<SwingValidator<?>> validators; - /** - * list of messages actual displayed - */ + + /** list of messages actual displayed */ protected List<SwingValidatorMessage> data; public SwingValidatorMessageTableModel() { - super(); validators = new ArrayList<SwingValidator<?>>(); data = new ArrayList<SwingValidatorMessage>(); } @@ -78,21 +79,32 @@ */ public void registerValidator(SwingValidator<?> validator) { if (validators.contains(validator)) { - throw new IllegalArgumentException("the validator " + validator + " is already registred in " + this); + throw new IllegalArgumentException( + "the validator " + validator + " is already registred in " + + this); } validators.add(validator); validator.addBeanValidatorListener(this); } - public void addMessages(SwingValidator<?> validator, String fieldName, BeanValidatorScope scope, String... messages) { + public void addMessages(SwingValidator<?> validator, + String fieldName, + BeanValidatorScope scope, + String... messages) { addMessages(validator, fieldName, scope, true, messages); } - public void addMessages(JComponent editor, String fieldName, BeanValidatorScope scope, String... messages) { + public void addMessages(JComponent editor, + String fieldName, + BeanValidatorScope scope, + String... messages) { addMessages(editor, fieldName, scope, true, messages); } - public void addMessages(SwingValidator<?> validator, BeanValidatorField<?> field, BeanValidatorScope scope, String... messages) { + public void addMessages(SwingValidator<?> validator, + BeanValidatorField<?> field, + BeanValidatorScope scope, + String... messages) { addMessages(validator, field, scope, true, messages); } @@ -103,7 +115,8 @@ for (int i = getRowCount() - 1; i > -1; i--) { SwingValidatorMessage error = data.get(i); - if (error.getEditor() == editor && (scope == null || error.getScope() == scope)) { + if (error.getEditor() == editor && + (scope == null || error.getScope() == scope)) { // remove the message data.remove(i); fireTableRowsDeleted(i, i); @@ -111,15 +124,23 @@ } } - public void removeMessages(SwingValidator<?> validator, String fieldName, BeanValidatorScope scope, String... messages) { + public void removeMessages(SwingValidator<?> validator, + String fieldName, + BeanValidatorScope scope, + String... messages) { removeMessages(validator, fieldName, scope, true, messages); } - public void removeMessages(JComponent editor, String fieldName, BeanValidatorScope scope) { + public void removeMessages(JComponent editor, + String fieldName, + BeanValidatorScope scope) { removeMessages(editor, fieldName, scope, true); } - public void removeMessages(SwingValidator<?> validator, BeanValidatorField<?> field, BeanValidatorScope scope, String... messages) { + public void removeMessages(SwingValidator<?> validator, + BeanValidatorField<?> field, + BeanValidatorScope scope, + String... messages) { removeMessages(validator, field, scope, true, messages); } @@ -223,24 +244,43 @@ return null; } - protected void ensureRowIndex(int rowIndex) throws ArrayIndexOutOfBoundsException { + protected void ensureRowIndex(int rowIndex) + throws ArrayIndexOutOfBoundsException { if (rowIndex < -1 || rowIndex >= getRowCount()) { - throw new ArrayIndexOutOfBoundsException("the rowIndex was " + rowIndex + ", but should be int [0," + (getRowCount() - 1) + "]"); + throw new ArrayIndexOutOfBoundsException( + "the rowIndex was " + rowIndex + ", but should be int [0," + + (getRowCount() - 1) + "]"); } } - protected void ensureColumnIndex(int index) throws ArrayIndexOutOfBoundsException { + protected void ensureColumnIndex(int index) + throws ArrayIndexOutOfBoundsException { if (index < -1 || index >= getColumnCount()) { - throw new ArrayIndexOutOfBoundsException("the columnIndex was " + index + ", but should be int [0," + (getColumnCount() - 1) + "]"); + throw new ArrayIndexOutOfBoundsException( + "the columnIndex was " + index + ", but should be int [0," + + (getColumnCount() - 1) + "]"); } } - protected void addMessages(SwingValidator<?> validator, BeanValidatorField<?> field, BeanValidatorScope scope, boolean sort, String... messages) { + protected void addMessages(SwingValidator<?> validator, + BeanValidatorField<?> field, + BeanValidatorScope scope, + boolean sort, + String... messages) { - JComponent editor = validator == null ? null : validator.getFieldRepresentation(field.getName()); + JComponent editor = validator == null ? + null : + validator.getFieldRepresentation(field.getName()); // add new errors for (String error : messages) { - SwingValidatorMessage row = new SwingValidatorMessage(validator, field, error, scope, editor); + SwingValidatorMessage row = + new SwingValidatorMessage( + validator, + field, + error, + scope, + editor + ); data.add(row); if (!sort) { fireTableRowsInserted(data.size() - 1, data.size() - 1); @@ -257,12 +297,25 @@ } } - protected void addMessages(SwingValidator<?> validator, String fieldName, BeanValidatorScope scope, boolean sort, String... messages) { + protected void addMessages(SwingValidator<?> validator, + String fieldName, + BeanValidatorScope scope, + boolean sort, + String... messages) { - JComponent editor = validator == null ? null : validator.getFieldRepresentation(fieldName); + JComponent editor = validator == null ? + null : + validator.getFieldRepresentation(fieldName); // add new errors for (String error : messages) { - SwingValidatorMessage row = new SwingValidatorMessage(validator, fieldName, error, scope, editor); + SwingValidatorMessage row = + new SwingValidatorMessage( + validator, + fieldName, + error, + scope, + editor + ); data.add(row); if (!sort) { fireTableRowsInserted(data.size() - 1, data.size() - 1); @@ -279,11 +332,22 @@ } } - protected void addMessages(JComponent editor, String fieldName, BeanValidatorScope scope, boolean sort, String... messages) { + protected void addMessages(JComponent editor, + String fieldName, + BeanValidatorScope scope, + boolean sort, + String... messages) { // add new errors for (String error : messages) { - SwingValidatorMessage row = new SwingValidatorMessage(null, fieldName, error, scope, editor); + SwingValidatorMessage row = + new SwingValidatorMessage( + null, + fieldName, + error, + scope, + editor + ); data.add(row); if (!sort) { fireTableRowsInserted(data.size() - 1, data.size() - 1); @@ -300,16 +364,24 @@ } } - protected void removeMessages(SwingValidator<?> validator, BeanValidatorField<?> field, BeanValidatorScope scope, boolean notify, String... messages) { + protected void removeMessages(SwingValidator<?> validator, + BeanValidatorField<?> field, + BeanValidatorScope scope, + boolean notify, + String... messages) { - List<String> messagesToDel = new ArrayList<String>(Arrays.asList(messages)); + List<String> messagesToDel = + new ArrayList<String>(Arrays.asList(messages)); // do it in reverse mode (only one pass in that way since index // will stay coherent while removing them) for (int i = getRowCount() - 1; i > -1; i--) { SwingValidatorMessage error = data.get(i); - if (error.getValidator() == validator && error.getScope() == scope && error.getFieldName().equals(field.getName()) && messagesToDel.contains(error.getMessage())) { + if (error.getValidator() == validator && + error.getScope() == scope && + error.getFieldName().equals(field.getName()) && + messagesToDel.contains(error.getMessage())) { // remove the message data.remove(i); if (notify) { @@ -319,16 +391,24 @@ } } - protected void removeMessages(SwingValidator<?> validator, String fieldName, BeanValidatorScope scope, boolean notify, String... messages) { + protected void removeMessages(SwingValidator<?> validator, + String fieldName, + BeanValidatorScope scope, + boolean notify, + String... messages) { - List<String> messagesToDel = new ArrayList<String>(Arrays.asList(messages)); + List<String> messagesToDel = + new ArrayList<String>(Arrays.asList(messages)); // do it in reverse mode (only one pass in that way since index // will stay coherent while removing them) for (int i = getRowCount() - 1; i > -1; i--) { SwingValidatorMessage error = data.get(i); - if (error.getValidator() == validator && error.getScope() == scope && error.getFieldName().equals(fieldName) && messagesToDel.contains(error.getMessage())) { + if (error.getValidator() == validator && + error.getScope() == scope && + error.getFieldName().equals(fieldName) && + messagesToDel.contains(error.getMessage())) { // remove the message data.remove(i); if (notify) { @@ -338,14 +418,19 @@ } } - protected void removeMessages(JComponent editor, String fieldName, BeanValidatorScope scope, boolean notify) { + protected void removeMessages(JComponent editor, + String fieldName, + BeanValidatorScope scope, + boolean notify) { // do it in reverse mode (only one pass in that way since index // will stay coherent while removing them) for (int i = getRowCount() - 1; i > -1; i--) { SwingValidatorMessage error = data.get(i); - if (error.getEditor() == editor && (scope == null || error.getScope() == scope) && error.getFieldName().equals(fieldName)) { + if (error.getEditor() == editor && + (scope == null || error.getScope() == scope) && + error.getFieldName().equals(fieldName)) { // remove the message data.remove(i); if (notify) { Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableMouseListener.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableMouseListener.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableMouseListener.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -23,25 +23,26 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import javax.swing.JComponent; -import javax.swing.JList; -import javax.swing.JTable; +import javax.swing.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; /** - * A mouse listener to put on a {@link JList} with a {@link SwingValidatorMessageTableModel} as a model. + * A mouse listener to put on a {@link JList} with a {@link + * SwingValidatorMessageTableModel} as a model. * <p/> - * When a double click occurs, find the selected error in model and then focus to the associated component of error. + * When a double click occurs, find the selected error in model and then focus + * to the associated component of error. * * @author tchemit <chemit@codelutin.com> */ public class SwingValidatorMessageTableMouseListener extends MouseAdapter { /** to use log facility, just put in your code: log.info(\"...\"); */ - private static Log log = LogFactory.getLog(SwingValidatorMessageTableMouseListener.class); + private static Log log = + LogFactory.getLog(SwingValidatorMessageTableMouseListener.class); public static final String HIGHLIGHT_ERROR_PROPERTY = "highlightError"; Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableRenderer.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableRenderer.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorMessageTableRenderer.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -41,8 +41,21 @@ private static final long serialVersionUID = 1L; @Override - public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { - JLabel rendererComponent = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + public Component getTableCellRendererComponent(JTable table, + Object value, + boolean isSelected, + boolean hasFocus, + int row, + int column) { + JLabel rendererComponent = (JLabel) + super.getTableCellRendererComponent( + table, + value, + isSelected, + hasFocus, + row, + column + ); ImageIcon icon = null; String text = null; @@ -88,14 +101,16 @@ } public String getMessage(JTable table, String value, int row) { - SwingValidatorMessageTableModel tableModel = (SwingValidatorMessageTableModel) table.getModel(); + SwingValidatorMessageTableModel tableModel = + (SwingValidatorMessageTableModel) table.getModel(); SwingValidatorMessage model = tableModel.getRow(row); String text = SwingValidatorUtil.getMessage(model); return text; } public String getFieldName(JTable table, String value, int row) { - SwingValidatorMessageTableModel tableModel = (SwingValidatorMessageTableModel) table.getModel(); + SwingValidatorMessageTableModel tableModel = + (SwingValidatorMessageTableModel) table.getModel(); SwingValidatorMessage model = tableModel.getRow(row); String fieldName = SwingValidatorUtil.getFieldName(model, value); return fieldName; Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/SwingValidatorUtil.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,19 +20,16 @@ */ package jaxx.runtime.validator.swing; -import jaxx.runtime.*; -import jaxx.runtime.validator.*; +import jaxx.runtime.SwingUtil; +import jaxx.runtime.validator.BeanValidatorScope; +import jaxx.runtime.validator.BeanValidatorUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import javax.swing.JList; -import javax.swing.JTable; +import javax.swing.*; import java.awt.event.MouseListener; import java.util.Arrays; -import javax.swing.ImageIcon; -import javax.swing.JComponent; -import javax.swing.RowSorter; -import javax.swing.SortOrder; + import static org.nuiton.i18n.I18n.n_; /** @@ -43,8 +40,11 @@ public class SwingValidatorUtil extends BeanValidatorUtil { static ImageIcon errorIcon; + static ImageIcon warningIcon; + static ImageIcon infoIcon; + /** to use log facility, just put in your code: log.info(\"...\"); */ static private final Log log = LogFactory.getLog(SwingValidatorUtil.class); @@ -75,14 +75,17 @@ /** * Prepare the ui where to display the validators messages. - * + * * @param errorTable the table where to display validators messages - * @param render renderer to use + * @param render renderer to use */ - public static void installUI(JTable errorTable, SwingValidatorMessageTableRenderer render) { + public static void installUI(JTable errorTable, + SwingValidatorMessageTableRenderer render) { errorTable.setDefaultRenderer(Object.class, render); - errorTable.getRowSorter().setSortKeys(Arrays.asList(new RowSorter.SortKey(0, SortOrder.ASCENDING))); - SwingUtil.setI18nTableHeaderRenderer(errorTable, + errorTable.getRowSorter().setSortKeys( + Arrays.asList(new RowSorter.SortKey(0, SortOrder.ASCENDING))); + SwingUtil.setI18nTableHeaderRenderer( + errorTable, n_("validator.scope.header"), n_("validator.scope.header.tip"), n_("validator.field.header"), @@ -96,7 +99,7 @@ /** * Register for a given validator list ui a validator mouse listener. - * + * <p/> * Note: there is only one listener registred for a given list model, so * invoking this method tiwce or more will have no effect. * @@ -105,7 +108,8 @@ * @see SwingValidatorMessageListMouseListener */ public static SwingValidatorMessageListMouseListener registerErrorListMouseListener(JList list) { - SwingValidatorMessageListMouseListener listener = getErrorListMouseListener(list); + SwingValidatorMessageListMouseListener listener = + getErrorListMouseListener(list); if (listener != null) { return listener; @@ -120,7 +124,7 @@ /** * Register for a given validator table ui a validator mouse listener - * + * <p/> * Note: there is onlt one listener registred for a givne table model, so * invokin this method twice or more will have no effect. * @@ -129,7 +133,8 @@ * @see SwingValidatorMessageTableMouseListener */ public static SwingValidatorMessageTableMouseListener registerErrorTableMouseListener(JTable table) { - SwingValidatorMessageTableMouseListener listener = getErrorTableMouseListener(table); + SwingValidatorMessageTableMouseListener listener = + getErrorTableMouseListener(table); if (listener != null) { return listener; @@ -144,7 +149,8 @@ /** * @param list the validator list ui - * @return the validator list mouse listener, or <code>null</code> if not found + * @return the validator list mouse listener, or <code>null</code> if not + * found * @see SwingValidatorMessageListMouseListener */ public static SwingValidatorMessageListMouseListener getErrorListMouseListener(JList list) { @@ -160,7 +166,8 @@ /** * @param table the validator table ui - * @return the validator table mouse listener, or <code>null</code> if not found + * @return the validator table mouse listener, or <code>null</code> if not + * found * @see SwingValidatorMessageTableMouseListener */ public static SwingValidatorMessageTableMouseListener getErrorTableMouseListener(JTable table) { @@ -187,11 +194,11 @@ JComponent editor = model.getEditor(); if (editor != null) { text = (String) editor.getClientProperty("validatorLabel"); - /*if (l != null) { - text = I18n._(l); - } else { - // TODO should try the text - }*/ + /*if (l != null) { + text = I18n._(l); + } else { + // TODO should try the text + }*/ } if (text == null) { text = value; Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/AbstractBeanValidatorUI.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/AbstractBeanValidatorUI.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/AbstractBeanValidatorUI.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -27,7 +27,7 @@ import org.apache.commons.logging.LogFactory; import org.jdesktop.jxlayer.plaf.AbstractLayerUI; -import javax.swing.JComponent; +import javax.swing.*; /** * Abstract renderer @@ -38,6 +38,7 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ private static final Log log = LogFactory.getLog(AbstractBeanValidatorUI.class); + /** the field to render */ protected BeanValidatorField<?> field; Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/IconValidationUI.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/IconValidationUI.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/IconValidationUI.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,29 +20,29 @@ */ package jaxx.runtime.validator.swing.ui; +import jaxx.runtime.validator.BeanValidatorField; +import jaxx.runtime.validator.BeanValidatorScope; import org.jdesktop.jxlayer.JXLayer; -import javax.swing.BorderFactory; -import javax.swing.JComponent; -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.RenderingHints; +import javax.swing.*; +import java.awt.*; import java.awt.image.BufferedImage; -import jaxx.runtime.validator.BeanValidatorField; -import jaxx.runtime.validator.BeanValidatorScope; /** - * An implementation of {@link AbstractBeanValidatorUI} which paints a - * icon on top right corner. + * An implementation of {@link AbstractBeanValidatorUI} which paints a icon on + * top right corner. * * @author tchemit <chemit@codelutin.com> */ public class IconValidationUI extends AbstractBeanValidatorUI { // The icon to be shown at the layer's corner + protected static BufferedImage errorIcon; + protected static BufferedImage warningIcon; + protected static BufferedImage infoIcon; public IconValidationUI(BeanValidatorField<?> field) { Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/ImageValidationUI.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/ImageValidationUI.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/ImageValidationUI.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -21,28 +21,27 @@ package jaxx.runtime.validator.swing.ui; import jaxx.runtime.SwingUtil; +import jaxx.runtime.validator.BeanValidatorField; +import jaxx.runtime.validator.BeanValidatorScope; import org.jdesktop.jxlayer.JXLayer; -import javax.swing.BorderFactory; -import javax.swing.ImageIcon; -import javax.swing.JComponent; -import java.awt.Graphics2D; -import java.awt.RenderingHints; +import javax.swing.*; +import java.awt.*; import java.awt.image.BufferedImage; -import jaxx.runtime.validator.BeanValidatorField; -import jaxx.runtime.validator.BeanValidatorScope; /** - * An implementation of {@link AbstractBeanValidatorUI} which paints a - * icon on top right corner. + * An implementation of {@link AbstractBeanValidatorUI} which paints a icon on + * top right corner. * * @author tchemit <chemit@codelutin.com> */ public class ImageValidationUI extends AbstractBeanValidatorUI { protected static BufferedImage errorIcon; + protected static BufferedImage warningIcon; + protected static BufferedImage infoIcon; public ImageValidationUI(BeanValidatorField<?> field) { Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/TranslucentValidationUI.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/TranslucentValidationUI.java 2010-03-18 09:16:31 UTC (rev 1791) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/validator/swing/ui/TranslucentValidationUI.java 2010-03-18 09:52:42 UTC (rev 1792) @@ -20,16 +20,12 @@ */ package jaxx.runtime.validator.swing.ui; +import jaxx.runtime.validator.BeanValidatorField; import jaxx.runtime.validator.BeanValidatorScope; import org.jdesktop.jxlayer.JXLayer; -import javax.swing.JComponent; -import java.awt.AlphaComposite; -import java.awt.Color; -import java.awt.Graphics2D; -import java.awt.Insets; -import java.awt.Rectangle; -import jaxx.runtime.validator.BeanValidatorField; +import javax.swing.*; +import java.awt.*; /** * An implementation of {@link AbstractBeanValidatorUI} which paints a @@ -41,7 +37,9 @@ public class TranslucentValidationUI extends AbstractBeanValidatorUI { protected Color errorHightlight; + protected Color warningHightlight; + protected Color infoHightlight; public TranslucentValidationUI(BeanValidatorField<?> field) { @@ -64,8 +62,8 @@ // To prevent painting on view's border Insets insets = view.getInsets(); g2.clip(new Rectangle(insets.left, insets.top, - view.getWidth() - insets.left - insets.right, - view.getHeight() - insets.top - insets.bottom)); + view.getWidth() - insets.left - insets.right, + view.getHeight() - insets.top - insets.bottom)); BeanValidatorScope scope = field.getScope(); @@ -84,7 +82,7 @@ break; } } - + g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f)); g2.fillRect(0, 0, l.getWidth(), l.getHeight()); }
participants (1)
-
tchemit@users.nuiton.org