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.ui.client;
029
030import org.opencms.gwt.client.property.I_CmsPropertySaver;
031import org.opencms.gwt.client.rpc.CmsRpcPrefetcher;
032import org.opencms.gwt.client.ui.contenteditor.I_CmsContentEditorHandler;
033import org.opencms.gwt.client.ui.contextmenu.CmsEditProperties;
034import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand;
035import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler;
036import org.opencms.gwt.client.util.I_CmsSimpleCallback;
037import org.opencms.gwt.shared.CmsGwtConstants;
038import org.opencms.gwt.shared.property.CmsPropertiesBean;
039import org.opencms.gwt.shared.property.CmsPropertyChangeSet;
040import org.opencms.gwt.shared.rpc.I_CmsVfsService;
041import org.opencms.ui.components.extensions.CmsPropertyDialogExtension;
042import org.opencms.ui.shared.rpc.I_CmsPropertyClientRpc;
043import org.opencms.ui.shared.rpc.I_CmsPropertyServerRpc;
044import org.opencms.util.CmsUUID;
045
046import java.util.List;
047import java.util.Map;
048
049import com.google.common.collect.Lists;
050import com.google.gwt.core.client.GWT;
051import com.google.gwt.user.client.rpc.AsyncCallback;
052import com.google.gwt.user.client.rpc.SerializationException;
053import com.google.gwt.user.client.rpc.SerializationStreamFactory;
054import com.google.gwt.user.client.rpc.SerializationStreamWriter;
055import com.vaadin.client.ServerConnector;
056import com.vaadin.client.extensions.AbstractExtensionConnector;
057import com.vaadin.shared.ui.Connect;
058
059/**
060 * Connector for the  property dialog extension.
061 */
062@Connect(CmsPropertyDialogExtension.class)
063public class CmsPropertyDialogExtensionConnector extends AbstractExtensionConnector
064implements I_CmsPropertyClientRpc, CmsEditProperties.I_MultiFileNavigation {
065
066    /**
067     * Context menu handler.<p>
068     */
069    public class ContextMenuHandler implements I_CmsContextMenuHandler {
070
071        /**
072         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#ensureLockOnResource(org.opencms.util.CmsUUID, org.opencms.gwt.client.util.I_CmsSimpleCallback)
073         */
074        public void ensureLockOnResource(CmsUUID structureId, I_CmsSimpleCallback<Boolean> callback) {
075
076            notImplemented();
077        }
078
079        /**
080         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextMenuCommands()
081         */
082        public Map<String, I_CmsContextMenuCommand> getContextMenuCommands() {
083
084            notImplemented();
085            return null;
086        }
087
088        /**
089         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextType()
090         */
091        public String getContextType() {
092
093            return CmsGwtConstants.CONTEXT_TYPE_FILE_TABLE;
094        }
095
096        /**
097         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getEditorHandler()
098         */
099        public I_CmsContentEditorHandler getEditorHandler() {
100
101            notImplemented();
102            return null;
103        }
104
105        /**
106         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#leavePage(java.lang.String)
107         */
108        public void leavePage(String targetUri) {
109
110            notImplemented();
111        }
112
113        /**
114         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#onSiteOrProjectChange(java.lang.String, java.lang.String)
115         */
116        public void onSiteOrProjectChange(String sitePath, String serverLink) {
117
118            notImplemented();
119        }
120
121        /**
122         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#refreshResource(org.opencms.util.CmsUUID)
123         */
124        public void refreshResource(CmsUUID structureId) {
125
126            List<String> changed = Lists.newArrayList();
127            changed.add("" + structureId);
128            m_changed = changed;
129            close(100);
130        }
131
132        /**
133         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#unlockResource(org.opencms.util.CmsUUID)
134         */
135        public void unlockResource(CmsUUID structureId) {
136
137            notImplemented();
138        }
139
140        /**
141         * Throws an illegal state exception for not implemented methods.<p>
142         */
143        private void notImplemented() {
144
145            throw new IllegalStateException("Not implemented");
146        }
147    }
148
149    /**
150     * The property saver.<p>
151     */
152    public class PropertySaver implements I_CmsPropertySaver {
153
154        /**
155         * @see org.opencms.gwt.client.property.I_CmsPropertySaver#saveProperties(org.opencms.gwt.shared.property.CmsPropertyChangeSet, com.google.gwt.user.client.rpc.AsyncCallback)
156         */
157        @SuppressWarnings("synthetic-access")
158        public void saveProperties(CmsPropertyChangeSet changes, AsyncCallback<Void> callback) {
159
160            try {
161                m_propertySaveCallback = callback;
162                SerializationStreamFactory streamFactory = (SerializationStreamFactory)GWT.create(
163                    I_CmsVfsService.class);
164                SerializationStreamWriter streamWriter = streamFactory.createStreamWriter();
165                streamWriter.writeObject(changes);
166                String serializedData = streamWriter.toString();
167                getRpcProxy(I_CmsPropertyServerRpc.class).savePropertiesForNewResource(serializedData);
168            } catch (Exception e) {
169                throw new RuntimeException(e);
170            }
171
172        }
173
174    }
175
176    /** Serial version id. */
177    private static final long serialVersionUID = 1L;
178
179    /** Changed ids. */
180    protected List<String> m_changed = Lists.newArrayList();
181
182    /** Callback to be called after the properties have been saved. */
183    protected AsyncCallback<Void> m_propertySaveCallback;
184
185    /** Stored callback. */
186    private AsyncCallback<CmsUUID> m_currentCallback;
187
188    /**
189     * @see org.opencms.ui.shared.rpc.I_CmsPropertyClientRpc#confirmSaveForNew()
190     */
191    public void confirmSaveForNew() {
192
193        m_propertySaveCallback.onSuccess(null);
194    }
195
196    /**
197     * @see org.opencms.ui.shared.rpc.I_CmsPropertyClientRpc#editProperties(java.lang.String, boolean, boolean)
198     */
199    public void editProperties(String editStructureId, boolean editName, boolean disablePrevNext) {
200
201        CmsEditProperties.PropertyEditingContext context = new CmsEditProperties.PropertyEditingContext();
202        if (!disablePrevNext) {
203            context.setMultiFileNavigation(this);
204        }
205        CmsEditProperties.editPropertiesWithFileNavigation(
206            new CmsUUID(editStructureId),
207            new ContextMenuHandler(),
208            editName,
209            new Runnable() {
210
211                public void run() {
212
213                    // nop
214                }
215            },
216            false,
217            context,
218            null);
219
220    }
221
222    /**
223     * @see org.opencms.ui.shared.rpc.I_CmsPropertyClientRpc#editPropertiesForNewResource(java.lang.String)
224     */
225    public void editPropertiesForNewResource(String propertyDataString) {
226
227        try {
228            CmsVUI.clearStoredFocusForCurrentInstance(); // we need to control the focus for ourselves in the property dialog
229            CmsPropertiesBean propData = (CmsPropertiesBean)(CmsRpcPrefetcher.getSerializedObjectFromString(
230                GWT.create(I_CmsVfsService.class),
231                propertyDataString));
232            CmsEditProperties.PropertyEditingContext context = new CmsEditProperties.PropertyEditingContext();
233            context.setPropertySaver(new PropertySaver());
234            context.setAllowCreateProperties(false);
235            context.setFocusNameField(true);
236            CmsEditProperties.openPropertyDialog(propData, new ContextMenuHandler(), true, new Runnable() {
237
238                @SuppressWarnings("synthetic-access")
239                public void run() {
240
241                    getRpcProxy(I_CmsPropertyServerRpc.class).removeExtension();
242                }
243            }, false, context);
244
245        } catch (SerializationException e) {
246            throw new RuntimeException(e);
247        }
248
249    }
250
251    /**
252     * @see org.opencms.gwt.client.ui.contextmenu.CmsEditProperties.I_MultiFileNavigation#requestNextFile(int, com.google.gwt.user.client.rpc.AsyncCallback)
253     */
254    public void requestNextFile(int offset, AsyncCallback<CmsUUID> callback) {
255
256        m_currentCallback = callback;
257        getRpcProxy(I_CmsPropertyServerRpc.class).requestNextFile(offset);
258
259    }
260
261    /**
262     * @see org.opencms.ui.shared.rpc.I_CmsPropertyClientRpc#sendNextId(java.lang.String)
263     */
264    public void sendNextId(String id) {
265
266        if (m_currentCallback != null) {
267            m_currentCallback.onSuccess(new CmsUUID(id));
268            m_currentCallback = null;
269        }
270    }
271
272    /**
273     * Disposes of the extension on the server side and notifies the server of which resources have been changed.<p>
274     *
275     * @param delayMillis the time to wait on the server before refreshing the view
276     */
277    protected void close(long delayMillis) {
278
279        getRpcProxy(I_CmsPropertyServerRpc.class).onClose(delayMillis);
280    }
281
282    /**
283     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
284     */
285    @Override
286    protected void extend(ServerConnector target) {
287
288        registerRpc(I_CmsPropertyClientRpc.class, this);
289    }
290
291}