/* java.beans.beancontext.BeanContextChildSupport
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.beans.beancontext;
import java.beans.PropertyChangeListener;
import java.beans.VetoableChangeListener;
import java.beans.PropertyVetoException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeSupport;
import java.beans.VetoableChangeSupport;
import java.io.Serializable;
/**
* Support for creating a BeanContextChild
.
* This class contains the most common implementations of the methods in
* the BeanContextChild
*
* @specnote This class is not very well specified. I had to "fill in the
* blanks" in most places with what I thought was reasonable
* behavior. If there are problems, let me know.
*
* @author John Keiser
* @since JDK1.2
* @see java.beans.beancontext.BeanContextChild
*/
public class BeanContextChildSupport implements BeanContextChild, BeanContextServicesListener, Serializable {
/**
* The peer on which to perform set
actions.
* This is here so that this class can be used as a peer.
*
*
* When extending this class, this variable will be set to
* this
.
*/
public BeanContextChild beanContextChildPeer;
/**
* The parent BeanContext
.
*/
protected transient BeanContext beanContext;
/**
* If setBeanContext()
was vetoed once before, this
* is set to true
so that the next time, vetoes will
* be ignored.
*/
protected transient boolean rejectedSetBCOnce;
/**
* Listeners are registered here and events are fired through here.
*/
protected PropertyChangeSupport pcSupport;
/**
* Listeners are registered here and events are fired through here.
*/
protected VetoableChangeSupport vcSupport;
/**
* Create a new BeanContextChildSupport
with itself as the peer.
* This is meant to be used when you subclass
* BeanContextChildSupport
to create your child.
*/
public BeanContextChildSupport() {
this(null);
};
/**
* Create a new BeanContextChildSupport
with the specified peer.
* @param peer the peer to use, or null
to specify
* this
.
*/
public BeanContextChildSupport(BeanContextChild peer) {
if(peer == null) {
peer = this;
}
beanContextChildPeer = peer;
pcSupport = new PropertyChangeSupport(peer);
vcSupport = new VetoableChangeSupport(peer);
}
/**
* Set the parent BeanContext
.
*
* * When this Object is being added to a new BeanContext or moved * from an old one, a non-null value will be passed in. *
*
* When this Object is being removed from the current
* BeanContext
, setBeanContext()
will
* receive the parameter null
.
*
* * Order of events: *
BeanContext
is the same as the old
* one, nothing happens.
* validatePendingSetBeanContext()
. If this call
* returns false
, the change is rejected and a
* PropertyVetoException
is thrown.
* VetoableChangeEvent
s are fired with the name
* "beanContext"
, using the
* fireVetoableChange()
method. If a veto
* occurs, reversion events are fired using the same method,
* the change is rejected, and the veto is rethrown.
* releaseBeanContextResources()
is called.
* PropertyChangeEvent
s are fired using the
* firePropertyChange()
method.
* initializeBeanContextResources()
is called.
*
*
* @param newBeanContext the new parent for the
* BeanContextChild
, or null
to
* signify removal from a tree.
* @exception PropertyVetoException if the
* BeanContextChild
implementor does not
* wish to have its parent changed.
*/
public void setBeanContext(BeanContext newBeanContext)
throws PropertyVetoException {
synchronized(beanContextChildPeer) {
if(newBeanContext == beanContext)
return;
if(!rejectedSetBCOnce) {
if(!validatePendingSetBeanContext(newBeanContext)) {
rejectedSetBCOnce = true;
throw new PropertyVetoException("validatePendingSetBeanContext() rejected change",
new PropertyChangeEvent(beanContextChildPeer, "beanContext", beanContext, newBeanContext));
}
try {
fireVetoableChange("beanContext", beanContext, newBeanContext);
} catch(PropertyVetoException e) {
rejectedSetBCOnce = true;
throw e;
}
}
releaseBeanContextResources();
beanContext = newBeanContext;
rejectedSetBCOnce = false;
firePropertyChange("beanContext", beanContext, newBeanContext);
initializeBeanContextResources();
}
}
/**
* Get the parent BeanContext
.
* @return the parent BeanContext
.
*/
public BeanContext getBeanContext() {
return beanContext;
}
/**
* Get the peer (or this
if there is no peer).
* @return the peer, or this
if there is no peer.
*/
public BeanContextChild getBeanContextChildPeer() {
return beanContextChildPeer;
}
/**
* Determine whether there is a peer.
* This is true iff getBeanContextChildPeer() == this
.
* @return whether there is a peer.
*/
public boolean isDelegated() {
return beanContextChildPeer == this;
}
/**
* Add a listener that will be notified when a specific property changes.
* @param propertyName the name of the property to listen on.
* @param listener the listener to listen on the property.
*/
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(propertyName, listener);
}
/**
* Remove a listener to a certain property.
*
* @param propertyName the name of the property being listened on.
* @param listener the listener listening on the property.
*/
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(propertyName, listener);
}
/**
* Add a listener that will be notified when a specific property
* change is requested (a PropertyVetoException may be thrown) as
* well as after the change is successfully made.
*
* @param propertyName the name of the property to listen on.
* @param listener the listener to listen on the property.
*/
public void addVetoableChangeListener(String propertyName, VetoableChangeListener listener) {
vcSupport.addVetoableChangeListener(propertyName, listener);
}
/**
* Remove a listener to a certain property.
*
* @param propertyName the name of the property being listened on
* @param listener the listener listening on the property.
*/
public void removeVetoableChangeListener(String propertyName, VetoableChangeListener listener) {
vcSupport.removeVetoableChangeListener(propertyName, listener);
}
/**
* Fire a property change.
*
* @param propertyName the name of the property that changed
* @param oldVal the old value of the property
* @param newVal the new value of the property
*/
public void firePropertyChange(String propertyName, Object oldVal, Object newVal) {
pcSupport.firePropertyChange(propertyName, oldVal, newVal);
}
/**
* Fire a vetoable property change.
*
* @param propertyName the name of the property that changed
* @param oldVal the old value of the property
* @param newVal the new value of the property
* @exception PropertyVetoException if the change is vetoed.
*/
public void fireVetoableChange(String propertyName, Object oldVal, Object newVal)
throws PropertyVetoException {
vcSupport.fireVetoableChange(propertyName, oldVal, newVal);
}
/**
* Called by BeanContextServices.revokeService()
to indicate that a service has been revoked.
* If you have a reference to such a service, it should be
* discarded and may no longer function properly.
* getService()
will no longer work on the specified
* service class after this event has been fired.
*
*
* This method is meant to be overriden.
* BeanContextChildSupport
's implementation does
* nothing.
*
* @param event the service revoked event.
* @see java.beans.beancontext.BeanContextServices#revokeService(java.lang.Class,java.beans.beancontext.BeanContextServiceProvider,boolean)
*/
public void serviceRevoked(BeanContextServiceRevokedEvent event) {
}
/**
* Called by BeanContextServices
whenever a service is made available.
*
*
* This method is meant to be overriden.
* BeanContextChildSupport
's implementation does
* nothing.
*
* @param event the service revoked event, with useful information
* about the new service.
*/
public void serviceAvailable(BeanContextServiceAvailableEvent event) {
}
/**
* Called by setBeanContext()
to determine whether the set should be rejected.
*
*
* This method is meant to be overriden.
* BeanContextChildSupport
's implementation simply
* returns true
.
*
* @param newBeanContext the new parent.
* @return whether to allow the parent to be changed to the new
* value.
*/
public boolean validatePendingSetBeanContext(BeanContext newBeanContext) {
return true;
}
/**
* Called by setBeanContext()
to release resources of a what will soon no longer be the parent.
*
*
* This method is meant to be overriden.
* BeanContextChildSupport
's implementation does
* nothing.
*/
protected void releaseBeanContextResources() {
}
/**
* Called by setBeanContext()
to grab resources when the parent has been set.
*
*
* This method is meant to be overriden.
* BeanContextChildSupport
's implementation does
* nothing.
*/
protected void initializeBeanContextResources() {
}
}