KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > event > EventManager


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.event;
23
24 import org.netbeans.lib.cvsclient.ClientServices;
25
26 import java.io.File JavaDoc;
27
28 /**
29  * This class is responsible for firing CVS events to registered listeners.
30  * It can either fire events as they are generated or wait until a suitable
31  * checkpoint and fire many events at once. This can prevent event storms
32  * from degrading system performance.
33  * @author Robert Greig
34  */

35 public class EventManager {
36     /**
37      * Registered listeners for events. This is an array for performance when
38      * firing events. We take the hit when adding or removing listeners - that
39      * should be a relatively rare occurrence.
40      */

41     private CVSListener[] listeners;
42
43     /**
44      * Holds value of property fireEnhancedEventSet.
45      * If true, the library fires the EnhancedMessageEvents.
46      * Default is true. Some builders might work badly, if set to false.
47      */

48     private boolean fireEnhancedEventSet = true;
49     
50     private final ClientServices services;
51
52     /**
53      * Construct a new EventManager
54      */

55     public EventManager(ClientServices services) {
56         this.services = services;
57     }
58
59     /**
60      * Returns Client services implementation tied to this event manager.
61      *
62      * @return a ClientServices implementation
63      */

64     public ClientServices getClientServices() {
65         return services;
66     }
67
68     /**
69      * Add a listener to the list.
70      * @param listener the listener to add
71      */

72     public synchronized void addCVSListener(CVSListener listener) {
73         if (listeners == null || listeners.length == 0) {
74             listeners = new CVSListener[1];
75         }
76         else {
77             // allocate a new array and copy existing listeners
78
CVSListener[] l = new CVSListener[listeners.length + 1];
79             for (int i = 0; i < listeners.length; i++) {
80                 l[i] = listeners[i];
81             }
82             listeners = l;
83         }
84         listeners[listeners.length - 1] = listener;
85     }
86
87     /**
88      * Remove a listeners from the list
89      * @param listener the listener to remove
90      */

91     public synchronized void removeCVSListener(CVSListener listener) {
92         // TODO: test this method!!
93
if (listeners.length == 1) {
94             listeners = null;
95         }
96         else {
97             CVSListener[] l = new CVSListener[listeners.length - 1];
98             int i = 0;
99             while (i < l.length) {
100                 if (listeners[i] == listener) {
101                     for (int j = i + 1; j < listeners.length; j++) {
102                         l[j - 1] = listeners[j];
103                     }
104                     break;
105                 }
106                 else {
107                     l[i] = listeners[i];
108                 }
109                 i++;
110             }
111             listeners = l;
112         }
113     }
114
115     /**
116      * Fire a CVSEvent to all the listeners
117      * @param e the event to send
118      */

119     public void fireCVSEvent(CVSEvent e) {
120         // if we have no listeners, then there is nothing to do
121
if (listeners == null || listeners.length == 0)
122             return;
123         if (e instanceof FileInfoEvent) {
124             File JavaDoc file = ((FileInfoEvent) e).getInfoContainer().getFile();
125             if (services.getGlobalOptions().isExcluded(file)) return;
126         }
127         CVSListener[] l = null;
128         synchronized (listeners) {
129             l = new CVSListener[listeners.length];
130             System.arraycopy(listeners, 0, l, 0, l.length);
131         }
132
133         for (int i = 0; i < l.length; i++) {
134             e.fireEvent(l[i]);
135         }
136     }
137
138     /** Getter for property fireEnhancedEventSet.
139      * @return Value of property fireEnhancedEventSet.
140      */

141     public boolean isFireEnhancedEventSet() {
142         return fireEnhancedEventSet;
143     }
144
145     /** Setter for property fireEnhancedEventSet.
146      * @param fireEnhancedEventSet New value of property fireEnhancedEventSet.
147      */

148     public void setFireEnhancedEventSet(boolean fireEnhancedEventSet) {
149         this.fireEnhancedEventSet = fireEnhancedEventSet;
150     }
151
152 }
153
Popular Tags