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
072        super();
073        registerRpc(I_CmsSitemapClientRpc.class, this);
074        INSTANCE = this;
075    }
076
077    /**
078     * Gets the static instance of this class.<p>
079     *
080     * @return the instance
081     */
082    public static CmsSitemapExtensionConnector get() {
083
084        return INSTANCE;
085    }
086
087    /**
088     * @see org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc#finishPageCopyDialog(java.lang.String, java.lang.String)
089     */
090    public void finishPageCopyDialog(String callId, String response) {
091
092        m_callbacks.get(callId).onSuccess(response);
093        m_callbacks.remove(callId);
094    }
095
096    /**
097     * Installs the native JavaScript functions used to access Vaadin functionality from the sitemap editor's GWT module.<p>
098     */
099    public native void installNativeFunctions() /*-{
100                                                var self = this;
101                                                $wnd.cmsOpenPageCopyDialog = function(id, callback) {
102                                                self.@org.opencms.ui.client.CmsSitemapExtensionConnector::openPageCopyDialog(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(id , callback);
103                                                }
104                                                }-*/;
105
106    /**
107     * Opens the page copy dialog.<p>
108     *
109     * @param structureId the structure id of the resource for which to open the dialog
110     * @param callback the callback to call with the result when the dialog has finished
111     */
112    public void openPageCopyDialog(String structureId, AsyncCallback<String> callback) {
113
114        String callId = "" + CALL_COUNTER++;
115        m_callbacks.put(callId, callback);
116        getRpcProxy(I_CmsSitemapServerRpc.class).openPageCopyDialog(callId, "" + structureId);
117
118    }
119
120    /**
121     * Opens the page copy dialog.<p>
122     *
123     * @param id the structure id of the resource for which to open the dialog
124     * @param callback the native callback to call with the result when the dialog has finished
125     */
126    public void openPageCopyDialog(String id, JavaScriptObject callback) {
127
128        openPageCopyDialog(id, CmsJsUtil.wrapCallback(callback));
129    }
130
131    /**
132     * @see org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc#openPropertyDialog(java.lang.String, java.lang.String)
133     */
134    public void openPropertyDialog(String structureIdStr, String rootIdStr) {
135
136        CmsJsUtil.callNamedFunctionWithString2(
137            CmsGwtConstants.LOCALECOMPARE_EDIT_PROPERTIES,
138            structureIdStr,
139            rootIdStr);
140    }
141
142    /**
143     * @see org.opencms.ui.shared.rpc.I_CmsSitemapClientRpc#showInfoHeader(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
144     */
145    public void showInfoHeader(String title, String description, String path, String locale, String typeIcon) {
146
147        CmsInfoHeader header = new CmsInfoHeader(title, description, path, locale, typeIcon);
148        RootPanel panel = RootPanel.get(CmsGwtConstants.ID_LOCALE_HEADER_CONTAINER);
149        if (panel == null) {
150            CmsDebugLog.consoleLog("no element #locale-header-container found");
151        } else {
152            if (panel.getWidgetCount() > 0) {
153                panel.remove(0);
154            }
155            panel.add(header);
156        }
157    }
158
159    /**
160     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
161     */
162    @Override
163    protected void extend(ServerConnector target) {
164
165        installNativeFunctions();
166        CmsScriptCallbackHelper callbackHelper = new CmsScriptCallbackHelper() {
167
168            @Override
169            @SuppressWarnings("synthetic-access")
170            public void run() {
171
172                JsArrayString arguments = m_arguments.cast();
173                getRpcProxy(I_CmsSitemapServerRpc.class).showLocaleComparison(arguments.get(0));
174
175            }
176        };
177        callbackHelper.installCallbackOnWindow(CmsGwtConstants.CALLBACK_REFRESH_LOCALE_COMPARISON);
178
179        CmsScriptCallbackHelper propertySaveHelper = new CmsScriptCallbackHelper() {
180
181            @SuppressWarnings("synthetic-access")
182            @Override
183            public void run() {
184
185                JsArrayString arguments = m_arguments.cast();
186                String id = arguments.get(0);
187                getRpcProxy(I_CmsSitemapServerRpc.class).handleChangedProperties(id);
188            }
189
190        };
191        propertySaveHelper.installCallbackOnWindow(CmsGwtConstants.CALLBACK_HANDLE_CHANGED_PROPERTIES);
192
193    }
194
195}