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.A_CmsEntryPoint;
031import org.opencms.gwt.client.CmsCoreProvider;
032import org.opencms.gwt.client.util.CmsDebugLog;
033
034import java.util.HashSet;
035import java.util.Set;
036
037import com.google.gwt.core.client.JavaScriptObject;
038import com.google.gwt.core.client.JsArrayString;
039import com.google.gwt.core.client.Scheduler;
040import com.google.gwt.core.client.Scheduler.ScheduledCommand;
041import com.vaadin.client.ResourceLoader;
042import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
043import com.vaadin.client.ResourceLoader.ResourceLoadListener;
044import com.vaadin.client.WidgetUtil;
045
046/**
047 * Entry point class for the OpenCms standard widgetset.<p>
048 */
049public class CmsWidgetSetEntryPoint extends A_CmsEntryPoint {
050
051    /**
052     * Loads JavaScript resources into the window context.<p>
053     *
054     * @param dependencies the dependencies to load
055     * @param callback the callback to execute once the resources are loaded
056     */
057    public static void loadScriptDependencies(final JsArrayString dependencies, final JavaScriptObject callback) {
058
059        if (dependencies.length() == 0) {
060            return;
061        }
062        final Set<String> absoluteUris = new HashSet<String>();
063        for (int i = 0; i < dependencies.length(); i++) {
064            absoluteUris.add(WidgetUtil.getAbsoluteUrl(dependencies.get(i)));
065        }
066        // Listener that loads the next when one is completed
067        ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
068
069            @Override
070            public void onError(ResourceLoadEvent event) {
071
072                CmsDebugLog.consoleLog(event.getResourceUrl() + " could not be loaded.");
073                // The show must go on
074                absoluteUris.remove(event.getResourceUrl());
075                if (absoluteUris.isEmpty()) {
076                    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
077
078                        public void execute() {
079
080                            callNativeFunction(callback);
081                        }
082                    });
083                }
084
085            }
086
087            @Override
088            public void onLoad(ResourceLoadEvent event) {
089
090                absoluteUris.remove(event.getResourceUrl());
091                if (absoluteUris.isEmpty()) {
092                    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
093
094                        public void execute() {
095
096                            callNativeFunction(callback);
097                        }
098                    });
099
100                }
101            }
102        };
103
104        ResourceLoader loader = ResourceLoader.get();
105        for (int i = 0; i < dependencies.length(); i++) {
106            String preloadUrl = dependencies.get(i);
107            loader.loadScript(preloadUrl, resourceLoadListener);
108        }
109    }
110
111    /**
112     * Calls the native function.<p>
113     *
114     * @param callback the function to call
115     */
116    static native void callNativeFunction(JavaScriptObject callback)/*-{
117        callback.call();
118    }-*/;
119
120    /**
121     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
122     */
123    @Override
124    public void onModuleLoad() {
125
126        super.onModuleLoad();
127        exportUtitlityFunctions();
128        try {
129            String tinyMCE = CmsCoreProvider.get().getTinymce().getLink();
130            if (tinyMCE == null) {
131                CmsDebugLog.consoleLog("tinyMCE link is null");
132            } else {
133                ResourceLoader.get().loadScript(tinyMCE, new ResourceLoadListener() {
134
135                    public void onError(ResourceLoadEvent event) {
136
137                        CmsDebugLog.consoleLog("error loading TinyMCE");
138                    }
139
140                    public void onLoad(ResourceLoadEvent event) {
141                        // ignore
142
143                    }
144                });
145            }
146        } catch (Exception e) {
147            //
148        }
149    }
150
151    /**
152     * Exports utility methods to the window context.<p>
153     */
154    private native void exportUtitlityFunctions()/*-{
155        $wnd.cmsLoadScripts = function(scriptURIs, callback) {
156            @org.opencms.ui.client.CmsWidgetSetEntryPoint::loadScriptDependencies(Lcom/google/gwt/core/client/JsArrayString;Lcom/google/gwt/core/client/JavaScriptObject;)(scriptURIs, callback);
157        }
158        $wnd.cmsSafeLoadCSS = function(cssURIs, callback) {
159            @org.opencms.gwt.client.util.CmsDomUtil::safeLoadStylesheets([Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)(cssURIs, callback);
160        }
161        $wnd.cmsLoadCSS = function(cssURIs) {
162            for (i = 0; i < cssURIs.length; i++) {
163                @org.opencms.gwt.client.util.CmsDomUtil::ensureStyleSheetIncluded(Ljava/lang/String;)(cssURIs[i]);
164            }
165        }
166    }-*/;
167
168}