propertychangesupport.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 546 行 · 第 1/2 页

JAVA
546
字号
  {    if (children == null)      return;    PropertyChangeSupport s      = (PropertyChangeSupport) children.get(propertyName);    if (s == null)      return;    while (l instanceof PropertyChangeListenerProxy)      {        PropertyChangeListenerProxy p = (PropertyChangeListenerProxy) l;        if (propertyName == null ? p.propertyName != null            : ! propertyName.equals(p.propertyName))          return;        l = (PropertyChangeListener) p.getListener();      }    s.listeners.remove(l);    if (s.listeners.isEmpty())      {        children.remove(propertyName);        if (children.isEmpty())          children = null;      }  }  /**   * Returns an array of all property change listeners registered under the   * given property name. If there are no registered listeners, or   * propertyName is null, this returns an empty array.   *   * @return the array of registered listeners   * @since 1.4   */  public synchronized PropertyChangeListener[]    getPropertyChangeListeners(String propertyName)  {    if (children == null || propertyName == null)      return new PropertyChangeListener[0];    PropertyChangeSupport s      = (PropertyChangeSupport) children.get(propertyName);    if (s == null)      return new PropertyChangeListener[0];    return (PropertyChangeListener[])      s.listeners.toArray(new PropertyChangeListener[s.listeners.size()]);  }  /**   * Fire a PropertyChangeEvent containing the old and new values of the   * property to all the global listeners, and to all the listeners for the   * specified property name. This does nothing if old and new are non-null   * and equal.   *   * @param propertyName the name of the property that changed   * @param oldVal the old value   * @param newVal the new value   */  public void firePropertyChange(String propertyName,                                 Object oldVal, Object newVal)  {    firePropertyChange(new PropertyChangeEvent(source, propertyName,                                               oldVal, newVal));  }  /**   * Fire a PropertyChangeEvent containing the old and new values of the   * property to all the global listeners, and to all the listeners for the   * specified property name. This does nothing if old and new are equal.   *   * @param propertyName the name of the property that changed   * @param oldVal the old value   * @param newVal the new value   */  public void firePropertyChange(String propertyName, int oldVal, int newVal)  {    if (oldVal != newVal)      firePropertyChange(new PropertyChangeEvent(source, propertyName,                                                 new Integer(oldVal),                                                 new Integer(newVal)));  }  /**   * Fire a PropertyChangeEvent containing the old and new values of the   * property to all the global listeners, and to all the listeners for the   * specified property name. This does nothing if old and new are equal.   *   * @param propertyName the name of the property that changed   * @param oldVal the old value   * @param newVal the new value   */  public void firePropertyChange(String propertyName,                                 boolean oldVal, boolean newVal)  {    if (oldVal != newVal)      firePropertyChange(new PropertyChangeEvent(source, propertyName,                                                 Boolean.valueOf(oldVal),                                                 Boolean.valueOf(newVal)));  }  /**   * Fire a PropertyChangeEvent to all the global listeners, and to all the   * listeners for the specified property name. This does nothing if old and   * new values of the event are equal.   *   * @param event the event to fire   * @throws NullPointerException if event is null   */  public void firePropertyChange(PropertyChangeEvent event)  {    if (event.oldValue != null && event.oldValue.equals(event.newValue))      return;    Vector v = listeners; // Be thread-safe.    if (v != null)      {        int i = v.size();        while (--i >= 0)          ((PropertyChangeListener) v.get(i)).propertyChange(event);      }    Hashtable h = children; // Be thread-safe.    if (h != null && event.propertyName != null)      {        PropertyChangeSupport s          = (PropertyChangeSupport) h.get(event.propertyName);        if (s != null)          {            v = s.listeners; // Be thread-safe.            int i = v == null ? 0 : v.size();            while (--i >= 0)              ((PropertyChangeListener) v.get(i)).propertyChange(event);          }      }  }  /**   * Fire an indexed property change event.  This will only fire   * an event if the old and new values are not equal and not null.    * @param name the name of the property which changed   * @param index the index of the property which changed   * @param oldValue the old value of the property   * @param newValue the new value of the property   * @since 1.5   */  public void fireIndexedPropertyChange(String name, int index,                                        Object oldValue, Object newValue)  {    // Argument checking is done in firePropertyChange(PropertyChangeEvent) .    firePropertyChange(new IndexedPropertyChangeEvent(source, name,                                                      oldValue, newValue,                                                      index));  }  /**   * Fire an indexed property change event.  This will only fire   * an event if the old and new values are not equal.   * @param name the name of the property which changed   * @param index the index of the property which changed   * @param oldValue the old value of the property   * @param newValue the new value of the property   * @since 1.5   */  public void fireIndexedPropertyChange(String name, int index,                                        int oldValue, int newValue)  {    if (oldValue != newValue)      fireIndexedPropertyChange(name, index, Integer.valueOf(oldValue),                                Integer.valueOf(newValue));  }  /**   * Fire an indexed property change event.  This will only fire   * an event if the old and new values are not equal.   * @param name the name of the property which changed   * @param index the index of the property which changed   * @param oldValue the old value of the property   * @param newValue the new value of the property   * @since 1.5   */  public void fireIndexedPropertyChange(String name, int index,                                        boolean oldValue, boolean newValue)  {    if (oldValue != newValue)      fireIndexedPropertyChange(name, index, Boolean.valueOf(oldValue),                                Boolean.valueOf(newValue));  }  /**   * Tell whether the specified property is being listened on or not. This   * will only return <code>true</code> if there are listeners on all   * properties or if there is a listener specifically on this property.   *   * @param propertyName the property that may be listened on   * @return whether the property is being listened on   */  public synchronized boolean hasListeners(String propertyName)  {    return listeners != null || (children != null                                 && children.get(propertyName) != null);  }  /**   * Saves the state of the object to the stream.   *   * @param s the stream to write to   * @throws IOException if anything goes wrong   * @serialData this writes out a null-terminated list of serializable   *             global property change listeners (the listeners for a named   *             property are written out as the global listeners of the   *             children, when the children hashtable is saved)   */  private synchronized void writeObject(ObjectOutputStream s)    throws IOException  {    s.defaultWriteObject();    if (listeners != null)      {        int i = listeners.size();        while (--i >= 0)          if (listeners.get(i) instanceof Serializable)            s.writeObject(listeners.get(i));      }    s.writeObject(null);  }  /**   * Reads the object back from stream (deserialization).   *   * XXX Since serialization for 1.1 streams was not documented, this may   * not work if propertyChangeSupportSerializedDataVersion is 1.   *   * @param s the stream to read from   * @throws IOException if reading the stream fails   * @throws ClassNotFoundException if deserialization fails   * @serialData this reads in a null-terminated list of serializable   *             global property change listeners (the listeners for a named   *             property are written out as the global listeners of the   *             children, when the children hashtable is saved)   */  private void readObject(ObjectInputStream s)    throws IOException, ClassNotFoundException  {    s.defaultReadObject();    PropertyChangeListener l = (PropertyChangeListener) s.readObject();    while (l != null)      {        addPropertyChangeListener(l);        l = (PropertyChangeListener) s.readObject();      }    // Sun is not as careful with children as we are, and lets some proxys    // in that can never receive events. So, we clean up anything that got    // serialized, to make sure our invariants hold.    if (children != null)      {        int i = children.size();        Iterator iter = children.entrySet().iterator();        while (--i >= 0)          {            Entry e = (Entry) iter.next();            String name = (String) e.getKey();            PropertyChangeSupport pcs = (PropertyChangeSupport) e.getValue();            if (pcs.listeners == null)              pcs.listeners = new Vector();            if (pcs.children != null)              pcs.listeners.addAll                (Arrays.asList(pcs.getPropertyChangeListeners(name)));            if (pcs.listeners.size() == 0)              iter.remove();            else              pcs.children = null;          }        if (children.size() == 0)          children = null;      }  }} // class PropertyChangeSupport

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?