Class BeanContextSupport
- All Implemented Interfaces:
Serializable
,Iterable
,Collection
,EventListener
,BeanContext
,BeanContextChild
,BeanContextServiceRevokedListener
,BeanContextServicesListener
,DesignMode
,PropertyChangeListener
,VetoableChangeListener
,Visibility
- Direct Known Subclasses:
BeanContextServicesSupport
BeanContext
interface.
This class can be used directly, or be a super class of your class,
or be a delegate of your implementation that needs to support
BeanContext
interface.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
Every child of context is companied with aBCSChild
instance.protected static final class
This implementation wraps an iterator and overrideremove()
with a noop method. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ArrayList
A list of registered membership listeners.protected HashMap
A map of children - key is child instance, value isBCSChild
instance.protected boolean
A flag indicating whether this context is in design mode.protected Locale
The locale of this context.protected boolean
A flag indicating whether this context is allowed to use GUI.Fields inherited from class javaa.beans.beancontext.BeanContextChildSupport
beanContext, beanContextChildPeer, pcSupport, rejectedSetBCOnce, vcSupport
Fields inherited from interface javaa.beans.beancontext.BeanContext
globalHierarchyLock
Fields inherited from interface javaa.beans.DesignMode
PROPERTYNAME
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a standloadBeanContextSupport
.Constructs aBeanContextSupport
which is a delegate of the given peer.BeanContextSupport
(BeanContext peer, Locale locale) Constructs aBeanContextSupport
which is a delegate of the given peer.BeanContextSupport
(BeanContext peer, Locale locale, boolean designTime) Constructs aBeanContextSupport
which is a delegate of the given peer.BeanContextSupport
(BeanContext peer, Locale locale, boolean designTime, boolean okToUseGui) Constructs aBeanContextSupport
which is a delegate of the given peer. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add a child to this context.boolean
addAll
(Collection collection) This method is unsupported, throwsUnsupportedOperationException
.void
boolean
protected Iterator
Returns an iterator of allBCSChild
instances, withremove()
disabled.protected void
This method is called byreadObject()
afterdefaultReadObject()
and before deserializing any children or listeners.protected void
This method is called bywriteObject()
afterdefaultWriteObject()
and before serializing any children or listeners.protected void
childDeserializedHook
(Object child, BeanContextSupport.BCSChild bcsChild) This method is called during deserialization everytime a child is read.protected void
childJustAddedHook
(Object child, BeanContextSupport.BCSChild bcsChild) This method is called everytime a child is added to this context.protected void
childJustRemovedHook
(Object child, BeanContextSupport.BCSChild bcsChild) This method is called everytime a child is removed from this context.protected static final boolean
classEquals
(Class clz1, Class clz2) Compares if two classes are equal or their class names are equal.void
clear()
This method is unsupported, throwsUnsupportedOperationException
.boolean
Returns true if the given object is a child of this context.boolean
containsAll
(Collection collection) Returns true if given objects are children of this context.boolean
containsKey
(Object child) Returns true if the given object is a child of this context.protected final Object[]
Returns an array containing all children of this context.protected BeanContextSupport.BCSChild
createBCSChild
(Object child, Object proxyPeer) Creates aBCSChild
object to company the given child.protected final void
deserialize
(ObjectInputStream ois, Collection collection) Deserialize a collection.void
protected final void
Notifies registeredBeanContextMembershipListener
s that a new child has been added.protected final void
Notifies registeredBeanContextMembershipListener
s that a child has been removed.Returns the peer of this context casted asBeanContext
.protected static final BeanContextChild
getChildBeanContextChild
(Object child) Returns theBeanContextChild
related with the given child.protected static final BeanContextMembershipListener
Returns the given child casted toBeanContextMembershipListener
, or null if it does not implements the interface.protected static final PropertyChangeListener
Returns the given child casted toPropertyChangeListener
, or null if it does not implements the interface.protected static final Serializable
getChildSerializable
(Object child) Returns the given child casted toSerializable
, or null if it does not implements the interface.protected static final VetoableChangeListener
Returns the given child casted toVetoableChangeListener
, or null if it does not implements the interface.protected static final Visibility
getChildVisibility
(Object child) Returns the given child casted toVisibility
, or null if it does not implements the interface.Returns the locale of this context.getResource
(String resourceName, BeanContextChild child) getResourceAsStream
(String resourceName, BeanContextChild child) protected void
Initializes all transient fields of this instance, called by constructors andreadObject()
.instantiateChild
(String beanName) boolean
boolean
isEmpty()
boolean
Returns true if this context is currently being serialized (by another thread).iterator()
Returns an iterator of children of this context, withremove()
disabled.boolean
needsGui()
Returns true if this context or its children needs GUI to work properly.void
void
final void
Deserializes children from the given object input stream.boolean
Removes the given child from this context.protected boolean
Removes the given child from this context.boolean
removeAll
(Collection collection) This method is unsupported, throwsUnsupportedOperationException
.void
boolean
retainAll
(Collection collection) This method is unsupported, throwsUnsupportedOperationException
.protected final void
serialize
(ObjectOutputStream oos, Collection collection) Serializes the given collection.void
setDesignTime
(boolean designTime) void
Sets the locale of this context.int
size()
Returns the number children of this context.Object[]
toArray()
Returns an array of children of this context.Object[]
Returns an array of children of this context.protected boolean
validatePendingAdd
(Object child) Validates the pending add of child.protected boolean
validatePendingRemove
(Object child) Validates the pending removal of child.void
final void
Serializes children to the given object input stream.Methods inherited from class javaa.beans.beancontext.BeanContextChildSupport
addPropertyChangeListener, addVetoableChangeListener, firePropertyChange, fireVetoableChange, getBeanContext, getBeanContextChildPeer, initializeBeanContextResources, isDelegated, releaseBeanContextResources, removePropertyChangeListener, removeVetoableChangeListener, serviceAvailable, serviceRevoked, setBeanContext, validatePendingSetBeanContext
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javaa.beans.beancontext.BeanContextChild
addPropertyChangeListener, addVetoableChangeListener, getBeanContext, removePropertyChangeListener, removeVetoableChangeListener, setBeanContext
Methods inherited from interface java.util.Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
bcmListeners
A list of registered membership listeners. All access to this object should be synchronized on itself. -
children
A map of children - key is child instance, value isBCSChild
instance. All access to this object should be synchronized on itself. -
locale
The locale of this context. -
okToUseGui
protected boolean okToUseGuiA flag indicating whether this context is allowed to use GUI. -
designTime
protected boolean designTimeA flag indicating whether this context is in design mode.
-
-
Constructor Details
-
BeanContextSupport
public BeanContextSupport()Constructs a standloadBeanContextSupport
. -
BeanContextSupport
Constructs aBeanContextSupport
which is a delegate of the given peer.- Parameters:
peer
- the peer of this context
-
BeanContextSupport
Constructs aBeanContextSupport
which is a delegate of the given peer.- Parameters:
peer
- the peer of this contextlocale
- the locale of this context
-
BeanContextSupport
Constructs aBeanContextSupport
which is a delegate of the given peer.- Parameters:
peer
- the peer of this contextlocale
- the locale of this contextdesignTime
- whether in design mode or not
-
BeanContextSupport
Constructs aBeanContextSupport
which is a delegate of the given peer.- Parameters:
peer
- the peer of this contextlocale
- the locale of this contextdesignTime
- whether in design mode or notokToUseGui
- whether GUI is usable or not
-
-
Method Details
-
add
Add a child to this context.If the child already exists in this context, simply returns false. Otherwise, it is validated by calling
validatePendingAdd()
. If the add is valid, the child and its proxy (if the child implementsBeanContextProxy
) is then added, andsetBeanContext()
is called on it (if the child implementsBeanContextChild
or it has a proxy). Last, thechildJustAddedHook()
is called and all registeredBeanContextMembershipListener
s are notified.- Specified by:
add
in interfaceCollection
- Parameters:
child
- the child to add- Returns:
- true if the child is added to this context; otherwise false
- Throws:
IllegalStateException
- if the child is not valid to add- See Also:
-
addAll
This method is unsupported, throwsUnsupportedOperationException
.- Specified by:
addAll
in interfaceCollection
- See Also:
-
addBeanContextMembershipListener
- Specified by:
addBeanContextMembershipListener
in interfaceBeanContext
-
avoidingGui
public boolean avoidingGui()- Specified by:
avoidingGui
in interfaceVisibility
-
bcsChildren
Returns an iterator of allBCSChild
instances, withremove()
disabled.- Returns:
- an iterator of all
BCSChild
instances
-
bcsPreDeserializationHook
protected void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException, ClassNotFoundException This method is called byreadObject()
afterdefaultReadObject()
and before deserializing any children or listeners. Subclass can insert its specific deserialization behavior by overrideing this method.The default implementation does nothing.
- Parameters:
ois
- the object input stream- Throws:
IOException
ClassNotFoundException
-
bcsPreSerializationHook
This method is called bywriteObject()
afterdefaultWriteObject()
and before serializing any children or listeners. Subclass can insert its specific serialization behavior by overrideing this method.The default implementation does nothing.
- Parameters:
oos
- the object output stream- Throws:
IOException
-
childDeserializedHook
This method is called during deserialization everytime a child is read.The default implementation does nothing.
- Parameters:
child
- the child just deserializedbcsChild
- theBCSChild
just deserialized
-
childJustAddedHook
This method is called everytime a child is added to this context. This method is called with child synchronized.The default implementation does nothing.
- Parameters:
child
- the child just addedbcsChild
- theBCSChild
just added
-
childJustRemovedHook
This method is called everytime a child is removed from this context. This method is called with child synchronized.The default implementation does nothing.
- Parameters:
child
- the child just removedbcsChild
- theBCSChild
just removed
-
classEquals
Compares if two classes are equal or their class names are equal.- Parameters:
clz1
- a classclz2
- another class- Returns:
- true if two class objects are equal or their class names are equal.
-
clear
public void clear()This method is unsupported, throwsUnsupportedOperationException
.- Specified by:
clear
in interfaceCollection
- See Also:
-
contains
Returns true if the given object is a child of this context.- Specified by:
contains
in interfaceCollection
- Parameters:
child
- the object to test- Returns:
- true if the given object is a child of this context
- See Also:
-
containsAll
Returns true if given objects are children of this context.- Specified by:
containsAll
in interfaceCollection
- Parameters:
collection
- a collection of objects- Returns:
- true if given objects are children of this context
- See Also:
-
containsKey
Returns true if the given object is a child of this context.- Parameters:
child
- the object to test- Returns:
- true if the given object is a child of this context
-
copyChildren
Returns an array containing all children of this context.- Returns:
- an array containing all children of this context
-
createBCSChild
Creates aBCSChild
object to company the given child.- Parameters:
child
- the childproxyPeer
- the proxy peer of the child if there is one- Returns:
- a
BCSChild
object to company the given child
-
deserialize
protected final void deserialize(ObjectInputStream ois, Collection collection) throws IOException, ClassNotFoundException Deserialize a collection.First read a
int
indicating of number of rest objects, then read the objects one by one.- Parameters:
ois
- the stream where the collection is read fromcollection
- the collection to hold read objects- Throws:
IOException
- if I/O exception occursClassNotFoundException
- if class of any read object is not found
-
dontUseGui
public void dontUseGui()- Specified by:
dontUseGui
in interfaceVisibility
-
fireChildrenAdded
Notifies registeredBeanContextMembershipListener
s that a new child has been added.- Parameters:
event
- theBeanContextMembershipEvent
-
fireChildrenRemoved
Notifies registeredBeanContextMembershipListener
s that a child has been removed.- Parameters:
event
- theBeanContextMembershipEvent
-
getBeanContextPeer
Returns the peer of this context casted asBeanContext
.- Returns:
- the peer of this context casted as
BeanContext
-
getChildBeanContextChild
Returns theBeanContextChild
related with the given child.If the child implements
BeanContextChild
, it is returned. If the child implementsBeanContextProxy
, the proxy is returned. Otherwise, null is returned.- Parameters:
child
- a child- Returns:
- the
BeanContextChild
related with the given child - Throws:
IllegalStateException
- if the child implements bothBeanContextChild
andBeanContextProxy
-
getChildBeanContextMembershipListener
protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(Object child) Returns the given child casted toBeanContextMembershipListener
, or null if it does not implements the interface.- Parameters:
child
- a child- Returns:
- the given child casted to
BeanContextMembershipListener
, or null if it does not implements the interface
-
getChildPropertyChangeListener
Returns the given child casted toPropertyChangeListener
, or null if it does not implements the interface.- Parameters:
child
- a child- Returns:
- the given child casted to
PropertyChangeListener
, or null if it does not implements the interface
-
getChildSerializable
Returns the given child casted toSerializable
, or null if it does not implements the interface.- Parameters:
child
- a child- Returns:
- the given child casted to
Serializable
, or null if it does not implements the interface
-
getChildVetoableChangeListener
Returns the given child casted toVetoableChangeListener
, or null if it does not implements the interface.- Parameters:
child
- a child- Returns:
- the given child casted to
VetoableChangeListener
, or null if it does not implements the interface
-
getChildVisibility
Returns the given child casted toVisibility
, or null if it does not implements the interface.- Parameters:
child
- a child- Returns:
- the given child casted to
Visibility
, or null if it does not implements the interface
-
getLocale
Returns the locale of this context.- Returns:
- the locale of this context
-
getResource
- Specified by:
getResource
in interfaceBeanContext
-
getResourceAsStream
public InputStream getResourceAsStream(String resourceName, BeanContextChild child) throws IllegalArgumentException - Specified by:
getResourceAsStream
in interfaceBeanContext
- Throws:
IllegalArgumentException
-
initialize
protected void initialize()Initializes all transient fields of this instance, called by constructors andreadObject()
. -
instantiateChild
- Specified by:
instantiateChild
in interfaceBeanContext
- Throws:
IOException
ClassNotFoundException
-
isDesignTime
public boolean isDesignTime()- Specified by:
isDesignTime
in interfaceDesignMode
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection
-
isSerializing
public boolean isSerializing()Returns true if this context is currently being serialized (by another thread).- Returns:
- true if this context is currently being serialized (by another thread)
-
iterator
Returns an iterator of children of this context, withremove()
disabled.- Specified by:
iterator
in interfaceCollection
- Specified by:
iterator
in interfaceIterable
- See Also:
-
needsGui
public boolean needsGui()Returns true if this context or its children needs GUI to work properly.The implementation checks the peer and all the children that implement
Visibility
to see if any of theirneedsGui()
returns true, and if any of the children extendsjava.awt.Component
.- Specified by:
needsGui
in interfaceVisibility
- See Also:
-
okToUseGui
public void okToUseGui()- Specified by:
okToUseGui
in interfaceVisibility
-
propertyChange
- Specified by:
propertyChange
in interfacePropertyChangeListener
-
readChildren
Deserializes children from the given object input stream.The implementation reads pairs of child object and
BCSChild
object according toserializable
property. For each pair, it is added to thechildren
map and thechildDeserializedHook()
is called. If the child implementsBeanContextChild
, itssetBeanContext()
is also called.This method is called by
readObject()
if the context works standalone. Or if this support object is a delegate of anotherBeanContext
implementation, then this method should be called by the peer. Doing this means that derialization can proceed without any circular dependency problems.- Parameters:
ois
- the object input stream- Throws:
IOException
- if I/O exception occursClassNotFoundException
- if class of read object is not found
-
remove
Removes the given child from this context.Delegates to
remove(child, true)
.- Specified by:
remove
in interfaceCollection
- Parameters:
child
- a child of this context- Returns:
- true if the child is removed; or false if it is not a child of this context
- Throws:
IllegalArgumentException
- if the child is nullIllegalStateException
- if the child is not valid to remove- See Also:
-
remove
Removes the given child from this context.If the given child is not a child of this context, simply returns false. Otherwise,
validatePendingRemove()
is called. If the removal is valid, the child'sbeanContext
property is updated (if required) and the child and its proxy peer (if there is one) is removed. Last,childJustRemovedHook()
is called and listeners are notified.- Parameters:
child
- a child of this contextsetChildBC
- whether to callsetBeanContext()
on the child or not- Returns:
- true if the child is removed; or false if it is not a child of this context
- Throws:
IllegalArgumentException
- if the child is nullIllegalStateException
- if the child is not valid to remove
-
removeAll
This method is unsupported, throwsUnsupportedOperationException
.- Specified by:
removeAll
in interfaceCollection
- See Also:
-
removeBeanContextMembershipListener
- Specified by:
removeBeanContextMembershipListener
in interfaceBeanContext
-
retainAll
This method is unsupported, throwsUnsupportedOperationException
.- Specified by:
retainAll
in interfaceCollection
- See Also:
-
serialize
Serializes the given collection.First writes a
int
indicating the number of all serializable elements (implementsSerializable
, then objects are writtern one by one.- Parameters:
oos
- the stream where the collection is writtern tocollection
- the collection to serialize- Throws:
IOException
- if I/O exception occurs
-
setDesignTime
public void setDesignTime(boolean designTime) - Specified by:
setDesignTime
in interfaceDesignMode
-
setLocale
Sets the locale of this context.VetoableChangeListener
s andPropertyChangeListener
s are notified.- Parameters:
newLocale
- the new locale to set- Throws:
PropertyVetoException
- if anyVetoableChangeListener
vetos this change
-
size
public int size()Returns the number children of this context.- Specified by:
size
in interfaceCollection
- Returns:
- the number children of this context
- See Also:
-
toArray
Returns an array of children of this context.- Specified by:
toArray
in interfaceCollection
- Returns:
- an array of children of this context
- See Also:
-
toArray
Returns an array of children of this context.- Specified by:
toArray
in interfaceCollection
- Returns:
- an array of children of this context
- See Also:
-
validatePendingAdd
Validates the pending add of child.Default implementation always returns true.
- Parameters:
child
- the child to be added- Returns:
- true if it is valid to add the child
-
validatePendingRemove
Validates the pending removal of child.Default implementation always returns true.
- Parameters:
child
- the child to be removed- Returns:
- true if it is valid to remove the child
-
vetoableChange
- Specified by:
vetoableChange
in interfaceVetoableChangeListener
- Throws:
PropertyVetoException
-
writeChildren
Serializes children to the given object input stream.The implementation iterates through all children and writes out pairs of child object and
BCSChild
object if the child is serializable (implementsSerialization
.This method is called by
writeObject()
if the context works standalone. Or if this support object is a delegate of anotherBeanContext
implementation, then this method should be called by the peer to avoid the 'chicken and egg' problem during deserialization.- Parameters:
oos
- the stream to write- Throws:
IOException
- if I/O exception occurs
-