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        String notEmptyMessage = Messages.get().key(Messages.GUI_CATEGORY_VALIDATION_TITLE_NOTEMPTY_0);
146        dlg.getForm().addField(
147            CmsBasicFormField.createField(createBasicStringProperty("title", titleLabel, ".+", notEmptyMessage)),
148            "");
149        dlg.getForm().addField(
150            CmsBasicFormField.createField(createBasicStringProperty("name", nameLabel, null, null)),
151            "");
152        dlg.getForm().render();
153        dlg.center();
154
155    }
156
157    /**
158     * Creates a property configuration for a simple named string field.<p>
159     *
160     * @param name the name of the field
161     * @param niceName the display name of the field
162     * @param validationRegex the validation regex
163     * @param error the validation error message 
164     *
165     * @return the property configuration
166     */
167    public static CmsXmlContentProperty createBasicStringProperty(
168        String name,
169        String niceName,
170        String validationRegex,
171        String error) {
172
173        CmsXmlContentProperty prop = new CmsXmlContentProperty(
174            name, //name
175            "string", // type
176            "string", // widget
177            "", // widgetconfig
178            validationRegex, //regex
179            "error", //ruletype
180            null, //default
181            niceName, //nicename
182            null, //description
183            error, //error
184            null //preferfolder
185        );
186        return prop;
187    }
188
189    /**
190     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
191     */
192    public void execute() {
193
194        final CmsSitemapController controller = getHoverbar().getController();
195        final CmsUUID id = getHoverbar().getId();
196        askForNewCategoryInfo(id, new AsyncCallback<CmsCategoryTitleAndName>() {
197
198            public void onFailure(Throwable caught) {
199
200                // do nothing
201            }
202
203            public void onSuccess(CmsCategoryTitleAndName result) {
204
205                controller.createCategory(id, result.getTitle(), result.getName());
206            }
207        });
208
209    }
210
211    /**
212     * @see org.opencms.ade.sitemap.client.hoverbar.A_CmsSitemapMenuEntry#onShow()
213     */
214    @Override
215    public void onShow() {
216
217        setVisible(getHoverbar().getController().isEditable());
218    }
219}