Class BeanContextSupport

java.lang.Object
javaa.beans.beancontext.BeanContextChildSupport
javaa.beans.beancontext.BeanContextSupport
All Implemented Interfaces:
Serializable, Iterable, Collection, EventListener, BeanContext, BeanContextChild, BeanContextServiceRevokedListener, BeanContextServicesListener, DesignMode, PropertyChangeListener, VetoableChangeListener, Visibility
Direct Known Subclasses:
BeanContextServicesSupport

This support class implements 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:
  • Field Details

    • bcmListeners

      protected transient ArrayList bcmListeners
      A list of registered membership listeners. All access to this object should be synchronized on itself.
    • children

      protected transient HashMap children
      A map of children - key is child instance, value is BCSChild instance. All access to this object should be synchronized on itself.
    • locale

      protected Locale locale
      The locale of this context.
    • okToUseGui

      protected boolean okToUseGui
      A flag indicating whether this context is allowed to use GUI.
    • designTime

      protected boolean designTime
      A flag indicating whether this context is in design mode.
  • Constructor Details

    • BeanContextSupport

      public BeanContextSupport()
      Constructs a standload BeanContextSupport.
    • BeanContextSupport

      public BeanContextSupport(BeanContext peer)
      Constructs a BeanContextSupport which is a delegate of the given peer.
      Parameters:
      peer - the peer of this context
    • BeanContextSupport

      public BeanContextSupport(BeanContext peer, Locale locale)
      Constructs a BeanContextSupport which is a delegate of the given peer.
      Parameters:
      peer - the peer of this context
      locale - the locale of this context
    • BeanContextSupport

      public BeanContextSupport(BeanContext peer, Locale locale, boolean designTime)
      Constructs a BeanContextSupport which is a delegate of the given peer.
      Parameters:
      peer - the peer of this context
      locale - the locale of this context
      designTime - whether in design mode or not
    • BeanContextSupport

      public BeanContextSupport(BeanContext peer, Locale locale, boolean designTime, boolean okToUseGui)
      Constructs a BeanContextSupport which is a delegate of the given peer.
      Parameters:
      peer - the peer of this context
      locale - the locale of this context
      designTime - whether in design mode or not
      okToUseGui - whether GUI is usable or not
  • Method Details

    • add

      public boolean add(Object child)
      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 implements BeanContextProxy) is then added, and setBeanContext() is called on it (if the child implements BeanContextChild or it has a proxy). Last, the childJustAddedHook() is called and all registered BeanContextMembershipListeners are notified.

      Specified by:
      add in interface Collection
      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

      public boolean addAll(Collection collection)
      This method is unsupported, throws UnsupportedOperationException.
      Specified by:
      addAll in interface Collection
      See Also:
    • addBeanContextMembershipListener

      public void addBeanContextMembershipListener(BeanContextMembershipListener listener)
      Specified by:
      addBeanContextMembershipListener in interface BeanContext
    • avoidingGui

      public boolean avoidingGui()
      Specified by:
      avoidingGui in interface Visibility
    • bcsChildren

      protected Iterator bcsChildren()
      Returns an iterator of all BCSChild instances, with remove() disabled.
      Returns:
      an iterator of all BCSChild instances
    • bcsPreDeserializationHook

      protected void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException, ClassNotFoundException
      This method is called by readObject() after defaultReadObject() 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

      protected void bcsPreSerializationHook(ObjectOutputStream oos) throws IOException
      This method is called by writeObject() after defaultWriteObject() 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

      protected void childDeserializedHook(Object child, BeanContextSupport.BCSChild bcsChild)
      This method is called during deserialization everytime a child is read.

      The default implementation does nothing.

      Parameters:
      child - the child just deserialized
      bcsChild - the BCSChild just deserialized
    • childJustAddedHook

      protected void childJustAddedHook(Object child, BeanContextSupport.BCSChild bcsChild)
      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 added
      bcsChild - the BCSChild just added
    • childJustRemovedHook

      protected void childJustRemovedHook(Object child, BeanContextSupport.BCSChild bcsChild)
      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 removed
      bcsChild - the BCSChild just removed
    • classEquals

      protected static final boolean classEquals(Class clz1, Class clz2)
      Compares if two classes are equal or their class names are equal.
      Parameters:
      clz1 - a class
      clz2 - another class
      Returns:
      true if two class objects are equal or their class names are equal.
    • clear

      public void clear()
      This method is unsupported, throws UnsupportedOperationException.
      Specified by:
      clear in interface Collection
      See Also:
    • contains

      public boolean contains(Object child)
      Returns true if the given object is a child of this context.
      Specified by:
      contains in interface Collection
      Parameters:
      child - the object to test
      Returns:
      true if the given object is a child of this context
      See Also:
    • containsAll

      public boolean containsAll(Collection collection)
      Returns true if given objects are children of this context.
      Specified by:
      containsAll in interface Collection
      Parameters:
      collection - a collection of objects
      Returns:
      true if given objects are children of this context
      See Also:
    • containsKey

      public boolean containsKey(Object child)
      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

      protected final Object[] copyChildren()
      Returns an array containing all children of this context.
      Returns:
      an array containing all children of this context
    • createBCSChild

      protected BeanContextSupport.BCSChild createBCSChild(Object child, Object proxyPeer)
      Creates a BCSChild object to company the given child.
      Parameters:
      child - the child
      proxyPeer - 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 from
      collection - the collection to hold read objects
      Throws:
      IOException - if I/O exception occurs
      ClassNotFoundException - if class of any read object is not found
    • dontUseGui

      public void dontUseGui()
      Specified by:
      dontUseGui in interface Visibility
    • fireChildrenAdded

      protected final void fireChildrenAdded(BeanContextMembershipEvent event)
      Notifies registered BeanContextMembershipListeners that a new child has been added.
      Parameters:
      event - the BeanContextMembershipEvent
    • fireChildrenRemoved

      protected final void fireChildrenRemoved(BeanContextMembershipEvent event)
      Notifies registered BeanContextMembershipListeners that a child has been removed.
      Parameters:
      event - the BeanContextMembershipEvent
    • getBeanContextPeer

      public BeanContext getBeanContextPeer()
      Returns the peer of this context casted as BeanContext.
      Returns:
      the peer of this context casted as BeanContext
    • getChildBeanContextChild

      protected static final BeanContextChild getChildBeanContextChild(Object child)
      Returns the BeanContextChild related with the given child.

      If the child implements BeanContextChild, it is returned. If the child implements BeanContextProxy, 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 both BeanContextChild and BeanContextProxy
    • getChildBeanContextMembershipListener

      protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(Object child)
      Returns the given child casted to BeanContextMembershipListener, 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

      protected static final PropertyChangeListener getChildPropertyChangeListener(Object child)
      Returns the given child casted to PropertyChangeListener, 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

      protected static final Serializable getChildSerializable(Object child)
      Returns the given child casted to Serializable, 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

      protected static final VetoableChangeListener getChildVetoableChangeListener(Object child)
      Returns the given child casted to VetoableChangeListener, 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

      protected static final Visibility getChildVisibility(Object child)
      Returns the given child casted to Visibility, 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

      public Locale getLocale()
      Returns the locale of this context.
      Returns:
      the locale of this context
    • getResource

      public URL getResource(String resourceName, BeanContextChild child)
      Specified by:
      getResource in interface BeanContext
    • getResourceAsStream

      public InputStream getResourceAsStream(String resourceName, BeanContextChild child) throws IllegalArgumentException
      Specified by:
      getResourceAsStream in interface BeanContext
      Throws:
      IllegalArgumentException
    • initialize

      protected void initialize()
      Initializes all transient fields of this instance, called by constructors and readObject().
    • instantiateChild

      public Object instantiateChild(String beanName) throws IOException, ClassNotFoundException
      Specified by:
      instantiateChild in interface BeanContext
      Throws:
      IOException
      ClassNotFoundException
    • isDesignTime

      public boolean isDesignTime()
      Specified by:
      isDesignTime in interface DesignMode
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection
    • 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

      public Iterator iterator()
      Returns an iterator of children of this context, with remove() disabled.
      Specified by:
      iterator in interface Collection
      Specified by:
      iterator in interface Iterable
      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 their needsGui() returns true, and if any of the children extends java.awt.Component.

      Specified by:
      needsGui in interface Visibility
      See Also:
    • okToUseGui

      public void okToUseGui()
      Specified by:
      okToUseGui in interface Visibility
    • propertyChange

      public void propertyChange(PropertyChangeEvent event)
      Specified by:
      propertyChange in interface PropertyChangeListener
    • readChildren

      public final void readChildren(ObjectInputStream ois) throws IOException, ClassNotFoundException
      Deserializes children from the given object input stream.

      The implementation reads pairs of child object and BCSChild object according to serializable property. For each pair, it is added to the children map and the childDeserializedHook() is called. If the child implements BeanContextChild, its setBeanContext() is also called.

      This method is called by readObject() if the context works standalone. Or if this support object is a delegate of another BeanContext 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 occurs
      ClassNotFoundException - if class of read object is not found
    • remove

      public boolean remove(Object child)
      Removes the given child from this context.

      Delegates to remove(child, true).

      Specified by:
      remove in interface Collection
      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 null
      IllegalStateException - if the child is not valid to remove
      See Also:
    • remove

      protected boolean remove(Object child, boolean setChildBC)
      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's beanContext 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 context
      setChildBC - whether to call setBeanContext() 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 null
      IllegalStateException - if the child is not valid to remove
    • removeAll

      public boolean removeAll(Collection collection)
      This method is unsupported, throws UnsupportedOperationException.
      Specified by:
      removeAll in interface Collection
      See Also:
    • removeBeanContextMembershipListener

      public void removeBeanContextMembershipListener(BeanContextMembershipListener listener)
      Specified by:
      removeBeanContextMembershipListener in interface BeanContext
    • retainAll

      public boolean retainAll(Collection collection)
      This method is unsupported, throws UnsupportedOperationException.
      Specified by:
      retainAll in interface Collection
      See Also:
    • serialize

      protected final void serialize(ObjectOutputStream oos, Collection collection) throws IOException
      Serializes the given collection.

      First writes a int indicating the number of all serializable elements (implements Serializable, then objects are writtern one by one.

      Parameters:
      oos - the stream where the collection is writtern to
      collection - the collection to serialize
      Throws:
      IOException - if I/O exception occurs
    • setDesignTime

      public void setDesignTime(boolean designTime)
      Specified by:
      setDesignTime in interface DesignMode
    • setLocale

      public void setLocale(Locale newLocale) throws PropertyVetoException
      Sets the locale of this context. VetoableChangeListeners and PropertyChangeListeners are notified.
      Parameters:
      newLocale - the new locale to set
      Throws:
      PropertyVetoException - if any VetoableChangeListener vetos this change
    • size

      public int size()
      Returns the number children of this context.
      Specified by:
      size in interface Collection
      Returns:
      the number children of this context
      See Also:
    • toArray

      public Object[] toArray()
      Returns an array of children of this context.
      Specified by:
      toArray in interface Collection
      Returns:
      an array of children of this context
      See Also:
    • toArray

      public Object[] toArray(Object[] array)
      Returns an array of children of this context.
      Specified by:
      toArray in interface Collection
      Returns:
      an array of children of this context
      See Also:
    • validatePendingAdd

      protected boolean validatePendingAdd(Object child)
      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

      protected boolean validatePendingRemove(Object child)
      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

      public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException
      Specified by:
      vetoableChange in interface VetoableChangeListener
      Throws:
      PropertyVetoException
    • writeChildren

      public final void writeChildren(ObjectOutputStream oos) throws IOException
      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 (implements Serialization.

      This method is called by writeObject() if the context works standalone. Or if this support object is a delegate of another BeanContext 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