]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui/src/org/argeo/security/ui/dialogs/ChangePasswordDialog.java
Refactoring: move all code needing the workbench from eclipse.ui to eclipse.ui.workbe...
[lgpl/argeo-commons.git] / org.argeo.security.ui / src / org / argeo / security / 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.security.ui.dialogs;
17
18 import org.argeo.ArgeoException;
19 import org.argeo.eclipse.ui.workbench.ErrorFeedback;
20 import org.eclipse.jface.dialogs.IMessageProvider;
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 Text currentPassword, newPassword1, newPassword2;
36 private UserDetailsManager userDetailsManager;
37
38 public ChangePasswordDialog(Shell parentShell,
39 UserDetailsManager securityService) {
40 super(parentShell);
41 this.userDetailsManager = securityService;
42 }
43
44 protected Point getInitialSize() {
45 return new Point(300, 250);
46 }
47
48 protected Control createDialogArea(Composite parent) {
49 Composite dialogarea = (Composite) super.createDialogArea(parent);
50 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
51 Composite composite = new Composite(dialogarea, SWT.NONE);
52 composite.setLayout(new GridLayout(2, false));
53 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
54 currentPassword = createLP(composite, "Current password");
55 newPassword1 = createLP(composite, "New password");
56 newPassword2 = createLP(composite, "Repeat new password");
57
58 setMessage("Change password", IMessageProvider.INFORMATION);
59 parent.pack();
60 return composite;
61 }
62
63 @Override
64 protected void okPressed() {
65 if (!newPassword1.getText().equals(newPassword2.getText()))
66 throw new ArgeoException("Passwords are different");
67 try {
68 userDetailsManager.changePassword(currentPassword.getText(),
69 newPassword1.getText());
70 close();
71 } catch (Exception e) {
72 ErrorFeedback.show("Cannot change password", e);
73 }
74 }
75
76 /** Creates label and password. */
77 protected Text createLP(Composite parent, String label) {
78 new Label(parent, SWT.NONE).setText(label);
79 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.PASSWORD
80 | SWT.BORDER);
81 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
82 return text;
83 }
84
85 protected void configureShell(Shell shell) {
86 super.configureShell(shell);
87 shell.setText("Change password");
88 }
89
90 }