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