1 /*****************************************************/
2 /* This java file is a part of the */
4 /* - Plouf's Java IRC Client - */
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
8 /* All contacts : theplouf@yahoo.com */
10 /* PJIRC is free software; you can redistribute */
11 /* it and/or modify it under the terms of the GNU */
12 /* General Public License as published by the */
13 /* Free Software Foundation; version 2 or later of */
16 /* PJIRC is distributed in the hope that it will */
17 /* be useful, but WITHOUT ANY WARRANTY; without */
18 /* even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
20 /* General Public License for more details. */
22 /* You should have received a copy of the GNU */
23 /* General Public License along with PJIRC; if */
24 /* not, write to the Free Software Foundation, */
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
26 /* MA 02111-1307 USA */
28 /*****************************************************/
33 import irc.ChannelListener2;
34 import irc.ModeHandler;
35 import irc.style.StyledList;
36 import irc.tree.SortedList;
38 import java.awt.BorderLayout;
39 import java.awt.Label;
40 import java.awt.Panel;
41 import java.awt.event.MouseEvent;
42 import java.util.Enumeration;
43 import java.util.Hashtable;
48 class NickNameComparator implements irc.tree.Comparator {
49 private char[] _prefixes;
52 * Create a new NickNameComparator
56 public NickNameComparator(char[] prefixes) {
60 private int getPrefixCompareIndex(String nick) {
61 if (nick.length() == 0)
62 return _prefixes.length;
63 for (int i = _prefixes.length - 1; i >= 0; i--)
64 if (nick.charAt(0) == _prefixes[i])
66 return _prefixes.length;
70 public int compare(Object i1, Object i2) {
71 String n1 = (String) i1;
72 String n2 = (String) i2;
73 n1 = n1.toLowerCase(java.util.Locale.ENGLISH).toUpperCase(java.util.Locale.ENGLISH);
74 n2 = n2.toLowerCase(java.util.Locale.ENGLISH).toUpperCase(java.util.Locale.ENGLISH);
75 int prefixCmp = getPrefixCompareIndex(n1) - getPrefixCompareIndex(n2);
81 return n1.compareTo(n2);
88 public class AWTChannel extends BaseAWTSource implements ChannelListener2, PixxNickListListener {
89 private ScrollablePixxNickList _nicks;
91 private SortedList _sortedList;
92 private Hashtable _modeMapping;
93 private NickMenuHandler _menu;
96 * Create a new AWTChannel.
99 * the global irc configuration.
101 * the source channel.
103 public AWTChannel(PixxConfiguration config, Channel c) {
106 _menu = new NickMenuHandler(config, this, c);
107 _nicks = new ScrollablePixxNickList(_pixxConfiguration, c.getIRCServer().getNickPrefixes());
108 c.addChannelListener2(this);
109 _nicks.addPixxNickListListener(this);
110 _sortedList = new SortedList(new NickNameComparator(c.getIRCServer().getNickPrefixes()));
111 _modeMapping = new Hashtable();
112 _label = new Label("");
113 _label.setBackground(_pixxConfiguration.getColor(PixxColorModel.COLOR_BACK));
114 _label.setForeground(_pixxConfiguration.getColor(PixxColorModel.COLOR_WHITE));
115 if (_pixxConfiguration.getIRCConfiguration().getB("asl")) {
116 Panel right = new Panel();
117 right.setLayout(new BorderLayout());
118 right.add(_nicks, BorderLayout.CENTER);
119 Panel outlabel = new Panel();
120 outlabel.setLayout(new BorderLayout());
121 outlabel.add(new PixxSeparator(PixxSeparator.BORDER_LEFT), BorderLayout.WEST);
122 outlabel.add(new PixxSeparator(PixxSeparator.BORDER_RIGHT), BorderLayout.EAST);
123 outlabel.add(new PixxSeparator(PixxSeparator.BORDER_UP), BorderLayout.NORTH);
124 outlabel.add(new PixxSeparator(PixxSeparator.BORDER_DOWN), BorderLayout.SOUTH);
125 outlabel.add(_label, BorderLayout.CENTER);
126 right.add(outlabel, BorderLayout.SOUTH);
127 add(right, BorderLayout.EAST);
129 add(_nicks, BorderLayout.EAST);
134 if (_pixxConfiguration.getB("showchannelyoujoin"))
135 print("*** " + getText(PixxTextProvider.SOURCE_YOU_JOINED_AS, c.getName(), c.getServer().getNick()), 3);
139 public void release() {
141 ((Channel) _source).removeChannelListener2(this);
142 _nicks.removePixxNickListListener(this);
149 public void doLayout() {
155 public void setVisible(boolean b) {
161 private String getFullModeNick(String nick, String mode) {
162 Channel c = (Channel) getSource();
163 char[] _prefixes = c.getIRCServer().getNickPrefixes();
164 char[] _modes = c.getIRCServer().getNickModes();
165 char[][] chanmodes = c.getIRCServer().getChannelModes();
166 ModeHandler h = new ModeHandler(mode, chanmodes, _modes);
167 for (int i = 0; i < _modes.length; i++) {
168 if (h.hasMode(_modes[i]))
169 return _prefixes[i] + nick;
175 private String getUnprefixedNick(String nick) {
176 if (nick.length() == 0)
178 Channel c = (Channel) getSource();
179 char[] _prefixes = c.getIRCServer().getNickPrefixes();
180 for (int i = 0; i < _prefixes.length; i++)
181 if (nick.charAt(0) == _prefixes[i])
182 return nick.substring(1);
186 private void setNicks(String[] nicks) {
187 for (int i = 0; i < nicks.length; i++)
191 private void addNick(String nick) {
192 String mode = ((Channel) _source).getNickMode(nick);
194 String full = getFullModeNick(nick, mode);
195 _sortedList.add(full);
196 _modeMapping.put(nick, full);
200 private void removeNick(String nick) {
201 String full = (String) _modeMapping.get(nick);
203 _sortedList.remove(full);
204 _modeMapping.remove(nick);
208 private void updateNick(String nick) {
213 private void update() {
214 String[] n = new String[_modeMapping.size()];
215 Enumeration e = _modeMapping.keys();
217 while (e.hasMoreElements())
218 n[i++] = (String) e.nextElement();
219 _textField.setCompleteList(n);
221 n = new String[_modeMapping.size()];
222 e = _modeMapping.keys();
224 while (e.hasMoreElements()) {
225 String nick = (String) e.nextElement();
226 n[i++] = nick + ":" + ((Channel) _source).whois(getUnprefixedNick(nick));
228 _list.setNickList(n);
230 n = new String[_sortedList.getSize()];
231 e = _sortedList.getItems();
233 while (e.hasMoreElements()) {
234 String nick = (String) e.nextElement();
235 String whois = ((Channel) _source).whois(getUnprefixedNick(nick));
236 n[i++] = nick + ":" + whois;
244 public synchronized void nickSet(String[] nicks, String[] modes, Channel channel) {
250 public synchronized void nickReset(Channel channel) {
252 _modeMapping.clear();
258 public synchronized void nickJoin(String nick, String mode, Channel channel) {
261 if (_pixxConfiguration.getB("showchannelnickjoin"))
262 print("*** " + getText(PixxTextProvider.SOURCE_HAS_JOINED, nick, _source.getName()), 3);
266 public synchronized void nickPart(String nick, String reason, Channel channel) {
269 if (_pixxConfiguration.getB("showchannelnickpart")) {
270 if (reason.length() > 0)
271 print("*** " + getText(PixxTextProvider.SOURCE_HAS_LEFT, nick, _source.getName()) + " (" + reason + ")", 3);
273 print("*** " + getText(PixxTextProvider.SOURCE_HAS_LEFT, nick, _source.getName()), 3);
278 public synchronized void nickKick(String nick, String by, String reason, Channel channel) {
281 if (_pixxConfiguration.getB("showchannelnickkick")) {
282 if (reason.length() > 0)
283 print("*** " + getText(PixxTextProvider.SOURCE_HAS_BEEN_KICKED_BY, nick, by) + " (" + reason + ")", 3);
285 print("*** " + getText(PixxTextProvider.SOURCE_HAS_BEEN_KICKED_BY, nick, by), 3);
287 if (nick.equals(_source.getServer().getNick())) {
288 _source.getServer().sendStatusMessage(
289 getText(PixxTextProvider.SOURCE_YOU_KICKED, channel.getName(), by) + " (" + reason + ")");
294 public synchronized void nickQuit(String nick, String reason, Channel channel) {
297 if (_pixxConfiguration.getB("showchannelnickquit")) {
298 if (reason.length() > 0)
299 print("*** " + getText(PixxTextProvider.SOURCE_HAS_QUIT, nick) + " (" + reason + ")", 2);
301 print("*** " + getText(PixxTextProvider.SOURCE_HAS_QUIT, nick), 2);
305 private void title() {
306 int count = _sortedList.getSize();
308 if (_pixxConfiguration.getB("displaychannelname"))
309 title += _source.getName();
310 if (_pixxConfiguration.getB("displaychannelcount"))
311 title += " [" + count + "]";
312 if (_pixxConfiguration.getB("displaychannelmode"))
313 title += " [" + ((Channel) _source).getMode() + "]";
314 if (_pixxConfiguration.getB("displaychanneltopic")) {
315 if (title.length() != 0)
316 title += ": " + ((Channel) _source).getTopic();
318 title = ((Channel) _source).getTopic();
320 setTitle(title.trim());
324 public synchronized void topicChanged(String topic, String by, Channel channel) {
325 if (_pixxConfiguration.getB("showchanneltopicchanged")) {
326 if (by.length() == 0)
327 print("*** " + getText(PixxTextProvider.SOURCE_TOPIC_IS, topic), 3);
329 print("*** " + getText(PixxTextProvider.SOURCE_CHANGED_TOPIC, by, topic), 3);
335 public synchronized void modeApply(String mode, String from, Channel channel) {
336 if (_pixxConfiguration.getB("showchannelmodeapply")) {
337 if (from.length() > 0)
338 print("*** " + getText(PixxTextProvider.SOURCE_CHANNEL_MODE, from, mode), 3);
340 print("*** " + getText(PixxTextProvider.SOURCE_CHANNEL_MODE_IS, mode), 3);
346 public synchronized void nickModeApply(String nick, String mode, String from, Channel channel) {
347 if (_pixxConfiguration.getB("showchannelnickmodeapply"))
348 print("*** " + getText(PixxTextProvider.SOURCE_USER_MODE, from, mode, nick), 3);
354 public synchronized void nickChanged(String oldNick, String newNick, Channel channel) {
355 if (_pixxConfiguration.getB("showchannelnickchanged"))
356 print("*** " + getText(PixxTextProvider.SOURCE_KNOWN_AS, oldNick, newNick), 3);
363 public void nickWhoisUpdated(String nick, String whois, Channel channel) {
368 public void eventOccured(String nick, MouseEvent e) {
369 if (_pixxConfiguration.matchMouseConfiguration("nickpopup", e)) {
370 _menu.popup(nick, ((Channel) _source).whois(nick), _nicks, e.getX(), e.getY());
371 } else if (_pixxConfiguration.matchMouseConfiguration("nickquery", e)) {
372 if (_pixxConfiguration.getB("automaticqueries")) {
373 if (!nick.equals(_source.getServer().getNick())) {
374 _source.sendString("/focus Query " + nick);
375 _source.sendString("/query " + nick);
382 public void ASLEventOccured(String nick, String info) {
383 _label.setText(_pixxConfiguration.getIRCConfiguration().formatASL(info));
387 public void nickEvent(StyledList lis, String nick, MouseEvent e) {
388 if (_pixxConfiguration.matchMouseConfiguration("nickpopup", e)) {
389 _menu.popup(nick, ((Channel) _source).whois(nick), _list, e.getX(), e.getY());
391 super.nickEvent(lis, nick, e);