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 /*****************************************************/
32 import irc.ListenerGroup;
34 import java.awt.Color;
35 import java.awt.Dimension;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.Image;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
42 import java.awt.event.MouseMotionListener;
43 import java.util.Vector;
46 * The pixx nick list display.
48 public class PixxNickList extends PixxPanel implements MouseListener, MouseMotionListener {
49 private Vector _nicks;
50 private Image _buffer;
53 private int _selected;
54 private int _overindex;
56 private int _toScroll;
57 private Object _scrollLock = new Object();
58 private ListenerGroup _listeners;
59 private char[] _prefixes;
60 private boolean _leftAlign;
63 * Create a new PixxNickList.
66 * global irc configuration.
68 * known nick prefixes.
70 public PixxNickList(PixxConfiguration config, char[] prefixes) {
76 _listeners = new ListenerGroup();
77 addMouseListener(this);
78 addMouseMotionListener(this);
80 _nicks = new Vector();
81 _font = new Font("", 0, 12);
82 _leftAlign = _pixxConfiguration.getB("leftnickalign");
86 public void release() {
88 removeMouseListener(this);
89 removeMouseMotionListener(this);
99 public void addPixxNickListListener(PixxNickListListener lis) {
100 _listeners.addListener(lis);
107 * listener to remove.
109 public void removePixxNickListListener(PixxNickListListener lis) {
110 _listeners.removeListener(lis);
114 public Dimension getPreferredSize() {
115 return new Dimension(_pixxConfiguration.getI("nicklistwidth"), 16);
119 * Clear any off-screen ressources. The next display might be slower.
121 public void dispose() {
125 private void reinit() {
126 synchronized (_scrollLock) {
132 private void scroll(int v) {
133 synchronized (_scrollLock) {
139 private int getScrollValue() {
140 synchronized (_scrollLock) {
148 * Set first displayed nick.
151 * first displayed nick index.
153 public void setBase(int b) {
159 * Get first displayed nick.
161 * @return first dispalyed nick index.
163 public int getBase() {
168 * Get amount of nicks in the list.
170 * @return nick list size.
172 public int getNickCount() {
173 return _nicks.size();
177 * Add a nick in the list.
182 public void add(String nick) {
183 _nicks.insertElementAt(nick, _nicks.size());
189 * Remove the given nick name from the list.
192 * nick name to remove.
194 public void remove(String nick) {
195 for (int i = 0; i < _nicks.size(); i++) {
196 String s = (String) _nicks.elementAt(i);
197 if (s.equals(nick)) {
198 _nicks.removeElementAt(i);
207 * Change all the nicks in the list.
212 public void set(String[] nicks) {
213 _nicks = new Vector();
214 for (int i = 0; i < nicks.length; i++)
215 _nicks.insertElementAt(nicks[i], _nicks.size());
221 * Remove all the nicks from the list.
224 public void removeAll() {
225 _nicks = new Vector();
232 public void paint(Graphics g) {
236 private Color findColor(String info) {
237 return _pixxConfiguration.getIRCConfiguration().getASLColor(info, getColor(COLOR_MALE), getColor(COLOR_FEMEALE),
238 getColor(COLOR_UNDEF));
241 private void update(Graphics gra, int ytop, int height) {
242 int w = getSize().width;
243 // int h=getSize().height;
244 int fh = _font.getSize();
246 gra.setColor(getColor(COLOR_BACK));
247 gra.fillRect(0, ytop, w, height);
248 gra.setColor(getColor(COLOR_BLACK));
249 gra.drawLine(w - 1, ytop, w - 1, ytop + height - 1);
254 FontMetrics fm = gra.getFontMetrics();
257 while (y + fh + 5 < ytop) {
265 while ((i < _nicks.size()) && (y <= ytop + height)) {
266 String nick = (String) _nicks.elementAt(i);
268 int pos = nick.indexOf(":");
269 Color back = getColor(COLOR_FRONT);
271 info = nick.substring(pos + 1);
272 nick = nick.substring(0, pos);
273 back = findColor(info);
276 back = getColor(COLOR_SELECTED);
279 for (int ci = 0; ci < _prefixes.length; ci++)
280 if ((nick.length() > 0) && (nick.charAt(0) == _prefixes[ci]))
281 prefix = _prefixes[ci];
283 nick = nick.substring(1);
285 int sw = fm.stringWidth(nick);
288 gra.fillRect(20, y - 1, w - 28, fh + 2);
289 gra.setColor(getColor(COLOR_WHITE));
290 gra.drawRect(20, y - 1, w - 28, fh + 2);
292 gra.setColor(getColor(COLOR_WHITE));
293 gra.setClip(20, y - 1, w - 28, fh + 2);
294 int px = w - sw - 12;
301 gra.drawString(nick, px, y + fh - 1);
302 gra.setClip(0, 0, getSize().width, getSize().height);
306 gra.setColor(getColor(COLOR_OP));
307 else if (prefix == '+')
308 gra.setColor(getColor(COLOR_VOICE));
309 else if (prefix == '%')
310 gra.setColor(getColor(COLOR_SEMIOP));
312 gra.setColor(getColor(COLOR_FRONT));
313 gra.fillRect(20 - fh - 6, y - 1, fh + 2, fh + 2);
314 gra.setColor(getColor(COLOR_WHITE));
315 gra.drawRect(20 - fh - 6, y - 1, fh + 2, fh + 2);
317 gra.setColor(getColor(COLOR_WHITE));
318 sw = fm.stringWidth("" + prefix);
319 int tx = 20 - fh - 6 + (fh + 2 - sw) / 2 + 1;
325 gra.drawString("" + prefix, tx, ty);
334 public void update(Graphics g) {
335 int w = getSize().width;
336 int h = getSize().height;
337 int fh = _font.getSize();
339 if (_buffer != null) {
340 if ((_buffer.getWidth(this) != w) || (_buffer.getHeight(this) != h))
344 if (_buffer == null) {
347 _buffer = createImage(w, h);
348 gra = _buffer.getGraphics();
350 } catch (Throwable e) {
354 int scr = getScrollValue();
356 Graphics gra = _buffer.getGraphics();
357 int dy = -(fh + 6) * scr;
359 gra.copyArea(0, -dy, w, h + dy, 0, dy);
360 update(gra, h + dy, -dy);
362 gra.copyArea(0, 0, w, h - dy, 0, dy);
369 g.drawImage(_buffer, 0, 0, this);
370 if ((_overindex != -1) && _pixxConfiguration.getIRCConfiguration().getB("style:floatingasl")) {
372 int y = 8 + (_overindex - _base) * (fh + 6) + 2;
375 String info = getInfo(_overindex);
376 String text = _pixxConfiguration.getIRCConfiguration().formatASL(info);
377 if (text.length() > 0) {
378 int tw = g.getFontMetrics().stringWidth(text);
383 g.setColor(getAlphaColor(findColor(info),
384 _pixxConfiguration.getIRCConfiguration().getI("style:floatingaslalpha")));
385 g.fillRect(x, y, tw + 4, fh + 4);
386 g.setColor(getColor(COLOR_WHITE));
387 g.drawRect(x, y, tw + 4, fh + 4);
388 g.drawString(text, x + 2, y + fh);
393 private Color getAlphaColor(Color c, int alpha) {
395 return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
396 } catch (Throwable ex) {
401 private int getIndex(int x, int y) {
402 int fh = _font.getSize();
403 // increase y value so that it won't reach negative value. neg y is a
404 // problem since / rounds to 0, not to lower : -0.5 will lead to 0, not
413 if (y >= _nicks.size())
419 public void mouseClicked(MouseEvent e) {
423 public void mouseEntered(MouseEvent e) {
427 public void mouseExited(MouseEvent e) {
428 if (_overindex != -1) {
435 public void mousePressed(MouseEvent e) {
436 int index = getIndex(e.getX(), e.getY());
441 _listeners.sendEventAsync("eventOccured", getNick(_selected), e);
445 public void mouseReleased(MouseEvent e) {
449 public void mouseDragged(MouseEvent e) {
453 private String getUnprefixedNick(String nick) {
454 if (nick.length() == 0)
456 for (int i = 0; i < _prefixes.length; i++)
457 if (nick.charAt(0) == _prefixes[i])
458 return nick.substring(1);
462 private String getNick(int index) {
465 String nick = (String) _nicks.elementAt(index);
466 nick = getUnprefixedNick(nick);
467 int pos = nick.indexOf(":");
469 nick = nick.substring(0, pos);
474 private String getInfo(int index) {
477 String nick = (String) _nicks.elementAt(index);
478 nick = getUnprefixedNick(nick);
479 int pos = nick.indexOf(":");
482 info = nick.substring(pos + 1);
488 public void mouseMoved(MouseEvent e) {
489 if (!_pixxConfiguration.getIRCConfiguration().getASLMaster())
491 int index = getIndex(e.getX(), e.getY());
492 if (index == _overindex)
498 _listeners.sendEventAsync("ASLEventOccured", getNick(index), getInfo(index));