GWT Tk 0.2.3

asquare.gwt.tk.client.util
Class DomUtil

java.lang.Object
  extended by asquare.gwt.tk.client.util.DomUtil

public class DomUtil
extends Object

Utility methods for working with DOM.


Constructor Summary
DomUtil()
           
 
Method Summary
static void appendText(Element element, String text, boolean create)
          Appends text to a #text node which is a child of the element.
static void clean(Element parent)
          Removes all of the element's children.
static int eventGetAbsoluteX(Event event)
          Get the mouse x coordinate relative to the document's origin.
static int eventGetAbsoluteY(Event event)
          Get the mouse y coordinate relative to the document's origin.
static char eventGetCharCode(Event event)
          Gets the unicode character from a keypress event.
static char eventGetKeyCode(Event event)
          Gets a code representing a specific keyboard key which was pressed or released.
static List findElementsWithClass(Element element, String className, List result)
          Creates a list of all elements contained by the specified element (inclusive) which have the specified CSS class (in GWT terms stylename).
static String getAttribute(UIObject uio, String name)
          Gets an attribute on the specified UIObject's element.
static int getDocumentScrollHeight()
          Get the height of the document.
static int getDocumentScrollWidth()
          Get the width of the document.
static String getId(Element e)
           
static String getId(UIObject uio)
          Gets the id attribute of the specified UIObject's element.
static int getIntAttribute(UIObject uio, String name)
          Gets an int attribute on the specified UIObject's element.
static int getIntStyleAttribute(UIObject uio, String name)
          Gets a CSS style property for the specified UIObject's element.
static int getPixelStyleAttribute(UIObject uio, String name)
          Gets a CSS style property for the specified UIObject's element.
static String getStyleAttribute(UIObject uio, String name)
          Gets a CSS style property for the specified UIObject's element.
static int getViewportHeight()
          Get the height of the GWT application's primary viewport, aka "client area".
static int getViewportScrollX()
          Get the horizontal scroll offset of the GWT application's primary window.
static int getViewportScrollY()
          Get the vertical scroll offset of the GWT application's primary window.
static int getViewportWidth()
          Get the width of the GWT application's primary viewport, aka "client area".
static boolean isMac()
          Determines if the browser is running on the Mac OS.
static boolean isQuirksMode()
          Reads document.compatMode and returns true if the property exists and equals "BackCompat".
static boolean isWin()
          Determines if the browser is running on a Windows OS.
static void setAttribute(UIObject uio, String name, String value)
          Sets an attribute on the specified UIObject's element.
static void setId(Element e, String id)
           
static void setId(UIObject uio, String id)
          Sets the id attribute of the specified UIObject's element.
static void setIntAttribute(UIObject uio, String name, int value)
          Sets an int attribute on the specified UIObject's element.
static void setIntStyleAttribute(UIObject uio, String name, int value)
          Sets a CSS style property for the specified UIObject's element.
static void setPixelStyleAttribute(UIObject uio, String name, int value)
          Sets a CSS style property for the specified UIObject's element.
static void setStyleAttribute(UIObject uio, String name, String value)
          Sets a CSS style property for the specified UIObject's element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DomUtil

public DomUtil()
Method Detail

appendText

public static void appendText(Element element,
                              String text,
                              boolean create)
Appends text to a #text node which is a child of the element. Drills down recursivly via '.firstChild' until a #text node is found.

Parameters:
element - an element containing a text node as a first child (e.g. a Label or HTML)
text - text to append
create - true to create a text node if one is not found
Throws:
NullPointerException - if element is null
IllegalArgumentException - if element has no child nodes
JavaScriptException - if a #text node is not found

getId

public static String getId(UIObject uio)
Gets the id attribute of the specified UIObject's element.

Parameters:
uio - the UIObject
Returns:
the id or null if the id attribute is not set

setId

public static void setId(UIObject uio,
                         String id)
Sets the id attribute of the specified UIObject's element. Useful if you need to select a specific component in CSS.

Usage notes:

CSS Selection Example

      #fooPackage-barPanel {
        border: groove black 1px;
        text-align: left;
      }
 

Parameters:
uio - the UIObject
id - a unique id. It is a good practice to namespace ids to avoid future id confilicts, i.e. organization.project.foo.

getId

public static String getId(Element e)
Parameters:
e - a native DOM element
Returns:
the id or null if the id attribute is not set
See Also:
getId(UIObject)

setId

public static void setId(Element e,
                         String id)
Parameters:
e - a native DOM element
id - a unique id. It is a good practice to namespace ids to avoid future id confilicts, i.e. organization.project.foo.
See Also:
setId(UIObject, String)

getAttribute

public static String getAttribute(UIObject uio,
                                  String name)
Gets an attribute on the specified UIObject's element.

Parameters:
uio - a UIObject
name - an attribute name, in "camelCase"
Returns:
value the attribute value, or null if the attribute is not defined

setAttribute

public static void setAttribute(UIObject uio,
                                String name,
                                String value)
Sets an attribute on the specified UIObject's element.

Parameters:
uio - a UIObject
name - the attribute name, in "camelCase"
value - a value

getIntAttribute

public static int getIntAttribute(UIObject uio,
                                  String name)
Gets an int attribute on the specified UIObject's element.

Parameters:
uio - a UIObject
name - the attribute name, in "camelCase"
Returns:
the value, or 0 if the attribute is not defined

setIntAttribute

public static void setIntAttribute(UIObject uio,
                                   String name,
                                   int value)
Sets an int attribute on the specified UIObject's element.

