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_integer.
/**
* Test that javascript can call java and pass an integer to java.
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback_with_integer() {
// 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.
// It's useful to run at least one function test on webkit1 locally.
// run locally. Skip on hudson that runs webkit1.
assumeFalse(webkit1SkipMsg(), (SwtTestUtil.isRunningOnEclipseOrgHudsonGTK && isWebkit1));
AtomicInteger returnInt = 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) {
Double returnedDouble = (Double) arguments[0];
// 5.0 -> 5
returnInt.set(returnedDouble.intValue());
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 ** with value of 5.
" jsCallbackToJava(5)\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(() -> returnInt.get() == 5);
String message = "Javascript should have passed an integer to java. But this did not happen";
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_with_javaReturningInt.
/**
* Test that javascript can call java, java returns an Integer back to javascript.
*
* It's a bit tricky to tell if javascript actually received the correct value from java.
* Solution: make a second function/callback that is called with the value that javascript received from java.
*
* Logic:
* 1) Java registers function callCustomFunction() by setting html body.
* 2) which in turn calls JavascriptCallback, which returns value 42 back to javascript.
* 3) javascript then calls JavascriptCallback_javascriptReceivedJavaInt() and passes it value received from java.
* 4) Java validates that the correct value (42) was passed to javascript and was passed back to java.
*
* loosely based on Snippet307.
*/
@Test
public void test_BrowserFunction_callback_with_javaReturningInt() {
// 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);
AtomicInteger returnInt = 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) {
return 42;
}
}
class JavascriptCallback_javascriptReceivedJavaInt extends // Note: Local class defined inside method.
BrowserFunction {
JavascriptCallback_javascriptReceivedJavaInt(Browser browser, String name) {
super(browser, name);
}
@Override
public Object function(Object[] arguments) {
Double returnVal = (Double) arguments[0];
// 4)
returnInt.set(returnVal.intValue());
return null;
}
}
String htmlWithScript = "<html><head>\n" + "<script language=\"JavaScript\">\n" + // Define a javascript function.
"function callCustomFunction() {\n" + " document.body.style.backgroundColor = 'red'\n" + // 2)
" var retVal = jsCallbackToJava()\n" + // This calls the javafunction that we registered. Set HTML body to return value.
" document.write(retVal)\n" + // 3)
" jsSuccess(retVal)\n" + "}" + "</script>\n" + "</head>\n" + "<body> If you see this, javascript did not receive anything from Java. This page should just be '42' </body>\n" + "</html>\n";
// 1)
browser.setText(htmlWithScript);
new JavascriptCallback(browser, "jsCallbackToJava");
new JavascriptCallback_javascriptReceivedJavaInt(browser, "jsSuccess");
browser.addProgressListener(callCustomFunctionUponLoad);
shell.open();
boolean passed = waitForPassCondition(() -> returnInt.get() == 42);
String message = "Java should have returned something back to javascript. But something went wrong";
assertTrue(message, passed);
}
use of org.eclipse.swt.browser.BrowserFunction in project eclipse-integration-commons by spring-projects.
the class StsBrowserManager method setClient.
public void setClient(Browser browser) {
this.browser = browser;
browser.execute("window.ide = {\n" + " call: function () {\n" + " return ide_call.apply(this, arguments);\n" + " }\n" + "}");
if (browser_function == null) {
browser_function = new BrowserFunction(browser, "ide_call") {
@Override
public Object function(Object[] arguments) {
call((String) arguments[0], (String) arguments[1]);
return false;
}
};
}
onLoadFunctions = new ArrayList<IEclipseToBrowserFunction>();
String oldUrl = currentUrl;
currentUrl = browser.getUrl();
// System.out.println("oldUrl: "+oldUrl);
// System.out.println("newUrl: "+currentUrl);
// Need to remove any query parameters that might break pattern matching for extensions
currentUrl = StringUtils.substringBeforeLast(currentUrl, "?");
currentUrl = StringUtils.substringBeforeLast(currentUrl, "&");
loadInitialFunctions();
}
use of org.eclipse.swt.browser.BrowserFunction in project pentaho-kettle by pentaho.
the class ThinDialog method createDialog.
public void createDialog(String title, String url, int options, Image logo) {
display = getParent().getDisplay();
dialog = new Shell(getParent(), options);
dialog.setText(title);
dialog.setImage(logo);
dialog.setSize(width, height);
dialog.setLayout(new FillLayout());
dialog.addListener(SWT.Traverse, e -> {
if (e.detail == SWT.TRAVERSE_ESCAPE) {
e.doit = false;
}
});
try {
browser = new Browser(dialog, SWT.NONE);
if (doAuthenticate) {
authenticate();
}
browser.setUrl(url);
browser.addCloseWindowListener(event -> {
Browser browse = (Browser) event.widget;
Shell shell = browse.getShell();
shell.close();
});
if (Const.isRunningOnWebspoonMode()) {
new BrowserFunction(browser, "getConnectionId") {
@Override
public Object function(Object[] arguments) {
try {
Class webSpoonUtils = Class.forName("org.pentaho.di.webspoon.WebSpoonUtils");
Method getConnectionId = webSpoonUtils.getDeclaredMethod("getConnectionId");
return getConnectionId.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return null;
}
}
};
}
} catch (Exception e) {
MessageBox messageBox = new MessageBox(dialog, SWT.ICON_ERROR | SWT.OK);
messageBox.setMessage("Browser cannot be initialized.");
messageBox.setText("Exit");
messageBox.open();
}
setPosition();
dialog.open();
}
use of org.eclipse.swt.browser.BrowserFunction in project pentaho-kettle by pentaho.
the class SpoonTabsDelegate method addSpoonBrowser.
public boolean addSpoonBrowser(String name, String urlString, boolean isURL, LocationListener listener, Map<String, Runnable> functions, boolean showControls) {
TabSet tabfolder = spoon.tabfolder;
try {
// OK, now we have the HTML, create a new browser tab.
// See if there already is a tab for this browser
// If no, add it
// If yes, select that tab
//
TabMapEntry tabMapEntry = findTabMapEntry(name, ObjectType.BROWSER);
if (tabMapEntry == null) {
CTabFolder cTabFolder = tabfolder.getSwtTabset();
final SpoonBrowser browser = new SpoonBrowser(cTabFolder, spoon, urlString, isURL, showControls, listener);
browser.getBrowser().addOpenWindowListener(new OpenWindowListener() {
@Override
public void open(WindowEvent event) {
if (event.required) {
event.browser = browser.getBrowser();
}
}
});
if (functions != null) {
for (String functionName : functions.keySet()) {
new BrowserFunction(browser.getBrowser(), functionName) {
public Object function(Object[] arguments) {
functions.get(functionName).run();
return null;
}
};
}
}
new BrowserFunction(browser.getBrowser(), "genericFunction") {
public Object function(Object[] arguments) {
try {
ExtensionPointHandler.callExtensionPoint(log, KettleExtensionPoint.SpoonBrowserFunction.id, arguments);
} catch (KettleException ignored) {
}
return null;
}
};
new BrowserFunction(browser.getBrowser(), "openURL") {
public Object function(Object[] arguments) {
Program.launch(arguments[0].toString());
return null;
}
};
PropsUI props = PropsUI.getInstance();
TabItem tabItem = new TabItem(tabfolder, name, name, props.getSashWeights());
tabItem.setImage(GUIResource.getInstance().getImageLogoSmall());
tabItem.setControl(browser.getComposite());
tabMapEntry = new TabMapEntry(tabItem, isURL ? urlString : null, name, null, null, browser, ObjectType.BROWSER);
tabMap.add(tabMapEntry);
}
int idx = tabfolder.indexOf(tabMapEntry.getTabItem());
// keep the focus on the graph
tabfolder.setSelected(idx);
return true;
} catch (Throwable e) {
boolean ok = false;
if (isURL) {
// Retry to show the welcome page in an external browser.
//
Status status = Launch.openURL(urlString);
ok = status.equals(Status.Success);
}
if (!ok) {
// Log an error
//
log.logError("Unable to open browser tab", e);
return false;
} else {
return true;
}
}
}
Aggregations