KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > EventDispatcher


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.EventListener JavaDoc;
24
25 public class EventDispatcher {
26   protected transient ArrayList JavaDoc listeners = new ArrayList JavaDoc(1);
27
28   public void dispatch(DispatchableEvent e) {
29     EventListener JavaDoc[] listenersCopy = null;
30     synchronized(this) {
31       if (hasListeners())
32         listenersCopy = (EventListener JavaDoc[])listeners.toArray(new EventListener JavaDoc[listeners.size()]);
33     }
34
35     if (listenersCopy != null) {
36       int count = listenersCopy.length;
37       for (int index = 0; index < count; ++index) {
38         e.dispatch(listenersCopy[index]);
39       }
40     }
41   }
42
43   public synchronized boolean hasListeners() {
44     return !listeners.isEmpty();
45   }
46
47   public synchronized int getListenerCount() {
48     return listeners.size();
49   }
50
51   public synchronized int find(EventListener JavaDoc listener) {
52     return listeners.indexOf(listener);
53   }
54
55   public synchronized void add(EventListener JavaDoc listener) {
56     if (find(listener) < 0)
57       listeners.add(listener);
58   }
59
60   public synchronized void remove(EventListener JavaDoc listener) {
61     listeners.remove(listener);
62   }
63
64   public synchronized void clear() {
65     listeners.clear();
66   }
67
68   public static EventDispatcher add(EventDispatcher dispatcher, EventListener JavaDoc listener) {
69     if (dispatcher == null)
70       dispatcher = new EventDispatcher();
71     dispatcher.add(listener);
72     return dispatcher;
73   }
74
75   public final static EventDispatcher remove(EventDispatcher dispatcher, EventListener JavaDoc listener) {
76     if (dispatcher != null) {
77       dispatcher.remove(listener);
78       if (!dispatcher.hasListeners())
79         dispatcher = null;
80     }
81     return dispatcher;
82   }
83 }
84
Popular Tags