Project

General

Profile

Download (8.69 KB) Statistics
| Branch: | Tag: | Revision:
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.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.model.agent.Person;
26
import eu.etaxonomy.cdm.model.common.User;
27
import eu.etaxonomy.taxeditor.model.CommonHelper;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
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 UserWizardPage extends WizardPage implements ModifyListener{
38

    
39
//	private static final Logger logger = Logger.getLogger(UserWizardPage.class);
40
	
41
	/** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
42
	public static final String NAME = "USER_WIZARD_PAGE";
43
	
44

    
45
	private Composite composite;
46
	private Text text_username;
47
	private Text text_oldPassword;
48
	private Text text_password;
49
	private Text text_passwordRepeat;
50
	private User user;
51
	private boolean newMode = false;
52
	private Group group_person;
53
	private Text text_personFirstName;
54
	private Text text_personLastName;
55
	private Text text_userEmail;
56

    
57
	private IInputValidator uniqueUserNameValidator;
58
	private PasswordValidator passwordValidator;
59
	
60
	/**
61
	 * <p>Constructor for UserWizardPage.</p>
62
	 *
63
	 * @param user a {@link eu.etaxonomy.cdm.model.common.User} object.
64
	 */
65
	protected UserWizardPage(User user) {
66
		super(NAME);
67
		
68
		this.user = user;
69
		
70
		if(user == null){
71
			setTitle("Create new user");
72
			setDescription("Enter information for new user.");
73
			newMode = true;
74
			this.user = User.NewInstance(null, null);
75
		}else{
76
			setTitle("Edit existing user");
77
			setDescription("Edit information for the selected user.");
78
		}
79

    
80
		uniqueUserNameValidator = new UniqueUserNameValidator();
81
		passwordValidator = new PasswordValidator();
82
	}
83

    
84

    
85
	/* (non-Javadoc)
86
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
87
	 */
88
	/** {@inheritDoc} */
89
	public void createControl(Composite parent) {
90
		// right - edit Form
91
		composite = new Composite(parent, SWT.NULL);
92
		GridLayout compositeLayout = new GridLayout();
93
		compositeLayout.numColumns = 2;
94
		composite.setLayout(compositeLayout);
95
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
96
		
97
		CLabel label_username = new CLabel(composite, SWT.NULL);
98
		label_username.setText("Username");
99
		text_username = new Text(composite, SWT.BORDER);
100
		text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
101
		text_username.addModifyListener(this);
102
		
103
		if(! newMode){
104
			text_username.setEnabled(false);
105
		}
106
		
107
		if(newMode){
108
			createPasswordFieldsNew(composite);
109
			setPageComplete(false);
110
		}else if(userIsAuthenticated()){
111
			createPasswordFieldsEdit(composite);
112
			setPageComplete(false);
113
		}
114

    
115
		group_person = createPersonGroup(composite);
116
		group_person.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
117
		
118
		setControl(composite);
119
		
120
		
121
		
122
		init();
123
	}
124

    
125
	/**
126
	 * @return
127
	 */
128
	private boolean userIsAuthenticated() {
129
		return user.equals(CdmStore.getLoginManager().getAuthenticatedUser());
130
	}
131

    
132
	private void createPasswordFieldsEdit(Composite parent){
133
		CLabel label_oldPassword = new CLabel(parent, SWT.NULL);
134
		label_oldPassword.setText("Old Password");
135
		text_oldPassword = new Text(parent, SWT.PASSWORD | SWT.BORDER);
136
		text_oldPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
137
		text_oldPassword.addModifyListener(this);
138
		
139
		createPasswordFieldsNew(parent);
140
	}
141
	
142
	private void createPasswordFieldsNew(Composite parent){
143
		CLabel label_password = new CLabel(parent, SWT.NULL);
144
		label_password.setText("Password");
145
		
146
		text_password = new Text(parent, SWT.PASSWORD | SWT.BORDER);
147
		text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
148
		text_password.addModifyListener(this);
149
		
150
		new CLabel(parent, SWT.NULL);
151
		text_passwordRepeat = new Text(parent, SWT.PASSWORD | SWT.BORDER);
152
		text_passwordRepeat.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
153
		text_passwordRepeat.addModifyListener(this);
154
	}
155
	
156
	private Group createPersonGroup(Composite parent){
157

    
158
		Group group = new Group(parent, SWT.NONE);
159
		GridLayout layout = new GridLayout();
160
		layout.numColumns = 2;
161
		group.setLayout(layout);
162
		
163
		CLabel label_personFirstName = new CLabel(group, SWT.NULL);
164
		label_personFirstName.setText("First Name");
165
		text_personFirstName = new Text(group, SWT.BORDER);
166
		text_personFirstName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
167
		text_personFirstName.addModifyListener(this);
168
		
169
		CLabel label_personLastName = new CLabel(group, SWT.NULL);
170
		label_personLastName.setText("Last Name");
171
		text_personLastName = new Text(group, SWT.BORDER);
172
		text_personLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
173
		text_personLastName.addModifyListener(this);
174
		
175
		
176
		CLabel label_userEmail = new CLabel(group, SWT.NULL);
177
		label_userEmail.setText("Email");
178
		text_userEmail = new Text(group, SWT.BORDER);
179
		text_userEmail.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
180
		text_userEmail.addModifyListener(this);
181
		
182
		return group;
183
	}
184
	
185
	/**
186
	 * 
187
	 */
188
	private void init() {
189
		CommonHelper.editTextWithoutTriggeringListeners(text_username, this, user.getUsername());
190
		if(user.getPerson() == null){
191
			user.setPerson(Person.NewInstance());
192
		}else{	
193
			CommonHelper.editTextWithoutTriggeringListeners(text_personFirstName, this, user.getPerson().getFirstname());
194
			CommonHelper.editTextWithoutTriggeringListeners(text_personLastName, this, user.getPerson().getLastname());
195
			CommonHelper.editTextWithoutTriggeringListeners(text_userEmail, this, user.getEmailAddress());
196
		}
197
	}
198

    
199
	/* (non-Javadoc)
200
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
201
	 */
202
	/** {@inheritDoc} */
203
	public void modifyText(ModifyEvent e) {
204
		if(newMode){
205
			user.setUsername(text_username.getText().trim());			
206
			user.setPassword(text_password.getText());
207
			validate();
208
		}else if(userIsAuthenticated()){
209
			user.setPassword(text_password.getText());
210
			validate();
211
		}
212
		user.getPerson().setFirstname(text_personFirstName.getText());
213
		user.getPerson().setLastname(text_personLastName.getText());
214
		user.setEmailAddress(text_userEmail.getText());
215
	}
216

    
217
	/**
218
	 * <p>Getter for the field <code>user</code>.</p>
219
	 *
220
	 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
221
	 */
222
	public User getUser() {
223
		return user;
224
	}
225
	
226
	private void validate(){
227
		String message;
228
		
229
		if(!userIsAuthenticated()){
230
			if((message = uniqueUserNameValidator.isValid(text_username.getText())) != null){
231
				// pass
232
			}
233
		}else if((message = passwordValidator.isValid(text_password.getText())) != null){
234
			// pass
235
		}else if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
236
			// pass
237
		}
238
		
239
		setErrorMessage(message);		
240
	}
