1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.jabberaccregwizz;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.util.*;
import org.jivesoftware.smack.*;
/**
* Dialog for adding a new Jabber account.
*
* @author Nicolas Grandclaude
* @author Yana Stamcheva
*/
public class JabberAccountCreationForm
extends TransparentPanel
implements JabberAccountCreationFormService
{
/**
* Serial version UID.
*/
private static final long serialVersionUID = 0L;
private static final Logger logger = Logger
.getLogger(JabberAccountCreationForm.class);
private JabberServerChooserDialog jabberServerChooserDialog;
// Panels
private JPanel userIDPassPanel
= new TransparentPanel(new BorderLayout(10, 10));
private JPanel labelsPanel
= new TransparentPanel(new GridLayout(0, 1, 10, 10));
private JPanel valuesPanel
= new TransparentPanel(new GridLayout(0, 1, 10, 10));
private JPanel serverPanel
= new TransparentPanel(new BorderLayout(10, 10));
// Labels
private JLabel serverLabel
= new JLabel(Resources.getString("plugin.jabberaccregwizz.CSERVER"));
private JLabel userIDLabel
= new JLabel(Resources.getString("plugin.jabberaccregwizz.USERNAME"));
private JLabel passLabel
= new JLabel(Resources.getString("service.gui.PASSWORD"));
private JLabel pass2Label
= new JLabel(Resources.getString(
"plugin.jabberaccregwizz.PASSWORD_CONFIRM"));
private JLabel portLabel
= new JLabel(Resources.getString("service.gui.PORT"));
// Textfield
private JTextField serverField = new JTextField();
private JTextField userIDField = new JTextField();
private JPasswordField passField = new JPasswordField();
private JPasswordField pass2Field = new JPasswordField();
private JTextField portField = new JTextField("5222");
// Button
private JButton chooseButton = new JButton();
// Smack objects
private XMPPConnection xmppConnection = null;
private AccountManager accountManager = null;
/**
* The error text pane.
*/
private final JTextPane errorPane = new JTextPane();
/**
* Creates an instance of <tt>JabberNewAccountDialog</tt>.
*/
public JabberAccountCreationForm()
{
initErrorArea();
labelsPanel.add(serverLabel);
labelsPanel.add(userIDLabel);
labelsPanel.add(passLabel);
labelsPanel.add(pass2Label);
labelsPanel.add(portLabel);
userIDField.setColumns(30);
serverPanel.add(serverField, BorderLayout.CENTER);
serverPanel.add(chooseButton, BorderLayout.EAST);
valuesPanel.add(serverPanel);
valuesPanel.add(userIDField);
valuesPanel.add(passField);
valuesPanel.add(pass2Field);
valuesPanel.add(portField);
userIDPassPanel.add(labelsPanel, BorderLayout.WEST);
userIDPassPanel.add(valuesPanel, BorderLayout.CENTER);
chooseButton.setText(
Resources.getString("plugin.jabberaccregwizz.CHOOSE"));
chooseButton.setMnemonic(
Resources.getMnemonic("plugin.jabberaccregwizz.CHOOSE"));
// Choose button open the JabberServerChooserDialog
chooseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
jabberServerChooserDialog = new JabberServerChooserDialog();
if (jabberServerChooserDialog.isOK) // OK pressed in
// JabberServerChooserDialog
{
serverField
.setText(jabberServerChooserDialog.serverSelected);
}
}
});
this.add(userIDPassPanel, BorderLayout.CENTER);
}
/**
* Connects to the chosen server and creates a new account with Smack.
*
* @param server the server domain
* @param port TCP port to connect
* @param username the account username
* @param password the account password
*/
private boolean createJabberAccount(String server, int port, String username,
String password)
{
try
{
ConnectionConfiguration config = new ConnectionConfiguration(
server, port);
xmppConnection = new XMPPConnection(config);
xmppConnection.connect();
accountManager = new AccountManager(xmppConnection);
accountManager.createAccount(username, password);
return true;
}
catch (XMPPException exc)
{
logger.error(exc);
if (exc.getXMPPError() != null
&& exc.getXMPPError().getCode() == 409)
{
showErrorMessage(Resources.getString(
"plugin.jabberaccregwizz.USER_EXISTS_ERROR"));
logger.error(
"Error when created a new Jabber account :" +
" user already exist");
}
else
{
showErrorMessage(Resources.getResources().getI18NString(
"plugin.jabberaccregwizz.UNKNOWN_XMPP_ERROR",
new String[]{exc.getMessage()}
));
}
return false;
}
}
/**
* Creates an account.
*
* @return the created account
*/
public NewAccount createAccount()
{
String userID = userIDField.getText();
char[] password = passField.getPassword();
char[] password2 = pass2Field.getPassword();
String server = serverField.getText();
int port = 5222; // default port
try
{
// custom port, if exists
port = Integer.parseInt(portField.getText());
}
catch (NumberFormatException e){}
if (new String(password).equals(new String(password2)))
{
// the two password fields are the same
boolean result = createJabberAccount(server,
port,
userID,
new String(password));
if (result == true)
{
return new NewAccount(
getCompleteUserID(userID, server),
password,
server,
String.valueOf(port));
}
}
else
{
showErrorMessage(Resources.getString(
"plugin.jabberaccregwizz.NOT_SAME_PASSWORD"));
}
return null;
}
/**
* Returns the create account form.
*
* @return the create account form
*/
public Component getForm()
{
return this;
}
/**
* Clears all fields contained in this form.
*/
public void clear()
{
userIDField.setText("");
passField.setText("");
pass2Field.setText("");
serverField.setText(Resources.getSettingsString(
"plugin.jabberaccregwizz.NEW_ACCOUNT_DEFAULT_SERVER"));
errorPane.setText("");
userIDPassPanel.remove(errorPane);
}
/**
* Creates the error area component.
*/
private void initErrorArea()
{
SimpleAttributeSet attribs = new SimpleAttributeSet();
StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontFamily(attribs, errorPane.getFont().getFamily());
StyleConstants.setForeground(attribs, Color.RED);
errorPane.setParagraphAttributes(attribs, true);
errorPane.setPreferredSize(new Dimension(100, 50));
errorPane.setMinimumSize(new Dimension(100, 50));
errorPane.setOpaque(false);
}
/**
* Shows the given error message.
*
* @param text the text of the error
*/
private void showErrorMessage(String text)
{
errorPane.setText(text);
if (errorPane.getParent() == null)
userIDPassPanel.add(errorPane, BorderLayout.NORTH);
Window ancestor = SwingUtilities.getWindowAncestor(this);
if (ancestor != null)
ancestor.pack();
}
/**
* Returns the complete user id, by adding to it the server part.
*
* @param userID the username
* @param server the server address
* @return the complete user id
*/
private String getCompleteUserID(String userID, String server)
{
if (userID.indexOf("@") < 0 && server != null && server.length() > 0)
{
return userID + "@" + server;
}
return userID;
}
}
|