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);
}
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());
}
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());
}
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);
}
});
}
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"));
}
Aggregations