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.ade.sitemap.client.hoverbar;
029
030import org.opencms.ade.sitemap.client.CmsSitemapView;
031import org.opencms.ade.sitemap.client.Messages;
032import org.opencms.ade.sitemap.client.control.CmsSitemapController;
033import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
034import org.opencms.ade.sitemap.shared.CmsSitemapData;
035import org.opencms.gwt.client.ui.A_CmsListItemSelectDialog;
036import org.opencms.gwt.client.ui.CmsConfirmDialog;
037import org.opencms.gwt.client.ui.CmsListItemWidget;
038import org.opencms.gwt.client.ui.I_CmsConfirmDialogHandler;
039import org.opencms.gwt.shared.CmsListInfoBean;
040import org.opencms.util.CmsStringUtil;
041
042import java.util.List;
043
044/**
045 * Sitemap context menu create sub sitemap entry.<p>
046 *
047 * @since 8.0.0
048 */
049public class CmsSubSitemapMenuEntry extends A_CmsSitemapMenuEntry {
050
051    /**
052     * Selection dialog for sitemap folder types.<p>
053     */
054    protected class SitemapTypeDialog extends A_CmsListItemSelectDialog<CmsListInfoBean> {
055
056        /**
057         * Creates a new instance.<p>
058         *
059         * @param itemInfos the list info beans for the different sitemap types
060         */
061        public SitemapTypeDialog(List<CmsListInfoBean> itemInfos) {
062
063            super(itemInfos, Messages.get().key(Messages.GUI_SITEMAP_TYPE_CHOICE_TITLE_0), Messages.get().key(
064                Messages.GUI_SITEMAP_TYPE_CHOICE_TEXT_0));
065
066        }
067
068        /**
069         * @see org.opencms.gwt.client.ui.A_CmsListItemSelectDialog#handleSelection(org.opencms.gwt.shared.CmsListInfoBean)
070         */
071        @Override
072        protected void handleSelection(CmsListInfoBean info) {
073
074            CmsSitemapController controller = CmsSitemapView.getInstance().getController();
075            String typeName = info.getResourceType();
076            final CmsClientSitemapEntry entry = getHoverbar().getEntry();
077            controller.createSitemapSubEntry(entry, typeName);
078        }
079
080    }
081
082    /**
083     * Constructor.<p>
084     *
085     * @param hoverbar the hoverbar
086     */
087    public CmsSubSitemapMenuEntry(CmsSitemapHoverbar hoverbar) {
088
089        super(hoverbar);
090        setLabel(Messages.get().key(Messages.GUI_HOVERBAR_SUBSITEMAP_0));
091        setActive(true);
092    }
093
094    /**
095     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
096     */
097    public void execute() {
098
099        if (isCreateFolderMode()) {
100            executeFolderMode();
101        } else {
102            executeConvertMode();
103        }
104    }
105
106    /**
107     * @see org.opencms.ade.sitemap.client.hoverbar.A_CmsSitemapMenuEntry#onShow()
108     */
109    @Override
110    public void onShow() {
111
112        CmsSitemapController controller = getHoverbar().getController();
113        CmsClientSitemapEntry entry = getHoverbar().getEntry();
114        boolean show = controller.isEditable()
115            && CmsSitemapView.getInstance().isNavigationMode()
116            && entry.isInNavigation()
117            && entry.isFolderType()
118            && !controller.isRoot(entry.getSitePath());
119        if (isCreateFolderMode()) {
120            setVisible(show);
121            setActive(show);
122            setDisabledReason(null);
123        } else {
124            setVisible(show);
125            if (show && !entry.isEditable()) {
126                setActive(false);
127                setDisabledReason(controller.getNoEditReason(entry));
128            } else {
129                setActive(true);
130                setDisabledReason(null);
131            }
132        }
133    }
134
135    /**
136     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
137     */
138    private void executeConvertMode() {
139
140        final CmsSitemapController controller = getHoverbar().getController();
141        String confirmTitle = Messages.get().key(Messages.GUI_SUBSITEMAP_CONFIRM_TITLE_0);
142        String confirmMessage = Messages.get().key(Messages.GUI_SUBSITEMAP_CONFIRM_TEXT_0);
143        CmsConfirmDialog confirmDialog = new CmsConfirmDialog(confirmTitle, confirmMessage);
144
145        final CmsClientSitemapEntry entry = getHoverbar().getEntry();
146
147        CmsListInfoBean infoBean = new CmsListInfoBean();
148        infoBean.setTitle(entry.getTitle());
149        infoBean.setSubTitle(entry.getSitePath());
150        infoBean.addAdditionalInfo(Messages.get().key(Messages.GUI_NAME_0), entry.getName());
151        String shownPath = entry.getVfsPath();
152        if (CmsStringUtil.isEmptyOrWhitespaceOnly(shownPath)) {
153            shownPath = "-";
154        }
155        infoBean.addAdditionalInfo(Messages.get().key(Messages.GUI_VFS_PATH_0), shownPath);
156        // showing the resource type icon of the default file in navigation mode
157        infoBean.setResourceType(CmsStringUtil.isNotEmptyOrWhitespaceOnly(entry.getDefaultFileType())
158        ? entry.getDefaultFileType()
159        : entry.getResourceTypeName());
160        confirmDialog.addTopWidget(new CmsListItemWidget(infoBean));
161        confirmDialog.setHandler(new I_CmsConfirmDialogHandler() {
162
163            /**
164             * @see org.opencms.gwt.client.ui.I_CmsCloseDialogHandler#onClose()
165             */
166            public void onClose() {
167
168                // do nothing
169            }
170
171            /**
172             * @see org.opencms.gwt.client.ui.I_CmsConfirmDialogHandler#onOk()
173             */
174            public void onOk() {
175
176                controller.createSubSitemap(entry.getId());
177            }
178        });
179        confirmDialog.center();
180    }
181
182    /**
183     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
184     */
185    private void executeFolderMode() {
186
187        final CmsSitemapController controller = getHoverbar().getController();
188        SitemapTypeDialog dialog = new SitemapTypeDialog(controller.getData().getSubsitemapFolderTypeInfos());
189        dialog.center();
190    }
191
192    /**
193     * Checks if new folders should be created for subsitemaps.<p>
194     *
195     * @return true if new folders should be created for subsitemaps
196     */
197    private boolean isCreateFolderMode() {
198
199        CmsSitemapController controller = getHoverbar().getController();
200        CmsSitemapData data = controller.getData();
201        return data.isCreateNewFoldersForSubsitemaps()
202            && (data.getSubsitemapFolderTypeInfos() != null)
203            && !data.getSubsitemapFolderTypeInfos().isEmpty();
204    }
205
206}