1 /*
2 * (C) 2002 David Carr david@carr.name
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package net.sourceforge.mflow.api.base;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.EventObject;
25 import java.util.List;
26
27 import net.sourceforge.mflow.api.Msg;
28 import net.sourceforge.mflow.api.MsgChangeEvent;
29 import net.sourceforge.mflow.api.MsgChangeListener;
30 import net.sourceforge.mflow.api.MsgFolder;
31
32 /***
33 * Abstract implementation of MsgFolder
34 *
35 * @author <a href="mailto:david@carr.name">David Carr</a>
36 * @version $Revision: 1.3 $
37 * @todo use generics to remove Msg[] parameters
38 */
39 public abstract class AbstractMsgFolder
40 extends AbstractMsgFlowComponent
41 implements MsgFolder {
42 /***
43 * Private storage for the messages
44 */
45 private List messages = Collections.synchronizedList(new ArrayList());
46 /***
47 * Private storage for the MsgChangeLsiteners
48 */
49 private List listeners = Collections.synchronizedList(new ArrayList());
50 /***
51 * Private storage for the parent folder
52 */
53 private MsgFolder parent = null;
54
55 /***
56 * @return the messages in this folder
57 */
58 protected final List getMessages() {
59 return this.messages;
60 }
61
62 /***
63 * Removes all messages from the folder
64 */
65 protected final void clear() {
66 this.messages.clear();
67 fireMsgChangeEvent(new MsgChangeEventImpl(this));
68 }
69
70 /***
71 * Gets all the messages
72 *
73 * @return an array of all Msgs contained in this folder
74 */
75 public final Msg[] getMsg() {
76 return (Msg[]) this.messages.toArray(new Msg[this.messages.size()]);
77 }
78
79 /***
80 * Sets all the messages
81 *
82 * @param msgs the new msgs
83 */
84 public final void setMsg(final Msg[] msgs) {
85 this.messages.clear();
86 this.messages.addAll(Arrays.asList(msgs));
87 fireMsgChangeEvent(new MsgChangeEventImpl(msgs));
88 }
89
90 /***
91 * Gets a single Msg
92 *
93 * @param index the index of the Msg to get
94 * @return the Msg at the specified index
95 */
96 public final Msg getMsg(final int index) {
97 if (index >= 0 && index < this.messages.size()) {
98 return (Msg) this.messages.get(index);
99 } else {
100 throw new ArrayIndexOutOfBoundsException(index);
101 }
102 }
103
104 /***
105 * Sets a single Msg
106 *
107 * @param index the index of the Msg to set
108 * @param msg the Msg to set to the specified index
109 */
110 public final void setMsg(final int index, final Msg msg) {
111 if (index >= 0 && index < this.messages.size()) {
112 this.messages.set(index, msg);
113 fireMsgChangeEvent(new MsgChangeEventImpl(msg));
114 } else {
115 throw new ArrayIndexOutOfBoundsException(index);
116 }
117 }
118
119 /***
120 * Adds a message to the folder
121 *
122 * @param m the msg to add
123 */
124 public final void addMsg(final Msg m) {
125 this.messages.add(m);
126 fireMsgChangeEvent(new MsgChangeEventImpl(m));
127 }
128
129 /***
130 * Removes a msg from the folder
131 *
132 * @param m the msg to remove
133 */
134 public final void removeMsg(final Msg m) {
135 this.messages.remove(m);
136 fireMsgChangeEvent(new MsgChangeEventImpl(m));
137 }
138
139 /***
140 * Gets the number of new messages in the folder
141 *
142 * @return the number of new messages
143 */
144 public final int getNewMsgCount() {
145 int count = 0;
146 for (int i = 0; i < this.messages.size(); i++) {
147 Msg m = (Msg) this.messages.get(i);
148 if (m.isNew()) {
149 count++;
150 }
151 }
152 return count;
153 }
154
155 /***
156 * Gets the number of unread messages in the folder
157 *
158 * @return the number of unread messages
159 */
160 public final int getUnreadMsgCount() {
161 int count = 0;
162 for (int i = 0; i < this.messages.size(); i++) {
163 Msg m = (Msg) this.messages.get(i);
164 if (m.isUnread()) {
165 count++;
166 }
167 }
168 return count;
169 }
170
171 /***
172 * Adds a new listener
173 *
174 * @param listener the new listener
175 */
176 public final void addMsgChangeListener(final MsgChangeListener listener) {
177 if (!this.listeners.contains(listener)) {
178 this.listeners.add(listener);
179 }
180 }
181
182 /***
183 * Gets the current listeners
184 *
185 * @return an array of the listeners
186 */
187 public final MsgChangeListener[] getMsgChangeListeners() {
188 return (MsgChangeListener[]) this.listeners.toArray(
189 new MsgChangeListener[this.listeners.size()]);
190 }
191
192 /***
193 * Sets all the listeners
194 *
195 * @param aListeners an array of MsgChangeListeners
196 */
197 public final void setMsgChangeListeners(
198 final MsgChangeListener[] aListeners) {
199 this.listeners.clear();
200 for (int i = 0; i < aListeners.length; i++) {
201 addMsgChangeListener(aListeners[i]);
202 }
203 }
204
205 /***
206 * Removes a MsgChangeListener
207 *
208 * @param aListener the listener to remove
209 */
210 public final void removeMsgChangeListener(final MsgChangeListener aListener) {
211 this.listeners.remove(aListener);
212 }
213
214 /***
215 * Gets the parent folder, or null if there isn't one
216 *
217 * @return the parent folder, or null
218 */
219 public final MsgFolder getParent() {
220 return this.parent;
221 }
222
223 /***
224 * Sets the parent folder
225 *
226 * @param aParent the new parent, or null to make it a root
227 */
228 public final void setParent(final MsgFolder aParent) {
229 this.parent = aParent;
230 }
231
232 /***
233 * Whether or not the folder is a root (no parent)
234 *
235 * @return whether or not the folder is a root
236 */
237 public final boolean isRootFolder() {
238 return this.parent == null;
239 }
240
241 /***
242 * Fires a new {@link MsgChangeEvent} with this object as the source
243 */
244 protected final void fireMsgChangeEvent() {
245 fireMsgChangeEvent(new MsgChangeEventImpl(this));
246 }
247
248 /***
249 * Fires a MsgChangeEvent
250 *
251 * @param mce the event to fire
252 */
253 protected final void fireMsgChangeEvent(final MsgChangeEvent mce) {
254 for (int i = 0; i < this.listeners.size(); i++) {
255 MsgChangeListener mcl = (MsgChangeListener) this.listeners.get(i);
256 mcl.msgChange(mce);
257 }
258 }
259
260 /***
261 * An event thrown when a Msg object changes
262 *
263 * @author <a href="mailto:david@carr.name">David Carr</a>
264 */
265 private static class MsgChangeEventImpl
266 extends EventObject
267 implements MsgChangeEvent {
268 /***
269 * Constructor taking the source of the event
270 *
271 * @param src the event source
272 */
273 public MsgChangeEventImpl(final Object src) {
274 super(src);
275 }
276 }
277 }
This page was automatically generated by Maven