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.ui.CmsInfoHeader;
031import org.opencms.gwt.client.util.CmsDebugLog;
032import org.opencms.gwt.client.util.CmsJsUtil;
033import org.opencms.gwt.client.util.CmsScriptCallbackHelper;
034import org.opencms.gwt.shared.CmsGwtConstants;
035import org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc;
036import org.opencms.ui.shared.rpc.I_CmsSitemapServerRpc;
037
038import java.util.Map;
039
040import com.google.common.collect.Maps;
041import com.google.gwt.core.client.JavaScriptObject;
042import com.google.gwt.core.client.JsArrayString;
043import com.google.gwt.user.client.rpc.AsyncCallback;
044import com.google.gwt.user.client.ui.RootPanel;
045import com.vaadin.client.ServerConnector;
046import com.vaadin.client.extensions.AbstractExtensionConnector;
047import com.vaadin.shared.ui.Connect;
048
049/**
050 * Connector class for the Vaadin extension used to embed Vaadin dialogs in the sitemap editor.<p>
051 */
052@Connect(org.opencms.ui.sitemap.CmsSitemapExtension.class)
053public class CmsSitemapExtensionConnector extends AbstractExtensionConnector implements I_CmsSitemapClientRpc {
054
055    /** Counter used to generate unique ids for RPC calls. */
056    public static int CALL_COUNTER = 1;
057
058    /** The singleton instance of this class. */
059    static CmsSitemapExtensionConnector INSTANCE;
060
061    /** Serial version id. */
062    private static final long serialVersionUID = 1L;
063
064    /** Map of callbacks for RPC responses. */
065    private Map<String, AsyncCallback<String>> m_callbacks = Maps.newHashMap();
066
067    /**
068     * Creates a new instance.<p>
069     */
070    public CmsSitemapExtensionConnector() {
071        super();
072        registerRpc(I_CmsSitemapClientRpc.class, this);
073        INSTANCE = this;
074    }
075
076    /**
077     * Gets the static instance of this class.<p>
078     *
079     * @return the instance
080     */
081    public static CmsSitemapExtensionConnector get() {
082
083        return INSTANCE;
084    }
085
086    /**
087     * @see org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc#finishPageCopyDialog(java.lang.String, java.lang.String)
088     */
089    public void finishPageCopyDialog(String callId, String response) {
090
091        m_callbacks.get(callId).onSuccess(response);
092        m_callbacks.remove(callId);
093    }
094
095    /**
096     * Installs the native JavaScript functions used to access Vaadin functionality from the sitemap editor's GWT module.<p>
097     */
098    public native void installNativeFunctions() /*-{
099                                                var self = this;
100                                                $wnd.cmsOpenPageCopyDialog = function(id, callback) {
101                                                self.@org.opencms.ui.client.CmsSitemapExtensionConnector::openPageCopyDialog(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(id , callback);
102                                                }
103                                                }-*/;
104
105    /**
106     * Opens the page copy dialog.<p>
107     *
108     * @param structureId the structure id of the resource for which to open the dialog
109     * @param callback the callback to call with the result when the dialog has finished
110     */
111    public void openPageCopyDialog(String structureId, AsyncCallback<String> callback) {
112
113        String callId = "" + CALL_COUNTER++;
114        m_callbacks.put(callId, callback);
115        getRpcProxy(I_CmsSitemapServerRpc.class).openPageCopyDialog(callId, "" + structureId);
116
117    }
118
119    /**
120     * Opens the page copy dialog.<p>
121     *
122     * @param id the structure id of the resource for which to open the dialog
123     * @param callback the native callback to call with the result when the dialog has finished
124     */
125    public void openPageCopyDialog(String id, JavaScriptObject callback) {
126
127        openPageCopyDialog(id, CmsJsUtil.wrapCallback(callback));
128    }
129
130    /**
131     * @see org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc#openPropertyDialog(java.lang.String, java.lang.String)
132     */
133    public void openPropertyDialog(String structureIdStr, String rootIdStr) {
134
135        CmsJsUtil.callNamedFunctionWithString2(
136            CmsGwtConstants.LOCALECOMPARE_EDIT_PROPERTIES,
137            structureIdStr,
138            rootIdStr);
139    }
140
141    /**
142     * @see org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc#showInfoHeader(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
143     */
144    public void showInfoHeader(String title, String description, String path, String locale, String typeIcon) {
145
146        CmsInfoHeader header = new CmsInfoHeader(title, description, path, locale, typeIcon);
147        RootPanel panel = RootPanel.get(CmsGwtConstants.ID_LOCALE_HEADER_CONTAINER);
148        if (panel == null) {
149            CmsDebugLog.consoleLog("no element #locale-header-container found");
150        } else {
151            if (panel.getWidgetCount() > 0) {
152                panel.remove(0);
153            }
154            panel.add(header);
155        }
156    }
157
158    /**
159     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
160     */
161    @Override
162    protected void extend(ServerConnector target) {
163
164        installNativeFunctions();
165        CmsScriptCallbackHelper callbackHelper = new CmsScriptCallbackHelper() {
166
167            @Override
168            @SuppressWarnings("synthetic-access")
169            public void run() {
170
171                JsArrayString arguments = m_arguments.cast();
172                getRpcProxy(I_CmsSitemapServerRpc.class).showLocaleComparison(arguments.get(0));
173
174            }
175        };
176        callbackHelper.installCallbackOnWindow(CmsGwtConstants.CALLBACK_REFRESH_LOCALE_COMPARISON);
177
178        CmsScriptCallbackHelper propertySaveHelper = new CmsScriptCallbackHelper() {
179
180            @SuppressWarnings("synthetic-access")
181            @Override
182            public void run() {
183
184                JsArrayString arguments = m_arguments.cast();
185                String id = arguments.get(0);
186                getRpcProxy(I_CmsSitemapServerRpc.class).handleChangedProperties(id);
187            }
188
189        };
190        propertySaveHelper.installCallbackOnWindow(CmsGwtConstants.CALLBACK_HANDLE_CHANGED_PROPERTIES);
191
192    }
193
194}