}
}
+ /**
+ * Sets the selection range of an input element.
+ *
+ * We need this JSNI function to set selection range so that we can use the
+ * optional direction attribute to set the anchor to the end and the focus
+ * to the start. This makes Firefox work the same way as other browsers
+ * (#13477)
+ *
+ * @param elem
+ * the html input element.
+ * @param pos
+ * the index of the first selected character.
+ * @param length
+ * the selection length.
+ * @param 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.
+ *
+ * @since
+ */
+ public native static void setSelectionRange(Element elem, int pos,
+ int length, String direction)
+ /*-{
+ try {
+ elem.setSelectionRange(pos, pos + length, direction);
+ } catch (e) {
+ // Firefox throws exception if TextBox is not visible, even if attached
+ }
+ }-*/;
+
/**
* Wrap a css size value and its unit and translate back and forth to the
* string representation.<br/>
* @return true if the two sizes are equals, otherwise false.
*/
public static boolean equals(String cssSize1, String cssSize2) {
- return CssSize.fromString(cssSize1).equals(CssSize.fromString(cssSize2));
+ return CssSize.fromString(cssSize1).equals(
+ CssSize.fromString(cssSize2));
}
}
@Override
public void setSelectionRange(int pos, int length) {
if (textInputEnabled) {
- super.setSelectionRange(pos, length);
+ /*
+ * set selection range with a backwards direction: anchor at the
+ * back, focus at the front. This means that items that are too
+ * long to display will display from the start and not the end
+ * even on Firefox.
+ *
+ * We need the JSNI function to set selection range so that we
+ * can use the optional direction attribute to set the anchor to
+ * the end and the focus to the start. This makes Firefox work
+ * the same way as other browsers (#13477)
+ */
+ Util.setSelectionRange(getElement(), pos, length, "backward");
+
} else {
- super.setSelectionRange(0, getValue().length());
+ /*
+ * Setting the selectionrange for an uneditable textbox leads to
+ * unwanted behaviour when the width of the textbox is narrower
+ * than the width of the entry: the end of the entry is shown
+ * instead of the beginning. (see #13477)
+ *
+ * To avoid this, we set the caret to the beginning of the line.
+ */
+
+ super.setSelectionRange(0, 0);
}
}
+
}
@Deprecated
private void setText(final String text) {
/**
- * To leave caret in the beginning of the line.
- * SetSelectionRange wouldn't work on IE
- * (see #13477)
+ * To leave caret in the beginning of the line. SetSelectionRange
+ * wouldn't work on IE (see #13477)
*/
Direction previousDirection = tb.getDirection();
tb.setDirection(Direction.RTL);