/**
 * 
 */
package org.ajax4jsf.gwt.client.ui;

import org.ajax4jsf.gwt.client.ComponentEntryPoint;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Image;

/**
 * Version of original {@link com.google.gwt.user.client.ui.Image} widget to load
 * images in packed to jar images from module public path. This image will work with
 * same relative url's in hosted mode and as packed component.
 * @author shura
 *
 */
public class JSFImage extends Image {

	/**
	 * 
	 */
	public JSFImage() {
		super();
		// TODO Auto-generated constructor stub
	}
	  /**
	   * Causes the browser to pre-fetch the image at a given URL.
	   * 
	   * @param url the URL of the image to be prefetched
	   */
	  public static void prefetch(String url) {
			String base = ComponentEntryPoint.getMetaProperty("base");
			if(null != base){
				url=base+GWT.getModuleName()+"/"+url;
			}
			Image.prefetch(url);
	  }

	/**
	 * @param arg0
	 */
	public JSFImage(String arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.Image#getUrl()
	 */
	public String getUrl() {
		// TODO Auto-generated method stub
		String url = super.getUrl();
		String base = ComponentEntryPoint.getMetaProperty("base");
		if(null != base){
			url = url.substring((base+GWT.getModuleName()+"/").length());
		}
		return url;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.Image#setUrl(java.lang.String)
	 */
	public void setUrl(String url) {
		String base = ComponentEntryPoint.getMetaProperty("base");
		if(null != base){
			url=base+GWT.getModuleName()+"/"+url;
		}
		super.setUrl(url);
	}

}
