1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.chat;
21
22
23 public abstract class AbstractChatProvider implements ChatProvider {
24
25
26
27
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
35
36 public AbstractChatProvider(String name)
37 {
38 this.name = name;
39 }
40
41 public AbstractChatProvider()
42 {
43 this(null);
44 }
45
46
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 }