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.toolbar;
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.client.ui.CmsCreatableListItem;
034import org.opencms.ade.sitemap.client.ui.CmsCreatableListItem.NewEntryType;
035import org.opencms.ade.sitemap.client.ui.css.I_CmsSitemapLayoutBundle;
036import org.opencms.ade.sitemap.shared.CmsNewResourceInfo;
037import org.opencms.gwt.client.ui.CmsList;
038import org.opencms.gwt.client.ui.CmsListItemWidget;
039import org.opencms.gwt.client.ui.I_CmsButton;
040import org.opencms.gwt.client.ui.I_CmsListItem;
041import org.opencms.gwt.shared.CmsAdditionalInfoBean;
042import org.opencms.gwt.shared.CmsListInfoBean;
043import org.opencms.util.CmsStringUtil;
044
045import com.google.gwt.user.client.ui.Label;
046import com.google.gwt.user.client.ui.SimplePanel;
047
048/**
049 * Sitemap toolbar new menu button.<p>
050 *
051 * @since 8.0.0
052 */
053public class CmsToolbarNewButton extends A_CmsToolbarListMenuButton {
054
055    /** The tag for identifying redirect items. */
056    public static final String TAG_REDIRECT = "redirect";
057
058    /** The tag for identifying items from the special tab. */
059    public static final String TAG_SPECIAL = "special";
060
061    /** The special elements list. */
062    private CmsList<I_CmsListItem> m_detailList;
063
064    /** The function element list. */
065    private CmsList<I_CmsListItem> m_functionList;
066
067    /** The new-elements list. */
068    private CmsList<I_CmsListItem> m_newElementsList;
069
070    /**
071     * Constructor.<p>
072     *
073     * @param toolbar the toolbar instance
074     * @param controller the sitemap controller
075     */
076    public CmsToolbarNewButton(CmsSitemapToolbar toolbar, CmsSitemapController controller) {
077
078        super(
079            Messages.get().key(Messages.GUI_TOOLBAR_NEW_BUTTON_TITLE_0),
080            I_CmsButton.ButtonData.WAND_BUTTON.getIconClass(),
081            toolbar,
082            controller);
083    }
084
085    /**
086     * @see org.opencms.ade.sitemap.client.toolbar.A_CmsToolbarListMenuButton#initContent()
087     */
088    @Override
089    protected boolean initContent() {
090
091        while (m_tabs.getTabCount() > 0) {
092            m_tabs.removeTab(0);
093        }
094        boolean hasTabs = false;
095        m_newElementsList = new CmsList<I_CmsListItem>();
096        for (CmsNewResourceInfo info : getController().getData().getNewElementInfos()) {
097            m_newElementsList.add(makeNewElementItem(info));
098        }
099        if (m_newElementsList.getWidgetCount() > 0) {
100            hasTabs = true;
101            addTab(createTab(m_newElementsList), Messages.get().key(Messages.GUI_NEW_PAGES_TAB_TITLE_0));
102        }
103        m_detailList = new CmsList<I_CmsListItem>();
104        if (getController().getData().canEditDetailPages()) {
105            for (CmsNewResourceInfo typeInfo : getController().getData().getResourceTypeInfos()) {
106                if (CmsStringUtil.isEmptyOrWhitespaceOnly(typeInfo.getCreateParameter())) {
107                    CmsCreatableListItem item = makeDetailPageItem(typeInfo);
108                    m_detailList.add(item);
109                }
110            }
111        }
112
113        for (CmsNewResourceInfo typeInfo : getController().getData().getResourceTypeInfos()) {
114            if (!CmsStringUtil.isEmptyOrWhitespaceOnly(typeInfo.getCreateParameter())) {
115                CmsCreatableListItem item = makeDetailPageItem(typeInfo);
116                m_detailList.add(item);
117            }
118        }
119        if (m_detailList.getWidgetCount() > 0) {
120            hasTabs = true;
121            addTab(createTab(m_detailList), Messages.get().key(Messages.GUI_SPECIAL_TAB_TITLE_0));
122        }
123
124        m_functionList = new CmsList<I_CmsListItem>();
125        m_functionList.add(makeRedirectItem());
126        m_functionList.add(makeNavigationLevelItem());
127
128        if (m_functionList.getWidgetCount() > 0) {
129            hasTabs = true;
130            String tabLabel = Messages.get().key(Messages.GUI_FUNCTION_TAB_TITLE_0);
131            addTab(createTab(m_functionList), tabLabel);
132        }
133
134        if (!hasTabs) {
135            // no new elements available, show appropriate message
136
137            //TODO: improve styling, add localization
138            Label messageLabel = new Label(Messages.get().key(Messages.GUI_NO_CREATABLE_ELEMENTS_0));
139            messageLabel.addStyleName(I_CmsSitemapLayoutBundle.INSTANCE.clipboardCss().menuTabContainer());
140            SimplePanel content = new SimplePanel();
141            content.setWidget(messageLabel);
142            setMenuWidget(messageLabel);
143        }
144        return false;
145    }
146
147    /**
148     * Creates a list item representing a detail page to be created.<p>
149     *
150     * @param typeInfo the bean for the type for which the detail page item should be created
151     *
152     * @return the detail page list item
153     */
154    private CmsCreatableListItem makeDetailPageItem(CmsNewResourceInfo typeInfo) {
155
156        CmsListInfoBean info = new CmsListInfoBean();
157        String subtitle = typeInfo.getSubTitle();
158        String title = Messages.get().key(
159            typeInfo.isFunction() ? Messages.GUI_FUNCTION_PAGE_TITLE_1 : Messages.GUI_DETAIL_PAGE_TITLE_1,
160            typeInfo.getTitle());
161
162        info.setTitle(title);
163        info.setSubTitle(subtitle);
164        CmsListItemWidget widget = new CmsListItemWidget(info);
165        widget.setIcon(typeInfo.getBigIconClasses());
166        CmsCreatableListItem listItem = new CmsCreatableListItem(widget, typeInfo, NewEntryType.detailpage);
167        listItem.initMoveHandle(CmsSitemapView.getInstance().getTree().getDnDHandler());
168        return listItem;
169    }
170
171    /**
172     * Creates a list item representing a redirect.<p>
173     *
174     * @return the new list item
175     */
176    private CmsCreatableListItem makeNavigationLevelItem() {
177
178        CmsNewResourceInfo typeInfo = getController().getData().getNewNavigationLevelElementInfo();
179        CmsListItemWidget widget = new CmsListItemWidget(typeInfo);
180        CmsCreatableListItem listItem = new CmsCreatableListItem(widget, typeInfo, NewEntryType.regular);
181        listItem.initMoveHandle(CmsSitemapView.getInstance().getTree().getDnDHandler());
182        return listItem;
183    }
184
185    /**
186     * Create a new-element list item.<p>
187     *
188     * @param typeInfo the new-element info
189     *
190     * @return the list item
191     */
192    private CmsCreatableListItem makeNewElementItem(final CmsNewResourceInfo typeInfo) {
193
194        CmsListItemWidget widget = new CmsListItemWidget(typeInfo);
195        if ((typeInfo.getDescription() != null) && (typeInfo.getDescription().trim().length() > 0)) {
196            widget.addAdditionalInfo(
197                new CmsAdditionalInfoBean(
198                    Messages.get().key(Messages.GUI_LABEL_DESCRIPTION_0),
199                    typeInfo.getDescription(),
200                    null));
201        }
202        if (typeInfo.getVfsPath() != null) {
203            widget.addAdditionalInfo(
204                new CmsAdditionalInfoBean(
205                    Messages.get().key(Messages.GUI_LABEL_VFSPATH_0),
206                    typeInfo.getVfsPath(),
207                    null));
208        }
209        if (typeInfo.getDate() != null) {
210            widget.addAdditionalInfo(
211                new CmsAdditionalInfoBean(Messages.get().key(Messages.GUI_LABEL_DATE_0), typeInfo.getDate(), null));
212        }
213        CmsCreatableListItem listItem = new CmsCreatableListItem(widget, typeInfo, NewEntryType.regular);
214        listItem.initMoveHandle(CmsSitemapView.getInstance().getTree().getDnDHandler(), true);
215        return listItem;
216    }
217
218    /**
219     * Creates a list item representing a redirect.<p>
220     *
221     * @return the new list item
222     */
223    private CmsCreatableListItem makeRedirectItem() {
224
225        CmsNewResourceInfo typeInfo = getController().getData().getNewRedirectElementInfo();
226        CmsListInfoBean info = new CmsListInfoBean();
227        info.setTitle(typeInfo.getTitle());
228        info.setSubTitle(Messages.get().key(Messages.GUI_REDIRECT_SUBTITLE_0));
229        CmsListItemWidget widget = new CmsListItemWidget(info);
230        widget.setIcon(typeInfo.getBigIconClasses());
231        CmsCreatableListItem listItem = new CmsCreatableListItem(widget, typeInfo, NewEntryType.redirect);
232        listItem.initMoveHandle(CmsSitemapView.getInstance().getTree().getDnDHandler());
233        return listItem;
234    }
235}