Example usage for com.vaadin.client ResourceLoader get

List of usage examples for com.vaadin.client ResourceLoader get

Introduction

In this page you can find the example usage for com.vaadin.client ResourceLoader get.

Prototype

public static ResourceLoader get() 

Source Link

Document

Returns the default ResourceLoader.

Usage

From source file:org.opencms.ui.client.CmsWidgetSetEntryPoint.java

License:Open Source License

/**
 * Loads JavaScript resources into the window context.<p>
 *
 * @param dependencies the dependencies to load
 * @param callback the callback to execute once the resources are loaded
 *//*from  w  w w .  j  a  v a  2s.com*/
public static void loadScriptDependencies(final JsArrayString dependencies, final JavaScriptObject callback) {

    if (dependencies.length() == 0) {
        return;
    }

    // Listener that loads the next when one is completed
    ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {

        @Override
        public void onError(ResourceLoadEvent event) {

            CmsDebugLog.consoleLog(event.getResourceUrl() + " could not be loaded.");
            // The show must go on
            onLoad(event);
        }

        @Override
        public void onLoad(ResourceLoadEvent event) {

            if (dependencies.length() != 0) {
                String url = dependencies.shift();
                // Load next in chain (hopefully already preloaded)
                event.getResourceLoader().loadScript(url, this);
            } else {
                // finished loading dependencies
                callNativeFunction(callback);
            }
        }
    };

    ResourceLoader loader = ResourceLoader.get();

    // Start chain by loading first
    String url = dependencies.shift();
    loader.loadScript(url, resourceLoadListener);
    if (ResourceLoader.supportsInOrderScriptExecution()) {
        for (int i = 0; i < dependencies.length(); i++) {
            String preloadUrl = dependencies.get(i);
            loader.loadScript(preloadUrl, null);
        }
    } else {
        // Preload all remaining
        for (int i = 0; i < dependencies.length(); i++) {
            String preloadUrl = dependencies.get(i);
            loader.preloadResource(preloadUrl, null);
        }
    }
}