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.apps.user;
029
030import org.opencms.main.OpenCms;
031import org.opencms.security.CmsDefaultPasswordGenerator;
032import org.opencms.security.I_CmsPasswordGenerator;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.apps.Messages;
035import org.opencms.ui.components.CmsBasicDialog;
036import org.opencms.ui.components.CmsButtonFormRow;
037import org.opencms.ui.components.OpenCmsTheme;
038
039import com.vaadin.icons.VaadinIcons;
040import com.vaadin.ui.Button;
041import com.vaadin.ui.Button.ClickEvent;
042import com.vaadin.ui.FormLayout;
043import com.vaadin.v7.ui.Label;
044import com.vaadin.v7.ui.TextField;
045
046/**
047 * Dialog to generate a random password.<p>
048 */
049public class CmsGeneratePasswordDialog extends CmsBasicDialog {
050
051    /**Vaadin serial id. */
052    private static final long serialVersionUID = -7522845215366141986L;
053
054    /**
055     * public constructor.<p>
056     *
057     * @param passwordFetcher fetcher to send result to
058     * @param close runnable
059     */
060    public CmsGeneratePasswordDialog(final I_CmsPasswordFetcher passwordFetcher, final Runnable close) {
061
062        setWidth("500px");
063        FormLayout layout = new FormLayout();
064        layout.setWidth("100%");
065        layout.addStyleName(OpenCmsTheme.FORMLAYOUT_WORKPLACE_MAIN);
066        layout.setMargin(true);
067        layout.setSpacing(true);
068        //        layout.setWidth("400px");
069        final TextField passwordField = new TextField();
070        passwordField.setValue(getRandomPassword());
071        CmsButtonFormRow<TextField> row = new CmsButtonFormRow<TextField>(
072            passwordField,
073            VaadinIcons.REFRESH,
074            new Runnable() {
075
076                public void run() {
077
078                    passwordField.setValue(getRandomPassword());
079                }
080            },
081            CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GEN_PASSWORD_REFRESH_0));
082
083        Label label = new Label(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GEN_PASSWORD_TEXT_0));
084        label.setWidth("100%");
085        layout.addComponent(label);
086        row.setWidth("100%");
087        layout.addComponent(row);
088        setContent(layout);
089        Button closeButton = new Button(CmsVaadinUtils.messageCancel());
090        closeButton.addClickListener(new Button.ClickListener() {
091
092            private static final long serialVersionUID = -8994698147835117427L;
093
094            public void buttonClick(ClickEvent event) {
095
096                close.run();
097
098            }
099        });
100
101        Button okButton = new Button(CmsVaadinUtils.messageOk());
102        okButton.addClickListener(new Button.ClickListener() {
103
104            private static final long serialVersionUID = -1375411076842792728L;
105
106            public void buttonClick(ClickEvent event) {
107
108                passwordFetcher.fetchPassword(passwordField.getValue());
109                close.run();
110            }
111        });
112        addButton(okButton);
113        addButton(closeButton);
114    }
115
116    /**
117     * Gets random password from password handler.<p>
118     *
119     * @return String
120     */
121    public static String getRandomPassword() {
122
123        if (OpenCms.getPasswordHandler() instanceof I_CmsPasswordGenerator) {
124            return ((I_CmsPasswordGenerator)OpenCms.getPasswordHandler()).getRandomPassword();
125        }
126        return CmsDefaultPasswordGenerator.getRandomPWD();
127    }
128}