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.client.edit.CmsEditEntryHandler;
034import org.opencms.ade.sitemap.client.edit.CmsNavModePropertyEditor;
035import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
036import org.opencms.gwt.client.CmsCoreProvider;
037import org.opencms.gwt.client.property.A_CmsPropertyEditor;
038import org.opencms.gwt.client.property.CmsPropertySubmitHandler;
039import org.opencms.gwt.client.property.CmsVfsModePropertyEditor;
040import org.opencms.gwt.client.property.I_CmsPropertyEditorHandler;
041import org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton;
042import org.opencms.gwt.client.rpc.CmsRpcAction;
043import org.opencms.gwt.client.ui.input.form.CmsDialogFormHandler;
044import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
045import org.opencms.gwt.client.ui.input.form.I_CmsFormSubmitHandler;
046import org.opencms.gwt.shared.CmsListInfoBean;
047import org.opencms.util.CmsUUID;
048import org.opencms.xml.content.CmsXmlContentProperty;
049
050import java.util.ArrayList;
051import java.util.HashSet;
052import java.util.LinkedHashMap;
053import java.util.List;
054import java.util.Map;
055import java.util.Set;
056
057import com.google.gwt.dom.client.Style;
058
059/**
060 * Sitemap context menu edit entry.<p>
061 *
062 * @since 8.0.0
063 */
064public class CmsEditMenuEntry extends A_CmsSitemapMenuEntry {
065
066    /**
067     * Constructor.<p>
068     *
069     * @param hoverbar the hoverbar
070     */
071    public CmsEditMenuEntry(CmsSitemapHoverbar hoverbar) {
072
073        super(hoverbar);
074        setLabel(Messages.get().key(Messages.GUI_HOVERBAR_EDIT_0));
075        setActive(true);
076    }
077
078    /**
079     * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuEntry#execute()
080     */
081    public void execute() {
082
083        final CmsSitemapController controller = getHoverbar().getController();
084        final CmsClientSitemapEntry entry = getHoverbar().getEntry();
085
086        final CmsUUID infoId;
087
088        if ((entry.getDefaultFileId() != null) && CmsSitemapView.getInstance().isNavigationMode()) {
089            infoId = entry.getDefaultFileId();
090        } else {
091            infoId = entry.getId();
092        }
093        Set<CmsUUID> idsForPropertyConfig = new HashSet<CmsUUID>();
094        if (entry.getDefaultFileId() != null) {
095            idsForPropertyConfig.add(entry.getDefaultFileId());
096        }
097        if (entry.getId() != null) {
098            idsForPropertyConfig.add(entry.getId());
099        }
100        final List<CmsUUID> propertyConfigIds = new ArrayList<CmsUUID>(idsForPropertyConfig);
101
102        CmsRpcAction<CmsListInfoBean> action = new CmsRpcAction<CmsListInfoBean>() {
103
104            @Override
105            public void execute() {
106
107                start(0, true);
108                CmsCoreProvider.getVfsService().getPageInfo(infoId, this);
109            }
110
111            @Override
112            protected void onResponse(CmsListInfoBean infoResult) {
113
114                stop(false);
115                final CmsEditEntryHandler handler = new CmsEditEntryHandler(
116                    controller,
117                    entry,
118                    CmsSitemapView.getInstance().isNavigationMode());
119                handler.setPageInfo(infoResult);
120
121                CmsRpcAction<Map<CmsUUID, Map<String, CmsXmlContentProperty>>> propertyAction = new CmsRpcAction<Map<CmsUUID, Map<String, CmsXmlContentProperty>>>() {
122
123                    @Override
124                    public void execute() {
125
126                        start(0, true);
127                        CmsCoreProvider.getVfsService().getDefaultProperties(propertyConfigIds, this);
128                    }
129
130                    @Override
131                    protected void onResponse(Map<CmsUUID, Map<String, CmsXmlContentProperty>> propertyResult) {
132
133                        stop(false);
134                        Map<String, CmsXmlContentProperty> propConfig = new LinkedHashMap<String, CmsXmlContentProperty>();
135                        for (Map<String, CmsXmlContentProperty> defaultProps : propertyResult.values()) {
136                            propConfig.putAll(defaultProps);
137                        }
138                        propConfig.putAll(CmsSitemapView.getInstance().getController().getData().getProperties());
139                        A_CmsPropertyEditor editor = createEntryEditor(handler, propConfig);
140                        editor.setPropertyNames(
141                            CmsSitemapView.getInstance().getController().getData().getAllPropertyNames());
142                        final CmsFormDialog dialog = new CmsFormDialog(handler.getDialogTitle(), editor.getForm());
143                        CmsPropertyDefinitionButton defButton = new CmsPropertyDefinitionButton() {
144
145                            /**
146                             * @see org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton#onBeforeEditPropertyDefinition()
147                             */
148                            @Override
149                            public void onBeforeEditPropertyDefinition() {
150
151                                dialog.hide();
152                            }
153
154                        };
155                        defButton.getElement().getStyle().setFloat(Style.Float.LEFT);
156                        defButton.installOnDialog(dialog);
157                        CmsDialogFormHandler formHandler = new CmsDialogFormHandler();
158                        formHandler.setDialog(dialog);
159                        I_CmsFormSubmitHandler submitHandler = new CmsPropertySubmitHandler(handler);
160                        formHandler.setSubmitHandler(submitHandler);
161                        dialog.setFormHandler(formHandler);
162                        editor.initializeWidgets(dialog);
163                        dialog.centerHorizontally(50);
164                        dialog.catchNotifications();
165                        String noEditReason = controller.getNoEditReason(entry);
166                        if (noEditReason != null) {
167                            editor.disableInput(noEditReason);
168                            dialog.getOkButton().disable(noEditReason);
169                        }
170
171                    }
172                };
173                propertyAction.execute();
174            }
175
176        };
177        action.execute();
178    }
179
180    /**
181     * @see org.opencms.ade.sitemap.client.hoverbar.A_CmsSitemapMenuEntry#onShow()
182     */
183    @Override
184    public void onShow() {
185
186        CmsClientSitemapEntry entry = getHoverbar().getEntry();
187        boolean show = getHoverbar().getController().isEditable()
188            && (entry != null)
189            && (!CmsSitemapView.getInstance().isGalleryMode()
190                || getHoverbar().getController().getData().isGalleryManager());
191        setVisible(show);
192    }
193
194    /**
195     * Creates the right sitemap entry editor for the current mode.<p>
196     *
197     * @param handler the entry editor handler
198     * @param  propConfig the property configuration to use
199     *
200     * @return a sitemap entry editor instance
201     */
202    protected A_CmsPropertyEditor createEntryEditor(
203        I_CmsPropertyEditorHandler handler,
204        Map<String, CmsXmlContentProperty> propConfig) {
205
206        if (CmsSitemapView.getInstance().isNavigationMode()) {
207            return new CmsNavModePropertyEditor(propConfig, handler);
208        } else {
209            boolean isFolder = handler.isFolder();
210            CmsVfsModePropertyEditor result = new CmsVfsModePropertyEditor(propConfig, handler);
211            result.setShowResourceProperties(!isFolder);
212            return result;
213        }
214    }
215}