Author: tchemit
Date: 2008-10-17 16:43:39 +0000 (Fri, 17 Oct 2008)
New Revision: 946
Modified:
lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/BeanValidator.java
lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Util.java
Log:
move util method to Util class
reformat + javadoc
Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/BeanValidator.java
===================================================================
--- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/BeanValidator.java 2008-10-17 16:43:00 UTC (rev 945)
+++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/BeanValidator.java 2008-10-17 16:43:39 UTC (rev 946)
@@ -48,20 +48,14 @@
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
-import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.LinkedList;
import java.util.List;
-import java.util.ListIterator;
import java.util.Map;
/**
@@ -144,28 +138,36 @@
/** to use log facility, just put in your code: log.info(\"...\"); */
static private Log log = LogFactory.getLog(BeanValidator.class);
- protected PropertyChangeSupport pcs = new PropertyChangeSupport(this);
+ protected PropertyChangeSupport pcs;
- protected ValidationAwareSupport validationSupport = new ValidationAwareSupport();
- protected DelegatingValidatorContext validationContext =
- new DelegatingValidatorContext(validationSupport);
+ protected ValidationAwareSupport validationSupport;
+ protected DelegatingValidatorContext validationContext;
- protected transient ActionValidatorManager validator = null;
+ protected transient ActionValidatorManager validator;
- /** indique si le bean a ete modifie depuis sont arrive */
+ /** indique si le bean a ete modifie depuis son arrivee */
protected boolean changed = false;
+
/** le bean a surveiller */
protected T bean = null;
+
/** l'objet qui recoit les notifications de modification du bean */
protected Listener l = new Listener();
+
/** permet de faire le lien en un champs du bean et l'objet qui permet de l'editer */
- protected Map<String, JComponent> fieldRepresentation = new HashMap<String, JComponent>();
+ protected Map<String, JComponent> fieldRepresentation;
+
/** Objet servant a afficher toutes les erreurs */
protected JList errorList = null;
+
/** Object servant a contenir la liste des erreurs */
protected DefaultListModel errorListModel;
public BeanValidator() {
+ pcs = new PropertyChangeSupport(this);
+ validationSupport = new ValidationAwareSupport();
+ validationContext = new DelegatingValidatorContext(validationSupport);
+ fieldRepresentation = new HashMap<String, JComponent>();
}
/**
@@ -193,8 +195,8 @@
* Permet d'indiquer le composant graphique responsable de l'affichage
* d'un attribut du bean
*
- * @param fieldname
- * @param c
+ * @param fieldname the field name in the bean
+ * @param c the editor component for the field
*/
public void setFieldRepresentation(String fieldname, JComponent c) {
JComponent old = fieldRepresentation.put(fieldname, c);
@@ -207,17 +209,15 @@
}
}
-
public JComponent getFieldRepresentation(String fieldname) {
return fieldRepresentation.get(fieldname);
}
-
/**
* Retourne vrai si l'objet bean a ete modifie depuis le dernier
* {@link #setBean}
*
- * @return
+ * @return <code>true</code> if bean was modify since last {@link #setBean(Object)} invocation
*/
public boolean isChanged() {
return changed;
@@ -226,7 +226,7 @@
/**
* Permet de force la remise a false de l'etat de changement du bean
*
- * @param changed
+ * @param changed flag to force reset of property {@link #changed}
*/
public void setChanged(boolean changed) {
boolean oldChanged = this.changed;
@@ -303,67 +303,62 @@
public void validate() {
// on ne valide que si il y a un bean et que le resultat de la validation
// pourra etre affiche quelque part
- if (bean != null && getErrorListModel() != null && fieldRepresentation.size() != 0) {
- try {
- final List<String> newErrors = new ArrayList<String>();
+ if (bean == null || getErrorListModel() == null || fieldRepresentation.size() == 0) {
+ return;
+ }
- Enumeration enumeration = getErrorListModel().elements();
- while (enumeration.hasMoreElements()) {
- Object o = enumeration.nextElement();
- newErrors.add((String) o);
+ try {
+ List<String> newErrors = new ArrayList<String>();
+
+ Enumeration enumeration = getErrorListModel().elements();
+ while (enumeration.hasMoreElements()) {
+ Object o = enumeration.nextElement();
+ newErrors.add((String) o);
+ }
+ for (Object o : validationContext.getFieldErrors().entrySet()) {
+ Map.Entry<?, ?> r = (Map.Entry<?, ?>) o;
+ String field = (String) r.getKey();
+ List<String> errors = (List<String>) r.getValue();
+ JComponent component = fieldRepresentation.get(field);
+ for (String error : errors) {
+ newErrors.remove(component.getName() + " : " + error);
}
- for (Object o : validationContext.getFieldErrors().entrySet()) {
- Map.Entry<?, ?> r = (Map.Entry<?, ?>) o;
- String field = (String) r.getKey();
- List<String> errors = (List<String>) r.getValue();
- JComponent component = fieldRepresentation.get(field);
- for (String error : errors) {
- newErrors.remove(component.getName() + " : " + error);
- }
- }
+ }
- validationSupport.clearErrorsAndMessages();
+ validationSupport.clearErrorsAndMessages();
- getValidator().validate(bean, null, validationContext);
+ getValidator().validate(bean, null, validationContext);
- if (log.isDebugEnabled()) {
- log.debug("Action errors: " + validationContext.getActionErrors());
- log.debug("Action messages: " + validationContext.getActionMessages());
- log.debug("Field errors: " + validationContext.getFieldErrors());
- }
+ if (log.isDebugEnabled()) {
+ log.debug("Action errors: " + validationContext.getActionErrors());
+ log.debug("Action messages: " + validationContext.getActionMessages());
+ log.debug("Field errors: " + validationContext.getFieldErrors());
+ }
- log.info(this + " : " + validationContext.getFieldErrors());
-
- for (Object o : validationContext.getFieldErrors().entrySet()) {
- Map.Entry<?, ?> r = (Map.Entry<?, ?>) o;
- String field = (String) r.getKey();
- List<String> errors = (List<String>) r.getValue();
- JComponent component = fieldRepresentation.get(field);
- for (String error : errors) {
- newErrors.add(component.getName() + " : " + error);
- }
- }
+ log.info(this + " : " + validationContext.getFieldErrors());
- getErrorListModel().clear();
- for (String newError : newErrors) {
- getErrorListModel().addElement(newError);
+ for (Object o : validationContext.getFieldErrors().entrySet()) {
+ Map.Entry<?, ?> r = (Map.Entry<?, ?>) o;
+ String field = (String) r.getKey();
+ List<String> errors = (List<String>) r.getValue();
+ JComponent component = fieldRepresentation.get(field);
+ for (String error : errors) {
+ newErrors.add(component.getName() + " : " + error);
}
+ }
- for (String fieldname : fieldRepresentation.keySet()) {
- JComponent c = fieldRepresentation.get(fieldname);
- //fixme : sinon le layer ne se repain pas bien, cela n'est pas normal
- c.getParent().repaint();
- }
- // TODO: trouver autre chose
- // On est obliger de le refaire ici car avec jaxx, lors du
- // setFieldRepresentation le composant n'a pas encore de pere :(
- /*for (String fieldname : fieldRepresentation.keySet()) {
- JComponent c = fieldRepresentation.get(fieldname);
- setErrorRepresentation(fieldname, null, c);
- }*/
- } catch (ValidationException eee) {
- log.warn("Error during validation", eee);
+ getErrorListModel().clear();
+ for (String newError : newErrors) {
+ getErrorListModel().addElement(newError);
}
+
+ for (String fieldname : fieldRepresentation.keySet()) {
+ JComponent c = fieldRepresentation.get(fieldname);
+ //todo: a supprimer mais actuellemnt le layer ne se repaint pas bien, cela n'est pas normal
+ c.getParent().repaint();
+ }
+ } catch (ValidationException eee) {
+ log.warn("Error during validation", eee);
}
}
@@ -373,7 +368,7 @@
// suppression du jxlayer sous l'ancien composant
Container container = old.getParent();
if (container instanceof JXLayer) {
- JXLayer<JComponent> jx = (JXLayer<JComponent>) container;
+ JXLayer<?> jx = (JXLayer<?>) container;
jx.setUI(null);
}
}
@@ -382,7 +377,7 @@
Container container = c.getParent();
if (container instanceof JXLayer) {
IconValidationUI ui = new IconValidationUI(fieldname, c.getName(), validationSupport);
-// TranslucentValidationUI ui = new TranslucentValidationUI(fieldname, validationSupport.getFieldErrors());
+ // TranslucentValidationUI ui = new TranslucentValidationUI(fieldname, validationSupport.getFieldErrors());
ui.setEnabled(true);
JXLayer<JComponent> jx = (JXLayer<JComponent>) container;
jx.setUI(ui);
@@ -397,67 +392,9 @@
validate();
setChanged(true);
}
-
}
- /**
- * recherche les composants portant le meme nom que les champs de la classe
- * clazz. Cette methode est statique pour pouvoir eventuellement l'utiliser
- * dans un autre context (je pense par exemple a la generation jaxx).
- * <p/>
- * <p/>
- * Si la recherche echoue pour quelque raison que se soit, aucune exception
- * n'est leve, et la map retournee est tout simplement vide ou incomplete
- *
- * @param clazz la classe ou recherche les champs
- * @param container le container ou rechercher les composants d'edition
- * @return
- */
- public static Map<String, JComponent> lookingForEditor(Class clazz, Container container) {
- Map<String, JComponent> result = new HashMap<String, JComponent>();
- try {
- // looking for all component with name set
- Map<String, JComponent> allNamedComponent = new HashMap<String, JComponent>();
- List<Container> todo = new LinkedList<Container>();
- todo.add(container);
- while (todo.size() > 0) {
- for (ListIterator<Container> i = todo.listIterator(); i.hasNext();) {
- Container parent = i.next();
- i.remove();
- for (Component c : parent.getComponents()) {
- if (c instanceof Container) {
- i.add((Container) c);
- String name = c.getName();
- if (c instanceof JComponent &&
- name != null && !"".equals(name)) {
- allNamedComponent.put(name, (JComponent) c);
- }
- }
- }
- }
- }
- // looking for all properties on class
- BeanInfo info = Introspector.getBeanInfo(clazz);
- PropertyDescriptor[] props = info.getPropertyDescriptors();
-
- // find if one properties have same name that component
- for (PropertyDescriptor prop : props) {
- String name = prop.getName();
- if (allNamedComponent.containsKey(name)) {
- result.put(name, allNamedComponent.get(name));
- }
- }
-
- } catch (IntrospectionException eee) {
- log.warn("Can't introspect bean", eee);
- }
-
- System.out.println("Result: " + result);
-
- return result;
- }
-
public static class TranslucentValidationUI extends AbstractLayerUI<JComponent> {
protected String field = null;
@@ -543,9 +480,9 @@
Component component = l.getComponent(1);
// There is no need to take insets into account for this painter
if (resultValidation.getFieldErrors().containsKey(field)) {
- System.out.println("requiredId : " + componentId + " : componentName : " + component.getName());
+ log.info("requiredId : " + componentId + " : componentName : " + component.getName());
g2.drawImage(INVALID_ICON, l.getWidth() - INVALID_ICON.getWidth() - 1, 0, null);
-// g2.drawImage(INVALID_ICON, 0, 0, null);
+ // g2.drawImage(INVALID_ICON, 0, 0, null);
}
}
}
Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Util.java
===================================================================
--- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Util.java 2008-10-17 16:43:00 UTC (rev 945)
+++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Util.java 2008-10-17 16:43:39 UTC (rev 946)
@@ -1,8 +1,16 @@
package jaxx.runtime;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import javax.swing.JComponent;
import java.awt.Component;
+import java.awt.Container;
import java.awt.Dimension;
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -13,13 +21,21 @@
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.EventListener;
+import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
+import java.util.ListIterator;
import java.util.Map;
import java.util.WeakHashMap;
-import java.util.EventListener;
import java.util.zip.GZIPInputStream;
public class Util {
+
+ /** to use log facility, just put in your code: log.info(\"...\"); */
+ static private Log log = LogFactory.getLog(Util.class);
+
+
// Maps root objects to lists of event listeners
private static Map<Object, WeakReference<List<EventListenerDescriptor>>> eventListeners = new WeakHashMap<Object, WeakReference<List<EventListenerDescriptor>>>();
private static Map<JAXXObject, WeakReference<List<DataBindingUpdateListener>>> dataBindingUpdateListeners = new WeakHashMap<JAXXObject, WeakReference<List<DataBindingUpdateListener>>>();
@@ -45,7 +61,7 @@
try {
byte[] data = new byte[descriptor.length()];
// copy low-order bytes into the array. The high-order bytes should all be zero.
- System.arraycopy(descriptor.getBytes(),0,data,0,data.length);
+ System.arraycopy(descriptor.getBytes(), 0, data, 0, data.length);
//descriptor.getBytes(0, descriptor.length(), data, 0);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
return (JAXXObjectDescriptor) in.readObject();
@@ -63,7 +79,7 @@
try {
byte[] data = new byte[descriptor.length()];
// copy low-order bytes into the array. The high-order bytes should all be zero.
- System.arraycopy(descriptor.getBytes(),0,data,0,data.length);
+ System.arraycopy(descriptor.getBytes(), 0, data, 0, data.length);
//descriptor.getBytes(0, descriptor.length(), data, 0);
ObjectInputStream in = new ObjectInputStream(new GZIPInputStream(new ByteArrayInputStream(data)));
return (JAXXObjectDescriptor) in.readObject();
@@ -109,8 +125,9 @@
}
}
}
- if (listenerMethodName != null && listenerMethod == null)
+ if (listenerMethodName != null && listenerMethod == null) {
throw new IllegalArgumentException("no method named " + listenerMethodName + " found in class " + listenerClass.getName());
+ }
Class[] parameterTypes = listenerMethods.get(0).getParameterTypes();
Class<?> methodContainerClass = methodContainer.getClass();
final Method targetMethod = methodContainerClass.getMethod(methodName, parameterTypes);
@@ -164,8 +181,9 @@
dataBindingUpdateListeners.put(object, new WeakReference<List<DataBindingUpdateListener>>(listeners));
} else {
for (DataBindingUpdateListener listener : listeners) {
- if (bindingName.equals(listener.getBindingName()))
+ if (bindingName.equals(listener.getBindingName())) {
return listener;
+ }
}
}
DataBindingUpdateListener listener = new DataBindingUpdateListener(object, bindingName);
@@ -180,8 +198,9 @@
JComponent jcomponent = (JComponent) component;
jcomponent.setPreferredSize(new Dimension(width, jcomponent.getPreferredSize().height));
jcomponent.setMinimumSize(new Dimension(width, jcomponent.getPreferredSize().height));
- if (jcomponent.isDisplayable())
+ if (jcomponent.isDisplayable()) {
jcomponent.revalidate();
+ }
}
}
@@ -192,8 +211,9 @@
JComponent jcomponent = (JComponent) component;
jcomponent.setPreferredSize(new Dimension(jcomponent.getPreferredSize().width, height));
jcomponent.setMinimumSize(new Dimension(jcomponent.getPreferredSize().width, height));
- if (jcomponent.isDisplayable())
+ if (jcomponent.isDisplayable()) {
jcomponent.revalidate();
+ }
}
}
@@ -250,4 +270,65 @@
src.firePropertyChange(name.trim(), null, "dummy value");
return value;
}
+
+ /**
+ * recherche les composants portant le meme nom que les champs de la classe
+ * clazz. Cette methode est statique pour pouvoir eventuellement l'utiliser
+ * dans un autre context (je pense par exemple a la generation jaxx).
+ * <p/>
+ * <p/>
+ * Si la recherche echoue pour quelque raison que se soit, aucune exception
+ * n'est leve, et la map retournee est tout simplement vide ou incomplete
+ *
+ * @param clazz la classe ou recherche les champs
+ * @param container le container ou rechercher les composants d'edition
+ * @return
+ */
+ public static Map<String, JComponent> lookingForEditor(Class clazz, Container container) {
+ Map<String, JComponent> result = new HashMap<String, JComponent>();
+ try {
+ // looking for all component with name set
+ Map<String, JComponent> allNamedComponent = new HashMap<String, JComponent>();
+ List<Container> todo = new LinkedList<Container>();
+ todo.add(container);
+ while (todo.size() > 0) {
+ for (ListIterator<Container> i = todo.listIterator(); i.hasNext();) {
+ Container parent = i.next();
+ i.remove();
+ for (Component c : parent.getComponents()) {
+ if (c instanceof Container) {
+ i.add((Container) c);
+ String name = c.getName();
+ if (c instanceof JComponent &&
+ name != null && !"".equals(name)) {
+ allNamedComponent.put(name, (JComponent) c);
+ }
+ }
+ }
+ }
+ }
+
+ // looking for all properties on class
+ BeanInfo info = Introspector.getBeanInfo(clazz);
+ PropertyDescriptor[] props = info.getPropertyDescriptors();
+
+ // find if one properties have same name that component
+ for (PropertyDescriptor prop : props) {
+ String name = prop.getName();
+ if (allNamedComponent.containsKey(name)) {
+ result.put(name, allNamedComponent.get(name));
+ }
+ }
+
+ } catch (IntrospectionException eee) {
+ log.warn("Can't introspect bean", eee);
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Result: " + result);
+ }
+
+ return result;
+ }
+
}