package com.akjava.gwt.lib.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.typedarrays.shared.ArrayBuffer;
import com.google.gwt.xhr.client.ReadyStateChangeHandler;
import com.google.gwt.xhr.client.XMLHttpRequest;
import com.google.gwt.xhr.client.XMLHttpRequest.ResponseType;

public class BrowserUtils {
	public static native String getUserAgent() /*-{
	return navigator.userAgent.toLowerCase();
	}-*/;
	
	public static boolean isChrome(){
		return getUserAgent().indexOf("chrome")!=-1;
	}
	public static boolean isFirefox(){
		return getUserAgent().indexOf("firefox")!=-1;
	}
	
	public interface LoadBinaryListener{
		public void onLoadBinaryFile(ArrayBuffer buffer);
		public void onFaild(int states,String statesText);
	}
	public interface LoadTextListener{
		public void onLoadTextFile(String text);
		public void onFaild(int states,String statesText);
	}
	
	public static void loadBinaryFile(String url,final LoadBinaryListener listener) {
		
		XMLHttpRequest request=XMLHttpRequest.create();
		request.setResponseType(ResponseType.ArrayBuffer);
		request.setOnReadyStateChange(new ReadyStateChangeHandler() {
			@Override
			public void onReadyStateChange(XMLHttpRequest xhr) {
				if(xhr.getResponseArrayBuffer()==null){//pre loading
					return;
				}
				if(xhr.getStatus()==200){
				ArrayBuffer arrayBufer=xhr.getResponseArrayBuffer();
				
				listener.onLoadBinaryFile(arrayBufer);
				}else{
					listener.onFaild(xhr.getStatus(),xhr.getStatusText());
				}
				
			}
		});
		request.open("GET",url);
		request.send();
	}
	
	public static void loadTextFile(String url,final LoadTextListener listener){
		   try {
				new RequestBuilder(RequestBuilder.GET, url).sendRequest(null, new RequestCallback() {
					
					@Override
					public void onResponseReceived(Request request, Response response) {
						listener.onLoadTextFile(response.getText());
					}
					
					@Override
					public void onError(Request request, Throwable exception) {
						listener.onFaild(0,exception.getMessage());
					}
				});
			} catch (RequestException e) {
				listener.onFaild(0, e.getMessage());
			}
	}
	
	
}

@@ -31,16 +31,142 @@
 ent;%0D%0A%0D%0A
+import com.akjava.gwt.lib.client.MultiImageElementLoader.MultiImageElementListener;%0D%0Aimport com.google.common.collect.Lists;%0D%0A
 import c
@@ -1930,32 +1930,361 @@
 send();%0D%0A%09%7D%0D%0A%09%0D%0A
+%09public static void loadImageFile(String url,final ImageElementListener listener)%7B%0D%0A%09%09new ImageElementLoader().load(url, listener);%0D%0A%09%7D%0D%0A%09%0D%0A%09public static void loadImageFiles(Iterable%3CString%3E urls,final MultiImageElementListener listener)%7B%0D%0A%09%09new MultiImageElementLoader().loadImages(Lists.newArrayList(urls), listener);%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public static v

add load file