KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > event > MessageSourceSupport


1 /*
2  * $Id: MessageSourceSupport.java,v 1.2 2004/09/07 18:16:02 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.event;
9
10 import javax.swing.event.EventListenerList JavaDoc;
11
12 import java.util.logging.Level JavaDoc;
13
14 /**
15  * A helper class which is an implementation of a message source and a
16  * progress source.
17  * This class manages the listener list and has convenience methods
18  * to fire MessageEvents and ProgressEvents.
19  *
20  * @see MessageEvent
21  * @see MessageSource
22  * @see ProgressEvent
23  * @see ProgressSource
24  * @author Mark Davidson
25  */

26 public class MessageSourceSupport implements MessageSource,
27                          ProgressSource {
28
29     private EventListenerList JavaDoc listeners;
30     private Object JavaDoc source;
31
32     /**
33      * Creates an MessageSource instance using the source object
34      * as the source of events.
35      */

36     public MessageSourceSupport(Object JavaDoc source) {
37     if (source == null) {
38         throw new IllegalArgumentException JavaDoc("source Object cannot be null");
39     }
40     this.source = source;
41     this.listeners = new EventListenerList JavaDoc();
42     }
43
44     public void addMessageListener(MessageListener l) {
45     if (l != null) {
46         listeners.add(MessageListener.class, l);
47     }
48     }
49
50     public void removeMessageListener(MessageListener l) {
51     if (l != null) {
52         listeners.remove(MessageListener.class, l);
53     }
54     }
55
56     public MessageListener[] getMessageListeners() {
57     return (MessageListener[])listeners.getListeners(MessageListener.class);
58     }
59
60     // Perhaps the ProgressListener interface should be defined
61

62     public void addProgressListener(ProgressListener l) {
63     if (l != null) {
64         listeners.add(ProgressListener.class, l);
65     }
66     }
67
68     public void removeProgressListener(ProgressListener l) {
69     if (l != null) {
70         listeners.remove(ProgressListener.class, l);
71     }
72     }
73
74     public ProgressListener[] getProgressListeners() {
75     return (ProgressListener[])listeners.getListeners(ProgressListener.class);
76     }
77
78     /**
79      * Indicates that a long operation is staring.
80      * For a determinite progress operation, the minimum value should be less than
81      * the maximum value. For inderminate operations, set minimum equal to maximum.
82      *
83      * @param minimum the minimum value of the progress operation
84      * @param maximum the maximum value of the progress operation
85      */

86     public void fireProgressStarted(int minimum, int maximum) {
87     fireProgressStarted(new ProgressEvent(source, minimum, maximum));
88     }
89
90     /**
91      * Indicates that an increment of progress has occured.
92      * @param progress total value of the progress operation. This
93      * value should be between the minimum and maximum values
94      */

95     public void fireProgressIncremented(int progress) {
96     fireProgressIncremented(new ProgressEvent(source, progress));
97     }
98
99
100     /**
101      * Indicates that a progress operation has completed
102      */

103     public void fireProgressEnded() {
104     fireProgressEnded(new ProgressEvent(source));
105     }
106
107     /**
108      * Create a SEVERE MessageEvent which represents an Exception.
109      * <p>
110      * <b>NOTE: This will create an event which is by default at Level.SEVERE</b>
111      *
112      * @see java.util.logging.Level
113      */

114     public void fireException(Throwable JavaDoc t) {
115     fireMessage(new MessageEvent(source, t, Level.SEVERE));
116     }
117
118     /**
119      * Send a Level.INFO, non-timestamped message to the list of listeners.
120      *
121      * @param message a string of text to send
122      */

123     public void fireMessage(String JavaDoc message) {
124     fireMessage(message, Level.INFO);
125     }
126
127     /**
128      * Send a non-timestamped message to the list of listeners.
129      *
130      * @param value the contents of the message
131      * @param level the level of message.
132      */

133     public void fireMessage(Object JavaDoc value, Level JavaDoc level) {
134     fireMessage(value, level, 0L);
135     }
136
137     /**
138      * Send a message to the list of listeners with a timestamp.
139      *
140      * @param value the contents of the message
141      * @param level the level of message
142      * @param when timestamp of when this event occured
143      */

144     public void fireMessage(Object JavaDoc value, Level JavaDoc level, long when) {
145     fireMessage(new MessageEvent(source, value, level, when));
146     }
147
148     /**
149      * Send the MessageEvent to the list of listeners.
150      *
151      * @param evt a non-null MessageEvent.
152      */

153     public void fireMessage(MessageEvent evt) {
154     if (evt == null) {
155         throw new IllegalArgumentException JavaDoc("the event should not be null");
156     }
157
158     MessageListener[] ls = getMessageListeners();
159     for (int i = 0; i < ls.length; i++) {
160         ls[i].message(evt);
161     }
162     }
163
164     /**
165      * Send the ProgessEvent to the list of listeners.
166      *
167      * @param evt a non-null ProgressEvent
168      */

169     public void fireProgressIncremented(ProgressEvent evt) {
170     if (evt == null) {
171         throw new IllegalArgumentException JavaDoc("the event should not be null");
172     }
173
174     ProgressListener[] ls = getProgressListeners();
175     for (int i = 0; i < ls.length; i++) {
176         ls[i].progressIncremented(evt);
177     }
178     }
179
180     /**
181      * Send the ProgessEvent to the list of listeners.
182      *
183      * @param evt a non-null ProgressEvent
184      */

185     public void fireProgressStarted(ProgressEvent evt) {
186     if (evt == null) {
187         throw new IllegalArgumentException JavaDoc("the event should not be null");
188     }
189
190     ProgressListener[] ls = getProgressListeners();
191     for (int i = 0; i < ls.length; i++) {
192         ls[i].progressStarted(evt);
193     }
194     }
195
196     /**
197      * Send the ProgessEvent to the list of listeners.
198      *
199      * @param evt a non-null ProgressEvent
200      */

201     public void fireProgressEnded(ProgressEvent evt) {
202         if (evt == null) {
203             throw new IllegalArgumentException JavaDoc("the event should not be null");
204         }
205
206         ProgressListener[] ls = getProgressListeners();
207         for (int i = 0; i < ls.length; i++) {
208             ls[i].progressEnded(evt);
209         }
210     }
211 }
212
Popular Tags