]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/pixx/AWTChanList.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 / AWTChanList.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc.gui.pixx;\r
31 \r
32 import irc.ChanList;\r
33 import irc.ChanListListener;\r
34 import irc.ChannelInfo;\r
35 import irc.style.StyledList;\r
36 \r
37 import java.awt.BorderLayout;\r
38 \r
39 /**\r
40  * The AWT channel list.\r
41  */\r
42 public class AWTChanList extends BaseAWTSource implements ChanListListener {\r
43         /**\r
44          * Horizontal scrollbar.\r
45          */\r
46         protected PixxHorizontalScrollBar _hscroll;\r
47 \r
48         /**\r
49          * Create a new AWTChanList.\r
50          * \r
51          * @param config\r
52          *          the global irc configuration.\r
53          * @param list\r
54          *          the source channel list.\r
55          */\r
56         public AWTChanList(PixxConfiguration config, ChanList list) {\r
57                 super(config, list, true);\r
58 \r
59                 list.addChanListListener(this);\r
60 \r
61                 _hscroll = new PixxHorizontalScrollBar(_pixxConfiguration, 0, 0, 0.1);\r
62                 _hscroll.addPixxScrollBarListener(this);\r
63 \r
64                 _list.setWrap(false);\r
65 \r
66                 remove(_textField);\r
67                 add(_hscroll, BorderLayout.SOUTH);\r
68 \r
69                 setTitle(getText(PixxTextProvider.SOURCE_CHANLIST, getChanList().getName()));\r
70                 _list.clear(1024);\r
71         }\r
72 \r
73         @Override\r
74         public void release() {\r
75                 ((ChanList) _source).removeChanListListeners(this);\r
76                 _hscroll.removePixxScrollBarListener(this);\r
77                 _hscroll.release();\r
78                 _hscroll = null;\r
79                 super.release();\r
80         }\r
81 \r
82         @Override\r
83         public String getShortTitle() {\r
84                 return getText(PixxTextProvider.GUI_CHANNELS);\r
85         }\r
86 \r
87         @Override\r
88         public void setFieldText(String txt) {\r
89         }\r
90 \r
91         @Override\r
92         public String getFieldText() {\r
93                 return "";\r
94         }\r
95 \r
96         @Override\r
97         public void validateText() {\r
98         }\r
99 \r
100         /**\r
101          * Get the source chanlist.\r
102          * \r
103          * @return source chanlist.\r
104          */\r
105         public ChanList getChanList() {\r
106                 return (ChanList) getSource();\r
107         }\r
108 \r
109         @Override\r
110         public void channelBegin(ChanList list) {\r
111                 clear(getSource());\r
112                 print(_pixxConfiguration.getText(PixxTextProvider.SOURCE_CHANLIST_RETREIVING));\r
113                 _list.setFirst(0);\r
114         }\r
115 \r
116         private void sort(ChannelInfo[] info, int begin, int end, int deep) {\r
117                 if (deep < 50) {\r
118                         if (begin < end) {\r
119                                 ChannelInfo tmp;\r
120 \r
121                                 int f = (begin + end) / 2;\r
122                                 tmp = info[f];\r
123                                 info[f] = info[begin];\r
124                                 info[begin] = tmp;\r
125 \r
126                                 int p_pos = begin;\r
127                                 ChannelInfo pivot = info[p_pos];\r
128                                 for (int i = begin; i <= end; i++) {\r
129                                         if (info[i].userCount > pivot.userCount) {\r
130                                                 p_pos++;\r
131                                                 tmp = info[p_pos];\r
132                                                 info[p_pos] = info[i];\r
133                                                 info[i] = tmp;\r
134                                         }\r
135                                 }\r
136                                 tmp = info[p_pos];\r
137                                 info[p_pos] = info[begin];\r
138                                 info[begin] = tmp;\r
139 \r
140                                 sort(info, begin, p_pos - 1, deep + 1);\r
141                                 sort(info, p_pos + 1, end, deep + 1);\r
142                         }\r
143                 } else {\r
144                         for (int i = begin; i <= end; i++) {\r
145                                 ChannelInfo little = info[i];\r
146                                 int littleindex = i;\r
147                                 int littleuser = little.userCount;\r
148                                 for (int j = i + 1; j <= end; j++) {\r
149                                         if (info[j].userCount > littleuser) {\r
150                                                 little = info[j];\r
151                                                 littleindex = j;\r
152                                                 littleuser = little.userCount;\r
153                                         }\r
154                                 }\r
155 \r
156                                 ChannelInfo tmp = info[i];\r
157                                 info[i] = info[littleindex];\r
158                                 info[littleindex] = tmp;\r
159                         }\r
160                 }\r
161         }\r
162 \r
163         private void sort(ChannelInfo[] info) {\r
164                 sort(info, 0, info.length - 1, 0);\r
165         }\r
166 \r
167         @Override\r
168         public void channelEnd(ChanList list) {\r
169                 ChannelInfo[] info = getChanList().getChannels();\r
170                 sort(info);\r
171                 int count = info.length;\r
172                 if (count > 1024)\r
173                         count = 1024;\r
174                 String[] lines = new String[count];\r
175                 for (int i = 0; i < count; i++)\r
176                         lines[i] = format(info[i]);\r
177 \r
178                 clear(getSource());\r
179                 _list.addLines(lines);\r
180                 _list.setFirst(0);\r
181                 _scroll.setMaximum(_list.getLineCount() - 1);\r
182                 _scroll.setValue(_list.getLast());\r
183                 _hscroll.setMaximum(_list.getLogicalWidth() / 10);\r
184         }\r
185 \r
186         private String format(ChannelInfo item) {\r
187                 String msg = item.name;\r
188                 String count = "" + item.userCount;\r
189                 for (int i = 0; i < 20 - item.name.length(); i++)\r
190                         msg += " ";\r
191                 msg += "   " + item.userCount;\r
192                 for (int i = 0; i < 5 - count.length(); i++)\r
193                         msg += " ";\r
194                 msg += "   " + item.topic;\r
195                 return msg;\r
196         }\r
197 \r
198         @Override\r
199         public void channelAdded(ChannelInfo item, ChanList list) {\r
200                 int count = getChanList().getChannelCount();\r
201                 int total = getChanList().getIgnoredChannelCount() + count;\r
202                 if (total % 100 == 0) {\r
203                         clear(getSource());\r
204                         print(_pixxConfiguration.getText(PixxTextProvider.SOURCE_CHANLIST_RETREIVING) + " (" + count + "/" + total + ")");\r
205                         _list.setFirst(0);\r
206                 }\r
207         }\r
208 \r
209         @Override\r
210         public void valueChanged(PixxScrollBar pixScrollBar) {\r
211                 if (pixScrollBar == _hscroll)\r
212                         _list.setLeft(_hscroll.getValue() * 10);\r
213                 else\r
214                         super.valueChanged(pixScrollBar);\r
215         }\r
216 \r
217         @Override\r
218         public void virtualSizeChanged(StyledList lis) {\r
219                 _hscroll.setMaximum(_list.getLogicalWidth() / 10);\r
220                 super.virtualSizeChanged(lis);\r
221         }\r
222 }\r