`
夏莹_合肥
  • 浏览: 179338 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

Chapter 1: The common package

阅读更多

Before you read this article, I suppose you know well about JSNI and JavaScriptObject. We are going to talk about three classes in this package:

  1. JavaScriptObjectHelper
  2. BaseJavaScriptObjectImpl
  3. BaseJavaScriptObject

1. JavaScriptObjectHelper

    Some useful methods in this class, now just one method "toJavaScriptArray", This method is used to convert Object[] of java to JavaScript Array.(" BaseJavaScriptObjectImpl.setProperty " will be mentioned in the next class)

 

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.  BaseJavaScriptObjectImpl

     This class has 18 static native methods(JSNI), 6 getProperty, 6 setProperty and 6 invokeMethod which for operating JavaScriptObject. The number 6 means 6 types: int, float, double, boolean, String and JavaScriptObject. If method of JavaScriptObject has no return value, then the return value is "undefined" which is also type of JavaScriptObject.

           Please pay attention to the param "arrayArguments " of the invoke methods, then relate it to the method above "toJavaScriptArray "...

 

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.  BaseJavaScriptObject

     It is the base class, who can be extended by any JavaScript class. It has one protected constructor, one static method "createNew" and 6 setProperty, 6 getProperty, 6 invokeMethod. After reading the code, you can understand the 18 protected final methods provide convenience for you to access JavaScriptObject properties and methods, keep you far away from JSNI because all the JSNI methods come to one place "BaseJavaScriptObjectImpl" .

            Also pay attention to the param objs of invoke methods, see this line

     JavaScriptObject arrayArguments = JavaScriptObjectHelper.toJavaScriptArray(objs);

     We convert Object[] to JavaScript Array which is used by apply 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 class in your js file can be make the same class in GWT by extend BaseJavaScriptObject, have fun.

 

Next Chapter : OpenLayers.Map

0
0
分享到:
评论

相关推荐

    Big.Data.Analytics.A.Practical.Guide.for.Manager

    Chapter 1: Introduction Chapter 2: The Mother of Invention's Triplets: Moore's Law, the Proliferation of Data, and Data Stor Chapter 3: Hadoop Chapter 4: HBase and Other Big Data Databases Chapter 5: ...

    Beginning Microsoft Visual CSharp 2008 Wiley Publishing(english)

    Chapter 1: Introducing C# 40 What Is the .NET Framework? 40 What Is C#? 45 Visual Studio 2008 46 Summary 48 Chapter 2: Writing a C# Program 50 The Development Environments 51 ...

    Automated.Trading.with.R.Quantitative.Research.and.Platform.Development

    Chapter 1: Fundamentals of Automated Trading Part 2: Building the Platform Chapter 2: Networking Part I Chapter 3: Data Preparation Chapter 4: Indicators Chapter 5: Rule Sets Chapter 6: High-...

    Cross-platform.UI.Development.with.Xamarin.Forms.1784391190

    Chapter 1: In the Beginning… Chapter 2: Let's Get the Party Started Chapter 3: Making It Look Pretty and Logging In Chapter 4: Making Your Application Portable Chapter 5: Data, Generics, and Making ...

    Splunk.Best.Practices.1785281399

    Chapter 1: Application Logging Chapter 2: Data Inputs Chapter 3: Data Scrubbing Chapter 4: Knowledge Management Chapter 5: Alerting Chapter 6: Searching and Reporting Chapter 7: Form-Based Dashboards ...

    网络协议攻击

    Chapter 1: The Basics of Networking This chapter describes the basics of computer networking with a particular focus on TCP/IP, which forms the basis of application-level network protocols. Subsequent...

    PROGRAMMING ACTIONSCRIPT 3.0

    Chapter 1: Introduction to ActionScript 3.0.. 19 About ActionScript..19 Advantages of ActionScript 3.0.20 What’s new in ActionScript 3.0.. 21 Core language features...21 Flash Player API features......

    Beginning Python (2005).pdf

    The numarray Package 422 Using Arrays 422 Computing the Standard Deviation 423 Summary 424 Exercises 425 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxiii xxiv Contents Chapter 20: Python in the ...

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

    Linking the Android Support Package to Your Project 260 Exploring Nested Fragments 261xviii Contents Summary 261 Quiz Questions 262 Exercises 262 References and More Information 263 10 Displaying ...

    R.Packages.1491910593

    In the process, you’ll work with devtools, roxygen, and testthat, a set of R packages that automate common development tasks. Devtools encapsulates best practices that Hadley has learned from years ...

    Microelectronic Circuits, 6ed, 2010.pdf

    the most common signal-processing function, amplification, and the characterization and types of amplifiers. Besides diodes and transistors, the basic electronic devices, the op amp is studied in Part...

    R Packages

    package, and the forms it can take, in Chapter 2, Package Structure. The subsequent chapters go into more detail about each component. They’re roughly organized in order of importance: Chapter 3, R ...

    Troubleshooting.Xcode.14842156

    Chapter 1. Xcode crashing when opening a project file Chapter 2. The identity used to sign the executable is invalid Chapter 3. Xcode fails to compile and blames "SBPartialInfo" Chapter 4. No matching...

    Troubleshooting.Xcode.1484215613

    Chapter 1. Xcode crashing when opening a project file Chapter 2. The identity used to sign the executable is invalid Chapter 3. Xcode fails to compile and blames "SBPartialInfo" Chapter 4. No matching...

    a project model for the FreeBSD Project.7z

    The vision is “To produce the best UNIX-like operating system package possible, with due respect to the original software tools ideology as well as usability, performance and stability.” The ...

    Electric Circuits, 10th Global Edition

    Chapter 1 Circuit Variables 24 Chapter 2 Circuit Elements 46 Chapter 3 Simple Resistive Circuits 78 Chapter 4 Techniques of Circuit Analysis 110 Chapter 5 The Operational Amplifier 166 Chapter 6 ...

    go系统编程(英文版)

    which(1), pwd(1), and find(1), but first you will learn how to use the flag package in order to parse the command-line arguments and options of a Go program. Additionally, you will learn how to delete...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    The chapter ends with a couple of examples of less common proce‐ dures, including selecting a set of contiguous rows and adding a header row to the dataset. Chapter 3, Excel Files Next, we’ll cover...

    R.Deep.Learning.Essentials.1785280589

    Master the common problems faced such as overfitting of data, anomalous datasets, image recognition, and performance tuning while building the models Build models relating to neural networks, ...

Global site tag (gtag.js) - Google Analytics