View Javadoc
1 /* 2 * Angkor Web Framework 3 * 4 * Distributable under LGPL license. 5 * See terms of license at gnu.org. 6 */ 7 8 package com.tirsen.angkor.beans; 9 10 import com.tirsen.angkor.widget.AbstractValueModel; 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import java.beans.IntrospectionException; 15 import java.beans.Introspector; 16 import java.beans.PropertyDescriptor; 17 import java.beans.PropertyEditor; 18 import java.beans.PropertyEditorManager; 19 import java.lang.reflect.InvocationTargetException; 20 21 /*** 22 * A value model implementation which fetches and updates it's value according to the patterns of 23 * the JavaBean specification, useful together with BeanModel. 24 * 25 * <!-- $Id: PropertyValueModel.java,v 1.4 2002/10/09 21:37:37 tirsen Exp $ --> 26 * 27 * @author $Author: tirsen $ 28 * @version $Revision: 1.4 $ 29 */ 30 class PropertyValueModel extends AbstractValueModel 31 { 32 private static final Log logger = LogFactory.getLog(PropertyValueModel.class); 33 34 private Object bean; 35 private BeanModel beanModel; 36 37 private transient PropertyDescriptor property; 38 39 /*** 40 * If the object can be null and created at a later stage use the {@link BeanModel} to create 41 * a value model for a property. 42 * @throws NullPointerException if object is <code>null</code>. 43 */ 44 public PropertyValueModel(Object object, String property) 45 { 46 this(object, findPropertyDescriptor(object.getClass(), property)); 47 } 48 49 public PropertyValueModel(Object object, PropertyDescriptor property) 50 { 51 this.bean = object; 52 this.property = property; 53 } 54 55 public PropertyValueModel(BeanModel proxy, String property) 56 { 57 this.beanModel = proxy; 58 this.property = findPropertyDescriptor(proxy.getObjectClass(), property); 59 } 60 61 private static PropertyDescriptor findPropertyDescriptor(Class klass, String property) 62 { 63 PropertyDescriptor[] properties; 64 try 65 { 66 properties = Introspector.getBeanInfo(klass).getPropertyDescriptors(); 67 } 68 catch (IntrospectionException e) 69 { 70 throw new RuntimeException(e.getMessage()); 71 } 72 73 PropertyDescriptor propertyDesc = null; 74 for (int i = 0; i < properties.length; i++) 75 { 76 if (properties[i].getName().equals(property)) 77 { 78 propertyDesc = properties[i]; 79 break; 80 } 81 } 82 if (propertyDesc == null) throw new RuntimeException("No property " + property + " in bean of type " + klass.getName()); 83 84 return propertyDesc; 85 } 86 87 Object getBean() 88 { 89 Object bean = this.bean; 90 if (bean == null) bean = beanModel.getBean(); 91 if (bean == null) 92 { 93 throw new IllegalStateException("Tried to access value of property with no valid value-bean."); 94 } 95 return bean; 96 } 97 98 public Object getValue() 99 { 100 try 101 { 102 return property.getReadMethod().invoke(getBean(), null); 103 } 104 catch (IllegalAccessException e) 105 { 106 throw new IllegalAccessError(e.getMessage()); 107 } 108 catch (InvocationTargetException e) 109 { 110 // handle this better? 111 throw new RuntimeException(e.getMessage()); 112 } 113 } 114 115 public boolean isReadOnly() 116 { 117 return property.getWriteMethod() == null || getPropertyEditor() == null; 118 } 119 120 public Class getValueClass() 121 { 122 return property.getPropertyType(); 123 } 124 125 public void setValue(Object value) 126 { 127 if (isReadOnly()) throw new IllegalStateException("Trying to update a read-only property."); 128 try 129 { 130 property.getWriteMethod().invoke(getBean(), new Object[]{value}); 131 changed(); 132 } 133 catch (IllegalAccessException e) 134 { 135 throw new IllegalAccessError("Could not access property \"" + property.getName() + "\"." + 136 "Note: If you are using inner classes as models they need to be public."); 137 } 138 catch (InvocationTargetException e) 139 { 140 // handle this better? 141 throw new RuntimeException(e.getMessage()); 142 } 143 } 144 145 public PropertyEditor getPropertyEditor() 146 { 147 PropertyEditor editor = null; 148 149 Class editorClass = property.getPropertyEditorClass(); 150 if (editorClass != null) 151 { 152 try 153 { 154 editor = (PropertyEditor) editorClass.newInstance(); 155 } 156 catch (InstantiationException ignore) 157 { 158 } 159 catch (IllegalAccessException ignore) 160 { 161 } 162 } 163 164 if (editor == null) 165 { 166 editor = PropertyEditorManager.findEditor(getValueClass()); 167 } 168 169 return editor; 170 } 171 }

This page was automatically generated by Maven