KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > event > EventListenerList


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: EventListenerList.java,v $
11    Revision 1.7 2004/05/05 12:43:22 bobintetley
12    Patches/new files from Laurent Martell
13
14    Revision 1.6 2004/04/16 10:19:07 dannaab
15    Misc bug fixes, InputMap implementation, preliminary undo support
16
17    Revision 1.5 2004/03/30 10:42:47 bobintetley
18    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
19
20    Revision 1.4 2003/12/14 09:13:38 bobintetley
21    Added CVS log to source headers
22
23 */

24
25 package swingwtx.swing.event;
26
27 import java.lang.reflect.Array JavaDoc;
28 import java.util.EventListener JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 public class EventListenerList {
32     
33     protected Vector JavaDoc listenerList = new Vector JavaDoc();
34
35     public Object JavaDoc[] getListenerList() {
36     return listenerList.toArray();
37     }
38
39     public synchronized void add(Class JavaDoc t, EventListener JavaDoc l) {
40         listenerList.add(l);
41     }
42
43     public synchronized void remove(Class JavaDoc t, EventListener JavaDoc l) {
44
45         listenerList.remove(l);
46     }
47
48     /*
49     public synchronized void add(Class t, EventListener l) {
50         Iterator iterator = listenerList.iterator();
51         while (iterator.hasNext())
52         {
53             Class listenerClass = (Class) iterator.next();
54             EventListener eventListener = (EventListener) iterator.next();
55             if (listenerClass == t && eventListener == l)
56             {
57                 iterator.remove();
58                 listenerList
59             }
60
61         }
62         for (int i = 0; i < listeners.length; i +=2 ) {
63             if ( listeners[i] == t &&
64                  listeners[i+1] == l )
65             {
66
67             }
68         }
69         listenerList.add(t);
70         listenerList.add(l);
71     }
72
73     public synchronized void remove(Class t, EventListener l) {
74         int index = listenerList.indexOf(l);
75         listenerList.remove(index);
76         listenerList.remove(index);
77     }
78     */

79
80     public EventListener JavaDoc[] getListeners(Class JavaDoc t) {
81         int n = getListenerCount(t);
82         EventListener JavaDoc[] listeners = (EventListener JavaDoc[])Array.newInstance(t, n);
83         int listenersIndex = 0;
84         for (int i = listenerList.size() -2; i>=0; i-=2) {
85             if (listenerList.get(i) == t) {
86                 listeners[listenersIndex] = listeners[i+1];
87             listenersIndex++;
88             }
89         }
90         return listeners;
91     }
92
93     public int getListenerCount() {
94     return listenerList.size();
95     }
96
97     public int getListenerCount(Class JavaDoc t) {
98         int listenerCount = 0;
99         Object JavaDoc[] listeners = getListenerList();
100         for (int i = 0; i < listeners.length; i +=2 ) {
101             if (t == listeners[i])
102                 listenerCount++;
103         }
104         return listenerCount;
105     }
106
107     public String JavaDoc toString() {
108     Object JavaDoc[] list = listenerList.toArray();
109     String JavaDoc s = "EventListenerList: ";
110     s += list.length + " listeners: ";
111     for (int i = 0 ; i < list.length ; i++) {
112         s += " type " + ((Class JavaDoc)list[i]).getName();
113         s += " listener " + list[i].toString();
114     }
115     return s;
116     }
117 }
118
Popular Tags