Search in sources :

Example 11 with VetoableChangeListener

use of java.beans.VetoableChangeListener in project commons-lang by apache.

the class EventListenerSupportTest method testEventDispatchOrder.

@Test
public void testEventDispatchOrder() throws PropertyVetoException {
    final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
    final List<VetoableChangeListener> calledListeners = new ArrayList<>();
    final VetoableChangeListener listener1 = createListener(calledListeners);
    final VetoableChangeListener listener2 = createListener(calledListeners);
    listenerSupport.addListener(listener1);
    listenerSupport.addListener(listener2);
    listenerSupport.fire().vetoableChange(new PropertyChangeEvent(new Date(), "Day", 4, 5));
    assertEquals(calledListeners.size(), 2);
    assertSame(calledListeners.get(0), listener1);
    assertSame(calledListeners.get(1), listener2);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) ArrayList(java.util.ArrayList) VetoableChangeListener(java.beans.VetoableChangeListener) Date(java.util.Date) Test(org.junit.Test)

Example 12 with VetoableChangeListener

use of java.beans.VetoableChangeListener in project commons-lang by apache.

the class EventListenerSupportTest method testGetListeners.

@Test
public void testGetListeners() {
    final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
    final VetoableChangeListener[] listeners = listenerSupport.getListeners();
    assertEquals(0, listeners.length);
    assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType());
    final VetoableChangeListener[] empty = listeners;
    // for fun, show that the same empty instance is used
    assertSame(empty, listenerSupport.getListeners());
    final VetoableChangeListener listener1 = EasyMock.createNiceMock(VetoableChangeListener.class);
    listenerSupport.addListener(listener1);
    assertEquals(1, listenerSupport.getListeners().length);
    final VetoableChangeListener listener2 = EasyMock.createNiceMock(VetoableChangeListener.class);
    listenerSupport.addListener(listener2);
    assertEquals(2, listenerSupport.getListeners().length);
    listenerSupport.removeListener(listener1);
    assertEquals(1, listenerSupport.getListeners().length);
    listenerSupport.removeListener(listener2);
    assertSame(empty, listenerSupport.getListeners());
}
Also used : VetoableChangeListener(java.beans.VetoableChangeListener) Test(org.junit.Test)

Example 13 with VetoableChangeListener

use of java.beans.VetoableChangeListener in project commons-lang by apache.

the class EventListenerSupportTest method testAddListenerNoDuplicates.

@Test
public void testAddListenerNoDuplicates() {
    final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
    final VetoableChangeListener[] listeners = listenerSupport.getListeners();
    assertEquals(0, listeners.length);
    assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType());
    final VetoableChangeListener[] empty = listeners;
    // for fun, show that the same empty instance is used
    assertSame(empty, listenerSupport.getListeners());
    final VetoableChangeListener listener1 = EasyMock.createNiceMock(VetoableChangeListener.class);
    listenerSupport.addListener(listener1);
    assertEquals(1, listenerSupport.getListeners().length);
    listenerSupport.addListener(listener1, false);
    assertEquals(1, listenerSupport.getListeners().length);
    listenerSupport.removeListener(listener1);
    assertSame(empty, listenerSupport.getListeners());
}
Also used : VetoableChangeListener(java.beans.VetoableChangeListener) Test(org.junit.Test)

Example 14 with VetoableChangeListener

use of java.beans.VetoableChangeListener in project chatty by chatty.

the class GuiUtil method installTextComponentFocusWorkaround.

/**
 * Java 8u161/162 introduced a bug that causes high CPU usage when a
 * JTextField/JTextArea is focused as first component after the window is
 * focused.
 *
 * This workaround aims to prevent this by rejecting that focus change if it
 * occurs and focusing another component first, then focusing the original
 * text component.
 *
 * This may or may not actually work, but it seemed fine in testing.
 */
