]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ChangePasswordDialog.java
943a00e30d0e54d61d56a92a12e465fc683e5343
[lgpl/argeo-commons.git] / ChangePasswordDialog.java
1 package org.argeo.security.ui.dialogs;
2
3 import org.argeo.ArgeoException;
4 import org.argeo.eclipse.ui.ErrorFeedback;
5 import org.eclipse.jface.dialogs.IMessageProvider;
6 import org.eclipse.jface.dialogs.TitleAreaDialog;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.graphics.Point;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.swt.widgets.Text;
16 import org.springframework.security.userdetails.UserDetailsManager;
17
18 /** Dialog to change the current user password */
19 public class ChangePasswordDialog extends TitleAreaDialog {
20 private Text currentPassword, newPassword1, newPassword2;
21 private UserDetailsManager userDetailsManager;
22
23 public ChangePasswordDialog(Shell parentShell,
24 UserDetailsManager securityService) {
25 super(parentShell);
26 this.userDetailsManager = securityService;
27 }
28
29 protected Point getInitialSize() {
30 return new Point(300, 250);
31 }
32
33 protected Control createDialogArea(Composite parent) {
34 Composite dialogarea = (Composite) super.createDialogArea(parent);
35 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
36 Composite composite = new Composite(dialogarea, SWT.NONE);
37 composite.setLayout(new GridLayout(2, false));
38 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
39 currentPassword = createLP(composite, "Current password");
40 newPassword1 = createLP(composite, "New password");
41 newPassword2 = createLP(composite, "Repeat new password");
42
43 setMessage("Change password", IMessageProvider.INFORMATION);
44 parent.pack();
45 return composite;
46 }
47
48 @Override
49 protected void okPressed() {
50 if (!newPassword1.getText().equals(newPassword2.getText()))
51 throw new ArgeoException("Passwords are different");
52 try {
53 userDetailsManager.changePassword(currentPassword.getText(),
54 newPassword1.getText());
55 close();
56 } catch (Exception e) {
57 ErrorFeedback.show("Cannot change password", e);
58 }
59 }
60
61 /** Creates label and password. */
62 protected Text createLP(Composite parent, String label) {
63 new Label(parent, SWT.NONE).setText(label);
64 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD
65 | SWT.BORDER);
66 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
67 return text;
68 }
69
70 protected void configureShell(Shell shell) {
71 super.configureShell(shell);
72 shell.setText("Change password");
73 }
74
75 }