fixes #805 and started to work on #835
[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.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.model.Resources;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30
31 /**
32 * @author n.hoffmann
33 * @created 02.07.2009
34 * @version 1.0
35 */
36 public class UserWizardPage extends WizardPage implements ModifyListener{
37 private Composite composite;
38 private Text text_username;
39 private Text text_oldPassword;
40 private Text text_password;
41 private Text text_passwordRepeat;
42 private User user;
43 private boolean newMode = false;
44 private Group group_person;
45 private Text text_personFirstName;
46 private Text text_personLastName;
47 private Text text_userEmail;
48
49 /**
50 * @param pageName
51 */
52 protected UserWizardPage(User user) {
53 super("USER_WIZARD_PAGE");
54
55 this.user = user;
56
57 if(user == null){
58 setTitle("Create new user");
59 setDescription("Enter information for new user.");
60 newMode = true;
61 this.user = User.NewInstance(null, null);
62 }else{
63 setTitle("Edit existing user");
64 setDescription("Edit information for the selected user.");
65
66 }
67 }
68
69 private static final Logger logger = Logger.getLogger(UserWizardPage.class);
70
71 /* (non-Javadoc)
72 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
73 */
74 public void createControl(Composite parent) {
75 // right - edit Form
76 composite = new Composite(parent, SWT.NULL);
77 GridLayout compositeLayout = new GridLayout();
78 compositeLayout.numColumns = 2;
79 composite.setLayout(compositeLayout);
80 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
81
82 CLabel label_username = new CLabel(composite, SWT.NULL);
83 label_username.setText("Username");
84 text_username = new Text(composite, SWT.BORDER);
85 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
86 text_username.addModifyListener(this);
87
88 if(! newMode){
89 text_username.setEditable(false);
90 text_username.setBackground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
91 }
92
93 if(newMode){
94 createPasswordFieldsNew(composite);
95 }else if(CdmStore.getLoginManager().getAuthenticatedUser().equals(user)){
96 createPasswordFieldsEdit(composite);
97 }
98
99 group_person = createPersonGroup(composite);
100 group_person.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
101
102 setControl(composite);
103
104 init();
105 }
106
107 private void createPasswordFieldsEdit(Composite parent){
108 CLabel label_oldPassword = new CLabel(parent, SWT.NULL);
109 label_oldPassword.setText("Old Password");
110 text_oldPassword = new Text(parent, SWT.BORDER);
111 text_oldPassword.setEchoChar('*');
112 text_oldPassword.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
113 text_oldPassword.addModifyListener(this);
114
115 createPasswordFieldsNew(parent);
116 }
117
118 private void createPasswordFieldsNew(Composite parent){
119 CLabel label_password = new CLabel(parent, SWT.NULL);
120 label_password.setText("Password");
121
122 text_password = new Text(parent, SWT.BORDER);
123 text_password.setEchoChar('*');
124 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
125 text_password.addModifyListener(this);
126
127 new CLabel(parent, SWT.NULL);
128 text_passwordRepeat = new Text(parent, SWT.BORDER);
129 text_passwordRepeat.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
130 text_passwordRepeat.setEchoChar('*');
131 text_passwordRepeat.addModifyListener(this);
132 }
133
134 private Group createPersonGroup(Composite parent){
135
136 Group group = new Group(parent, SWT.NONE);
137 GridLayout layout = new GridLayout();
138 layout.numColumns = 2;
139 group.setLayout(layout);
140
141 CLabel label_personFirstName = new CLabel(group, SWT.NULL);
142 label_personFirstName.setText("First Name");
143 text_personFirstName = new Text(group, SWT.BORDER);
144 text_personFirstName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
145 text_personFirstName.addModifyListener(this);
146
147 CLabel label_personLastName = new CLabel(group, SWT.NULL);
148 label_personLastName.setText("Last Name");
149 text_personLastName = new Text(group, SWT.BORDER);
150 text_personLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
151 text_personLastName.addModifyListener(this);
152
153
154 CLabel label_userEmail = new CLabel(group, SWT.NULL);
155 label_userEmail.setText("Email");
156 text_userEmail = new Text(group, SWT.BORDER);
157 text_userEmail.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
158 text_userEmail.addModifyListener(this);
159
160 return group;
161 }
162
163 /**
164 *
165 */
166 private void init() {
167 CommonHelper.editTextWithoutTriggeringListeners(text_username, this, user.getUsername());
168 if(user.getPerson() == null){
169 user.setPerson(Person.NewInstance());
170 }else{
171 CommonHelper.editTextWithoutTriggeringListeners(text_personFirstName, this, user.getPerson().getFirstname());
172 CommonHelper.editTextWithoutTriggeringListeners(text_personLastName, this, user.getPerson().getLastname());
173 CommonHelper.editTextWithoutTriggeringListeners(text_userEmail, this, user.getEmailAddress());
174 }
175 }
176
177 /* (non-Javadoc)
178 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
179 */
180 public void modifyText(ModifyEvent e) {
181 if(newMode){
182 user.setUsername(text_username.getText().trim());
183 user.setPassword(text_password.getText());
184 }
185 user.getPerson().setFirstname(text_personFirstName.getText());
186 user.getPerson().setLastname(text_personLastName.getText());
187 user.setEmailAddress(text_userEmail.getText());
188 }
189
190 /**
191 * @return
192 */
193 public User getUser() {
194 return user;
195 }
196 }