1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.event.ContainerAdapter;
25 import java.awt.event.ContainerEvent;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28
29 import javax.swing.JPanel;
30 import javax.swing.SwingUtilities;
31 import javax.swing.event.ChangeListener;
32
33 import org.xnap.XNap;
34 import org.xnap.chat.Channel;
35 import org.xnap.chat.ChannelEvent;
36 import org.xnap.chat.ChannelListener;
37 import org.xnap.chat.ChatManager;
38 import org.xnap.event.ListEvent;
39 import org.xnap.event.ListListener;
40 import org.xnap.gui.component.CloseableTabbedPane;
41 import org.xnap.gui.util.*;
42
43 /***
44 * Provides a {@link CloseableTabbedPane} that displays the {@link
45 * ChatProviderPanel} and a {@link ChannelPanel} for each joined channels
46 * as tabs.
47 */
48 public class ChatPanel extends JPanel implements ListListener {
49
50
51
52 private CloseableTabbedPane jtp;
53 private ChatProviderPanel jpProviders;
54
55
56
57 public ChatPanel()
58 {
59 initialize();
60
61 Channel[] channels = ChatManager.getInstance().getChannels();
62 for (int i = 0; i < channels.length; i++) {
63 add(channels[i]);
64 }
65
66 ChatManager.getInstance().addChannelListListener(this);
67 }
68
69
70
71 private void initialize()
72 {
73 jtp = new CloseableTabbedPane();
74 jtp.addContainerListener(new TabListener());
75
76 jpProviders = new ChatProviderPanel();
77 jtp.addTab(XNap.tr("Global"), jpProviders, null, false);
78
79 setLayout(new BorderLayout());
80 add(jtp, BorderLayout.CENTER);
81 }
82
83 public ChannelPanel add(Channel channel)
84 {
85 ChannelPanel cp = new ChannelPanel(this, channel);
86 jtp.addTab(channel.getName(), cp, channel.getIcon());
87 return cp;
88 }
89
90 public CloseableTabbedPane getPane()
91 {
92 return jtp;
93 }
94
95 /***
96 * Invoked when a channel has been joined.
97 */
98 public void itemAdded(ListEvent e)
99 {
100 final Channel channel = (Channel)e.getItem();
101
102 final ChannelEventBuffer listener = new ChannelEventBuffer();
103 channel.addChannelListener(listener);
104
105 Runnable runner = new Runnable()
106 {
107 public void run()
108 {
109 channel.removeChannelListener(listener);
110
111 ChannelPanel cp = add(channel);
112
113 for (Iterator i = listener.buffer.iterator();
114 i.hasNext();) {
115 cp.messageReceived((ChannelEvent)i.next());
116 }
117
118 }
119 };
120 SwingUtilities.invokeLater(runner);
121 }
122
123 /***
124 * Invoked when a channel is removed.
125 */
126 public synchronized void itemRemoved(final ListEvent e)
127 {
128 final Channel channel = (Channel)e.getItem();
129 Runnable runner = new Runnable()
130 {
131 public void run()
132 {
133 for (int i = jtp.getComponentCount() - 1; i > 0; i--) {
134 ChannelPanel cp = (ChannelPanel)jtp.getComponentAt(i);
135 if (cp.getChannel() == channel) {
136 jtp.remove(i);
137 return;
138 }
139 }
140 }
141 };
142
143 SwingUtilities.invokeLater(runner);
144 }
145
146 /***
147 * Called by {@link XNapFrame} to update the toolbar when the active
148 * tab is changed.
149 */
150 public void addChangeListener(ChangeListener l)
151 {
152 jtp.addChangeListener(l);
153 }
154
155 /***
156 * Called by {@link XNapFrame} to update get the active tab.
157 */
158 public Component getSelectedTab()
159 {
160 return jtp.getSelectedComponent();
161 }
162
163 public void savePrefs()
164 {
165 jpProviders.savePrefs();
166 }
167
168 /***
169 * Sets the title of <code>tab</code> to <code>title</code>.
170 *
171 * @param tab the search result panel
172 * @param title the title
173 */
174 public void setTabTitle(Component tab, String title)
175 {
176 int i = jtp.indexOfComponent(tab);
177 if (i != -1) {
178 jtp.setTitleAt(i, title);
179 }
180 }
181
182
183
184 private class TabListener extends ContainerAdapter
185 {
186
187 /***
188 * Invoked when a tab is closed.
189 */
190 public void componentRemoved(ContainerEvent e)
191 {
192 ChannelPanel cp = (ChannelPanel)e.getChild();
193 cp.getChannel().close();
194 ChatManager.getInstance().remove(cp.getChannel());
195 }
196 }
197
198 private class ChannelEventBuffer implements ChannelListener
199 {
200
201 LinkedList buffer = new LinkedList();
202
203 public void messageReceived(ChannelEvent event)
204 {
205 buffer.add(event);
206 }
207
208 public void channelJoined(ChannelEvent event)
209 {
210 }
211
212 public void channelParted(ChannelEvent event)
213 {
214 }
215
216 public void peerAdded(ChannelEvent event)
217 {
218 }
219
220 public void peerChanged(ChannelEvent event)
221 {
222 }
223
224 public void peerRemoved(ChannelEvent event)
225 {
226 }
227
228 public void topicChanged(ChannelEvent event)
229 {
230 }
231
232 }
233 }