I too was frustrated by the fact that I could not get access to the
cursor position as I wanted to insert something at the current cursor
position.. So I thought.. I have getHTML() and setHTML().. and I have
the ability to set images at the current cursor position
Here is some code that I came up with yesterday.. It truly is a hack
(in my mind) but I was desparate for some kind of solution even if was
half baked..
--------------------------
package dtb.gwt.common.client.ui.util.richtextarea;
import com.google.gwt.user.client.ui.RichTextArea;
/**
* Inserts text at the current cursor position within a RichTextArea.
*
* @author David Balme
*
*/
public class StringInserter {
private int dummyMarkerCounter=0;
public void insertStringAtCurrentPosition(RichTextArea richTextArea,
String stringToAdd) {
dummyMarkerCounter++; // I have this counter to create a unique
marker every time that this method is called.
String dummyURL = "
http://wwwdummycom/MARKER_"+dummyMarkerCounter;
String imgPatternMatch = "<[Ii][Mm][Gg]\\s*.*[sS][Rr][Cc]\\s*=\\s*.*\
\x22
http://wwwdummycom/MARKER_"+dummyMarkerCounter+"\\x22>";
// Insert dummy image at current cursor position
richTextArea.getExtendedFormatter().insertImage(dummyURL);
String sHTML = richTextArea.getHTML();
// replace dummy image with the text we want, use regexp to find img
just inserted
sHTML = sHTML.replaceAll(imgPatternMatch, stringToAdd);
// Set the new text.. problem with this is that after the setHTML()
call,
// we lose the current cursor position (at least in
IE7).. <sigh>
richTextArea.setHTML(sHTML);
}
}
--------------------------
Example call within in a form that contains a RichTextArea
private StringInserter stringInserter = new StringInserter();
private void addTokenToRichTextArea(String sToken) {
stringInserter.insertStringAtCurrentPosition(richTextArea, "["+sToken
+"]");
> > Thanks.- Hide quoted text -
>
> - Show quoted text -