Started refactoring user and group functionality
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / account / user / wizard / UserWizardPage.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.account.user.wizard;
12
13 import org.eclipse.jface.dialogs.IInputValidator;
14 import org.eclipse.jface.wizard.WizardPage;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.CLabel;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Group;
23 import org.eclipse.swt.widgets.Text;
24
25 import eu.etaxonomy.cdm.api.service.IUserService;
26 import eu.etaxonomy.cdm.model.agent.Person;
27 import eu.etaxonomy.cdm.model.common.User;
28 import eu.etaxonomy.taxeditor.model.CommonHelper;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
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 UserWizardPage extends WizardPage implements ModifyListener{
39
40 // private static final Logger logger = Logger.getLogger(UserWizardPage.class);
41
42 /** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
43 public static final String NAME = "USER_WIZARD_PAGE";
44
45
46 private Composite composite;
47 private Text text_username;
48 private Text text_oldPassword;
49 private Text text_password;
50 private Text text_passwordRepeat;
51 private User user;
52 private boolean newMode = false;
53 private Group group_person;
54 private Text text_personFirstName;
55 private Text text_personLastName;
56 private Text text_userEmail;
57
58 private IInputValidator uniqueUserNameValidator;
59 private PasswordValidator passwordValidator;
60
61 /**
62 * <p>Constructor for UserWizardPage.</p>
63 *
64 * @param user a {@link eu.etaxonomy.cdm.model.common.User} object.
65 */
66 protected UserWizardPage(User user) {
67 super(NAME);
68
69 this.user = user;
70
71 if(user == null){
72 setTitle("Create new user");
73 setDescription("Enter information for new user.");
74 newMode = true;
75 this.user = User.NewInstance(null, null);
76 }else{
77 setTitle("Edit existing user");
78 setDescription("Edit information for the selected user.");
79 }
80
81 uniqueUserNameValidator = new UniqueUserNameValidator();
82 passwordValidator = new PasswordValidator();
83 }
84
85
86 /* (non-Javadoc)
87 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
88 */
89 /** {@inheritDoc} */
90 public void createControl(Composite parent) {
91 // right - edit Form
92 composite = new Composite(parent, SWT.NULL);
93 GridLayout compositeLayout = new GridLayout();
94 compositeLayout.numColumns = 2;
95 composite.setLayout(compositeLayout);
96 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
97
98 CLabel label_username = new CLabel(composite, SWT.NULL);
99 label_username.setText("Username");
100 text_username = new Text(composite, SWT.BORDER);
101 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
102 text_username.addModifyListener(this);
103
104 if(! newMode){
105 text_username.setEnabled(false);
106 }
107
108 if(newMode){
109 createPasswordFieldsNew(composite);
110 setPageComplete(false);
111 }else if(userIsAuthenticated()){
112 createPasswordFieldsEdit(composite);
113 setPageComplete(false);
114 }
115
116 group_person = createPersonGroup(composite);
117 group_person.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
118
119 setControl(composite);
120
121
122
123 init();
124 }
125
126 /**
127 * @return
128 */
129 private boolean userIsAuthenticated() {
130 return user.equals(CdmStore.getLoginManager().getAuthenticatedUser());
131 }
132
133 private void createPasswordFieldsEdit(Composite parent){
134 CLabel label_oldPassword = new CLabel(parent, SWT.NULL);
135 label_oldPassword.setText("Old Password");
136 text_oldPassword = new Text(parent, SWT.PASSWORD | SWT.BORDER);
137 text_oldPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
138 text_oldPassword.addModifyListener(this);
139
140 createPasswordFieldsNew(parent);
141 }
142
143 private void createPasswordFieldsNew(Composite parent){
144 CLabel label_password = new CLabel(parent, SWT.NULL);
145 label_password.setText("Password");
146
147 text_password = new Text(parent, SWT.PASSWORD | SWT.BORDER);
148 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
149 text_password.addModifyListener(this);
150
151 new CLabel(parent, SWT.NULL);
152 text_passwordRepeat = new Text(parent, SWT.PASSWORD | SWT.BORDER);
153 text_passwordRepeat.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
154 text_passwordRepeat.addModifyListener(this);
155 }
156
157 private Group createPersonGroup(Composite parent){
158
159 Group group = new Group(parent, SWT.NONE);
160 GridLayout layout = new GridLayout();
161 layout.numColumns = 2;
162 group.setLayout(layout);
163
164 CLabel label_personFirstName = new CLabel(group, SWT.NULL);
165 label_personFirstName.setText("First Name");
166 text_personFirstName = new Text(group, SWT.BORDER);
167 text_personFirstName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
168 text_personFirstName.addModifyListener(this);
169
170 CLabel label_personLastName = new CLabel(group, SWT.NULL);
171 label_personLastName.setText("Last Name");
172 text_personLastName = new Text(group, SWT.BORDER);
173 text_personLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
174 text_personLastName.addModifyListener(this);
175
176
177 CLabel label_userEmail = new CLabel(group, SWT.NULL);
178 label_userEmail.setText("Email");
179 text_userEmail = new Text(group, SWT.BORDER);
180 text_userEmail.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
181 text_userEmail.addModifyListener(this);
182
183 return group;
184 }
185
186 /**
187 *
188 */
189 private void init() {
190 CommonHelper.editTextWithoutTriggeringListeners(text_username, this, user.getUsername());
191 if(user.getPerson() == null){
192 user.setPerson(Person.NewInstance());
193 }else{
194 CommonHelper.editTextWithoutTriggeringListeners(text_personFirstName, this, user.getPerson().getFirstname());
195 CommonHelper.editTextWithoutTriggeringListeners(text_personLastName, this, user.getPerson().getLastname());
196 CommonHelper.editTextWithoutTriggeringListeners(text_userEmail, this, user.getEmailAddress());
197 }
198 }
199
200 /* (non-Javadoc)
201 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
202 */
203 /** {@inheritDoc} */
204 public void modifyText(ModifyEvent e) {
205 if(newMode){
206 user.setUsername(text_username.getText().trim());
207 user.setPassword(text_password.getText());
208 validate();
209 }else if(userIsAuthenticated()){
210 user.setPassword(text_password.getText());
211 validate();
212 }
213 user.getPerson().setFirstname(text_personFirstName.getText());
214 user.getPerson().setLastname(text_personLastName.getText());
215 user.setEmailAddress(text_userEmail.getText());
216 }
217
218 /**
219 * <p>Getter for the field <code>user</code>.</p>
220 *
221 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
222 */
223 public User getUser() {
224 return user;
225 }
226
227 private void validate(){
228 String message;
229
230 if(!userIsAuthenticated()){
231 if((message = uniqueUserNameValidator.isValid(text_username.getText())) != null){
232 // pass
233 }
234 }else if((message = passwordValidator.isValid(text_password.getText())) != null){
235 // pass
236 }else if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
237 // pass
238 }
239
240 setErrorMessage(message);
241 }
242
243 private class UniqueUserNameValidator implements IInputValidator{
244
245 private static final String USER_EXISTS = "Username already exists";
246 private static final String NAME_TO_SHORT = "Username is empty";
247
248 /* (non-Javadoc)
249 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
250 */
251 public String isValid(String newText) {
252 // query for username
253 if(newText.length() < 1){
254 setPageComplete(false);
255 return NAME_TO_SHORT;
256 }
257 if(CdmStore.getService(IUserService.class).userExists(newText)){
258 setPageComplete(false);
259 return USER_EXISTS;
260 }
261
262 setPageComplete(true);
263 return null;
264 }
265 }
266
267 private class PasswordValidator implements IInputValidator{
268
269 private static final int PW_MIN_LENGTH = 5;
270
271 private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters";
272 private static final String NO_MATCH = "The passwords do not match";
273
274 /* (non-Javadoc)
275 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
276 */
277 public String isValid(String newText) {
278 if(newText.length() < PW_MIN_LENGTH){
279 setPageComplete(false);
280 return TO_SHORT;
281 }
282
283 setPageComplete(true);
284 return null;
285 }
286
287 public String passwordsMatch(String password1, String password2){
288
289 if(! password1.equals(password2)){
290 setPageComplete(false);
291 return NO_MATCH;
292 }
293
294 setPageComplete(true);
295 return null;
296 }
297
298 }
299 }