Chapter 1:The comon package


Before you read this articale,I suppose you know well about JSNI and JavaScript Object.We are going to talk about three classis package:
  • JavaScript ObjectHelper
  • BaseJavaScript ObjectImpl
  • BaseJavaScript Object
  • 1.JavaScript ObjectHelper
        Some useful methods in this clast,now just one method「to JavaScript Aray」,This method is used to convert Object[]of javato JavaScript Aray.(「BasejavaScript ObjectImpl.set」)
     
    package org.gwtopenmaps.openlayers.client.common;
    
    import com.google.gwt.core.client.JavaScriptObject;
    
    public class JavaScriptObjectHelper {
    	
    	/**
    	 * notice: if element of objs is primitive type which is not supported, it must be cast to its packaged class
    	 * 
    	 * @param objs java.lang.Integer, java.lang.Float, java.lang.Double, java.lang.Boolean, java.lang.String, JavaScriptObject
    	 * @return JavaScript Array
    	 */
    	public static JavaScriptObject toJavaScriptArray(Object... objs) {
    		
    		JavaScriptObject javaScriptArray = JavaScriptObject.createArray();
    		
    		for(int i = 0; i < objs.length; i ++) {
    			
    			Object obj = objs[i];
    			
    			if(obj.getClass().equals(java.lang.Integer.class)) {
    				BaseJavaScriptObjectImpl.setProperty(javaScriptArray, String.valueOf(i), Integer.valueOf(obj.toString()));
    			}
    			
    			if(obj.getClass().equals(java.lang.Float.class)) {
    				BaseJavaScriptObjectImpl.setProperty(javaScriptArray, String.valueOf(i), Float.valueOf(obj.toString()));
    			}
    			
    			if(obj.getClass().equals(java.lang.Double.class)) {
    				BaseJavaScriptObjectImpl.setProperty(javaScriptArray, String.valueOf(i), Double.valueOf(obj.toString()));
    			}
    			
    			if(obj.getClass().equals(java.lang.Boolean.class)) {
    				BaseJavaScriptObjectImpl.setProperty(javaScriptArray, String.valueOf(i), Boolean.valueOf(obj.toString()));
    			}
    			
    			if(obj.getClass().equals(java.lang.String.class)) {
    				BaseJavaScriptObjectImpl.setProperty(javaScriptArray, String.valueOf(i), obj.toString());
    			}
    			
    			if(obj instanceof JavaScriptObject) {
    				BaseJavaScriptObjectImpl.setProperty(javaScriptArray, String.valueOf(i), (JavaScriptObject) obj);
    			}
    		}
    		
    		return javaScriptArray;
    		
    	}
    	
    }
     
     
    2.  BaseJava Script Object Impl
         This class 18 static natic natitic methods(JSNI)、6 get Property、6 set Propertyand 6 invokeMethod which foroperate JavaScraptObject.The number 6 means 6 types:int、float、doublle、double Obole、Storeeb.Strivastststststininininineeeeeebubububububububububububuststststststststststinininininininineeeeeeebububububububububububububububuststststststststststststststststst「undefined」which is also type of JavaScript Object.
               Please pay atentation to the param「arrayAgments」of the invoke methods、then relate it to the method above「toJavaScripAray」…
     
    package org.gwtopenmaps.openlayers.client.common;
    
    import com.google.gwt.core.client.JavaScriptObject;
    
    public class BaseJavaScriptObjectImpl {
    
    	/*-- set ----------------------------------------------------------------------------*/
    	
    	public static native void setProperty(JavaScriptObject javaScriptObject, String propertyName, int propertyValue) /*-{
    		javaScriptObject[propertyName] = propertyValue;
    	}-*/;
    	
    	public static native void setProperty(JavaScriptObject javaScriptObject, String propertyName, float propertyValue) /*-{
    		javaScriptObject[propertyName] = propertyValue;
    	}-*/;
    	
    	public static native void setProperty(JavaScriptObject javaScriptObject, String propertyName, double propertyValue) /*-{
    		javaScriptObject[propertyName] = propertyValue;
    	}-*/;
    	
    	public static native void setProperty(JavaScriptObject javaScriptObject, String propertyName, boolean propertyValue) /*-{
    		javaScriptObject[propertyName] = propertyValue;
    	}-*/;
    	
    	public static native void setProperty(JavaScriptObject javaScriptObject, String propertyName, String propertyValue) /*-{
    		javaScriptObject[propertyName] = propertyValue;
    	}-*/;
    	
    	public static native void setProperty(JavaScriptObject javaScriptObject, String propertyName, JavaScriptObject propertyValue) /*-{
    		javaScriptObject[propertyName] = propertyValue;
    	}-*/;
    	
    	/*-- get ----------------------------------------------------------------------------*/
    	
    	public static native int getPropertyAsInt(JavaScriptObject javaScriptObject, String propertyName) /*-{
    		return javaScriptObject[propertyName];
    	}-*/;
    	
    	public static native float getPropertyAsFloat(JavaScriptObject javaScriptObject, String propertyName) /*-{
    		return javaScriptObject[propertyName];
    	}-*/;
    	
    	public static native double getPropertyAsDouble(JavaScriptObject javaScriptObject, String propertyName) /*-{
    		return javaScriptObject[propertyName];
    	}-*/;
    	
    	public static native boolean getPropertyAsBoolean(JavaScriptObject javaScriptObject, String propertyName) /*-{
    		return javaScriptObject[propertyName];
    	}-*/;
    	
    	public static native String getPropertyAsString(JavaScriptObject javaScriptObject, String propertyName) /*-{
    		return javaScriptObject[propertyName];
    	}-*/;
    	
    	public static native JavaScriptObject getPropertyAsJavaScriptObject(JavaScriptObject javaScriptObject, String propertyName) /*-{
    		return javaScriptObject[propertyName];
    	}-*/;
    	
    	/*-- invoke method ----------------------------------------------------------------------------*/
    	
    	public static native int invokeMethodReturnInt(JavaScriptObject javaScriptObject, String methodName, JavaScriptObject arrayArguments) /*-{
    		return javaScriptObject[methodName].apply(javaScriptObject, arrayArguments);
    	}-*/;
    	
    	public static native float invokeMethodReturnFloat(JavaScriptObject javaScriptObject, String methodName, JavaScriptObject arrayArguments) /*-{
    		return javaScriptObject[methodName].apply(javaScriptObject, arrayArguments);
    	}-*/;
    	
    	public static native double invokeMethodReturnDouble(JavaScriptObject javaScriptObject, String methodName, JavaScriptObject arrayArguments) /*-{
    		return javaScriptObject[methodName].apply(javaScriptObject, arrayArguments);
    	}-*/;
    	
    	public static native boolean invokeMethodReturnBoolean(JavaScriptObject javaScriptObject, String methodName, JavaScriptObject arrayArguments) /*-{
    		return javaScriptObject[methodName].apply(javaScriptObject, arrayArguments);
    	}-*/;
    	
    	public static native String invokeMethodReturnString(JavaScriptObject javaScriptObject, String methodName, JavaScriptObject arrayArguments) /*-{
    		return javaScriptObject[methodName].apply(javaScriptObject, arrayArguments);
    	}-*/;
    	
    	public static native JavaScriptObject invokeMethodReturnJavaScriptObject(JavaScriptObject javaScriptObject, String methodName, JavaScriptObject arrayArguments) /*-{
    		return javaScriptObject[methodName].apply(javaScriptObject, arrayArguments);
    	}-*/;
    }
    
     
     
    3.  BaseJava Script Object
         It is is the base class,who can be exteded by any JavaScrcript clas. It has one protected constststrutoて,one static method“createNew”and 6 setPropty,6 getPropty,6 invokeMethod.Afteradrereininininininininininindededededededededededestststststststininininininininininininininininininininininininininininininininininindededededededededededededestststststststststinininininininininininininand methods,keep you far away from JSNI because all the JSNI methods cone place"BaseJava Script Object Impl"
                Also pay atention to the param objs of invoke methods、see thisライン
         JavaScript Object arrayAgments=JavaScript ObjectHelper.toJavaScript Aray(objs)
         We convert Object[]to JavaScript Aray which is used by appy method of js.
     
    package org.gwtopenmaps.openlayers.client.common;
    
    import com.google.gwt.core.client.JavaScriptObject;
    
    public class BaseJavaScriptObject extends JavaScriptObject {
    
    	protected BaseJavaScriptObject() {}
    	
    	public static BaseJavaScriptObject createNew() {
    		return (BaseJavaScriptObject) JavaScriptObject.createObject();
    	}
    	
    	/*-- set ----------------------------------------------------------------------------*/
    	
    	protected final void setProperty(String propertyName, int propertyValue) {
    		BaseJavaScriptObjectImpl.setProperty(this, propertyName, propertyValue);
    	}
    	
    	protected final void setProperty(String propertyName, float propertyValue) {
    		BaseJavaScriptObjectImpl.setProperty(this, propertyName, propertyValue);
    	}
    	
    	protected final void setProperty(String propertyName, double propertyValue) {
    		BaseJavaScriptObjectImpl.setProperty(this, propertyName, propertyValue);
    	}
    	
    	protected final void setProperty(String propertyName, boolean propertyValue) {
    		BaseJavaScriptObjectImpl.setProperty(this, propertyName, propertyValue);
    	}
    	
    	protected final void setProperty(String propertyName, String propertyValue) {
    		BaseJavaScriptObjectImpl.setProperty(this, propertyName, propertyValue);
    	}
    	
    	protected final void setProperty(String propertyName, JavaScriptObject propertyValue) {
    		BaseJavaScriptObjectImpl.setProperty(this, propertyName, propertyValue);
    	}
    	
    	/*-- get ----------------------------------------------------------------------------*/
    	
    	protected final int getPropertyAsInt(String propertyName) {
    		return BaseJavaScriptObjectImpl.getPropertyAsInt(this, propertyName);
    	}
    	
    	protected final float getPropertyAsFloat(String propertyName) {
    		return BaseJavaScriptObjectImpl.getPropertyAsFloat(this, propertyName);
    	}
    	
    	protected final double getPropertyAsDouble(String propertyName) {
    		return BaseJavaScriptObjectImpl.getPropertyAsDouble(this, propertyName);
    	}
    	
    	protected final boolean getPropertyAsBoolean(String propertyName) {
    		return BaseJavaScriptObjectImpl.getPropertyAsBoolean(this, propertyName);
    	}
    	
    	protected final String getPropertyAsString(String propertyName) {
    		return BaseJavaScriptObjectImpl.getPropertyAsString(this, propertyName);
    	}
    	
    	protected final JavaScriptObject getPropertyAsJavaScriptObject(String propertyName) {
    		return BaseJavaScriptObjectImpl.getPropertyAsJavaScriptObject(this, propertyName);
    	}
    	
    	/*-- invoke method ----------------------------------------------------------------------------*/
    	
    	protected final int invokeMethodReturnInt(String methodName, Object... objs) {
    		JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);
    		return BaseJavaScriptObjectImpl.invokeMethodReturnInt(this, methodName, arrayArguments);
    	}
    	
    	protected final float invokeMethodReturnFloat(String methodName, Object... objs) {
    		JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);
    		return BaseJavaScriptObjectImpl.invokeMethodReturnFloat(this, methodName, arrayArguments);
    	}
    	
    	protected final double invokeMethodReturnDouble(String methodName, Object... objs) {
    		JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);
    		return BaseJavaScriptObjectImpl.invokeMethodReturnDouble(this, methodName, arrayArguments);
    	}
    	
    	protected final boolean invokeMethodReturnBoolean(String methodName, Object... objs) {
    		JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);
    		return BaseJavaScriptObjectImpl.invokeMethodReturnBoolean(this, methodName, arrayArguments);
    	}
    	
    	protected final String invokeMethodReturnString(String methodName, Object... objs) {
    		JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);
    		return BaseJavaScriptObjectImpl.invokeMethodReturnString(this, methodName, arrayArguments);
    	}
    	
    	protected final JavaScriptObject invokeMethodReturnJavaScriptObject(String methodName, Object... objs) {
    		JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);
    		return BaseJavaScriptObjectImpl.invokeMethodReturnJavaScriptObject(this, methodName, arrayArguments);
    	}
    }
    
     
    4.  Epilogue
         Now all the JavaScript clast in your js file can be make the same class in GW by exted BaseJavaScript Object,have fun.
     
    Next Chapter:OpenLayers.Map