1 22 package org.jboss.util.property; 23 24 import java.beans.BeanInfo ; 25 import java.beans.IntrospectionException ; 26 import java.beans.Introspector ; 27 import java.beans.PropertyDescriptor ; 28 import java.beans.PropertyEditor ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 32 import org.jboss.util.ThrowableHandler; 33 import org.jboss.util.propertyeditor.PropertyEditors; 34 35 41 public class MethodBoundPropertyListener 42 extends BoundPropertyAdapter 43 { 44 45 protected final String propertyName; 46 47 48 protected final Object instance; 49 50 51 protected final Method setter; 52 53 54 protected final PropertyDescriptor descriptor; 55 56 65 public MethodBoundPropertyListener(final Object instance, 66 final String propertyName, 67 final String beanPropertyName) 68 { 69 this.instance = instance; 70 this.propertyName = propertyName; 71 72 try { 73 descriptor = getPropertyDescriptor(beanPropertyName); 74 if (descriptor == null) { 75 throw new PropertyException 76 ("missing method for: " + beanPropertyName); 77 } 78 79 setter = descriptor.getWriteMethod(); 80 if (setter == null) { 81 throw new PropertyException 82 ("missing setter method for: " + beanPropertyName); 83 } 84 try { 85 setter.setAccessible(true); 86 } 87 catch (SecurityException e) { 88 ThrowableHandler.add(e); 89 } 90 } 91 catch (IntrospectionException e) { 92 throw new PropertyException(e); 93 } 94 } 95 96 102 private PropertyDescriptor getPropertyDescriptor(final String beanPropertyName) 103 throws IntrospectionException 104 { 105 Class instanceType = instance.getClass(); 106 BeanInfo beanInfo = Introspector.getBeanInfo(instanceType); 107 PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors(); 108 PropertyDescriptor descriptor = null; 109 110 for (int i=0; i<descriptors.length; i++) { 111 if (descriptors[i].getName().equals(beanPropertyName)) { 112 descriptor = descriptors[i]; 113 break; 114 } 115 } 116 117 return descriptor; 118 } 119 120 126 public MethodBoundPropertyListener(final Object instance, 127 final String propertyName) 128 { 129 this(instance, propertyName, propertyName); 130 } 131 132 137 public final String getPropertyName() { 138 return propertyName; 139 } 140 141 148 protected void invokeSetter(String value) { 149 try { 150 Class type = descriptor.getPropertyType(); 152 PropertyEditor editor = PropertyEditors.findEditor(type); 153 editor.setAsText(value); 154 Object coerced = editor.getValue(); 155 158 setter.invoke(instance, new Object [] { coerced }); 160 } 161 catch (InvocationTargetException e) { 162 Throwable target = e.getTargetException(); 163 if (target instanceof PropertyException) { 164 throw (PropertyException)target; 165 } 166 else { 167 throw new PropertyException(target); 168 } 169 } 170 catch (Exception e) { 171 throw new PropertyException(e); 172 } 173 } 174 175 180 public void propertyAdded(final PropertyEvent event) { 181 invokeSetter(event.getPropertyValue()); 182 } 183 184 189 public void propertyChanged(final PropertyEvent event) { 190 invokeSetter(event.getPropertyValue()); 191 } 192 193 198 public void propertyBound(final PropertyMap map) { 199 if (map.containsProperty(propertyName)) { 201 invokeSetter(map.getProperty(propertyName)); 202 } 203 } 204 } 205 | Popular Tags |