Project

General

Profile

Download (4.38 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.graphics.Color;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Text;
20

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

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

    
39
	private TextWithLabelElement text_oldPassword;
40
	private TextWithLabelElement text_password;
41
	private TextWithLabelElement text_passwordRepeat;
42
	private final PasswordValidator passwordValidator;
43

    
44
	protected PasswordWizardPage(CdmFormFactory formFactory,
45
			ConversationHolder conversation, User entity) {
46
		super(formFactory, conversation, entity);
47

    
48
		passwordValidator = new PasswordValidator();
49

    
50
		setTitle(Messages.PasswordWizardPage_CHANGE_PASSWORD);
51
		setDescription(Messages.PasswordWizardPage_CHANGE_PASSWORD_AND_CONFIRM);
52
	}
53

    
54
	@Override
55
	public void createControl(Composite parent) {
56
		Composite control = formFactory.createComposite(parent);
57
		control.setLayoutData(LayoutConstants.FILL());
58

    
59
		setPageComplete(false);
60

    
61
		control.setLayout(LayoutConstants.LAYOUT(2, false));
62
		WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder());
63

    
64
		if(isChangingOwnPassword()) {
65
		    text_oldPassword = formFactory.createTextWithLabelElement(rootElement, Messages.PasswordWizardPage_OLD_PASSWORD, null, SWT.PASSWORD);
66
		}
67
		text_password =  formFactory.createTextWithLabelElement(rootElement, Messages.PasswordWizardPage_NEW_PASSWORD, null, SWT.PASSWORD);
68
		text_passwordRepeat =  formFactory.createTextWithLabelElement(rootElement, Messages.PasswordWizardPage_REPEAT_PASSWORD, null, SWT.PASSWORD);
69

    
70
		((Text)text_passwordRepeat.getMainControl()).addModifyListener(this);
71

    
72
		Color bgColor = getShell().getBackground();
73
		rootElement.setBackground(bgColor);
74
		control.setBackground(bgColor);
75
		setControl(control);
76
	}
77

    
78
    protected boolean isChangingOwnPassword() {
79
        return getEntity() != null && getEntity().getUsername().equals(CdmStore.getCurrentAuthentiation().getName());
80
    }
81

    
82

    
83
	@Override
84
	public AbstractCdmDetailElement<User> createElement(
85
			ICdmFormElement rootElement) {
86
		// not used
87
		return null;
88
	}
89

    
90
	/** {@inheritDoc} */
91
	@Override
92
    public void modifyText(ModifyEvent e) {
93
		validate();
94
	}
95

    
96
	private void validate(){
97
		String message;
98

    
99
		if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
100
			// pass
101
		}
102
		else if((message = passwordValidator.isValid(text_password.getText())) != null){
103
			// pass
104
		}
105

    
106
		setErrorMessage(message);
107
	}
108

    
109
	private class PasswordValidator implements IInputValidator{
110

    
111
		private final int PW_MIN_LENGTH = 5;
112

    
113
		private final String TO_SHORT = String.format(Messages.PasswordWizardPage_PASSWORD_MIN_CHARACTER, PW_MIN_LENGTH);
114
		private final String NO_MATCH = Messages.PasswordWizardPage_PASSWORDS_DO_NOT_MATCH;
115

    
116
		@Override
117
        public String isValid(String newText) {
118
			if(newText.length() < PW_MIN_LENGTH){
119
				setPageComplete(false);
120
				return TO_SHORT;
121
			}
122

    
123
			setPageComplete(true);
124
			return null;
125
		}
126

    
127
		public String passwordsMatch(String password1, String password2){
128

    
129
			if(! password1.equals(password2)){
130
				setPageComplete(false);
131
				return NO_MATCH;
132
			}
133

    
134
			setPageComplete(true);
135
			return null;
136
		}
137

    
138
	}
139

    
140
	public String getOldPassword() {
141
		return text_oldPassword.getText();
142
	}
143

    
144
	public String getNewPassword() {
145
		return text_password.getText();
146
	}
147
}
(3-3/3)