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

The following examples show how to use java.beans.PropertyChangeSupport#addPropertyChangeListener() . 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: TestEquals.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    TestEquals one = new TestEquals(1);
    TestEquals two = new TestEquals(2);

    Object source = TestEquals.class;
    PropertyChangeSupport pcs = new PropertyChangeSupport(source);
    pcs.addPropertyChangeListener(PROPERTY, one);
    pcs.addPropertyChangeListener(PROPERTY, two);

    PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);
    pcs.firePropertyChange(event);
    test(one, two, 1); // only one check
    pcs.firePropertyChange(PROPERTY, one, two);
    test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
    pcs.fireIndexedPropertyChange(PROPERTY, 1, one, two);
    test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
}
 
Example 2
Source File: TestEquals.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    TestEquals one = new TestEquals(1);
    TestEquals two = new TestEquals(2);

    Object source = TestEquals.class;
    PropertyChangeSupport pcs = new PropertyChangeSupport(source);
    pcs.addPropertyChangeListener(PROPERTY, one);
    pcs.addPropertyChangeListener(PROPERTY, two);

    PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);
    pcs.firePropertyChange(event);
    test(one, two, 1); // only one check
    pcs.firePropertyChange(PROPERTY, one, two);
    test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
    pcs.fireIndexedPropertyChange(PROPERTY, 1, one, two);
    test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
}
 
Example 3
Source File: TestEquals.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    TestEquals one = new TestEquals(1);
    TestEquals two = new TestEquals(2);

    Object source = TestEquals.class;
    PropertyChangeSupport pcs = new PropertyChangeSupport(source);
    pcs.addPropertyChangeListener(PROPERTY, one);
    pcs.addPropertyChangeListener(PROPERTY, two);

    PropertyChangeEvent event = new PropertyChangeEvent(source, PROPERTY, one, two);
    pcs.firePropertyChange(event);
    test(one, two, 1); // only one check
    pcs.firePropertyChange(PROPERTY, one, two);
    test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
    pcs.fireIndexedPropertyChange(PROPERTY, 1, one, two);
    test(one, two, 2); // because it invokes firePropertyChange(PropertyChangeEvent)
}
 
Example 4
Source File: MockPropertyChangeListenerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    source = new Object();
    pcs = new PropertyChangeSupport(source);
    l = new MockPropertyChangeListener();
    pcs.addPropertyChangeListener(l);
}
 
Example 5
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 6
Source File: DirWatcher.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new directory watcher.
 *
 * @param observer watcher observer
 * @throws IOException if an I/O error occurs
 */
private DirWatcher(PropertyChangeListener observer) throws IOException {
  watcher = FileSystems.getDefault().newWatchService();
  keys = new HashMap<>();
  pcs = new PropertyChangeSupport(this);
  pcs.addPropertyChangeListener(observer);
}
 
Example 7
Source File: TestListeners.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
    pcs.addPropertyChangeListener(new TestListeners(0));
    pcs.addPropertyChangeListener(NAME, new TestListeners(2));
    pcs.addPropertyChangeListener(new TestListeners(1));
    pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));


    current = 0;
    pcs.firePropertyChange(NAME, 0, 1);
    if (current != 4)
        throw new Error("Expected 4 listeners, but called " + current);

    current = 0;
    pcs.firePropertyChange(NONE, 1, 0);
    if (current != 2)
        throw new Error("Expected 2 listeners, but called " + current);


    PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
    if (all.length != 4)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 2)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);

    pcs.removePropertyChangeListener(new TestListeners(0));
    pcs.removePropertyChangeListener(new TestListeners(1));
    pcs.removePropertyChangeListener(NAME, new TestListeners(2));
    pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));

    all = pcs.getPropertyChangeListeners();
    if (all.length != 0)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 0)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);
}
 
