diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/vaadin/terminal/gwt/client/ui/VTextField.java | 38 | ||||
-rw-r--r-- | src/com/vaadin/ui/TextField.java | 51 |
2 files changed, 89 insertions, 0 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/ui/VTextField.java b/src/com/vaadin/terminal/gwt/client/ui/VTextField.java index 7594bde3b1..50c81e19bd 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VTextField.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VTextField.java @@ -55,9 +55,11 @@ public class VTextField extends TextBoxBase implements Paintable, Field, private static final String CLASSNAME_PROMPT = "prompt"; private static final String ATTR_INPUTPROMPT = "prompt"; + private static final String VAR_CURSOR = "c"; private String inputPrompt = null; private boolean prompting = false; + private int lastCursorPos = -1; public VTextField() { this(DOM.createInputText()); @@ -142,6 +144,19 @@ public class VTextField extends TextBoxBase implements Paintable, Field, } valueBeforeEdit = uidl.getStringVariable("text"); + + if (uidl.hasAttribute("selpos")) { + final int pos = uidl.getIntAttribute("selpos"); + final int length = uidl.getIntAttribute("sellen"); + /* + * Gecko defers setting the text so we need to defer the selection. + */ + DeferredCommand.addCommand(new Command() { + public void execute() { + setSelectionRange(pos, length); + } + }); + } } private void setMaxLength(int newMaxLength) { @@ -197,12 +212,35 @@ public class VTextField extends TextBoxBase implements Paintable, Field, valueBeforeEdit = newText; } + /* + * also send cursor position, no public api yet but for easier + * extension + */ + updateCursorPosition(); + if (sendBlurEvent || sendValueChange) { client.sendPendingVariableChanges(); } } } + /** + * Updates the cursor position variable if it has changed since the last + * update. + * + * @return true iff the value was updated + */ + protected boolean updateCursorPosition() { + int cursorPos = getCursorPos(); + if (lastCursorPos != cursorPos) { + client.updateVariable(id, VAR_CURSOR, cursorPos, false); + lastCursorPos = cursorPos; + return true; + } else { + return false; + } + } + private static VTextField focusedTextField; public static void flushChangesFromFocusedTextField() { diff --git a/src/com/vaadin/ui/TextField.java b/src/com/vaadin/ui/TextField.java index aa4c7c8661..fff0beaf0c 100644 --- a/src/com/vaadin/ui/TextField.java +++ b/src/com/vaadin/ui/TextField.java @@ -90,6 +90,10 @@ public class TextField extends AbstractField implements */ private int maxLength = -1; + private int selectionPosition = -1; + + private int selectionLength; + /* Constructors */ /** @@ -174,6 +178,11 @@ public class TextField extends AbstractField implements if (inputPrompt != null) { target.addAttribute("prompt", inputPrompt); } + if (selectionPosition != -1) { + target.addAttribute("selpos", selectionPosition); + target.addAttribute("sellen", selectionLength); + selectionPosition = -1; + } // Adds the number of column and rows final int columns = getColumns(); @@ -635,4 +644,46 @@ public class TextField extends AbstractField implements removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener); } + /** + * Selects all text in the field. + * + * @since 6.4 + */ + public void selectAll() { + String text = getValue() == null ? "" : getValue().toString(); + setSelectionRange(0, text.length()); + } + + /** + * Sets the range of text to be selected. + * + * As a side effect the field will become focused. + * + * @since 6.4 + * + * @param pos + * the position of the first character to be selected + * @param length + * the number of characters to be selected + */ + public void setSelectionRange(int pos, int length) { + selectionPosition = pos; + selectionLength = length; + focus(); + requestRepaint(); + } + + /** + * Sets the cursor position in the field. As a side effect the field will + * become focused. + * + * @since 6.4 + * + * @param pos + * the position for the cursor + * */ + public void setCursorPosition(int pos) { + setSelectionRange(pos, 0); + } + } |