Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / password / PasswordWizardPage.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.password;
11
12 import org.eclipse.jface.dialogs.IInputValidator;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.swt.graphics.Color;
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.model.common.User;
22 import eu.etaxonomy.taxeditor.l10n.Messages;
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 * @author n.hoffmann
33 * @created 02.07.2009
34 * @version 1.0
35 */
36 public class PasswordWizardPage extends AbstractCdmEntityWizardPage<User> implements ModifyListener{
37
38 private TextWithLabelElement text_oldPassword;
39 private TextWithLabelElement text_password;
40 private TextWithLabelElement text_passwordRepeat;
41 private final PasswordValidator passwordValidator;
42
43 protected PasswordWizardPage(CdmFormFactory formFactory,
44 ConversationHolder conversation, User entity) {
45 super(formFactory, conversation, entity);
46
47 passwordValidator = new PasswordValidator();
48
49 setTitle(Messages.PasswordWizardPage_CHANGE_PASSWORD);
50 setDescription(Messages.PasswordWizardPage_CHANGE_PASSWORD_AND_CONFIRM);
51 }
52
53 @Override
54 public void createControl(Composite parent) {
55 Composite control = formFactory.createComposite(parent);
56 control.setLayoutData(LayoutConstants.FILL());
57
58 setPageComplete(false);
59
60 control.setLayout(LayoutConstants.LAYOUT(2, false));
61 WizardPageRootElement rootElement = new WizardPageRootElement(formFactory, control, getConversationHolder());
62
63 if(isChangingOwnPassword()) {
64 text_oldPassword = formFactory.createTextWithLabelElement(rootElement, Messages.PasswordWizardPage_OLD_PASSWORD, null, SWT.PASSWORD);
65 }
66 text_password = formFactory.createTextWithLabelElement(rootElement, Messages.PasswordWizardPage_NEW_PASSWORD, null, SWT.PASSWORD);
67 text_passwordRepeat = formFactory.createTextWithLabelElement(rootElement, Messages.PasswordWizardPage_REPEAT_PASSWORD, null, SWT.PASSWORD);
68
69 ((Text)text_passwordRepeat.getMainControl()).addModifyListener(this);
70
71 Color bgColor = getShell().getBackground();
72 rootElement.setBackground(bgColor);
73 control.setBackground(bgColor);
74 setControl(control);
75 }
76
77 protected boolean isChangingOwnPassword() {
78 return getEntity() != null && getEntity().getUsername().equals(CdmStore.getCurrentAuthentiation().getName());
79 }
80
81
82 @Override
83 public AbstractCdmDetailElement<User> createElement(
84 ICdmFormElement rootElement) {
85 // not used
86 return null;
87 }
88
89 /** {@inheritDoc} */
90 @Override
91 public void modifyText(ModifyEvent e) {
92 validate();
93 }
94
95 private void validate(){
96 String message;
97
98 if((message = passwordValidator.passwordsMatch(text_password.getText(), text_passwordRepeat.getText())) != null){
99 // pass
100 }
101 else if((message = passwordValidator.isValid(text_password.getText())) != null){
102 // pass
103 }
104
105 setErrorMessage(message);
106 }
107
108 private class PasswordValidator implements IInputValidator{
109
110 private final int PW_MIN_LENGTH = 5;
111
112 private final String TO_SHORT = String.format(Messages.PasswordWizardPage_PASSWORD_MIN_CHARACTER, PW_MIN_LENGTH);
113 private final String NO_MATCH = Messages.PasswordWizardPage_PASSWORDS_DO_NOT_MATCH;
114
115 @Override
116 public String isValid(String newText) {
117 if(newText.length() < PW_MIN_LENGTH){
118 setPageComplete(false);
119 return TO_SHORT;
120 }
121
122 setPageComplete(true);
123 return null;
124 }
125
126 public String passwordsMatch(String password1, String password2){
127
128 if(! password1.equals(password2)){
129 setPageComplete(false);
130 return NO_MATCH;
131 }
132
133 setPageComplete(true);
134 return null;
135 }
136
137 }
138
139 public String getOldPassword() {
140 return text_oldPassword.getText();
141 }
142
143 public String getNewPassword() {
144 return text_password.getText();
145 }
146 }