241
	
242
	private class UniqueUserNameValidator implements IInputValidator{
243

    
244
		private static final String USER_EXISTS = "Username already exists";
245
		private static final String NAME_TO_SHORT = "Username is empty";
246
		
247
		/* (non-Javadoc)
248
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
249
		 */
250
		public String isValid(String newText) {
251
			// query for username
252
			if(newText.length() < 1){
253
				setPageComplete(false);
254
				return NAME_TO_SHORT;
255
			}
256
			if(CdmStore.getUserService().userExists(newText)){
257
				setPageComplete(false);
258
				return USER_EXISTS;
259
			}
260
			
261
			setPageComplete(true);
262
			return null;
263
		}
264
	}
265
	
266
	private class PasswordValidator implements IInputValidator{
267

    
268
		private static final int PW_MIN_LENGTH = 5;
269
		
270
		private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters";
271
		private static final String NO_MATCH = "The passwords do not match";
272
		
273
		/* (non-Javadoc)
274
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
275
		 */
276
		public String isValid(String newText) {
277
			if(newText.length() < PW_MIN_LENGTH){
278
				setPageComplete(false);
279
				return TO_SHORT;
280
			}
281
			
282
			setPageComplete(true);
283
			return null;
284
		}
285
		
286
		public String passwordsMatch(String password1, String password2){
287

    
288
			if(! password1.equals(password2)){
289
				setPageComplete(false);
290
				return NO_MATCH;
291
			}
292
			
293
			setPageComplete(true);
294
			return null;
295
		}
296
		
297
	}
298
}
(2-2/2)