/* * Copyright (c) 2003,2007 Declarative Engineering LLC. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Declarative Engineering LLC * verson 1 which accompanies this distribution, and is available at * http://declarativeengineering.com/legal/DE_Developer_License_v1.txt */ package com.foundation.view; import com.common.debug.*; import com.foundation.attribute.AttributeSupport; import com.foundation.event.EventSupport; import com.foundation.metadata.Attribute; /* * Models a view association with an entity attribute. */ public class AttributeAssociation extends ValueHolderAssociation implements IAttributeAssociation { /** The view unique number for this association. */ private int associationNumber = 0; /** The handler which is invoked to get and set the attribute value. */ private IAssociationHandler handler = null; /** The attribute's unique (within the context of the value holder's held type hierarchy) number. */ private int attributeNumber = 0; /** The unique (within the context of the value holder's held type hierarchy) name of the attribute. */ private String attributeName = null; /** The type of value returned when retreiving the attribute value. Also the type required by the attribute setter method. */ private Class attributeType = null; /** The listener for attribute change events. This will either be an instance of IAttributeAssociationChangeListener or IEventAssociationChangeListener. */ private Object listener = null; /** * AttributeAssociation constructor. * @param handler The object that handles the method invokations to get and set the attribute value. * @param associationNumber The association number which will be passed to the handler when the assocation is invoked. This number allows fast indexing of the methods that the handler handles. * @param component The component the association is connected to. * @param rowType The class which defines the attribute. * @param attribute The associated attribute's unique (within the class) identifier. * @param attributeType The class assignable by values held by the attribute. */ public AttributeAssociation(IAssociationHandler handler, int associationNumber, IAbstractComponent component, Class rowType, Attribute attribute, Class attributeType) { super(component, null, null, rowType); this.handler = handler; this.associationNumber = associationNumber; this.attributeNumber = attribute.getNumber(); this.attributeName = AttributeSupport.getAttributeName(rowType, attributeNumber); this.attributeType = attributeType; }//AttributeAssociation()// /** * AttributeAssociation constructor. * @param handler The object that handles the method invokations to get and set the attribute value. * @param associationNumber The association number which will be passed to the handler when the assocation is invoked. This number allows fast indexing of the methods that the handler handles. * @param component The component the association is connected to. * @param rowType The class which defines the attribute. * @param attributeName The associated attribute's unique (within the class) name. * @param attributeType The class assignable by values held by the attribute. */ public AttributeAssociation(IAssociationHandler handler, int associationNumber, IAbstractComponent component, Class rowType, String attributeName, Class attributeType) { super(component, null, null, rowType); this.listener = component; this.handler = handler; this.associationNumber = associationNumber; this.attributeName = attributeName; this.attributeNumber = AttributeSupport.getAttributeNumber(rowType, attributeName); this.attributeType = attributeType; }//AttributeAssociation()// /** * AttributeAssociation constructor. * @param handler The object that handles the method invokations to get and set the attribute value. * @param associationNumber The association number which will be passed to the handler when the assocation is invoked. This number allows fast indexing of the methods that the handler handles. * @param component The component the association is connected to. * @param valueHolderName The name of the value holder for the attribute. The value holder's held type defines the association if the value type is null. * @param valueHolderType The type that the held value must match in order to be valid. This is optional and only used if the value holder is non-null. * @param attribute The associated attribute's unique (within the class) identifier. * @param attributeType The class assignable by values held by the attribute. */ public AttributeAssociation(IAssociationHandler handler, int associationNumber, IAbstractComponent component, String valueHolderName, Class valueHolderType, Class rowType, Attribute attribute, Class attributeType) { super(component, valueHolderName, valueHolderType, rowType); this.handler = handler; this.associationNumber = associationNumber; this.attributeNumber = attribute.getNumber(); this.attributeName = AttributeSupport.getAttributeName(rowType == null ? getValueHolder().getHeldType() : rowType, attributeNumber); this.attributeType = attributeType; }//AttributeAssociation()// /** * AttributeAssociation constructor. * @param handler The object that handles the method invokations to get and set the attribute value. * @param associationNumber The association number which will be passed to the handler when the assocation is invoked. This number allows fast indexing of the methods that the handler handles. * @param component The component the association is connected to. * @param valueHolderName The name of the value holder for the attribute. The value holder's held type defines the association if the value type is null. * @param valueHolderType The type that the held value must match in order to be valid. This is optional and only used if the value holder is non-null. * @param attributeName The associated attribute's unique (within the class) name. * @param attributeType The class assignable by values held by the attribute. */ public AttributeAssociation(IAssociationHandler handler, int associationNumber, IAbstractComponent component, String valueHolderName, Class valueHolderType, String attributeName, Class attributeType) { super(component, valueHolderName, valueHolderType, null); this.listener = component; this.handler = handler; this.associationNumber = associationNumber; this.attributeName = attributeName; this.attributeNumber = AttributeSupport.getAttributeNumber(valueHolderType == null ? getValueHolder().getHeldType() : valueHolderType, attributeName); this.attributeType = attributeType; }//AttributeAssociation()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#getAttributeValue() */ public Object getAttributeValue() { try { if(getValueHolder().getValue() != null) { return handler.invokeGetMethod(associationNumber, getValueHolder().getValue()); }//if// else { return null; }//else// }//try// catch(Throwable e) { Debug.log(e); return null; }//catch// }//getAttributeValue()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#setAttributeValue(Object) */ public void setAttributeValue(Object value) { try { handler.invokeSetMethod(associationNumber, getValueHolder().getValue(), value); }//try// catch(Throwable e) { Debug.log(e); }//catch// }//setAttributeValue()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#getAttributeValue(java.lang.Object) */ public Object getAttributeValue(Object object) { Object retVal = null; try { if(object != null) { retVal = handler.invokeGetMethod(associationNumber, object); }//if// }//try// catch(Throwable e) { Debug.log(e); }//catch// return retVal; }//getAttributeValue()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#setAttributeValue(java.lang.Object, java.lang.Object) */ public void setAttributeValue(Object object, Object value) { if(object != null) { try { handler.invokeSetMethod(associationNumber, object, value); }//try// catch(Throwable e) { Debug.log(e); }//catch// }//if// }//setAttributeValue()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#getAttributeType() */ public Class getAttributeType() { return attributeType; }//getAttributeType()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#register() */ public void register() { getValueHolder().registerListener(this); }//register()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#unregister() */ public void unregister() { getValueHolder().unregisterListener(this); }//unregister()// /* (non-Javadoc) * @see com.foundation.view.IAttributeAssociation#setChangeListener(com.foundation.view.IAttributeAssociationChangeListener) */ public void setChangeListener(IAttributeAssociationChangeListener listener) { if(listener == null) { listener = getComponent(); }//if// this.listener = listener; }//setChangeListener()// /* (non-Javadoc) * @see com.foundation.view.IEventAssociation#setChangeListener(com.foundation.view.IEventAssociationChangeListener) */ public void setChangeListener(IEventAssociationChangeListener listener) { if(listener == null) { listener = getComponent(); }//if// this.listener = listener; }//setChangeListener()// /* (non-Javadoc) * @see com.common.event.IHandler#evaluate(int, java.lang.Object[], int) */ public void evaluate(int eventNumber, Object[] parameters, int flags) { //Note: I commented out the isTrustedChangeEvent because we don't want to listen to lazy load and reflection load events which are marked as trusted.// //If we did listen to them we would be performing updates before we have finished initializing during an initialization where more than one association is connected to a lazy or reflection loaded attribute.// //We do care about reflection updates which are now marked as trusted when setting the attribute, but are not marked as trusted when firing the events.// //I don't think there is anything else marked as trusted that we care about here - thus the code is commented out.// if(EventSupport.isStandardEvent(flags)/* || EventSupport.isTrustedChangeEvent(flags)*/) { //Notify the listener.// if(listener instanceof IAttributeAssociationChangeListener) { ((IAttributeAssociationChangeListener) listener).onValueChanged(this); }//if// else if(listener instanceof IEventAssociationChangeListener) { ((IEventAssociationChangeListener) listener).onEventFired(this, EMPTY_ARGS); }//else if// }//if// }//evaluate()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IAttributeAssociation#getAttributeName() */ public String getAttributeName() { return attributeName; }//getAttributeName()// /* (non-Javadoc) * @see com.foundation.tcv.swt.IEventAssociation#getEventName() */ public int getEventNumber() { return attributeNumber; }//getEventNumber()// }//AttributeAssociation//