]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/pixx/AWTChannel.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / gui / pixx / AWTChannel.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
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  */
14 /*  the License.                                     */
15 /*                                                   */
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.         */
21 /*                                                   */
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                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc.gui.pixx;
31
32 import irc.Channel;
33 import irc.ChannelListener2;
34 import irc.ModeHandler;
35 import irc.style.StyledList;
36 import irc.tree.SortedList;
37
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;
44
45 /**
46  * NickNameComparator.
47  */
48 class NickNameComparator implements irc.tree.Comparator {
49         private char[] _prefixes;
50
51         /**
52          * Create a new NickNameComparator
53          * 
54          * @param prefixes
55          */
56         public NickNameComparator(char[] prefixes) {
57                 _prefixes = prefixes;
58         }
59
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])
65                                 return i;
66                 return _prefixes.length;
67         }
68
69         @Override
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);
76                 if (prefixCmp < 0)
77                         return -1;
78                 if (prefixCmp > 0)
79                         return 1;
80
81                 return n1.compareTo(n2);
82         }
83 }
84
85 /**
86  * The AWT Channel.
87  */
88 public class AWTChannel extends BaseAWTSource implements ChannelListener2, PixxNickListListener {
89         private ScrollablePixxNickList _nicks;
90         private Label _label;
91         private SortedList _sortedList;
92         private Hashtable _modeMapping;
93         private NickMenuHandler _menu;
94
95         /**
96          * Create a new AWTChannel.
97          * 
98          * @param config
99          *          the global irc configuration.
100          * @param c
101          *          the source channel.
102          */
103         public AWTChannel(PixxConfiguration config, Channel c) {
104                 super(config, c);
105
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);
128                 } else {
129                         add(_nicks, BorderLayout.EAST);
130                 }
131                 doLayout();
132                 title();
133
134                 if (_pixxConfiguration.getB("showchannelyoujoin"))
135                         print("*** " + getText(PixxTextProvider.SOURCE_YOU_JOINED_AS, c.getName(), c.getServer().getNick()), 3);
136         }
137
138         @Override
139         public void release() {
140                 _menu.release();
141                 ((Channel) _source).removeChannelListener2(this);
142                 _nicks.removePixxNickListListener(this);
143                 _nicks.release();
144                 _menu = null;
145                 super.release();
146         }
147
148         @Override
149         public void doLayout() {
150                 _label.setText("");
151                 super.doLayout();
152         }
153
154         @Override
155         public void setVisible(boolean b) {
156                 super.setVisible(b);
157                 if (!b)
158                         _nicks.dispose();
159         }
160
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;
170                 }
171
172                 return nick;
173         }
174
175         private String getUnprefixedNick(String nick) {
176                 if (nick.length() == 0)
177                         return nick;
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);
183                 return nick;
184         }
185
186         private void setNicks(String[] nicks) {
187                 for (int i = 0; i < nicks.length; i++)
188                         addNick(nicks[i]);
189         }
190
191         private void addNick(String nick) {
192                 String mode = ((Channel) _source).getNickMode(nick);
193                 if (mode != null) {
194                         String full = getFullModeNick(nick, mode);
195                         _sortedList.add(full);
196                         _modeMapping.put(nick, full);
197                 }
198         }
199
200         private void removeNick(String nick) {
201                 String full = (String) _modeMapping.get(nick);
202                 if (full != null) {
203                         _sortedList.remove(full);
204                         _modeMapping.remove(nick);
205                 }
206         }
207
208         private void updateNick(String nick) {
209                 removeNick(nick);
210                 addNick(nick);
211         }
212
213         private void update() {
214                 String[] n = new String[_modeMapping.size()];
215                 Enumeration e = _modeMapping.keys();
216                 int i = 0;
217                 while (e.hasMoreElements())
218                         n[i++] = (String) e.nextElement();
219                 _textField.setCompleteList(n);
220
221                 n = new String[_modeMapping.size()];
222                 e = _modeMapping.keys();
223                 i = 0;
224                 while (e.hasMoreElements()) {
225                         String nick = (String) e.nextElement();
226                         n[i++] = nick + ":" + ((Channel) _source).whois(getUnprefixedNick(nick));
227                 }
228                 _list.setNickList(n);
229
230                 n = new String[_sortedList.getSize()];
231                 e = _sortedList.getItems();
232                 i = 0;
233                 while (e.hasMoreElements()) {
234                         String nick = (String) e.nextElement();
235                         String whois = ((Channel) _source).whois(getUnprefixedNick(nick));
236                         n[i++] = nick + ":" + whois;
237                 }
238
239                 _nicks.set(n);
240                 title();
241         }
242
243         @Override
244         public synchronized void nickSet(String[] nicks, String[] modes, Channel channel) {
245                 setNicks(nicks);
246                 update();
247         }
248
249         @Override
250         public synchronized void nickReset(Channel channel) {
251                 _sortedList.clear();
252                 _modeMapping.clear();
253                 _nicks.removeAll();
254                 update();
255         }
256
257         @Override
258         public synchronized void nickJoin(String nick, String mode, Channel channel) {
259                 addNick(nick);
260                 update();
261                 if (_pixxConfiguration.getB("showchannelnickjoin"))
262                         print("*** " + getText(PixxTextProvider.SOURCE_HAS_JOINED, nick, _source.getName()), 3);
263         }
264
265         @Override
266         public synchronized void nickPart(String nick, String reason, Channel channel) {
267                 removeNick(nick);
268                 update();
269                 if (_pixxConfiguration.getB("showchannelnickpart")) {
270                         if (reason.length() > 0)
271                                 print("*** " + getText(PixxTextProvider.SOURCE_HAS_LEFT, nick, _source.getName()) + " (" + reason + ")", 3);
272                         else
273                                 print("*** " + getText(PixxTextProvider.SOURCE_HAS_LEFT, nick, _source.getName()), 3);
274                 }
275         }
276
277         @Override
278         public synchronized void nickKick(String nick, String by, String reason, Channel channel) {
279                 removeNick(nick);
280                 update();
281                 if (_pixxConfiguration.getB("showchannelnickkick")) {
282                         if (reason.length() > 0)
283                                 print("*** " + getText(PixxTextProvider.SOURCE_HAS_BEEN_KICKED_BY, nick, by) + " (" + reason + ")", 3);
284                         else
285                                 print("*** " + getText(PixxTextProvider.SOURCE_HAS_BEEN_KICKED_BY, nick, by), 3);
286                 }
287                 if (nick.equals(_source.getServer().getNick())) {
288                         _source.getServer().sendStatusMessage(
289                                         getText(PixxTextProvider.SOURCE_YOU_KICKED, channel.getName(), by) + " (" + reason + ")");
290                 }
291         }
292
293         @Override
294         public synchronized void nickQuit(String nick, String reason, Channel channel) {
295                 removeNick(nick);
296                 update();
297                 if (_pixxConfiguration.getB("showchannelnickquit")) {
298                         if (reason.length() > 0)
299                                 print("*** " + getText(PixxTextProvider.SOURCE_HAS_QUIT, nick) + " (" + reason + ")", 2);
300                         else
301                                 print("*** " + getText(PixxTextProvider.SOURCE_HAS_QUIT, nick), 2);
302                 }
303         }
304
305         private void title() {
306                 int count = _sortedList.getSize();
307                 String title = "";
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();
317                         else
318                                 title = ((Channel) _source).getTopic();
319                 }
320                 setTitle(title.trim());
321         }
322
323         @Override
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);
328                         else
329                                 print("*** " + getText(PixxTextProvider.SOURCE_CHANGED_TOPIC, by, topic), 3);
330                 }
331                 title();
332         }
333
334         @Override
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);
339                         else
340                                 print("*** " + getText(PixxTextProvider.SOURCE_CHANNEL_MODE_IS, mode), 3);
341                 }
342                 title();
343         }
344
345         @Override
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);
349                 updateNick(nick);
350                 update();
351         }
352
353         @Override
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);
357                 removeNick(oldNick);
358                 addNick(newNick);
359                 update();
360         }
361
362         @Override
363         public void nickWhoisUpdated(String nick, String whois, Channel channel) {
364                 update();
365         }
366
367         @Override
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);
376                                 }
377                         }
378                 }
379         }
380
381         @Override
382         public void ASLEventOccured(String nick, String info) {
383                 _label.setText(_pixxConfiguration.getIRCConfiguration().formatASL(info));
384         }
385
386         @Override
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());
390                 } else {
391                         super.nickEvent(lis, nick, e);
392                 }
393         }
394 }