public class WidgetUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
WidgetUtil.CssSize
Wrap a css size value and its unit and translate back and forth to the
string representation.
E.g. |
static class |
WidgetUtil.ErrorUtil
Utility methods for displaying error message on components.
|
static class |
WidgetUtil.Reference<T>
Simple object to store another object.
|
static class |
WidgetUtil.TextRectangle |
Modifier and Type | Field and Description |
---|---|
static double |
PIXEL_EPSILON
The allowed value inaccuracy when comparing two double-typed pixel
values.
|
Constructor and Description |
---|
WidgetUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
alert(String string) |
static void |
browserDebugger()
Helper method for debugging purposes.
|
static void |
clearTextSelection()
JavaScript hack to clear text selection in various browsers.
|
static com.google.gwt.dom.client.Element |
cloneNode(com.google.gwt.dom.client.Element element,
boolean deep)
Clones given element as in JavaScript.
|
static void |
detachAttach(com.google.gwt.dom.client.Element element)
Detaches and re-attaches the element from its parent.
|
static String |
escapeAttribute(String attribute)
Escapes the string so it is safe to write inside an HTML attribute.
|
static String |
escapeHTML(String html)
Converts html entities to text.
|
static <T> T |
findWidget(com.google.gwt.dom.client.Element element)
Helper method to find first instance of any Widget found by traversing
DOM upwards from given element.
|
static <T> T |
findWidget(com.google.gwt.dom.client.Element element,
Class<? extends com.google.gwt.user.client.ui.Widget> class1)
Helper method to find first instance of given Widget type found by
traversing DOM upwards from given element.
|
static <T> T |
findWidget(com.google.gwt.dom.client.Element element,
Class<? extends com.google.gwt.user.client.ui.Widget> class1,
boolean exactMatch)
Helper method to find first instance of given Widget type found by
traversing DOM upwards from given element.
|
static void |
focus(com.google.gwt.dom.client.Element el)
Will (attempt) to focus the given DOM Element.
|
static void |
forceIERedraw(com.google.gwt.dom.client.Element e)
Performs a hack to trigger a re-layout in the IE browser.
|
static void |
forceWebkitRedraw(com.google.gwt.dom.client.Element element)
Force webkit to redraw an element.
|
static String |
getAbsoluteUrl(String url)
Resolve a relative URL to an absolute URL based on the current document's
location.
|
static double |
getBorderBottomThickness(com.google.gwt.dom.client.Element element)
Returns the thickness of the given element's bottom border.
|
static double |
getBorderLeftAndRightThickness(com.google.gwt.dom.client.Element element)
Returns the thickness of the given element's left and right borders.
|
static double |
getBorderLeftThickness(com.google.gwt.dom.client.Element element)
Returns the thickness of the given element's left border.
|
static double |
getBorderRightThickness(com.google.gwt.dom.client.Element element)
Returns the thickness of the given element's right border.
|
static double |
getBorderTopAndBottomThickness(com.google.gwt.dom.client.Element element)
Returns the combined thickness of the given element's top and bottom
borders.
|
static double |
getBorderTopThickness(com.google.gwt.dom.client.Element element)
Returns the thickness of the given element's top border.
|
static WidgetUtil.TextRectangle |
getBoundingClientRect(com.google.gwt.dom.client.Element e) |
static int |
getChildElementIndex(com.google.gwt.dom.client.Element childElement)
Returns the index of the childElement within its parent.
|
static com.google.gwt.dom.client.Element |
getElementFromPoint(int clientX,
int clientY)
Returns the topmost element of from given coordinates.
|
static com.google.gwt.dom.client.Element |
getElementUnderMouse(com.google.gwt.dom.client.NativeEvent event)
Find the element corresponding to the coordinates in the passed mouse
event.
|
static com.google.gwt.dom.client.Element |
getFocusedElement()
Gets the currently focused element.
|
static int |
getKeyCode(com.google.gwt.event.dom.client.KeyEvent<?> event)
Helper method for a bug fix #14041.
|
static int |
getNativeScrollbarSize() |
static int |
getRelativeX(com.google.gwt.dom.client.Element element,
com.google.gwt.dom.client.NativeEvent event)
Returns the X coordinate of an event relative to an element.
|
static int |
getRelativeY(com.google.gwt.dom.client.Element element,
com.google.gwt.dom.client.NativeEvent event)
Returns the Y coordinate of an event relative to an element.
|
static int |
getRequiredHeight(com.google.gwt.dom.client.Element element)
Gets the border-box height for the given element, i.e.
|
static int |
getRequiredHeight(com.google.gwt.user.client.ui.Widget widget) |
static int |
getRequiredHeightBoundingClientRect(com.google.gwt.dom.client.Element element)
Calculates the height of the element's bounding rectangle.
|
static double |
getRequiredHeightBoundingClientRectDouble(com.google.gwt.dom.client.Element element)
Calculates the height of the element's bounding rectangle to subpixel
precision.
|
static int |
getRequiredHeightComputedStyle(com.google.gwt.dom.client.Element element) |
static double |
getRequiredHeightComputedStyleDouble(com.google.gwt.dom.client.Element element) |
static double |
getRequiredHeightDouble(com.google.gwt.dom.client.Element element)
Gets the border-box height for the given element, i.e.
|
static int |
getRequiredWidth(com.google.gwt.dom.client.Element element)
Gets the border-box width for the given element, i.e.
|
static int |
getRequiredWidth(com.google.gwt.user.client.ui.Widget widget) |
static int |
getRequiredWidthBoundingClientRect(com.google.gwt.dom.client.Element element)
Calculates the width of the element's bounding rectangle.
|
static double |
getRequiredWidthBoundingClientRectDouble(com.google.gwt.dom.client.Element element)
Calculates the width of the element's bounding rectangle to subpixel
precision.
|
static int |
getRequiredWidthComputedStyle(com.google.gwt.dom.client.Element element) |
static int |
getRequiredWidthComputedStyleDouble(com.google.gwt.dom.client.Element element) |
static double |
getRequiredWidthDouble(com.google.gwt.dom.client.Element element)
Gets the border-box width for the given element, i.e.
|
static int |
getTouchOrMouseClientX(com.google.gwt.user.client.Event event)
A helper method to return the client position from an event.
|
static int |
getTouchOrMouseClientX(com.google.gwt.dom.client.NativeEvent event) |
static int |
getTouchOrMouseClientY(com.google.gwt.user.client.Event event)
A helper method to return the client position from an event.
|
static int |
getTouchOrMouseClientY(com.google.gwt.dom.client.NativeEvent currentGwtEvent) |
static boolean |
isAttachedAndDisplayed(com.google.gwt.user.client.ui.Widget widget)
Kind of stronger version of isAttached().
|
static boolean |
isDisplayed(com.google.gwt.dom.client.Element element)
Returns whether the given element is displayed.
|
static boolean |
isFocusedElementEditable()
Gets currently focused element and checks if it's editable.
|
static boolean |
isString(Object obj)
Returns whether the given object is a string.
|
static boolean |
isTouchEvent(com.google.gwt.user.client.Event event) |
static boolean |
isTouchEvent(com.google.gwt.dom.client.NativeEvent event) |
static boolean |
isTouchEventOrLeftMouseButton(com.google.gwt.user.client.Event event)
Checks if the given event is either a touch event or caused by the left
mouse button.
|
static boolean |
mayHaveScrollBars(com.google.gwt.dom.client.Element pe)
Detects what is currently the overflow style attribute in given element.
|
static int |
measureHorizontalBorder(com.google.gwt.dom.client.Element element) |
static int |
measureHorizontalPaddingAndBorder(com.google.gwt.dom.client.Element element,
int paddingGuess) |
static int |
measureMarginLeft(com.google.gwt.dom.client.Element element) |
static int |
measureVerticalBorder(com.google.gwt.dom.client.Element element) |
static int |
measureVerticalPaddingAndBorder(com.google.gwt.dom.client.Element element,
int paddingGuess) |
static float |
parseRelativeSize(String size) |
static boolean |
pixelValuesEqual(double num1,
double num2)
Compares two double values with the error margin of
PIXEL_EPSILON (i.e. |
static void |
redirect(String url)
Redirects the browser to the given url or refreshes the page if url is
null.
|
static double |
roundSizeDown(double size)
Rounds the given size down to a value which the browser will accept.
|
static double |
roundSizeUp(double size)
Rounds the given size up to a value which the browser will accept.
|
static void |
runWebkitOverflowAutoFix(com.google.gwt.dom.client.Element elem)
Run workaround for webkits overflow auto issue.
|
static void |
runWebkitOverflowAutoFixDeferred(com.google.gwt.dom.client.Element elem)
Defers the execution of
runWebkitOverflowAutoFix(Element) . |
static void |
scrollIntoViewVertically(com.google.gwt.dom.client.Element elem)
Scrolls an element into view vertically only.
|
static void |
setFloat(com.google.gwt.dom.client.Element element,
String value) |
static int |
setHeightExcludingPaddingAndBorder(com.google.gwt.dom.client.Element element,
int requestedHeight,
int verticalPaddingBorderGuess,
boolean requestedHeightIncludesPaddingBorder) |
static int |
setHeightExcludingPaddingAndBorder(com.google.gwt.user.client.ui.Widget widget,
String height,
int paddingBorderGuess) |
static void |
setSelectionRange(com.google.gwt.dom.client.Element elem,
int pos,
int length,
String direction)
Sets the selection range of an input element.
|
static void |
setStyleTemporarily(com.google.gwt.dom.client.Element element,
String styleProperty,
String tempValue)
Temporarily sets the
styleProperty to tempValue and then
resets it to its current value. |
static void |
setTextSelectionEnabled(com.google.gwt.dom.client.Element e,
boolean enable)
JavaScript hack to prevent text selection in various browsers.
|
static int |
setWidthExcludingPaddingAndBorder(com.google.gwt.dom.client.Element element,
int requestedWidth,
int horizontalPaddingBorderGuess,
boolean requestedWidthIncludesPaddingBorder) |
static int |
setWidthExcludingPaddingAndBorder(com.google.gwt.user.client.ui.Widget widget,
String width,
int paddingBorderGuess) |
static void |
simulateClickFromTouchEvent(com.google.gwt.user.client.Event touchevent,
com.google.gwt.user.client.ui.Widget widget) |
static void |
sinkOnloadForImages(com.google.gwt.dom.client.Element element) |
public static final double PIXEL_EPSILON
Since we're comparing pixels on a screen, epsilon must be less than 1. 0.49 was deemed a perfectly fine and beautifully round number.
public static void browserDebugger()
public static void redirect(String url)
url
- The url to redirect to or null to refreshpublic static int getKeyCode(com.google.gwt.event.dom.client.KeyEvent<?> event)
event
- public static com.google.gwt.dom.client.Element getElementFromPoint(int clientX, int clientY)
x
- y
- public static float parseRelativeSize(String size)
public static String escapeHTML(String html)
html
- public static String escapeAttribute(String attribute)
attribute
- The string to escapepublic static com.google.gwt.dom.client.Element cloneNode(com.google.gwt.dom.client.Element element, boolean deep)
element
- deep
- clone child tree alsopublic static int measureHorizontalPaddingAndBorder(com.google.gwt.dom.client.Element element, int paddingGuess)
public static int measureVerticalPaddingAndBorder(com.google.gwt.dom.client.Element element, int paddingGuess)
public static int measureHorizontalBorder(com.google.gwt.dom.client.Element element)
public static int measureVerticalBorder(com.google.gwt.dom.client.Element element)
public static int measureMarginLeft(com.google.gwt.dom.client.Element element)
public static int setHeightExcludingPaddingAndBorder(com.google.gwt.user.client.ui.Widget widget, String height, int paddingBorderGuess)
public static int setWidthExcludingPaddingAndBorder(com.google.gwt.user.client.ui.Widget widget, String width, int paddingBorderGuess)
public static int setWidthExcludingPaddingAndBorder(com.google.gwt.dom.client.Element element, int requestedWidth, int horizontalPaddingBorderGuess, boolean requestedWidthIncludesPaddingBorder)
public static int setHeightExcludingPaddingAndBorder(com.google.gwt.dom.client.Element element, int requestedHeight, int verticalPaddingBorderGuess, boolean requestedHeightIncludesPaddingBorder)
public static void setFloat(com.google.gwt.dom.client.Element element, String value)
public static int getNativeScrollbarSize()
public static void runWebkitOverflowAutoFixDeferred(com.google.gwt.dom.client.Element elem)
runWebkitOverflowAutoFix(Element)
.elem
- with overflow autopublic static void runWebkitOverflowAutoFix(com.google.gwt.dom.client.Element elem)
elem
- with overflow autopublic static void alert(String string)
public static int getRequiredWidth(com.google.gwt.dom.client.Element element)
element
- The element to checkpublic static double getRequiredWidthDouble(com.google.gwt.dom.client.Element element)
element
- The element to checkpublic static int getRequiredHeight(com.google.gwt.dom.client.Element element)
element
- The element to checkpublic static double getRequiredHeightDouble(com.google.gwt.dom.client.Element element)
element
- The element to checkpublic static int getRequiredWidthBoundingClientRect(com.google.gwt.dom.client.Element element)
In case the browser doesn't support bounding rectangles, the returned value is the offset width.
element
- the element of which to calculate the widthpublic static double getRequiredWidthBoundingClientRectDouble(com.google.gwt.dom.client.Element element)
In case the browser doesn't support bounding rectangles, the returned value is the offset width.
element
- the element of which to calculate the widthpublic static int getRequiredHeightComputedStyle(com.google.gwt.dom.client.Element element)
public static double getRequiredHeightComputedStyleDouble(com.google.gwt.dom.client.Element element)
public static int getRequiredWidthComputedStyle(com.google.gwt.dom.client.Element element)
public static int getRequiredWidthComputedStyleDouble(com.google.gwt.dom.client.Element element)
public static int getRequiredHeightBoundingClientRect(com.google.gwt.dom.client.Element element)
In case the browser doesn't support bounding rectangles, the returned value is the offset height.
element
- the element of which to calculate the heightpublic static double getRequiredHeightBoundingClientRectDouble(com.google.gwt.dom.client.Element element)
In case the browser doesn't support bounding rectangles, the returned value is the offset height.
element
- the element of which to calculate the heightpublic static int getRequiredWidth(com.google.gwt.user.client.ui.Widget widget)
public static int getRequiredHeight(com.google.gwt.user.client.ui.Widget widget)
public static boolean mayHaveScrollBars(com.google.gwt.dom.client.Element pe)
pe
- the element to detectpublic static void focus(com.google.gwt.dom.client.Element el)
el
- the element to focuspublic static <T> T findWidget(com.google.gwt.dom.client.Element element)
Note: If element
is inside some widget W
, and W
in turn is wrapped in a
Composite
C
, this
method will not find W
but returns C
. This may also be
the case with other Composite-like classes that hijack the event handling
of their child widget(s).
element
- the element where to start seeking of Widgetpublic static <T> T findWidget(com.google.gwt.dom.client.Element element, Class<? extends com.google.gwt.user.client.ui.Widget> class1)
Note: If element
is inside some widget W
, and W
in turn is wrapped in a
Composite
C
, this
method will not find W
. It returns either C
or null,
depending on whether the class parameter matches. This may also be the
case with other Composite-like classes that hijack the event handling of
their child widget(s).
Only accepts the exact class class1
if not null.
element
- the element where to start seeking of Widgetclass1
- the Widget type to seek for, null for anypublic static <T> T findWidget(com.google.gwt.dom.client.Element element, Class<? extends com.google.gwt.user.client.ui.Widget> class1, boolean exactMatch)
Note: If element
is inside some widget W
, and W
in turn is wrapped in a
Composite
C
, this
method will not find W
. It returns either C
or null,
depending on whether the class parameter matches. This may also be the
case with other Composite-like classes that hijack the event handling of
their child widget(s).
element
- the element where to start seeking of Widgetclass1
- the Widget type to seek forexactMatch
- true to only accept class1, false to also accept its
superclassespublic static void forceWebkitRedraw(com.google.gwt.dom.client.Element element)
element
- The element that should be redrawnpublic static void forceIERedraw(com.google.gwt.dom.client.Element e)
e
- The element to perform the hack onpublic static void detachAttach(com.google.gwt.dom.client.Element element)
element
- The element to detach and re-attachpublic static void sinkOnloadForImages(com.google.gwt.dom.client.Element element)
public static int getChildElementIndex(com.google.gwt.dom.client.Element childElement)
subElement
- public static void setStyleTemporarily(com.google.gwt.dom.client.Element element, String styleProperty, String tempValue)
styleProperty
to tempValue
and then
resets it to its current value. Used mainly to work around rendering
issues in IE (and possibly in other browsers)element
- The target elementstyleProperty
- The name of the property to settempValue
- The temporary valuepublic static int getTouchOrMouseClientX(com.google.gwt.user.client.Event event)
event
- public static com.google.gwt.dom.client.Element getElementUnderMouse(com.google.gwt.dom.client.NativeEvent event)
event
- the mouse event to get coordinates frompublic static int getTouchOrMouseClientY(com.google.gwt.user.client.Event event)
event
- public static int getTouchOrMouseClientY(com.google.gwt.dom.client.NativeEvent currentGwtEvent)
currentGwtEvent
- getTouchOrMouseClientY(Event)
public static int getTouchOrMouseClientX(com.google.gwt.dom.client.NativeEvent event)
event
- getTouchOrMouseClientX(Event)
public static boolean isTouchEvent(com.google.gwt.user.client.Event event)
public static boolean isTouchEvent(com.google.gwt.dom.client.NativeEvent event)
public static void simulateClickFromTouchEvent(com.google.gwt.user.client.Event touchevent, com.google.gwt.user.client.ui.Widget widget)
public static com.google.gwt.dom.client.Element getFocusedElement()
public static boolean isFocusedElementEditable()
public static boolean isAttachedAndDisplayed(com.google.gwt.user.client.ui.Widget widget)
widget
- public static void scrollIntoViewVertically(com.google.gwt.dom.client.Element elem)
elem
- The element to scroll into viewpublic static boolean isTouchEventOrLeftMouseButton(com.google.gwt.user.client.Event event)
event
- public static String getAbsoluteUrl(String url)
url
- a string with the relative URL to resolvepublic static void setSelectionRange(com.google.gwt.dom.client.Element elem, int pos, int length, String direction)
elem
- the html input element.pos
- the index of the first selected character.length
- the selection length.direction
- a string indicating the direction in which the selection was
performed. This may be "forward" or "backward", or "none" if
the direction is unknown or irrelevant.public static void setTextSelectionEnabled(com.google.gwt.dom.client.Element e, boolean enable)
e
- element for enabling or disabling text selectionenable
- true
if selection is enabled; false
if notpublic static void clearTextSelection()
public static boolean pixelValuesEqual(double num1, double num2)
PIXEL_EPSILON
(i.e. 0.49)num1
- the first value for which to compare equalitynum2
- the second value for which to compare equalitypublic static WidgetUtil.TextRectangle getBoundingClientRect(com.google.gwt.dom.client.Element e)
public static double getBorderTopThickness(com.google.gwt.dom.client.Element element)
The value is determined using computed style when available and calculated otherwise.
element
- the element to measurepublic static double getBorderBottomThickness(com.google.gwt.dom.client.Element element)
The value is determined using computed style when available and calculated otherwise.
element
- the element to measurepublic static double getBorderTopAndBottomThickness(com.google.gwt.dom.client.Element element)
The value is determined using computed style when available and calculated otherwise.
element
- the element to measurepublic static double getBorderLeftThickness(com.google.gwt.dom.client.Element element)
The value is determined using computed style when available and calculated otherwise.
element
- the element to measurepublic static double getBorderRightThickness(com.google.gwt.dom.client.Element element)
The value is determined using computed style when available and calculated otherwise.
element
- the element to measurepublic static double getBorderLeftAndRightThickness(com.google.gwt.dom.client.Element element)
The value is determined using computed style when available and calculated otherwise.
element
- the element to measurepublic static double roundSizeUp(double size)
size
- the value to roundpublic static double roundSizeDown(double size)
size
- the value to roundpublic static int getRelativeX(com.google.gwt.dom.client.Element element, com.google.gwt.dom.client.NativeEvent event)
element
- base element of the relative coordinatesevent
- with touch or mouse coordinatespublic static int getRelativeY(com.google.gwt.dom.client.Element element, com.google.gwt.dom.client.NativeEvent event)
element
- base element of the relative coordinatesevent
- with touch or mouse coordinatespublic static boolean isString(Object obj)
obj
- the object of which the type is examinedtrue
if the object is a string; false
if notpublic static boolean isDisplayed(com.google.gwt.dom.client.Element element)
This method returns false if either the given element or any of its
ancestors has the style display: none
applied.
element
- the element to test for visibilitytrue
if the element is displayed, false
otherwiseCopyright © 2018 Vaadin Ltd. All rights reserved.