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.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsRole;
034import org.opencms.ui.A_CmsUI;
035import org.opencms.ui.CmsCssIcon;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.apps.Messages;
038import org.opencms.ui.components.CmsBasicDialog;
039import org.opencms.ui.components.CmsResourceInfo;
040import org.opencms.ui.components.OpenCmsTheme;
041
042import java.util.Collections;
043
044import com.vaadin.event.LayoutEvents.LayoutClickEvent;
045import com.vaadin.event.LayoutEvents.LayoutClickListener;
046import com.vaadin.ui.AbstractComponent;
047import com.vaadin.ui.Button;
048import com.vaadin.ui.Button.ClickEvent;
049import com.vaadin.ui.Button.ClickListener;
050import com.vaadin.ui.Window;
051import com.vaadin.v7.ui.Label;
052import com.vaadin.v7.ui.VerticalLayout;
053
054/**
055 * Dialog to create new element. (User, Group or OU).
056 */
057public class CmsNewElementDialog extends CmsBasicDialog {
058
059    /**ID for user.*/
060    private static String ID_USER = "user";
061
062    /**ID for group. */
063    private static String ID_GROUP = "group";
064
065    /**ID for OU. */
066    private static String ID_OU = "ou";
067
068    /**vaadin serial id.*/
069    private static final long serialVersionUID = 2351253053915340926L;
070
071    /**vaadin component.*/
072    private VerticalLayout m_container;
073
074    /**vaadin component.*/
075    private Button m_cancelButton;
076
077    /**window. */
078    private Window m_window;
079
080    /**The cms object. */
081    private CmsObject m_cms;
082
083    /**vaadin component. */
084    private Label m_ouLabel;
085
086    /**The ou. */
087    private String m_ou;
088
089    /**Accounts app. */
090    private CmsAccountsApp m_app;
091
092    /**
093     * public constructor.<p>
094     * @param cms CmsObject
095     * @param ou ou
096     *
097     * @param window window holding the dialog
098     */
099    public CmsNewElementDialog(CmsObject cms, String ou, final Window window, CmsAccountsApp app) {
100
101        m_app = app;
102        m_window = window;
103        m_cms = cms;
104        m_ou = ou;
105        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
106
107        try {
108            displayResourceInfoDirectly(
109                Collections.singletonList(
110                    CmsAccountsApp.getOUInfo(
111                        OpenCms.getOrgUnitManager().readOrganizationalUnit(A_CmsUI.getCmsObject(), ou))));
112            m_ouLabel.setValue(
113                OpenCms.getOrgUnitManager().readOrganizationalUnit(m_cms, ou).getDisplayName(
114                    m_cms.getRequestContext().getLocale()));
115        } catch (CmsException e) {
116            //
117        }
118        m_cancelButton.addClickListener(new ClickListener() {
119
120            private static final long serialVersionUID = -7494631798452339165L;
121
122            public void buttonClick(ClickEvent event) {
123
124                window.close();
125
126            }
127        });
128        CmsResourceInfo newUser = new CmsResourceInfo(
129            CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_USER_0),
130            CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_USER_HELP_0),
131            new CmsCssIcon(OpenCmsTheme.ICON_USER));
132        newUser.setData(ID_USER);
133        CmsResourceInfo newGroup = new CmsResourceInfo(
134            CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_GROUP_0),
135            CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_GROUP_HELP_0),
136            new CmsCssIcon(OpenCmsTheme.ICON_GROUP));
137        newGroup.setData(ID_GROUP);
138        if (OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ADMINISTRATOR.forOrgUnit(ou))) {
139            CmsResourceInfo newOU = new CmsResourceInfo(
140                CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_OU_0),
141                CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_OU_HELP_0),
142                new CmsCssIcon(OpenCmsTheme.ICON_OU));
143            newOU.setData(ID_OU);
144            m_container.addComponent(newOU);
145        }
146        m_container.addComponent(newUser);
147        m_container.addComponent(newGroup);
148
149        m_container.addLayoutClickListener(new LayoutClickListener() {
150
151            private static final long serialVersionUID = 5189868437695349511L;
152
153            public void layoutClick(LayoutClickEvent event) {
154
155                AbstractComponent component = (AbstractComponent)event.getChildComponent();
156                if (component != null) {
157                    if (component.getData() instanceof String) {
158                        openNewDialog((String)component.getData());
159                    }
160                }
161            }
162        });
163    }
164
165    /**
166     * Opens the dialog.<p>
167     *
168     * @param id of selected item
169     */
170    protected void openNewDialog(String id) {
171
172        CmsBasicDialog dialog = null;
173        String caption = "";
174
175        if (id.equals(ID_GROUP)) {
176            dialog = new CmsGroupEditDialog(m_cms, m_window, m_ou, m_app);
177            caption = CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_GROUP_0);
178        }
179        if (id.equals(ID_OU)) {
180            dialog = new CmsOUEditDialog(m_cms, m_window, m_ou, m_app);
181            caption = CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_OU_0);
182
183        }
184        if (id.equals(ID_USER)) {
185            dialog = new CmsUserEditDialog(m_cms, m_window, m_ou, m_app);
186            caption = CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_ADD_USER_0);
187        }
188
189        if (dialog != null) {
190            m_window.setContent(dialog);
191            m_window.setCaption(caption);
192            m_window.center();
193        }
194    }
195}