use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.
the class Bug510905_Browser_JsConsole method main.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(500, 600);
GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
final Text jsConsole = new Text(shell, SWT.BORDER);
// jsConsole.setText("document.body.innerHTML = theJavaFunction(123, 'hello', null, true)");
// Case where there are no paramaters.
jsConsole.setText("document.body.innerHTML = theJavaFunction()");
jsConsole.setSelection(jsConsole.getText().length());
GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
jsConsole.setLayoutData(data);
final Browser browser = new Browser(shell, SWT.NONE);
browser.setText("hello <b>world!</b>");
data = new GridData(SWT.FILL, SWT.FILL, true, true);
browser.setLayoutData(data);
jsConsole.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.keyCode == 13) {
// 13 = Enter
browser.execute(jsConsole.getText());
}
}
});
Button loadNewPage = new Button(shell, SWT.PUSH);
loadNewPage.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
loadNewPage.setText("Load new Page");
loadNewPage.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
browser.setText("New page!" + count++);
}
});
// BrowserFunction Code
@SuppressWarnings("unused") final BrowserFunction function = new CustomFunction(browser, "theJavaFunction");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback_with_boolean.
/**
* Test that javascript can call java and pass a Boolean to java.
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback_with_boolean() {
// On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
// culprit seems to be the main_context_iteration() call in shell.setVisible().
// See Bug 509587. Solution: Webkit2.
assumeFalse(webkit1SkipMsg(), isWebkit1);
AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
class JavascriptCallback extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
Boolean returnBool = (Boolean) arguments[0];
javaCallbackExecuted.set(returnBool);
return null;
}
}
String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
"function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + // This calls the javafunction that we registered.
" jsCallbackToJava(true)\n" + "}" + "</script>\n" + "</head>\n" + "<body> I'm going to make a callback to java </body>\n" + "</html>\n";
browser.setText(htmlWithScript);
new JavascriptCallback(browser, "jsCallbackToJava");
browser.addProgressListener(callCustomFunctionUponLoad);
shell.open();
boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
String message = "Javascript did not pass a boolean back to java";
assertTrue(message, passed);
}
use of org.eclipse.swt.browser.BrowserFunction in project pentaho-kettle by pentaho.
the class ConnectionDialog method open.
public void open(String title, String connectionName) {
StringBuilder clientPath = new StringBuilder();
clientPath.append(getClientPath());
if (connectionName != null) {
clientPath.append("#!/summary");
clientPath.append("?connection=").append(connectionName);
} else {
clientPath.append("#!/intro");
}
super.createDialog(title, getRepoURL(clientPath.toString()), OPTIONS, LOGO);
super.dialog.setMinimumSize(630, 630);
new BrowserFunction(browser, "close") {
@Override
public Object function(Object[] arguments) {
browser.dispose();
dialog.close();
dialog.dispose();
return true;
}
};
new BrowserFunction(browser, "setTitle") {
@Override
public Object function(Object[] arguments) {
dialog.setText((String) arguments[0]);
return true;
}
};
new BrowserFunction(browser, "browse") {
@Override
public Object function(Object[] arguments) {
FileDialog dialog = new FileDialog(getParent().getShell(), SWT.OPEN);
return dialog.open();
}
};
while (!dialog.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback_afterPageReload.
/**
* Test that a callback works even after a new page is loaded.
* I.e, BrowserFunctions should have to be re-initialized after a page load.
*
* Logic:
* - load a page.
* - Register java callback.
* - call java callback from javascript. (exec)
*
* - java callback instantiates new page load.
* - new page load triggers 'completed' listener
* - completed listener calls the registered function again.
*
* - once regiseterd function is called a 2nd time, it sets the test to pass.
*/
@Test
public void test_BrowserFunction_callback_afterPageReload() {
// On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
// culprit seems to be the main_context_iteration() call in shell.setVisible().
// See Bug 509587. Solution: Webkit2.
assumeFalse(webkit1SkipMsg(), isWebkit1);
AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
AtomicInteger callCount = new AtomicInteger(0);
class JavascriptCallback extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
if (callCount.get() == 0) {
callCount.set(1);
browser.setText("2nd page load");
} else {
javaCallbackExecuted.set(true);
}
return null;
}
}
browser.setText("1st (initial) page load");
new JavascriptCallback(browser, "jsCallbackToJava");
browser.execute("jsCallbackToJava()");
// see if function still works after a page change:
browser.addProgressListener(completedAdapter(e -> browser.execute("jsCallbackToJava()")));
shell.open();
boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
String message = "A javascript callback should work after a page has been reloaded. But something went wrong";
assertTrue(message, passed);
}
use of org.eclipse.swt.browser.BrowserFunction in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_browser_Browser method test_BrowserFunction_callback.
/**
* Test that javascript can call java.
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback() {
// On webkit1, this test works if ran on it's own. But sometimes in test-suite with other tests it causes jvm crash.
// culprit seems to be the main_context_iteration() call in shell.setVisible().
// See Bug 509587. Solution: Webkit2.
assumeFalse(webkit1SkipMsg(), isWebkit1);
AtomicBoolean javaCallbackExecuted = new AtomicBoolean(false);
class JavascriptCallback extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
javaCallbackExecuted.set(true);
return null;
}
}
String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
"function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + // This calls the javafunction that we registered.
" jsCallbackToJava()\n" + "}" + "</script>\n" + "</head>\n" + "<body> I'm going to make a callback to java </body>\n" + "</html>\n";
browser.setText(htmlWithScript);
new JavascriptCallback(browser, "jsCallbackToJava");
browser.addProgressListener(callCustomFunctionUponLoad);
shell.open();
boolean passed = waitForPassCondition(() -> javaCallbackExecuted.get());
String message = "Java failed to get a callback from javascript. Test timed out";
assertTrue(message, passed);
}
Aggregations