All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geomajas.gwt2.client.gfx.HtmlImageImpl Maven / Gradle / Ivy

/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.gwt2.client.gfx;

import org.geomajas.geometry.Bbox;
import org.geomajas.gwt.client.util.Dom;

import com.google.gwt.core.client.Callback;
import com.google.gwt.event.dom.client.ErrorEvent;
import com.google.gwt.event.dom.client.ErrorHandler;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Image;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

/**
 * 

* Extension of an HtmlObject that represents a single HTML image (IMG tag). Next to the default values provided by the * HtmlObject, an extra 'src' field is provided. This string value should point to the actual image. *

*

* Instances of this class can be initiated with a {@link Callback} that is notified when the image is done loading. The * image is done loading when it has either loaded successfully or when 5 attempts have failed. In any case, the * callback's execute method will be invoked, thereby indicating success or failure. *

* * @author Pieter De Graef */ public class HtmlImageImpl extends AbstractHtmlObject implements HtmlImage { // ------------------------------------------------------------------------ // Constructors: // ------------------------------------------------------------------------ /** * Create an HtmlImage widget that represents an HTML IMG element. * * @param src Pointer to the actual image. * @param bbox The bounding box of the image. */ @AssistedInject HtmlImageImpl(@Assisted String src, @Assisted Bbox bbox) { this(src, bbox, null); } /** * Create an HtmlImage widget that represents an HTML IMG element. * * @param src Pointer to the actual image. * @param bbox The bounding box of the image. * @param onLoadingDone Call-back to be executed when the image finished loading, or when an error occurs while * loading. */ @AssistedInject HtmlImageImpl(@Assisted String src, @Assisted Bbox bbox, @Assisted Callback onLoadingDone) { this(src, bbox, onLoadingDone, 0); } /** * Create an HtmlImage widget that represents an HTML IMG element. * * @param src Pointer to the actual image. * @param bbox The bounding box of the image. * @param onLoadingDone Call-back to be executed when the image finished loading, or when an error occurs while * loading. * @param nrRetries Total number of retries should loading fail. Default is 0. */ @AssistedInject HtmlImageImpl(@Assisted String src, @Assisted Bbox bbox, @Assisted Callback onLoadingDone, @Assisted int nrRetries) { this(src, (int) bbox.getWidth(), (int) bbox.getHeight(), (int) bbox.getY(), (int) bbox.getX(), onLoadingDone, nrRetries); } /** * Create an HtmlImage widget that represents an HTML IMG element. * * @param src Pointer to the actual image. * @param width The width for this image, expressed in pixels. * @param height The height for this image, expressed in pixels. * @param top How many pixels should this image be placed from the top (relative to the parent origin). * @param left How many pixels should this image be placed from the left (relative to the parent origin). */ public HtmlImageImpl(String src, int width, int height, int top, int left) { this(src, width, height, top, left, null); } /** * Create an HtmlImage widget that represents an HTML IMG element. * * @param src Pointer to the actual image. * @param width The width for this image, expressed in pixels. * @param height The height for this image, expressed in pixels. * @param top How many pixels should this image be placed from the top (relative to the parent origin). * @param left How many pixels should this image be placed from the left (relative to the parent origin). * @param onLoadingDone Call-back to be executed when the image finished loading, or when an error occurs while * loading. */ public HtmlImageImpl(String src, int width, int height, int top, int left, Callback onLoadingDone) { this(src, width, height, top, left, onLoadingDone, 0); } /** * Create an HtmlImage widget that represents an HTML IMG element. * * @param src Pointer to the actual image. * @param width The width for this image, expressed in pixels. * @param height The height for this image, expressed in pixels. * @param top How many pixels should this image be placed from the top (relative to the parent origin). * @param left How many pixels should this image be placed from the left (relative to the parent origin). * @param onLoadingDone Call-back to be executed when the image finished loading, or when an error occurs while * loading. * @param nrRetries Total number of retries should loading fail. Default is 0. */ public HtmlImageImpl(String src, int width, int height, int top, int left, Callback onLoadingDone, int nrRetries) { super(new Image(Dom.makeUrlAbsolute(src))); setWidth(width); setHeight(height); setTop(top); setLeft(left); DOM.setStyleAttribute(asWidget().getElement(), "border", "none"); // set visible when loaded ! setVisible(false); onLoadingDone(onLoadingDone, nrRetries); } // ------------------------------------------------------------------------ // HtmlImage implementation: // ------------------------------------------------------------------------ /** * Apply a call-back that is executed when the image is done loading. This image is done loading when it has either * loaded successfully or when 5 attempts have failed. In any case, the callback's execute method will be invoked, * thereby indicating success or failure. * * @param onLoadingDone The call-back to be executed when loading has finished. The boolean value indicates whether * or not it was successful while loading. Both the success and failure type expect a String. This is used to * pass along the image URL. * @param nrRetries Total number of retries should loading fail. Default is 0. */ public void onLoadingDone(Callback onLoadingDone, int nrRetries) { ImageReloader reloader = new ImageReloader(getSrc(), onLoadingDone, nrRetries); asImage().addLoadHandler(reloader); asImage().addErrorHandler(reloader); } /** * Get the pointer to the actual image. In HTML this is represented by the 'src' attribute in an IMG element. * * @return The pointer to the actual image. */ public String getSrc() { return asImage().getUrl(); } /** * Set the pointer to the actual image. In HTML this is represented by the 'src' attribute in an IMG element. * * @param src The new image pointer. */ public void setSrc(String src) { asImage().setUrl(src); } protected Image asImage() { return (Image) asWidget(); } /** * DOM event handler that attempts up to 5 times to reload the requested image. When the image is loaded (or the 5 * attempts have failed), it notifies the given {@link Callback}, calling the execute method with true or false * indicating whether or not the image was really loaded. * * @author Pieter De Graef */ public class ImageReloader implements LoadHandler, ErrorHandler { private int nrAttempts = 5; private String src; private Callback onDoneLoading; public ImageReloader(String src, Callback onDoneLoading, int nrRetries) { this.src = src; this.onDoneLoading = onDoneLoading; this.nrAttempts = nrRetries + 1; } public void onLoad(LoadEvent event) { setVisible(true); if (onDoneLoading != null) { onDoneLoading.onSuccess(src); } } public void onError(ErrorEvent event) { nrAttempts--; if (nrAttempts > 0) { asImage().addLoadHandler(this); asImage().addErrorHandler(this); asImage().setUrl(src); } else if (onDoneLoading != null) { onDoneLoading.onFailure(src); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy