1 | package nu.fw.jeti.ui; |
---|
2 | |
---|
3 | import java.awt.event.ItemEvent; |
---|
4 | import java.awt.event.ItemListener; |
---|
5 | import java.text.MessageFormat; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.LinkedList; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Map; |
---|
10 | |
---|
11 | import javax.swing.*; |
---|
12 | |
---|
13 | import nu.fw.jeti.jabber.Backend; |
---|
14 | import nu.fw.jeti.jabber.JID; |
---|
15 | import nu.fw.jeti.jabber.JIDStatus; |
---|
16 | import nu.fw.jeti.util.I18N; |
---|
17 | import nu.fw.jeti.util.Popups; |
---|
18 | |
---|
19 | /** |
---|
20 | * @author E.S. de Boer |
---|
21 | */ |
---|
22 | |
---|
23 | public class JIDInput extends JPanel |
---|
24 | { |
---|
25 | private JLabel jLabel1 = new JLabel(); |
---|
26 | private JTextField txtContact = new JTextField(); |
---|
27 | private JLabel jLabel2 = new JLabel(); |
---|
28 | private JLabel jLabel3 = new JLabel(); |
---|
29 | private JComboBox cmbService; |
---|
30 | private Backend backend; |
---|
31 | private Transport service; |
---|
32 | |
---|
33 | public JIDInput(Backend backend) |
---|
34 | { |
---|
35 | setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); |
---|
36 | this.backend = backend; |
---|
37 | Object[] transports = makeTransportList(backend.getAvailableTransports()); |
---|
38 | cmbService = new JComboBox(transports); |
---|
39 | cmbService.setAlignmentX(0.0f); |
---|
40 | cmbService.addItemListener(new ItemListener() |
---|
41 | { |
---|
42 | |
---|
43 | public void itemStateChanged(ItemEvent e) |
---|
44 | { |
---|
45 | service = (Transport) e.getItem(); |
---|
46 | jLabel1.setText(service.getLabelName()); |
---|
47 | } |
---|
48 | }); |
---|
49 | service = (Transport) transports[0]; |
---|
50 | JLabel jLabel0 = new JLabel(); |
---|
51 | I18N.setTextAndMnemonic("main.AddContact.Select_Service",jLabel0); |
---|
52 | jLabel0.setLabelFor(cmbService); |
---|
53 | jLabel0.setHorizontalAlignment(SwingConstants.LEFT); |
---|
54 | add(jLabel0); |
---|
55 | add(cmbService); |
---|
56 | jbInit(); |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | public JIDInput(JID jid, Backend backend) |
---|
61 | { |
---|
62 | this.backend = backend; |
---|
63 | Object[] transports = makeTransportList(backend.getAvailableTransports()); |
---|
64 | String server = jid.getDomain(); |
---|
65 | for(int i=0;i<transports.length;i++) |
---|
66 | { |
---|
67 | if(server.equals(((Transport)transports[i]).getServer())) |
---|
68 | { |
---|
69 | service = (Transport)transports[i]; |
---|
70 | if(service.equalsToType("msn")) |
---|
71 | { |
---|
72 | txtContact.setText(jid.getUser().replace('%', '@')); |
---|
73 | } |
---|
74 | else txtContact.setText(jid.getUser()); |
---|
75 | } |
---|
76 | } |
---|
77 | if(service==null) |
---|
78 | {//if no recognized transport then jabber |
---|
79 | service = (Transport) transports[0]; |
---|
80 | txtContact.setText(jid.toStringNoResource()); |
---|
81 | } |
---|
82 | jLabel1.setText(service.getLabelName()); |
---|
83 | txtContact.setEditable(false); |
---|
84 | txtContact.setHorizontalAlignment(SwingConstants.LEFT); |
---|
85 | jbInit(); |
---|
86 | } |
---|
87 | |
---|
88 | void jbInit() |
---|
89 | { |
---|
90 | // String[] groups = backend.getAllGroups(); |
---|
91 | // if (groups.length == 0) groups = new String[] { I18N.gettext("main.main.roster.Friends") }; |
---|
92 | jLabel1.setText(service.getLabelName()); |
---|
93 | jLabel1.setHorizontalAlignment(SwingConstants.LEFT); |
---|
94 | txtContact.setHorizontalAlignment(SwingConstants.LEADING); |
---|
95 | add(Box.createHorizontalGlue()); |
---|
96 | add(jLabel1, null); |
---|
97 | add(txtContact, null); |
---|
98 | add(jLabel2, null); |
---|
99 | add(jLabel3, null); |
---|
100 | } |
---|
101 | |
---|
102 | public JID createJID() |
---|
103 | { |
---|
104 | JID contact = null; |
---|
105 | if (service.equalsToType("jabber") || service.equalsToType("other")) |
---|
106 | { |
---|
107 | try |
---|
108 | { |
---|
109 | contact = JID.checkedJIDFromString(txtContact.getText()); |
---|
110 | } catch (InstantiationException e1) |
---|
111 | { |
---|
112 | Popups.errorPopup(e1.getMessage(), I18N.gettext("main.error.Wrong_Jabber_Identifier")); |
---|
113 | } |
---|
114 | } else |
---|
115 | { |
---|
116 | String contactname = txtContact.getText(); |
---|
117 | if (service.equalsToType("aim")) |
---|
118 | { |
---|
119 | if (contactname.indexOf(' ') > -1) |
---|
120 | {//remove spaces |
---|
121 | StringBuffer temp = new StringBuffer(); |
---|
122 | for (int i = 0; i < contactname.length(); i++) |
---|
123 | { |
---|
124 | char c = contactname.charAt(i); |
---|
125 | if (c != ' ') temp.append(c); |
---|
126 | } |
---|
127 | contactname = temp.toString(); |
---|
128 | } |
---|
129 | } else if (service.equalsToType("msn")) contactname = contactname.replace('@', '%'); |
---|
130 | if (JID.isValidUser(contactname)) contact = new JID(contactname, service.getServer()); |
---|
131 | else Popups.errorPopup(MessageFormat.format(I18N.gettext("main.error.{0}_is_not_valid"), new Object[]{contactname}), I18N.gettext("main.error.Wrong_contact_name")); |
---|
132 | } |
---|
133 | return contact; |
---|
134 | } |
---|
135 | |
---|
136 | private Object[] makeTransportList(Map availableTransports) |
---|
137 | { |
---|
138 | List transports = new LinkedList(); |
---|
139 | transports.add(new Transport("jabber", "Jabber", "JID", null)); |
---|
140 | for (Iterator i = availableTransports.entrySet().iterator(); i.hasNext();) |
---|
141 | { |
---|
142 | Map.Entry entry = (Map.Entry) i.next(); |
---|
143 | String type = (String) entry.getKey(); |
---|
144 | if (type.equals("msn")) transports.add(new Transport(type, "MSN Messenger", I18N.gettext("main.AddContact.Address"), (JIDStatus) entry.getValue())); |
---|
145 | else if (type.equals("icq")) transports.add(new Transport(type, "ICQ", "AddContact.UIN", (JIDStatus) entry.getValue())); |
---|
146 | else if (type.equals("aim")) transports.add(new Transport(type, "AOL Instant Messenger", I18N.gettext("main.AddContact.Screen_Name"), (JIDStatus) entry.getValue())); |
---|
147 | else if (type.equals("yahoo")) transports.add(new Transport(type, "Yahoo! Messenger", "ID", (JIDStatus) entry.getValue())); |
---|
148 | } |
---|
149 | transports.add(new Transport("other", I18N.gettext("main.AddContact.Other"), I18N.gettext("main.AddContact.Contactname@Transport"), null)); |
---|
150 | return transports.toArray(); |
---|
151 | } |
---|
152 | |
---|
153 | static class Transport |
---|
154 | { |
---|
155 | private String type; |
---|
156 | private String name; |
---|
157 | private String server; |
---|
158 | private String labelName; |
---|
159 | |
---|
160 | public Transport(String type, String name, String labelName, JIDStatus server) |
---|
161 | { |
---|
162 | this.type = type; |
---|
163 | this.name = name; |
---|
164 | this.labelName = labelName; |
---|
165 | if (server != null) this.server = server.getJID().getDomain(); |
---|
166 | } |
---|
167 | |
---|
168 | public String getLabelName() |
---|
169 | { |
---|
170 | return labelName; |
---|
171 | } |
---|
172 | |
---|
173 | public String getServer() |
---|
174 | { |
---|
175 | return server; |
---|
176 | } |
---|
177 | |
---|
178 | public String toString() |
---|
179 | { |
---|
180 | return name; |
---|
181 | } |
---|
182 | |
---|
183 | public boolean equalsToType(String type) |
---|
184 | { |
---|
185 | return this.type.equals(type); |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | /* |
---|
190 | * Overrides for emacs |
---|
191 | * Local variables: |
---|
192 | * tab-width: 4 |
---|
193 | * End: |
---|
194 | */ |
---|