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

This page was automatically generated by Maven