001    /*
002     * Copyright 2002-2006 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application.support;
017    
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    
021    import org.springframework.context.ApplicationEvent;
022    import org.springframework.context.ApplicationListener;
023    
024    /**
025     * This class is responsible for re-dispatching application events to a collection of
026     * additional registered listeners. This is needed due to the way that the application
027     * context handles the ApplicationListener tag interface. For any beans that are
028     * prototypes, one instance will be registered as an ApplicationListener and all the
029     * others will not. Further, there is no mechanism to get a reference to the context's
030     * event dispatcher to add listeners at runtime.
031     * <p>
032     * By defining a bean with this type in the context, it will be added as a typical event
033     * listener. Then, this dispatcher can be set on any other bean (including prototypes)
034     * that need to be added to the event mechanism. In order to wire it all together, any
035     * object that wants to register for events simply defines a property of type
036     * ApplicationEventDispatcher and in the property setter method, call the
037     * {@link #addListener(ApplicationListener)} method on itself (or a delegate handler).
038     * Like this:
039     * 
040     * <pre>
041     * public void setApplicationEventRedispatcher( ApplicationEventRedispatcher dispatcher ) {
042     *     dispatcher.addListener(this);
043     * }
044     * </pre>
045     * 
046     * @author Larry Streepy
047     * 
048     */
049    public class ApplicationEventRedispatcher implements ApplicationListener {
050    
051        /** Our list of delegates to dispatch events to. */
052        private ArrayList delegates = new ArrayList();
053    
054        /**
055         * Handle an application event, dispatch it to all our delegates.
056         * 
057         * @param event to dispatch
058         */
059        public void onApplicationEvent( ApplicationEvent event ) {
060            Iterator iter = delegates.iterator();
061            while( iter.hasNext() ) {
062                ((ApplicationListener) iter.next()).onApplicationEvent(event);
063            }
064        }
065    
066        /**
067         * Add a listener to our set.
068         * 
069         * @param l Listener to add
070         */
071        public void addListener( ApplicationListener l ) {
072            delegates.add(l);
073        }
074    
075        /**
076         * Remove a listener from our set.
077         * 
078         * @param l Listener to remove
079         */
080        public void removeListener( ApplicationListener l ) {
081            delegates.remove(l);
082        }
083    }