(no commit message)
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / 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.user.wizard;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.jface.dialogs.IInputValidator;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.CLabel;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Group;
24 import org.eclipse.swt.widgets.Text;
25
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.model.Resources;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31
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 public static final String NAME = "USER_WIZARD_PAGE";
40
41
42 private Composite composite;
43 private Text text_username;
44 private Text text_oldPassword;
45 private Text text_password;
46 private Text text_passwordRepeat;
47 private User user;
48 private boolean newMode = false;
49 private Group group_person;
50 private Text text_personFirstName;
51 private Text text_personLastName;
52 private Text text_userEmail;
53
54 private IInputValidator uniqueUserNameValidator;
55 private IInputValidator passwordRepetitionEqualityValidator;
56
57 /**
58 * @param pageName
59 */
60 protected UserWizardPage(User user) {
61 super(NAME);
62
63 this.user = user;
64
65 if(user == null){
66 setTitle("Create new user");
67 setDescription("Enter information for new user.");
68 newMode = true;
69 this.user = User.NewInstance(null, null);
70 }else{
71 setTitle("Edit existing user");
72 setDescription("Edit information for the selected user.");
73 }
74
75 uniqueUserNameValidator = new UniqueUserNameValidator();
76 passwordRepetitionEqualityValidator = new PasswordRepetitionEqualityValidator();
77 }
78
79 private static final Logger logger = Logger.getLogger(UserWizardPage.class);
80
81 /* (non-Javadoc)
82 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
83 */
84 public void createControl(Composite parent) {
85 // right - edit Form
86 composite = new Composite(parent, SWT.NULL);
87 GridLayout compositeLayout = new GridLayout();
88 compositeLayout.numColumns = 2;
89 composite.setLayout(compositeLayout);
90 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91
92 CLabel label_username = new CLabel(composite, SWT.NULL);
93 label_username.setText("Username");
94 text_username = new Text(composite, SWT.BORDER);
95 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
96 text_username.addModifyListener(this);
97
98 if(! newMode){
99 text_username.setEditable(false);
100 text_username.setBackground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
101 }
102
103 if(newMode){
104 createPasswordFieldsNew(composite);
105 }else if(CdmStore.getLoginManager().getAuthenticatedUser().equals(user)){
106 createPasswordFieldsEdit(composite);
107 }
108
109 group_person = createPersonGroup(composite);
110 group_person.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
111
112 setControl(composite);
113
114 setPageComplete(false);
115
116 init();
117 }
118
119 private void createPasswordFieldsEdit(Composite parent){
120 CLabel label_oldPassword = new CLabel(parent, SWT.NULL);
121 label_oldPassword.setText("Old Password");
122 text_oldPassword = new Text(parent, SWT.PASSWORD | SWT.BORDER);
123 text_oldPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
124 text_oldPassword.addModifyListener(this);
125
126 createPasswordFieldsNew(parent);
127 }
128
129 private void createPasswordFieldsNew(Composite parent){
130 CLabel label_password = new CLabel(parent, SWT.NULL);
131 label_password.setText("Password");
132
133 text_password = new Text(parent, SWT.PASSWORD | SWT.BORDER);
134 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
135 text_password.addModifyListener(this);
136
137 new CLabel(parent, SWT.NULL);
138 text_passwordRepeat = new Text(parent, SWT.BORDER);
139 text_passwordRepeat.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
140 text_passwordRepeat.setEchoChar('*');
141 text_passwordRepeat.addModifyListener(this);
142 }
143
144 private Group createPersonGroup(Composite parent){
145
146 Group group = new Group(parent, SWT.NONE);
147 GridLayout layout = new GridLayout();
148 layout.numColumns = 2;
149 group.setLayout(layout);
150
151 CLabel label_personFirstName = new CLabel(group, SWT.NULL);
152 label_personFirstName.setText("First Name");
153 text_personFirstName = new Text(group, SWT.BORDER);
154 text_personFirstName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
155 text_personFirstName.addModifyListener(this);
156
157 CLabel label_personLastName = new CLabel(group, SWT.NULL);
158 label_personLastName.setText("Last Name");
159 text_personLastName = new Text(group, SWT.BORDER);
160 text_personLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
161 text_personLastName.addModifyListener(this);
162
163
164 CLabel label_userEmail = new CLabel(group, SWT.NULL);
165 label_userEmail.setText("Email");
166 text_userEmail = new Text(group, SWT.BORDER);
167 text_userEmail.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
168 text_userEmail.addModifyListener(this);
169
170 return group;
171 }
172
173 /**
174 *
175 */
176 private void init() {
177 CommonHelper.editTextWithoutTriggeringListeners(text_username, this, user.getUsername());
178 if(user.getPerson() == null){
179 user.setPerson(Person.NewInstance());
180 }else{
181 CommonHelper.editTextWithoutTriggeringListeners(text_personFirstName, this, user.getPerson().getFirstname());
182 CommonHelper.editTextWithoutTriggeringListeners(text_personLastName, this, user.getPerson().getLastname());
183 CommonHelper.editTextWithoutTriggeringListeners(text_userEmail, this, user.getEmailAddress());
184 }
185 }
186
187 /* (non-Javadoc)
188 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
189 */
190 public void modifyText(ModifyEvent e) {
191 if(newMode){
192 user.setUsername(text_username.getText().trim());
193 user.setPassword(text_password.getText());
194 }
195 user.getPerson().setFirstname(text_personFirstName.getText());
196 user.getPerson().setLastname(text_personLastName.getText());
197 user.setEmailAddress(text_userEmail.getText());
198 }
199
200 /**
201 * @return
202 */
203 public User getUser() {
204 return user;
205 }
206
207 private class UniqueUserNameValidator implements IInputValidator{
208
209 private static final String MESSAGE = "Username already exists";
210
211 /* (non-Javadoc)
212 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
213 */
214 public String isValid(String newText) {
215 // query for username
216 if( CdmStore.getUserService().loadUserByUsername(newText) != null){
217 return MESSAGE;
218 }
219
220 return null;
221 }
222 }
223
224 private class PasswordRepetitionEqualityValidator implements IInputValidator{
225
226 /* (non-Javadoc)
227 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
228 */
229 public String isValid(String newText) {
230 // TODO Auto-generated method stub
231 return null;
232 }
233
234 }
235 }