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.CmsSitemapCategoryData;
034import org.opencms.gwt.client.ui.CmsPopup;
035import org.opencms.gwt.client.ui.input.form.CmsBasicFormField;
036import org.opencms.gwt.client.ui.input.form.CmsDialogFormHandler;
037import org.opencms.gwt.client.ui.input.form.CmsForm;
038import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
039import org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler;
040import org.opencms.gwt.shared.CmsCategoryTreeEntry;
041import org.opencms.util.CmsUUID;
042import org.opencms.xml.content.CmsXmlContentProperty;
043
044import java.util.Map;
045import java.util.Set;
046
047import com.google.gwt.user.client.rpc.AsyncCallback;
048
049/**
050 * Menu entry for creating new categories.<p>
051 */
052public class CmsCreateCategoryMenuEntry extends A_CmsSitemapMenuEntry {
053
054    /**
055     * Bean containing the title and name of a category.<p>
056     */
057    public static class CmsCategoryTitleAndName {
058
059        /** The name of the category. */
060        private String m_name;
061
062        /** The title of the category. */
063        private String m_title;
064
065        /**
066         * Creates a new instance.<p>
067         *
068         * @param title the title of the category
069         * @param name the name of the category
070         */
071        public CmsCategoryTitleAndName(String title, String name) {
072
073            m_name = name;
074            m_title = title;
075        }
076
077        /**
078         * Gets the name of the category.<p>
079         *
080         * @return the category name
081         */
082        public String getName() {
083
084            return m_name;
085        }
086
087        /**
088         * Gets the title of the category.<p>
089         *
090         * @return the category title
091         */
092        public String getTitle() {
093
094            return m_title;
095        }
096    }
097
098    /**
099     * Constructor.<p>
100     *
101     * @param hoverbar the hoverbar
102     */
103    public CmsCreateCategoryMenuEntry(CmsSitemapHoverbar hoverbar) {
104
105        super(hoverbar);
106        setLabel(Messages.get().key(Messages.GUI_SITEMAP_CONTEXT_MENU_CREATE_CATEGORY_0));
107        setActive(true);
108    }
109
110    /**
111     * Asks the user for a new category's name and title.<p>
112     *
113     * @param parentId the parent category
114     * @param callback the callback to call with the user-supplied information
115     */
116    public static void askForNewCategoryInfo(CmsUUID parentId, final AsyncCallback<CmsCategoryTitleAndName> callback) {
117
118        CmsSitemapCategoryData categoryData = CmsSitemapView.getInstance().getController().getCategoryData();
119
120        CmsCategoryTreeEntry entry = categoryData.getEntryById(parentId);
121        String caption;
122        if (entry == null) {
123            caption = Messages.get().key(Messages.GUI_SITEMAP_CREATE_CATEGORY_TITLE_0);
124        } else {
125            caption = Messages.get().key(Messages.GUI_SITEMAP_CREATE_SUBCATEGORY_TITLE_1, entry.getPath());
126        }
127        CmsFormDialog dlg = new CmsFormDialog(caption, new CmsForm(true));
128        dlg.setWidth(CmsPopup.DEFAULT_WIDTH);
129        CmsDialogFormHandler fh = new CmsDialogFormHandler();
130        fh.setSubmitHandler(new I_CmsFormSubmitHandler() {
131
132            public void onSubmitForm(CmsForm form, Map<String, String> fieldValues, Set<String> editedFields) {
133
134                String title = fieldValues.get("title");
135                String name = fieldValues.get("name");
136                callback.onSuccess(new CmsCategoryTitleAndName(title, name));
137
138            }
139        });
140
141        dlg.setFormHandler(fh);
142        fh.setDialog(dlg);
143        String nameLabel = Messages.get().key(Messages.GUI_CATEGORY_NAME_LABEL_0);
144        String titleLabel = Messages.get().key(Messages.GUI_CATEGORY_TITLE_LABEL_0);
145        dlg.getForm().addField(CmsBasicFormField.createField(createBasicStringProperty("title", titleLabel)), "");
146        dlg.getForm().addField(CmsBasicFormField.createField(createBasicStringProperty("name", nameLabel)), "");
147        dlg.getForm().render();
148        dlg.center();
149
150    }
151
152    /**
153     * Creates a property configuration for a simple named string field.<p>
154     *
155     * @param name the name of the field
156     * @param niceName the display name of the field
157     *
158     * @return the property configuration
159     */
160    public static CmsXmlContentProperty createBasicStringProperty(String name, String niceName) {
161
162        CmsXmlContentProperty prop = new CmsXmlContentProperty(name, //name
163            "string", // type
164            "string", // widget
165            "", // widgetconfig
166            null, //regex
167            null, //ruletype
168            null, //default
169            niceName, //nicename
170            null, //description
171            null, //error
172            null //preferfolder
173        );
174        return prop;
175    }
176
177    /**
178     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
179     */
180    public void execute() {
181
182        final CmsSitemapController controller = getHoverbar().getController();
183        final CmsUUID id = getHoverbar().getId();
184        askForNewCategoryInfo(id, new AsyncCallback<CmsCategoryTitleAndName>() {
185
186            public void onFailure(Throwable caught) {
187
188                // do nothing
189            }
190
191            public void onSuccess(CmsCategoryTitleAndName result) {
192
193                controller.createCategory(id, result.getTitle(), result.getName());
194            }
195        });
196
197    }
198
199    /**
200     * @see org.opencms.ade.sitemap.client.hoverbar.A_CmsSitemapMenuEntry#onShow()
201     */
202    @Override
203    public void onShow() {
204
205        setVisible(getHoverbar().getController().isEditable());
206    }
207}