- FullBeanContextSupport.java was initially added on branch boston.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/corba/src/org/netbeans/modules/corba/utils/FullBeanContextSupport.java Sun Aug 27 10:25:45 2000 +0000
1.3 @@ -0,0 +1,488 @@
1.4 +/*
1.5 + * Sun Public License Notice
1.6 + *
1.7 + * The contents of this file are subject to the Sun Public License
1.8 + * Version 1.0 (the "License"). You may not use this file except in
1.9 + * compliance with the License. A copy of the License is available at
1.10 + * http://www.sun.com/
1.11 + *
1.12 + * The Original Code is NetBeans. The Initial Developer of the Original
1.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
1.14 + * Microsystems, Inc. All Rights Reserved.
1.15 + */
1.16 +
1.17 +package org.netbeans.modules.corba.utils;
1.18 +
1.19 +import java.beans.PropertyChangeEvent;
1.20 +import java.beans.PropertyChangeListener;
1.21 +import java.beans.VetoableChangeListener;
1.22 +import java.beans.PropertyVetoException;
1.23 +import java.beans.beancontext.*;
1.24 +import java.util.Iterator;
1.25 +import java.util.ArrayList;
1.26 +import java.util.Vector;
1.27 +import java.util.HashMap;
1.28 +import java.util.Collection;
1.29 +import java.io.Serializable;
1.30 +import java.io.ObjectInputStream;
1.31 +import java.io.ObjectOutputStream;
1.32 +import java.io.IOException;
1.33 +/**
1.34 + *
1.35 + * @author Tomas Zezula
1.36 + * @version 1.0
1.37 + * This class does not offers full functionality of BeanContextSupport
1.38 + */
1.39 +public class FullBeanContextSupport extends Vector implements BeanContext, Serializable {
1.40 +
1.41 + public static final String BEAN_CONTEXT = "beanContext";
1.42 + public static final String DESIGN_MODE ="designMode";
1.43 +
1.44 + protected boolean avoidGui = false;
1.45 + protected boolean isDesignTime = false;
1.46 + protected int noChildDesignTime = 0;
1.47 + protected BeanContext parent = null;
1.48 + transient protected ArrayList contentMembershipListeners;
1.49 + transient protected HashMap propertyChangeListeners;
1.50 + transient protected HashMap vetoableListeners;
1.51 + transient protected PropertyChangeDispatcher pchDispatcher;
1.52 + transient protected VetoableChangeListener vchDispatcher;
1.53 +
1.54 + private class PropertyChangeDispatcher implements PropertyChangeListener {
1.55 + public void propertyChange (PropertyChangeEvent event) {
1.56 + //racecondition avoidance
1.57 + if (!FullBeanContextSupport.this.contains(event.getSource()))
1.58 + return;
1.59 + String propName = event.getPropertyName();
1.60 + if (DESIGN_MODE.equals (propName)) {
1.61 + Boolean b = (Boolean) event.getNewValue();
1.62 + if (b != null) {
1.63 + if (b.booleanValue())
1.64 + noChildDesignTime++;
1.65 + else
1.66 + noChildDesignTime--;
1.67 + }
1.68 + }
1.69 + synchronized (FullBeanContextSupport.this) {
1.70 + ArrayList slot = (ArrayList) FullBeanContextSupport.this.propertyChangeListeners.get (propName);
1.71 + if (slot != null && slot.size() > 0)
1.72 + FullBeanContextSupport.this.firePropertyChange (event, slot);
1.73 + }
1.74 + }
1.75 + }
1.76 +
1.77 + private class VetoableChangeDispatcher implements VetoableChangeListener {
1.78 + public void vetoableChange (PropertyChangeEvent event) throws PropertyVetoException {
1.79 + //racecondition avoidance
1.80 + if (!FullBeanContextSupport.this.contains(event.getSource()))
1.81 + return;
1.82 + String propName = event.getPropertyName();
1.83 + synchronized (FullBeanContextSupport.this) {
1.84 + ArrayList slot = (ArrayList) FullBeanContextSupport.this.vetoableListeners.get (propName);
1.85 + if (slot != null && slot.size() > 0)
1.86 + FullBeanContextSupport.this.fireVetoableChange (event,slot);
1.87 + }
1.88 + }
1.89 + }
1.90 +
1.91 + /** Creates new FullBeanContextSupport */
1.92 + public FullBeanContextSupport() {
1.93 + this.contentMembershipListeners = new ArrayList();
1.94 + this.propertyChangeListeners = new HashMap ();
1.95 + this.propertyChangeListeners.put (DESIGN_MODE, new ArrayList());
1.96 + this.vetoableListeners = new HashMap ();
1.97 + this.pchDispatcher = this.new PropertyChangeDispatcher();
1.98 + this.vchDispatcher = this.new VetoableChangeDispatcher();
1.99 + }
1.100 +
1.101 +
1.102 + /** Methods defined in BeanContext interface */
1.103 +
1.104 + public java.lang.Object instantiateChild(java.lang.String str) throws java.io.IOException, java.lang.ClassNotFoundException {
1.105 + return java.beans.Beans.instantiate (this.getClass().getClassLoader(), str);
1.106 + }
1.107 +
1.108 + public synchronized void addBeanContextMembershipListener(java.beans.beancontext.BeanContextMembershipListener beanContextMembershipListener) {
1.109 + this.contentMembershipListeners.add (beanContextMembershipListener);
1.110 + }
1.111 +
1.112 + public synchronized void removeBeanContextMembershipListener(java.beans.beancontext.BeanContextMembershipListener beanContextMembershipListener) {
1.113 + this.contentMembershipListeners.remove (beanContextMembershipListener);
1.114 + }
1.115 +
1.116 + public java.net.URL getResource(java.lang.String str, java.beans.beancontext.BeanContextChild beanContextChild) throws java.lang.IllegalArgumentException {
1.117 + return beanContextChild.getClass().getClassLoader().getResource (str);
1.118 + }
1.119 +
1.120 + public java.io.InputStream getResourceAsStream(java.lang.String str, java.beans.beancontext.BeanContextChild beanContextChild) throws java.lang.IllegalArgumentException {
1.121 + return beanContextChild.getClass().getClassLoader().getResourceAsStream (str);
1.122 + }
1.123 +
1.124 +
1.125 +
1.126 + /** Mothods defined in BeanContextChildren inteface */
1.127 +
1.128 + public void setBeanContext(java.beans.beancontext.BeanContext beanContext) throws java.beans.PropertyVetoException {
1.129 + Object oldParent = this.parent;
1.130 + this.fireVetoableChange (BEAN_CONTEXT, this.parent, beanContext);
1.131 + this.parent = beanContext;
1.132 + this.firePropertyChange (BEAN_CONTEXT, oldParent, this.parent);
1.133 + }
1.134 +
1.135 + public java.beans.beancontext.BeanContext getBeanContext() {
1.136 + return parent;
1.137 + }
1.138 +
1.139 + public synchronized void addVetoableChangeListener(java.lang.String str, java.beans.VetoableChangeListener vetoableChangeListener) {
1.140 + ArrayList slot = (ArrayList) this.vetoableListeners.get (str);
1.141 + if (slot == null) {
1.142 + slot = new ArrayList ();
1.143 + this.vetoableListeners.put (str, slot);
1.144 + Iterator it = this.iterator();
1.145 + while (it.hasNext()) {
1.146 + Object obj = it.next();
1.147 + BeanContextChild bcc = getBeanContextChild (obj);
1.148 + if (bcc != null)
1.149 + bcc.addVetoableChangeListener (str, vchDispatcher);
1.150 + }
1.151 + }
1.152 + slot.add (vetoableChangeListener);
1.153 + }
1.154 +
1.155 + public synchronized void removeVetoableChangeListener(java.lang.String str, java.beans.VetoableChangeListener vetoableChangeListener) {
1.156 + ArrayList slot = (ArrayList) this.vetoableListeners.get (str);
1.157 + if (slot == null)
1.158 + return;
1.159 + slot.remove (vetoableChangeListener);
1.160 + if (slot.size() == 0) {
1.161 + this.vetoableListeners.put (str, null);
1.162 + Iterator it = this.iterator ();
1.163 + while (it.hasNext()) {
1.164 + Object obj = it.next();
1.165 + BeanContextChild bcc = getBeanContextChild (obj);
1.166 + if (bcc != null)
1.167 + bcc.removeVetoableChangeListener (str, vchDispatcher);
1.168 + }
1.169 + }
1.170 + }
1.171 +
1.172 + public synchronized void addPropertyChangeListener(java.lang.String str, java.beans.PropertyChangeListener propertyChangeListener) {
1.173 + ArrayList slot = (ArrayList) this.propertyChangeListeners.get (str);
1.174 + if (slot == null) {
1.175 + slot = new ArrayList();
1.176 + this.propertyChangeListeners.put (str, slot);
1.177 + Iterator it = this.iterator ();
1.178 + while (it.hasNext()) {
1.179 + Object obj = it.next();
1.180 + BeanContextChild bcc = getBeanContextChild (obj);
1.181 + if (obj != null)
1.182 + bcc.addPropertyChangeListener (str, pchDispatcher);
1.183 + }
1.184 + }
1.185 + slot.add (propertyChangeListener);
1.186 + }
1.187 +
1.188 + public synchronized void removePropertyChangeListener(java.lang.String str, java.beans.PropertyChangeListener propertyChangeListener) {
1.189 + ArrayList slot = (ArrayList) this.propertyChangeListeners.get (str);
1.190 + if (slot == null)
1.191 + return;
1.192 + slot.remove (propertyChangeListener);
1.193 + if (slot.size() == 0 && ! DESIGN_MODE.equals(str)) {
1.194 + this.propertyChangeListeners.remove (str);
1.195 + Iterator it = this.iterator ();
1.196 + while (it.hasNext()) {
1.197 + Object obj = it.next();
1.198 + BeanContextChild bcc = getBeanContextChild (obj);
1.199 + if (bcc != null)
1.200 + bcc.removePropertyChangeListener (str, pchDispatcher);
1.201 + }
1.202 + }
1.203 + }
1.204 +
1.205 +
1.206 +
1.207 + /** Visibility and DesignTime defined methods */
1.208 +
1.209 + public void okToUseGui() {
1.210 + this.avoidGui = false;
1.211 + }
1.212 +
1.213 + public boolean needsGui() {
1.214 + return ! this.avoidGui;
1.215 + }
1.216 +
1.217 + public boolean avoidingGui() {
1.218 + return this.avoidGui;
1.219 + }
1.220 +
1.221 + public void dontUseGui() {
1.222 + this.avoidGui = true;
1.223 + }
1.224 +
1.225 + public boolean isDesignTime() {
1.226 + return this.isDesignTime || (this.noChildDesignTime>0);
1.227 + }
1.228 +
1.229 + public void setDesignTime(boolean param) {
1.230 + boolean oldValue = this.isDesignTime;
1.231 + this.isDesignTime = param;
1.232 + firePropertyChange (DESIGN_MODE,new Boolean(oldValue),new Boolean(this.isDesignTime));
1.233 + }
1.234 +
1.235 + public synchronized boolean add (Object element) {
1.236 + boolean res = super.add (element);
1.237 + if (res) {
1.238 + BeanContextChild cld = getBeanContextChild (element);
1.239 + if (cld != null) {
1.240 + try {
1.241 + cld.setBeanContext (this);
1.242 + }catch (PropertyVetoException pve) {}
1.243 + if (cld instanceof BeanContext && ((BeanContext)cld).isDesignTime())
1.244 + this.noChildDesignTime++;
1.245 + Iterator it = this.propertyChangeListeners.keySet().iterator();
1.246 + while (it.hasNext())
1.247 + cld.addPropertyChangeListener ((String)it.next(), this.pchDispatcher);
1.248 + it = this.vetoableListeners.keySet().iterator();
1.249 + while (it.hasNext())
1.250 + cld.addVetoableChangeListener ((String)it.next(), this.vchDispatcher);
1.251 + }
1.252 + fireChildrenAdded (new Object[] {element});
1.253 + }
1.254 + return res;
1.255 + }
1.256 +
1.257 + public synchronized boolean addAll (Collection c) {
1.258 + ArrayList subList = new ArrayList ();
1.259 + Iterator it = c.iterator ();
1.260 + while (it.hasNext()) {
1.261 + Object entry = it.next();
1.262 + if (!this.contains (entry)) {
1.263 + subList.add (entry);
1.264 + super.add (entry);
1.265 + BeanContextChild cld = getBeanContextChild (entry);
1.266 + if (cld != null) {
1.267 + try {
1.268 + cld.setBeanContext (this);
1.269 + }catch (PropertyVetoException pve){}
1.270 + if (cld instanceof BeanContext && ((BeanContext)cld).isDesignTime())
1.271 + this.noChildDesignTime++;
1.272 + for (Iterator lit = this.propertyChangeListeners.keySet().iterator();it.hasNext();)
1.273 + cld.addPropertyChangeListener ((String)lit.next(), this.pchDispatcher);
1.274 + for (Iterator lit = this.vetoableListeners.keySet().iterator(); it.hasNext();)
1.275 + cld.addVetoableChangeListener ((String)lit.next(),vchDispatcher);
1.276 + }
1.277 + }
1.278 + }
1.279 + fireChildrenAdded (subList);
1.280 + return subList.size() != 0;
1.281 + }
1.282 +
1.283 + public synchronized void clear () {
1.284 + ArrayList entries = new ArrayList();
1.285 + while (this.size()>0) {
1.286 + Object element = this.remove (0);
1.287 + entries.add (element);
1.288 + BeanContextChild bcc = getBeanContextChild (element);
1.289 + if (bcc != null) {
1.290 + try {
1.291 + bcc.setBeanContext (null);
1.292 + }catch (PropertyVetoException pve) {}
1.293 + for (Iterator lit = this.propertyChangeListeners.keySet().iterator(); lit.hasNext();)
1.294 + bcc.removePropertyChangeListener ((String)lit.next(), this.pchDispatcher);
1.295 + for (Iterator lit = this.vetoableListeners.keySet().iterator(); lit.hasNext();)
1.296 + bcc.removeVetoableChangeListener ((String)lit.next(),this.vchDispatcher);
1.297 + }
1.298 +
1.299 + }
1.300 + this.noChildDesignTime = 0;
1.301 + fireChildrenRemoved (entries);
1.302 + }
1.303 +
1.304 + public synchronized boolean remove (Object entry) {
1.305 + boolean res = super.remove (entry);
1.306 + if (res) {
1.307 + BeanContextChild cld = getBeanContextChild (entry);
1.308 + if (cld != null) {
1.309 + try {
1.310 + cld.setBeanContext (null);
1.311 + }catch (PropertyVetoException pve) {}
1.312 + if (cld instanceof BeanContext && ((BeanContext)cld).isDesignTime())
1.313 + this.noChildDesignTime--;
1.314 + Iterator it = this.propertyChangeListeners.keySet().iterator();
1.315 + while (it.hasNext())
1.316 + cld.removePropertyChangeListener ((String)it.next(),this.pchDispatcher);
1.317 + it = this.vetoableListeners.keySet().iterator();
1.318 + while (it.hasNext())
1.319 + cld.removeVetoableChangeListener ((String)it.next(), this.vchDispatcher);
1.320 + }
1.321 + fireChildrenRemoved (new Object[] {entry});
1.322 + }
1.323 + return res;
1.324 + }
1.325 +
1.326 + public synchronized boolean removeAll (Collection c) {
1.327 + ArrayList subList = new ArrayList ();
1.328 + Iterator it = c.iterator();
1.329 + while (it.hasNext()) {
1.330 + Object entry = it.next();
1.331 + subList.add (entry);
1.332 + super.remove (entry);
1.333 + BeanContextChild cld = getBeanContextChild (entry);
1.334 + if (cld != null) {
1.335 + try {
1.336 + cld.setBeanContext (null);
1.337 + }catch (PropertyVetoException pve) {}
1.338 + if (cld instanceof BeanContext && ((BeanContext)cld).isDesignTime())
1.339 + this.noChildDesignTime--;
1.340 + for (Iterator lit = this.propertyChangeListeners.keySet().iterator(); it.hasNext();)
1.341 + cld.addPropertyChangeListener ((String)lit.next(), pchDispatcher);
1.342 + for (Iterator lit = this.vetoableListeners.keySet().iterator(); it.hasNext();)
1.343 + cld.addVetoableChangeListener ((String)lit.next(), vchDispatcher);
1.344 + }
1.345 + }
1.346 + fireChildrenRemoved (subList);
1.347 + return subList.size()!=0;
1.348 + }
1.349 +
1.350 +
1.351 + public synchronized boolean retainAll (Collection c) {
1.352 + ArrayList removed = new ArrayList();
1.353 + Iterator it = this.iterator();
1.354 + while (it.hasNext()) {
1.355 + Object element = it.next();
1.356 + if (!c.contains (element)) {
1.357 + super.remove (element);
1.358 + BeanContextChild bcc = getBeanContextChild (element);
1.359 + if (bcc != null) {
1.360 + try {
1.361 + bcc.setBeanContext (null);
1.362 + }catch (PropertyVetoException pve) {}
1.363 + if (bcc instanceof BeanContext && ((BeanContext)bcc).isDesignTime())
1.364 + this.noChildDesignTime--;
1.365 + for (Iterator lit = this.propertyChangeListeners.keySet().iterator();lit.hasNext();)
1.366 + bcc.removePropertyChangeListener ((String)lit.next(), this.pchDispatcher);
1.367 + for (Iterator lit = this.vetoableListeners.keySet().iterator(); lit.hasNext();)
1.368 + bcc.removeVetoableChangeListener ((String)lit.next(), this.vchDispatcher);
1.369 + }
1.370 + removed.add (element);
1.371 + }
1.372 + }
1.373 + fireChildrenRemoved (removed);
1.374 + return removed.size()!=0;
1.375 + }
1.376 +
1.377 + /** Listener notification Helpers */
1.378 +
1.379 + protected void fireChildrenAdded (Object[] change) {
1.380 + if (change == null || change.length == 0)
1.381 + return;
1.382 + BeanContextMembershipEvent event = new BeanContextMembershipEvent (this, change);
1.383 + fireChildrenAdded (event);
1.384 + }
1.385 +
1.386 + protected void fireChildrenAdded (Collection change) {
1.387 + if (change == null || change.size() == 0)
1.388 + return;
1.389 + BeanContextMembershipEvent event = new BeanContextMembershipEvent (this, change);
1.390 + fireChildrenAdded (event);
1.391 + }
1.392 +
1.393 + protected void fireChildrenAdded (BeanContextMembershipEvent event) {
1.394 + Iterator it;
1.395 + synchronized (this) {
1.396 + it = ((ArrayList)this.contentMembershipListeners.clone()).iterator();
1.397 + }
1.398 + while (it.hasNext()) {
1.399 + ((BeanContextMembershipListener)it.next()).childrenAdded (event);
1.400 + }
1.401 + }
1.402 +
1.403 + protected void fireChildrenRemoved (Object[] change) {
1.404 + if (change == null || change.length == 0)
1.405 + return;
1.406 + BeanContextMembershipEvent event = new BeanContextMembershipEvent (this, change);
1.407 + fireChildrenRemoved (event);
1.408 + }
1.409 +
1.410 + protected void fireChildrenRemoved (Collection change) {
1.411 + if (change == null || change.size() == 0)
1.412 + return;
1.413 + BeanContextMembershipEvent event = new BeanContextMembershipEvent (this, change);
1.414 + fireChildrenRemoved (event);
1.415 + }
1.416 +
1.417 + protected void fireChildrenRemoved (BeanContextMembershipEvent event) {
1.418 + Iterator it;
1.419 + synchronized (this) {
1.420 + it = ((ArrayList)this.contentMembershipListeners.clone()).iterator();
1.421 + }
1.422 + while (it.hasNext()) {
1.423 + ((BeanContextMembershipListener)it.next()).childrenRemoved (event);
1.424 + }
1.425 + }
1.426 +
1.427 + protected void firePropertyChange (String propName, Object oldValue, Object newValue) {
1.428 + PropertyChangeEvent event = new PropertyChangeEvent (this, propName, oldValue, newValue);
1.429 + ArrayList slot;
1.430 + synchronized (this) {
1.431 + slot = (ArrayList) this.propertyChangeListeners.get (propName);
1.432 + if (slot == null || slot.size() ==0)
1.433 + return;
1.434 + slot = (ArrayList) slot.clone();
1.435 + }
1.436 + firePropertyChange (event, slot);
1.437 + }
1.438 +
1.439 + /** Caller of this method has to ensure synchronization !!
1.440 + */
1.441 + protected void firePropertyChange (PropertyChangeEvent event, ArrayList slot) {
1.442 + Iterator it = slot.iterator();
1.443 + while (it.hasNext()) {
1.444 + ((PropertyChangeListener)it.next()).propertyChange (event);
1.445 + }
1.446 + }
1.447 +
1.448 + protected void fireVetoableChange (String propName, Object oldValue, Object newValue) throws PropertyVetoException {
1.449 + PropertyChangeEvent event = new PropertyChangeEvent (this, propName, oldValue, newValue);
1.450 + ArrayList slot;
1.451 + synchronized (this) {
1.452 + slot = (ArrayList) this.vetoableListeners.get (propName);
1.453 + if (slot == null || slot.size() == 0)
1.454 + return;
1.455 + slot = (ArrayList) slot.clone();
1.456 + }
1.457 + fireVetoableChange (event, slot);
1.458 + }
1.459 +
1.460 + /** Caller of this method has to ensure synchronization !!
1.461 + */
1.462 + protected void fireVetoableChange (PropertyChangeEvent event, ArrayList slot) throws PropertyVetoException {
1.463 + Iterator it = slot.iterator();
1.464 + while (it.hasNext()) {
1.465 + ((VetoableChangeListener)it.next()).vetoableChange (event);
1.466 + }
1.467 + }
1.468 +
1.469 + protected BeanContextChild getBeanContextChild (Object obj) {
1.470 + if (obj instanceof BeanContextChild)
1.471 + return (BeanContextChild) obj;
1.472 + if (obj instanceof BeanContextProxy)
1.473 + ((BeanContextProxy)obj).getBeanContextProxy();
1.474 + return null;
1.475 + }
1.476 +
1.477 +
1.478 + private void writeObject (ObjectOutputStream out) throws IOException {
1.479 + out.defaultWriteObject();
1.480 + }
1.481 +
1.482 + private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
1.483 + in.defaultReadObject();
1.484 + this.contentMembershipListeners = new ArrayList ();
1.485 + this.propertyChangeListeners = new HashMap ();
1.486 + this.vetoableListeners = new HashMap ();
1.487 + this.pchDispatcher = new PropertyChangeDispatcher ();
1.488 + this.vchDispatcher = new VetoableChangeDispatcher ();
1.489 + }
1.490 +
1.491 +}