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.v7.data.Property.ValueChangeEvent;
050import com.vaadin.v7.data.Property.ValueChangeListener;
051import com.vaadin.ui.Button;
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    /**
075     * Creates a new instance.<p>
076     *
077     * @param folderResource the folder resource
078     * @param context the context
079     */
080    public CmsNewDialog(CmsResource folderResource, I_CmsDialogContext context) {
081        super(folderResource, context);
082        m_defaultLocationCheckbox.setValue(getInitialValueForUseDefaultLocationOption(folderResource));
083        m_defaultLocationCheckbox.addValueChangeListener(new ValueChangeListener() {
084
085            private static final long serialVersionUID = 1L;
086
087            public void valueChange(ValueChangeEvent event) {
088
089                try {
090                    init(m_currentView, ((Boolean)event.getProperty().getValue()).booleanValue());
091                } catch (Exception e) {
092                    m_dialogContext.error(e);
093                }
094
095            }
096        });
097
098    }
099
100    @Override
101    public Button getCancelButton() {
102
103        return m_cancelButton;
104    }
105
106    @Override
107    public VerticalLayout getVerticalLayout() {
108
109        return m_typeContainer;
110    }
111
112    @Override
113    public ComboBox getViewSelector() {
114
115        return m_viewSelector;
116    }
117
118    /**
119     * Handles selection of a type.<p>
120     *
121     * @param selectedType the selected type
122     */
123    @Override
124    public void handleSelection(final CmsResourceTypeBean selectedType) {
125
126        CmsObject cms = A_CmsUI.getCmsObject();
127        m_selectedType = selectedType;
128        try {
129
130            CmsNewResourceBuilder builder = new CmsNewResourceBuilder(cms);
131            builder.addCallback(new I_Callback() {
132
133                public void onError(Exception e) {
134
135                    m_dialogContext.error(e);
136                }
137
138                public void onResourceCreated(CmsNewResourceBuilder builderParam) {
139
140                    finish(Lists.newArrayList(builderParam.getCreatedResource().getStructureId()));
141                }
142            });
143
144            m_selectedType = selectedType;
145
146            Boolean useDefaultLocation = m_defaultLocationCheckbox.getValue();
147            if (useDefaultLocation.booleanValue() && (m_selectedType.getCreatePath() != null)) {
148                try {
149                    CmsADEConfigData configData = OpenCms.getADEManager().lookupConfiguration(
150                        cms,
151                        m_folderResource.getRootPath());
152
153                    CmsResourceTypeConfig typeConfig = configData.getResourceType(m_selectedType.getType());
154                    if (typeConfig != null) {
155                        typeConfig.configureCreateNewElement(cms, m_folderResource.getRootPath(), builder);
156                    }
157                } catch (Exception e) {
158                    m_dialogContext.error(e);
159                }
160
161            } else {
162                boolean explorerNameGenerationMode = false;
163                String sitePath = cms.getRequestContext().removeSiteRoot(m_folderResource.getRootPath());
164                String namePattern = m_selectedType.getNamePattern();
165                if (CmsStringUtil.isEmptyOrWhitespaceOnly(namePattern)) {
166                    namePattern = OpenCms.getWorkplaceManager().getDefaultNamePattern(m_selectedType.getType());
167                    explorerNameGenerationMode = true;
168                }
169                String fileName = CmsStringUtil.joinPaths(sitePath, namePattern);
170                builder.setPatternPath(fileName);
171                builder.setType(m_selectedType.getType());
172                builder.setExplorerNameGeneration(explorerNameGenerationMode);
173
174            }
175            CmsPropertyDialogExtension ext = new CmsPropertyDialogExtension(A_CmsUI.get(), null);
176            CmsAppWorkplaceUi.get().disableGlobalShortcuts();
177            ext.editPropertiesForNewResource(builder);
178            finish(new ArrayList<CmsUUID>());
179
180        } catch (Exception e) {
181            throw new RuntimeException(e);
182        }
183
184    }
185
186    @Override
187    public boolean useDefault() {
188
189        return m_defaultLocationCheckbox.getValue().booleanValue();
190    }
191
192    /**
193     * Gets the initial value for the 'default location' option.<p>
194     *
195     * @param folderResource the current folder
196     *
197     * @return the initial value for the option
198     */
199    private Boolean getInitialValueForUseDefaultLocationOption(CmsResource folderResource) {
200
201        String rootPath = folderResource.getRootPath();
202        return Boolean.valueOf(OpenCms.getSiteManager().getSiteForRootPath(rootPath) != null);
203    }
204
205}