Example 8
Source File: TestListeners.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
    pcs.addPropertyChangeListener(new TestListeners(0));
    pcs.addPropertyChangeListener(NAME, new TestListeners(2));
    pcs.addPropertyChangeListener(new TestListeners(1));
    pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));


    current = 0;
    pcs.firePropertyChange(NAME, 0, 1);
    if (current != 4)
        throw new Error("Expected 4 listeners, but called " + current);

    current = 0;
    pcs.firePropertyChange(NONE, 1, 0);
    if (current != 2)
        throw new Error("Expected 2 listeners, but called " + current);


    PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
    if (all.length != 4)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 2)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);

    pcs.removePropertyChangeListener(new TestListeners(0));
    pcs.removePropertyChangeListener(new TestListeners(1));
    pcs.removePropertyChangeListener(NAME, new TestListeners(2));
    pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));

    all = pcs.getPropertyChangeListeners();
    if (all.length != 0)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 0)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);
}
 
Example 9
Source File: TestSerialization.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static PropertyChangeSupport create() {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestSerialization.class);
    pcs.addPropertyChangeListener(new TestSerialization(0));
    pcs.addPropertyChangeListener(NAME, new TestSerialization(1));
    return pcs;
}
 
Example 10
Source File: TestListeners.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
    pcs.addPropertyChangeListener(new TestListeners(0));
    pcs.addPropertyChangeListener(NAME, new TestListeners(2));
    pcs.addPropertyChangeListener(new TestListeners(1));
    pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));


    current = 0;
    pcs.firePropertyChange(NAME, 0, 1);
    if (current != 4)
        throw new Error("Expected 4 listeners, but called " + current);

    current = 0;
    pcs.firePropertyChange(NONE, 1, 0);
    if (current != 2)
        throw new Error("Expected 2 listeners, but called " + current);


    PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
    if (all.length != 4)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 2)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);

    pcs.removePropertyChangeListener(new TestListeners(0));
    pcs.removePropertyChangeListener(new TestListeners(1));
    pcs.removePropertyChangeListener(NAME, new TestListeners(2));
    pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));

    all = pcs.getPropertyChangeListeners();
    if (all.length != 0)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 0)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);
}
 
Example 11
Source File: Test7148143.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeListener listener = new CustomProxy();
    PropertyChangeSupport support = new PropertyChangeSupport(listener);
    support.addPropertyChangeListener(listener);
    support.addPropertyChangeListener("foo", listener); // cast class exception
}
 
Example 12
Source File: TestSerialization.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static PropertyChangeSupport create() {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestSerialization.class);
    pcs.addPropertyChangeListener(new TestSerialization(0));
    pcs.addPropertyChangeListener(NAME, new TestSerialization(1));
    return pcs;
}
 
Example 13
Source File: Test7148143.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeListener listener = new CustomProxy();
    PropertyChangeSupport support = new PropertyChangeSupport(listener);
    support.addPropertyChangeListener(listener);
    support.addPropertyChangeListener("foo", listener); // cast class exception
}
 
Example 14
Source File: TestSerialization.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static PropertyChangeSupport create() {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestSerialization.class);
    pcs.addPropertyChangeListener(new TestSerialization(0));
    pcs.addPropertyChangeListener(NAME, new TestSerialization(1));
    return pcs;
}
 
Example 15
Source File: StatsPanelController.java    From stendhal with GNU General Public License v2.0 4 votes vote down vote up
private void addPropertyChangeListenerWithModifiedSupport(PropertyChangeSupport pcs, String attribute, PropertyChangeListener listener) {
	pcs.addPropertyChangeListener(attribute, listener);
	pcs.addPropertyChangeListener("modified_" + attribute, listener);
}
 
Example 16
Source File: Test7148143.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeListener listener = new CustomProxy();
    PropertyChangeSupport support = new PropertyChangeSupport(listener);
    support.addPropertyChangeListener(listener);
    support.addPropertyChangeListener("foo", listener); // cast class exception
}
 
Example 17
Source File: TestListeners.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
    pcs.addPropertyChangeListener(new TestListeners(0));
    pcs.addPropertyChangeListener(NAME, new TestListeners(2));
    pcs.addPropertyChangeListener(new TestListeners(1));
    pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));


    current = 0;
    pcs.firePropertyChange(NAME, 0, 1);
    if (current != 4)
        throw new Error("Expected 4 listeners, but called " + current);

    current = 0;
    pcs.firePropertyChange(NONE, 1, 0);
    if (current != 2)
        throw new Error("Expected 2 listeners, but called " + current);


    PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
    if (all.length != 4)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 2)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);

    pcs.removePropertyChangeListener(new TestListeners(0));
    pcs.removePropertyChangeListener(new TestListeners(1));
    pcs.removePropertyChangeListener(NAME, new TestListeners(2));
    pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));

    all = pcs.getPropertyChangeListeners();
    if (all.length != 0)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 0)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);
}
 
