3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.account
.user
.wizard
;
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
;
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
;
32 * <p>UserWizardPage class.</p>
38 public class UserWizardPage
extends WizardPage
implements ModifyListener
{
40 // private static final Logger logger = Logger.getLogger(UserWizardPage.class);
42 /** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
43 public static final String NAME
= "USER_WIZARD_PAGE";
46 private Composite composite
;
47 private Text text_username
;
48 private Text text_oldPassword
;
49 private Text text_password
;
50 private Text text_passwordRepeat
;
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
;
58 private IInputValidator uniqueUserNameValidator
;
59 private PasswordValidator passwordValidator
;
62 * <p>Constructor for UserWizardPage.</p>
64 * @param user a {@link eu.etaxonomy.cdm.model.common.User} object.
66 protected UserWizardPage(User user
) {
72 setTitle("Create new user");
73 setDescription("Enter information for new user.");
75 this.user
= User
.NewInstance(null, null);
77 setTitle("Edit existing user");
78 setDescription("Edit information for the selected user.");
81 uniqueUserNameValidator
= new UniqueUserNameValidator();
82 passwordValidator
= new PasswordValidator();
87 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
90 public void createControl(Composite parent
) {
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));
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);
105 text_username
.setEnabled(false);
109 createPasswordFieldsNew(composite
);
110 setPageComplete(false);
111 }else if(userIsAuthenticated()){
112 createPasswordFieldsEdit(composite
);
113 setPageComplete(false);
116 group_person
= createPersonGroup(composite
);
117 group_person
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1));
119 setControl(composite
);
129 private boolean userIsAuthenticated() {
130 return user
.equals(CdmStore
.getLoginManager().getAuthenticatedUser());
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);
140 createPasswordFieldsNew(parent
);
143 private void createPasswordFieldsNew(Composite parent
){
144 CLabel label_password
= new CLabel(parent
, SWT
.NULL
);
145 label_password
.setText("Password");
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);
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);
157 private Group
createPersonGroup(Composite parent
){
159 Group group
= new Group(parent
, SWT
.NONE
);
160 GridLayout layout
= new GridLayout();
161 layout
.numColumns
= 2;
162 group
.setLayout(layout
);
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);
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);
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);
189 private void init() {
190 CommonHelper
.editTextWithoutTriggeringListeners(text_username
, this, user
.getUsername());
191 if(user
.getPerson() == null){
192 user
.setPerson(Person
.NewInstance());
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());
201 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
204 public void modifyText(ModifyEvent e
) {
206 user
.setUsername(text_username
.getText().trim());
207 user
.setPassword(text_password
.getText());
209 }else if(userIsAuthenticated()){
210 user
.setPassword(text_password
.getText());
213 user
.getPerson().setFirstname(text_personFirstName
.getText());
214 user
.getPerson().setLastname(text_personLastName
.getText());
215 user
.setEmailAddress(text_userEmail
.getText());
219 * <p>Getter for the field <code>user</code>.</p>
221 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
223 public User
getUser() {
227 private void validate(){
230 if(!userIsAuthenticated()){
231 if((message
= uniqueUserNameValidator
.isValid(text_username
.getText())) != null){
234 }else if((message
= passwordValidator
.isValid(text_password
.getText())) != null){
236 }else if((message
= passwordValidator
.passwordsMatch(text_password
.getText(), text_passwordRepeat
.getText())) != null){
240 setErrorMessage(message
);
243 private class UniqueUserNameValidator
implements IInputValidator
{
245 private static final String USER_EXISTS
= "Username already exists";
246 private static final String NAME_TO_SHORT
= "Username is empty";
249 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
251 public String
isValid(String newText
) {
252 // query for username
253 if(newText
.length() < 1){
254 setPageComplete(false);
255 return NAME_TO_SHORT
;
257 if(CdmStore
.getService(IUserService
.class).userExists(newText
)){
258 setPageComplete(false);
262 setPageComplete(true);
267 private class PasswordValidator
implements IInputValidator
{
269 private static final int PW_MIN_LENGTH
= 5;
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";
275 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
277 public String
isValid(String newText
) {
278 if(newText
.length() < PW_MIN_LENGTH
){
279 setPageComplete(false);
283 setPageComplete(true);
287 public String
passwordsMatch(String password1
, String password2
){
289 if(! password1
.equals(password2
)){
290 setPageComplete(false);
294 setPageComplete(true);