public class JSObject
extends java.lang.Object
A JSObject is associated with a particular JavascriptContext
and it is backed
by a Javascript object inside the javascript context. This object acts as a
proxy to call methods and access properties of the actual javascript object.
All return values for Javascript calls in the JavascriptContext
will be
converted to the appropriate Java type. Javascript objects will automatically
be wrapped in a JSObject proxy.
JSObject provides get(String)
and set(String,Object)
methods to get and set properties on the
object. E.g.
obj.set("name", "Steve");
obj.set("age", 12);
String name = (String)obj.get("name"); // Steve
Double age = (Double)obj.get("age"); // 12.0
The return value of get(String)
will be one of Double, String, Boolean, or JSObject
depending on the type of Javascript value that is being returned. get(String)
requires
you to cast the return value to the correct type, which is a bit a pain. Luckily,
JSObject provides a set of typed getter methods that will automatically cast to
particular types:
getInt() | Returns int |
getString() | Returns String |
getDouble() | Returns double |
getObject() | Returns JSObject |
getBoolean() | Returns boolean |
JSObject can wrap Javascript arrays also. You can retrieve the indexed
properties of the array using indexed versions of get(int)
and set(int,Object)
(i.e. they
take an int as their first parameter instead of a String). There are also
typed versions of the indexed get(int)
method to allow directly returning
values of the correct type without having to type cast
JSObject colors = ctx.get("['red','green','blue']");
int len = colors.getInt("length");
for ( int i=0; i< len; i++ ){
System.out.println("Color "+i+" is "+colors.getString(i));
}
JSObject allows you to call Javascript object methods on the wrapped
Javascript object via the its call(String,Object[])
method. It takes the name of the
method (i.e. property name that stores the function), and an array of
parameters to pass to the method.
JSObject document = (JSObject)context.get("document");
// Call document.writeln("Hello world");
document.call("writeln", new Object[]{"Hello world"});
Since, in Javascript, functions are objects, it is possible to wrap a function
with a JSObject object also. You can then call the function using the alternate
version of the #call(JSObject,Object[])
method.
JSObject window = (JSObject)context.get("window");
// reference to the window object so that we can pass it as the context
// of the function call.
JSObject myfunc = (JSObject)context.get("function(a,b){ return a+b}");
Double result = (Double)myfunc.call(window, new Object[]{new Integer(1), new Integer(2)});
The JSFunction
interface allows you to implement functions in Java that can
be called from Javascript. You can assign any JSFunction
object to be a member
method of an existing JSObject via the JSObject.set()
method. Then the function
can be called from javascript just like any other Javascript method. JSFunction
methods are called asynchronously from Javascript to prevent deadlocks. If you
require a return value to Javascript, you can do that by passing a callback
function which is called by the JSFunction with some parameters.
The following example, adds a camera object to the Javascript environment that has a capture() method, which can be used to capture images using the device's camera:
// Create a new Javascript object "camera"
final JSObject camera = (JSObject)ctx.get("{}");
// Create a capture() method on the camera object
// as a JSFunction callback.
camera.set("capture", new JSFunction(){
public void apply(JSObject self, final Object[] args) {
Display.getInstance().capturePhoto(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
String imagePath = (String)evt.getSource();
// Get the callback function that was provided
// from javascript
JSObject callback = (JSObject)args[0];
ctx.call(
callback, // The function
camera, // The "this" object
new Object[]{"file://"+imagePath} // Parameters
);
}
});
}
});
// Add the camera object to the top-level window object
ctx.set("window.camera", camera);
We can then capture photos directly from Javascript using a function similar to the following:
camera.capture(function(url){
if ( url == null ){
// No image was captured
return;
}
// Fetch the preview <img> tag.
var image = document.getElementById('preview-image');
// Set the preview URL to the image that was taken.
image.src = url;
});
Constructor and Description |
---|
JSObject(JavascriptContext context,
java.lang.String expr)
Constructor for a JSObject.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
call(java.lang.Object[] params)
Calls the object as a function statically.
|
java.lang.Object |
call(java.lang.String key,
java.lang.Object[] params)
Calls a method on the underlying Javascript object.
|
java.lang.Object |
get(int index)
This method is useful only for JSObjects that encapsulate Javascript arrays.
|
java.lang.Object |
get(java.lang.String key)
Returns a member variable of the Javascript object.
|
boolean |
getBoolean(int index)
Wrapper for get(int) for indexed boolean values.
|
boolean |
getBoolean(java.lang.String key)
Wrapper around get() to return a boolean.
|
double |
getDouble(int index)
Wrapper for get(int) for indexed double values.
|
double |
getDouble(java.lang.String key)
Wrapper around get() to return a double.
|
int |
getInt(int index)
Wrapper for get(int) for indexed int values.
|
int |
getInt(java.lang.String key)
Wrapper around get() to return an int
|
JSObject |
getObject(int index)
Wrapper for get(int) for indexed object values.
|
JSObject |
getObject(java.lang.String key)
Wrapper around the get() method to return a JSObject.
|
java.lang.String |
getString(int index)
Wrapper for get(int) for indexed string values.
|
java.lang.String |
getString(java.lang.String key)
Wrapper around get() to return a String.
|
void |
set(int index,
java.lang.Object js) |
void |
set(java.lang.String key,
java.lang.Object js)
Sets a property on the underlying Javascript object.
|
void |
setBoolean(int index,
boolean value) |
void |
setBoolean(java.lang.String key,
boolean value) |
void |
setDouble(int index,
double value) |
void |
setDouble(java.lang.String key,
double value) |
void |
setInt(int index,
int value) |
void |
setInt(java.lang.String key,
int value) |
java.lang.String |
toJSPointer()
Returns a Javascript variable name for the underlying Javascript object.
|
public JSObject(JavascriptContext context, java.lang.String expr)
// Create a JavascriptContext for a browser component
JavascriptContext ctx = new JavascriptContext(browserComponent);
// Get reference to the window object
JSObject window = new JSObject(ctx, "window");
// This is equivalent to
window = (JSObject)ctx.get("window");
context
- The javascript context in which this object is being created.expr
- A javascript expression that resolves to an Javascript Object.public java.lang.Object get(java.lang.String key)
E.g., suppose you have a Javascript object myCar whose JSON representation is
{ make : 'Ford', model : 'Escort', 'year' : 1989}
Then the JSObject proxy for this object could call:
String model = (String)myCar.get("model");
And this would return "Ford".
JSObject document = (JSObject)ctx.get("document");
// Get document title
String title = document.get("title");
// Since the "title" property is a string, get() returns a String.
// We could equivalently use
title = document.getString("title");
// Get a grandchild property
Double titleLength = (Double)document.get("title.length");
// Since length is an integer, it is probably easier to use getInt()
int titleLengthInt = document.getInt("title.length");
// Get a child object
JSObject body = (JSObject)document.get("body");
// Since "body" is a Javascript object, it is probably easier to use
// getObject()
JSObject body2 = document.getObject("body");
// Get a method as a function object
JSObject open = (JSObject) document.get("open");
// Call the open() method, with document as "this"
open.call(document, new Object[]{}); // Takes no parameters
// Equivalently we could have called open via the document object
document.call("open", new Object[]{});
key
- The name of the property to retrieve on this object.public java.lang.String getString(java.lang.String key)
key
- The name of the property in the object to retrieve. Value of this
property must be a string.public int getInt(java.lang.String key)
key
- The name of the property in the object to retrieve. Value of this
property must be an integer.public double getDouble(java.lang.String key)
key
- The name of the property in the object to retrieve. Value of this property
must be a number.public boolean getBoolean(java.lang.String key)
key
- The name of the property in the object to retrieve. Value of this property
must be a boolean.public JSObject getObject(java.lang.String key)
key
- The name of the property in the object to retrieve. Value of this property
must be an Javascript object or function.public java.lang.Object get(int index)
JSObject colors = ctx.get("['red','green','blue']");
String red = (String)colors.get(0);
String green = (String)colors.get(1);
String blue = (String)colors.get(2);
It may be more convenient to use the typed wrapper methods so that you don't have to typecast the values. E.g.
String red = colors.getString(0);
String green = colors.getString(1);
String blue = colors.getString(2);
JSObject colors = ctx.get("['red','green','blue']");
int len = colors.getInt("length");
for ( int i=0; i< len; i++ ){
System.out.println("Color "+i+" is "+colors.getString(i));
}
index
- The index of the entry within the array to return.public java.lang.String getString(int index)
index
- The index within an Array object whose value to retrieve.public int getInt(int index)
index
- The index within the Array object whose value to retrieve.public double getDouble(int index)
index
- The index within the Array object whose value to retrieve.public boolean getBoolean(int index)
index
- The index within the Array object whose value to retrieve.public JSObject getObject(int index)
index
- The index within the Array object whose value to retrieve.public void set(java.lang.String key, java.lang.Object js)
key
- The name of the property to set on the current object.js
- The value of the property. This value should be provided as a
Java value and it will be converted to the appropriate Javascript value.
See @ref JavascriptContext.set() for a conversion table of the Java to
Javascript type conversions.public void setInt(java.lang.String key, int value)
public void setDouble(java.lang.String key, double value)
public void setBoolean(java.lang.String key, boolean value)
public void set(int index, java.lang.Object js)
public void setInt(int index, int value)
public void setDouble(int index, double value)
public void setBoolean(int index, boolean value)
public java.lang.String toJSPointer()
public java.lang.Object call(java.lang.String key, java.lang.Object[] params)
key
- The name of the method to call.params
- Array of parameters to pass to the method. These will be
converted to corresponding Javascript types according to the translation
table specified in JavascriptContext.set(String,Object)
JavascriptContext.get(String)
public java.lang.Object call(java.lang.Object[] params)
E.g.
JSObject alert = (JSObject)ctx.get("window.alert");
alert.call(new Object[]{"An alert message"});
The above gets a reference to the alert() function (remember functions are objects in Javascript). Then it calls it via Java, passing it a single string parameter. This is equivalent to the following Javasript:
alert("An alert message");
params
- The parameters to pass to the function. These will be converted
to the appropriate Javascript type.