下面列出了怎么用java.util.EventListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Removes a listener that is equal to the given one from this aggregate.
* <code>equals()</code> method is used to compare listeners.
*
* @param listener the listener to be removed
*
* @return <code>true</code> if this aggregate contained the specified
* <code>listener</code>; <code>false</code> otherwise
*
* @throws ClassCastException if <code>listener</code> is not
* an instatce of <code>listenerClass</code> specified
* in the constructor
*/
public synchronized boolean remove(EventListener listener) {
Class<?> listenerClass = getListenerClass();
if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
throw new ClassCastException("listener " + listener + " is not " +
"an instance of listener class " + listenerClass);
}
for (int i = 0; i < listenerList.length; i++) {
if (listenerList[i].equals(listener)) {
EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass,
listenerList.length - 1);
System.arraycopy(listenerList, 0, tmp, 0, i);
System.arraycopy(listenerList, i + 1, tmp, i, listenerList.length - i - 1);
listenerList = tmp;
return true;
}
}
return false;
}
/**
* Checks that an XYPlot deregisters listeners when clearing markers.
*/
@Test
public void testListenersWithXYPlot() {
XYPlot plot = new XYPlot();
ValueMarker marker1 = new ValueMarker(1.0);
ValueMarker marker2 = new ValueMarker(2.0);
plot.addDomainMarker(marker1);
plot.addRangeMarker(marker2);
EventListener[] listeners1 = marker1.getListeners(
MarkerChangeListener.class);
assertTrue(Arrays.asList(listeners1).contains(plot));
EventListener[] listeners2 = marker1.getListeners(
MarkerChangeListener.class);
assertTrue(Arrays.asList(listeners2).contains(plot));
plot.clearDomainMarkers();
plot.clearRangeMarkers();
listeners1 = marker1.getListeners(MarkerChangeListener.class);
assertFalse(Arrays.asList(listeners1).contains(plot));
listeners2 = marker1.getListeners(MarkerChangeListener.class);
assertFalse(Arrays.asList(listeners2).contains(plot));
}
@Override
public void addListener(final Class<? extends EventListener> listenerClass) {
ensureNotInitialized();
ensureNotProgramaticListener();
if (ApplicationListeners.listenerState() != NO_LISTENER
&& ServletContextListener.class.isAssignableFrom(listenerClass)) {
throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
}
InstanceFactory<? extends EventListener> factory = null;
try {
factory = deploymentInfo.getClassIntrospecter().createInstanceFactory(listenerClass);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
final ListenerInfo listener = new ListenerInfo(listenerClass, factory);
deploymentInfo.addListener(listener);
deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
}
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
if (SecurityUtil.isPackageProtectionEnabled()) {
doPrivileged("addListener",
new Class[]{Class.class},
new Object[]{listenerClass});
} else {
context.addListener(listenerClass);
}
}
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
EventListener listener;
try {
listener = createListener(listenerClass);
} catch (ServletException e) {
throw new IllegalArgumentException(sm.getString(
"applicationContext.addListener.iae.init",
listenerClass.getName()), e);
}
addListener(listener);
}
@Override
public <T extends EventListener> void addListener(T t) {
if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
throw new IllegalStateException(
sm.getString("applicationContext.addListener.ise",
getContextPath()));
}
boolean match = false;
if (t instanceof ServletContextAttributeListener ||
t instanceof ServletRequestListener ||
t instanceof ServletRequestAttributeListener ||
t instanceof HttpSessionIdListener ||
t instanceof HttpSessionAttributeListener) {
context.addApplicationEventListener(t);
match = true;
}
if (t instanceof HttpSessionListener ||
(t instanceof ServletContextListener && newServletContextListenerAllowed)) {
// Add listener directly to the list of instances rather than to
// the list of class names.
context.addApplicationLifecycleListener(t);
match = true;
}
if (match) return;
if (t instanceof ServletContextListener) {
throw new IllegalArgumentException(sm.getString(
"applicationContext.addListener.iae.sclNotAllowed",
t.getClass().getName()));
} else {
throw new IllegalArgumentException(sm.getString(
"applicationContext.addListener.iae.wrongType",
t.getClass().getName()));
}
}
private static int getListenerCount(EventListener l, Class<?> listenerType) {
if (l instanceof AWTEventMulticaster) {
AWTEventMulticaster mc = (AWTEventMulticaster)l;
return getListenerCount(mc.a, listenerType) +
getListenerCount(mc.b, listenerType);
}
else {
// Only count listeners of correct type
return listenerType.isInstance(l) ? 1 : 0;
}
}
public void clear() {
for (EventListener listener : listeners) {
if (listener instanceof Closeable) {
((Closeable) listener).close();
}
}
this.listeners.clear();
}
/**
* Adds the listener to this aggregate.
*
* @param listener the listener to be added
*
* @throws ClassCastException if <code>listener</code> is not
* an instatce of <code>listenerClass</code> specified
* in the constructor
*/
public synchronized void add(EventListener listener) {
Class<?> listenerClass = getListenerClass();
if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
throw new ClassCastException("listener " + listener + " is not " +
"an instance of listener class " + listenerClass);
}
EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
tmp[listenerList.length] = listener;
listenerList = tmp;
}
public ListenerInfo(final Class<? extends EventListener> listenerClass, final InstanceFactory<? extends EventListener> instanceFactory, boolean programatic) {
this.listenerClass = listenerClass;
this.instanceFactory = instanceFactory;
this.programatic = programatic;
if(!ApplicationListeners.isListenerClass(listenerClass)) {
throw UndertowServletMessages.MESSAGES.listenerMustImplementListenerClass(listenerClass);
}
}
public ListenerInfo(final Class<? extends EventListener> listenerClass, boolean programatic) {
this.listenerClass = listenerClass;
this.programatic = programatic;
try {
final Constructor<EventListener> ctor = (Constructor<EventListener>) listenerClass.getDeclaredConstructor();
ctor.setAccessible(true);
this.instanceFactory = new ConstructorInstanceFactory<>(ctor);
} catch (NoSuchMethodException e) {
throw UndertowServletMessages.MESSAGES.componentMustHaveDefaultConstructor("Listener", listenerClass);
}
}
@Override
public <T extends EventListener> void addListener(final T t) {
ensureNotInitialized();
ensureNotProgramaticListener();
if (ApplicationListeners.listenerState() != NO_LISTENER
&& ServletContextListener.class.isAssignableFrom(t.getClass())) {
throw UndertowServletMessages.MESSAGES.cannotAddServletContextListener();
}
ListenerInfo listener = new ListenerInfo(t.getClass(), new ImmediateInstanceFactory<EventListener>(t));
deploymentInfo.addListener(listener);
deployment.getApplicationListeners().addListener(new ManagedListener(listener, true));
}
@Override
public <T extends EventListener> T createListener(final Class<T> clazz) throws ServletException {
ensureNotProgramaticListener();
if (!ApplicationListeners.isListenerClass(clazz)) {
throw UndertowServletMessages.MESSAGES.listenerMustImplementListenerClass(clazz);
}
try {
return deploymentInfo.getClassIntrospecter().createInstanceFactory(clazz).createInstance().getInstance();
} catch (Exception e) {
throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(clazz.getName(), e);
}
}
protected static EventListener removeInternal(EventListener l, EventListener oldl) {
if (l == oldl || l == null) {
return null;
} else if (l instanceof TopLevelWindowMulticaster) {
return ((TopLevelWindowMulticaster)l).remove(oldl);
} else {
return l; // it's not here
}
}
private static int populateListenerArray(EventListener[] a, EventListener l, int index) {
if (l instanceof AWTEventMulticaster) {
AWTEventMulticaster mc = (AWTEventMulticaster)l;
int lhs = populateListenerArray(a, mc.a, index);
return populateListenerArray(a, mc.b, lhs);
}
else if (a.getClass().getComponentType().isInstance(l)) {
a[index] = l;
return index + 1;
}
// Skip nulls, instances of wrong class
else {
return index;
}
}
protected static void save(ObjectOutputStream s, String k, EventListener l) throws IOException {
if (l == null) {
return;
}
else if (l instanceof AWTEventMulticaster) {
((AWTEventMulticaster)l).saveInternal(s, k);
}
else if (l instanceof Serializable) {
s.writeObject(k);
s.writeObject(l);
}
}
/**
* Removes a listener from this multicaster and returns the
* resulting multicast listener.
* @param oldl the listener to be removed
*/
protected EventListener remove(EventListener oldl) {
if (oldl == a) return b;
if (oldl == b) return a;
EventListener a2 = removeInternal(a, oldl);
EventListener b2 = removeInternal(b, oldl);
if (a2 == a && b2 == b) {
return this; // it's not here
}
return addInternal(a2, b2);
}
private Collection<? extends EventListener> getListeners(ListnerHolder.Type type) {
if ( listeners.isEmpty()) {
return Collections.emptySet();
}
Collection<EventListener> l = new ArrayList<EventListener>();
for (ListnerHolder listnerHolder : listeners ) {
if (listnerHolder.type == type) {
l.add( listnerHolder.listener );
}
}
return l;
}
/**
* Adds the listener to this aggregate.
*
* @param listener the listener to be added
*
* @throws ClassCastException if <code>listener</code> is not
* an instatce of <code>listenerClass</code> specified
* in the constructor
*/
public synchronized void add(EventListener listener) {
Class<?> listenerClass = getListenerClass();
if (!listenerClass.isInstance(listener)) { // null is not an instance of any class
throw new ClassCastException("listener " + listener + " is not " +
"an instance of listener class " + listenerClass);
}
EventListener[] tmp = (EventListener[])Array.newInstance(listenerClass, listenerList.length + 1);
System.arraycopy(listenerList, 0, tmp, 0, listenerList.length);
tmp[listenerList.length] = listener;
listenerList = tmp;
}
protected static EventListener removeInternal(EventListener l, EventListener oldl) {
if (l == oldl || l == null) {
return null;
} else if (l instanceof GUIInitializedMulticaster) {
return ((GUIInitializedMulticaster)l).remove(oldl);
} else {
return l; // it's not here
}
}
protected static EventListener removeInternal(EventListener l, EventListener oldl) {
if (l == oldl || l == null) {
return null;
} else if (l instanceof TopLevelWindowMulticaster) {
return ((TopLevelWindowMulticaster)l).remove(oldl);
} else {
return l; // it's not here
}
}
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
throw new UnsupportedOperationException();
}
@Override
public <T extends EventListener> T createListener(Class<T> c)
throws ServletException {
return null;
}
@Override
public <T extends EventListener> void addListener(T t) {
throw new RuntimeException("Not implemented");
}
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
throw new RuntimeException("Not implemented");
}
@Override
public <T extends EventListener> T createListener(Class<T> c)
throws ServletException {
throw new RuntimeException("Not implemented");
}
/**
* Hook an SWT listener on the canvas where the chart is drawn.
* The purpose of this method is to allow some degree of customization.
*
* @param listener The SWT listener to attach to the canvas.
*/
public void addSWTListener(EventListener listener) {
if (listener instanceof ControlListener) {
this.canvas.addControlListener((ControlListener) listener);
}
else if (listener instanceof DisposeListener) {
this.canvas.addDisposeListener((DisposeListener) listener);
// }
// else if (listener instanceof DragDetectListener) {
// this.canvas.addDragDetectListener((DragDetectListener) listener);
}
else if (listener instanceof FocusListener) {
this.canvas.addFocusListener((FocusListener) listener);
}
else if (listener instanceof HelpListener) {
this.canvas.addHelpListener((HelpListener) listener);
}
else if (listener instanceof KeyListener) {
this.canvas.addKeyListener((KeyListener) listener);
// }
// else if (listener instanceof MenuDetectListener) {
// this.canvas.addMenuDetectListener((MenuDetectListener) listener);
}
else if (listener instanceof MouseListener) {
this.canvas.addMouseListener((MouseListener) listener);
}
else if (listener instanceof MouseMoveListener) {
this.canvas.addMouseMoveListener((MouseMoveListener) listener);
}
else if (listener instanceof MouseTrackListener) {
this.canvas.addMouseTrackListener((MouseTrackListener) listener);
// } else if (listener instanceof MouseWheelListener) {
// this.canvas.addMouseWheelListener((MouseWheelListener) listener);
}
else if (listener instanceof PaintListener) {
this.canvas.addPaintListener((PaintListener) listener);
}
else if (listener instanceof TraverseListener) {
this.canvas.addTraverseListener((TraverseListener) listener);
}
}
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
throw new UnsupportedOperationException();
}
protected TopLevelWindowMulticaster(EventListener a, EventListener b) {
super(a, b);
}
@Override
public <T extends EventListener> void addListener(T t) {
if (t instanceof ServletContextListener) {
((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
}
}