Parameters:
uio - a UIObject
name - the attribute name, in "camelCase"
value - an int value

getStyleAttribute

public static String getStyleAttribute(UIObject uio,
                                       String name)
Gets a CSS style property for the specified UIObject's element.

Parameters:
uio - a UIObject
name - a CSS style property name, in "camelCase"
Returns:
value the style property value, or null if the property is not set

setStyleAttribute

public static void setStyleAttribute(UIObject uio,
                                     String name,
                                     String value)
Sets a CSS style property for the specified UIObject's element.

Parameters:
uio - a UIObject
name - a CSS style property name, in "camelCase"
value - a valid CSS property value

getIntStyleAttribute

public static int getIntStyleAttribute(UIObject uio,
                                       String name)
Gets a CSS style property for the specified UIObject's element.

Parameters:
uio - a UIObject
name - a CSS style property name, in "camelCase"
Returns:
value an int value, or null if the property is not set

setIntStyleAttribute

public static void setIntStyleAttribute(UIObject uio,
                                        String name,
                                        int value)
Sets a CSS style property for the specified UIObject's element.

Parameters:
uio - a UIObject
name - a CSS style property name, in "camelCase"
value - an int value

getPixelStyleAttribute

public static int getPixelStyleAttribute(UIObject uio,
                                         String name)
Gets a CSS style property for the specified UIObject's element.

Parameters:
uio - a UIObject
name - a CSS style property name, in "camelCase"
Returns:
value a screen pixel value, or null if the property is not set

setPixelStyleAttribute

public static void setPixelStyleAttribute(UIObject uio,
                                          String name,
                                          int value)
Sets a CSS style property for the specified UIObject's element.

Parameters:
uio - a UIObject
name - a CSS style property name, in "camelCase"
value - an int value, in screen pixels

findElementsWithClass

public static List findElementsWithClass(Element element,
                                         String className,
                                         List result)
Creates a list of all elements contained by the specified element (inclusive) which have the specified CSS class (in GWT terms stylename).

Parameters:
element - the element which is at the root of the hierarchy that you wish to search
className - the name of the CSS class to search for
result - a writable list which will be returned as the result (for recursion). Typically, you pass null and the list will be created on the fly.
Returns:
result a list containing 0 or more HTML elements

isQuirksMode

public static boolean isQuirksMode()
Reads document.compatMode and returns true if the property exists and equals "BackCompat". Supported on Opera, IE and Mozilla. Always returns false in Safari because the property is not defined.

Note: Safari does have a quirks mode, but you cannot determine this from JavaScript.

Returns:
true if the browser is rendering in quirks mode
See Also:

isMac

public static boolean isMac()
Determines if the browser is running on the Mac OS.

Returns:
true if the browser is running on a Mac

isWin

public static boolean isWin()
Determines if the browser is running on a Windows OS.

Returns:
true if the browser is running on Windows

getViewportWidth

public static int getViewportWidth()
Get the width of the GWT application's primary viewport, aka "client area". (This measurement does not include scrollbars.)

Returns:
the width in screen pixels

getViewportHeight

public static int getViewportHeight()
Get the height of the GWT application's primary viewport, aka "client area". (This measurement does not include scrollbars.)

Returns:
the height in screen pixels

getViewportScrollX

public static int getViewportScrollX()
Get the horizontal scroll offset of the GWT application's primary window. Measured from the left-most position in the document (i.e. document.documentElement origin) to the left edge of the viewport.

Returns:
0 or a positive value in screen pixels

getViewportScrollY

public static int getViewportScrollY()
Get the vertical scroll offset of the GWT application's primary window. Measured from the top-most position in the document (i.e. document.documentElement origin) to the top edge of the viewport.

Returns:
0 or a positive value in screen pixels

getDocumentScrollWidth

public static int getDocumentScrollWidth()
Get the width of the document.

Returns:
the width in screen pixels
See Also:
Mozilla reference, Microsoft reference

getDocumentScrollHeight

public static int getDocumentScrollHeight()
Get the height of the document.

Returns:
the height in screen pixels
See Also:
Mozilla reference, Microsoft reference

eventGetKeyCode

public static char eventGetKeyCode(Event event)
Gets a code representing a specific keyboard key which was pressed or released.

Parameters:
event - a keydown or keyup event
Returns:
a char representing the key pressed
Throws:
IllegalArgumentException - if event is other than onkeydown or onkeyup

eventGetCharCode

public static char eventGetCharCode(Event event)
Gets the unicode character from a keypress event. Returns 0 for keypresses which do not generate characters (i.e. Delete).

Parameters:
event - a keypress event
Returns:
a unicode character
Throws:
IllegalArgumentException - if event is other than onkeypress

eventGetAbsoluteX

public static int eventGetAbsoluteX(Event event)
Get the mouse x coordinate relative to the document's origin. This method differs from DOM.eventGetClientX(Event) in that it includes the area hidden by document scroll.

Parameters:
event - a native DOM event object
Returns:
the x coordinate in screen pixels

eventGetAbsoluteY

public static int eventGetAbsoluteY(Event event)
Get the mouse y coordinate relative to the document's origin. This method differs from DOM.eventGetClientY(Event) in that it includes the area hidden by document scroll.

Parameters:
event - a native DOM event object
Returns:
the y coordinate in screen pixels

clean

public static void clean(Element parent)
Removes all of the element's children.

Parameters:
parent - a DOM element
Throws:
IllegalArgumentException - if parent is null

GWT Tk 0.2.3

© 2007 Mat Gessel