Hello All
For firefox, I am finally able to write a code to get and set cursor
location. Here is the solution.
The field selectionStart will have the cursor location. If its value
is equal to that of selectionEnd; there is no selection. Note that the
value of selectionEnd can be less than selectionStart.
You can set the new values in these fields and call setSelection() to
reflect the changes in the control.
I hope it will help some people.
// Fields
int selectionStart;
int selectionEnd;
// Add KeyboardListener
richTextArea.addKeyboardListener(new KeyboardListenerAdapter()
{
public void onKeyUp(Widget sender, char keyCode, int modifiers)
{
calculateSelectionIndices();
}
});
// JSNIs
private native Node getAnchorNode(IFrameElement frame)
/*-{
return frame.contentWindow.getSelection().anchorNode;
}-*/;
private native int getAnchorOffset(IFrameElement frame)
/*-{
return frame.contentWindow.getSelection().anchorOffset;
}-*/;
private native Node getFocusNode(IFrameElement frame)
/*-{
return frame.contentWindow.getSelection().focusNode;
}-*/;
private native int getFocusOffset(IFrameElement frame)
/*-{
return frame.contentWindow.getSelection().focusOffset;
}-*/;
private native void setSelection(IFrameElement frame, Document
document,
Node anchor,
int anchorOffset, Node focus, int focusOffset)
/*-{
var range = document.createRange();
range.setStart(anchor, anchorOffset);
range.setEnd(focus, focusOffset);
var selection = frame.contentWindow.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}-*/;
protected IFrameElement getIFrame()
{
return IFrameElement.as(textArea.getElement());
}
private void calculateSelectionIndices()
{
IFrameElement frame = getIFrame();
BodyElement b = frame.getContentDocument().getBody();
Node anchorNode = getAnchorNode(frame);
Node focusNode = getFocusNode(frame);
int anchorOffset = getAnchorOffset(frame);
int focusOffset = getFocusOffset(frame);
selectionStart = 0;
selectionEnd = 0;
Stack<Node> nodeStack = new Stack<Node>();
Node n = b.getFirstChild();
int charCount = 0;
while(n != null && (anchorNode != null || focusNode != null))
{
if(n == anchorNode)
{
selectionStart = charCount + anchorOffset;
anchorNode = null;
}
if(n == focusNode)
{
selectionEnd = charCount + focusOffset;
focusNode = null;
}
if(n.getNodeName().toLowerCase().equals("span"))
{
nodeStack.push(n);
n = n.getFirstChild();
}
else
{
if(n.getNodeType() == Node.TEXT_NODE)
{
charCount += Text.as(n).getLength();
}
else if(n.getNodeName().toLowerCase().equals("br"))
{
charCount++;
}
n = n.getNextSibling();
}
if(n == null && !nodeStack.isEmpty())
{
n = nodeStack.pop().getNextSibling();
}
}
}
private void setSelection()
{
IFrameElement frame = getIFrame();
Document d = frame.getContentDocument();
BodyElement b = d.getBody();
Node anchorNode = null;
Node focusNode = null;
Node lastTextNode = null;
int anchorOffset = -1;
int focusOffset = -1;
Stack<Node> nodeStack = new Stack<Node>();
Node n = b.getFirstChild();
int charCount = 0;
int i = 0;
while(n != null && (anchorNode == null || focusNode == null))
{
if(n.getNodeName().toLowerCase().equals("span"))
{
nodeStack.push(n);
n = n.getFirstChild();
}
else
{
if(n.getNodeType() == Node.TEXT_NODE)
{
lastTextNode = n;
i = charCount;
charCount += Text.as(n).getLength();
if(selectionStart < charCount && anchorNode ==
null)
{
anchorOffset = selectionStart - i;
anchorNode = n;
}
if(selectionEnd < charCount && focusNode ==
null)
{
focusOffset = selectionEnd - i;
focusNode = n;
}
}
else if(n.getNodeName().toLowerCase().equals("br"))
{
charCount++;
}
n = n.getNextSibling();
}
if(n == null && !nodeStack.isEmpty())
{
n = nodeStack.pop().getNextSibling();
}
}
if(anchorNode == null || focusNode == null)
{
anchorNode = focusNode = lastTextNode;
anchorOffset = focusOffset = Text.as(lastTextNode).getLength();
}
this.setSelection(frame, d, anchorNode, anchorOffset, focusNode,
focusOffset);
}
Omer Akhter
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---