Java Code Examples for java.beans.PropertyChangeSupport#hasListeners()

The following examples show how to use java.beans.PropertyChangeSupport#hasListeners() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SharedClassObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Adds the specified property change listener to receive property
 * change events from this object.
 * @param         l the property change listener
 */
public final void addPropertyChangeListener(PropertyChangeListener l) {
    boolean noListener;

    synchronized (getLock()) {
        //      System.out.println ("added listener: " + l + " to: " + getClass ()); // NOI18N
        PropertyChangeSupport supp = (PropertyChangeSupport) getProperty(PROP_SUPPORT);

        if (supp == null) {
            //        System.out.println ("Creating support"); // NOI18N
            putProperty(PROP_SUPPORT, supp = new PropertyChangeSupport(this));
        }

        noListener = !supp.hasListeners(null);
        supp.addPropertyChangeListener(l);
    }

    if (noListener) {
        addNotifySuper = false;
        addNotify();

        if (!addNotifySuper) {
            // [PENDING] theoretical race condition for this warning if listeners are added
            // and removed very quickly from two threads, I guess, and addNotify() impl is slow
            String msg = "You must call super.addNotify() from " + getClass().getName() + ".addNotify()"; // NOI18N
            err.warning(msg);
        }
    }
}
 
Example 2
Source File: SharedClassObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the specified property change listener so that it
 * no longer receives property change events from this object.
 * @param         l     the property change listener
 */
public final void removePropertyChangeListener(PropertyChangeListener l) {
    boolean callRemoved;

    synchronized (getLock()) {
        PropertyChangeSupport supp = (PropertyChangeSupport) getProperty(PROP_SUPPORT);

        if (supp == null) {
            return;
        }

        boolean hasListener = supp.hasListeners(null);
        supp.removePropertyChangeListener(l);
        callRemoved = hasListener && !supp.hasListeners(null);
    }

    if (callRemoved) {
        putProperty(PROP_SUPPORT, null); // clean the PCS, see #25417
        removeNotifySuper = false;
        removeNotify();

        if (!removeNotifySuper) {
            String msg = "You must call super.removeNotify() from " + getClass().getName() + ".removeNotify()"; // NOI18N
            err.warning(msg);
        }
    }
}