01 /*
02 * Created on Jul 3, 2004
03 *
04 * To change the template for this generated file go to
05 * Window - Preferences - Java - Code Generation - Code and Comments
06 */
07 package biz.xsoftware.examples.listener;
08
09 import java.util.HashSet;
10 import java.util.Set;
11
12 import javax.swing.event.EventListenerList;
13
14 /**
15 *
16 * @author Dean Hiller
17 */
18 public class UserServiceImpl implements UserService {
19
20 private EventListenerList listenerList = new EventListenerList();
21 private Set<String> names = new HashSet<String>();
22
23 public UserServiceImpl() {
24 }
25
26 /**
27 * @see biz.xsoftware.examples.listener.UserService#addUser(java.lang.String)
28 */
29 public void addUser(String userName) {
30 if(names.contains(userName))
31 throw new IllegalArgumentException("The user="+userName+" is already in the system");
32 names.add(userName);
33 fireUserAdded(userName);
34 }
35
36 /**
37 * @see biz.xsoftware.examples.listener.UserService#deleteUser(java.lang.String)
38 */
39 public void deleteUser(String userName) {
40 boolean isRemoved = names.remove(userName);
41 if(!isRemoved)
42 throw new IllegalArgumentException("The user="+userName+" is not in the system");
43 fireUserDeleted(userName);
44 }
45
46 /**
47 * @see biz.xsoftware.examples.listener.UserService#addUserListener(biz.xsoftware.examples.listener.UserListener)
48 */
49 public void addUserListener(UserListener l) {
50 listenerList.add(UserListener.class, l);
51 }
52 /**
53 * @see biz.xsoftware.examples.listener.UserService#removeUserListener(biz.xsoftware.examples.listener.UserListener)
54 */
55 public void removeUserListener(UserListener l) {
56 listenerList.remove(UserListener.class, l);
57 }
58
59 protected void fireUserAdded(String userName) {
60 // Guaranteed to return a non-null array
61 Object[] listeners = listenerList.getListenerList();
62 // Process the listeners last to first, notifying
63 // those that are interested in this event
64 for (int i = listeners.length-2; i>=0; i-=2) {
65 if (listeners[i]==UserListener.class) {
66 // Lazily create the event:
67 ((UserListener)listeners[i+1]).userAdded(userName);
68 }
69 }
70 }
71
72 protected void fireUserDeleted(String userName) {
73 // Guaranteed to return a non-null array
74 Object[] listeners = listenerList.getListenerList();
75 // Process the listeners last to first, notifying
76 // those that are interested in this event
77 for (int i = listeners.length-2; i>=0; i-=2) {
78 if (listeners[i]==UserListener.class) {
79 // Lazily create the event:
80 ((UserListener)listeners[i+1]).userDeleted(userName);
81 }
82 }
83 }
84 }
|