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,
040: ProgressSource {
041:
042: private EventListenerList listeners;
043:
044: private Object source;
045:
046: /**
047: * Creates an MessageSource instance using the source object as the source
048: * of events.
049: */
050: public MessageSourceSupport(Object source) {
051: if (source == null) {
052: throw new IllegalArgumentException(
053: "source Object cannot be null");
054: }
055: this .source = source;
056: this .listeners = new EventListenerList();
057: }
058:
059: public void addMessageListener(MessageListener l) {
060: if (l != null) {
061: listeners.add(MessageListener.class, l);
062: }
063: }
064:
065: public void removeMessageListener(MessageListener l) {
066: if (l != null) {
067: listeners.remove(MessageListener.class, l);
068: }
069: }
070:
071: public MessageListener[] getMessageListeners() {
072: return (MessageListener[]) listeners
073: .getListeners(MessageListener.class);
074: }
075:
076: // Perhaps the ProgressListener interface should be defined
077:
078: public void addProgressListener(ProgressListener l) {
079: if (l != null) {
080: listeners.add(ProgressListener.class, l);
081: }
082: }
083:
084: public void removeProgressListener(ProgressListener l) {
085: if (l != null) {
086: listeners.remove(ProgressListener.class, l);
087: }
088: }
089:
090: public ProgressListener[] getProgressListeners() {
091: return (ProgressListener[]) listeners
092: .getListeners(ProgressListener.class);
093: }
094:
095: /**
096: * Indicates that a long operation is staring. For a determinite progress
097: * operation, the minimum value should be less than the maximum value. For
098: * inderminate operations, set minimum equal to maximum.
099: *
100: * @param minimum the minimum value of the progress operation
101: * @param maximum the maximum value of the progress operation
102: */
103: public void fireProgressStarted(int minimum, int maximum) {
104: fireProgressStarted(new ProgressEvent(source, minimum, maximum));
105: }
106:
107: /**
108: * Indicates that an increment of progress has occured.
109: *
110: * @param progress total value of the progress operation. This value should
111: * be between the minimum and maximum values
112: */
113: public void fireProgressIncremented(int progress) {
114: fireProgressIncremented(new ProgressEvent(source, progress));
115: }
116:
117: /**
118: * Indicates that a progress operation has completed
119: */
120: public void fireProgressEnded() {
121: fireProgressEnded(new ProgressEvent(source));
122: }
123:
124: /**
125: * Create a SEVERE MessageEvent which represents an Exception.
126: * <p>
127: * <b>NOTE: This will create an event which is by default at Level.SEVERE</b>
128: *
129: * @see java.util.logging.Level
130: */
131: public void fireException(Throwable t) {
132: fireMessage(new MessageEvent(source, t, Level.SEVERE));
133: }
134:
135: /**
136: * Send a Level.INFO, non-timestamped message to the list of listeners.
137: *
138: * @param message a string of text to send
139: */
140: public void fireMessage(String message) {
141: fireMessage(message, Level.INFO);
142: }
143:
144: /**
145: * Send a non-timestamped message to the list of listeners.
146: *
147: * @param value the contents of the message
148: * @param level the level of message.
149: */
150: public void fireMessage(Object value, Level level) {
151: fireMessage(value, level, 0L);
152: }
153:
154: /**
155: * Send a message to the list of listeners with a timestamp.
156: *
157: * @param value the contents of the message
158: * @param level the level of message
159: * @param when timestamp of when this event occured
160: */
161: public void fireMessage(Object value, Level level, long when) {
162: fireMessage(new MessageEvent(source, value, level, when));
163: }
164:
165: /**
166: * Send the MessageEvent to the list of listeners.
167: *
168: * @param evt a non-null MessageEvent.
169: */
170: public void fireMessage(MessageEvent evt) {
171: if (evt == null) {
172: throw new IllegalArgumentException(
173: "the event should not be null");
174: }
175:
176: MessageListener[] ls = getMessageListeners();
177: for (int i = 0; i < ls.length; i++) {
178: ls[i].message(evt);
179: }
180: }
181:
182: /**
183: * Send the ProgessEvent to the list of listeners.
184: *
185: * @param evt a non-null ProgressEvent
186: */
187: public void fireProgressIncremented(ProgressEvent evt) {
188: if (evt == null) {
189: throw new IllegalArgumentException(
190: "the event should not be null");
191: }
192:
193: ProgressListener[] ls = getProgressListeners();
194: for (int i = 0; i < ls.length; i++) {
195: ls[i].progressIncremented(evt);
196: }
197: }
198:
199: /**
200: * Send the ProgessEvent to the list of listeners.
201: *
202: * @param evt a non-null ProgressEvent
203: */
204: public void fireProgressStarted(ProgressEvent evt) {
205: if (evt == null) {
206: throw new IllegalArgumentException(
207: "the event should not be null");
208: }
209:
210: ProgressListener[] ls = getProgressListeners();
211: for (int i = 0; i < ls.length; i++) {
212: ls[i].progressStarted(evt);
213: }
214: }
215:
216: /**
217: * Send the ProgessEvent to the list of listeners.
218: *
219: * @param evt a non-null ProgressEvent
220: */
221: public void fireProgressEnded(ProgressEvent evt) {
222: if (evt == null) {
223: throw new IllegalArgumentException(
224: "the event should not be null");
225: }
226:
227: ProgressListener[] ls = getProgressListeners();
228: for (int i = 0; i < ls.length; i++) {
229: ls[i].progressEnded(evt);
230: }
231: }
232: }
|