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