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.Collections;
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  import javax.swing.Icon;
27  
28  import org.xnap.XNap;
29  import org.xnap.cmdl.Command;
30  import org.xnap.peer.Peer;
31  
32  public abstract class AbstractChannel implements Channel {
33  
34      // --- Constant(s) ---
35  
36      //--- Data field(s) ---
37      
38      private String name;
39      private String topic;
40      private List peers = Collections.synchronizedList(new LinkedList());
41  
42      protected ChannelSupport cs = new ChannelSupport(this);
43  
44      //--- Constructor(s) ---
45      
46      public AbstractChannel(String name)
47      {
48  		this.name = name;
49      }
50  
51      //--- Method(s) ---
52  
53      public void actionMessageReceived(Peer sender, String message)
54      {
55  		cs.fireMessageReceived(sender, message, 
56  							   ChannelEvent.MESSAGE_TYPE_ACTION_MESSAGE);
57      }
58  
59      public void add(Peer peer)
60      {
61  		peers.add(peer);
62  		cs.firePeerAdded(peer);
63      }
64  
65      public Peer[] getPeers()
66      {
67  		return (Peer[])peers.toArray(new Peer[0]);
68      }
69  
70      public void remove(Peer peer)
71      {
72  		peers.remove(peer);
73  		cs.firePeerRemoved(peer);
74      }
75  
76      public void addChannelListener(ChannelListener l)
77      {
78  		cs.addChannelListener(l);
79      }
80  
81      public void removeChannelListener(ChannelListener l)
82      {
83  		cs.removeChannelListener(l);
84      }
85  
86      /***
87       * Returns null.
88       */
89      public Command[] getCommands()
90  	{
91  		return null;
92  	}
93  
94      /***
95       * Returns null.
96       */
97      public Icon getIcon()
98      {
99  		return null;
100     }
101 
102     public String getName()
103     {
104 		return name;
105     }
106 
107     public int getPeerCount()
108     {
109 		return peers.size();
110     }
111 
112     /***
113      * Returns the empty string.
114      */
115     public String getPrefix(Peer peer)
116     {
117 		return "";
118     }
119 
120     public String getTopic()
121     {
122 		return topic;
123     }
124 
125     public void setTopic(String newValue)
126     {
127 		topic = newValue;
128 		cs.fireTopicChanged(topic);
129     }
130 
131     public void joined() 
132     {
133 		infoReceived(XNap.tr("You are now talking on {0}.", getName()));
134 		cs.fireChannelJoined();
135     }
136 
137     public void parted(String reason) 
138     {
139 		if (reason != null) {
140 			infoReceived(XNap.tr("You have left {0} ({1}).", getName(), 
141 								 reason));
142 		}
143 		else {
144 			infoReceived(XNap.tr("You have left {0}.", getName()));
145 		}
146 		cs.fireChannelParted();
147     }
148 
149     public void errorReceived(String message)
150     {
151 		cs.fireMessageReceived(message, ChannelEvent.MESSAGE_TYPE_ERROR);
152     }
153 
154     public void infoReceived(String message)
155     {
156 		cs.fireMessageReceived(message, ChannelEvent.MESSAGE_TYPE_INFO);
157     }
158 
159     public void messageReceived(Peer sender, String message)
160     {
161 		cs.fireMessageReceived(sender, message, 
162 							   ChannelEvent.MESSAGE_TYPE_MESSAGE);
163     }
164 
165     public void peerChanged(Peer peer)
166     {
167 		cs.firePeerChanged(peer);
168     }
169 
170     public void topicChanged(String newTopic)
171     {
172 		cs.fireTopicChanged(newTopic);
173     }
174     
175 }