Add menu entry for password dialog to main menu #3102
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / password / PasswordWizardPage.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.password;
12
13 import org.eclipse.jface.dialogs.IInputValidator;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Text;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.model.common.User;
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmEntityWizardPage;
25 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
27 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
28 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
29 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
30
31 /**
32 * @author n.hoffmann
33 * @created 02.07.2009
34 * @version 1.0
35 */
36 public class PasswordWizardPage extends AbstractCdmEntityWizardPage<User> implements ModifyListener{
37
38 public static final String NAME = "USER_WIZARD_PAGE";
39
40 private TextWithLabelElement text_oldPassword;
41 private TextWithLabelElement text_password;
42 private TextWithLabelElement text_passwordRepeat;
43 private final PasswordValidator passwordValidator;
44
45 protected PasswordWizardPage(CdmFormFactory formFactory,
46 ConversationHolder conversation, User entity) {
47 super(formFactory, conversation, entity);
48
49 passwordValidator = new PasswordValidator();
50
51 setTitle("Change password");
52 setDescription("Change password and confirm with current password");
53 }
54
55 @Override
56 public void createControl(Composite parent) {
57 Composite control = formFactory.createComposite(parent);
58 control.setLayoutData(LayoutConstants.FILL());
59
60 setPageComplete(false);
61
62 control.setLayout(LayoutConstants.LAYOUT(2, false));
63 WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder());
64
65 if(isChangingOwnPassword()) {
66 text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD);
67 }
68 text_password = formFactory.createTextWithLabelElement(rootElement, "New Password", null, SWT.PASSWORD);
69 text_passwordRepeat = formFactory.createTextWithLabelElement(rootElement, "Repeat Password", null, SWT.PASSWORD);
70
71 ((Text)text_passwordRepeat.getMainControl()).addModifyListener(this);
72
73 Color bgColor = getShell().getBackground();
74 rootElement.setBackground(bgColor);
75 control.setBackground(bgColor);
76 setControl(control);
77 }
78
79 protected boolean isChangingOwnPassword() {
80 return getEntity() != null && getEntity().getUsername().equals(CdmStore.getCurrentAuthentiation().getName());
81 }
82
83
84 @Override
85 public AbstractCdmDetailElement<User> createElement(
86 ICdmFormElement rootElement) {
87 // not used
88 return null;
89 }
90
91 /** {@inheritDoc} */
92 @Override
93 public void modifyText(ModifyEvent e) {
94 validate();
95 }
96
97 private void validate(){
98 String message;
99
100 if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
101 // pass
102 }
103 else if((message = passwordValidator.isValid(text_password.getText())) != null){
104 // pass
105 }
106
107 setErrorMessage(message);
108 }
109
110 private class PasswordValidator implements IInputValidator{
111
112 private static final int PW_MIN_LENGTH = 5;
113
114 private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters";
115 private static final String NO_MATCH = "The passwords do not match";
116
117 @Override
118 public String isValid(String newText) {
119 if(newText.length() < PW_MIN_LENGTH){
120 setPageComplete(false);
121 return TO_SHORT;
122 }
123
124 setPageComplete(true);
125 return null;
126 }
127
128 public String passwordsMatch(String password1, String password2){
129
130 if(! password1.equals(password2)){
131 setPageComplete(false);
132 return NO_MATCH;
133 }
134
135 setPageComplete(true);
136 return null;
137 }
138
139 }
140
141 public String getOldPassword() {
142 return text_oldPassword.getText();
143 }
144
145 public String getNewPassword() {
146 return text_password.getText();
147 }
148 }