package javax.swing.event;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.EventListener;
*
* @throws NullPointerException if <code>t</code> is <code>null</code>.
*/
- public void add(Class t, EventListener listener)
+ public <T extends EventListener> void add(Class<T> t, T listener)
{
int oldLength;
Object[] newList;
* <code>t</code>. Thus, subclasses of <code>t</code> will not be
* counted.
*/
- public int getListenerCount(Class t)
+ public int getListenerCount(Class<?> t)
{
int result = 0;
for (int i = 0; i < listenerList.length; i += 2)
*
* @since 1.3
*/
- public EventListener[] getListeners(Class c)
+ public <T extends EventListener> T[] getListeners(Class<T> c)
{
int count, f;
EventListener[] result;
if (listenerList[i] == c)
result[f++] = (EventListener) listenerList[i + 1];
- return result;
+ return (T[]) result;
}
*
* @throws NullPointerException if <code>t</code> is <code>null</code>.
*/
- public void remove(Class t, EventListener listener)
+ public <T extends EventListener> void remove(Class<T> t, T listener)
{
Object[] oldList, newList;
int oldLength;
}
return buf.toString();
}
+
+ /**
+ * Serializes an instance to an ObjectOutputStream.
+ *
+ * @param out the stream to serialize to
+ *
+ * @throws IOException if something goes wrong
+ */
+ private void writeObject(ObjectOutputStream out)
+ throws IOException
+ {
+ out.defaultWriteObject();
+ for (int i = 0; i < listenerList.length; i += 2)
+ {
+ Class cl = (Class) listenerList[i];
+ EventListener l = (EventListener) listenerList[i + 1];
+ if (l != null && l instanceof Serializable)
+ {
+ out.writeObject(cl.getName());
+ out.writeObject(l);
+ }
+ }
+ // Write end marker.
+ out.writeObject(null);
+ }
+
+ /**
+ * Deserializes an instance from an ObjectInputStream.
+ *
+ * @param in the input stream
+ *
+ * @throws ClassNotFoundException if a serialized class can't be found
+ * @throws IOException if something goes wrong
+ */
+ private <T extends EventListener> void readObject(ObjectInputStream in)
+ throws ClassNotFoundException, IOException
+ {
+ listenerList = NO_LISTENERS;
+ in.defaultReadObject();
+ Object type;
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ while ((type = in.readObject()) != null)
+ {
+ EventListener l = (EventListener) in.readObject();
+ add(((Class<T>) Class.forName((String) type, true, cl)), (T) l);
+ }
+ }
}