Passing Array's as a parameter to a JSNI native method?

172 views
Skip to first unread message

aglaforge

unread,
Jun 27, 2006, 10:58:29 PM6/27/06
to Google Web Toolkit
Reading the documentation there appears to be no way to pass an Array
object to a native JavaScript method. I've tried something like the
following to see if I could work around the limitation... however it
did not... The only thing I could thing of would be to cheat, use a
wrapper object in Java and then create call methods. ... Any ideas?


/*
* Creates a polyline from an array of vertices. The color is given as
a string that contains the color in hexadecimal numeric HTML style,
i.e. #RRGGBB. The weight is the width of the line in pixels. The
opacity is given as a number between 0 and 1. The line will be
antialiased and semitransparent.
*/
public static native GPolyline create(GLatLng[] points)/*-{
var length =
points.@[Lcom/mapitz/gwt/googleMaps/client/GLatLng;::length;
$wnd.alert(length);
return new $wnd.GPolyline(points);
}-*/;

Mat Gessel

unread,
Jun 28, 2006, 4:17:54 AM6/28/06
to Google Web Toolkit
You can convert the Java array to a JavaScript array then pass it to
your JSNI method. The following code seems to work.

-= Mat

public static JavaScriptObject arrayConvert(Object[] array)
{
JavaScriptObject result = newJSArray(array.length);
for (int i = 0; i < array.length; i++)
{
arraySet(result, i, array[i]);
}
return result;
}

private static native JavaScriptObject newJSArray(int length) /*-{
if (length < 0)
{
return new Array();
}
else
{
return new Array(length);
}
}-*/;

public static native int arrayLength(JavaScriptObject array) /*-{
return array.length;
}-*/;

public static native Object arrayGetObject(JavaScriptObject array, int
index) /*-{
return array[index];
}-*/;

public static native void arraySet(JavaScriptObject array, int index,
Object value) /*-{
array[index] = value;
}-*/;

aglaforge

unread,
Jun 29, 2006, 12:41:11 AM6/29/06
to Google Web Toolkit
Mat,

Thanks for the code sample, it works well.

Cheers,

Anthony

Reply all
Reply to author
Forward
0 new messages