summaryrefslogtreecommitdiff
path: root/src/plugins/common/src/com/motorola/studio/android/common/utilities/ui/PasswordInputDialog.java
blob: de80b330d4b03c9a77389975472b330ce0f753ee (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
/*
* Copyright (C) 2012 The Android Open Source Project
*
* 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 com.motorola.studio.android.common.utilities.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.motorola.studio.android.common.utilities.i18n.UtilitiesNLS;

public class PasswordInputDialog extends Dialog
{
    private static final int SMALL_TEXT_SIZE = 64;

    private String newPassword = null;

    private String oldPassword = null;

    private final boolean changePassword;

    private Text newPasswordConfirmText;

    private Text newPasswordText;

    private Text oldPasswordText;

    private Label message;

    private Label image;

    private final String description;

    private Button saveCheckBox;

    private boolean needToStorePassword = true;

    private int passwordMinimumSize = 1; //1 is default, it is recommended to change in the constructor

    /**
     * Create a new Input dialog
     * @param parent: the parent shell
     * @param description: the textual description of dialog
     * @param changePassword: true if you want this dialog be a change password dialog (it will present old, new and confirm new fields). False to show only enter password field
     * @param oldPassword: null if user must enter oldpassword. Anything to create dialog with preentered password (only if changePassword is true);
     */
    public PasswordInputDialog(Shell parent, String description, boolean changePassword,
            String oldPassword, int passwordMinimumSize)
    {
        super(parent);
        this.changePassword = changePassword;
        this.description = description;
        if (changePassword)
        {
            this.oldPassword = oldPassword;
        }
        this.passwordMinimumSize = passwordMinimumSize;
    }

    @Override
    protected Control createDialogArea(Composite parent)
    {
        Composite mainComposite = new Composite(parent, SWT.NONE);
        mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        mainComposite.setLayout(new GridLayout(2, false));

        /**
         * The Message Area (With an image and a text)
         */
        Composite messageComposite = new Composite(mainComposite, SWT.NONE);
        GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
        messageComposite.setLayoutData(layoutData);
        GridLayout layout = new GridLayout(2, false);
        layout.marginWidth = 0;
        messageComposite.setLayout(layout);

        image = new Label(messageComposite, SWT.NONE);
        image.setVisible(false);
        layoutData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        image.setLayoutData(layoutData);

        message = new Label(messageComposite, SWT.NONE);
        message.setText(description);
        layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        message.setLayoutData(layoutData);

        Label oldPasswordLabel = new Label(mainComposite, SWT.NONE);
        layoutData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
        oldPasswordLabel.setLayoutData(layoutData);
        oldPasswordLabel.setText(UtilitiesNLS.Passwordinput_Enterpassword_Label);

        oldPasswordText = new Text(mainComposite, SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
        oldPasswordText.setTextLimit(SMALL_TEXT_SIZE);
        layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        layoutData.minimumWidth = 150;
        oldPasswordText.setLayoutData(layoutData);
        if (oldPassword != null)
        {
            oldPasswordText.setEnabled(false);
            oldPasswordText.setText(oldPassword);
            oldPasswordLabel.setEnabled(false);
        }
        oldPasswordText.addModifyListener(new ModifyListener()
        {
            public void modifyText(ModifyEvent e)
            {
                updateStatus();
            }
        });

        /**
         * If the input is to change the passwords, create all other fields
         */
        if (changePassword)
        {
            Label separator = new Label(mainComposite, SWT.HORIZONTAL | SWT.SEPARATOR);
            layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
            separator.setLayoutData(layoutData);

            /**
             * The newPassword area
             */
            Label passwordLabel = new Label(mainComposite, SWT.NONE);
            layoutData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
            passwordLabel.setLayoutData(layoutData);
            passwordLabel.setText(UtilitiesNLS.Passwordinput_Enternewpassword_Label);

            newPasswordText = new Text(mainComposite, SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
            newPasswordText.setTextLimit(SMALL_TEXT_SIZE);
            layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
            layoutData.minimumWidth = 150;
            newPasswordText.setLayoutData(layoutData);

            Label passwordConfirmLabel = new Label(mainComposite, SWT.NONE);
            layoutData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
            passwordConfirmLabel.setLayoutData(layoutData);
            passwordConfirmLabel.setText(UtilitiesNLS.Passwordinput_Reenterpassword_Label);

            newPasswordConfirmText =
                    new Text(mainComposite, SWT.BORDER | SWT.SINGLE | SWT.PASSWORD);
            newPasswordConfirmText.setTextLimit(SMALL_TEXT_SIZE);
            layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
            layoutData.minimumWidth = 150;
            newPasswordConfirmText.setLayoutData(layoutData);

            newPasswordText.addModifyListener(new ModifyListener()
            {

                public void modifyText(ModifyEvent e)
                {
                    updateStatus();
                }
            });

            newPasswordConfirmText.addModifyListener(new ModifyListener()
            {

                public void modifyText(ModifyEvent e)
                {
                    updateStatus();
                }
            });
        }

        //Creates the save password checkbox
        saveCheckBox = new Button(mainComposite, SWT.CHECK);
        saveCheckBox.setText(UtilitiesNLS.PasswordProvider_SaveThisPassword);
        saveCheckBox.setSelection(false);
        GridData gridData = new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1);
        saveCheckBox.setLayoutData(gridData);

        return mainComposite;
    }

    public void updateStatus()
    {
        String errorMessage = null;
        int severity = IMessageProvider.NONE;

        if (oldPasswordText.getText().length() > 0)
        {
            oldPassword = oldPasswordText.getText();
        }
        else
        {
            errorMessage = "";
            oldPassword = null;
        }

        if ((errorMessage == null) && changePassword)
        {
            if (newPasswordText.getText().length() < passwordMinimumSize)
            {
                errorMessage =
                        UtilitiesNLS.bind(UtilitiesNLS.Passwordinput_Error_PasswordMinimumSize,
                                passwordMinimumSize);
                severity = IMessageProvider.ERROR;
            }
            else
            {
                if (!newPasswordText.getText().equals(newPasswordConfirmText.getText()))
                {
                    errorMessage = UtilitiesNLS.Passwordinput_Error_Passwordnotmatch;
                    severity = IMessageProvider.ERROR;
                }
                else
                {
                    newPassword = newPasswordText.getText();
                }
            }
        }

        setErrorMessage(errorMessage, severity);

    }

    private void setErrorMessage(String errorMsg, int severity)
    {
        if ((errorMsg == null) || (severity == IMessageProvider.NONE))
        {
            image.setImage(null);
            image.setVisible(false);
            message.setText(description);
            getButton(OK).setEnabled(errorMsg == null);
        }
        else
        {
            message.setText(errorMsg);
            message.setVisible(true);
            switch (severity)
            {
                case IMessageProvider.ERROR:
                    image.setImage(PlatformUI.getWorkbench().getSharedImages()
                            .getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
                    break;

                case IMessageProvider.INFORMATION:
                    image.setImage(PlatformUI.getWorkbench().getSharedImages()
                            .getImage(ISharedImages.IMG_OBJS_INFO_TSK));
                    break;

                case IMessageProvider.WARNING:
                    image.setImage(PlatformUI.getWorkbench().getSharedImages()
                            .getImage(ISharedImages.IMG_OBJS_WARN_TSK));
                    break;

                default:
                    image.setImage(PlatformUI.getWorkbench().getSharedImages()
                            .getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
                    break;
            }
            image.setVisible(true);
            getButton(OK).setEnabled(false);
        }
        message.getParent().layout();
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent)
    {
        super.createButtonsForButtonBar(parent);
        getButton(OK).setEnabled(false);
    }

    public String getNewPassword()
    {
        return newPassword;
    }

    public String getOldPassword()
    {
        return oldPassword;
    }

    public boolean needToStorePassword()
    {
        return needToStorePassword;
    }

    @Override
    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText(UtilitiesNLS.Passwordinput_Title);
        newShell.setMinimumSize(500, 200);
        newShell.layout(true);

    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed()
    {
        needToStorePassword = saveCheckBox.getSelection();
        super.okPressed();
    }

}