001    /*
002     * $Id: MessageSourceSupport.java,v 1.4 2005/10/26 14:29:56 kleopatra Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    package org.jdesktop.swingx.event;
023    
024    import java.util.logging.Level;
025    
026    import javax.swing.event.EventListenerList;
027    
028    /**
029     * A helper class which is an implementation of a message source and a progress
030     * source. This class manages the listener list and has convenience methods to
031     * fire MessageEvents and ProgressEvents.
032     * 
033     * @see MessageEvent
034     * @see MessageSource
035     * @see ProgressEvent
036     * @see ProgressSource
037     * @author Mark Davidson
038     */
039    public class MessageSourceSupport implements MessageSource, ProgressSource {
040    
041        private EventListenerList listeners;
042    
043        private Object source;
044    
045        /**
046         * Creates an MessageSource instance using the source object as the source
047         * of events.
048         */
049        public MessageSourceSupport(Object source) {
050            if (source == null) {
051                throw new IllegalArgumentException("source Object cannot be null");
052            }
053            this.source = source;
054            this.listeners = new EventListenerList();
055        }
056    
057        public void addMessageListener(MessageListener l) {
058            if (l != null) {
059                listeners.add(MessageListener.class, l);
060            }
061        }
062    
063        public void removeMessageListener(MessageListener l) {
064            if (l != null) {
065                listeners.remove(MessageListener.class, l);
066            }
067        }
068    
069        public MessageListener[] getMessageListeners() {
070            return (MessageListener[]) listeners
071                    .getListeners(MessageListener.class);
072        }
073    
074        // Perhaps the ProgressListener interface should be defined
075    
076        public void addProgressListener(ProgressListener l) {
077            if (l != null) {
078                listeners.add(ProgressListener.class, l);
079            }
080        }
081    
082        public void removeProgressListener(ProgressListener l) {
083            if (l != null) {
084                listeners.remove(ProgressListener.class, l);
085            }
086        }
087    
088        public ProgressListener[] getProgressListeners() {
089            return (ProgressListener[]) listeners
090                    .getListeners(ProgressListener.class);
091        }
092    
093        /**
094         * Indicates that a long operation is staring. For a determinite progress
095         * operation, the minimum value should be less than the maximum value. For
096         * inderminate operations, set minimum equal to maximum.
097         * 
098         * @param minimum the minimum value of the progress operation
099         * @param maximum the maximum value of the progress operation
100         */
101        public void fireProgressStarted(int minimum, int maximum) {
102            fireProgressStarted(new ProgressEvent(source, minimum, maximum));
103        }
104    
105        /**
106         * Indicates that an increment of progress has occured.
107         * 
108         * @param progress total value of the progress operation. This value should
109         *        be between the minimum and maximum values
110         */
111        public void fireProgressIncremented(int progress) {
112            fireProgressIncremented(new ProgressEvent(source, progress));
113        }
114    
115        /**
116         * Indicates that a progress operation has completed
117         */
118        public void fireProgressEnded() {
119            fireProgressEnded(new ProgressEvent(source));
120        }
121    
122        /**
123         * Create a SEVERE MessageEvent which represents an Exception.
124         * <p>
125         * <b>NOTE: This will create an event which is by default at Level.SEVERE</b>
126         * 
127         * @see java.util.logging.Level
128         */
129        public void fireException(Throwable t) {
130            fireMessage(new MessageEvent(source, t, Level.SEVERE));
131        }
132    
133        /**
134         * Send a Level.INFO, non-timestamped message to the list of listeners.
135         * 
136         * @param message a string of text to send
137         */
138        public void fireMessage(String message) {
139            fireMessage(message, Level.INFO);
140        }
141    
142        /**
143         * Send a non-timestamped message to the list of listeners.
144         * 
145         * @param value the contents of the message
146         * @param level the level of message.
147         */
148        public void fireMessage(Object value, Level level) {
149            fireMessage(value, level, 0L);
150        }
151    
152        /**
153         * Send a message to the list of listeners with a timestamp.
154         * 
155         * @param value the contents of the message
156         * @param level the level of message
157         * @param when timestamp of when this event occured
158         */
159        public void fireMessage(Object value, Level level, long when) {
160            fireMessage(new MessageEvent(source, value, level, when));
161        }
162    
163        /**
164         * Send the MessageEvent to the list of listeners.
165         * 
166         * @param evt a non-null MessageEvent.
167         */
168        public void fireMessage(MessageEvent evt) {
169            if (evt == null) {
170                throw new IllegalArgumentException("the event should not be null");
171            }
172    
173            MessageListener[] ls = getMessageListeners();
174            for (int i = 0; i < ls.length; i++) {
175                ls[i].message(evt);
176            }
177        }
178    
179        /**
180         * Send the ProgessEvent to the list of listeners.
181         * 
182         * @param evt a non-null ProgressEvent
183         */
184        public void fireProgressIncremented(ProgressEvent evt) {
185            if (evt == null) {
186                throw new IllegalArgumentException("the event should not be null");
187            }
188    
189            ProgressListener[] ls = getProgressListeners();
190            for (int i = 0; i < ls.length; i++) {
191                ls[i].progressIncremented(evt);
192            }
193        }
194    
195        /**
196         * Send the ProgessEvent to the list of listeners.
197         * 
198         * @param evt a non-null ProgressEvent
199         */
200        public void fireProgressStarted(ProgressEvent evt) {
201            if (evt == null) {
202                throw new IllegalArgumentException("the event should not be null");
203            }
204    
205            ProgressListener[] ls = getProgressListeners();
206            for (int i = 0; i < ls.length; i++) {
207                ls[i].progressStarted(evt);
208            }
209        }
210    
211        /**
212         * Send the ProgessEvent to the list of listeners.
213         * 
214         * @param evt a non-null ProgressEvent
215         */
216        public void fireProgressEnded(ProgressEvent evt) {
217            if (evt == null) {
218                throw new IllegalArgumentException("the event should not be null");
219            }
220    
221            ProgressListener[] ls = getProgressListeners();
222            for (int i = 0; i < ls.length; i++) {
223                ls[i].progressEnded(evt);
224            }
225        }
226    }