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.CmsResourceTypeConfig;
031import org.opencms.ade.containerpage.CmsAddDialogTypeHelper;
032import org.opencms.ade.galleries.shared.CmsResourceTypeBean;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.lock.CmsLockActionRecord;
036import org.opencms.lock.CmsLockActionRecord.LockChange;
037import org.opencms.lock.CmsLockUtil;
038import org.opencms.main.CmsException;
039import org.opencms.main.CmsLog;
040import org.opencms.main.OpenCms;
041import org.opencms.ui.A_CmsUI;
042import org.opencms.ui.CmsVaadinUtils;
043import org.opencms.ui.I_CmsDialogContext;
044import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
045
046import java.util.Arrays;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.ui.HorizontalLayout;
051import com.vaadin.ui.Label;
052
053/**
054 * Dialog for changing the resource type.<p>
055 */
056public class CmsChangeTypeDialog extends CmsNewDialog {
057
058    /** Logger instance for this class. */
059    static final Log LOG = CmsLog.getLog(CmsChangeTypeDialog.class);
060
061    /** Serial version id. */
062    private static final long serialVersionUID = 1L;
063
064    /**
065     * Creates a new instance.<p>
066     *
067     * @param context the dialog context
068     *
069     * @throws CmsException if something goes wrong
070     */
071    public CmsChangeTypeDialog(I_CmsDialogContext context)
072    throws CmsException {
073
074        super(A_CmsUI.getCmsObject().readParentFolder(context.getResources().get(0).getStructureId()), context);
075        displayResourceInfo(context.getResources());
076        HorizontalLayout layout = (HorizontalLayout)m_defaultLocationCheckbox.getParent();
077        Label l = new Label();
078        l.setWidth("100%");
079        layout.replaceComponent(m_defaultLocationCheckbox, l);
080        layout.setExpandRatio(l, 1.0f);
081    }
082
083    /**
084     * @see org.opencms.ui.dialogs.CmsNewDialog#handleSelection(org.opencms.ade.galleries.shared.CmsResourceTypeBean)
085     */
086    @Override
087    public void handleSelection(CmsResourceTypeBean typeBean) {
088
089        CmsResource changeRes = m_dialogContext.getResources().get(0);
090        CmsObject cms = A_CmsUI.getCmsObject();
091        CmsLockActionRecord lockRecord = null;
092        try {
093            lockRecord = CmsLockUtil.ensureLock(cms, changeRes);
094            cms.chtype(cms.getSitePath(changeRes), OpenCms.getResourceManager().getResourceType(typeBean.getType()));
095            m_dialogContext.finish(Arrays.asList(changeRes.getStructureId()));
096        } catch (CmsException e) {
097            m_dialogContext.error(e);
098        } finally {
099            if ((lockRecord != null) && (lockRecord.getChange() == LockChange.locked)) {
100                try {
101                    cms.unlockResource(changeRes);
102                    m_dialogContext.finish(Arrays.asList(changeRes.getStructureId()));
103                } catch (CmsException e) {
104                    LOG.error(e.getLocalizedMessage());
105                }
106            }
107        }
108
109    }
110
111    /**
112     * @see org.opencms.ui.dialogs.CmsNewDialog#createTypeHelper()
113     */
114    @Override
115    protected CmsAddDialogTypeHelper createTypeHelper() {
116
117        return new CmsAddDialogTypeHelper(CmsResourceTypeConfig.AddMenuType.workplace) {
118
119            @Override
120            protected boolean exclude(CmsResourceTypeBean type) {
121
122                boolean sameType = OpenCms.getResourceManager().matchResourceType(
123                    type.getType(),
124                    m_dialogContext.getResources().get(0).getTypeId());
125                if (sameType) {
126                    return true;
127                }
128
129                String typeName = type.getType();
130                try {
131                    boolean isFolder = m_dialogContext.getResources().get(0).isFolder();
132                    boolean identicalTypeGroup = OpenCms.getResourceManager().getResourceType(
133                        typeName).isFolder() == isFolder;
134                    return !identicalTypeGroup;
135                } catch (Exception e) {
136                    LOG.debug(e.getLocalizedMessage(), e);
137                    return false;
138                }
139            }
140        };
141    }
142
143    /**
144     *
145     * @see org.opencms.ui.dialogs.CmsNewDialog#getSubtitle(org.opencms.ade.galleries.shared.CmsResourceTypeBean, boolean)
146     */
147    @Override
148    protected String getSubtitle(CmsResourceTypeBean type, boolean useDefault) {
149
150        CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type.getType());
151        if (explorerType != null) {
152            String explorerInfo = explorerType.getInfo();
153            if (explorerInfo != null) {
154                return CmsVaadinUtils.getMessageText(explorerInfo);
155            }
156        }
157        return "";
158
159    }
160
161}