Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/batik/dom/events/EventListenerList.java


1   /*
2   
3      Copyright 2000-2002  The Apache Software Foundation 
4   
5      Licensed under the Apache License, Version 2.0 (the "License");
6      you may not use this file except in compliance with the License.
7      You may obtain a copy of the License at
8   
9          http://www.apache.org/licenses/LICENSE-2.0
10  
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16  
17   */
18  package org.apache.batik.dom.events;
19  
20  import org.w3c.dom.events.EventListener;
21  
22  /**
23   * A simple list of EventListener. Listeners are always added at the
24   * head of the list.
25   *
26   * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
27   */
28  public class EventListenerList {
29  
30      /**
31       * Current number of entries in list.
32       */
33      protected int              n         = 0;
34      /**
35       * Simple Linked list of listeners.
36       */
37      protected Entry            first     = null;
38      /**
39       * Array of listeners retained between calls to getEventListeners if the
40       * list of listeners doesn't change.  This needs to be a copy so if the
41       * list of listeners changes during event dispatch it doesn't effect
42       * the inprogress dispatch.
43       */
44      protected EventListener [] listeners = null;
45  
46      /**
47       * Returns an array of the event listeners of this list, or null if any.
48       */
49      public EventListener [] getEventListeners() {
50    if (first == null)     return null;
51          if (listeners != null) return listeners;
52  
53    listeners = new EventListener[n];
54    Entry current = first;
55    for (int i=0; i < n; ++i, current = current.next) {
56        listeners[i] = current.listener;
57    }
58    return listeners;
59      }
60  
61      /**
62       * Adds the specified event listener.
63       * @param listener the event listener to add
64       */
65      public void add(EventListener listener) {
66    first = new Entry(listener, first);
67          listeners = null; // Clear current listener list.
68    n++;
69      }
70  
71      /**
72       * Removes the specified event listener.
73       * @param listener the event listener to remove
74       */
75      public void remove(EventListener listener) {
76    if (first == null) return;
77  
78  
79          if (first.listener == listener) {
80        first = first.next;
81              listeners = null; // Clear current listener list.
82        --n;
83    } else {
84        Entry prev = first;
85        Entry e = first.next;
86        while (e != null) {
87                  if (e.listener == listener) {
88                      prev.next = e.next;
89                      listeners = null; // Clear current listener list.
90                      --n;
91                      break;
92                  }
93      prev = e;
94      e = e.next;
95        }
96    }
97      }
98  
99      /**
100      * Returns true of the specified event listener has already been
101      * added to this list, false otherwise.
102      * @param listener the listener th check
103      */
104     public boolean contains(EventListener listener) {
105   for (Entry e=first; e != null; e = e.next) {
106       if (listener == e.listener)
107     return true;
108   }
109   return false;
110     }
111 
112     /**
113      * Returns the number of listeners in the list.
114      */
115     public int size() {
116         return n;
117     }
118 
119     // simple entry for the list
120     protected static class Entry {
121   EventListener listener;
122   Entry         next;
123 
124   public Entry(EventListener listener, Entry next) {
125       this.listener = listener;
126       this.next = next;
127   }
128     }
129 }