📄 propertychangesupport.java
字号:
s = new PropertyChangeSupport(source); s.listeners = new Vector(); children.put(propertyName, s); } s.listeners.add(l); } /** * Removes a PropertyChangeListener from listening to a specific property. * If <code>add()</code> has been called multiple times for a particular * listener on a property, <code>remove()</code> will have to be called the * same number of times to deregister it. This method will unwrap a * PropertyChangeListenerProxy, removing the underlying delegate from the * named property list if the names match. * * @param propertyName the property to stop listening on * @param l the listener to remove * @throws NullPointerException if propertyName is null */ public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener l) { 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, this returns * an empty array. * * @return the array of registered listeners * @throws NullPointerException if propertyName is null * @since 1.4 */ public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) { if (children == 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); } } } /** * 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 * @throws NullPointerException if propertyName is null */ 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -