001
002 /*****************************************************************************
003
004 jBudget.
005 Copyright (C) 2006 Dmitri Petrov
006
007 This program is free software: you can redistribute it and/or modify
008 it under the terms of the GNU General Public License as published by
009 the Free Software Foundation, either version 3 of the License, or
010 (at your option) any later version.
011
012 This program 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
015 GNU General Public License for more details.
016
017 You should have received a copy of the GNU General Public License
018 along with this program. If not, see <http://www.gnu.org/licenses/>.
019
020 *****************************************************************************/
021
022 /*
023 * EventDispatcher.java
024 *
025 * Created on June 21, 2006, 5:55 PM
026 */
027
028 package org.jbudget.io;
029
030 import java.util.LinkedList;
031 import java.util.List;
032 import org.jbudget.Core.Account;
033 import org.jbudget.Core.Budget;
034 import org.jbudget.Core.Month;
035 import org.jbudget.Core.Transaction;
036 import org.jbudget.Core.User;
037
038 /**
039 * A class that contains methods for delivering data related events to
040 * registered listeners.
041 *
042 * @author petrov
043 */
044 public class EventDispatcher
045 {
046
047 /** A list of listeners that receive budget updates */
048 private final List<BudgetListener> budgetListeners =
049 new LinkedList<BudgetListener>();
050 /** A list of listeners that receive month updates */
051 private final List<MonthListener> monthListeners =
052 new LinkedList<MonthListener>();
053 /** A list of listeners that receive account updates */
054 private final List<AccountListener> accountListeners =
055 new LinkedList<AccountListener>();
056 /** A list of listeners that receive account updates */
057 private final List<UserListener> userListeners =
058 new LinkedList<UserListener>();
059 /** A list of listeners that receive transaction updates */
060 private final List<TransactionListener> transactionListeners =
061 new LinkedList<TransactionListener>();
062 /** A list of listeners that receive allocation updates */
063 private final List<AllocationListener> allocationListeners =
064 new LinkedList<AllocationListener>();
065 /** A list of listeners that receive data directory updates */
066 private final List<DataSourceListener> dataSourceListeners =
067 new LinkedList<DataSourceListener>();
068
069 /** Creates a new instance of EventDispatcher */
070 public EventDispatcher()
071 {
072 }
073
074 //================= Adding and Removing Listeners ========================//
075 public void addBudgetListener(BudgetListener listener)
076 {
077 budgetListeners.add(listener);
078 }
079
080 public void removeBudgetListener(BudgetListener listener)
081 {
082 budgetListeners.remove(listener);
083 }
084
085 public void addUserListener(UserListener listener)
086 {
087 userListeners.add(listener);
088 }
089
090 public void removeUserListener(UserListener listener)
091 {
092 userListeners.remove(listener);
093 }
094
095 public void addAccountListener(AccountListener listener)
096 {
097 accountListeners.add(listener);
098 }
099
100 public void removeAccountListener(AccountListener listener)
101 {
102 accountListeners.remove(listener);
103 }
104
105 public void addMonthListener(MonthListener listener)
106 {
107 monthListeners.add(listener);
108 }
109
110 public void removeMonthListener(MonthListener listener)
111 {
112 monthListeners.remove(listener);
113 }
114
115 public void addTransactionListener(TransactionListener listener)
116 {
117 transactionListeners.add(listener);
118 }
119
120 public void removeTransactionListener(TransactionListener listener)
121 {
122 transactionListeners.remove(listener);
123 }
124
125 public void removeAllocationListener(AllocationListener listener)
126 {
127 allocationListeners.remove(listener);
128 }
129
130 public void addAllocationListener(AllocationListener listener)
131 {
132 allocationListeners.add(listener);
133 }
134
135 public void removeDataSourceListener(DataSourceListener listener)
136 {
137 dataSourceListeners.remove(listener);
138 }
139
140 public void addDataSourceListener(DataSourceListener listener)
141 {
142 dataSourceListeners.add(listener);
143 }
144
145 // ========================= Notifying Listeners ========================//
146 /** Notifies all listeners that have registered to receive budget update
147 * events that a budget was added */
148 public void fireBudgetAdded(Budget budget)
149 {
150 for (BudgetListener listener : budgetListeners)
151 listener.budgetAdded(budget);
152 }
153
154 /** Notifies all listeners that have registered to receive budget update
155 * events that a budget was removed */
156 public void fireBudgetRemoved(int budgetId)
157 {
158 for (BudgetListener listener : budgetListeners)
159 listener.budgetRemoved(budgetId);
160 }
161
162 /** Notifies all listeners that have registered to receive account updates
163 * events that an account was added */
164 public void fireAccountAdded(Account account)
165 {
166 for (AccountListener listener : accountListeners)
167 listener.accountAdded(account);
168 }
169
170 /** Notifies all listeners that have registered to receive account updates
171 * events that an account was added */
172 public void fireAccountUpdated(Account account)
173 {
174 for (AccountListener listener : accountListeners)
175 listener.accountUpdated(account);
176 }
177
178 /** Notifies all listeners that have registered to receive account updates
179 * events that an account was removed */
180 public void fireAccountRemoved(int accountId)
181 {
182 for (AccountListener listener : accountListeners)
183 listener.accountRemoved(accountId);
184 }
185
186 /** Notifies all listeners that have registered to receive user updates
187 * events that a user was added */
188 public void fireUserAdded(User user)
189 {
190 for (UserListener listener : userListeners)
191 listener.userAdded(user);
192 }
193
194 /** Notifies all listeners that have registered to receive user updates
195 * events that a user was removed */
196 public void fireUserRemoved(int uid)
197 {
198 for (UserListener listener : userListeners)
199 listener.userRemoved(uid);
200 }
201
202 /** Notifies all listeners that have registered to receive user updates
203 * events that the current user has changed. */
204 public void fireUserChanged(User user)
205 {
206 for (UserListener listener : userListeners)
207 listener.userChanged(user);
208 }
209
210 /** Notifies all listeners that have registered to receive month update
211 * events that a month was added */
212 public void fireMonthAdded(Month month)
213 {
214 for (MonthListener listener : monthListeners)
215 listener.monthAdded(month);
216 }
217
218 /** Notifies all listeners that have registered to receive month update
219 * events that a month was removed */
220 public void fireMonthRemoved(int cMonth, int cYear)
221 {
222 for (MonthListener listener : monthListeners)
223 listener.monthRemoved(cMonth, cYear);
224 }
225
226 /** Notifies all listeners that have registered to receive month update
227 * events that a month was updated */
228 public void fireMonthUpdated(Month month)
229 {
230 for (MonthListener listener : monthListeners)
231 listener.monthUpdated(month);
232 }
233
234 /** Notifies all listeners that have registered to receive transaction update
235 * events that a transaction was added */
236 public void fireTransactionAdded(Transaction transaction)
237 {
238 for (TransactionListener listener : transactionListeners)
239 listener.transactionAdded(transaction);
240 }
241
242 /** Notifies all listeners that have registered to receive transaction update
243 * events that a transaction that occured on a specific date was removed */
244 public void fireTransactionRemoved(List<Transaction.Transfer> transfers,
245 int cDay, int cMonth, int cYear)
246 {
247 for (TransactionListener listener : transactionListeners)
248 listener.transactionRemoved(transfers, cDay, cMonth, cYear);
249 }
250
251 /** Notifies all listeners that have registered to receive allocation update
252 * events that an allocation was added */
253 public void fireAllocationAdded(Transaction allocation)
254 {
255 for (AllocationListener listener : allocationListeners)
256 listener.allocationAdded(allocation);
257 }
258
259 /** Notifies all listeners that have registered to receive allocation update
260 * events that an allocation that occurred on a specific date was removed */
261 public void fireAllocationRemoved(List<Transaction.Transfer> transfers,
262 int cDay, int cMonth, int cYear)
263 {
264 for (AllocationListener listener : allocationListeners)
265 listener.allocationRemoved(transfers, cDay, cMonth, cYear);
266 }
267
268 /** Notifies all registered listeners that the data directory has
269 * changed. */
270 public void fireDataDirChanged()
271 {
272 for (DataSourceListener listener : dataSourceListeners)
273 listener.dataSourceChanged();
274 }
275
276 //========================= Inner Classes ==============================//
277
278 public static interface BudgetListener
279 {
280 public void budgetAdded(Budget budget);
281
282 public void budgetRemoved(int budgetId);
283 }
284
285 public static interface AccountListener
286 {
287 public void accountAdded(Account account);
288
289 public void accountUpdated(Account account);
290
291 public void accountRemoved(int accountID);
292 }
293
294 public static interface UserListener
295 {
296 public void userAdded(User user);
297
298 public void userRemoved(int uid);
299
300 public void userChanged(User user);
301 }
302
303 public static interface MonthListener
304 {
305 public void monthAdded(Month month);
306
307 public void monthRemoved(int cMonth, int cYear);
308
309 public void monthUpdated(Month month);
310 }
311
312 public static interface TransactionListener
313 {
314 public void transactionAdded(Transaction transaction);
315
316 public void transactionRemoved(List<Transaction.Transfer> transfers,
317 int cDay, int cMonth, int cYear);
318 }
319
320 public static interface AllocationListener
321 {
322
323 public void allocationAdded(Transaction transaction);
324
325 public void allocationRemoved(List<Transaction.Transfer> transfers,
326 int cDay, int cMonth, int cYear);
327 }
328
329 /** Interface that is used by the components that want to receive updates
330 * notifying them that the data directory has been changed. */
331 public static interface DataSourceListener
332 {
333 public void dataSourceChanged();
334 }
335 }