/*
 Eteria IRC Client, an RFC 1459 compliant client program written in Java.
 Copyright (C) 2001  Christian Buck <cbuck at lantis.de>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 import java.awt.*;
 import java.awt.event.*;
 import java.util.*;
 import java.text.MessageFormat;
 import ar.com.jkohen.awt.ImageButton;
 import ar.com.jkohen.util.Resources;
 
 public class ModeList extends NewDialog implements ActionListener
 {
 	private java.awt.List items;
 	private ImageButton b;
 
 	private Resources res;
 
 	private String chan;
 	private char mode;
 	private static int MODE_NUMBER = 4;
 
 	public ModeList(EIRC eirc)
 	{
 		super(eirc);
 
 		GridBagLayout gb = new GridBagLayout();
 		GridBagConstraints gbc = new GridBagConstraints();
 		setLayout(gb);
 		gbc.insets = new Insets(2, 2, 2, 2);
 
 		items = new java.awt.List(10, true);
 		gbc.anchor = GridBagConstraints.CENTER;
 	  	gbc.weightx = 1;
 		gbc.weighty = 1;
 		gbc.fill = GridBagConstraints.BOTH;
 		gbc.gridwidth = GridBagConstraints.REMAINDER;
 		gb.setConstraints(items, gbc);
 		add(items);
 
 		b = new ImageButton(res.getString("ok"));
 		b.setActionCommand("ok");
 		gbc.weighty = 0;
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.gridwidth = 1;
 		gb.setConstraints(b, gbc);
 		add(b);
 		b.addActionListener(this);
 
 		b = new ImageButton();
 		b.setActionCommand("delete");
 		gb.setConstraints(b, gbc);
 		add(b);
 		b.addActionListener(this);
 
 		setResizable(true);
 		pack();
 	}
 
 	public Dimension getPreferredSize()
 	{
 		Dimension d = eirc.getFrame().getSize();
 		return(new Dimension(d.width * 2/3, d.height * 2/3));
 	}
 	
 	public static void setMaximumNumber(int i)
 	{
 		MODE_NUMBER = i;
 	}
 
 	public void initList(String chan, boolean is_op, Vector data, String title, MessageFormat msg, String button, char mode)
 	{
 		setTitle(title);
 		this.chan = chan;
 		this.mode = mode;
 
 		items.removeAll();
 	  	for (Enumeration e = data.elements(); e.hasMoreElements(); )
 	  	{
 			ListItem bi = (ListItem) e.nextElement();
 			Object [] ob = { bi.getMask(), bi.getOp(), bi.getDate() };
 			items.add(msg.format(ob));
 		}
 		b.setText(button);
 		b.setEnabled(is_op);
 		setVisible(true);
 	}
 
 	public void actionPerformed(ActionEvent ev)
 	{
 		String ac = ev.getActionCommand();
 		String masks[] = items.getSelectedItems();
 
 		if (ac.equals("ok"))
 			processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
 
 		if (ac.equals("delete") && masks.length > 0)
 		{
 			String params[] = new String[2 + MODE_NUMBER];
 			params[0] = chan;
 			for (int i = 0; i < MODE_NUMBER; i++)
 				params[2 + i] = "";
 
 			for (int i = 0; i &lt; masks.length; i += MODE_NUMBER)
 			{
 				params[1] = "-";
 
 				for (int j = 0; j &lt; MODE_NUMBER && (i + j) &lt; masks.length; j++)
 				{
 					params[1] += mode;
 					params[2 + j] = masks[i + j].substring(0, masks[i + j].indexOf(' '));
 					items.remove(masks[i + j]);
 				}
 				eirc.sendMessage("MODE", params);
 			}
 		}
 	}
 }