Java Code Examples for org.openide.util.ChangeSupport#addChangeListener()

The following examples show how to use org.openide.util.ChangeSupport#addChangeListener() . 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: MockChangeListenerTest.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();
    cs = new ChangeSupport(source);
    l = new MockChangeListener();
    cs.addChangeListener(l);
}
 
Example 2
Source File: MockChangeListenerTest.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();
    cs = new ChangeSupport(source);
    l = new MockChangeListener();
    cs.addChangeListener(l);
}
 
Example 3
Source File: ChangeSupportTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testChangeSupport() {
    final int[] changeCount = { 0 };
    ChangeSupport support = new ChangeSupport(this);
    MockChangeListener listener1 = new MockChangeListener(), listener2 = new MockChangeListener();

    support.addChangeListener(null);
    assertFalse(support.hasListeners());

    support.removeChangeListener(null);
    assertFalse(support.hasListeners());

    support.addChangeListener(listener1);
    support.addChangeListener(listener2);
    assertTrue(support.hasListeners());
    Set<ChangeListener> listeners = new HashSet<ChangeListener>(support.listeners);
    assertEquals(2, listeners.size());
    assertTrue(listeners.contains(listener1));
    assertTrue(listeners.contains(listener2));

    support.fireChange();
    List<ChangeEvent> events = listener1.allEvents();
    assertEquals(1, events.size());
    listener2.assertEventCount(1);
    assertSame(this, events.iterator().next().getSource());

    support.removeChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
        }
    });
    support.fireChange();
    listener1.assertEventCount(1);
    listener2.assertEventCount(1);

    support.removeChangeListener(listener1);
    support.fireChange();
    listener1.assertEventCount(0);
    listener2.assertEventCount(1);

    support.addChangeListener(listener2);
    support.fireChange();
    listener2.assertEventCount(2);

    support.removeChangeListener(listener2);
    support.fireChange();
    listener2.assertEventCount(1);

    support.removeChangeListener(listener2);
    support.fireChange();
    listener2.assertEventCount(0);
    assertFalse(support.hasListeners());
}