package nu.fw.jeti.ui; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.text.MessageFormat; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.swing.*; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.JID; import nu.fw.jeti.jabber.JIDStatus; import nu.fw.jeti.util.I18N; import nu.fw.jeti.util.Popups; /** * @author E.S. de Boer */ public class JIDInput extends JPanel { private JLabel jLabel1 = new JLabel(); private JTextField txtContact = new JTextField(); private JLabel jLabel2 = new JLabel(); private JLabel jLabel3 = new JLabel(); private JComboBox cmbService; private Backend backend; private Transport service; public JIDInput(Backend backend) { setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); this.backend = backend; Object[] transports = makeTransportList(backend.getAvailableTransports()); cmbService = new JComboBox(transports); cmbService.setAlignmentX(0.0f); cmbService.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { service = (Transport) e.getItem(); jLabel1.setText(service.getLabelName()); } }); service = (Transport) transports[0]; JLabel jLabel0 = new JLabel(); I18N.setTextAndMnemonic("main.AddContact.Select_Service",jLabel0); jLabel0.setLabelFor(cmbService); jLabel0.setHorizontalAlignment(SwingConstants.LEFT); add(jLabel0); add(cmbService); jbInit(); } public JIDInput(JID jid, Backend backend) { this.backend = backend; Object[] transports = makeTransportList(backend.getAvailableTransports()); String server = jid.getDomain(); for(int i=0;i -1) {//remove spaces StringBuffer temp = new StringBuffer(); for (int i = 0; i < contactname.length(); i++) { char c = contactname.charAt(i); if (c != ' ') temp.append(c); } contactname = temp.toString(); } } else if (service.equalsToType("msn")) contactname = contactname.replace('@', '%'); if (JID.isValidUser(contactname)) contact = new JID(contactname, service.getServer()); else Popups.errorPopup(MessageFormat.format(I18N.gettext("main.error.{0}_is_not_valid"), new Object[]{contactname}), I18N.gettext("main.error.Wrong_contact_name")); } return contact; } private Object[] makeTransportList(Map availableTransports) { List transports = new LinkedList(); transports.add(new Transport("jabber", "Jabber", "JID", null)); for (Iterator i = availableTransports.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String type = (String) entry.getKey(); if (type.equals("msn")) transports.add(new Transport(type, "MSN Messenger", I18N.gettext("main.AddContact.Address"), (JIDStatus) entry.getValue())); else if (type.equals("icq")) transports.add(new Transport(type, "ICQ", "AddContact.UIN", (JIDStatus) entry.getValue())); else if (type.equals("aim")) transports.add(new Transport(type, "AOL Instant Messenger", I18N.gettext("main.AddContact.Screen_Name"), (JIDStatus) entry.getValue())); else if (type.equals("yahoo")) transports.add(new Transport(type, "Yahoo! Messenger", "ID", (JIDStatus) entry.getValue())); } transports.add(new Transport("other", I18N.gettext("main.AddContact.Other"), I18N.gettext("main.AddContact.Contactname@Transport"), null)); return transports.toArray(); } static class Transport { private String type; private String name; private String server; private String labelName; public Transport(String type, String name, String labelName, JIDStatus server) { this.type = type; this.name = name; this.labelName = labelName; if (server != null) this.server = server.getJID().getDomain(); } public String getLabelName() { return labelName; } public String getServer() { return server; } public String toString() { return name; } public boolean equalsToType(String type) { return this.type.equals(type); } } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */