1 20 21 package org.apache.directory.ldapstudio.browser.core.events; 22 23 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 31 38 public class EventRegistry 39 { 40 41 42 private static Set <Thread > suspendedEventFireringThreads = new HashSet <Thread >();; 43 44 45 private static Object lock = new Object (); 46 47 48 53 public static boolean isEventFireingSuspendedInCurrentThread() 54 { 55 return suspendedEventFireringThreads.contains( Thread.currentThread() ); 56 } 57 58 59 62 public static void resumeEventFireingInCurrentThread() 63 { 64 suspendedEventFireringThreads.remove( Thread.currentThread() ); 65 } 66 67 68 71 public static void suspendEventFireingInCurrentThread() 72 { 73 suspendedEventFireringThreads.add( Thread.currentThread() ); 74 } 75 76 77 78 79 private static Map <SearchUpdateListener, EventRunner> searchUpdateListeners = new HashMap <SearchUpdateListener, EventRunner>(); 80 81 82 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 105 public static void removeSearchUpdateListener( SearchUpdateListener listener ) 106 { 107 if ( searchUpdateListeners.containsKey( listener ) ) 108 { 109 searchUpdateListeners.remove( listener ); 110 } 111 } 112 113 114 121 public static void fireSearchUpdated( final SearchUpdateEvent searchUpdateEvent, final Object source ) 122 { 123 if( isEventFireingSuspendedInCurrentThread() ) 124 { 125 return; 126 } 127 128 Iterator <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 151 private static Map <BookmarkUpdateListener, EventRunner> bookmarkUpdateListeners = new HashMap <BookmarkUpdateListener, EventRunner>(); 152 153 154 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 177 public static void removeBookmarkUpdateListener( BookmarkUpdateListener listener ) 178 { 179 if ( bookmarkUpdateListeners.containsKey( listener ) ) 180 { 181 bookmarkUpdateListeners.remove( listener ); 182 } 183 } 184 185 186 193 public static void fireBookmarkUpdated( final BookmarkUpdateEvent bookmarkUpdateEvent, final Object source ) 194 { 195 if( isEventFireingSuspendedInCurrentThread() ) 196 { 197 return; 198 } 199 200 Iterator <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 221 private static Map <ConnectionUpdateListener, EventRunner> connectionUpdateListeners = new HashMap <ConnectionUpdateListener, EventRunner>(); 222 223 224 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 247 public static void removeConnectionUpdateListener( ConnectionUpdateListener listener ) 248 { 249 if ( connectionUpdateListeners.containsKey( listener ) ) 250 { 251 connectionUpdateListeners.remove( listener ); 252 } 253 } 254 255 256 263 public static void fireConnectionUpdated( final ConnectionUpdateEvent connectionUpdateEvent, final Object source ) 264 { 265 if( isEventFireingSuspendedInCurrentThread() ) 266 { 267 return; 268 } 269 270 Iterator <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 292 private static Map <EntryUpdateListener, EventRunner> entryUpdateListeners = new HashMap <EntryUpdateListener, EventRunner>(); 293 294 295 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 318 public static void removeEntryUpdateListener( EntryUpdateListener listener ) 319 { 320 if ( entryUpdateListeners.containsKey( listener ) ) 321 { 322 entryUpdateListeners.remove( listener ); 323 } 324 } 325 326 327 334 public static void fireEntryUpdated( final EntryModificationEvent entryUpdateEvent, final Object source ) 335 { 336 if( isEventFireingSuspendedInCurrentThread() ) 337 { 338 return; 339 } 340 341 Iterator <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 |