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.dialogs;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.configuration.CmsResourceTypeConfig;
032import org.opencms.ade.galleries.shared.CmsResourceTypeBean;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.I_CmsDialogContext;
038import org.opencms.ui.apps.CmsAppWorkplaceUi;
039import org.opencms.ui.components.extensions.CmsPropertyDialogExtension;
040import org.opencms.ui.util.CmsNewResourceBuilder;
041import org.opencms.ui.util.CmsNewResourceBuilder.I_Callback;
042import org.opencms.util.CmsStringUtil;
043import org.opencms.util.CmsUUID;
044
045import java.util.ArrayList;
046
047import com.google.common.collect.Lists;
048import com.vaadin.annotations.DesignRoot;
049import com.vaadin.ui.Button;
050import com.vaadin.v7.data.Property.ValueChangeEvent;
051import com.vaadin.v7.data.Property.ValueChangeListener;
052import com.vaadin.v7.ui.CheckBox;
053import com.vaadin.v7.ui.ComboBox;
054import com.vaadin.v7.ui.VerticalLayout;
055
056/**
057 * Dialog for creating new resources.<p>
058 */
059@DesignRoot
060public class CmsNewDialog extends A_CmsSelectResourceTypeDialog {
061
062    /** Element view selector. */
063    protected ComboBox m_viewSelector;
064
065    /** Container for the type list. */
066    protected VerticalLayout m_typeContainer;
067
068    /** Check box for enabling / disabling default creation folders. */
069    protected CheckBox m_defaultLocationCheckbox;
070
071    /** The cancel button. */
072    protected Button m_cancelButton;
073
074    protected Button m_modeToggle;
075
076    /**
077     * Creates a new instance.<p>
078     *
079     * @param folderResource the folder resource
080     * @param context the context
081     */
082    public CmsNewDialog(CmsResource folderResource, I_CmsDialogContext context) {
083
084        super(folderResource, context);
085
086        m_defaultLocationCheckbox.setValue(getInitialValueForUseDefaultLocationOption(folderResource));
087        m_defaultLocationCheckbox.addValueChangeListener(new ValueChangeListener() {
088
089            private static final long serialVersionUID = 1L;
090
091            public void valueChange(ValueChangeEvent event) {
092
093                try {
094                    init(m_currentView, ((Boolean)event.getProperty().getValue()).booleanValue(), m_filterString);
095                } catch (Exception e) {
096                    m_dialogContext.error(e);
097                }
098
099            }
100        });
101
102    }
103
104    @Override
105    public Button getCancelButton() {
106
107        return m_cancelButton;
108    }
109
110    @Override
111    public Button getModeToggle() {
112
113        return m_modeToggle;
114    }
115
116    @Override
117    public VerticalLayout getVerticalLayout() {
118
119        return m_typeContainer;
120    }
121
122    @Override
123    public ComboBox getViewSelector() {
124
125        return m_viewSelector;
126    }
127
128    /**
129     * Handles selection of a type.<p>
130     *
131     * @param selectedType the selected type
132     */
133    @Override
134    public void handleSelection(final CmsResourceTypeBean selectedType) {
135
136        CmsObject cms = A_CmsUI.getCmsObject();
137        m_selectedType = selectedType;
138        try {
139
140            CmsNewResourceBuilder builder = new CmsNewResourceBuilder(cms);
141            builder.addCallback(new I_Callback() {
142
143                public void onError(Exception e) {
144
145                    m_dialogContext.error(e);
146                }
147
148                public void onResourceCreated(CmsNewResourceBuilder builderParam) {
149
150                    finish(Lists.newArrayList(builderParam.getCreatedResource().getStructureId()));
151                }
152            });
153
154            m_selectedType = selectedType;
155
156            Boolean useDefaultLocation = m_defaultLocationCheckbox.getValue();
157            if (useDefaultLocation.booleanValue() && (m_selectedType.getCreatePath() != null)) {
158                try {
159                    CmsADEConfigData configData = OpenCms.getADEManager().lookupConfiguration(
160                        cms,
161                        m_folderResource.getRootPath());
162
163                    CmsResourceTypeConfig typeConfig = configData.getResourceType(m_selectedType.getType());
164                    if (typeConfig != null) {
165                        typeConfig.configureCreateNewElement(cms, m_folderResource.getRootPath(), builder);
166                    }
167                } catch (Exception e) {
168                    m_dialogContext.error(e);
169                }
170
171            } else {
172                boolean explorerNameGenerationMode = false;
173                String sitePath = cms.getRequestContext().removeSiteRoot(m_folderResource.getRootPath());
174                String namePattern = m_selectedType.getNamePattern();
175                if (CmsStringUtil.isEmptyOrWhitespaceOnly(namePattern)) {
176                    namePattern = OpenCms.getWorkplaceManager().getDefaultNamePattern(m_selectedType.getType());
177                    explorerNameGenerationMode = true;
178                }
179                String fileName = CmsStringUtil.joinPaths(sitePath, namePattern);
180                builder.setPatternPath(fileName);
181                builder.setType(m_selectedType.getType());
182                builder.setExplorerNameGeneration(explorerNameGenerationMode);
183
184            }
185            CmsPropertyDialogExtension ext = new CmsPropertyDialogExtension(A_CmsUI.get(), null);
186            CmsAppWorkplaceUi.get().disableGlobalShortcuts();
187            ext.editPropertiesForNewResource(builder);
188            finish(new ArrayList<CmsUUID>());
189
190        } catch (Exception e) {
191            throw new RuntimeException(e);
192        }
193
194    }
195
196    @Override
197    public boolean useDefault() {
198
199        return m_defaultLocationCheckbox.getValue().booleanValue();
200    }
201
202    /**
203     * Gets the initial value for the 'default location' option.<p>
204     *
205     * @param folderResource the current folder
206     *
207     * @return the initial value for the option
208     */
209    private Boolean getInitialValueForUseDefaultLocationOption(CmsResource folderResource) {
210
211        String rootPath = folderResource.getRootPath();
212        return Boolean.valueOf(OpenCms.getSiteManager().getSiteForRootPath(rootPath) != null);
213    }
214
215}