/** * Binds an event listener to a specific method on a specific object. * * @param <L> the event listener type * @param target the target object * @param methodName the name of the method to be called * @param eventSource the object which is generating events (JButton, JList, etc.) * @param listenerType the listener interface (ActionListener.class, SelectionListener.class, etc.) * @param eventTypes the event types (method names) from the listener interface (if none specified, all will be * supported) */ public static <L> void bindEventsToMethod(final Object target, final String methodName, final Object eventSource, final Class<L> listenerType, final String... eventTypes) { final L listener = listenerType.cast(Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerType }, new EventBindingInvocationHandler(target, methodName, eventTypes))); addEventListener(eventSource, listenerType, listener); }
@Test public void testAddEventListenerWithNoAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final ObjectChangeListener listener = handler.createListener(ObjectChangeListener.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Class " + src.getClass().getName() + " does not have a public add" + ObjectChangeListener.class.getSimpleName() + " method which takes a parameter of type " + ObjectChangeListener.class.getName() + "."); EventUtils.addEventListener(src, ObjectChangeListener.class, listener); }
@Test public void testAddEventListenerWithPrivateAddMethod() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Class " + src.getClass().getName() + " does not have a public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + "."); EventUtils.addEventListener(src, VetoableChangeListener.class, listener); }
@Test public void testAddEventListenerThrowsException() { final ExceptionEventSource src = new ExceptionEventSource(); expectedException.expect(RuntimeException.class); EventUtils.addEventListener(src, PropertyChangeListener.class, new PropertyChangeListener() { @Override public void propertyChange(final PropertyChangeEvent e) { // Do nothing! } }); }
@Test public void testAddEventListener() { final PropertyChangeSource src = new PropertyChangeSource(); final EventCountingInvociationHandler handler = new EventCountingInvociationHandler(); final PropertyChangeListener listener = handler.createListener(PropertyChangeListener.class); assertEquals(0, handler.getEventCount("propertyChange")); EventUtils.addEventListener(src, PropertyChangeListener.class, listener); assertEquals(0, handler.getEventCount("propertyChange")); src.setProperty("newValue"); assertEquals(1, handler.getEventCount("propertyChange")); }
/** * Binds an event listener to a specific method on a specific object. * * @param <L> the event listener type * @param target the target object * @param methodName the name of the method to be called * @param eventSource the object which is generating events (JButton, JList, etc.) * @param listenerType the listener interface (ActionListener.class, SelectionListener.class, etc.) * @param eventTypes the event types (method names) from the listener interface (if none specified, all will be * supported) */ public static <L> void bindEventsToMethod(final Object target, final String methodName, final Object eventSource, final Class<L> listenerType, final String... eventTypes) { final L listener = listenerType.cast(Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerType }, new EventBindingInvocationHandler(target, methodName, eventTypes))); addEventListener(eventSource, listenerType, listener); }
/** * Binds an event listener to a specific method on a specific object. * * @param <L> the event listener type * @param target the target object * @param methodName the name of the method to be called * @param eventSource the object which is generating events (JButton, JList, etc.) * @param listenerType the listener interface (ActionListener.class, SelectionListener.class, etc.) * @param eventTypes the event types (method names) from the listener interface (if none specified, all will be * supported) */ public static <L> void bindEventsToMethod(final Object target, final String methodName, final Object eventSource, final Class<L> listenerType, final String... eventTypes) { final L listener = listenerType.cast(Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerType }, new EventBindingInvocationHandler(target, methodName, eventTypes))); addEventListener(eventSource, listenerType, listener); }
/** * Binds an event listener to a specific method on a specific object. * * @param <L> the event listener type * @param target the target object * @param methodName the name of the method to be called * @param eventSource the object which is generating events (JButton, JList, etc.) * @param listenerType the listener interface (ActionListener.class, SelectionListener.class, etc.) * @param eventTypes the event types (method names) from the listener interface (if none specified, all will be * supported) */ public static <L> void bindEventsToMethod(final Object target, final String methodName, final Object eventSource, final Class<L> listenerType, final String... eventTypes) { final L listener = listenerType.cast(Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] { listenerType }, new EventBindingInvocationHandler(target, methodName, eventTypes))); addEventListener(eventSource, listenerType, listener); }