Sam Saint-Pettersen
Sam Saint-Pettersen

Reputation: 511

Using Controlled Embedded Browser in SWT

Does anyone know how to use the "Controlled Embedded Browser" in SWT, which allows page manipulation? I can only find info on how to use the normal SWT browser, but I need to be able to interact with the loaded page. Thank you. Like this - http://publib.boulder.ibm.com/infocenter/btt/v7r0/index.jsp?topic=%2Fcom.ibm.btt.application_presentation.doc_7.0%2Fdoc%2Freference%2Frichclient%2Fcontrolembededbrowser.html - but there is no instruction on how to initiate such a class.

Upvotes: 0

Views: 2544

Answers (1)

didxga
didxga

Reputation: 6125

Here is an example from Eclipse SWT snippets website

Also this post might give you some insight on this. Using Java Objects in JavaScript in Eclipse SWT Browser Control

To expose Java Object from Eclipse to JavaScript, you need to create a class that extends BrowserFunction. The constructor of this class takes two arguments; the first one is Browser instance and second one is name of the the function that will be available in JavaScript code running the SWT browser control... ...

Code snippet

BrowserFunction:

import java.io.File;

import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.BrowserFunction;

public class ListFilesFunction extends BrowserFunction {

Browser browser = null;
String functionName = null;

public ListFilesFunction(Browser browser, String name) {
    super(browser, name);
    this.browser = browser;
    this.functionName = name;
}

public Object function (Object[] args)
{
    if (args.length == 0)
        browser.execute("alert('Function " + 
        functionName + " requires one argument - parent folder path');");

    File file = new File(args[0].toString());

    if (!file.exists())
        browser.execute("alert('Folder " + args[0] + 

" does not exist');");

    if (!file.isDirectory())
        browser.execute("alert('Path " + args[0] + " must be a folder');");

    return file.list();
}

}

associate this function with the browser control

public class View extends ViewPart
{
    Browser browserCtl = null;
    ...

public void createPartControl(Composite parent) {
    ...
    browserCtl = new Browser(parent, SWT.None);

    new ListFilesFunction(browserCtl, "getFiles");
    ...
}
...

}

invoke this function from JavaScript:

<html>
    <head>
        <script type='text/javascript'>
            files = getFiles("c:/");

        for (i = 0; i < files.length; i++)
        {
            document.writeln(files[i] + "<br>");
        }
        </script>
    </head>
    <body>

    </body>
</html>

Upvotes: 3

Related Questions