Project

General

Profile

Download (5.54 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.ui.password;
12

    
13
import org.eclipse.jface.dialogs.IInputValidator;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.ModifyEvent;
16
import org.eclipse.swt.events.ModifyListener;
17
import org.eclipse.swt.widgets.Composite;
18
import org.eclipse.swt.widgets.Text;
19

    
20
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
21
import eu.etaxonomy.cdm.api.service.IUserService;
22
import eu.etaxonomy.cdm.model.common.User;
23
import eu.etaxonomy.taxeditor.store.CdmStore;
24
import eu.etaxonomy.taxeditor.ui.element.AbstractCdmEntityWizardPage;
25
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
27
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
28
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
29
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
30

    
31
/**
32
 * <p>UserWizardPage class.</p>
33
 *
34
 * @author n.hoffmann
35
 * @created 02.07.2009
36
 * @version 1.0
37
 */
38
public class PasswordWizardPage extends AbstractCdmEntityWizardPage<User> implements ModifyListener{
39

    
40
	/** Constant <code>NAME="USER_WIZARD_PAGE"</code> */
41
	public static final String NAME = "USER_WIZARD_PAGE";
42

    
43
	private TextWithLabelElement text_oldPassword;
44
	private TextWithLabelElement text_password;
45
	private TextWithLabelElement text_passwordRepeat;
46
	private final PasswordValidator passwordValidator;
47

    
48
	private final UniqueUserNameValidator uniqueUsernameValidator;
49

    
50
	/**
51
	 * @param formFactory
52
	 * @param conversation
53
	 * @param entity
54
	 */
55
	protected PasswordWizardPage(CdmFormFactory formFactory,
56
			ConversationHolder conversation, User entity) {
57
		super(formFactory, conversation, entity);
58

    
59
		passwordValidator = new PasswordValidator();
60
		uniqueUsernameValidator = new UniqueUserNameValidator();
61
	}
62

    
63
	/* (non-Javadoc)
64
	 * @see eu.etaxonomy.taxeditor.ui.forms.AbstractCdmEntityWizardPage#createControl(org.eclipse.swt.widgets.Composite)
65
	 */
66
	@Override
67
	public void createControl(Composite parent) {
68
		Composite control = formFactory.createComposite(parent);
69
		control.setLayoutData(LayoutConstants.FILL());
70

    
71
		setPageComplete(false);
72

    
73
		control.setLayout(LayoutConstants.LAYOUT(2, false));
74
		WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder());
75

    
76
		if(isChangingOwnPassword()) {
77
		    text_oldPassword = formFactory.createTextWithLabelElement(rootElement, "Old Password", null, SWT.PASSWORD);
78
		}
79
		text_password =  formFactory.createTextWithLabelElement(rootElement, "New Password", null, SWT.PASSWORD);
80
		text_passwordRepeat =  formFactory.createTextWithLabelElement(rootElement, "Repeat Password", null, SWT.PASSWORD);
81

    
82
		((Text)text_passwordRepeat.getMainControl()).addModifyListener(this);
83

    
84
		setControl(control);
85
	}
86

    
87
    protected boolean isChangingOwnPassword() {
88
        return getEntity() != null && getEntity().getUsername().equals(CdmStore.getCurrentAuthentiation().getName());
89
    }
90

    
91

    
92
	/* (non-Javadoc)
93
	 * @see eu.etaxonomy.taxeditor.ui.forms.AbstractCdmEntityWizardPage#createElement(eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement)
94
	 */
95
	@Override
96
	public AbstractCdmDetailElement<User> createElement(
97
			ICdmFormElement rootElement) {
98
		// not used
99
		return null;
100
	}
101

    
102
	/* (non-Javadoc)
103
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
104
	 */
105
	/** {@inheritDoc} */
106
	@Override
107
    public void modifyText(ModifyEvent e) {
108
		validate();
109
	}
110

    
111
	private void validate(){
112
		String message;
113

    
114
		if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
115
			// pass
116
		}
117
		else if((message = passwordValidator.isValid(text_password.getText())) != null){
118
			// pass
119
		}
120

    
121
		setErrorMessage(message);
122
	}
123

    
124
	private class UniqueUserNameValidator implements IInputValidator{
125

    
126
		private static final String USER_EXISTS = "Username already exists";
127
		private static final String NAME_TO_SHORT = "Username is empty";
128

    
129
		/* (non-Javadoc)
130
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
131
		 */
132
		@Override
133
        public String isValid(String newText) {
134
			// query for username
135
			if(newText.length() < 1){
136
				setPageComplete(false);
137
				return NAME_TO_SHORT;
138
			}
139
			if(CdmStore.getService(IUserService.class).userExists(newText)){
140
				setPageComplete(false);
141
				return USER_EXISTS;
142
			}
143

    
144
			setPageComplete(true);
145
			return null;
146
		}
147
	}
148

    
149
	private class PasswordValidator implements IInputValidator{
150

    
151
		private static final int PW_MIN_LENGTH = 5;
152

    
153
		private static final String TO_SHORT = "Password has to have at least " + PW_MIN_LENGTH + " characters";
154
		private static final String NO_MATCH = "The passwords do not match";
155

    
156
		/* (non-Javadoc)
157
		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
158
		 */
159
		@Override
160
        public String isValid(String newText) {
161
			if(newText.length() < PW_MIN_LENGTH){
162
				setPageComplete(false);
163
				return TO_SHORT;
164
			}
165

    
166
			setPageComplete(true);
167
			return null;
168
		}
169

    
170
		public String passwordsMatch(String password1, String password2){
171

    
172
			if(! password1.equals(password2)){
173
				setPageComplete(false);
174
				return NO_MATCH;
175
			}
176

    
177
			setPageComplete(true);
178
			return null;
179
		}
180

    
181
	}
182

    
183
	/**
184
	 * @return
185
	 */
186
	public String getOldPassword() {
187
		return text_oldPassword.getText();
188
	}
189

    
190
	/**
191
	 * @return
192
	 */
193
	public String getNewPassword() {
194
		return text_password.getText();
195
	}
196
}
(3-3/3)