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.gwt.client.ui;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.Messages;
032import org.opencms.gwt.client.rpc.CmsRpcAction;
033import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
034import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
035import org.opencms.gwt.client.ui.input.CmsTextBox;
036import org.opencms.gwt.client.util.CmsMessages;
037
038import com.google.gwt.core.client.GWT;
039import com.google.gwt.event.dom.client.ClickEvent;
040import com.google.gwt.event.dom.client.ClickHandler;
041import com.google.gwt.uibinder.client.UiBinder;
042import com.google.gwt.uibinder.client.UiField;
043import com.google.gwt.user.client.ui.Composite;
044import com.google.gwt.user.client.ui.Label;
045import com.google.gwt.user.client.ui.PasswordTextBox;
046import com.google.gwt.user.client.ui.Widget;
047
048/**
049 * Widget used to change the current user's password.<p>
050 */
051public class CmsChangePasswordWidget extends Composite {
052
053    /** The uiBinder interface for this widget. */
054    interface I_CmsPasswordWidgetUiBinder extends UiBinder<Widget, CmsChangePasswordWidget> {
055        //empty
056    }
057
058    /** Constant for the width of the text box labels. */
059    public static final String LABEL_WIDTH = "200px";
060
061    /** The uiBinder instance for this widget. */
062    private static I_CmsPasswordWidgetUiBinder uiBinder = GWT.create(I_CmsPasswordWidgetUiBinder.class);
063
064    /** Label for displaying error messages. */
065    @UiField
066    protected Label m_errorLabel;
067
068    /** Action to execute if the password was successfully changed. */
069    protected Runnable m_finishAction;
070
071    /** Input field to the new password. */
072    @UiField(provided = true)
073    protected CmsTextBox m_newPassword1 = new CmsTextBox(new PasswordTextBox());
074
075    /** Input field for the new password confirmation. */
076    @UiField(provided = true)
077    protected CmsTextBox m_newPassword2 = new CmsTextBox(new PasswordTextBox());
078
079    /** Label for the new password confirmation. */
080    @UiField(provided = true)
081    protected Label m_newPasswordConfirmLabel;
082
083    /** Label for the new password. */
084    @UiField(provided = true)
085    protected Label m_newPasswordLabel;
086
087    /** Input field for the old password. */
088    @UiField(provided = true)
089    protected CmsTextBox m_oldPassword = new CmsTextBox(new PasswordTextBox());
090
091    /** Label for the old password. */
092    @UiField(provided = true)
093    protected Label m_oldPasswordLabel;
094
095    /**
096     * Creates a new instance.<p>
097     *
098     * @param finishAction the action to execute when the password is successfully changed
099     */
100    public CmsChangePasswordWidget(Runnable finishAction) {
101
102        CmsMessages m = Messages.get();
103        m_oldPasswordLabel = new Label(m.key(Messages.GUI_PASSWORD_OLD_0));
104        m_newPasswordLabel = new Label(m.key(Messages.GUI_PASSWORD_NEW_0));
105        m_newPasswordConfirmLabel = new Label(m.key(Messages.GUI_PASSWORD_CONFIRM_0));
106        initWidget(uiBinder.createAndBindUi(this));
107        addStyleName(CmsTextBox.CSS.highTextBoxes());
108        m_finishAction = finishAction;
109    }
110
111    /**
112     * Opens a popup dialog for changing the password.<p>
113     */
114    public static void showDialog() {
115
116        final CmsPopup popup = new CmsPopup(
117            Messages.get().key(Messages.GUI_PASSWORD_CHANGE_TITLE_1, CmsCoreProvider.get().getUserInfo().getName()));
118        popup.setModal(true);
119        popup.setGlassEnabled(true);
120        CmsPushButton okButton = new CmsPushButton();
121        okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
122        okButton.setText(Messages.get().key(Messages.GUI_OK_0));
123        okButton.setUseMinWidth(true);
124        CmsPushButton cancelButton = new CmsPushButton();
125        cancelButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
126        cancelButton.setText(Messages.get().key(Messages.GUI_CANCEL_0));
127        cancelButton.setUseMinWidth(true);
128        popup.addButton(cancelButton);
129        popup.addButton(okButton);
130
131        Runnable finishAction = new Runnable() {
132
133            public void run() {
134
135                popup.hide();
136            }
137        };
138        final CmsChangePasswordWidget passwordWidget = new CmsChangePasswordWidget(finishAction);
139        okButton.addClickHandler(new ClickHandler() {
140
141            public void onClick(ClickEvent e) {
142
143                passwordWidget.changePassword();
144            }
145        });
146
147        cancelButton.addClickHandler(new ClickHandler() {
148
149            public void onClick(ClickEvent e) {
150
151                popup.hide();
152            }
153        });
154        popup.setMainContent(passwordWidget);
155        popup.center();
156
157    }
158
159    /**
160     * Tries to change the password using the data entered by the user.<p>
161     */
162    public void changePassword() {
163
164        final String oldPassword = m_oldPassword.getFormValueAsString();
165        final String newPassword1 = m_newPassword1.getFormValueAsString();
166        final String newPassword2 = m_newPassword2.getFormValueAsString();
167
168        CmsRpcAction<String> action = new CmsRpcAction<String>() {
169
170            @Override
171            public void execute() {
172
173                start(0, true);
174                CmsCoreProvider.getService().changePassword(oldPassword, newPassword1, newPassword2, this);
175            }
176
177            @Override
178            public void onResponse(String result) {
179
180                stop(false);
181                if (result == null) {
182                    m_finishAction.run();
183                } else {
184                    showError(result);
185                }
186            }
187        };
188        action.execute();
189    }
190
191    /**
192     * Displays an error message.<p>
193     *
194     * @param error the error message to display
195     */
196    protected void showError(String error) {
197
198        if (null == error) {
199            m_errorLabel.setText("");
200        } else {
201            m_errorLabel.setText(error);
202        }
203    }
204
205}