package nu.fw.jeti.ui; import java.awt.*; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import javax.swing.*; import javax.swing.JOptionPane; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.JID; import nu.fw.jeti.jabber.elements.InfoQueryBuilder; import nu.fw.jeti.jabber.elements.Presence; import nu.fw.jeti.jabber.elements.RosterBuilder; import nu.fw.jeti.jabber.elements.RosterItemBuilder; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Utils; /** * @author E.S. de Boer */ public class AddContact extends JDialog { private Box panel1 = Box.createVerticalBox(); private JLabel jLabel2 = new JLabel(); private JTextField txtNick = new JTextField(); private JLabel jLabel3 = new JLabel(); private JComboBox cmbGroup; private JPanel jPanel1 = new JPanel(); private JButton btnCancel = new JButton(); private JButton btnOK = new JButton(); private Backend backend; private JIDInput jidInput; private String nameContact; private String nameOrganization; public AddContact(Backend backend) { super(backend.getMainFrame(), I18N.gettext("main.AddContact.Add_Contact"), false); this.backend = backend; jidInput = new JIDInput(backend); jbInit(); pack(); setLocationRelativeTo(backend.getMainFrame()); } public AddContact(JID jid, String nick,String group, Backend backend) { //xmppuri super(backend.getMainFrame(), I18N.gettext("main.AddContact.Add_Contact"), false); this.backend = backend; jidInput = new JIDInput(jid,backend); txtNick.setText(nick); jbInit(); if(group!=null)cmbGroup.setSelectedItem(group); pack(); setLocationRelativeTo(backend.getMainFrame()); } public AddContact(JID jid, JFrame frame, Backend backend, String name, String org) { super(frame, I18N.gettext("main.AddContact.Add_Contact"), false); this.backend = backend; jidInput = new JIDInput(jid,backend); nameContact = name; nameOrganization = org; name = name.substring(name.indexOf(": ") + 2); txtNick.setText(name.substring(0, name.indexOf(" "))); jbInit(); pack(); setLocationRelativeTo(frame); } void jbInit() { String[] groups = backend.getAllGroups(); if (groups.length == 0) groups = new String[] { I18N.gettext("main.main.roster.Friends") }; getRootPane().setDefaultButton(btnOK); txtNick.setHorizontalAlignment(SwingConstants.LEFT); cmbGroup = new JComboBox(groups); cmbGroup.setAlignmentX(0.0f); cmbGroup.setEditable(true); jPanel1.setAlignmentX(0.0f); setDefaultCloseOperation(DISPOSE_ON_CLOSE); panel1.add(Box.createHorizontalGlue()); I18N.setTextAndMnemonic("main.AddContact.Nickname",jLabel2); jLabel2.setLabelFor(txtNick); jLabel3.setHorizontalAlignment(SwingConstants.LEFT); I18N.setTextAndMnemonic("main.AddContact.Group",jLabel3); jLabel3.setLabelFor(cmbGroup); Action cancelAction = new AbstractAction(I18N.gettext("Cancel")) { public void actionPerformed(ActionEvent e) { dispose(); } }; Utils.addCancelButton(this, btnCancel, cancelAction); btnOK.setText(I18N.gettext("OK")); btnOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { if ( !txtNick.getText().trim().equals("") ) btnOK_actionPerformed(e); else JOptionPane.showMessageDialog(null, "Informe um apelido."); } }); /** * * @Author: Alexandre Correia - alexandrecorreia@celepar.pr.gov.br * @Date : 17/10/2008 * @Description : Criado o JLabel Component para mostrar o nome do contato ao inves do uid. * */ JLabel contact = new JLabel(); contact.setBorder(BorderFactory.createEmptyBorder(5, 0, 2, 0)); contact.setText(nameContact); contact.setAlignmentX(0.0f); JLabel organization = new JLabel(); organization.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0)); organization.setText(nameOrganization); organization.setAlignmentX(0.0f); getContentPane().add(panel1, BorderLayout.CENTER); panel1.add(contact); panel1.add(organization); panel1.add(jLabel2); panel1.add(txtNick); panel1.add(jLabel3); panel1.add(cmbGroup); panel1.add(jPanel1); jPanel1.add(btnOK); } void btnOK_actionPerformed(ActionEvent e) { JID contact = jidInput.createJID(); if (contact == null) return; backend.send(new Presence(contact, "subscribe")); try { String nick = txtNick.getText(); if (nick.equals("")) nick = contact.getUser(); //still empty then use domain if (nick == null) nick = contact.getDomain(); RosterBuilder rb = new RosterBuilder(); RosterItemBuilder rib = new RosterItemBuilder(); rib.addGroup((String) cmbGroup.getSelectedItem()); rib.jid = contact; rib.name = nick; rib.ask = "subscribe"; rb.addItem(rib.build()); InfoQueryBuilder iqb = new InfoQueryBuilder(); iqb.addExtension(rb.build()); iqb.setType("set"); backend.send(iqb.build()); } catch (InstantiationException e2) { e2.printStackTrace(); } this.dispose(); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */