Class WidgetUtil

java.lang.Object
com.vaadin.client.WidgetUtil

public class WidgetUtil extends Object
Utility methods which are related to client side code only.
Since:
1.0
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    Anything in, anything out.
    static <T extends com.google.gwt.core.client.JavaScriptObject>
    T
    Anything in, JSO out.
    static elemental.json.JsonObject
    Creates a new JsonObject with the JavaScript prototype.
    static elemental.json.JsonObject
    Creates a new JsonObject without any JavaScript prototype at all.
    static void
    Removes a JavaScript property from an object.
    static boolean
    equals(Object obj1, Object obj2)
    Checks whether the objects are equal either as Java objects (considering types and Java Object.equals(Object) method) or as JS values.
    static boolean
    equalsInJS(Object obj1, Object obj2)
    Checks whether the objects are equal as JS values.
    static String
    Resolve a relative URL to an absolute URL based on the current document's location.
    static Object
    getJsProperty(Object object, String name)
    Retrieves the value of a JavaScript property.
    static String[]
    getKeys(Object value)
    Gets all JavaScript property names of the given object.
    static boolean
    hasJsProperty(Object object, String name)
    Checks whether the provided object has or inherits a JavaScript property with the given name.
    static boolean
    Checks whether the provided object itself has a JavaScript property with the given name.
    static boolean
    Detects if an URL is absolute.
    static boolean
    Gets the boolean value of the provided value based on JavaScript semantics.
    static boolean
    isUndefined(Object property)
    Checks if the given value is explicitly undefined.
    static void
    Redirects the browser to the given url or refreshes the page if url is null
    static void
    Refreshes the browser.
    static void
    setJsProperty(Object object, String name, Object value)
    Assigns a value as JavaScript property of an object.
    static String
    stringify(elemental.json.JsonObject payload)
    When serializing the JsonObject we check the values for dom nodes and throw and exception if one is found as they should not be synced and may create cyclic dependencies.
    static String
    toPrettyJson(elemental.json.JsonValue json)
    Converts a JSON value to a formatted string.
    static void
    updateAttribute(elemental.dom.Element element, String attribute, String value)
    Updates the attribute value for the element to the given value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WidgetUtil

      public WidgetUtil()
  • Method Details

    • refresh

      public static void refresh()
      Refreshes the browser.
    • redirect

      public static void redirect(String url)
      Redirects the browser to the given url or refreshes the page if url is null
      Parameters:
      url - The url to redirect to or null to refresh
    • getAbsoluteUrl

      public static String getAbsoluteUrl(String url)
      Resolve a relative URL to an absolute URL based on the current document's location.
      Parameters:
      url - a string with the relative URL to resolve
      Returns:
      the corresponding absolute URL as a string
    • isAbsoluteUrl

      public static boolean isAbsoluteUrl(String url)
      Detects if an URL is absolute. URLs wihtout schema but starting with double slashes (e.g. //myhost/path} are considered absolute.
      Parameters:
      url - a string with the URL to check
      Returns:
      true if the url is absolute, otherwise false.
    • crazyJsCast

      public static <T> T crazyJsCast(Object value)
      Anything in, anything out. It's JavaScript after all. This method just makes the Java compiler accept the fact.
      Type Parameters:
      T - the object type
      Parameters:
      value - anything
      Returns:
      the same stuff
    • crazyJsoCast

      public static <T extends com.google.gwt.core.client.JavaScriptObject> T crazyJsoCast(Object value)
      Anything in, JSO out. It's JavaScript after all. This method just makes the Java compiler accept the fact. The regular crazy cast doesn't work for JSOs since the generics still makes the compiler insert a JSO check.
      Type Parameters:
      T - the object type
      Parameters:
      value - anything
      Returns:
      the same stuff
    • toPrettyJson

      public static String toPrettyJson(elemental.json.JsonValue json)
      Converts a JSON value to a formatted string.
      Parameters:
      json - the JSON value to stringify
      Returns:
      the JSON string
    • updateAttribute

      public static void updateAttribute(elemental.dom.Element element, String attribute, String value)
      Updates the attribute value for the element to the given value.

      If value is null then attribute is removed, otherwise value is set as its value.

      Parameters:
      element - the DOM element owning attribute
      attribute - the attribute to update
      value - the value to update
    • setJsProperty

      public static void setJsProperty(Object object, String name, Object value)
      Assigns a value as JavaScript property of an object.
      Parameters:
      object - the target object
      name - the property name
      value - the property value
    • getJsProperty

      public static Object getJsProperty(Object object, String name)
      Retrieves the value of a JavaScript property.
      Parameters:
      object - the target object
      name - the property name
      Returns:
      the value
    • hasOwnJsProperty

      public static boolean hasOwnJsProperty(Object object, String name)
      Checks whether the provided object itself has a JavaScript property with the given name. Inherited properties are not taken into account.
      Parameters:
      object - the target object
      name - the name of the property
      Returns:
      true if the object itself has the named property; false if it doesn't have the property of if the property is inherited
      See Also:
    • hasJsProperty

      public static boolean hasJsProperty(Object object, String name)
      Checks whether the provided object has or inherits a JavaScript property with the given name.
      Parameters:
      object - the target object
      name - the name of the property
      Returns:
      true if the object itself has or inherits the named property; false otherwise
      See Also:
    • isUndefined

      public static boolean isUndefined(Object property)
      Checks if the given value is explicitly undefined. null values returns false.
      Parameters:
      property - the value to be verified
      Returns:
      true is the value is explicitly undefined, false otherwise
    • deleteJsProperty

      public static void deleteJsProperty(Object object, String name)
      Removes a JavaScript property from an object.
      Parameters:
      object - the object from which to remove the property
      name - the name of the property to remove
    • createJsonObjectWithoutPrototype

      public static elemental.json.JsonObject createJsonObjectWithoutPrototype()
      Creates a new JsonObject without any JavaScript prototype at all. Not having any prototype is only relevant for objects that are displayed through the browser console.
      Returns:
      a new json object
    • createJsonObject

      public static elemental.json.JsonObject createJsonObject()
      Creates a new JsonObject with the JavaScript prototype.
      Returns:
      a new json object
    • isTrueish

      public static boolean isTrueish(Object value)
      Gets the boolean value of the provided value based on JavaScript semantics.
      Parameters:
      value - the value to check for truthness
      Returns:
      true if the provided value is trueish according to JavaScript semantics, otherwise false
    • getKeys

      public static String[] getKeys(Object value)
      Gets all JavaScript property names of the given object. This directly calls Object.keys.
      Parameters:
      value - the value to get keys for
      Returns:
      an array of key names
    • stringify

      public static String stringify(elemental.json.JsonObject payload)
      When serializing the JsonObject we check the values for dom nodes and throw and exception if one is found as they should not be synced and may create cyclic dependencies.
      Parameters:
      payload - JsonObject to stringify
      Returns:
      json string of given object
    • equals

      public static boolean equals(Object obj1, Object obj2)
      Checks whether the objects are equal either as Java objects (considering types and Java Object.equals(Object) method) or as JS values.
      Parameters:
      obj1 - an object
      obj2 - an object to be compared with a for deep equality
      Returns:
      true if the arguments are equal to each other and false otherwise
      See Also:
    • equalsInJS

      public static boolean equalsInJS(Object obj1, Object obj2)
      Checks whether the objects are equal as JS values.

      This check ignores object types and checks the values via JS ==. E.g. it means that an empty string equals to 0.

      Parameters:
      obj1 - an object
      obj2 - an object to be compared with a for deep equality
      Returns:
      true if the arguments are equal via JS == to each other and false otherwise