Method from java.beans.beancontext.BeanContextSupport Detail: |
public boolean add(Object targetChild) {
if (targetChild == null) throw new IllegalArgumentException();
// The specification requires that we do nothing if the child
// is already nested herein.
if (children.containsKey(targetChild)) return false; // test before locking
synchronized(BeanContext.globalHierarchyLock) {
if (children.containsKey(targetChild)) return false; // check again
if (!validatePendingAdd(targetChild)) {
throw new IllegalStateException();
}
// The specification requires that we invoke setBeanContext() on the
// newly added child if it implements the java.beans.beancontext.BeanContextChild interface
BeanContextChild cbcc = getChildBeanContextChild(targetChild);
BeanContextChild bccp = null;
synchronized(targetChild) {
if (targetChild instanceof BeanContextProxy) {
bccp = ((BeanContextProxy)targetChild).getBeanContextProxy();
if (bccp == null) throw new NullPointerException("BeanContextPeer.getBeanContextProxy()");
}
BCSChild bcsc = createBCSChild(targetChild, bccp);
BCSChild pbcsc = null;
synchronized (children) {
children.put(targetChild, bcsc);
if (bccp != null) children.put(bccp, pbcsc = createBCSChild(bccp, targetChild));
}
if (cbcc != null) synchronized(cbcc) {
try {
cbcc.setBeanContext(getBeanContextPeer());
} catch (PropertyVetoException pve) {
synchronized (children) {
children.remove(targetChild);
if (bccp != null) children.remove(bccp);
}
throw new IllegalStateException();
}
cbcc.addPropertyChangeListener("beanContext", childPCL);
cbcc.addVetoableChangeListener("beanContext", childVCL);
}
Visibility v = getChildVisibility(targetChild);
if (v != null) {
if (okToUseGui)
v.okToUseGui();
else
v.dontUseGui();
}
if (getChildSerializable(targetChild) != null) serializable++;
childJustAddedHook(targetChild, bcsc);
if (bccp != null) {
v = getChildVisibility(bccp);
if (v != null) {
if (okToUseGui)
v.okToUseGui();
else
v.dontUseGui();
}
if (getChildSerializable(bccp) != null) serializable++;
childJustAddedHook(bccp, pbcsc);
}
}
// The specification requires that we fire a notification of the change
fireChildrenAdded(new BeanContextMembershipEvent(getBeanContextPeer(), bccp == null ? new Object[] { targetChild } : new Object[] { targetChild, bccp } ));
}
return true;
}
Adds/nests a child within this BeanContext.
Invoked as a side effect of java.beans.Beans.instantiate().
If the child object is not valid for adding then this method
throws an IllegalStateException.
|
public boolean addAll(Collection c) {
throw new UnsupportedOperationException();
}
add Collection to set of Children (Unsupported)
implementations must synchronized on the hierarchy lock and "children" protected field |
public void addBeanContextMembershipListener(BeanContextMembershipListener bcml) {
if (bcml == null) throw new NullPointerException("listener");
synchronized(bcmListeners) {
if (bcmListeners.contains(bcml))
return;
else
bcmListeners.add(bcml);
}
}
Adds a BeanContextMembershipListener |
public boolean avoidingGui() {
return !okToUseGui && needsGui();
}
Used to determine if the BeanContext
child is avoiding using its GUI. |
protected Iterator bcsChildren() {
synchronized(children) { return children.values().iterator(); }
}
Returns an iterator of all children
of this BeanContext. |
protected void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException, ClassNotFoundException {
}
called by readObject after defaultReadObject() but prior to
deserialization of any children.
This method may be overridden by subclasses to perform custom
deserialization of their state prior to this superclass deserializing
the children.
This method should not however be used by subclasses to replace their
own implementation (if any) of readObject(). |
protected void bcsPreSerializationHook(ObjectOutputStream oos) throws IOException {
}
called by writeObject after defaultWriteObject() but prior to
serialization of currently serializable children.
This method may be overridden by subclasses to perform custom
serialization of their state prior to this superclass serializing
the children.
This method should not however be used by subclasses to replace their
own implementation (if any) of writeObject(). |
protected void childDeserializedHook(Object child,
BCSChild bcsc) {
synchronized(children) {
children.put(child, bcsc);
}
}
Called by readObject with the newly deserialized child and BCSChild. |
protected void childJustAddedHook(Object child,
BCSChild bcsc) {
}
subclasses may override this method to simply extend add() semantics
after the child has been added and before the event notification has
occurred. The method is called with the child synchronized. |
protected void childJustRemovedHook(Object child,
BCSChild bcsc) {
}
subclasses may override this method to simply extend remove() semantics
after the child has been removed and before the event notification has
occurred. The method is called with the child synchronized. |
protected static final boolean classEquals(Class first,
Class second) {
return first.equals(second) || first.getName().equals(second.getName());
}
Tests to see if two class objects,
or their names are equal. |
public void clear() {
throw new UnsupportedOperationException();
}
clear the children (Unsupported)
implementations must synchronized on the hierarchy lock and "children" protected field |
public boolean contains(Object o) {
synchronized(children) {
return children.containsKey(o);
}
}
Determines whether or not the specified object
is currently a child of this BeanContext. |
public boolean containsAll(Collection c) {
synchronized(children) {
Iterator i = c.iterator();
while (i.hasNext())
if(!contains(i.next()))
return false;
return true;
}
}
Tests to see if all objects in the
specified Collection are children of
this BeanContext. |
public boolean containsKey(Object o) {
synchronized(children) {
return children.containsKey(o);
}
}
Determines whether or not the specified object
is currently a child of this BeanContext. |
protected final Object[] copyChildren() {
synchronized(children) { return children.keySet().toArray(); }
}
Gets a copy of the this BeanContext's children. |
protected BCSChild createBCSChild(Object targetChild,
Object peer) {
return new BCSChild(targetChild, peer);
}
Subclasses can override this method to insert their own subclass
of Child without having to override add() or the other Collection
methods that add children to the set.
|
protected final void deserialize(ObjectInputStream ois,
Collection coll) throws IOException, ClassNotFoundException {
int count = 0;
count = ois.readInt();
while (count-- > 0) {
coll.add(ois.readObject());
}
}
used by readObject to deserialize a collection. |
public synchronized void dontUseGui() {
if (okToUseGui) {
okToUseGui = false;
// lets also tell the Children that can that they may not use their GUI's
synchronized(children) {
for (Iterator i = children.keySet().iterator(); i.hasNext();) {
Visibility v = getChildVisibility(i.next());
if (v != null) v.dontUseGui();
}
}
}
}
notify this instance that it may no longer render a GUI. |
protected final void fireChildrenAdded(BeanContextMembershipEvent bcme) {
Object[] copy;
synchronized(bcmListeners) { copy = bcmListeners.toArray(); }
for (int i = 0; i < copy.length; i++)
((BeanContextMembershipListener)copy[i]).childrenAdded(bcme);
}
Fire a BeanContextshipEvent on the BeanContextMembershipListener interface |
protected final void fireChildrenRemoved(BeanContextMembershipEvent bcme) {
Object[] copy;
synchronized(bcmListeners) { copy = bcmListeners.toArray(); }
for (int i = 0; i < copy.length; i++)
((BeanContextMembershipListener)copy[i]).childrenRemoved(bcme);
}
Fire a BeanContextshipEvent on the BeanContextMembershipListener interface |
public BeanContext getBeanContextPeer() {
return (BeanContext)getBeanContextChildPeer();
}
Gets the instance of BeanContext that
this object is providing the implementation for. |
protected static final BeanContextChild getChildBeanContextChild(Object child) {
try {
BeanContextChild bcc = (BeanContextChild)child;
if (child instanceof BeanContextChild && child instanceof BeanContextProxy)
throw new IllegalArgumentException("child cannot implement both BeanContextChild and BeanContextProxy");
else
return bcc;
} catch (ClassCastException cce) {
try {
return ((BeanContextProxy)child).getBeanContextProxy();
} catch (ClassCastException cce1) {
return null;
}
}
}
Gets the BeanContextChild (if any) of the specified child |
protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(Object child) {
try {
return (BeanContextMembershipListener)child;
} catch (ClassCastException cce) {
return null;
}
}
Gets the BeanContextMembershipListener
(if any) of the specified child |
protected static final PropertyChangeListener getChildPropertyChangeListener(Object child) {
try {
return (PropertyChangeListener)child;
} catch (ClassCastException cce) {
return null;
}
}
Gets the PropertyChangeListener
(if any) of the specified child |
protected static final Serializable getChildSerializable(Object child) {
try {
return (Serializable)child;
} catch (ClassCastException cce) {
return null;
}
}
Gets the Serializable (if any) associated with the specified Child |
protected static final VetoableChangeListener getChildVetoableChangeListener(Object child) {
try {
return (VetoableChangeListener)child;
} catch (ClassCastException cce) {
return null;
}
}
Gets the VetoableChangeListener
(if any) of the specified child |
protected static final Visibility getChildVisibility(Object child) {
try {
return (Visibility)child;
} catch (ClassCastException cce) {
return null;
}
}
Gets the Component (if any) associated with the specified child. |
public synchronized Locale getLocale() {
return locale;
}
Gets the locale for this BeanContext. |
public URL getResource(String name,
BeanContextChild bcc) {
if (name == null) throw new NullPointerException("name");
if (bcc == null) throw new NullPointerException("bcc");
if (containsKey(bcc)) {
ClassLoader cl = bcc.getClass().getClassLoader();
return cl != null ? cl.getResource(name)
: ClassLoader.getSystemResource(name);
} else throw new IllegalArgumentException("Not a valid child");
}
|
public InputStream getResourceAsStream(String name,
BeanContextChild bcc) {
if (name == null) throw new NullPointerException("name");
if (bcc == null) throw new NullPointerException("bcc");
if (containsKey(bcc)) {
ClassLoader cl = bcc.getClass().getClassLoader();
return cl != null ? cl.getResourceAsStream(name)
: ClassLoader.getSystemResourceAsStream(name);
} else throw new IllegalArgumentException("Not a valid child");
}
|
protected synchronized void initialize() {
children = new HashMap(serializable + 1);
bcmListeners = new ArrayList(1);
childPCL = new PropertyChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* property changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
childVCL = new VetoableChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* vetoable changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
BeanContextSupport.this.vetoableChange(pce);
}
};
}
protected method called from constructor and readObject to initialize
transient state of BeanContextSupport instance.
This class uses this method to instantiate inner class listeners used
to monitor PropertyChange and VetoableChange events on children.
subclasses may envelope this method to add their own initialization
behavior |
public Object instantiateChild(String beanName) throws IOException, ClassNotFoundException {
BeanContext bc = getBeanContextPeer();
return Beans.instantiate(bc.getClass().getClassLoader(), beanName, bc);
}
The instantiateChild method is a convenience hook
in BeanContext to simplify
the task of instantiating a Bean, nested,
into a BeanContext.
The semantics of the beanName parameter are defined by java.beans.Beans.instantate.
|
public synchronized boolean isDesignTime() {
return designTime;
}
Reports whether or not this object is in
currently in design time mode. |
public boolean isEmpty() {
synchronized(children) {
return children.isEmpty();
}
}
Reports whether or not this
BeanContext is empty.
A BeanContext is considered
empty when it contains zero
nested children. |
public boolean isSerializing() {
return serializing;
}
Is this BeanContext in the
process of being serialized? |
public Iterator iterator() {
synchronized(children) {
return new BCSIterator(children.keySet().iterator());
}
}
Gets all JavaBean or BeanContext instances
currently nested in this BeanContext. |
public synchronized boolean needsGui() {
BeanContext bc = getBeanContextPeer();
if (bc != this) {
if (bc instanceof Visibility) return ((Visibility)bc).needsGui();
if (bc instanceof Container || bc instanceof Component)
return true;
}
synchronized(children) {
for (Iterator i = children.keySet().iterator(); i.hasNext();) {
Object c = i.next();
try {
return ((Visibility)c).needsGui();
} catch (ClassCastException cce) {
// do nothing ...
}
if (c instanceof Container || c instanceof Component)
return true;
}
}
return false;
}
This method is typically called from the environment in order to determine
if the implementor "needs" a GUI.
The algorithm used herein tests the BeanContextPeer, and its current children
to determine if they are either Containers, Components, or if they implement
Visibility and return needsGui() == true.
|
public synchronized void okToUseGui() {
if (!okToUseGui) {
okToUseGui = true;
// lets also tell the Children that can that they may use their GUI's
synchronized(children) {
for (Iterator i = children.keySet().iterator(); i.hasNext();) {
Visibility v = getChildVisibility(i.next());
if (v != null) v.okToUseGui();
}
}
}
}
Notify this instance that it may now render a GUI |
public void propertyChange(PropertyChangeEvent pce) {
String propertyName = pce.getPropertyName();
Object source = pce.getSource();
synchronized(children) {
if ("beanContext".equals(propertyName) &&
containsKey(source) &&
((BCSChild)children.get(source)).isRemovePending()) {
BeanContext bc = getBeanContextPeer();
if (bc.equals(pce.getOldValue()) && !bc.equals(pce.getNewValue())) {
remove(source, false);
} else {
((BCSChild)children.get(source)).setRemovePending(false);
}
}
}
}
subclasses may envelope to monitor child property changes. |
public final void readChildren(ObjectInputStream ois) throws IOException, ClassNotFoundException {
int count = serializable;
while (count-- > 0) {
Object child = null;
BeanContextSupport.BCSChild bscc = null;
try {
child = ois.readObject();
bscc = (BeanContextSupport.BCSChild)ois.readObject();
} catch (IOException ioe) {
continue;
} catch (ClassNotFoundException cnfe) {
continue;
}
synchronized(child) {
BeanContextChild bcc = null;
try {
bcc = (BeanContextChild)child;
} catch (ClassCastException cce) {
// do nothing;
}
if (bcc != null) {
try {
bcc.setBeanContext(getBeanContextPeer());
bcc.addPropertyChangeListener("beanContext", childPCL);
bcc.addVetoableChangeListener("beanContext", childVCL);
} catch (PropertyVetoException pve) {
continue;
}
}
childDeserializedHook(child, bscc);
}
}
}
When an instance of this class is used as a delegate for the
implementation of the BeanContext protocols (and its subprotocols)
there exists a 'chicken and egg' problem during deserialization |
public boolean remove(Object targetChild) {
return remove(targetChild, true);
}
Removes a child from this BeanContext. If the child object is not
for adding then this method throws an IllegalStateException. |
protected boolean remove(Object targetChild,
boolean callChildSetBC) {
if (targetChild == null) throw new IllegalArgumentException();
synchronized(BeanContext.globalHierarchyLock) {
if (!containsKey(targetChild)) return false;
if (!validatePendingRemove(targetChild)) {
throw new IllegalStateException();
}
BCSChild bcsc = (BCSChild)children.get(targetChild);
BCSChild pbcsc = null;
Object peer = null;
// we are required to notify the child that it is no longer nested here if
// it implements java.beans.beancontext.BeanContextChild
synchronized(targetChild) {
if (callChildSetBC) {
BeanContextChild cbcc = getChildBeanContextChild(targetChild);
if (cbcc != null) synchronized(cbcc) {
cbcc.removePropertyChangeListener("beanContext", childPCL);
cbcc.removeVetoableChangeListener("beanContext", childVCL);
try {
cbcc.setBeanContext(null);
} catch (PropertyVetoException pve1) {
cbcc.addPropertyChangeListener("beanContext", childPCL);
cbcc.addVetoableChangeListener("beanContext", childVCL);
throw new IllegalStateException();
}
}
}
synchronized (children) {
children.remove(targetChild);
if (bcsc.isProxyPeer()) {
pbcsc = (BCSChild)children.get(peer = bcsc.getProxyPeer());
children.remove(peer);
}
}
if (getChildSerializable(targetChild) != null) serializable--;
childJustRemovedHook(targetChild, bcsc);
if (peer != null) {
if (getChildSerializable(peer) != null) serializable--;
childJustRemovedHook(peer, pbcsc);
}
}
fireChildrenRemoved(new BeanContextMembershipEvent(getBeanContextPeer(), peer == null ? new Object[] { targetChild } : new Object[] { targetChild, peer } ));
}
return true;
}
internal remove used when removal caused by
unexpected setBeanContext or
by remove() invocation. |
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException();
}
remove all specified children (Unsupported)
implementations must synchronized on the hierarchy lock and "children" protected field |
public void removeBeanContextMembershipListener(BeanContextMembershipListener bcml) {
if (bcml == null) throw new NullPointerException("listener");
synchronized(bcmListeners) {
if (!bcmListeners.contains(bcml))
return;
else
bcmListeners.remove(bcml);
}
}
Removes a BeanContextMembershipListener |
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException();
}
retain only specified children (Unsupported)
implementations must synchronized on the hierarchy lock and "children" protected field |
protected final void serialize(ObjectOutputStream oos,
Collection coll) throws IOException {
int count = 0;
Object[] objects = coll.toArray();
for (int i = 0; i < objects.length; i++) {
if (objects[i] instanceof Serializable)
count++;
else
objects[i] = null;
}
oos.writeInt(count); // number of subsequent objects
for (int i = 0; count > 0; i++) {
Object o = objects[i];
if (o != null) {
oos.writeObject(o);
count--;
}
}
}
Used by writeObject to serialize a Collection. |
public synchronized void setDesignTime(boolean dTime) {
if (designTime != dTime) {
designTime = dTime;
firePropertyChange("designMode", Boolean.valueOf(!dTime), Boolean.valueOf(dTime));
}
}
Sets the new design time value for this BeanContext. |
public synchronized void setLocale(Locale newLocale) throws PropertyVetoException {
if ((locale != null && !locale.equals(newLocale)) && newLocale != null) {
Locale old = locale;
fireVetoableChange("locale", old, newLocale); // throws
locale = newLocale;
firePropertyChange("locale", old, newLocale);
}
}
Sets the locale of this BeanContext. |
public int size() {
synchronized(children) {
return children.size();
}
}
Gets the number of children currently nested in
this BeanContext. |
public Object[] toArray() {
synchronized(children) {
return children.keySet().toArray();
}
}
Gets all JavaBean or BeanContext
instances currently nested in this BeanContext. |
public Object[] toArray(Object[] arry) {
synchronized(children) {
return children.keySet().toArray(arry);
}
}
Gets an array containing all children of
this BeanContext that match
the types contained in arry. |
protected boolean validatePendingAdd(Object targetChild) {
return true;
}
Subclasses of this class may override, or envelope, this method to
add validation behavior for the BeanContext to examine child objects
immediately prior to their being added to the BeanContext.
|
protected boolean validatePendingRemove(Object targetChild) {
return true;
}
Subclasses of this class may override, or envelope, this method to
add validation behavior for the BeanContext to examine child objects
immediately prior to their being removed from the BeanContext.
|
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
String propertyName = pce.getPropertyName();
Object source = pce.getSource();
synchronized(children) {
if ("beanContext".equals(propertyName) &&
containsKey(source) &&
!getBeanContextPeer().equals(pce.getNewValue())
) {
if (!validatePendingRemove(source)) {
throw new PropertyVetoException("current BeanContext vetoes setBeanContext()", pce);
} else ((BCSChild)children.get(source)).setRemovePending(true);
}
}
}
subclasses may envelope to monitor veto child property changes. |
public final void writeChildren(ObjectOutputStream oos) throws IOException {
if (serializable < = 0) return;
boolean prev = serializing;
serializing = true;
int count = 0;
synchronized(children) {
Iterator i = children.entrySet().iterator();
while (i.hasNext() && count < serializable) {
Map.Entry entry = (Map.Entry)i.next();
if (entry.getKey() instanceof Serializable) {
try {
oos.writeObject(entry.getKey()); // child
oos.writeObject(entry.getValue()); // BCSChild
} catch (IOException ioe) {
serializing = prev;
throw ioe;
}
count++;
}
}
}
serializing = prev;
if (count != serializable) {
throw new IOException("wrote different number of children than expected");
}
}
Used to serialize all children of
this BeanContext. |