java.beans.beancontext

Class BeanContextSupport

public class BeanContextSupport extends BeanContextChildSupport implements BeanContext, Serializable, PropertyChangeListener, VetoableChangeListener

This is a helper class for implementing a bean context. It is intended to be used either by subclassing or by calling methods of this implementation from another.

Since: 1.2

Nested Class Summary
protected classBeanContextSupport.BCSChild
protected static classBeanContextSupport.BCSIterator
Field Summary
protected ArrayListbcmListeners
protected HashMapchildren
protected booleandesignTime
protected Localelocale
protected booleanokToUseGui
Constructor Summary
BeanContextSupport()
Construct a BeanContextSupport instance.
BeanContextSupport(BeanContext peer)
Construct a BeanContextSupport instance.
BeanContextSupport(BeanContext peer, Locale locale)
Construct a BeanContextSupport instance.
BeanContextSupport(BeanContext peer, Locale locale, boolean dtime)
Construct a BeanContextSupport instance.
BeanContextSupport(BeanContext peer, Locale locale, boolean dtime, boolean visible)
Construct a BeanContextSupport instance.
Method Summary
booleanadd(Object targetChild)

Add a child to the bean context.

booleanaddAll(Collection c)
voidaddBeanContextMembershipListener(BeanContextMembershipListener listener)
booleanavoidingGui()
Returns true if this bean needs a GUI but is being prevented from using one.
protected IteratorbcsChildren()
protected voidbcsPreDeserializationHook(ObjectInputStream ois)
Subclasses may use this method to perform their own deserialization after the default deserialization process has taken place, but prior to the deserialization of the children.
protected voidbcsPreSerializationHook(ObjectOutputStream oos)
Subclasses may use this method to perform their own serialization after the default serialization process has taken place, but prior to the serialization of the children.
protected voidchildDeserializedHook(Object child, BeanContextSupport.BCSChild bcsc)
Called when a child is deserialized.
protected voidchildJustAddedHook(Object child, BeanContextSupport.BCSChild bcsc)
protected voidchildJustRemovedHook(Object child, BeanContextSupport.BCSChild bcsc)
protected static booleanclassEquals(Class first, Class second)
voidclear()
booleancontains(Object o)
booleancontainsAll(Collection c)
booleancontainsKey(Object o)
protected Object[]copyChildren()
protected BeanContextSupport.BCSChildcreateBCSChild(Object targetChild, Object peer)
protected voiddeserialize(ObjectInputStream ois, Collection coll)
Deserializes objects (written by {@link #serialize(ObjectOutputStream, Collection)}) and adds them to the specified collection.
voiddontUseGui()
Informs this bean that is should not make use of the GUI.
protected voidfireChildrenAdded(BeanContextMembershipEvent bcme)
protected voidfireChildrenRemoved(BeanContextMembershipEvent bcme)
BeanContextgetBeanContextPeer()
Returns the bean context peer.
protected static BeanContextChildgetChildBeanContextChild(Object child)
Returns the {@link BeanContextChild} implementation for the given child.
protected static BeanContextMembershipListenergetChildBeanContextMembershipListener(Object child)
Returns child as an instance of {@link BeanContextMembershipListener}, or null if child does not implement that interface.
protected static PropertyChangeListenergetChildPropertyChangeListener(Object child)
Returns child as an instance of {@link PropertyChangeListener}, or null if child does not implement that interface.
protected static SerializablegetChildSerializable(Object child)
Returns child as an instance of {@link Serializable}, or null if child does not implement that interface.
protected static VetoableChangeListenergetChildVetoableChangeListener(Object child)
Returns child as an instance of {@link VetoableChangeListener}, or null if child does not implement that interface.
protected static VisibilitygetChildVisibility(Object child)
Returns child as an instance of {@link Visibility}, or null if child does not implement that interface.
LocalegetLocale()
URLgetResource(String name, BeanContextChild bcc)
InputStreamgetResourceAsStream(String name, BeanContextChild bcc)
protected voidinitialize()
ObjectinstantiateChild(String beanName)
This is a convenience method for instantiating a bean inside this context.
booleanisDesignTime()
Returns true if the BeanContext is in design time mode, and false if it is in runtime mode.
booleanisEmpty()
Returns true if this bean context has no children.
booleanisSerializing()
Returns true if the bean context is in the process of being serialized.
Iteratoriterator()
booleanneedsGui()
Returns false as this bean does not a GUI for its operation.
voidokToUseGui()
Informs this bean that it is okay to make use of the GUI.
voidpropertyChange(PropertyChangeEvent pce)
Subclasses may use this method to catch property changes arising from the children of this context.
voidreadChildren(ObjectInputStream ois)
Deerializes the children using the {@link #deserialize(ObjectInputStream, Collection} method and then calls {@link childDeserializedHook(Object, BCSChild)} for each child deserialized.
booleanremove(Object targetChild)
Remove the specified child from the context.
protected booleanremove(Object targetChild, boolean callChildSetBC)

Removes a child from the bean context.

booleanremoveAll(Collection c)
voidremoveBeanContextMembershipListener(BeanContextMembershipListener bcml)
booleanretainAll(Collection c)
protected voidserialize(ObjectOutputStream oos, Collection coll)
Writes the items in the collection to the specified output stream.
voidsetDesignTime(boolean dtime)
Sets the flag that indicates whether or not the BeanContext is in design mode.
voidsetLocale(Locale newLocale)
intsize()
Object[]toArray()
Returns an array containing the children of this BeanContext.
Object[]toArray(Object[] array)
Populates, then returns, the supplied array with the children of this BeanContext.
protected booleanvalidatePendingAdd(Object targetChild)
protected booleanvalidatePendingRemove(Object targetChild)
voidvetoableChange(PropertyChangeEvent pce)
Subclasses may use this method to veto changes arising from the children of this context.
voidwriteChildren(ObjectOutputStream oos)
Serializes the children using the {@link #serialize(ObjectOutputStream, Collection} method.

Field Detail

bcmListeners

protected transient ArrayList bcmListeners

children

protected transient HashMap children

designTime

protected transient boolean designTime

locale

protected transient Locale locale

okToUseGui

protected transient boolean okToUseGui

Constructor Detail

BeanContextSupport

public BeanContextSupport()
Construct a BeanContextSupport instance.

BeanContextSupport

public BeanContextSupport(BeanContext peer)
Construct a BeanContextSupport instance.

Parameters: peer the bean context peer (null permitted).

BeanContextSupport

public BeanContextSupport(BeanContext peer, Locale locale)
Construct a BeanContextSupport instance.

Parameters: peer the bean context peer (null permitted). locale the locale (null permitted, equivalent to the default locale).

BeanContextSupport

public BeanContextSupport(BeanContext peer, Locale locale, boolean dtime)
Construct a BeanContextSupport instance.

Parameters: peer the bean context peer (null permitted). locale the locale (null permitted, equivalent to the default locale). dtime a flag indicating whether or not the bean context is in design time mode.

BeanContextSupport

public BeanContextSupport(BeanContext peer, Locale locale, boolean dtime, boolean visible)
Construct a BeanContextSupport instance.

Parameters: peer the bean context peer (null permitted). locale the locale (null permitted, equivalent to the default locale). dtime a flag indicating whether or not the bean context is in design time mode. visible initial value of the okToUseGui flag.

Method Detail

add

public boolean add(Object targetChild)

Add a child to the bean context. A child can be a simple Object, a BeanContextChild or another BeanContext.

The children of a BeanContext form a set. As a result, this method returns false if the given object is already a child of this context.

If the child is a BeanContextChild, or a proxy for such a child, the setBeanContext() method is invoked on the child. If this operation is vetoed by the child, via throwing a PropertyVetoException, then the current completion state of the add() operation is rolled back and a IllegalStateException is thrown. If the BeanContextChild is successfully added, then the context registers with its PropertyChangeListener and VetoableChangeListener for "beanContext" events.

If the child implements java.beans.Visibility, then its ability to use a GUI is set based on that of this context.

A BeanContextMembershipEvent is fired when the child is successfully added to the bean context.

This method is synchronized over the global hierarchy lock.

Parameters: targetChild the child to add.

Returns: false if the child has already been added.

Throws: IllegalArgumentException if the child is null. IllegalStateException if the child vetos the setting of its context.

addAll

public boolean addAll(Collection c)

addBeanContextMembershipListener

public void addBeanContextMembershipListener(BeanContextMembershipListener listener)

avoidingGui

public boolean avoidingGui()
Returns true if this bean needs a GUI but is being prevented from using one.

Returns: true if needsGui() is true but the bean has been told not to use it.

bcsChildren

protected Iterator bcsChildren()

bcsPreDeserializationHook

protected void bcsPreDeserializationHook(ObjectInputStream ois)
Subclasses may use this method to perform their own deserialization after the default deserialization process has taken place, but prior to the deserialization of the children. It should not be used to replace the implementation of readObject in the subclass.

Parameters: ois the input stream.

Throws: ClassNotFoundException if the class of an object being deserialized could not be found. IOException if an I/O error occurs.

bcsPreSerializationHook

protected void bcsPreSerializationHook(ObjectOutputStream oos)
Subclasses may use this method to perform their own serialization after the default serialization process has taken place, but prior to the serialization of the children. It should not be used to replace the implementation of writeObject in the subclass.

Parameters: oos the output stream.

Throws: IOException if an I/O error occurs.

childDeserializedHook

protected void childDeserializedHook(Object child, BeanContextSupport.BCSChild bcsc)
Called when a child is deserialized.

Parameters: child the deserialized child. bcsc the deserialized context wrapper for the child.

childJustAddedHook

protected void childJustAddedHook(Object child, BeanContextSupport.BCSChild bcsc)

childJustRemovedHook

protected void childJustRemovedHook(Object child, BeanContextSupport.BCSChild bcsc)

classEquals

protected static final boolean classEquals(Class first, Class second)

clear

public void clear()

contains

public boolean contains(Object o)

containsAll

public boolean containsAll(Collection c)

containsKey

public boolean containsKey(Object o)

copyChildren

protected final Object[] copyChildren()

createBCSChild

protected BeanContextSupport.BCSChild createBCSChild(Object targetChild, Object peer)

deserialize

protected final void deserialize(ObjectInputStream ois, Collection coll)
Deserializes objects (written by {@link #serialize(ObjectOutputStream, Collection)}) and adds them to the specified collection.

Parameters: ois the input stream (null not permitted). coll the collection to add the objects to (null not permitted).

Throws: ClassNotFoundException IOException

See Also: BeanContextSupport

dontUseGui

public void dontUseGui()
Informs this bean that is should not make use of the GUI.

fireChildrenAdded

protected final void fireChildrenAdded(BeanContextMembershipEvent bcme)

fireChildrenRemoved

protected final void fireChildrenRemoved(BeanContextMembershipEvent bcme)

getBeanContextPeer

public BeanContext getBeanContextPeer()
Returns the bean context peer.

Returns: The bean context peer.

See Also: beanContextChildPeer

getChildBeanContextChild

protected static final BeanContextChild getChildBeanContextChild(Object child)
Returns the {@link BeanContextChild} implementation for the given child.

Parameters: child the child (null permitted).

Returns: The bean context child.

Throws: IllegalArgumentException if child implements both the {@link BeanContextChild} and {@link BeanContextProxy} interfaces.

getChildBeanContextMembershipListener

protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(Object child)
Returns child as an instance of {@link BeanContextMembershipListener}, or null if child does not implement that interface.

Parameters: child the child (null permitted).

Returns: The child cast to {@link BeanContextMembershipListener}.

getChildPropertyChangeListener

protected static final PropertyChangeListener getChildPropertyChangeListener(Object child)
Returns child as an instance of {@link PropertyChangeListener}, or null if child does not implement that interface.

Parameters: child the child (null permitted).

Returns: The child cast to {@link PropertyChangeListener}.

getChildSerializable

protected static final Serializable getChildSerializable(Object child)
Returns child as an instance of {@link Serializable}, or null if child does not implement that interface.

Parameters: child the child (null permitted).

Returns: The child cast to {@link Serializable}.

getChildVetoableChangeListener

protected static final VetoableChangeListener getChildVetoableChangeListener(Object child)
Returns child as an instance of {@link VetoableChangeListener}, or null if child does not implement that interface.

Parameters: child the child (null permitted).

Returns: The child cast to {@link VetoableChangeListener}.

getChildVisibility

protected static final Visibility getChildVisibility(Object child)
Returns child as an instance of {@link Visibility}, or null if child does not implement that interface.

Parameters: child the child (null permitted).

Returns: The child cast to {@link Visibility}.

getLocale

public Locale getLocale()

getResource

public URL getResource(String name, BeanContextChild bcc)

getResourceAsStream

public InputStream getResourceAsStream(String name, BeanContextChild bcc)

initialize

protected void initialize()

instantiateChild

public Object instantiateChild(String beanName)
This is a convenience method for instantiating a bean inside this context. It delegates to the appropriate method in java.beans.Beans using the context's classloader.

Parameters: beanName the name of the class of bean to instantiate.

Throws: IOException if an I/O error occurs in loading the class. ClassNotFoundException if the class, beanName, can not be found.

isDesignTime

public boolean isDesignTime()
Returns true if the BeanContext is in design time mode, and false if it is in runtime mode.

Returns: A boolean.

See Also: BeanContextSupport

isEmpty

public boolean isEmpty()
Returns true if this bean context has no children.

Returns: true if there are no children.

isSerializing

public boolean isSerializing()
Returns true if the bean context is in the process of being serialized.

Returns: true if the context is being serialized.

iterator

public Iterator iterator()

needsGui

public boolean needsGui()
Returns false as this bean does not a GUI for its operation.

Returns: false

okToUseGui

public void okToUseGui()
Informs this bean that it is okay to make use of the GUI.

propertyChange

public void propertyChange(PropertyChangeEvent pce)
Subclasses may use this method to catch property changes arising from the children of this context. At present, we just listen for the beans being assigned to a different context and remove them from here if such an event occurs.

Parameters: pce the property change event.

readChildren

public final void readChildren(ObjectInputStream ois)
Deerializes the children using the {@link #deserialize(ObjectInputStream, Collection} method and then calls {@link childDeserializedHook(Object, BCSChild)} for each child deserialized.

Parameters: oos the output stream.

Throws: IOException if an I/O error occurs.

remove

public boolean remove(Object targetChild)
Remove the specified child from the context. This is the same as calling remove(Object,boolean) with a request for the setBeanContext() method of the child to be called (i.e. the second argument is true).

Parameters: targetChild the child to remove.

remove

protected boolean remove(Object targetChild, boolean callChildSetBC)

Removes a child from the bean context. A child can be a simple Object, a BeanContextChild or another BeanContext. If the given child is not a child of this context, this method returns false.

If the child is a BeanContextChild, or a proxy for such a child, the setBeanContext() method is invoked on the child (if specified). If this operation is vetoed by the child, via throwing a PropertyVetoException, then the current completion state of the remove() operation is rolled back and a IllegalStateException is thrown. If the BeanContextChild is successfully removed, then the context deregisters with its PropertyChangeListener and VetoableChangeListener for "beanContext" events.

A BeanContextMembershipEvent is fired when the child is successfully removed from the bean context.

This method is synchronized over the global hierarchy lock.

Parameters: targetChild the child to remove. callChildSetBC true if the setBeanContext() method of the child should be called.

Returns: false if the child doesn't exist.

Throws: IllegalArgumentException if the child is null. IllegalStateException if the child vetos the setting of its context.

removeAll

public boolean removeAll(Collection c)

removeBeanContextMembershipListener

public void removeBeanContextMembershipListener(BeanContextMembershipListener bcml)

retainAll

public boolean retainAll(Collection c)

serialize

protected final void serialize(ObjectOutputStream oos, Collection coll)
Writes the items in the collection to the specified output stream. Items in the collection that are not instances of {@link Serializable} (this includes null) are simply ignored.

Parameters: oos the output stream (null not permitted). coll the collection (null not permitted).

Throws: IOException

See Also: BeanContextSupport

setDesignTime

public void setDesignTime(boolean dtime)
Sets the flag that indicates whether or not the BeanContext is in design mode. If the flag changes value, a {@link PropertyChangeEvent} (with the property name 'designMode') is sent to registered listeners. Note that the property name used here does NOT match the specification in the {@link DesignMode} interface, we match the reference implementation instead - see bug parade entry 4295174.

Parameters: dtime the new value for the flag.

See Also: isDesignTime

setLocale

public void setLocale(Locale newLocale)

size

public int size()

toArray

public Object[] toArray()
Returns an array containing the children of this BeanContext.

Returns: An array containing the children.

toArray

public Object[] toArray(Object[] array)
Populates, then returns, the supplied array with the children of this BeanContext. If the array is too short to hold the children, a new array is allocated and returned. If the array is too long, it is padded with null items at the end.

Parameters: array an array to populate (null not permitted).

validatePendingAdd

protected boolean validatePendingAdd(Object targetChild)

validatePendingRemove

protected boolean validatePendingRemove(Object targetChild)

vetoableChange

public void vetoableChange(PropertyChangeEvent pce)
Subclasses may use this method to veto changes arising from the children of this context.

Parameters: pce the vetoable property change event fired.

writeChildren

public final void writeChildren(ObjectOutputStream oos)
Serializes the children using the {@link #serialize(ObjectOutputStream, Collection} method.

Parameters: oos the output stream.

Throws: IOException if an I/O error occurs.