Source code: org/relayirc/swingui/UserList.java
1
2 /*
3 * FILE: UserList.java
4 *
5 * The contents of this file are subject to the Mozilla Public License
6 * Version 1.0 (the "License"); you may not use this file except in
7 * compliance with the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
12 * License for the specific language governing rights and limitations
13 * under the License.
14 *
15 * The Original Code is Relay IRC chat client.
16 *
17 * The Initial Developer of the Original Code is David M. Johnson.
18 * Portions created by David M. Johnson are Copyright (C) 1998.
19 * All Rights Reserved.
20 *
21 * Contributor(s): No contributors to this file.'
22 */
23 package org.relayirc.swingui;
24 import org.relayirc.chatengine.*;
25 import org.relayirc.swingutil.*;
26 import org.relayirc.util.*;
27
28 import java.awt.*;
29 import java.util.Hashtable;
30 import java.awt.event.*;
31 import javax.swing.*;
32
33 ///////////////////////////////////////////////////////////////////////
34 /**
35 * <p>List component which displays the users currently present in a
36 * chat channel. Provides a popup menu so that the user may request
37 * WHOIS, VERSION or PING information on specific users.</p>
38 *
39 * <p>Operators (users whose nicks start with @) are listed at the
40 * beginnning of the list.</p>
41 *
42 * FIX: List should be sorted alphabetically (with ops listed first).<br>
43 */
44 public class UserList extends JList {
45 private IChatEngine _engine;
46 private Channel _channel;
47 private String _selUser; // kludge around Linux popup bug
48 private Hashtable _wrappersByName = new Hashtable();
49
50 /** Construct a user list for a specified channel. */
51 public UserList(Channel chan ) {
52 super(new DefaultListModel());
53 _engine = ChatApp.getChatApp().getEngine();
54 _channel = chan;
55
56 setCellRenderer(new GuiListCellRenderer(this));
57
58 setFont(new Font(
59 ChatApp.getChatApp().getOptions().getFontName(),
60 ChatApp.getChatApp().getOptions().getFontStyle(),
61 ChatApp.getChatApp().getOptions().getFontSize()));
62
63 addMouseListener( new MouseAdapter() {
64 });
65 }
66 //-----------------------------------------------------------------
67 /**
68 * Returns true if the specified user is in the list. Checks the user
69 * name and @name in case the user is an operator.
70 */
71 public boolean contains(String nick) {
72 return _wrappersByName.containsKey(nick) || _wrappersByName.containsKey("@"+nick);
73 }
74 //-----------------------------------------------------------------
75 /**
76 * Adds the specified user to the list. If the user's is an operator
77 * with nick name of the format @name, then user is treated as an
78 * operator and added to the beginning of the list.
79 */
80 public void addItem(String nick) {
81
82 if (nick.substring(0,1).equals("@")) {
83
84 GuiObject go = (GuiObject)_wrappersByName.get(nick);
85 if (go!=null) {
86 ((DefaultListModel)getModel()).removeElement(go);
87 }
88
89 go = (GuiObject)_wrappersByName.get(nick.substring(1));
90 if (go!=null) {
91 ((DefaultListModel)getModel()).removeElement(go);
92 }
93
94 go = new GuiObject(nick,nick,new ImageIcon(getClass().getResource("images/User.gif")));
95 ((DefaultListModel)getModel()).insertElementAt(go,0);
96
97 _wrappersByName.put(nick,go);
98 }
99 else {
100
101 // If user is not already present in list as an operator, then remove and add
102 GuiObject go = (GuiObject)_wrappersByName.get("@"+nick);
103 if (go==null) {
104
105 go = (GuiObject)_wrappersByName.get(nick);
106 if (go!=null) {
107 ((DefaultListModel)getModel()).removeElement(go);
108 }
109
110 go = new GuiObject(nick,nick,new ImageIcon(getClass().getResource("images/User.gif")));
111 ((DefaultListModel)getModel()).addElement(go);
112
113 _wrappersByName.put(nick,go);
114 }
115 }
116 }
117 //-----------------------------------------------------------------
118 /** Removes the specified user from the list, checks both nick and @nick. */
119 public void remove(String nick) {
120
121 GuiObject go = (GuiObject)_wrappersByName.get(nick);
122 if (go!=null) {
123 _wrappersByName.remove(nick);
124 ((DefaultListModel)getModel()).removeElement(go);
125 }
126
127 go = (GuiObject)_wrappersByName.get("@"+nick);
128 if (go!=null) {
129 _wrappersByName.remove("@"+nick);
130 ((DefaultListModel)getModel()).removeElement(go);
131 }
132 }
133 //-----------------------------------------------------------------
134 /** Used internally to handler pop-up menu mouse clicks. */
135 public void processMouseEvent( MouseEvent e ) {
136
137 if (e.isPopupTrigger() && getSelectedValue()!=null) {
138
139 // FIX: Should find which user is at the mouse point, select them and then popup menu
140 _selUser = ((GuiObject)getSelectedValue()).getObject().toString();
141
142 JPopupMenu popup = new JPopupMenu();
143
144 //
145 // ADD BUDDY
146 //
147 JMenuItem addItem = new JMenuItem("Add to favorites");
148 addItem.addActionListener( new ActionListener() {
149 public void actionPerformed(ActionEvent ae) {
150 if (_selUser != null) {
151 if (_selUser.substring(0,1).equals("@")) {
152 _selUser = _selUser.substring(1);
153 }
154 ChatApp.getChatApp().getOptions().addUser(new User(_selUser));
155 }
156 }
157 });
158 popup.add(addItem);
159 popup.addSeparator();
160
161 //
162 // WHOIS
163 //
164 JMenuItem mi3 = new JMenuItem("Who is...");
165 mi3.addActionListener( new ActionListener() {
166 public void actionPerformed(ActionEvent ae) {
167 if (_selUser != null) {
168 if (_selUser.substring(0,1).equals("@")) {
169 _selUser = _selUser.substring(1);
170 }
171 _engine.sendCommand("WHOIS "+_selUser);
172 }
173 }
174 });
175 popup.add(mi3);
176
177 //
178 // VERSION
179 //
180 JMenuItem mi2 = new JMenuItem("Version");
181 mi2.addActionListener( new ActionListener() {
182 public void actionPerformed(ActionEvent ae) {
183 if (_selUser != null) {
184 if (_selUser.substring(0,1).equals("@")) {
185 _selUser = _selUser.substring(1);
186 }
187 _channel.sendVersion(_selUser);
188 }
189 }
190 });
191 popup.add(mi2);
192 popup.addSeparator();
193
194 //
195 // OP
196 //
197 JMenuItem mi5 = new JMenuItem("Op");
198 mi5.addActionListener( new ActionListener() {
199 public void actionPerformed(ActionEvent ae) {
200 if (_selUser != null) {
201 if (_selUser.substring(0,1).equals("@")) {
202 _selUser = _selUser.substring(1);
203 }
204 _channel.sendOp(_selUser);
205 }
206 }
207 });
208 popup.add(mi5);
209 //
210 // DEOP
211 //
212 JMenuItem mi8 = new JMenuItem("De-op");
213 mi8.addActionListener( new ActionListener() {
214 public void actionPerformed(ActionEvent ae) {
215 if (_selUser != null) {
216 if (_selUser.substring(0,1).equals("@")) {
217 _selUser = _selUser.substring(1);
218 }
219 _channel.sendDeop(_selUser);
220 }
221 }
222 });
223 popup.add(mi8);
224 //
225 // KICK
226 //
227 JMenuItem mi6 = new JMenuItem("Kick");
228 mi6.addActionListener( new ActionListener() {
229 public void actionPerformed(ActionEvent ae) {
230 if (_selUser != null) {
231 if (_selUser.substring(0,1).equals("@")) {
232 _selUser = _selUser.substring(1);
233 }
234 _channel.sendKick(_selUser);
235 }
236 }
237 });
238 popup.add(mi6);
239 //
240 // BAN
241 //
242 JMenuItem mi7 = new JMenuItem("Ban");
243 mi7.addActionListener( new ActionListener() {
244 public void actionPerformed(ActionEvent ae) {
245 if (_selUser != null) {
246 if (_selUser.substring(0,1).equals("@")) {
247 _selUser = _selUser.substring(1);
248 }
249 _channel.sendBan(_selUser);
250 }
251 }
252 });
253 popup.add(mi7);
254 //
255 // PING
256 //
257 /*
258 JPopupMenu popup = new JPopupMenu();
259 JMenuItem mi1 = new JMenuItem("PING");
260 mi1.addActionListener( new ActionListener() {
261 public void actionPerformed(ActionEvent ae) {
262 if (_selUser != null) {
263 if (_selUser.substring(0,1).equals("@")) {
264 _selUser = _selUser.substring(1);
265 }
266 _engine.sendVersion(_selUser);
267 }
268 }
269 });
270 popup.add(mi1);
271 */
272 //
273 // CLIENTINFO
274 //
275 /*
276 JMenuItem mi4 = new JMenuItem("CLIENTINFO");
277 mi4.addActionListener( new ActionListener() {
278 public void actionPerformed(ActionEvent ae) {
279 if (_selUser != null) {
280 if (_selUser.substring(0,1).equals("@")) {
281 _selUser = _selUser.substring(1);
282 }
283 _engine.sendCommand("CLIENTINFO "+_selUser);
284 }
285 }
286 });
287 popup.add(mi4);
288 */
289
290 add(popup);
291 RCTest.println("UserList: about to show popup");
292 popup.show(this,e.getX(),e.getY());
293 RCTest.println("UserList: showed popup");
294 }
295 else super.processMouseEvent(e);
296 }
297 }
298