Home‎ > ‎JSNI‎ > ‎

Wrapping


Wrapping Javascripts for GWT use. Or Using Overlay Types. Or Javascript Bindings.


1. Use GWTTestcase to debug each method as your wrap the JavaScriptObject.
2. Write the JavaScriptObject wrapper, but comment out all but one class and uncomment after each one has passed a written GWTTestCase.
3. I also setup another project to load the module with the native code. I call it my Sandbox.
4. In the sandbox, setup a native method containing the entire API javascript code setup and slowly replace it with the GWT wrapper code as you finish the wrapper classes. This will help break down debugging steps. 

Reference

For Example

public class LatLng extends JavaScriptObject {
  /**
   * LatLng is a point in geographical coordinates, latitude and longitude.
   * 
   * Notice that although usual map projections associate longitude with the x-coordinate of the map, and latitude with the y-coordinate, the latitude coordinate is always written first, followed by the longitude.
   * Notice also that you cannot modify the coordinates of a LatLng. If you want to compute another point, you have to create a new one.
   * {@link http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng}
   */
  protected LatLng() {}

  /**
   * Notice the ordering of latitude and longitude. If the noWrap flag is true, then the numbers will be used as passed, otherwise latitude will be clamped to lie between -90 degrees and +90 degrees, and longitude will be wrapped to lie between -180 degrees and +180 degrees.
   * @param lat
   * @param lng
   * @param noWrap
   * @return LatLng
   */
  public static LatLng newInstance(BigDecimal lat, BigDecimal lng) {
    return newInstance(lat, lng, false);
  }
  
  /**
   * Notice the ordering of latitude and longitude. If the noWrap flag is true, then the numbers will be used as passed, otherwise latitude will be clamped to lie between -90 degrees and +90 degrees, and longitude will be wrapped to lie between -180 degrees and +180 degrees.
   * @param lat
   * @param lng
   * @param noWrap
   * @return
   */
  public static LatLng newInstance(BigDecimal lat, BigDecimal lng, boolean noWrap) {
    String slat = null;
    if (lat != null) {
      slat = lat.toPlainString();
    }
    String slng = null;
    if (lng != null) {
      slng = lng.toPlainString();
    }
    return createJso(slat, slng, noWrap);
  }
  
  /**
   * Notice the ordering of latitude and longitude. If the noWrap flag is true, then the numbers will be used as passed, otherwise latitude will be clamped to lie between -90 degrees and +90 degrees, and longitude will be wrapped to lie between -180 degrees and +180 degrees.
   * LatLng(lat:number, lng:number, noWrap?:boolean)
   * @return LatLng
   */
  private static native LatLng createJso(String lat, String lng, boolean noWrap) /*-{
    if (lat != null) {
      lat = new Number(lat);
    }
    if (lng != null) {
      lng = new Number(lng);
    }
    return new $wnd.google.maps.LatLng(lat, lng, noWrap);
  }-*/;
  
  /**
   * is equal?
   * @param other
   */
  public final native boolean equals(LatLng other) /*-{
    return this.equals(other);
  }-*/;
  
//  /** (up next to work with)
//   * get latitude
//   * @return BigDecimal
//   */
//  public BigDecimal getLatitude() {
//    return new BigDecimal(lat());
//  }

// ....

}

GWT Test Case use of the LatLng class

public class LatLngTest extends GWTTestCase {

  public static final int ASYNC_DELAY_MS = 5000;
  
  public String getModuleName() {
    return "com.gonevertical.apis.googlemaps.Apis_Google_Maps";
  }

  public void testWorks() {
    assertEquals(true, true);
  }
  
  public void testUse() {
    LoadApi.go(new Runnable() {
      public void run() {
        LatLng ll = LatLng.newInstance(new BigDecimal(38), new BigDecimal(35.5));
        finishTest();
      }
    }, false);
    delayTestFinish(ASYNC_DELAY_MS);
  }
  
  public void testUse2() {
    LoadApi.go(new Runnable() {
      public void run() {
        LatLng ll = LatLng.newInstance(new BigDecimal(38), new BigDecimal(35.5), false);
        finishTest();
      }
    }, false);
    delayTestFinish(ASYNC_DELAY_MS);
  }
  
  public void testUse3() {
    LoadApi.go(new Runnable() {
      public void run() {
        LatLng ll = LatLng.newInstance(new BigDecimal(38), new BigDecimal(35.5), true);
        finishTest();
      }
    }, false);
    delayTestFinish(ASYNC_DELAY_MS);
  }
 
  public void testEqualsIsEqual() {
    LoadApi.go(new Runnable() {
      public void run() {
        LatLng left = LatLng.newInstance(new BigDecimal(38), new BigDecimal(35.5), false);
        LatLng right = LatLng.newInstance(new BigDecimal(38), new BigDecimal(35.5), false);
        boolean equals = left.equals(right);
        assertEquals(true, equals);
        finishTest();
} }, false);
    delayTestFinish(ASYNC_DELAY_MS);
  }
  
  public void testEqualsIsNotEqual() {
    LoadApi.go(new Runnable() {
      public void run() {
        LatLng left = LatLng.newInstance(new BigDecimal(38.3), new BigDecimal(35.5), false);
        LatLng right = LatLng.newInstance(new BigDecimal(38), new BigDecimal(35.5), false);
        boolean equals = left.equals(right);
        assertEquals(false, equals);
        finishTest();
      }
    }, false);
    delayTestFinish(ASYNC_DELAY_MS);
  }
  
}

Use "final" for native and Java methods when implementing

public final native String lat() /*-{
  return this.lat();
}-*/;







Subpages (1): Event Binding
Comments