Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT Browser Widget: how to detect selection of text?
SWT Browser Widget: how to detect selection of text? [message #446255] Mon, 22 November 2004 17:50 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 #446384 is a reply to message #446376] Wed, 24 November 2004 09:01 Go to previous messageGo to next message
Benjamin Pasero is currently offline Benjamin PaseroFriend
Messages: 337
Registered: July 2009
Senior Member
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

> 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 Go to previous messageGo to next message
Eclipse UserFriend
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 #446543 is a reply to message #446376] Thu, 25 November 2004 16:29 Go to previous messageGo to next message
Christophe Cornu is currently offline Christophe CornuFriend
Messages: 304
Registered: July 2009
Senior Member
>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

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.browser.*;

public class PRBrowserSCRIPT01 {
static String SCRIPT01 = "document.onmouseup = function() {if
(window.getSelection) { window.status = 'SCRIPT01'+window.getSelection(); }
else if (document.getSelection) { window.status =
'SCRIPT01'+document.getSelection(); } else if (document.selection) {
window.status = 'SCRIPT01'+document.selection.createRange().text; };
window.status='';}";

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);
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
}
public void completed(ProgressEvent event) {
browser.execute(SCRIPT01);
}
});
final Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
if (event.text.startsWith("SCRIPT01")) {
String selection = event.text.substring("SCRIPT01".length());
text.setText(selection);
}
}
});
/* Load an HTML document */
browser.setUrl(" http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/plat form-swt-home/dev.html");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: Any ideas? Getting more and more desperate... [message #446557 is a reply to message #446543] Fri, 26 November 2004 12:29 Go to previous messageGo to next message
Eclipse UserFriend
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 desperate... [message #446562 is a reply to message #446557] Fri, 26 November 2004 15:13 Go to previous messageGo to next message
Christophe Cornu is currently offline Christophe CornuFriend
Messages: 304
Registered: July 2009
Senior Member
Browser.execute is available starting with SWT 3.1 milestone 2. So for
example, you can try it by fetching Eclipse 3.1 M3 or the standalone SWT 3.1
M3 download.

Chris
Re: Any ideas? Getting more and more desperate... [message #446565 is a reply to message #446543] Fri, 26 November 2004 15:28 Go to previous messageGo to next message
Benjamin Pasero is currently offline Benjamin PaseroFriend
Messages: 337
Registered: July 2009
Senior Member
Hm, sounds not like the best way, sending data from IE via JavaScript over
changing the Status Text into Java.

Is there a certain reason that communication between SWT and Browser is so
limited? I understand that it maybe difficult to implement more ways of
communication, or that this task has not such a big priority for SWT, but
on the other hand I hope that now where browser.execute() was implemented
the other bug reports that cover Event handling in the Browser widget are
not ignored for the future.

Ben

>>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

> import org.eclipse.swt.*;
> import org.eclipse.swt.widgets.*;
> import org.eclipse.swt.layout.*;
> import org.eclipse.swt.browser.*;

> public class PRBrowserSCRIPT01 {
> static String SCRIPT01 = "document.onmouseup = function() {if
> (window.getSelection) { window.status = 'SCRIPT01'+window.getSelection(); }
> else if (document.getSelection) { window.status =
> 'SCRIPT01'+document.getSelection(); } else if (document.selection) {
> window.status = 'SCRIPT01'+document.selection.createRange().text; };
> window.status='';}";

> 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);
> browser.addProgressListener(new ProgressListener() {
> public void changed(ProgressEvent event) {
> }
> public void completed(ProgressEvent event) {
> browser.execute(SCRIPT01);
> }
> });
> final Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
> browser.addStatusTextListener(new StatusTextListener() {
> public void changed(StatusTextEvent event) {
> if (event.text.startsWith("SCRIPT01")) {
> String selection = event.text.substring("SCRIPT01".length());
> text.setText(selection);
> }
> }
> });
> /* Load an HTML document */
>
browser.setUrl(" http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/plat form-swt-home/dev.html");
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
Re: Any ideas? Getting more and more confident... :) [message #446574 is a reply to message #446543] Fri, 26 November 2004 17:18 Go to previous message
Eclipse UserFriend
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();
}
}
Previous Topic:custom control objects- their children, and FocusListener
Next Topic:Problems with German umlauts
Goto Forum:
  


Current Time: Sat Oct 31 02:09:08 GMT 2020

Powered by FUDForum. Page generated in 0.02002 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top