Search in sources :

Example 1 with Duration

use of com.google.gwt.core.client.Duration in project flow by vaadin.

the class ResourceLoader method loadStylesheet.

/**
 * Load a stylesheet and notify a listener when the stylesheet is loaded.
 * Calling this method when the stylesheet is currently loading or already
 * loaded doesn't cause the stylesheet to be loaded again, but the listener
 * will still be notified when appropriate.
 *
 * @param stylesheetUrl
 *            the url of the stylesheet to load
 * @param resourceLoadListener
 *            the listener that will get notified when the stylesheet is
 *            loaded
 */
public void loadStylesheet(final String stylesheetUrl, final ResourceLoadListener resourceLoadListener) {
    final String url = WidgetUtil.getAbsoluteUrl(stylesheetUrl);
    final ResourceLoadEvent event = new ResourceLoadEvent(this, url);
    if (loadedResources.has(url)) {
        if (resourceLoadListener != null) {
            resourceLoadListener.onLoad(event);
        }
        return;
    }
    if (addListener(url, resourceLoadListener, loadListeners)) {
        LinkElement linkElement = getDocument().createLinkElement();
        linkElement.setRel("stylesheet");
        linkElement.setType("text/css");
        linkElement.setHref(url);
        if (BrowserInfo.get().isSafariOrIOS()) {
            // Safari doesn't fire any events for link elements
            // See http://www.phpied.com/when-is-a-stylesheet-really-loaded/
            Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {

                private final Duration duration = new Duration();

                @Override
                public boolean execute() {
                    int styleSheetLength = getStyleSheetLength(url);
                    if (getStyleSheetLength(url) > 0) {
                        fireLoad(event);
                        // Stop repeating
                        return false;
                    } else if (styleSheetLength == 0) {
                        // "Loaded" empty sheet -> most likely 404 error
                        fireError(event);
                        return true;
                    } else if (duration.elapsedMillis() > 60 * 1000) {
                        fireError(event);
                        return false;
                    } else {
                        // Continue repeating
                        return true;
                    }
                }
            }, 10);
        } else {
            addOnloadHandler(linkElement, new StyleSheetLoadListener(url), event);
            if (BrowserInfo.get().isOpera()) {
                // Opera onerror never fired, assume error if no onload in x
                // seconds
                new Timer() {

                    @Override
                    public void run() {
                        if (!loadedResources.has(url)) {
                            fireError(event);
                        }
                    }
                }.schedule(5 * 1000);
            }
        }
        getHead().appendChild(linkElement);
    }
}
Also used : Timer(com.google.gwt.user.client.Timer) LinkElement(elemental.html.LinkElement) RepeatingCommand(com.google.gwt.core.client.Scheduler.RepeatingCommand) Duration(com.google.gwt.core.client.Duration)

Aggregations

Duration (com.google.gwt.core.client.Duration)1 RepeatingCommand (com.google.gwt.core.client.Scheduler.RepeatingCommand)1 Timer (com.google.gwt.user.client.Timer)1 LinkElement (elemental.html.LinkElement)1