aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/sipaccregwizz/AccountPanel.java
blob: c94c5fbf3cb3c2ba81c5a50163be7c3585716f1a (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.plugin.sipaccregwizz;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

import net.java.sip.communicator.util.*;
import net.java.sip.communicator.plugin.desktoputil.*;

/**
 * The panel containing all account related information like user name and
 * password.
 *
 * @author Yana Stamcheva
 */
public class AccountPanel
    extends TransparentPanel
    implements DocumentListener,
               ValidatingPanel
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    private final Logger logger = Logger.getLogger(AccountPanel.class);

    private final JPanel labelsPanel
        = new TransparentPanel(new GridLayout(0, 1));

    private final JPanel valuesPanel
        = new TransparentPanel(new GridLayout(0, 1));

    private final JPanel emptyPanel = new TransparentPanel();

    private final JTextField userIDField = new TrimTextField();

    private final JPasswordField passField = new JPasswordField();

    private final JTextField displayNameField = new JTextField();

    private final JCheckBox rememberPassBox
        = new SIPCommCheckBox(
            Resources.getString("service.gui.REMEMBER_PASSWORD"));

    private final JLabel displayNameLabel
        = new JLabel(Resources.getString("plugin.sipaccregwizz.DISPLAY_NAME"));

    private final JRadioButton existingAccountButton;

    private final JRadioButton createAccountButton;

    private final JPanel uinPassPanel
        = new TransparentPanel(new BorderLayout(10, 10));

    private final JPanel mainPanel
        = new TransparentPanel(new BorderLayout(5, 5));

    private final SIPAccountRegistrationForm regform;

    private Component registrationForm;

    private boolean isSimpleForm;

    private Component registerChoicePanel;

    /**
     * Creates an instance of the <tt>AccountPanel</tt>.
     * @param regform the parent registration form
     */
    public AccountPanel(SIPAccountRegistrationForm regform)
    {
        super(new BorderLayout());

        this.regform = regform;
        this.regform.addValidatingPanel(this);

        this.userIDField.getDocument().addDocumentListener(this);

        this.rememberPassBox.setSelected(true);

        existingAccountButton = new JRadioButton(
            regform.getExistingAccountLabel());

        createAccountButton = new JRadioButton(
            regform.getCreateAccountLabel());

        JLabel uinExampleLabel = new JLabel(regform.getUsernameExample());
        uinExampleLabel.setForeground(Color.GRAY);
        uinExampleLabel.setFont(uinExampleLabel.getFont().deriveFont(8));
        emptyPanel.setMaximumSize(new Dimension(40, 35));
        uinExampleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 8, 0));

        JLabel uinLabel
            = new JLabel(regform.getUsernameLabel());

        JLabel passLabel
            = new JLabel(Resources.getString("service.gui.PASSWORD"));

        labelsPanel.add(uinLabel);
        labelsPanel.add(emptyPanel);
        labelsPanel.add(passLabel);

        valuesPanel.add(userIDField);
        valuesPanel.add(uinExampleLabel);
        valuesPanel.add(passField);

        TransparentPanel southPanel
            = new TransparentPanel(new GridLayout(1, 2));

        uinPassPanel.add(labelsPanel, BorderLayout.WEST);
        uinPassPanel.add(valuesPanel, BorderLayout.CENTER);
        uinPassPanel.add(southPanel, BorderLayout.SOUTH);

        southPanel.add(rememberPassBox);

        String webSignupLinkText = regform.getWebSignupLinkName();

        if (webSignupLinkText != null && webSignupLinkText.length() > 0)
            southPanel.add(createWebSignupLabel(webSignupLinkText));
        else
        {
            String forgotPassLinkText = regform.getForgotPasswordLinkName();

            if (forgotPassLinkText != null && forgotPassLinkText.length() > 0)
            southPanel.add(createForgotPasswordLabel(forgotPassLinkText));
        }

        uinPassPanel.setBorder(BorderFactory.createTitledBorder(Resources
            .getString("plugin.sipaccregwizz.USERNAME_AND_PASSWORD")));

        SIPAccountCreationFormService createAccountService
            = regform.getCreateAccountService();

        if (createAccountService != null && isSimpleForm)
        {
            registrationForm = createAccountService.getForm();
            registerChoicePanel = createRegisterChoicePanel();
            mainPanel.add(registerChoicePanel, BorderLayout.NORTH);
        }
        else
            mainPanel.add(uinPassPanel, BorderLayout.NORTH);

        this.add(mainPanel, BorderLayout.NORTH);
    }

    /**
     * Initializes the advanced account panel.
     */
    void initAdvancedForm()
    {
        // If it's not yet added.
        if (displayNameLabel.getParent() == null)
            labelsPanel.add(displayNameLabel);

        // If it's not yet added.
        if (displayNameField.getParent() == null)
            valuesPanel.add(displayNameField);

        // Select the existing account radio button by default.
        existingAccountButton.setSelected(true);

        // Indicate that this panel is opened in a simple form.
        setSimpleForm(false);
    }

    /**
     * Handles the <tt>DocumentEvent</tt> triggered when user types in the UIN
     * field. Enables or disables the "Next" wizard button according to whether
     * the UIN field is empty.
     * @param e the <tt>DocumentEvent</tt> that notified us
     */
    public void insertUpdate(DocumentEvent e)
    {
        regform.setServerFieldAccordingToUIN(userIDField.getText());
        regform.reValidateInput();
    }

    /**
     * Handles the <tt>DocumentEvent</tt> triggered when user deletes letters
     * from the UIN field. Enables or disables the "Next" wizard button
     * according to whether the UIN field is empty.
     * @param e the <tt>DocumentEvent</tt> that notified us
     */
    public void removeUpdate(DocumentEvent e)
    {
        regform.setServerFieldAccordingToUIN(userIDField.getText());
        regform.reValidateInput();
    }

    public void changedUpdate(DocumentEvent e) {}

    /**
     * Returns the user identifier entered by the user.
     * @return the user identifier
     */
    String getUserID()
    {
        String userID = userIDField.getText();

        if(userID.startsWith("sip:"))
            return userID.substring(4);

        return userID;
    }

    /**
     * Returns the password entered by the user.
     * @return the password
     */
    char[] getPassword()
    {
        return passField.getPassword();
    }

    /**
     * Indicates if the "remember password" check box is selected.
     * @return <tt>true</tt> if the "remember password" check box is selected,
     * otherwise returns <tt>false</tt>
     */
    boolean isRememberPassword()
    {
        return rememberPassBox.isSelected();
    }

    /**
     * Returns the display name of the account.
     * @return the display name of the account
     */
    String getDisplayName()
    {
        return displayNameField.getText();
    }

    /**
     * Sets the display name of the account.
     * @param displayName the display name of the account
     */
    void setDisplayName(String displayName)
    {
        displayNameField.setText(displayName);
    }

    /**
     * Enables/disables the user id text field.
     * @param isEnabled <tt>true</tt> to enable the user id text field,
     * <tt>false</tt> - otherwise
     */
    void setUserIDEnabled(boolean isEnabled)
    {
        userIDField.setEnabled(isEnabled);
    }

    /**
     * Sets the user id.
     * @param userID the user id to set
     */
    void setUserID(String userID)
    {
        userIDField.setText(userID);
        regform.reValidateInput();
    }

    /**
     * Sets the password
     * @param password the password
     */
    void setPassword(String password)
    {
        this.passField.setText(password);
    }

    /**
     * Sets the password remember check box.
     * @param isRememberPassword <tt>true</tt> to select the remember password
     * check box, <tt>false</tt> - otherwise
     */
    void setRememberPassword(boolean isRememberPassword)
    {
        rememberPassBox.setSelected(isRememberPassword);
    }

    /**
     * Creates the subscribe label.
     * @param linkName the link name
     * @return the newly created subscribe label
     */
    private Component createWebSignupLabel(String linkName)
    {
        JLabel subscribeLabel =
            new JLabel("<html><a href=''>"
                + linkName
                + "</a></html>",
                JLabel.RIGHT);

        subscribeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        subscribeLabel.setToolTipText(
            Resources.getString("plugin.simpleaccregwizz.SPECIAL_SIGNUP"));
        subscribeLabel.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                try
                {
                    regform.webSignup();
                }
                catch (UnsupportedOperationException ex)
                {
                    // This should not happen, because we check if the
                    // operation is supported, before adding the sign
                    // up.
                    logger.error("The web sign up is not supported.",
                        ex);
                }
            }
        });
        return subscribeLabel;
    }

    /**
     * Creates the subscribe label.
     * @param linkName the link name
     * @return the newly created subscribe label
     */
    private Component createForgotPasswordLabel(String linkName)
    {
        JLabel subscribeLabel =
            new JLabel("<html><a href=''>"
                + linkName
                + "</a></html>",
                JLabel.RIGHT);

        subscribeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        subscribeLabel.setToolTipText(
            Resources.getString("plugin.simpleaccregwizz.FORGOT_PASSWORD"));
        subscribeLabel.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                try
                {
                    SIPAccRegWizzActivator.getBrowserLauncher()
                        .openURL(regform.getForgotPasswordLink());
                }
                catch (UnsupportedOperationException ex)
                {
                    // This should not happen, because we check if the
                    // operation is supported, before adding the sign
                    // up.
                    logger.error("The web sign up is not supported.",
                        ex);
                }
            }
        });
        return subscribeLabel;
    }

    /**
     * Creates a register choice panel.
     * @return the created component
     */
    private Component createRegisterChoicePanel()
    {
        JPanel registerChoicePanel = new TransparentPanel(new GridLayout(0, 1));

        existingAccountButton.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                if (existingAccountButton.isSelected())
                {
                    mainPanel.remove(registrationForm);
                    mainPanel.add(uinPassPanel, BorderLayout.CENTER);

                    Window window
                        = SwingUtilities.getWindowAncestor(AccountPanel.this);

                    if (window != null)
                        window.pack();
                }
            }
        });

        createAccountButton.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                if (createAccountButton.isSelected())
                {
                    mainPanel.remove(uinPassPanel);
                    mainPanel.add(registrationForm, BorderLayout.CENTER);
                    SwingUtilities.getWindowAncestor(AccountPanel.this).pack();
                }
            }
        });

        ButtonGroup buttonGroup = new ButtonGroup();

        existingAccountButton.setOpaque(false);
        createAccountButton.setOpaque(false);

        buttonGroup.add(existingAccountButton);
        buttonGroup.add(createAccountButton);

        registerChoicePanel.add(existingAccountButton);
        registerChoicePanel.add(createAccountButton);

        // By default we select the existing account button.
        existingAccountButton.setSelected(true);

        return registerChoicePanel;
    }

    /**
     * Indicates if the account information provided by this form is for new
     * account or an existing one.
     * @return <tt>true</tt> if the account information provided by this form
     * is for new account or <tt>false</tt> if it's for an existing one
     */
    boolean isCreateAccount()
    {
        return createAccountButton.isSelected();
    }

    /**
     * Whether current inserted values into the panel are valid and enough
     * to continue with account creation/modification.
     *
     * @return whether the input values are ok to continue with account
     * creation/modification.
     */
    public boolean isValidated()
    {
        return userIDField.getText() != null
                && userIDField.getText().length() > 0;
    }

    /**
     * Sets to <tt>true</tt> if this panel is opened in a simple form and
     * <tt>false</tt> if it's opened in an advanced form.
     *
     * @param isSimpleForm indicates if this panel is opened in a simple form or
     * in an advanced form
     */
    void setSimpleForm(boolean isSimpleForm)
    {
        this.isSimpleForm = isSimpleForm;

        SIPAccountCreationFormService createAccountService
            = regform.getCreateAccountService();

        if (createAccountService != null && isSimpleForm)
        {
            registrationForm = createAccountService.getForm();
            if (uinPassPanel != null)
                mainPanel.remove(uinPassPanel);

            registerChoicePanel = createRegisterChoicePanel();
            mainPanel.add(registerChoicePanel, BorderLayout.NORTH);
        }
        else
        {
            if (registerChoicePanel != null)
                mainPanel.remove(registerChoicePanel);

            mainPanel.add(uinPassPanel, BorderLayout.NORTH);
        }
    }

    /**
     * Returns <tt>true</tt> if this panel is opened in a simple form and
     * <tt>false</tt> if it's opened in an advanced form.
     *
     * @return <tt>true</tt> if this panel is opened in a simple form and
     * <tt>false</tt> if it's opened in an advanced form
     */
    boolean isSimpleForm()
    {
        return isSimpleForm;
    }

    /**
     * Selects the create account button.
     */
    void setCreateButtonSelected()
    {
        createAccountButton.setSelected(true);
    }
}