下面列出了怎么用java.beans.VetoableChangeListener的API类实例代码及写法,或者点击链接到github查看源代码。
public void testGetListeners() {
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
VetoableChangeListener[] listeners = listenerSupport.getListeners();
assertEquals(0, listeners.length);
assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType());
VetoableChangeListener[] empty = listeners;
//for fun, show that the same empty instance is used
assertSame(empty, listenerSupport.getListeners());
VetoableChangeListener listener1 = EasyMock.createNiceMock(VetoableChangeListener.class);
listenerSupport.addListener(listener1);
assertEquals(1, listenerSupport.getListeners().length);
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());
}
/**
* Gets the VetoableChangeListener
* (if any) of the specified child
* @param child the specified child
* @return the VetoableChangeListener (if any) of the specified child
*/
protected static final VetoableChangeListener getChildVetoableChangeListener(Object child) {
try {
return (VetoableChangeListener)child;
} catch (ClassCastException cce) {
return null;
}
}
/**
* protected method called from constructor and readObject to initialize
* transient state of BeanContextSupport instance.
*
* This class uses this method to instantiate inner class listeners used
* to monitor PropertyChange and VetoableChange events on children.
*
* subclasses may envelope this method to add their own initialization
* behavior
*/
protected synchronized void initialize() {
children = new HashMap(serializable + 1);
bcmListeners = new ArrayList(1);
childPCL = new PropertyChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* property changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
childVCL = new VetoableChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* vetoable changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
BeanContextSupport.this.vetoableChange(pce);
}
};
}
private static void check(VetoableChangeSupport vcs) {
VetoableChangeListener[] namedListeners = vcs.getVetoableChangeListeners(NAME);
check(namedListeners, 1);
check(namedListeners[0], 1);
VetoableChangeListener[] allListeners = vcs.getVetoableChangeListeners();
check(allListeners, 2);
check(allListeners[0], 0);
check(allListeners[1], 1, NAME);
}
public void testAddEventListenerWithPrivateAddMethod()
{
final PropertyChangeSource src = new PropertyChangeSource();
EventCountingInvociationHandler handler = new EventCountingInvociationHandler();
VetoableChangeListener listener = handler.createListener(VetoableChangeListener.class);
try
{
EventUtils.addEventListener(src, VetoableChangeListener.class, listener);
fail("Should not be allowed to add a listener to an object that doesn't support it.");
}
catch (IllegalArgumentException e)
{
assertEquals("Class " + src.getClass().getName() + " does not have a public add" + VetoableChangeListener.class.getSimpleName() + " method which takes a parameter of type " + VetoableChangeListener.class.getName() + ".", e.getMessage());
}
}
/**
* Maps
* {@code JComponent.removeVetoableChangeListener(VetoableChangeListener)}
* through queue
*/
public void removeVetoableChangeListener(final VetoableChangeListener vetoableChangeListener) {
runMapping(new MapVoidAction("removeVetoableChangeListener") {
@Override
public void map() {
((JComponent) getSource()).removeVetoableChangeListener(vetoableChangeListener);
}
});
}
@Test
public void testEventDispatchOrder() throws PropertyVetoException
{
final EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
final List<VetoableChangeListener> calledListeners = new ArrayList<VetoableChangeListener>();
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);
}
/** Adds listener for the veto of property change.
* @param listener the listener
*/
public final void addVetoableChangeListener(VetoableChangeListener listener) {
synchronized (internLock) {
if (vetoableChangeList == null) {
vetoableChangeList = new ListenerList<VetoableChangeListener>();
}
vetoableChangeList.add(listener);
}
}
/**
* protected method called from constructor and readObject to initialize
* transient state of BeanContextSupport instance.
*
* This class uses this method to instantiate inner class listeners used
* to monitor PropertyChange and VetoableChange events on children.
*
* subclasses may envelope this method to add their own initialization
* behavior
*/
protected synchronized void initialize() {
children = new HashMap(serializable + 1);
bcmListeners = new ArrayList(1);
childPCL = new PropertyChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* property changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
childVCL = new VetoableChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* vetoable changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
BeanContextSupport.this.vetoableChange(pce);
}
};
}
/**
* protected method called from constructor and readObject to initialize
* transient state of BeanContextSupport instance.
*
* This class uses this method to instantiate inner class listeners used
* to monitor PropertyChange and VetoableChange events on children.
*
* subclasses may envelope this method to add their own initialization
* behavior
*/
protected synchronized void initialize() {
children = new HashMap(serializable + 1);
bcmListeners = new ArrayList(1);
childPCL = new PropertyChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* property changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
childVCL = new VetoableChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* vetoable changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
BeanContextSupport.this.vetoableChange(pce);
}
};
}
private void addDeregisterListener(final EventListenerSupport<VetoableChangeListener> listenerSupport)
{
listenerSupport.addListener(new VetoableChangeListener()
{
@Override
public void vetoableChange(PropertyChangeEvent e)
{
listenerSupport.removeListener(this);
}
});
}
private static void check(VetoableChangeSupport vcs) {
VetoableChangeListener[] namedListeners = vcs.getVetoableChangeListeners(NAME);
check(namedListeners, 1);
check(namedListeners[0], 1);
VetoableChangeListener[] allListeners = vcs.getVetoableChangeListeners();
check(allListeners, 2);
check(allListeners[0], 0);
check(allListeners[1], 1, NAME);
}
/**
* Remove an occurrence of a VetoableChangeListener from the listener list.
* It removes at most one occurrence of the given listener.
* If the listener was added multiple times it must be removed
* mulitple times.
* This removes a VetoableChangeListener that was registered
* for all properties, and has no effect if registered for only
* one or more specified properties.
*
* @param listener The VetoableChangeListener to be removed
*/
public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {
int newlen = listeners.length-1;
if (newlen < 0 || listener == null) return;
// Copy while searching for element to remove
VetoableChangeListener[] newArray = new VetoableChangeListener[newlen];
for (int i = 0; i < newlen; ++i) {
if (listener.equals(listeners[i])) {
// copy remaining and exit
for (int k = i + 1; k <= newlen; ++k) newArray[k-1] = listeners[k];
listeners = newArray;
return;
}
else
newArray[i] = listeners[i];
}
// special-case last cell
if (listener.equals(listeners[newlen]))
listeners = newArray;
}
public void testSubclassInvocationHandling() throws PropertyVetoException {
@SuppressWarnings("serial")
EventListenerSupport<VetoableChangeListener> eventListenerSupport = new EventListenerSupport<VetoableChangeListener>(
VetoableChangeListener.class) {
protected java.lang.reflect.InvocationHandler createInvocationHandler() {
return new ProxyInvocationHandler() {
/**
* {@inheritDoc}
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
return "vetoableChange".equals(method.getName())
&& "Hour".equals(((PropertyChangeEvent) args[0]).getPropertyName()) ? null
: super.invoke(proxy, method, args);
}
};
};
};
VetoableChangeListener listener = EasyMock.createNiceMock(VetoableChangeListener.class);
eventListenerSupport.addListener(listener);
Object source = new Date();
PropertyChangeEvent ignore = new PropertyChangeEvent(source, "Hour", 5, 6);
PropertyChangeEvent respond = new PropertyChangeEvent(source, "Day", 6, 7);
listener.vetoableChange(respond);
EasyMock.replay(listener);
eventListenerSupport.fire().vetoableChange(ignore);
eventListenerSupport.fire().vetoableChange(respond);
EasyMock.verify(listener);
}
/**
* protected method called from constructor and readObject to initialize
* transient state of BeanContextSupport instance.
*
* This class uses this method to instantiate inner class listeners used
* to monitor PropertyChange and VetoableChange events on children.
*
* subclasses may envelope this method to add their own initialization
* behavior
*/
protected synchronized void initialize() {
children = new HashMap(serializable + 1);
bcmListeners = new ArrayList(1);
childPCL = new PropertyChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* property changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
childVCL = new VetoableChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* vetoable changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
BeanContextSupport.this.vetoableChange(pce);
}
};
}
/**
* Remove an occurrence of a VetoableChangeListener from the listener list.
* It removes at most one occurrence of the given listener.
* If the listener was added multiple times it must be removed
* mulitple times.
* This removes a VetoableChangeListener that was registered
* for all properties, and has no effect if registered for only
* one or more specified properties.
*
* @param listener The VetoableChangeListener to be removed
*/
public synchronized void removeVetoableChangeListener(VetoableChangeListener listener) {
int newlen = listeners.length-1;
if (newlen < 0 || listener == null) return;
// Copy while searching for element to remove
VetoableChangeListener[] newArray = new VetoableChangeListener[newlen];
for (int i = 0; i < newlen; ++i) {
if (listener.equals(listeners[i])) {
// copy remaining and exit
for (int k = i + 1; k <= newlen; ++k) newArray[k-1] = listeners[k];
listeners = newArray;
return;
}
else
newArray[i] = listeners[i];
}
// special-case last cell
if (listener.equals(listeners[newlen]))
listeners = newArray;
}
private static void check(VetoableChangeSupport vcs) {
VetoableChangeListener[] namedListeners = vcs.getVetoableChangeListeners(NAME);
check(namedListeners, 1);
check(namedListeners[0], 1);
VetoableChangeListener[] allListeners = vcs.getVetoableChangeListeners();
check(allListeners, 2);
check(allListeners[0], 0);
check(allListeners[1], 1, NAME);
}
@Test
public void testSerialization() throws IOException, ClassNotFoundException, PropertyVetoException {
EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
listenerSupport.addListener(new VetoableChangeListener() {
@Override
public void vetoableChange(PropertyChangeEvent e) {
}
});
listenerSupport.addListener(EasyMock.createNiceMock(VetoableChangeListener.class));
//serialize:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(listenerSupport);
objectOutputStream.close();
//deserialize:
@SuppressWarnings("unchecked")
EventListenerSupport<VetoableChangeListener> deserializedListenerSupport = (EventListenerSupport<VetoableChangeListener>) new ObjectInputStream(
new ByteArrayInputStream(outputStream.toByteArray())).readObject();
//make sure we get a listener array back, of the correct component type, and that it contains only the serializable mock
VetoableChangeListener[] listeners = deserializedListenerSupport.getListeners();
assertEquals(VetoableChangeListener.class, listeners.getClass().getComponentType());
assertEquals(1, listeners.length);
//now verify that the mock still receives events; we can infer that the proxy was correctly reconstituted
VetoableChangeListener listener = listeners[0];
PropertyChangeEvent evt = new PropertyChangeEvent(new Date(), "Day", 7, 9);
listener.vetoableChange(evt);
EasyMock.replay(listener);
deserializedListenerSupport.fire().vetoableChange(evt);
EasyMock.verify(listener);
//remove listener and verify we get an empty array of listeners
deserializedListenerSupport.removeListener(listener);
assertEquals(0, deserializedListenerSupport.getListeners().length);
}
/**
* protected method called from constructor and readObject to initialize
* transient state of BeanContextSupport instance.
*
* This class uses this method to instantiate inner class listeners used
* to monitor PropertyChange and VetoableChange events on children.
*
* subclasses may envelope this method to add their own initialization
* behavior
*/
protected synchronized void initialize() {
children = new HashMap(serializable + 1);
bcmListeners = new ArrayList(1);
childPCL = new PropertyChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* property changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void propertyChange(PropertyChangeEvent pce) {
BeanContextSupport.this.propertyChange(pce);
}
};
childVCL = new VetoableChangeListener() {
/*
* this adaptor is used by the BeanContextSupport class to forward
* vetoable changes from a child to the BeanContext, avoiding
* accidential serialization of the BeanContext by a badly
* behaved Serializable child.
*/
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
BeanContextSupport.this.vetoableChange(pce);
}
};
}
/**
* Gets the VetoableChangeListener
* (if any) of the specified child
* @param child the specified child
* @return the VetoableChangeListener (if any) of the specified child
*/
protected static final VetoableChangeListener getChildVetoableChangeListener(Object child) {
try {
return (VetoableChangeListener)child;
} catch (ClassCastException cce) {
return null;
}
}
private static void check(VetoableChangeSupport vcs) {
VetoableChangeListener[] namedListeners = vcs.getVetoableChangeListeners(NAME);
check(namedListeners, 1);
check(namedListeners[0], 1);
VetoableChangeListener[] allListeners = vcs.getVetoableChangeListeners();
check(allListeners, 2);
check(allListeners[0], 0);
check(allListeners[1], 1, NAME);
}
/** Removes veto listener.
*/
@Override
public void removeVetoableChangeListener(VetoableChangeListener l) {
}
public synchronized void addVetoableChangeListener(VetoableChangeListener l) {
assertNull ("This is the first veto listener", vetoL);
vetoL = l;
}
public static void removeVetoableChangeListener(InputOutput io, VetoableChangeListener listener ) {
IONotifier ion = find(io);
if (ion != null)
ion.removeVetoableChangeListener(listener);
}
/** Remove a veto listener from all instances of this exact class.
* @param list the listener to remove
*/
public final void removeVetoableChangeListener(VetoableChangeListener list) {
synchronized (getLock()) {
getVeto().remove(list);
}
}
public void removeVetoableChangeListener(VetoableChangeListener l) {
assertEquals ("Removing the right veto one", vetoL, l);
vetoL = null;
}
public final void removeVetoableChangeListener(VetoableChangeListener listener) {
this.vcs.removeVetoableChangeListener(listener);
}
public void removeVetoableChangeListener(VetoableChangeListener l) {
assertEquals ("Removing the right veto one", vetoL, l);
vetoL = null;
}
@Test(expected=NullPointerException.class)
public void testRemoveNullListener()
{
EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
listenerSupport.removeListener(null);
}
public void removeVetoableChangeListener(VetoableChangeListener l) {
assertEquals ("Removing the right veto one", vetoL, l);
vetoL = null;
}