View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.chat;
21  
22  import java.util.List;
23  import java.util.Vector;
24  
25  import org.xnap.peer.Peer;
26  
27  /***
28   * This is a utility class that can be used by chat {@link Channel}
29   * classes for handling channel event listeners. 
30   */
31  public class ChannelSupport { 
32  
33      //--- Data field(s) ---
34  
35      private Object source;
36      private List listeners = new Vector();
37  
38      //--- Constructor(s) ---
39      
40      /***
41       * Constructs a ChannelSupport object.
42       *
43       * @param source the object to be given as the source for any events
44       */
45      public ChannelSupport(Object source)
46      {
47  		this.source = source;
48      }
49  
50      //--- Method(s) ---
51  
52      public void addChannelListener(ChannelListener l) 
53      {
54          listeners.add(l);
55      }
56  
57      public void removeChannelListener(ChannelListener l) 
58      {
59          listeners.remove(l);
60      }
61  
62      public void fireChannelJoined()
63      {
64          Object[] array = listeners.toArray();
65  		if (array != null) {
66  			ChannelEvent event 
67  				= new ChannelEvent(source, ChannelEvent.CHANNEL_JOINED);
68  
69  			for (int i = array.length - 1; i >= 0; i--) {
70  				((ChannelListener)array[i]).channelJoined(event);
71  			}
72  		}
73      }
74  
75      public void fireChannelParted()
76      {
77          Object[] array = listeners.toArray();
78  		if (array != null) {
79  			ChannelEvent event 
80  				= new ChannelEvent(source, ChannelEvent.CHANNEL_PARTED);
81  
82  			for (int i = array.length - 1; i >= 0; i--) {
83  				((ChannelListener)array[i]).channelParted(event);
84  			}
85  		}
86      }
87          
88      public void fireMessageReceived(Peer sender, String message, 
89  									int messageType)
90      {
91          Object[] array = listeners.toArray();
92  
93  		if (array != null) {
94  			ChannelEvent event 
95  				= new ChannelEvent(source, sender, message, messageType);
96  
97  			for (int i = array.length - 1; i >= 0; i--) {
98  				((ChannelListener)array[i]).messageReceived(event);
99  			}
100 		}
101     }   
102 
103     public void fireMessageReceived(String message, int messageType)
104     {
105 		fireMessageReceived(null, message, messageType);
106     }
107 
108     public void firePeerAdded(Peer peer)
109     {
110         Object[] array = listeners.toArray();
111 
112 		if (array != null) {
113 			ChannelEvent event 
114 				= new ChannelEvent(source, ChannelEvent.PEER_ADDED, peer);
115 
116 			for (int i = array.length - 1; i >= 0; i--) {
117 				((ChannelListener)array[i]).peerAdded(event);
118 			}
119 		}
120     }   
121 
122     public void firePeerChanged(Peer peer)
123     {
124         Object[] array = listeners.toArray();
125 
126 		if (array != null) {
127 			ChannelEvent event 
128 				= new ChannelEvent(source, ChannelEvent.PEER_CHANGED, peer);
129 
130 			for (int i = array.length - 1; i >= 0; i--) {
131 				((ChannelListener)array[i]).peerChanged(event);
132 			}
133 		}
134     }   
135 
136     public void firePeerRemoved(Peer peer)
137     {
138         Object[] array = listeners.toArray();
139 
140 		if (array != null) {
141 			ChannelEvent event 
142 				= new ChannelEvent(source, ChannelEvent.PEER_REMOVED, peer);
143 
144 			for (int i = array.length - 1; i >= 0; i--) {
145 				((ChannelListener)array[i]).peerRemoved(event);
146 			}
147 		}
148     }   
149 
150     public void fireTopicChanged(String topic)
151     {
152         Object[] array = listeners.toArray();
153 
154 		if (array != null) {
155 			ChannelEvent event = new ChannelEvent(source, topic);
156 
157 			for (int i = array.length - 1; i >= 0; i--) {
158 				((ChannelListener)array[i]).topicChanged(event);
159 			}
160 		}
161     }   
162 
163 }