SWT Browser Widget: how to detect selection of text? [message #446255] |
Mon, 22 November 2004 12:50  |
Eclipse User |
|
|
|
Originally posted by: ozzo.freesurf.ch
Hi,
I played around with the SWT Browser widget.
My question: How is it possible to detect that the user marked a word
within the displayed webpage?
Example:
webpage: <html><body>Hello World.</body></html>
Now the user double-clicks on "World" - so this word will be
selected/marked.
I'd like to have an event for this and find out which word the user
marked.
How can I do this? Is it possible?
I tried several "Listeners" but I couldnt find a way to detect this action
of a user within a webpage.
Thanks for any help!
Regards,
Othmar
|
|
|
|
|
|
|
|
|
|
Re: Any ideas? Getting more and more confident... :) [message #446574 is a reply to message #446543] |
Fri, 26 November 2004 12:18  |
Eclipse User |
|
|
|
Originally posted by: ozzo.freesurf.ch
Hello all,
I downloaded now SWT 3.1 M3 and it works nicely.
I modified the code snippet of Chris a little bit, so it works on my
Computer ( WinXP and IE 6.0 ). I changed the mouse event handling - it's now
done with "document.attachEvent(...)".
Until now, I didn't test it on MAC (Safari) or UNIX (Mozilla) - probably
some more JavaScript will be necessary for a correct "MouseUp"-handling.
Below there is the modified code snippet.
Thanks again very much! I wish a nice weekend to all,
Othmar
--- code ---
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.browser.*;
public class JavaScriptSelection {
static String SELECTIONSCRIPT =
"function handleSelection() { " +
" var selTxt = ''; " +
" if (window.getSelection) { " +
" selTxt = window.getSelection(); " +
" } else if (document.getSelection) { " +
" selTxt = document.getSelection(); " +
" } else if (document.selection) { " +
" selTxt = document.selection.createRange().text; " +
" } " +
" window.status = '::SELECTION::' + selTxt; " +
"} " +
"document.attachEvent('onmouseup', handleSelection); ";
public static void main(String [] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Browser browser = new Browser(shell, SWT.NONE);
final Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
}
public void completed(ProgressEvent event) {
browser.execute(SELECTIONSCRIPT);
}
});
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
if (event.text.startsWith("::SELECTION::")) {
String selection = event.text.substring("::SELECTION::".length());
text.setText(selection);
}
}
});
/* Load an HTML document */
browser.setUrl("http://dev.eclipse.org/");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
|
|
|
Powered by
FUDForum. Page generated in 1.08256 seconds