Hi guys,
I’m using a TextField (large text area) and a button.
When clicking the button, I’d like the server to inject some text where the current prompt is inside the text area.
Is it possible without writing a GWT component?
Many thanks.
John.
Hi guys,
I’m using a TextField (large text area) and a button.
When clicking the button, I’d like the server to inject some text where the current prompt is inside the text area.
Is it possible without writing a GWT component?
Many thanks.
John.
It is not. Sorry.
Fortunately this is fairly easy to do: extend text field by adding cursor position as variable to
the component. Then the server side can inject text to correct position and even set the
cursor position if needed.
Sami - you have created such a component - would you like to share code or advises?
True, I have implemented a widget that does insert “text” to current current cursor position - in this case a tab character.
The component actually implements an “auto-complete pattern” that allows user to fill text by choosing value from a list.
You can find the demo of this here:
http://sami.virtuallypreinstalled.com/autocomplete
The source code is available here:
http://dev.vaadin.com/svn/incubator/AutoComplete/src/com/itmill/incubator/autocomplete/
.
Your case differs a bit from mine. It seems a little simpler in terms of required client-side functionality, and you only need to send the cursor position to the server whenever the content of the TextField is sent. Otherwise the “immediate mode” should be enough.
Some quick ideas/pointers:
Client-side implementation: Extend the VTextArea and use getCursorPos() to get the the position and override the onChange to sync the extra variable to the server. Also, if you wish to set the cursor position from the server override the updateFromUIDL to and use getCursorPos.
Server-side implementation: Extend the TextField and add API to get (and set) cursor position.
In your application it is matter of putting the component into immediate mode and responding to the button click. Text and cursor position of the TextField should be up-to-date by then. Also, you probably need some kind of focus handling if multiple fields are present.
Great widget, thanks!
Thank you guys. I’ll try that tomorrow.
It works nicely guys, thank you very much!
The example has really been helpful.
For your information, I don’t get the cursor position to the server (every time the user presses a key). I rather send a text to insert to the GWT widget when needed (when the user presses an insert button).
Nice to hear you got it working! That direct text injection approach is actually much better in this case.
I’m trying to do something comparable with RichTextArea. I’ve added a string variable to the server component’s paint method that is received at the client.
In paintContent …
if (injectedString != null)
target.addVariable(this,“injectedString”,injectedString);
In updateFromUIDL…
if (uidl.hasVariable(“injectedString”)) {
rta.getFormatter().insertHTML(uidl.getStringVariable(“injectedString”));
This appears to work first time around, and injects content at the prompt. Subsequent times, it puts text at beggining of prompt.
Mysterious.
Any suggestions for better ways to do this?
Many thanks
Peter