KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > events > EventRegistry


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
21 package org.apache.directory.ldapstudio.browser.core.events;
22
23
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30
31 /**
32  * The EventRegistry is a central point to register for LDAP Studio specific
33  * events and to fire events to registered listeners.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev$, $Date$
37  */

38 public class EventRegistry
39 {
40
41     /** The list of threads with suspended event fireing. */
42     private static Set JavaDoc<Thread JavaDoc> suspendedEventFireringThreads = new HashSet JavaDoc<Thread JavaDoc>();;
43
44     /** The lock used to synchronize event fireings */
45     private static Object JavaDoc lock = new Object JavaDoc();
46
47
48     /**
49      * Checks if event fireing is suspended in the current thread.
50      *
51      * @return true, if event fireing is suspended in the current thread
52      */

53     public static boolean isEventFireingSuspendedInCurrentThread()
54     {
55         return suspendedEventFireringThreads.contains( Thread.currentThread() );
56     }
57
58
59     /**
60      * Resumes event fireing in the current thread.
61      */

62     public static void resumeEventFireingInCurrentThread()
63     {
64         suspendedEventFireringThreads.remove( Thread.currentThread() );
65     }
66
67
68     /**
69      * Suspends event fireing in the current thread.
70      */

71     public static void suspendEventFireingInCurrentThread()
72     {
73         suspendedEventFireringThreads.add( Thread.currentThread() );
74     }
75
76
77
78     /** The map with search update listeners and their runners */
79     private static Map JavaDoc<SearchUpdateListener, EventRunner> searchUpdateListeners = new HashMap JavaDoc<SearchUpdateListener, EventRunner>();
80
81
82     /**
83      * Adds the search update listener.
84      *
85      * @param listener the listener
86      * @param runner the runner
87      */

88     public static void addSearchUpdateListener( SearchUpdateListener listener, EventRunner runner )
89     {
90         assert listener != null;
91         assert runner != null;
92
93         if ( !searchUpdateListeners.containsKey( listener ) )
94         {
95             searchUpdateListeners.put( listener, runner );
96         }
97     }
98
99
100     /**
101      * Removes the search update listener.
102      *
103      * @param listener the listener
104      */

105     public static void removeSearchUpdateListener( SearchUpdateListener listener )
106     {
107         if ( searchUpdateListeners.containsKey( listener ) )
108         {
109             searchUpdateListeners.remove( listener );
110         }
111     }
112
113
114     /**
115      * Notifies each {@link SearchUpdateListener} about the the given {@link SearchUpdateEvent}.
116      * Uses the {@link EventRunner}s.
117      *
118      * @param searchUpdateEvent the search update event
119      * @param source the source
120      */

121     public static void fireSearchUpdated( final SearchUpdateEvent searchUpdateEvent, final Object JavaDoc source )
122     {
123         if( isEventFireingSuspendedInCurrentThread() )
124         {
125             return;
126         }
127
128         Iterator JavaDoc<SearchUpdateListener> it = searchUpdateListeners.keySet().iterator();
129         while( it.hasNext() )
130         {
131             final SearchUpdateListener listener = it.next();
132             EventRunnable runnable = new EventRunnable()
133             {
134                 public void run()
135                 {
136                     listener.searchUpdated( searchUpdateEvent );
137                 }
138             };
139
140             EventRunner runner = searchUpdateListeners.get( listener );
141
142             synchronized( lock )
143             {
144                 runner.execute( runnable );
145             }
146         }
147     }
148
149
150     /** The map with bookmark update listeners and their runners */
151     private static Map JavaDoc<BookmarkUpdateListener, EventRunner> bookmarkUpdateListeners = new HashMap JavaDoc<BookmarkUpdateListener, EventRunner>();
152
153
154     /**
155      * Adds the bookmark update listener.
156      *
157      * @param listener the listener
158      * @param runner the runner
159      */

160     public static void addBookmarkUpdateListener( BookmarkUpdateListener listener, EventRunner runner )
161     {
162         assert listener != null;
163         assert runner != null;
164
165         if ( !bookmarkUpdateListeners.containsKey( listener ) )
166         {
167             bookmarkUpdateListeners.put( listener, runner );
168         }
169     }
170
171
172     /**
173      * Removes the bookmark update listener.
174      *
175      * @param listener the listener
176      */

177     public static void removeBookmarkUpdateListener( BookmarkUpdateListener listener )
178     {
179         if ( bookmarkUpdateListeners.containsKey( listener ) )
180         {
181             bookmarkUpdateListeners.remove( listener );
182         }
183     }
184
185
186     /**
187      * Notifies each {@link BookmarkUpdateListener} about the the given {@link BookmarkUpdateEvent}.
188      * Uses the {@link EventRunner}s.
189      *
190      * @param bookmarkUpdateEvent the bookmark update event
191      * @param source the source
192      */

193     public static void fireBookmarkUpdated( final BookmarkUpdateEvent bookmarkUpdateEvent, final Object JavaDoc source )
194     {
195         if( isEventFireingSuspendedInCurrentThread() )
196         {
197             return;
198         }
199
200         Iterator JavaDoc<BookmarkUpdateListener> it = bookmarkUpdateListeners.keySet().iterator();
201         while( it.hasNext() )
202         {
203             final BookmarkUpdateListener listener = it.next();
204             EventRunnable runnable = new EventRunnable()
205             {
206                 public void run()
207                 {
208                     listener.bookmarkUpdated( bookmarkUpdateEvent );
209                 }
210             };
211
212             EventRunner runner = bookmarkUpdateListeners.get( listener );
213             synchronized( lock )
214             {
215                 runner.execute( runnable );
216             }
217         }
218     }
219
220     /** The map with connection update listeners and their runners */
221     private static Map JavaDoc<ConnectionUpdateListener, EventRunner> connectionUpdateListeners = new HashMap JavaDoc<ConnectionUpdateListener, EventRunner>();
222
223
224     /**
225      * Adds the connection update listener.
226      *
227      * @param listener the listener
228      * @param runner the runner
229      */

230     public static void addConnectionUpdateListener( ConnectionUpdateListener listener, EventRunner runner )
231     {
232         assert listener != null;
233         assert runner != null;
234
235         if ( !connectionUpdateListeners.containsKey( listener ) )
236         {
237             connectionUpdateListeners.put( listener, runner );
238         }
239     }
240
241
242     /**
243      * Removes the connection update listener.
244      *
245      * @param listener the listener
246      */

247     public static void removeConnectionUpdateListener( ConnectionUpdateListener listener )
248     {
249         if ( connectionUpdateListeners.containsKey( listener ) )
250         {
251             connectionUpdateListeners.remove( listener );
252         }
253     }
254
255
256     /**
257      * Notifies each {@link ConnectionUpdateListener} about the the given {@link ConnectionUpdateEvent}.
258      * Uses the {@link EventRunner}s.
259      *
260      * @param connectionUpdateEvent the connection update event
261      * @param source the source
262      */

263     public static void fireConnectionUpdated( final ConnectionUpdateEvent connectionUpdateEvent, final Object JavaDoc source )
264     {
265         if( isEventFireingSuspendedInCurrentThread() )
266         {
267             return;
268         }
269
270         Iterator JavaDoc<ConnectionUpdateListener> it = connectionUpdateListeners.keySet().iterator();
271         while( it.hasNext() )
272         {
273             final ConnectionUpdateListener listener = it.next();
274             EventRunnable runnable = new EventRunnable()
275             {
276                 public void run()
277                 {
278                     listener.connectionUpdated( connectionUpdateEvent );
279                 }
280             };
281
282             EventRunner runner = connectionUpdateListeners.get( listener );
283             synchronized( lock )
284             {
285                 runner.execute( runnable );
286             }
287         }
288     }
289
290
291     /** The map with entry update listeners and their runners */
292     private static Map JavaDoc<EntryUpdateListener, EventRunner> entryUpdateListeners = new HashMap JavaDoc<EntryUpdateListener, EventRunner>();
293
294
295     /**
296      * Adds the entry update listener.
297      *
298      * @param listener the listener
299      * @param runner the runner
300      */

301     public static void addEntryUpdateListener( EntryUpdateListener listener, EventRunner runner )
302     {
303         assert listener != null;
304         assert runner != null;
305
306         if ( !entryUpdateListeners.containsKey( listener ) )
307         {
308             entryUpdateListeners.put( listener, runner );
309         }
310     }
311
312
313     /**
314      * Removes the entry update listener.
315      *
316      * @param listener the listener
317      */

318     public static void removeEntryUpdateListener( EntryUpdateListener listener )
319     {
320         if ( entryUpdateListeners.containsKey( listener ) )
321         {
322             entryUpdateListeners.remove( listener );
323         }
324     }
325
326
327     /**
328      * Notifies each {@link EntryUpdateListener} about the the given {@link EntryModificationEvent}.
329      * Uses the {@link EventRunner}s.
330      *
331      * @param entryUpdateEvent the entry update event
332      * @param source the source
333      */

334     public static void fireEntryUpdated( final EntryModificationEvent entryUpdateEvent, final Object JavaDoc source )
335     {
336         if( isEventFireingSuspendedInCurrentThread() )
337         {
338             return;
339         }
340
341         Iterator JavaDoc<EntryUpdateListener> it = entryUpdateListeners.keySet().iterator();
342         while( it.hasNext() )
343         {
344             final EntryUpdateListener listener = it.next();
345             EventRunnable runnable = new EventRunnable()
346             {
347                 public void run()
348                 {
349                     listener.entryUpdated( entryUpdateEvent );
350                 }
351             };
352
353             EventRunner runner = entryUpdateListeners.get( listener );
354             synchronized( lock )
355             {
356                 runner.execute( runnable );
357             }
358         }
359     }
360
361
362 }
363
Popular Tags