[3102] | 1 | package nu.fw.jeti.ui; |
---|
| 2 | |
---|
| 3 | import java.awt.*; |
---|
| 4 | import java.awt.BorderLayout; |
---|
| 5 | import java.awt.event.ActionEvent; |
---|
| 6 | |
---|
| 7 | import javax.swing.*; |
---|
| 8 | |
---|
| 9 | import javax.swing.JOptionPane; |
---|
| 10 | import javax.swing.border.Border; |
---|
| 11 | import javax.swing.border.EtchedBorder; |
---|
| 12 | |
---|
| 13 | import nu.fw.jeti.jabber.Backend; |
---|
| 14 | import nu.fw.jeti.jabber.JID; |
---|
| 15 | import nu.fw.jeti.jabber.elements.InfoQueryBuilder; |
---|
| 16 | import nu.fw.jeti.jabber.elements.Presence; |
---|
| 17 | import nu.fw.jeti.jabber.elements.RosterBuilder; |
---|
| 18 | import nu.fw.jeti.jabber.elements.RosterItemBuilder; |
---|
| 19 | import nu.fw.jeti.util.I18N; |
---|
| 20 | import nu.fw.jeti.util.Utils; |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * @author E.S. de Boer |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | public class AddContact extends JDialog |
---|
| 27 | { |
---|
| 28 | private Box panel1 = Box.createVerticalBox(); |
---|
| 29 | private JLabel jLabel2 = new JLabel(); |
---|
| 30 | private JTextField txtNick = new JTextField(); |
---|
| 31 | private JLabel jLabel3 = new JLabel(); |
---|
| 32 | private JComboBox cmbGroup; |
---|
| 33 | private JPanel jPanel1 = new JPanel(); |
---|
| 34 | private JButton btnCancel = new JButton(); |
---|
| 35 | private JButton btnOK = new JButton(); |
---|
| 36 | private Backend backend; |
---|
| 37 | |
---|
| 38 | private JIDInput jidInput; |
---|
| 39 | private String nameContact; |
---|
| 40 | private String nameOrganization; |
---|
| 41 | |
---|
| 42 | public AddContact(Backend backend) |
---|
| 43 | { |
---|
| 44 | super(backend.getMainFrame(), I18N.gettext("main.AddContact.Add_Contact"), false); |
---|
| 45 | this.backend = backend; |
---|
| 46 | jidInput = new JIDInput(backend); |
---|
| 47 | jbInit(); |
---|
| 48 | pack(); |
---|
| 49 | setLocationRelativeTo(backend.getMainFrame()); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | public AddContact(JID jid, String nick,String group, Backend backend) |
---|
| 53 | { |
---|
| 54 | //xmppuri |
---|
| 55 | super(backend.getMainFrame(), I18N.gettext("main.AddContact.Add_Contact"), false); |
---|
| 56 | this.backend = backend; |
---|
| 57 | jidInput = new JIDInput(jid,backend); |
---|
| 58 | txtNick.setText(nick); |
---|
| 59 | jbInit(); |
---|
| 60 | if(group!=null)cmbGroup.setSelectedItem(group); |
---|
| 61 | pack(); |
---|
| 62 | setLocationRelativeTo(backend.getMainFrame()); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | public AddContact(JID jid, JFrame frame, Backend backend, String name, String org) |
---|
| 66 | { |
---|
| 67 | super(frame, I18N.gettext("main.AddContact.Add_Contact"), false); |
---|
| 68 | this.backend = backend; |
---|
| 69 | jidInput = new JIDInput(jid,backend); |
---|
| 70 | nameContact = name; |
---|
| 71 | nameOrganization = org; |
---|
| 72 | name = name.substring(name.indexOf(": ") + 2); |
---|
| 73 | txtNick.setText(name.substring(0, name.indexOf(" "))); |
---|
| 74 | jbInit(); |
---|
| 75 | pack(); |
---|
| 76 | setLocationRelativeTo(frame); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void jbInit() |
---|
| 80 | { |
---|
| 81 | String[] groups = backend.getAllGroups(); |
---|
| 82 | if (groups.length == 0) groups = new String[] { I18N.gettext("main.main.roster.Friends") }; |
---|
| 83 | getRootPane().setDefaultButton(btnOK); |
---|
| 84 | |
---|
| 85 | txtNick.setHorizontalAlignment(SwingConstants.LEFT); |
---|
| 86 | cmbGroup = new JComboBox(groups); |
---|
| 87 | cmbGroup.setAlignmentX(0.0f); |
---|
| 88 | cmbGroup.setEditable(true); |
---|
| 89 | jPanel1.setAlignmentX(0.0f); |
---|
| 90 | setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
---|
| 91 | panel1.add(Box.createHorizontalGlue()); |
---|
| 92 | I18N.setTextAndMnemonic("main.AddContact.Nickname",jLabel2); |
---|
| 93 | jLabel2.setLabelFor(txtNick); |
---|
| 94 | jLabel3.setHorizontalAlignment(SwingConstants.LEFT); |
---|
| 95 | I18N.setTextAndMnemonic("main.AddContact.Group",jLabel3); |
---|
| 96 | jLabel3.setLabelFor(cmbGroup); |
---|
| 97 | Action cancelAction = new AbstractAction(I18N.gettext("Cancel")) |
---|
| 98 | { |
---|
| 99 | public void actionPerformed(ActionEvent e) |
---|
| 100 | { |
---|
| 101 | dispose(); |
---|
| 102 | } |
---|
| 103 | }; |
---|
| 104 | Utils.addCancelButton(this, btnCancel, cancelAction); |
---|
| 105 | |
---|
| 106 | btnOK.setText(I18N.gettext("OK")); |
---|
| 107 | btnOK.addActionListener(new java.awt.event.ActionListener() |
---|
| 108 | { |
---|
| 109 | |
---|
| 110 | public void actionPerformed(ActionEvent e) |
---|
| 111 | { |
---|
| 112 | if ( !txtNick.getText().trim().equals("") ) |
---|
| 113 | btnOK_actionPerformed(e); |
---|
| 114 | else |
---|
| 115 | JOptionPane.showMessageDialog(null, "Informe um apelido."); |
---|
| 116 | } |
---|
| 117 | }); |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * |
---|
| 121 | * @Author: Alexandre Correia - alexandrecorreia@celepar.pr.gov.br |
---|
| 122 | * @Date : 17/10/2008 |
---|
| 123 | * @Description : Criado o JLabel Component para mostrar o nome do contato ao inves do uid. |
---|
| 124 | * |
---|
| 125 | */ |
---|
| 126 | |
---|
| 127 | JLabel contact = new JLabel(); |
---|
| 128 | contact.setBorder(BorderFactory.createEmptyBorder(5, 0, 2, 0)); |
---|
| 129 | contact.setText(nameContact); |
---|
| 130 | contact.setAlignmentX(0.0f); |
---|
| 131 | |
---|
| 132 | JLabel organization = new JLabel(); |
---|
| 133 | organization.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0)); |
---|
| 134 | organization.setText(nameOrganization); |
---|
| 135 | organization.setAlignmentX(0.0f); |
---|
| 136 | |
---|
| 137 | getContentPane().add(panel1, BorderLayout.CENTER); |
---|
| 138 | panel1.add(contact); |
---|
| 139 | panel1.add(organization); |
---|
| 140 | panel1.add(jLabel2); |
---|
| 141 | panel1.add(txtNick); |
---|
| 142 | panel1.add(jLabel3); |
---|
| 143 | panel1.add(cmbGroup); |
---|
| 144 | panel1.add(jPanel1); |
---|
| 145 | jPanel1.add(btnOK); |
---|
| 146 | |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void btnOK_actionPerformed(ActionEvent e) |
---|
| 150 | { |
---|
| 151 | JID contact = jidInput.createJID(); |
---|
| 152 | if (contact == null) return; |
---|
| 153 | backend.send(new Presence(contact, "subscribe")); |
---|
| 154 | try |
---|
| 155 | { |
---|
| 156 | String nick = txtNick.getText(); |
---|
| 157 | if (nick.equals("")) nick = contact.getUser(); |
---|
| 158 | //still empty then use domain |
---|
| 159 | if (nick == null) nick = contact.getDomain(); |
---|
| 160 | RosterBuilder rb = new RosterBuilder(); |
---|
| 161 | RosterItemBuilder rib = new RosterItemBuilder(); |
---|
| 162 | rib.addGroup((String) cmbGroup.getSelectedItem()); |
---|
| 163 | rib.jid = contact; |
---|
| 164 | rib.name = nick; |
---|
| 165 | rib.ask = "subscribe"; |
---|
| 166 | rb.addItem(rib.build()); |
---|
| 167 | InfoQueryBuilder iqb = new InfoQueryBuilder(); |
---|
| 168 | iqb.addExtension(rb.build()); |
---|
| 169 | iqb.setType("set"); |
---|
| 170 | backend.send(iqb.build()); |
---|
| 171 | } catch (InstantiationException e2) |
---|
| 172 | { |
---|
| 173 | e2.printStackTrace(); |
---|
| 174 | } |
---|
| 175 | this.dispose(); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | /* |
---|
| 179 | * Overrides for emacs |
---|
| 180 | * Local variables: |
---|
| 181 | * tab-width: 4 |
---|
| 182 | * End: |
---|
| 183 | */ |
---|