Class JSOHelper

java.lang.Object
com.smartgwt.client.util.JSOHelper

public class JSOHelper extends Object
Utility class containing many useful static helper methods.

  • Method Details

    • getClassName

      public static String getClassName(JavaScriptObject javaScriptObject)
      Returns the javascript class name.
      Returns:
    • isScClassInstance

      public static boolean isScClassInstance(JavaScriptObject javaScriptObject)
      Returns whether the supplied JavaScriptObject is a SmartClient class instance.
      Parameters:
      the - object to evaluate
      Returns:
      whether object is an instance
    • isScClassObject

      public static boolean isScClassObject(JavaScriptObject javaScriptObject)
      Returns whether the supplied JavaScriptObject is a SmartClient class (a Class object itself, rather than an instance of a class).
      Parameters:
      the - object to evaluate
      Returns:
      whether object is a Class object
    • eval

      public static JavaScriptObject eval(String jsFrag)
      Evaluate the passed string as Javascript
      Parameters:
      jsFrag - the string to evaluate
      Returns:
      the JavaScriptObject upon evaluation
    • isJSO

      public static boolean isJSO(Object object)
    • doubleValue

      public static double doubleValue(Number num)
    • getAttribute

      public static String getAttribute(JavaScriptObject elem, String attr)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, String value)
    • getAttributeAsJavaScriptObject

      public static JavaScriptObject getAttributeAsJavaScriptObject(JavaScriptObject elem, String attr)
    • getAttributeAsJavaScriptObjectArray

      public static JavaScriptObject[] getAttributeAsJavaScriptObjectArray(JavaScriptObject elem, String attr)
    • toArray

      public static JavaScriptObject[] toArray(JavaScriptObject array)
    • isArray

      public static boolean isArray(JavaScriptObject jsObj)
    • toElementArray

      public static Element[] toElementArray(JavaScriptObject array)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, JavaScriptObject[] value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, int[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, float[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, double[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, String[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Integer[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Float[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Boolean[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Double[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Long[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Date[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, ValueEnum[] values)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Object value)
      Set the value of attribute "attr" in object "elem" to the Object "value"

      This method converts the value passed in to an equivalent object in JavaScript before storing on the underlying data object - see JavaToJavaScriptConversion for details of the conversions that take place.
      Developers can use setObjectAttribute(JavaScriptObject, String, Object) to store Java objects without converting to JavaScript.

      Parameters:
      elem - the JavaScriptObject on which to set the attribute
      attr - the attribute name
      value - the attribute value.
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, JavaScriptObject value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, int value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Number value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Long value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Boolean value)
    • setNullAttribute

      public static void setNullAttribute(JavaScriptObject elem, String attr)
    • deleteAttribute

      public static void deleteAttribute(JavaScriptObject elem, String attr)
    • deleteAttributeIfExists

      public static void deleteAttributeIfExists(JavaScriptObject elem, String attr)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, boolean value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, double value)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Function handler)
    • setAttribute

      public static void setAttribute(JavaScriptObject elem, String attr, Date value)
    • setObjectAttribute

      public static void setObjectAttribute(JavaScriptObject elem, String attr, Object object)
    • getAttributeAsElement

      public static Element getAttributeAsElement(JavaScriptObject elem, String attr)
    • getAttributeAsInt

      public static Integer getAttributeAsInt(JavaScriptObject elem, String attr)
    • getAttributeAsDouble

      public static Double getAttributeAsDouble(JavaScriptObject elem, String attr)
    • getAttributeAsDate

      public static Date getAttributeAsDate(JavaScriptObject elem, String attr)
    • getAttributeAsFloat

      public static Float getAttributeAsFloat(JavaScriptObject elem, String attr)
    • getAttributeAsIntArray

      public static int[] getAttributeAsIntArray(JavaScriptObject elem, String attr)
    • getAttributeAsDoubleArray

      public static double[] getAttributeAsDoubleArray(JavaScriptObject elem, String attr)
    • getAttributeAsStringArray

      public static String[] getAttributeAsStringArray(JavaScriptObject elem, String attr)
    • getJavaScriptObjectArraySize

      public static int getJavaScriptObjectArraySize(JavaScriptObject elem)
    • getIntValueFromJavaScriptObjectArray

      public static int getIntValueFromJavaScriptObjectArray(JavaScriptObject elem, int i)
    • getDoubleValueFromJavaScriptObjectArray

      public static double getDoubleValueFromJavaScriptObjectArray(JavaScriptObject elem, int i)
    • getStringValueFromJavaScriptObjectArray

      public static String getStringValueFromJavaScriptObjectArray(JavaScriptObject elem, int i)
    • getValueFromJavaScriptObjectArray

      public static JavaScriptObject getValueFromJavaScriptObjectArray(JavaScriptObject elem, int i)
    • getCellArray

      public static int[][] getCellArray(JavaScriptObject jsCells)
    • convertToCellArray

      public static JavaScriptObject convertToCellArray(int[][] cells)
    • getAttributeAsBoolean

      public static boolean getAttributeAsBoolean(JavaScriptObject elem, String attr)
      Returns attribute value set as a Boolean. For convenience in checking boolean properties, getAttributeAsBoolean will return Boolean false if the attribute value is null or not a Boolean. Use the three parameter variant of this API getAttributeAsBoolean(JavaScriptObject, String, boolean) if you want null returned for null attribute values.
      Parameters:
      elem - the JavaScriptObject containing the property
      attr - the property name
      Returns:
      the property value
    • getAttributeAsBoolean

      public static Boolean getAttributeAsBoolean(JavaScriptObject elem, String attr, boolean allowNull)
      Returns attribute value set as a Boolean. If the attribute value is null or not a Boolean, the return value depends upon allowNull. If allowNull is true, null will be returned; otherwise Boolean false will be returned. For a simpler approach that never returns null, use the two parameter variant of this API getAttributeAsBoolean(JavaScriptObject, String).
      Parameters:
      elem - the JavaScriptObject containing the property
      attr - the property name
      allowNull - whether to permit a null return value
      Returns:
      the property value
    • getAttributeAsObject

      public static Object getAttributeAsObject(JavaScriptObject elem, String attr)
    • getAttributeAsMap

      public static Map getAttributeAsMap(JavaScriptObject elem, String attr)
      Returns attribute "attr" of JavaScriptObject "elem", converted to a Map following the rules described in the JS -> Java conversion article
      Parameters:
      elem - JavaScriptObject containing the attribute
      attr - The attribute name
      Returns:
      The attribute converted to a Java Map
    • listToArray

      public static JavaScriptObject[] listToArray(List list)
    • arrayConvert

      public static JavaScriptObject arrayConvert(Object[] array)
    • arrayConvert

      public static JavaScriptObject arrayConvert(JavaScriptObject[] array)
    • arrayLength

      public static int arrayLength(JavaScriptObject array)
    • arrayGetObject

      public static Object arrayGetObject(JavaScriptObject array, int index)
    • arraySet

      public static void arraySet(JavaScriptObject array, int index, Object value)
    • arraySet

      public static void arraySet(JavaScriptObject array, int index, JavaScriptObject value)
    • getElementValueFromJavaScriptObjectArray

      public static Element getElementValueFromJavaScriptObjectArray(JavaScriptObject elem, int i)
      This is used to access Element array as JavaScriptObject
    • createObject

      public static JavaScriptObject createObject()
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(boolean[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(char[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(byte[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(short[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(int[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(float[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(double[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(long[] array)
    • convertToJava

      public static Object convertToJava(JavaScriptObject object, boolean listAsArray)
      Convert a JavaScriptObject to the appropriate type of Java Object, following the rules described in the JS -> Java conversion article
      Parameters:
      object - JavaScriptObject to convert
      listAsArray - Should arrays be converted to Object[] or List
      Returns:
      converted Java object. May be a Map, a List, an Object[], a JavaScriptObject, a Canvas or a FormItem, depending on the underlying JavaScript type
    • convertToJava

      public static Object convertToJava(JavaScriptObject object)
      Convert a JavaScriptObject to the appropriate type of Java Object, following the rules described in the JS -> Java conversion article. Any arrays encountered during conversion will be converted to Java ArrayLists
      Parameters:
      object - JavaScriptObject to convert
      Returns:
      converted Java object. May be a Map, a List, an Object[], a JavaScriptObject, a Canvas or a FormItem, depending on the underlying JavaScript type
    • convertToMap

      public static Map convertToMap(JavaScriptObject jsObj, boolean listAsArray)
      Convert a JavaScript object containing key:value pairs to a Java Map, following the rules described in the JS -> Java conversion article
      Parameters:
      jsObj - the JavaScript object to convert
      listAsArray - Should arrays be converted to Object[] or List
      Returns:
      the map
      Throws:
      IllegalArgumentException - if unable to convert the passed JavaScript object to a map
    • convertToMap

      public static Map convertToMap(JavaScriptObject jsObj)
      Convert a JavaScript object containing key:value pairs to a Java Map, following the rules described in the JS -> Java conversion article. Any arrays encountered during the conversion process will be converted to Java ArrayLists
      Parameters:
      jsObj - the JavaScript object to convert
      Returns:
      the map
      Throws:
      IllegalArgumentException - if unable to convert the passed JavaScript object to a map
    • convertToArray

      public static Object[] convertToArray(JavaScriptObject object)
      Convert a JavaScript object to an Object[], following the rules described in the JS -> Java conversion article. If the JavaScript object is not an array in Javascript, a new array will be created containing the converted object as the only entry.
      Parameters:
      object - the JavaScript object to convert
      Returns:
      the JavaScript object converted to a Java Object array
    • convertToList

      public static List convertToList(JavaScriptObject object)
      Convert a JavaScript object to a List, following the rules described in the JS -> Java conversion article. If the JavaScript object is not an array in JavaScript, a new List will be created containing the converted object as the only entry.
      Parameters:
      object - the JavaScript object to convert
      Returns:
      the JavaScript object converted to a Java List
    • convertToJavaScriptDate

      public static JsDate convertToJavaScriptDate(Date date)
    • convertToJavaDate

      public static Date convertToJavaDate(JavaScriptObject val)
    • convertToString

      public static String convertToString(Object obj)
    • isJavaDate

      public static boolean isJavaDate(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Date
    • isJavaNumber

      public static boolean isJavaNumber(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Number
    • isJavaInteger

      public static boolean isJavaInteger(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Integer
    • isJavaFloat

      public static boolean isJavaFloat(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Float
    • isJavaDouble

      public static boolean isJavaDouble(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Double
    • isJavaString

      public static boolean isJavaString(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java String
    • isJavaBoolean

      public static boolean isJavaBoolean(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Integer
    • isJavaList

      public static boolean isJavaList(Object obj)
    • isJavaMap

      public static boolean isJavaMap(Object obj)
    • isJavaArray

      public static boolean isJavaArray(Object obj)
      Parameters:
      obj - the object
      Returns:
      true if object is a Java Array
    • convertToJsArray

      public static <O extends JavaScriptObject> JsArray<O> convertToJsArray(O[] array)
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(Object[] array)
      Converts the parameter Object array to an equivalent Javascript array - see JavaToJavaScriptConversion for details of the conversions that take place.
      Parameters:
      array - the Java Object array to convert
    • convertToJavaScriptArray

      public static JavaScriptObject convertToJavaScriptArray(Object[] array, boolean strict)
    • throwUnconvertibleObjectException

      public static void throwUnconvertibleObjectException(Object object, String messageDetail)
    • toInteger

      public static Integer toInteger(int value)
    • toLong

      public static Long toLong(double value)
    • toFloat

      public static Float toFloat(float value)
    • toDouble

      public static Double toDouble(double value)
    • toDate

      public static Date toDate(double millis)
      Converts a time to a Java Date object.

      Note: If needing to convert a JavaScript object, it is preferable to use convertToJavaDate(JavaScriptObject) instead because that function is able to convert to LogicalDate and LogicalTime instances as needed.

      Parameters:
      millis - number of milliseconds since January 1, 1970, 00:00:00 GMT.
      Returns:
      a new Date representing the given time.
    • toDateJS

      public static JavaScriptObject toDateJS(Date date)
    • getJSLogicalDate

      public static JsDate getJSLogicalDate(Date date)
    • getJSLogicalDate

      public static JsDate getJSLogicalDate(int year, int month, int date)
    • getJSLogicalTime

      public static JsDate getJSLogicalTime(Date date)
    • getJSLogicalTime

      public static JsDate getJSLogicalTime(int hour, int minute, int second, int millisecond)
    • toBoolean

      public static Boolean toBoolean(boolean value)
    • createJavaScriptArray

      public static JavaScriptObject createJavaScriptArray()
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, Date value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, String value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, double value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, long value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, boolean value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, char value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, JavaScriptObject value)
    • setArrayValue

      public static void setArrayValue(JavaScriptObject array, int index, Object value)
    • getArrayValue

      public static String getArrayValue(JavaScriptObject array, int index)
    • getJSOArrayValue

      public static JavaScriptObject getJSOArrayValue(JavaScriptObject array, int index)
    • getObjectArrayValue

      public static Object getObjectArrayValue(JavaScriptObject array, int index)
    • getBooleanArrayValue

      public static Boolean getBooleanArrayValue(JavaScriptObject array, int index)
    • getIntArrayValue

      public static int getIntArrayValue(JavaScriptObject array, int index)
    • getfloatArrayValue

      public static float getfloatArrayValue(JavaScriptObject array, int index)
    • getdoubleArrayValue

      public static double getdoubleArrayValue(JavaScriptObject array, int index)
    • getIntegerArrayValue

      public static Integer getIntegerArrayValue(JavaScriptObject array, int index)
    • getFloatArrayValue

      public static Float getFloatArrayValue(JavaScriptObject array, int index)
    • getDoubleArrayValue

      public static Double getDoubleArrayValue(JavaScriptObject array, int index)
    • getDateArrayValue

      public static Date getDateArrayValue(JavaScriptObject array, int i)
    • getArrayLength

      public static int getArrayLength(JavaScriptObject array)
    • convertToJavaBooleanArray

      public static Boolean[] convertToJavaBooleanArray(JavaScriptObject array)
    • convertToJavaIntArray

      public static int[] convertToJavaIntArray(JavaScriptObject array)
    • convertToJavaInterArray

      public static Integer[] convertToJavaInterArray(JavaScriptObject array)
    • convertToJavaIntegerArray

      public static Integer[] convertToJavaIntegerArray(JavaScriptObject array)
    • convertToJavaStringArray

      public static String[] convertToJavaStringArray(JavaScriptObject array)
    • convertToJavaFloatArray

      public static Float[] convertToJavaFloatArray(JavaScriptObject array)
    • convertToJavaDoubleArray

      public static Double[] convertToJavaDoubleArray(JavaScriptObject array)
    • convertToJavaDateArray

      public static Date[] convertToJavaDateArray(JavaScriptObject array)
    • convertToJavaObjectArray

      public static Object[] convertToJavaObjectArray(JavaScriptObject array)
    • convertToJavaRefDataClassArray

      public static RefDataClass[] convertToJavaRefDataClassArray(JavaScriptObject nativeArray)
    • apply

      public static void apply(JavaScriptObject config, JavaScriptObject jsObj)
    • setAttribute

      public static void setAttribute(JavaScriptObject jsObj, String attr, Map valueMap)
      Sets attribute "attr" on Javascript object "jsObj" to the JavaScript equivalent of "valueMap". See JavaToJavaScriptConversion for details of the conversions that take place.
      Parameters:
      jsObj - the JavaScript object on which to set the attribute
      attr - the name of the attribute to set
      valueMap - the Java Map to convert and apply as the attribute value
    • convertMapToJavascriptObject

      public static JavaScriptObject convertMapToJavascriptObject(Map valueMap)
      Converts the parameter Map to an equivalent Javascript object - see JavaToJavaScriptConversion for details of the conversions that take place.
      Parameters:
      array - the Java Map to convert
    • convertMapToJavascriptObject

      public static JavaScriptObject convertMapToJavascriptObject(Map valueMap, boolean strict)
    • getProperties

      public static String[] getProperties(JavaScriptObject jsObj)
    • getPropertiesAsString

      public static String getPropertiesAsString(JavaScriptObject jsObj)
    • addProperties

      public static JavaScriptObject addProperties(JavaScriptObject destination, JavaScriptObject propertiesObject)
      Adds all properties and methods from the propertiesObject to the destination object.
      Parameters:
      destination - the destination object
      propertiesObject - the propertiesObject
    • cleanProperties

      public static JavaScriptObject cleanProperties(JavaScriptObject properties, boolean copyProperties)
      Removes non-transferable properties from the supplied object (e.g. widget ID), performing a copy first if requested.
      Parameters:
      properties - the supplied object to clean
      copyProperties - whether to copy the object first
      Returns:
      the cleaned properties object
    • callMethod

      public static Object callMethod(JavaScriptObject target, String methodName, Object[] arguments)
      Invoke a method on some JavaScript object, passing in the specified array of parameters.

      This method converts the specified parameters to a equivalent objects in JavaScript before calling the method in JavaScript scope, and will convert any return value to Java before returning it - see JavaToJavaScriptConversion for details of the conversions that take place.
      If the invoked method has no return value, this method will return null.

      Parameters:
      target -
      methodName -
      arguments -
      Returns: