aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/login/AuthenticationWindow.java
blob: df9cbdfe2699b9d30cd11c61afd17f1fc7172a71 (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
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.main.login;

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

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.main.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.swing.*;

/**
 * The <tt>LoginWindow</tt> is the window where the user should type his
 * user identifier and password to login.
 *
 * @author Yana Stamcheva
 */
public class AuthenticationWindow
    extends SIPCommFrame
    implements ActionListener
{
    private final JTextArea realmTextArea = new JTextArea();

    private JComponent uinValue;

    private final JPasswordField passwdField = new JPasswordField(15);

    private final JButton loginButton = new JButton(
        GuiActivator.getResources().getI18NString("service.gui.OK"));

    private final JButton cancelButton = new JButton(
        GuiActivator.getResources().getI18NString("service.gui.CANCEL"));

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

    private final TransparentPanel textFieldsPanel
        = new TransparentPanel(new GridLayout(0, 1, 8, 8));

    private final TransparentPanel mainPanel
        = new TransparentPanel(new BorderLayout(10, 10));

    private final TransparentPanel buttonsPanel
        = new TransparentPanel(new FlowLayout(FlowLayout.CENTER));

    private final JCheckBox rememberPassCheckBox
        = new SIPCommCheckBox(GuiActivator.getResources()
            .getI18NString("service.gui.REMEMBER_PASSWORD"));

    private LoginWindowBackground backgroundPanel;

    private UserCredentials userCredentials;

    private final Object lock = new Object();

    private String realm;

    private final boolean isUserNameEditable;

    /**
     * Creates an instance of the <tt>LoginWindow</tt>.
     * @param mainFrame the parent <tt>MainFrame</tt> window.
     * @param protocolProvider the protocol provider.
     * @param realm the realm
     * @param userCredentials the user credentials
     * @param isUserNameEditable indicates if the user name should be editable
     * by the user or not.
     */
    public AuthenticationWindow(MainFrame mainFrame,
                ProtocolProviderService protocolProvider,
                String realm,
                UserCredentials userCredentials,
                boolean isUserNameEditable)
    {
        this.userCredentials = userCredentials;

        this.realm = realm;

        this.isUserNameEditable = isUserNameEditable;

        Image logoImage = null;
        if(protocolProvider != null)
        {
            ProtocolIcon protocolIcon = protocolProvider.getProtocolIcon();

            if(protocolIcon.isSizeSupported(ProtocolIcon.ICON_SIZE_64x64))
                logoImage = ImageLoader.getBytesInImage(
                    protocolIcon.getIcon(ProtocolIcon.ICON_SIZE_64x64));
            else if(protocolIcon.isSizeSupported(ProtocolIcon.ICON_SIZE_48x48))
                logoImage = ImageLoader.getBytesInImage(
                    protocolIcon.getIcon(ProtocolIcon.ICON_SIZE_48x48));

            this.setTitle(GuiActivator.getResources().getI18NString(
                "service.gui.AUTHENTICATION_WINDOW_TITLE",
                new String[]{protocolProvider.getProtocolName()}));
        }

        backgroundPanel = new LoginWindowBackground(logoImage);
        this.backgroundPanel.setBorder(
                BorderFactory.createEmptyBorder(20, 5, 5, 5));
        this.backgroundPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        this.backgroundPanel.setPreferredSize(new Dimension(420, 230));

        this.getContentPane().setLayout(new BorderLayout());

        this.init();

        this.getContentPane().add(backgroundPanel, BorderLayout.CENTER);

        this.setResizable(false);

        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        this.enableKeyActions();
    }

    /**
     * Creates an instance of the <tt>LoginWindow</tt>.
     * @param mainFrame the parent <tt>MainFrame</tt> window.
     */
    public AuthenticationWindow(MainFrame mainFrame)
    {
        this(mainFrame, null, null, null, false);
    }

    /**
     * Creates an instance of the <tt>LoginWindow</tt>.
     * @param mainFrame the parent <tt>MainFrame</tt> window.
     * @param protocolProvider the protocol provider.
     * @param realm the realm
     * @param userCredentials the user credentials
     * @param isUserNameEditable indicates if the user name should be editable
     * by the user or not.
     * @param errorMessage an error message explaining a reason for opening
     * the authentication dialog (when a wrong password was provided, etc.)
     */
    public AuthenticationWindow(MainFrame mainFrame,
                ProtocolProviderService protocolProvider,
                String realm,
                UserCredentials userCredentials,
                boolean isUserNameEditable,
                String errorMessage)
    {
        this(  mainFrame,
                protocolProvider,
                realm,
                userCredentials,
                isUserNameEditable);

        this.realmTextArea.setForeground(Color.RED);
        this.realmTextArea.setText(errorMessage);
    }

    /**
     * Constructs the <tt>LoginWindow</tt>.
     */
    private void init()
    {
        if(userCredentials != null)
        {
            if(!isUserNameEditable)
                this.uinValue = new JLabel(userCredentials.getUserName());
            else
                this.uinValue = new JTextField(userCredentials.getUserName());

            char[] password = userCredentials.getPassword();
            if (password != null)
            {
                this.passwdField.setText(String.valueOf(password));
            }
        }
        else
        {
            // no user credentials just an empty field
            this.uinValue = new JTextField();
        }

        this.realmTextArea.setEditable(false);
        this.realmTextArea.setOpaque(false);
        this.realmTextArea.setLineWrap(true);
        this.realmTextArea.setWrapStyleWord(true);
        this.realmTextArea.setFont(
            realmTextArea.getFont().deriveFont(Font.BOLD));
        this.realmTextArea.setText(
            GuiActivator.getResources().getI18NString(
                "service.gui.SECURITY_AUTHORITY_REALM",
                new String[]{realm}));

        JLabel uinLabel
            = new JLabel(
                    GuiActivator.getResources().getI18NString(
                        "service.gui.IDENTIFIER"));
        uinLabel.setFont(uinLabel.getFont().deriveFont(Font.BOLD));

        JLabel passwdLabel
            = new JLabel(
                    GuiActivator.getResources().getI18NString(
                        "service.gui.PASSWORD"));
        passwdLabel.setFont(passwdLabel.getFont().deriveFont(Font.BOLD));

        this.labelsPanel.add(uinLabel);
        this.labelsPanel.add(passwdLabel);
        this.labelsPanel.add(new JLabel());

        this.rememberPassCheckBox.setOpaque(false);

        this.textFieldsPanel.add(uinValue);
        this.textFieldsPanel.add(passwdField);
        this.textFieldsPanel.add(rememberPassCheckBox);

        this.buttonsPanel.add(loginButton);
        this.buttonsPanel.add(cancelButton);

        this.mainPanel.add(realmTextArea, BorderLayout.NORTH);
        this.mainPanel.add(labelsPanel, BorderLayout.WEST);
        this.mainPanel.add(textFieldsPanel, BorderLayout.CENTER);
        this.mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

        this.backgroundPanel.add(mainPanel, BorderLayout.CENTER);

        this.loginButton.setName("ok");
        this.cancelButton.setName("cancel");

        this.loginButton.setMnemonic(
            GuiActivator.getResources().getI18nMnemonic("service.gui.OK"));
        this.cancelButton.setMnemonic(
            GuiActivator.getResources().getI18nMnemonic("service.gui.CANCEL"));

        this.loginButton.addActionListener(this);
        this.cancelButton.addActionListener(this);

        this.getRootPane().setDefaultButton(loginButton);

        this.setTransparent(true);
    }

    /**
     * Sets transparent background to all components in the login window,
     * because of the non-white background.
     * @param transparent <code>true</code> to set a transparent background,
     * <code>false</code> otherwise.
     */
    private void setTransparent(boolean transparent)
    {
        this.mainPanel.setOpaque(!transparent);
        this.labelsPanel.setOpaque(!transparent);
        this.textFieldsPanel.setOpaque(!transparent);
        this.buttonsPanel.setOpaque(!transparent);
    }

    /**
     * Handles the <tt>ActionEvent</tt> triggered when one of the buttons is
     * clicked. When "Login" button is choosen installs a new account from
     * the user input and logs in.
     *
     * @param evt the action event that has just occurred.
     */
    public void actionPerformed(ActionEvent evt)
    {
        JButton button = (JButton) evt.getSource();
        String buttonName = button.getName();

        if ("ok".equals(buttonName))
        {
            if(uinValue instanceof JLabel)
                userCredentials.setUserName(((JLabel)uinValue).getText());
            else if(uinValue instanceof JTextField)
                userCredentials.setUserName(((JTextField)uinValue).getText());

            userCredentials.setPassword(
                    passwdField.getPassword());
            userCredentials.setPasswordPersistent(
                    rememberPassCheckBox.isSelected());
        }
        else
        {
            // if userCredentials are created outside the exported window
            // by specifying null username we note that the window was canceled
            if (this.userCredentials != null)
            {
                this.userCredentials.setUserName(null);
                this.userCredentials = null;
            }
        }

        synchronized (lock)
        {
            lock.notify();
        }

        this.dispose();
    }

    /**
     * The <tt>LoginWindowBackground</tt> is a <tt>JPanel</tt> that overrides
     * the <code>paintComponent</code> method to provide a custom background
     * image for this window.
     */
    private static class LoginWindowBackground
        extends TransparentPanel
    {
        private final Image bgImage;

        LoginWindowBackground(Image bgImage)
        {
            this.bgImage = bgImage;
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);

            if (bgImage != null)
            {
                g = g.create();
                try
                {
                    AntialiasingManager.activateAntialiasing(g);

                    g.drawImage(bgImage, 30, 30, null);
                }
                finally
                {
                    g.dispose();
                }
            }
        }
    }

    /**
     * Enables the actions when a key is pressed, for now
     * closes the window when esc is pressed.
     */
    private void enableKeyActions()
    {
        UIAction act = new UIAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                AuthenticationWindow.this.setVisible(false);
            }
        };

        getRootPane().getActionMap().put("close", act);

        InputMap imap = this.getRootPane().getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
    }

    @Override
    protected void close(boolean isEscaped)
    {
        this.cancelButton.doClick();
    }

    /**
     * Shows this modal dialog.
     *
     * @param isVisible specifies whether we should be showing or hiding the
     * window.
     */
    @Override
    public void setVisible(boolean isVisible)
    {
        this.setName("AUTHENTICATION");

        super.setVisible(isVisible);

        if(isVisible)
        {
            this.passwdField.requestFocus();

            synchronized (lock)
            {
                try
                {
                    lock.wait();
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    void setParams(Object[] windowParams)
    {
        if(windowParams != null && windowParams.length > 0)
        {
            Object param = windowParams[0];
            if(param instanceof UserCredentials)
            {
                this.userCredentials = (UserCredentials)param;
            }
        }
    }
}