SWT Browser Widget: how to detect selection of text? [message #446255] |
Mon, 22 November 2004 17: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
|
|
|
Any ideas? Getting more and more desperate... [message #446376 is a reply to message #446255] |
Tue, 23 November 2004 23:21 |
Eclipse User |
|
|
|
Originally posted by: ozzo.freesurf.ch
Can anybody tell me if it's possible to get an event for a "text
selection" within the SWT Browser Widget?
Or is there no way?
(I have to build an application with a built-in browser and I need to
detect user actions such as "double-click on a word" or "mark a sentence
in a webpage".)
Thanks for any gimmicks... :)
Othmar
|
|
|
|
Re: Any ideas? Getting more and more desperate... [message #446423 is a reply to message #446384] |
Wed, 24 November 2004 18:58 |
Eclipse User |
|
|
|
Originally posted by: ozzo.freesurf.ch
Hi Ben,
Thanks for your message!
Seems that we need to be patient...
Regards,
Othmar
"Benjamin Pasero" <bpasero@rssowl.org> wrote in message
news:co1ira$pd0$1@www.eclipse.org...
> Hi Othmar,
>
> there is only a limited number of events that are fired from the
> browser widget. As far as I know Mouse Events, Selection Events,
> Key Events and else are not firing any event to the application.
>
> See the Event classes in org.eclipse.swt.browser package for a list
> of events that are fired from the browser widget.
>
> I am too waiting for a possibility to react on selection or mouse
> events (e.g. when a Link is clicked).
>
> Several bug reports are open to cover this issue.
>
> Regards,
> Ben
>
|
|
|
|
Re: Any ideas? Getting more and more desperate... [message #446557 is a reply to message #446543] |
Fri, 26 November 2004 12:29 |
Eclipse User |
|
|
|
Originally posted by: ozzo.freesurf.ch
Hi Chris,
Wow... thanks for your help! Your idea is very nice!
I don't have the method "browser.execute(..)" in my SWT Browser Widget
(did you write kind of a subclass of browser with a method "execute(..)"?),
so I couldn't run your code snippet.
But I understand the thinking behind your idea and I will try to find a way
for the "execute" !
Thanks very much!
Regards,
Othmar
"Christophe Cornu" <christophe_cornu@ca.ibm.com> wrote in message
news:co51aa$u23$1@www.eclipse.org...
> >My question: How is it possible to detect that the user marked a word
> >within the displayed webpage?
>
> Apparently web developers would use code like the one at:
> http://www.quirksmode.org/js/selected.html
>
> The following snippet is built after this technique. Should work for
simple
> documents (e.g. no multiple frames, no mouse up event trapped by the
> document itself) but you will need to see by yourself if it fits the HTML
> you are expecting or if it needs to be adapted.
>
> The snippet loads a simple HTML page. On every mouse up, it displays the
> selection in the SWT Text widget on the right. Hope this gives you some
> hints...
>
> Chris
>
> [... code snippet ...]
|
|
|
|
|
Re: Any ideas? Getting more and more confident... :) [message #446574 is a reply to message #446543] |
Fri, 26 November 2004 17: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 0.06084 seconds