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
.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
.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
;
31 * <p>UserWizardPage class.</p>
37 public class UserWizardPage
extends WizardPage
implements ModifyListener
{
39 // private static final Logger logger = Logger.getLogger(UserWizardPage.class);
41 /** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
42 public static final String NAME
= "USER_WIZARD_PAGE";
45 private Composite composite
;
46 private Text text_username
;
47 private Text text_oldPassword
;
48 private Text text_password
;
49 private Text text_passwordRepeat
;
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
;
57 private IInputValidator uniqueUserNameValidator
;
58 private PasswordValidator passwordValidator
;
61 * <p>Constructor for UserWizardPage.</p>
63 * @param user a {@link eu.etaxonomy.cdm.model.common.User} object.
65 protected UserWizardPage(User user
) {
71 setTitle("Create new user");
72 setDescription("Enter information for new user.");
74 this.user
= User
.NewInstance(null, null);
76 setTitle("Edit existing user");
77 setDescription("Edit information for the selected user.");
80 uniqueUserNameValidator
= new UniqueUserNameValidator();
81 passwordValidator
= new PasswordValidator();
86 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
89 public void createControl(Composite parent
) {
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));
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);
104 text_username
.setEnabled(false);
108 createPasswordFieldsNew(composite
);
109 setPageComplete(false);
110 }else if(userIsAuthenticated()){
111 createPasswordFieldsEdit(composite
);
112 setPageComplete(false);
115 group_person
= createPersonGroup(composite
);
116 group_person
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1));
118 setControl(composite
);
128 private boolean userIsAuthenticated() {
129 return user
.equals(CdmStore
.getLoginManager().getAuthenticatedUser());
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);
139 createPasswordFieldsNew(parent
);
142 private void createPasswordFieldsNew(Composite parent
){
143 CLabel label_password
= new CLabel(parent
, SWT
.NULL
);
144 label_password
.setText("Password");
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);
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);
156 private Group
createPersonGroup(Composite parent
){
158 Group group
= new Group(parent
, SWT
.NONE
);
159 GridLayout layout
= new GridLayout();
160 layout
.numColumns
= 2;
161 group
.setLayout(layout
);
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);
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);
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);
188 private void init() {
189 CommonHelper
.editTextWithoutTriggeringListeners(text_username
, this, user
.getUsername());
190 if(user
.getPerson() == null){
191 user
.setPerson(Person
.NewInstance());
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());
200 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
203 public void modifyText(ModifyEvent e
) {
205 user
.setUsername(text_username
.getText().trim());
206 user
.setPassword(text_password
.getText());
208 }else if(userIsAuthenticated()){
209 user
.setPassword(text_password
.getText());
212 user
.getPerson().setFirstname(text_personFirstName
.getText());
213 user
.getPerson().setLastname(text_personLastName
.getText());
214 user
.setEmailAddress(text_userEmail
.getText());
218 * <p>Getter for the field <code>user</code>.</p>
220 * @return a {@link eu.etaxonomy.cdm.model.common.User} object.
222 public User
getUser() {
226 private void validate(){
229 if(!userIsAuthenticated()){
230 if((message
= uniqueUserNameValidator
.isValid(text_username
.getText())) != null){
233 }else if((message
= passwordValidator
.isValid(text_password
.getText())) != null){
235 }else if((message
= passwordValidator
.passwordsMatch(text_password
.getText(), text_passwordRepeat
.getText())) != null){
239 setErrorMessage(message
);
242 private class UniqueUserNameValidator
implements IInputValidator
{
244 private static final String USER_EXISTS
= "Username already exists";
245 private static final String NAME_TO_SHORT
= "Username is empty";
248 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
250 public String
isValid(String newText
) {
251 // query for username
252 if(newText
.length() < 1){
253 setPageComplete(false);
254 return NAME_TO_SHORT
;
256 if(CdmStore
.getUserService().userExists(newText
)){
257 setPageComplete(false);
261 setPageComplete(true);
266 private class PasswordValidator
implements IInputValidator
{
268 private static final int PW_MIN_LENGTH
= 5;
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";
274 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
276 public String
isValid(String newText
) {
277 if(newText
.length() < PW_MIN_LENGTH
){
278 setPageComplete(false);
282 setPageComplete(true);
286 public String
passwordsMatch(String password1
, String password2
){
288 if(! password1
.equals(password2
)){
289 setPageComplete(false);
293 setPageComplete(true);