001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.login;
029
030import org.opencms.main.OpenCms;
031import org.opencms.ui.A_CmsUI;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.util.CmsStyleVariable;
034import org.opencms.util.CmsStringUtil;
035
036import java.util.Locale;
037
038import com.vaadin.server.UserError;
039import com.vaadin.ui.CssLayout;
040import com.vaadin.v7.ui.Label;
041import com.vaadin.v7.ui.PasswordField;
042import com.vaadin.v7.ui.VerticalLayout;
043
044/**
045 * The change password form.<p>
046 */
047public class CmsPasswordForm extends VerticalLayout {
048
049    /** The serial version id. */
050    private static final long serialVersionUID = 773244283339376105L;
051
052    /** Label with additional text. */
053    private Label m_additionalText;
054
055    /**Label for the head message.*/
056    private Label m_headMessage;
057
058    /** Field for the old password. */
059    private PasswordField m_oldPasswordField;
060
061    /** Old password style variable. */
062    private CmsStyleVariable m_oldPasswordStyle;
063
064    /** Layout wrapping the old password field. */
065    private CssLayout m_oldPasswordWrapper;
066
067    /** Password 1 style variable. */
068    private CmsStyleVariable m_password1Style;
069
070    /** Layout wrapping the password 1 field. */
071    private CssLayout m_password1Wrapper;
072
073    /** Password 2 style variable. */
074    private CmsStyleVariable m_password2Style;
075
076    /** Layout wrapping the password 2 field. */
077    private CssLayout m_password2Wrapper;
078
079    /** First password field. */
080    private PasswordField m_passwordField1;
081
082    /** Second password field. */
083    private PasswordField m_passwordField2;
084
085    /** Label to display the security hint. */
086    private Label m_securityHint;
087
088    /**
089     * Constructor.<p>
090     */
091    public CmsPasswordForm() {
092
093        this(A_CmsUI.get().getLocale());
094    }
095
096    /**
097     * Constructor.<p>
098     *
099     * @param locale the user locale
100     */
101    public CmsPasswordForm(Locale locale) {
102
103        CmsVaadinUtils.readAndLocalizeDesign(this, OpenCms.getWorkplaceManager().getMessages(locale), null);
104        m_securityHint.setVisible(false);
105        m_password1Style = new CmsStyleVariable(m_password1Wrapper);
106        m_password2Style = new CmsStyleVariable(m_password2Wrapper);
107        m_oldPasswordStyle = new CmsStyleVariable(m_oldPasswordWrapper);
108    }
109
110    /**
111     * Returns the old password value.<p>
112     *
113     * @return the old password
114     */
115    public String getOldPassword() {
116
117        return m_oldPasswordField.getValue();
118    }
119
120    /**
121     * Returns the old password field.<p>
122     *
123     * @return the old password field
124     */
125    public PasswordField getOldPasswordField() {
126
127        return m_oldPasswordField;
128    }
129
130    /**
131     * Returns the password 1 value.<p>
132     *
133     * @return the password 1
134     */
135    public String getPassword1() {
136
137        return m_passwordField1.getValue();
138    }
139
140    /**
141     * Returns the password 1 field.<p>
142     *
143     * @return the password 1 field
144     */
145    public PasswordField getPassword1Field() {
146
147        return m_passwordField1;
148    }
149
150    /**
151     * Returns the password 2 value.<p>
152     *
153     * @return the password 2
154     */
155    public String getPassword2() {
156
157        return m_passwordField2.getValue();
158    }
159
160    /**
161     * Returns the password 2 field.<p>
162     *
163     * @return the password 2 field
164     */
165    public PasswordField getPassword2Field() {
166
167        return m_passwordField2;
168    }
169
170    /**
171     * Hides the old password field.
172     */
173    public void hideOldPassword() {
174
175        m_oldPasswordWrapper.setVisible(false);
176    }
177
178    /**
179     * Displays an additional message.<p>
180     *
181     * @param text the message
182     */
183    public void setAdditionalText(String text) {
184
185        if (CmsStringUtil.isEmpty(text)) {
186            m_additionalText.setVisible(false);
187        } else {
188            m_additionalText.setValue(text);
189            m_additionalText.setVisible(true);
190        }
191    }
192
193    public void setEnabled(boolean enabled) {
194
195        m_passwordField1.setEnabled(enabled);
196        m_passwordField2.setEnabled(enabled);
197        m_oldPasswordField.setEnabled(enabled);
198    }
199
200    /**
201     * Sets the old password error.<p>
202     *
203     * @param error the error
204     * @param style the style class
205     */
206    public void setErrorOldPassword(UserError error, String style) {
207
208        m_oldPasswordField.setComponentError(error);
209        m_oldPasswordStyle.setStyle(style);
210    }
211
212    /**
213     * Sets the password 1 error.<p>
214     *
215     * @param error the error
216     * @param style the style class
217     */
218    public void setErrorPassword1(UserError error, String style) {
219
220        m_passwordField1.setComponentError(error);
221        m_password1Style.setStyle(style);
222    }
223
224    /**
225     * Sets the password 2 error.<p>
226     *
227     * @param error the error
228     * @param style the style class
229     */
230    public void setErrorPassword2(UserError error, String style) {
231
232        m_passwordField2.setComponentError(error);
233        m_password2Style.setStyle(style);
234    }
235
236    /**
237     * Sets the visibility of the head message.<p>
238     *
239     * @param visible true -> head is visible, false not
240     */
241    public void setHeaderVisible(boolean visible) {
242
243        m_headMessage.setVisible(false);
244    }
245
246    /**
247     * Sets the security hint.<p>
248     *
249     * @param hint the hint to display
250     */
251    public void setSecurityHint(String hint) {
252
253        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(hint)) {
254            m_securityHint.setValue(hint);
255            m_securityHint.setVisible(true);
256        } else {
257            m_securityHint.setVisible(false);
258        }
259    }
260}