]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/ChangePasswordDialog.java
09fbc18d60c080c256942daa47bd8101692f9e49
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / dialogs / ChangePasswordDialog.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.dialogs;
17
18 import org.argeo.ArgeoException;
19 import org.eclipse.jface.dialogs.IMessageProvider;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.dialogs.TitleAreaDialog;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31 import org.springframework.security.userdetails.UserDetailsManager;
32
33 /** Dialog to change the current user password */
34 public class ChangePasswordDialog extends TitleAreaDialog {
35 private static final long serialVersionUID = -2447446550246803237L;
36
37 private Text currentPassword, newPassword1, newPassword2;
38 private UserDetailsManager userDetailsManager;
39
40 public ChangePasswordDialog(Shell parentShell,
41 UserDetailsManager securityService) {
42 super(parentShell);
43 this.userDetailsManager = securityService;
44 }
45
46 protected Point getInitialSize() {
47 return new Point(400, 450);
48 }
49
50 protected Control createDialogArea(Composite parent) {
51 Composite dialogarea = (Composite) super.createDialogArea(parent);
52 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
53 Composite composite = new Composite(dialogarea, SWT.NONE);
54 composite.setLayout(new GridLayout(2, false));
55 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
56 currentPassword = createLP(composite, "Current password");
57 newPassword1 = createLP(composite, "New password");
58 newPassword2 = createLP(composite, "Repeat new password");
59
60 setMessage("Change password", IMessageProvider.INFORMATION);
61 parent.pack();
62 return composite;
63 }
64
65 @Override
66 protected void okPressed() {
67 if (!newPassword1.getText().equals(newPassword2.getText()))
68 throw new ArgeoException("Passwords are different");
69 try {
70 userDetailsManager.changePassword(currentPassword.getText(),
71 newPassword1.getText());
72 close();
73 } catch (Exception e) {
74 MessageDialog.openError(newPassword1.getShell(), "Error",
75 "Cannot change password");
76 e.printStackTrace();
77 }
78 }
79
80 /** Creates label and password. */
81 protected Text createLP(Composite parent, String label) {
82 new Label(parent, SWT.NONE).setText(label);
83 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD
84 | SWT.BORDER);
85 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
86 return text;
87 }
88
89 protected void configureShell(Shell shell) {
90 super.configureShell(shell);
91 shell.setText("Change password");
92 }
93
94 }