org.sgx.j2s.js
Class JsUtils

Package class diagram package JsUtils
java.lang.Object
  extended by org.sgx.j2s.js.JsUtils

public class JsUtils
extends java.lang.Object

Utilities that easies defining and accessing native javascript language contructions from java.


Constructor Summary
JsUtils()
           
 
Method Summary
static java.lang.Object APPLY(java.lang.Object func, java.lang.Object context, java.lang.Object[] params)
           
static java.lang.Object[] ARR(java.lang.Object[] arr)
          recursively apply NATIVE to each array item (returns an array of native elements or a "native array")
static Element body()
           
static java.lang.Object CALL(java.lang.Object obj, java.lang.String fName, java.lang.Object[] params)
           
static boolean castToBoolean(java.lang.Object o2)
          use this method when you need to cast from Object to a primitive type boolean in java code
static float castToFloat(java.lang.Object o)
          use this method when you need to cast from Object to a primitive type float in java code
static int castToInt(java.lang.Object o2)
          use this method when you need to cast from Object to a primitive type int in java code
static Document document()
           
static java.lang.String dump(java.lang.Object o, boolean onlyType)
           
static java.lang.String escapeHTML(java.lang.String s)
           
static void firebug()
           
static java.lang.Object FUNC(java.lang.Runnable r)
           
static java.lang.Object GET(java.lang.Object jsObj, java.lang.Object key)
           
static Element HTML(java.lang.Object obj)
           
static java.lang.Object JAVA(java.lang.Object o)
           
static java.lang.Object NATIVE_aux(java.lang.String prop, java.lang.Object val)
           
static java.lang.Object NATIVE(java.lang.Object val)
          transformation of java Boolean, Integer, Double, Float, Character, Object[], Runnable and YUINativeWrapper to native objects.
static java.lang.Object OBJ(java.lang.Object... props)
          creates a native javascript objects.
static boolean objectsAreEquals(java.lang.Object o1, java.lang.Object o2, java.util.Comparator<java.lang.Object> valueComparator)
           
static java.lang.String[] PROPS(java.lang.Object o)
          return the names of an javascript object properties
static java.lang.Object PUT(java.lang.Object jsObj, java.lang.Object key, java.lang.Object propValue)
          perform the javascript statement jsObj[key]=propValue;
static java.lang.Object runnableToFunc(java.lang.Runnable r)
           
private static java.lang.Object withContext(java.lang.Object func, java.lang.Object context)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsUtils

public JsUtils()
Method Detail

PROPS

public static java.lang.String[] PROPS(java.lang.Object o)
return the names of an javascript object properties

Parameters:
o - the object to get property names from

PUT

public static java.lang.Object PUT(java.lang.Object jsObj,
                                   java.lang.Object key,
                                   java.lang.Object propValue)
perform the javascript statement jsObj[key]=propValue;


GET

public static java.lang.Object GET(java.lang.Object jsObj,
                                   java.lang.Object key)
Returns:
the javascript result of statement jsObj[key]

OBJ

public static java.lang.Object OBJ(java.lang.Object... props)
creates a native javascript objects. Usage:
 Object o1 = OBJ(new Object[]{"attr1", 123, "attr2", false});
 Object o2 = OBJ(new Object[]{"attr1": 123, "attr2", o1});
 
pass null for creating an empty object. Note: the object literal syntax could be simple if using varargs function: Object o1 = OBJ("attr1", 123, "attr2", false); and I think this is ideally. Unfortunately, j2s varargs do not work when passing native objects as vararg arguments. So if you want to use this shorter syntax, you will have to "javize" your native objects. You can accomplish this in two ways. 1) use NativeWrapper: Object o1 = OBJ("nativeAttr1", NativeWrapper.create(nativeObject)); 2) "javize" the native object poblating the object with necesary java api with JsUtils.JAVA: Object o1 = OBJ("nativeAttr1", JAVA(nativeObject));


NATIVE_aux

public static java.lang.Object NATIVE_aux(java.lang.String prop,
                                          java.lang.Object val)

NATIVE

public static java.lang.Object NATIVE(java.lang.Object val)
transformation of java Boolean, Integer, Double, Float, Character, Object[], Runnable and YUINativeWrapper to native objects. A runnable r is translated to FUNC(r) and a YUINativeWrapper nr is translated to nr._getData(); Other objects are returned unmodified

Parameters:
val - object to convert to a native equivalent value
Returns:
the native javascript object equivalent

ARR

public static java.lang.Object[] ARR(java.lang.Object[] arr)
recursively apply NATIVE to each array item (returns an array of native elements or a "native array")


JAVA

public static java.lang.Object JAVA(java.lang.Object o)

APPLY

public static java.lang.Object APPLY(java.lang.Object func,
                                     java.lang.Object context,
                                     java.lang.Object[] params)
Parameters:
func - a javascript native function object
context - a javascript object used as a context in function eval
params - params to be passed to the function
Returns:
function's returned value

CALL

public static java.lang.Object CALL(java.lang.Object obj,
                                    java.lang.String fName,
                                    java.lang.Object[] params)

FUNC

public static java.lang.Object FUNC(java.lang.Runnable r)

runnableToFunc

public static java.lang.Object runnableToFunc(java.lang.Runnable r)

withContext

private static java.lang.Object withContext(java.lang.Object func,
                                            java.lang.Object context)
Parameters:
func - a js function object
context - a context object
Returns:
a function that will call function func with context

castToInt

public static int castToInt(java.lang.Object o2)
use this method when you need to cast from Object to a primitive type int in java code


castToBoolean

public static boolean castToBoolean(java.lang.Object o2)
use this method when you need to cast from Object to a primitive type boolean in java code


castToFloat

public static float castToFloat(java.lang.Object o)
use this method when you need to cast from Object to a primitive type float in java code


objectsAreEquals

public static boolean objectsAreEquals(java.lang.Object o1,
                                       java.lang.Object o2,
                                       java.util.Comparator<java.lang.Object> valueComparator)

firebug

public static void firebug()

dump

public static java.lang.String dump(java.lang.Object o,
                                    boolean onlyType)

HTML

public static Element HTML(java.lang.Object obj)

document

public static Document document()

body

public static Element body()

escapeHTML

public static java.lang.String escapeHTML(java.lang.String s)