Example 18
Source File: TestListeners.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
    pcs.addPropertyChangeListener(new TestListeners(0));
    pcs.addPropertyChangeListener(NAME, new TestListeners(2));
    pcs.addPropertyChangeListener(new TestListeners(1));
    pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));


    current = 0;
    pcs.firePropertyChange(NAME, 0, 1);
    if (current != 4)
        throw new Error("Expected 4 listeners, but called " + current);

    current = 0;
    pcs.firePropertyChange(NONE, 1, 0);
    if (current != 2)
        throw new Error("Expected 2 listeners, but called " + current);


    PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
    if (all.length != 4)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 2)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);

    pcs.removePropertyChangeListener(new TestListeners(0));
    pcs.removePropertyChangeListener(new TestListeners(1));
    pcs.removePropertyChangeListener(NAME, new TestListeners(2));
    pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));

    all = pcs.getPropertyChangeListeners();
    if (all.length != 0)
        throw new Error("Expected 4 listeners, but contained " + all.length);

    named = pcs.getPropertyChangeListeners(NAME);
    if (named.length != 0)
        throw new Error("Expected 2 named listeners, but contained " + named.length);

    other = pcs.getPropertyChangeListeners(NONE);
    if (other.length != 0)
        throw new Error("Expected 0 other listeners, but contained " + other.length);
}
 
Example 19
Source File: DocumentUtilities.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Adds a weak <code>PropertyChangeListener</code> to a document.
 *
 * <p>In general, document properties are key-value pairs where both the key
 * and the value can be any <code>Object</code>. Contrary to that <code>PropertyChangeListener</code>s
 * can only handle named properties that can have an arbitrary value, but have <code>String</code> names.
 * Therefore the listenera attached to a document will only ever recieve document
 * properties, which keys are of <code>java.lang.String</code> type.
 *
 * <p>Additionally, the list of document properties that clients can listen on
 * is not part of this contract.
 *
 * @param doc The document to add the listener to.
 * @param listenerImplementation The listener to be added weakly to the document.
 * @return the created weak listener - only the returned listener can be
 * used with {@link #removePropertyChangeListener}. If the document does not
 * support {@code PropertyChangeLister} {@code null} is returned.
 *
 * @since 1.68
 */
public static PropertyChangeListener addWeakPropertyChangeListener(Document doc, PropertyChangeListener listenerImplementation) {
    PropertyChangeSupport pcs = (PropertyChangeSupport) doc.getProperty(PropertyChangeSupport.class);
    PropertyChangeListener weakListener = null;
    if (pcs != null) {
        weakListener = WeakListeners.propertyChange(listenerImplementation, pcs);
        pcs.addPropertyChangeListener(weakListener);
    }
    return weakListener;
}
 
Example 20
Source File: DocumentUtilities.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Adds <code>PropertyChangeListener</code> to a document.
 *
 * <p>In general, document properties are key-value pairs where both the key
 * and the value can be any <code>Object</code>. Contrary to that <code>PropertyChangeListener</code>s
 * can only handle named properties that can have an arbitrary value, but have <code>String</code> names.
 * Therefore the listenera attached to a document will only ever recieve document
 * properties, which keys are of <code>java.lang.String</code> type.
 *
 * <p>Additionally, the list of document properties that clients can listen on
 * is not part of this contract.
 *
 * <p>Note that this method does <em>not</em> work with {@code WeakListeners}.
 * Use {@link #addWeakPropertyChangeListener} for that purpose.
 *
 * @param doc The document to add the listener to.
 * @param l The listener to add to the document.
 *
 * @since 1.35
 */
public static void addPropertyChangeListener(Document doc, PropertyChangeListener l) {
    PropertyChangeSupport pcs = (PropertyChangeSupport) doc.getProperty(PropertyChangeSupport.class);
    if (pcs != null) {
        pcs.addPropertyChangeListener(l);
    }
}