/*
* Hamsam - Instant Messaging API
* Copyright (C) 2003 Raghu K
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package gui;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import gui.tools.PropertiesManager;
import gui.tools.Traces;
import gui.action.ConnectionInfos;
import hamsam.api.Buddy;
/**
* @author Frederic
*/
public class AddBuddyDialog extends JDialog implements ActionListener {
private PropertiesManager propertiesMgr = null;
private MainFrame owner = null;
private ContactsMgrPanel contacts = null;
private JTextField nameField = new JTextField(20);
private JTextField groupField = new JTextField(20);
private JLabel nameLabel = null;
private JLabel groupLabel = null;
private JButton ok = null;
private JButton cancel = null;
private Buddy newBuddy = null;
public AddBuddyDialog(MainFrame owner, PropertiesManager p, ContactsMgrPanel c) {
super(owner, true);
this.owner = owner;
this.propertiesMgr = p;
this.contacts = c;
this.init();
}
private void init() {
this.setTitle(propertiesMgr.getValue("titleBuddyDialog"));
this.getContentPane().setLayout(new GridBagLayout());
nameLabel = new JLabel(propertiesMgr.getValue("buddyName"));
groupLabel = new JLabel(propertiesMgr.getValue("buddyGroup"));
ok = new JButton(propertiesMgr.getValue("okForAddBuddy"));
ok.addActionListener(this);
cancel = new JButton(propertiesMgr.getValue("cancelForAddBuddy"));
cancel.addActionListener(this);
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.insets = new Insets(5, 5, 5, 5);
//First line
c.gridy = 1;
this.getContentPane().add(nameLabel, c);
this.getContentPane().add(nameField, c);
//second line
c.gridy = 2;
this.getContentPane().add(groupLabel, c);
this.getContentPane().add(groupField, c);
//third line
c.gridy = 3;
c.insets = new Insets(25, 5, 5, 5);
this.getContentPane().add(ok, c);
this.getContentPane().add(cancel, c);
this.pack();
this.setResizable(false);
this.setVisible(true);
}
private boolean checkValues() {
boolean ret = !nameField.getText().equals("");
return ret;
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == ok) {
//if buddy name is given
if (checkValues()) {
//if no group is defined
if (groupField.equals("")) {
newBuddy = new Buddy(ConnectionInfos.currentProtocol,
nameField.getText());
}
else {
newBuddy = new Buddy(ConnectionInfos.currentProtocol,
nameField.getText(),
groupField.getText());
}
Traces.printTraces("New buddy created");
Traces.printTraces("name : " + newBuddy.getUsername());
Traces.printTraces("group : " + newBuddy.getGroup());
Traces.printTraces("protocol : " + newBuddy.getProtocol().getProtocolName());
this.contacts.addBuddy(newBuddy);
this.dispose();
}
}
else {
this.dispose();
}
}
}