listenermanager.java
来自「PIY(Program It Yourself)是一个基于Java的应用程序开发」· Java 代码 · 共 52 行
JAVA
52 行
package piy;
import java.util.ArrayList;
import java.lang.reflect.*;
/**
* Handles the event listeners in the listeners package. Give this class an object,
* and it will tell you the listener objects that can be added to the object.
* @author David Vivash
* @version 1.0, 12/02/01
*/
public class ListenerManager {
private Class[] support = null;
public ListenerManager(Class[] support) {
this.support = support;
}
/**
* Gets the event listeners that can be attached to the specified object.
* @param o the object on which event listeners can be attached
* @return an array of PiyEventListener representing all of the listeners that
* can be attached
*/
public PiyEventListener[] getListeners(Object o) {
Class type = null;
ArrayList listeners = new ArrayList(4);
Method getSupportedClass = null;
try{
for (int i=0; i<support.length; i++) {
getSupportedClass = support[i].getMethod("getSupportedClass", null);
type = (Class)getSupportedClass.invoke(null, null);
if (type.isAssignableFrom(o.getClass())) listeners.add(support[i].newInstance());
}
} catch(Exception e) {
System.out.println("Unable to instantiate listener class " + e.getMessage());
}
PiyEventListener[] toReturn = new PiyEventListener[listeners.size()];
for (int i=0; i<toReturn.length; i++)
toReturn[i] = (PiyEventListener)listeners.get(i);
return toReturn;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?