OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / event / EventListenerList.java
index bde8b3c..1568039 100644 (file)
@@ -37,6 +37,9 @@ exception statement from your version. */
 
 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;
@@ -136,7 +139,7 @@ public class EventListenerList
    *
    * @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;
@@ -175,7 +178,7 @@ public class EventListenerList
    * <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)
@@ -224,7 +227,7 @@ public class EventListenerList
    *
    * @since 1.3
    */
-  public EventListener[] getListeners(Class c)
+  public <T extends EventListener> T[] getListeners(Class<T> c)
   {
     int count, f;
     EventListener[] result;
@@ -236,7 +239,7 @@ public class EventListenerList
       if (listenerList[i] == c)
         result[f++] = (EventListener) listenerList[i + 1];
     
-    return result;
+    return (T[]) result;
   }
 
 
@@ -253,7 +256,7 @@ public class EventListenerList
    *
    * @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;
@@ -304,4 +307,51 @@ public class EventListenerList
       }
     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);
+      }
+  }
 }