public static void installTextComponentFocusWorkaround() {
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener(new VetoableChangeListener() {

        private boolean rejectNext = false;

        private JComponent target;

        @Override
        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
            if (evt.getNewValue() != null) {
                if (rejectNext && evt.getPropertyName().equals("focusOwner")) {
                    if (evt.getNewValue() instanceof JTextComponent) {
                        JComponent component = (JComponent) evt.getNewValue();
                        // Move focus up, this usually moves it to the
                        // window itself
                        KeyboardFocusManager.getCurrentKeyboardFocusManager().upFocusCycle(component);
                        target = component;
                        LOGGER.info("[Focus] Rejected JTextComponent focus");
                        // didn't seem to work
                        throw new PropertyVetoException("Rejected JTextComponent focus", evt);
                    } else {
                        // If anything else was focused, no need to reject
                        // anymore, change focus back if necessary
                        rejectNext = false;
                        if (target != null) {
                            LOGGER.info("[Focus] Temp: " + evt.getNewValue());
                            target.requestFocus();
                            target = null;
                        }
                    }
                } else if (evt.getPropertyName().equals("focusedWindow")) {
                    // Next focus on a text component should be rejected
                    LOGGER.info("[Focus] Window focused");
                    rejectNext = true;
                }
            }
            // Debug
            String oldV = evt.getOldValue() != null ? evt.getOldValue().getClass().toString() : null;
            String newV = evt.getNewValue() != null ? evt.getNewValue().getClass().toString() : null;
        // System.out.println(evt.getPropertyName()+": "+oldV+" -> "+newV);
        }
    });
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) PropertyChangeEvent(java.beans.PropertyChangeEvent) JComponent(javax.swing.JComponent) VetoableChangeListener(java.beans.VetoableChangeListener) JTextComponent(javax.swing.text.JTextComponent)

Example 15 with VetoableChangeListener

use of java.beans.VetoableChangeListener in project qi4j-sdk by Qi4j.

the class CircuitBreakerTest method GivenCBWhenTripCBWithExceptionsAndGetStatusWithFailureThenStatusIsOff.

@Test
public void GivenCBWhenTripCBWithExceptionsAndGetStatusWithFailureThenStatusIsOff() throws PropertyVetoException {
    assertThat(cb.status(), CoreMatchers.equalTo(CircuitBreaker.Status.on));
    cb.addVetoableChangeListener(new VetoableChangeListener() {

        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
            if (evt.getNewValue() == CircuitBreaker.Status.on) {
                throw new PropertyVetoException("Service is down", evt);
            }
        }
    });
    // Service levels goes down and causes a trip
    cb.throwable(new IOException());
    cb.throwable(new IOException());
    cb.throwable(new IOException());
    try {
        System.out.println("Wait...");
        Thread.sleep(300);
    } catch (InterruptedException e) {
    // Ignore
    }
    assertThat(cb.status(), CoreMatchers.equalTo(CircuitBreaker.Status.off));
    assertThat(cb.lastThrowable().getMessage(), CoreMatchers.equalTo("Service is down"));
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) PropertyChangeEvent(java.beans.PropertyChangeEvent) VetoableChangeListener(java.beans.VetoableChangeListener) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

VetoableChangeListener (java.beans.VetoableChangeListener)15 PropertyChangeEvent (java.beans.PropertyChangeEvent)9 Test (org.junit.Test)8 PropertyVetoException (java.beans.PropertyVetoException)4 VetoableChangeSupport (java.beans.VetoableChangeSupport)4 Date (java.util.Date)4 PropertyChangeListener (java.beans.PropertyChangeListener)2 ArrayList (java.util.ArrayList)2 Point (java.awt.Point)1 PropertyChangeSupport (java.beans.PropertyChangeSupport)1 VetoableChangeListenerProxy (java.beans.VetoableChangeListenerProxy)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 JComponent (javax.swing.JComponent)1 JTextComponent (javax.swing.text.JTextComponent)1