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  
23  public abstract class AbstractChatProvider implements ChatProvider {
24  
25      // --- Constant(s) ---
26  
27      //--- Data field(s) ---
28      
29      private String name;
30      private ChatProviderSupport cps = new ChatProviderSupport(this);
31      private StringBuffer msgBuffer = new StringBuffer();    
32      private int maxMsgBufferSize = Integer.MAX_VALUE;
33  
34      //--- Constructor(s) ---
35      
36      public AbstractChatProvider(String name)
37      {
38  		this.name = name;
39      }
40  
41      public AbstractChatProvider()
42      {
43  		this(null);
44      }
45  
46      //--- Method(s) ---
47  
48      public void addChatProviderListener(ChatProviderListener l)
49      {
50  		cps.addChatProviderListener(l);
51      }
52  
53      public void removeChatProviderListener(ChatProviderListener l)
54      {
55  		cps.removeChatProviderListener(l);
56      }
57  
58      public String getMessages()
59      {
60  		return msgBuffer.toString();
61      }
62  
63      public String getName()
64      {
65  		return name;
66      }
67  
68      /***
69       * Sets the maximum number of characters for the message buffer.
70       */
71      public void setMessageBufferSize(int maxSize)
72      {
73  		maxMsgBufferSize = maxSize;
74      }
75  
76      public void setName(String name)
77      {
78  		this.name = name;
79      }
80  
81      public void channelsUpdated()
82      {
83  		cps.fireChannelsUpdated();
84      }
85  
86      public void messageReceived(String message)
87      {
88  		msgBuffer.append(message);
89  		if (msgBuffer.length() > maxMsgBufferSize) {
90  			int offset = msgBuffer.length() - maxMsgBufferSize;
91  			while (offset >= 0) {
92  				if (msgBuffer.charAt(offset) == '\n') {
93  					msgBuffer.delete(0, offset);
94  					break;
95  				}
96  				offset--;
97  			}
98  		}
99  		cps.fireMessageReceived(message);
100     }
101 
102     /***
103      * Returns the value of {@link #getName()}.
104      */
105     public String toString()
106     {
107 		return getName();
108     }
109 
110 }