jdk/src/share/classes/java/beans/beancontext/BeanContextSupport.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans.beancontext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.Beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.AppletInitializer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.DesignMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.beans.PropertyChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.beans.VetoableChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.beans.VetoableChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.beans.PropertyVetoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.beans.Visibility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * This helper class provides a utility implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * java.beans.beancontext.BeanContext interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Since this class directly implements the BeanContext interface, the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * can, and is intended to be used either by subclassing this implementation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * or via ad-hoc delegation of an instance of this class from another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author Laurence P. G. Cable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class      BeanContextSupport extends BeanContextChildSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
       implements BeanContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                  Serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                  PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                  VetoableChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Fix for bug 4282900 to pass JCK regression test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final long serialVersionUID = -4879613978649577204L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Construct a BeanContextSupport instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param peer      The peer <tt>BeanContext</tt> we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *                  supplying an implementation for,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *                  or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *                  if this object is its own peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param lcle      The current Locale for this BeanContext. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *                  <tt>lcle</tt> is <tt>null</tt>, the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *                  is assigned to the <tt>BeanContext</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param dTime     The initial state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *                  <tt>true</tt> if in design mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *                  <tt>false</tt> if runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param visible   The initial visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @see java.util.Locale#getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see java.util.Locale#setDefault(java.util.Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public BeanContextSupport(BeanContext peer, Locale lcle, boolean dTime, boolean visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super(peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        locale          = lcle != null ? lcle : Locale.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        designTime      = dTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        okToUseGui      = visible;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Create an instance using the specified Locale and design mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param peer      The peer <tt>BeanContext</tt> we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *                  are supplying an implementation for,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *                  or <tt>null</tt> if this object is its own peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param lcle      The current Locale for this <tt>BeanContext</tt>. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *                  <tt>lcle</tt> is <tt>null</tt>, the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *                  is assigned to the <tt>BeanContext</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param dtime     The initial state, <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                  if in design mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *                  <tt>false</tt> if runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see java.util.Locale#getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see java.util.Locale#setDefault(java.util.Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public BeanContextSupport(BeanContext peer, Locale lcle, boolean dtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this (peer, lcle, dtime, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Create an instance using the specified locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param peer      The peer BeanContext we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *                  supplying an implementation for,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                  or <tt>null</tt> if this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *                  is its own peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param lcle      The current Locale for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *                  <tt>BeanContext</tt>. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *                  <tt>lcle</tt> is <tt>null</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *                  the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *                  is assigned to the <tt>BeanContext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *                  instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see java.util.Locale#getDefault()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see java.util.Locale#setDefault(java.util.Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public BeanContextSupport(BeanContext peer, Locale lcle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this (peer, lcle, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Create an instance using with a default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param peer      The peer <tt>BeanContext</tt> we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *                  supplying an implementation for,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *                  or <tt>null</tt> if this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *                  is its own peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public BeanContextSupport(BeanContext peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this (peer, null, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Create an instance that is not a delegate of another object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public BeanContextSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this (null, null, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Gets the instance of <tt>BeanContext</tt> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * this object is providing the implementation for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return the BeanContext instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public BeanContext getBeanContextPeer() { return (BeanContext)getBeanContextChildPeer(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The instantiateChild method is a convenience hook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * in BeanContext to simplify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the task of instantiating a Bean, nested,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * into a <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * The semantics of the beanName parameter are defined by java.beans.Beans.instantate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param beanName the name of the Bean to instantiate within this BeanContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws IOException if there is an I/O error when the bean is being deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws ClassNotFoundException if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * identified by the beanName parameter is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the new object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Object instantiateChild(String beanName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
           throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        BeanContext bc = getBeanContextPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return Beans.instantiate(bc.getClass().getClassLoader(), beanName, bc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Gets the number of children currently nested in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * this BeanContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return number of children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return children.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Reports whether or not this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <tt>BeanContext</tt> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * A <tt>BeanContext</tt> is considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * empty when it contains zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * nested children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return if there are not children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            return children.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Determines whether or not the specified object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * is currently a child of this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param o the Object in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return if this object is a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return children.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Determines whether or not the specified object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * is currently a child of this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param o the Object in question
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return if this object is a child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public boolean containsKey(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return children.containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Gets all JavaBean or <tt>BeanContext</tt> instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * currently nested in this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return an <tt>Iterator</tt> of the nested children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public Iterator iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return new BCSIterator(children.keySet().iterator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Gets all JavaBean or <tt>BeanContext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * instances currently nested in this BeanContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return children.keySet().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Gets an array containing all children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * this <tt>BeanContext</tt> that match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the types contained in arry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param arry The array of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * types that are of interest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return an array of children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public Object[] toArray(Object[] arry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return children.keySet().toArray(arry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * protected final subclass that encapsulates an iterator but implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * a noop remove() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    protected static final class BCSIterator implements Iterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        BCSIterator(Iterator i) { super(); src = i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        public boolean hasNext() { return src.hasNext(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        public Object  next()    { return src.next();    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        public void    remove()  { /* do nothing */      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        private Iterator src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * protected nested class containing per child information, an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * of which is associated with each child in the "children" hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * subclasses can extend this class to include their own per-child state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Note that this 'value' is serialized with the corresponding child 'key'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * when the BeanContextSupport is serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    protected class BCSChild implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static final long serialVersionUID = -5815286101609939109L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        BCSChild(Object bcc, Object peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            child     = bcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            proxyPeer = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Object  getChild()                  { return child; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        void    setRemovePending(boolean v) { removePending = v; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        boolean isRemovePending()           { return removePending; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        boolean isProxyPeer()               { return proxyPeer != null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        Object  getProxyPeer()              { return proxyPeer; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        private           Object   child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        private           Object   proxyPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        private transient boolean  removePending;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Subclasses can override this method to insert their own subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * of Child without having to override add() or the other Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * methods that add children to the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param targetChild the child to create the Child on behalf of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param peer        the peer if the tragetChild and the peer are related by an implementation of BeanContextProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    protected BCSChild createBCSChild(Object targetChild, Object peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return new BCSChild(targetChild, peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Adds/nests a child within this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Invoked as a side effect of java.beans.Beans.instantiate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * If the child object is not valid for adding then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * throws an IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param targetChild The child objects to nest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * within this <tt>BeanContext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return true if the child was added successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @see #validatePendingAdd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public boolean add(Object targetChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (targetChild == null) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // The specification requires that we do nothing if the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // is already nested herein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (children.containsKey(targetChild)) return false; // test before locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        synchronized(BeanContext.globalHierarchyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (children.containsKey(targetChild)) return false; // check again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (!validatePendingAdd(targetChild)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // The specification requires that we invoke setBeanContext() on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // newly added child if it implements the java.beans.beancontext.BeanContextChild interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            BeanContextChild cbcc  = getChildBeanContextChild(targetChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            BeanContextChild  bccp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            synchronized(targetChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (targetChild instanceof BeanContextProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    bccp = ((BeanContextProxy)targetChild).getBeanContextProxy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    if (bccp == null) throw new NullPointerException("BeanContextPeer.getBeanContextProxy()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                BCSChild bcsc  = createBCSChild(targetChild, bccp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                BCSChild pbcsc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                synchronized (children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    children.put(targetChild, bcsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (bccp != null) children.put(bccp, pbcsc = createBCSChild(bccp, targetChild));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (cbcc != null) synchronized(cbcc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        cbcc.setBeanContext(getBeanContextPeer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    } catch (PropertyVetoException pve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        synchronized (children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            children.remove(targetChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            if (bccp != null) children.remove(bccp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    cbcc.addPropertyChangeListener("beanContext", childPCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    cbcc.addVetoableChangeListener("beanContext", childVCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                Visibility v = getChildVisibility(targetChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    if (okToUseGui)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        v.okToUseGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        v.dontUseGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (getChildSerializable(targetChild) != null) serializable++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                childJustAddedHook(targetChild, bcsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (bccp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    v = getChildVisibility(bccp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        if (okToUseGui)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                            v.okToUseGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            v.dontUseGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    if (getChildSerializable(bccp) != null) serializable++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    childJustAddedHook(bccp, pbcsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            // The specification requires that we fire a notification of the change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            fireChildrenAdded(new BeanContextMembershipEvent(getBeanContextPeer(), bccp == null ? new Object[] { targetChild } : new Object[] { targetChild, bccp } ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Removes a child from this BeanContext.  If the child object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * for adding then this method throws an IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param targetChild The child objects to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see #validatePendingRemove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public boolean remove(Object targetChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return remove(targetChild, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * internal remove used when removal caused by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * unexpected <tt>setBeanContext</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * by <tt>remove()</tt> invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param targetChild the JavaBean, BeanContext, or Object to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param callChildSetBC used to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * the child should be notified that it is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * longer nested in this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    protected boolean remove(Object targetChild, boolean callChildSetBC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (targetChild == null) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        synchronized(BeanContext.globalHierarchyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (!containsKey(targetChild)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (!validatePendingRemove(targetChild)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            BCSChild bcsc  = (BCSChild)children.get(targetChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            BCSChild pbcsc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            Object   peer  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            // we are required to notify the child that it is no longer nested here if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // it implements java.beans.beancontext.BeanContextChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            synchronized(targetChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                if (callChildSetBC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    BeanContextChild cbcc = getChildBeanContextChild(targetChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    if (cbcc != null) synchronized(cbcc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        cbcc.removePropertyChangeListener("beanContext", childPCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        cbcc.removeVetoableChangeListener("beanContext", childVCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                            cbcc.setBeanContext(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        } catch (PropertyVetoException pve1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                            cbcc.addPropertyChangeListener("beanContext", childPCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                            cbcc.addVetoableChangeListener("beanContext", childVCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                synchronized (children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    children.remove(targetChild);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    if (bcsc.isProxyPeer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        pbcsc = (BCSChild)children.get(peer = bcsc.getProxyPeer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        children.remove(peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if (getChildSerializable(targetChild) != null) serializable--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                childJustRemovedHook(targetChild, bcsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    if (getChildSerializable(peer) != null) serializable--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    childJustRemovedHook(peer, pbcsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            fireChildrenRemoved(new BeanContextMembershipEvent(getBeanContextPeer(), peer == null ? new Object[] { targetChild } : new Object[] { targetChild, peer } ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Tests to see if all objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * specified <tt>Collection</tt> are children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param c the specified <tt>Collection</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return <tt>true</tt> if all objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * in the collection are children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * this <tt>BeanContext</tt>, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public boolean containsAll(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            Iterator i = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            while (i.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                if(!contains(i.next()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * add Collection to set of Children (Unsupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * implementations must synchronized on the hierarchy lock and "children" protected field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @throws UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public boolean addAll(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * remove all specified children (Unsupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * implementations must synchronized on the hierarchy lock and "children" protected field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @throws UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public boolean removeAll(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * retain only specified children (Unsupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * implementations must synchronized on the hierarchy lock and "children" protected field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @throws UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public boolean retainAll(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * clear the children (Unsupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * implementations must synchronized on the hierarchy lock and "children" protected field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @throws UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Adds a BeanContextMembershipListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param  bcml the BeanContextMembershipListener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public void addBeanContextMembershipListener(BeanContextMembershipListener bcml) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (bcml == null) throw new NullPointerException("listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        synchronized(bcmListeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (bcmListeners.contains(bcml))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                bcmListeners.add(bcml);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Removes a BeanContextMembershipListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param  bcml the BeanContextMembershipListener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @throws NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    public void removeBeanContextMembershipListener(BeanContextMembershipListener bcml) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (bcml == null) throw new NullPointerException("listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        synchronized(bcmListeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (!bcmListeners.contains(bcml))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                bcmListeners.remove(bcml);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @param name the name of the resource requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param bcc  the child object making the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @return  the requested resource as an InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public InputStream getResourceAsStream(String name, BeanContextChild bcc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (name == null) throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (bcc  == null) throw new NullPointerException("bcc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (containsKey(bcc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            ClassLoader cl = bcc.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return cl != null ? cl.getResourceAsStream(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                              : ClassLoader.getSystemResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        } else throw new IllegalArgumentException("Not a valid child");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param name the name of the resource requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @param bcc  the child object making the request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @return the requested resource as an InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public URL getResource(String name, BeanContextChild bcc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (name == null) throw new NullPointerException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (bcc  == null) throw new NullPointerException("bcc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (containsKey(bcc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            ClassLoader cl = bcc.getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return cl != null ? cl.getResource(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                              : ClassLoader.getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        } else throw new IllegalArgumentException("Not a valid child");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Sets the new design time value for this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param dTime the new designTime value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public synchronized void setDesignTime(boolean dTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (designTime != dTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            designTime = dTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            firePropertyChange("designMode", Boolean.valueOf(!dTime), Boolean.valueOf(dTime));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Reports whether or not this object is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * currently in design time mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @return <tt>true</tt> if in design time mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <tt>false</tt> if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public synchronized boolean isDesignTime() { return designTime; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Sets the locale of this BeanContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @param newLocale the new locale. This method call will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *        no effect if newLocale is <CODE>null</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @throws PropertyVetoException if the new value is rejected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public synchronized void setLocale(Locale newLocale) throws PropertyVetoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if ((locale != null && !locale.equals(newLocale)) && newLocale != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            Locale old = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            fireVetoableChange("locale", old, newLocale); // throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            locale = newLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            firePropertyChange("locale", old, newLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Gets the locale for this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @return the current Locale of the <tt>BeanContext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    public synchronized Locale getLocale() { return locale; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * This method is typically called from the environment in order to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * if the implementor "needs" a GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * The algorithm used herein tests the BeanContextPeer, and its current children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * to determine if they are either Containers, Components, or if they implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Visibility and return needsGui() == true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @return <tt>true</tt> if the implementor needs a GUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    public synchronized boolean needsGui() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        BeanContext bc = getBeanContextPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (bc != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (bc instanceof Visibility) return ((Visibility)bc).needsGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (bc instanceof Container || bc instanceof Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            for (Iterator i = children.keySet().iterator(); i.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                Object c = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        return ((Visibility)c).needsGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                        // do nothing ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    if (c instanceof Container || c instanceof Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * notify this instance that it may no longer render a GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    public synchronized void dontUseGui() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if (okToUseGui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            okToUseGui = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            // lets also tell the Children that can that they may not use their GUI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                for (Iterator i = children.keySet().iterator(); i.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    Visibility v = getChildVisibility(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    if (v != null) v.dontUseGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Notify this instance that it may now render a GUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public synchronized void okToUseGui() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (!okToUseGui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            okToUseGui = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            // lets also tell the Children that can that they may use their GUI's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                for (Iterator i = children.keySet().iterator(); i.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    Visibility v = getChildVisibility(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    if (v != null) v.okToUseGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Used to determine if the <tt>BeanContext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * child is avoiding using its GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @return is this instance avoiding using its GUI?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @see Visibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public boolean avoidingGui() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        return !okToUseGui && needsGui();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * Is this <tt>BeanContext</tt> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * process of being serialized?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @return if this <tt>BeanContext</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * currently being serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    public boolean isSerializing() { return serializing; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Returns an iterator of all children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * of this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @return an iterator for all the current BCSChild values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    protected Iterator bcsChildren() { synchronized(children) { return children.values().iterator();  } }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * called by writeObject after defaultWriteObject() but prior to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * serialization of currently serializable children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * This method may be overridden by subclasses to perform custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * serialization of their state prior to this superclass serializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * This method should not however be used by subclasses to replace their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * own implementation (if any) of writeObject().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    protected void bcsPreSerializationHook(ObjectOutputStream oos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * called by readObject after defaultReadObject() but prior to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * deserialization of any children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * This method may be overridden by subclasses to perform custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * deserialization of their state prior to this superclass deserializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * This method should not however be used by subclasses to replace their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * own implementation (if any) of readObject().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    protected void bcsPreDeserializationHook(ObjectInputStream ois) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Called by readObject with the newly deserialized child and BCSChild.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @param child the newly deserialized child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param bcsc the newly deserialized BCSChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    protected void childDeserializedHook(Object child, BCSChild bcsc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            children.put(child, bcsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Used by writeObject to serialize a Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @param oos the <tt>ObjectOutputStream</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * to use during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param coll the <tt>Collection</tt> to serialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @throws IOException if serialization failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    protected final void serialize(ObjectOutputStream oos, Collection coll) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        int      count   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        Object[] objects = coll.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        for (int i = 0; i < objects.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if (objects[i] instanceof Serializable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                objects[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        oos.writeInt(count); // number of subsequent objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        for (int i = 0; count > 0; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            Object o = objects[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                oos.writeObject(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * used by readObject to deserialize a collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @param ois the ObjectInputStream to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @param coll the Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    protected final void deserialize(ObjectInputStream ois, Collection coll) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        count = ois.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        while (count-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            coll.add(ois.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Used to serialize all children of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * this <tt>BeanContext</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @param oos the <tt>ObjectOutputStream</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * to use during serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @throws IOException if serialization failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    public final void writeChildren(ObjectOutputStream oos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        if (serializable <= 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        boolean prev = serializing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        serializing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            Iterator i = children.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            while (i.hasNext() && count < serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                Map.Entry entry = (Map.Entry)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                if (entry.getKey() instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        oos.writeObject(entry.getKey());   // child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        oos.writeObject(entry.getValue()); // BCSChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                        serializing = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                        throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        serializing = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if (count != serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            throw new IOException("wrote different number of children than expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * Serialize the BeanContextSupport, if this instance has a distinct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * peer (that is this object is acting as a delegate for another) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * the children of this instance are not serialized here due to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * 'chicken and egg' problem that occurs on deserialization of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * children at the same time as this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Therefore in situations where there is a distinct peer to this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * it should always call writeObject() followed by writeChildren() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * readObject() followed by readChildren().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @param oos the ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    private synchronized void writeObject(ObjectOutputStream oos) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        serializing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        synchronized (BeanContext.globalHierarchyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                oos.defaultWriteObject(); // serialize the BeanContextSupport object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                bcsPreSerializationHook(oos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                if (serializable > 0 && this.equals(getBeanContextPeer()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    writeChildren(oos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                serialize(oos, (Collection)bcmListeners);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                serializing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * When an instance of this class is used as a delegate for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * implementation of the BeanContext protocols (and its subprotocols)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * there exists a 'chicken and egg' problem during deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public final void readChildren(ObjectInputStream ois) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        int count = serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        while (count-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            Object                      child = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            BeanContextSupport.BCSChild bscc  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                child = ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                bscc  = (BeanContextSupport.BCSChild)ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            synchronized(child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                BeanContextChild bcc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    bcc = (BeanContextChild)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    // do nothing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                if (bcc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        bcc.setBeanContext(getBeanContextPeer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                       bcc.addPropertyChangeListener("beanContext", childPCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                       bcc.addVetoableChangeListener("beanContext", childVCL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    } catch (PropertyVetoException pve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                childDeserializedHook(child, bscc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * deserialize contents ... if this instance has a distinct peer the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * children are *not* serialized here, the peer's readObject() must call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * readChildren() after deserializing this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    private synchronized void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        synchronized(BeanContext.globalHierarchyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            ois.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            initialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            bcsPreDeserializationHook(ois);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            if (serializable > 0 && this.equals(getBeanContextPeer()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                readChildren(ois);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            deserialize(ois, bcmListeners = new ArrayList(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * subclasses may envelope to monitor veto child property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        String propertyName = pce.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        Object source       = pce.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if ("beanContext".equals(propertyName) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                containsKey(source)                    &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                !getBeanContextPeer().equals(pce.getNewValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                if (!validatePendingRemove(source)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    throw new PropertyVetoException("current BeanContext vetoes setBeanContext()", pce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                } else ((BCSChild)children.get(source)).setRemovePending(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * subclasses may envelope to monitor child property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    public void propertyChange(PropertyChangeEvent pce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        String propertyName = pce.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        Object source       = pce.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        synchronized(children) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            if ("beanContext".equals(propertyName) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                containsKey(source)                    &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                ((BCSChild)children.get(source)).isRemovePending()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                BeanContext bc = getBeanContextPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                if (bc.equals(pce.getOldValue()) && !bc.equals(pce.getNewValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    remove(source, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    ((BCSChild)children.get(source)).setRemovePending(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Subclasses of this class may override, or envelope, this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * add validation behavior for the BeanContext to examine child objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * immediately prior to their being added to the BeanContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @return true iff the child may be added to this BeanContext, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    protected boolean validatePendingAdd(Object targetChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Subclasses of this class may override, or envelope, this method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * add validation behavior for the BeanContext to examine child objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * immediately prior to their being removed from the BeanContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @return true iff the child may be removed from this BeanContext, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    protected boolean validatePendingRemove(Object targetChild) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * subclasses may override this method to simply extend add() semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * after the child has been added and before the event notification has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * occurred. The method is called with the child synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    protected void childJustAddedHook(Object child, BCSChild bcsc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * subclasses may override this method to simply extend remove() semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * after the child has been removed and before the event notification has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * occurred. The method is called with the child synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    protected void childJustRemovedHook(Object child, BCSChild bcsc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * Gets the Component (if any) associated with the specified child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @param child the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @return the Component (if any) associated with the specified child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    protected static final Visibility getChildVisibility(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            return (Visibility)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * Gets the Serializable (if any) associated with the specified Child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * @param child the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * @return the Serializable (if any) associated with the specified Child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    protected static final Serializable getChildSerializable(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return (Serializable)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * Gets the PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @param child the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @return the PropertyChangeListener (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    protected static final PropertyChangeListener getChildPropertyChangeListener(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            return (PropertyChangeListener)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * Gets the VetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * @param child the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return the VetoableChangeListener (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    protected static final VetoableChangeListener getChildVetoableChangeListener(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            return (VetoableChangeListener)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * Gets the BeanContextMembershipListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @param child the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @return the BeanContextMembershipListener (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    protected static final BeanContextMembershipListener getChildBeanContextMembershipListener(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            return (BeanContextMembershipListener)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * Gets the BeanContextChild (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @param child the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @return  the BeanContextChild (if any) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @throws  IllegalArgumentException if child implements both BeanContextChild and BeanContextProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    protected static final BeanContextChild getChildBeanContextChild(Object child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            BeanContextChild bcc = (BeanContextChild)child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            if (child instanceof BeanContextChild && child instanceof BeanContextProxy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                throw new IllegalArgumentException("child cannot implement both BeanContextChild and BeanContextProxy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                return bcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        } catch (ClassCastException cce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                return ((BeanContextProxy)child).getBeanContextProxy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            } catch (ClassCastException cce1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Fire a BeanContextshipEvent on the BeanContextMembershipListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    protected final void fireChildrenAdded(BeanContextMembershipEvent bcme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        Object[] copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        synchronized(bcmListeners) { copy = bcmListeners.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        for (int i = 0; i < copy.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            ((BeanContextMembershipListener)copy[i]).childrenAdded(bcme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * Fire a BeanContextshipEvent on the BeanContextMembershipListener interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    protected final void fireChildrenRemoved(BeanContextMembershipEvent bcme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        Object[] copy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        synchronized(bcmListeners) { copy = bcmListeners.toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        for (int i = 0; i < copy.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            ((BeanContextMembershipListener)copy[i]).childrenRemoved(bcme);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * protected method called from constructor and readObject to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * transient state of BeanContextSupport instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * This class uses this method to instantiate inner class listeners used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * to monitor PropertyChange and VetoableChange events on children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * subclasses may envelope this method to add their own initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    protected synchronized void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        children     = new HashMap(serializable + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        bcmListeners = new ArrayList(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        childPCL = new PropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
             * this adaptor is used by the BeanContextSupport class to forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
             * property changes from a child to the BeanContext, avoiding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
             * accidential serialization of the BeanContext by a badly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
             * behaved Serializable child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            public void propertyChange(PropertyChangeEvent pce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                BeanContextSupport.this.propertyChange(pce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        childVCL = new VetoableChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
             * this adaptor is used by the BeanContextSupport class to forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
             * vetoable changes from a child to the BeanContext, avoiding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
             * accidential serialization of the BeanContext by a badly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
             * behaved Serializable child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                BeanContextSupport.this.vetoableChange(pce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * Gets a copy of the this BeanContext's children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * @return a copy of the current nested children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    protected final Object[] copyChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        synchronized(children) { return children.keySet().toArray(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * Tests to see if two class objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * or their names are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @param first the first object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @param second the second object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @return true if equal, false if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    protected static final boolean classEquals(Class first, Class second) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        return first.equals(second) || first.getName().equals(second.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * all accesses to the <code> protected HashMap children </code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * shall be synchronized on that object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    protected transient HashMap         children;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    private             int             serializable  = 0; // children serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * all accesses to the <code> protected ArrayList bcmListeners </code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * shall be synchronized on that object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    protected transient ArrayList       bcmListeners;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * The current locale of this BeanContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    protected           Locale          locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * A <tt>boolean</tt> indicating if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * instance may now render a GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    protected           boolean         okToUseGui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * A <tt>boolean</tt> indicating whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * this object is currently in design time mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    protected           boolean         designTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * transient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    private transient PropertyChangeListener childPCL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    private transient VetoableChangeListener childVCL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    private transient boolean                serializing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
}