Clover coverage report - angkor - 0.4
Coverage timestamp: ti okt 15 2002 22:32:48 CEST
file stats: LOC: 175   Methods: 12
NCLOC: 134   Classes: 1
Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
PropertyValueModel.java 0% 0% 0% 0%
 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  0
     public PropertyValueModel(Object object, String property)
 40   
     {
 41  0
         this(object, findPropertyDescriptor(object.getClass(), property));
 42   
     }
 43   
 
 44  0
     public PropertyValueModel(Object object, PropertyDescriptor property)
 45   
     {
 46  0
         this.object = object;
 47  0
         this.property = property;
 48   
     }
 49   
 
 50  0
     public PropertyValueModel(ModelProxy proxy, String property)
 51   
     {
 52  0
         this.proxy = proxy;
 53  0
         this.property = findPropertyDescriptor(proxy.getObjectClass(), property);
 54   
     }
 55   
 
 56  0
     private static PropertyDescriptor findPropertyDescriptor(Class klass, String property)
 57   
     {
 58  0
         PropertyDescriptor[] properties;
 59  0
         try
 60   
         {
 61  0
             properties = Introspector.getBeanInfo(klass).getPropertyDescriptors();
 62   
         }
 63   
         catch (IntrospectionException e)
 64   
         {
 65  0
             throw new RuntimeException(e.getMessage());
 66   
         }
 67   
 
 68  0
         PropertyDescriptor propertyDesc = null;
 69  0
         for (int i = 0; i < properties.length; i++)
 70   
         {
 71  0
             if (properties[i].getName().equals(property))
 72   
             {
 73  0
                 propertyDesc = properties[i];
 74  0
                 break;
 75   
             }
 76   
         }
 77   
 
 78  0
         return propertyDesc;
 79   
     }
 80   
 
 81  0
     Object getObject()
 82   
     {
 83  0
         Object object = this.object;
 84  0
         if (object == null) object = proxy.getObject();
 85  0
         if (object == null)
 86   
         {
 87  0
             logger.debug("proxy = " + proxy);
 88  0
             throw new IllegalStateException("Tried to access value of property with no valid value-object.");
 89   
         }
 90  0
         return object;
 91   
     }
 92   
 
 93  0
     public Object getValue()
 94   
     {
 95  0
         try
 96   
         {
 97  0
             return property.getReadMethod().invoke(getObject(), null);
 98   
         }
 99   
         catch (IllegalAccessException e)
 100   
         {
 101  0
             throw new IllegalAccessError(e.getMessage());
 102   
         }
 103   
         catch (InvocationTargetException e)
 104   
         {
 105   
             // handle this better?
 106  0
             throw new RuntimeException(e.getMessage());
 107   
         }
 108   
     }
 109   
 
 110  0
     public boolean isReadOnly()
 111   
     {
 112  0
         return property.getReadMethod() == null || getPropertyEditor() == null;
 113   
     }
 114   
 
 115  0
     public Class getValueClass()
 116   
     {
 117  0
         return property.getPropertyType();
 118   
     }
 119   
 
 120  0
     public boolean isChanged()
 121   
     {
 122  0
         return changed;
 123   
     }
 124   
 
 125  0
     public void resetChanged()
 126   
     {
 127  0
         changed = false;
 128   
     }
 129   
 
 130  0
     public void setValue(Object value)
 131   
     {
 132  0
         if (isReadOnly()) throw new IllegalStateException("Trying to update a read-only property.");
 133  0
         try
 134   
         {
 135  0
             property.getWriteMethod().invoke(getObject(), new Object[]{value});
 136   
         }
 137   
         catch (IllegalAccessException e)
 138   
         {
 139  0
             throw new IllegalAccessError(e.getMessage());
 140   
         }
 141   
         catch (InvocationTargetException e)
 142   
         {
 143   
             // handle this better?
 144  0
             throw new RuntimeException(e.getMessage());
 145   
         }
 146   
     }
 147   
 
 148  0
     public PropertyEditor getPropertyEditor()
 149   
     {
 150  0
         PropertyEditor editor = null;
 151   
 
 152  0
         Class editorClass = property.getPropertyEditorClass();
 153  0
         if (editorClass != null)
 154   
         {
 155  0
             try
 156   
             {
 157  0
                 editor = (PropertyEditor) editorClass.newInstance();
 158   
             }
 159   
             catch (InstantiationException ignore)
 160   
             {
 161   
             }
 162   
             catch (IllegalAccessException ignore)
 163   
             {
 164   
             }
 165   
         }
 166   
 
 167  0
         if (editor == null)
 168   
         {
 169  0
             editor = PropertyEditorManager.findEditor(getValueClass());
 170   
         }
 171   
 
 172  0
         return editor;
 173   
     }
 174   
 }
 175