001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.properties.client;
029
030import org.opencms.ade.properties.shared.I_CmsAdePropertiesConstants;
031import org.opencms.gwt.client.A_CmsEntryPoint;
032import org.opencms.gwt.client.CmsCoreProvider;
033import org.opencms.gwt.client.property.CmsSimplePropertyEditorHandler;
034import org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton;
035import org.opencms.gwt.client.property.definition.CmsPropertyDefinitionDialog;
036import org.opencms.gwt.client.rpc.CmsRpcAction;
037import org.opencms.gwt.client.ui.CmsErrorDialog;
038import org.opencms.gwt.client.ui.contextmenu.CmsEditProperties;
039import org.opencms.gwt.client.ui.contextmenu.CmsEditProperties.PropertyEditingContext;
040import org.opencms.util.CmsUUID;
041
042import java.util.ArrayList;
043
044import com.google.gwt.core.client.Scheduler;
045import com.google.gwt.event.logical.shared.CloseEvent;
046import com.google.gwt.event.logical.shared.CloseHandler;
047import com.google.gwt.user.client.Window;
048import com.google.gwt.user.client.ui.PopupPanel;
049
050/**
051 * Entry point class for the standalone ADE properties dialog.<p>
052 */
053public class CmsPropertiesEntryPoint extends A_CmsEntryPoint {
054
055    /**
056     * Property editor handler for the standalone dialog.<p>
057     */
058    static class PropertyEditorHandler extends CmsSimplePropertyEditorHandler {
059
060        /**
061         * Default constructor.<p>
062         */
063        public PropertyEditorHandler() {
064
065            super(null);
066        }
067
068        /**
069         * Override: Always return false, since we want to be free to choose any JSP.
070         *
071         * @see org.opencms.gwt.client.property.CmsSimplePropertyEditorHandler#useAdeTemplates()
072         */
073        @Override
074        public boolean useAdeTemplates() {
075
076            return false;
077        }
078    }
079
080    /** The link to open after editing the properties / property definition is finished. */
081    protected String m_closeLink;
082
083    /** Flag which indicates that the property definition dialog needs to be opened. */
084    protected boolean m_needsPropertyDefinitionDialog;
085
086    /**
087     * @see org.opencms.gwt.client.A_CmsEntryPoint#onModuleLoad()
088     */
089    @Override
090    public void onModuleLoad() {
091
092        super.onModuleLoad();
093        m_closeLink = CmsCoreProvider.getMetaElementContent(I_CmsAdePropertiesConstants.META_BACKLINK);
094        String resource = CmsCoreProvider.getMetaElementContent(I_CmsAdePropertiesConstants.META_RESOURCE);
095        editProperties(new CmsUUID(resource));
096    }
097
098    /**
099     * Starts the property editor for the resource with the given structure id.<p>
100     *
101     * @param structureId the structure id of a resource
102     */
103    protected void editProperties(final CmsUUID structureId) {
104
105        CmsEditProperties.editProperties(structureId, null, false, null, false, new PropertyEditingContext() {
106
107            @Override
108            public CmsPropertyDefinitionButton createPropertyDefinitionButton() {
109
110                CmsPropertyDefinitionButton button = new CmsPropertyDefinitionButton() {
111
112                    /**
113                     * @see org.opencms.gwt.client.property.definition.CmsPropertyDefinitionButton#onBeforeEditPropertyDefinition()
114                     */
115                    @SuppressWarnings("synthetic-access")
116                    @Override
117                    public void onBeforeEditPropertyDefinition() {
118
119                        m_needsPropertyDefinitionDialog = true;
120                        m_formDialog.hide();
121                    }
122
123                    @Override
124                    public void onClosePropertyDefinitionDialog() {
125
126                        closeDelayed();
127                    }
128                };
129                return button;
130            }
131
132            @Override
133            public void initCloseHandler() {
134
135                m_formDialog.addCloseHandler(new CloseHandler<PopupPanel>() {
136
137                    public void onClose(CloseEvent<PopupPanel> event) {
138
139                        onClosePropertyDialog();
140                    }
141
142                });
143            }
144
145        });
146    }
147
148    /**
149     * Opens the dialog for creating new property definitions.<p>
150     */
151    protected void editPropertyDefinition() {
152
153        CmsRpcAction<ArrayList<String>> action = new CmsRpcAction<ArrayList<String>>() {
154
155            @Override
156            public void execute() {
157
158                start(200, true);
159                CmsCoreProvider.getVfsService().getDefinedProperties(this);
160            }
161
162            @Override
163            protected void onResponse(ArrayList<String> result) {
164
165                stop(false);
166                CmsPropertyDefinitionDialog dialog = new CmsPropertyDefinitionDialog(result);
167                dialog.center();
168                dialog.addCloseHandler(new CloseHandler<PopupPanel>() {
169
170                    public void onClose(CloseEvent<PopupPanel> event) {
171
172                        onClosePropertyDefinitionDialog();
173                    }
174
175                });
176            }
177
178        };
179        action.execute();
180    }
181
182    /**
183     * This method is called after the property definition dialog is closed.<p>
184     */
185    protected void onClosePropertyDefinitionDialog() {
186
187        closeDelayed();
188    }
189
190    /**
191     * This method is called after the property dialog is closed.<p>
192     */
193    protected void onClosePropertyDialog() {
194
195        if (!m_needsPropertyDefinitionDialog) {
196            closeDelayed();
197        }
198    }
199
200    /**
201     * Returns to the close link after a short delay.<p>
202     */
203    void closeDelayed() {
204
205        Scheduler.RepeatingCommand command = new Scheduler.RepeatingCommand() {
206
207            public boolean execute() {
208
209                if (CmsErrorDialog.isShowingErrorDialogs()) {
210                    return true;
211                } else {
212                    Window.Location.assign(m_closeLink);
213                    return false;
214                }
215
216            }
217        };
218        Scheduler.get().scheduleFixedDelay(command, 300);
219    }
220
221}