merge-update from trunk
[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.widgets.Composite;
18 import org.eclipse.swt.widgets.Text;
19
20 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
21 import eu.etaxonomy.cdm.api.service.IUserService;
22 import eu.etaxonomy.cdm.model.common.User;
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24 import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
25 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmEntityWizardPage;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
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 * <p>UserWizardPage class.</p>
33 *
34 * @author n.hoffmann
35 * @created 02.07.2009
36 * @version 1.0
37 */
38 public class PasswordWizardPage extends AbstractCdmEntityWizardPage<User> implements ModifyListener{
39
40 /** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
41 public static final String NAME = "USER_WIZARD_PAGE";
42
43 private TextWithLabelElement text_oldPassword;
44 private TextWithLabelElement text_password;
45 private TextWithLabelElement text_passwordRepeat;
46 private PasswordValidator passwordValidator;
47
48 private UniqueUserNameValidator uniqueUsernameValidator;
49
50 /**
51 * @param formFactory
52 * @param conversation
53 * @param entity
54 */
55 protected PasswordWizardPage(CdmFormFactory formFactory,
56 ConversationHolder conversation, User entity) {
57 super(formFactory, conversation, entity);
58
59 passwordValidator = new PasswordValidator();
60 uniqueUsernameValidator = new UniqueUserNameValidator();
61 }
62
63 /* (non-Javadoc)
64 * @see eu.etaxonomy.taxeditor.ui.forms.AbstractCdmEntityWizardPage#createControl(org.eclipse.swt.widgets.Composite)
65 */
66 @Override
67 public void createControl(Composite parent) {
68 Composite control = formFactory.createComposite(parent);
69 control.setLayoutData(LayoutConstants.FILL());
70
71 setPageComplete(false);
72
73 control.setLayout(LayoutConstants.LAYOUT(2, false));
74 WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder());
75
76 if(!CdmStore.getLoginManager().isAdmin()){
77 text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD);
78 }
79 text_password = formFactory.createTextWithLabelElement(rootElement, "New Password", null, SWT.PASSWORD);
80 text_passwordRepeat = formFactory.createTextWithLabelElement(rootElement, "Repeat Password", null, SWT.PASSWORD);
81
82 ((Text)text_passwordRepeat.getMainControl()).addModifyListener(this);
83
84 setControl(control);
85 }
86
87
88 /* (non-Javadoc)
89 * @see eu.etaxonomy.taxeditor.ui.forms.AbstractCdmEntityWizardPage#createElement(eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement)
90 */
91 @Override
92 public AbstractCdmDetailElement<User> createElement(
93 ICdmFormElement rootElement) {
94 // not used
95 return null;
96 }
97
98 /* (non-Javadoc)
99 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
100 */
101 /** {@inheritDoc} */
102 public void modifyText(ModifyEvent e) {
103 validate();
104 }
105
106 private void validate(){
107 String message;
108
109 if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
110 // pass
111 }
112 else if((message = passwordValidator.isValid(text_password.getText())) != null){
113 // pass
114 }
115
116 setErrorMessage(message);
117 }
118
119 private class UniqueUserNameValidator implements IInputValidator{
120
121 private static final String USER_EXISTS = "Username already exists";
122 private static final String NAME_TO_SHORT = "Username is empty";
123
124 /* (non-Javadoc)
125 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
126 */
127 public String isValid(String newText) {
128 // query for username
129 if(newText.length() < 1){
130 setPageComplete(false);
131 return NAME_TO_SHORT;
132 }
133 if(CdmStore.getService(IUserService.class).userExists(newText)){
134 setPageComplete(false);
135 return USER_EXISTS;
136 }
137
138 setPageComplete(true);
139 return null;
140 }
141 }
142
143 private class PasswordValidator implements IInputValidator{
144
145 private static final int PW_MIN_LENGTH = 5;
146
147 private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters";
148 private static final String NO_MATCH = "The passwords do not match";
149
150 /* (non-Javadoc)
151 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
152 */
153 public String isValid(String newText) {
154 if(newText.length() < PW_MIN_LENGTH){
155 setPageComplete(false);
156 return TO_SHORT;
157 }
158
159 setPageComplete(true);
160 return null;
161 }
162
163 public String passwordsMatch(String password1, String password2){
164
165 if(! password1.equals(password2)){
166 setPageComplete(false);
167 return NO_MATCH;
168 }
169
170 setPageComplete(true);
171 return null;
172 }
173
174 }
175
176 /**
177 * @return
178 */
179 public String getOldPassword() {
180 return text_oldPassword.getText();
181 }
182
183 /**
184 * @return
185 */
186 public String getNewPassword() {
187 return text_password.getText();
188 }
189 }