Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UlcFxBrowser Questions
#1
Some questions to the great new UlcFxBrowser Wink

- How does the communication in the background work between browser and ulc?
- What kind of parameters can one pass except the obvious ones (string, boolean..). Is it possible to pass some kind of objects / json from UlcFxBrowser to the server?
- Is it possible to debug it somehow?

Thanks & Best regards
Reply
#2
Hi,

You have access to premium support. Please post on Premium support forum for guaranteed response time.

On Developer Forum response time is not guaranteed.

> How does the communication in the background work between browser and ulc?

JS in Browser to ULC invocation is done via JSObject.

ULC to JS execution in Browser is done via api on WebView's WebEngine.

> What kind of parameters can one pass except the obvious ones (string, boolean..). Is it possible to pass some kind of objects / json from UlcFxBrowser to the server?

By default from JS to ULCFxBrowser client proxy you can pass String and the ULCFxBrowser client passes the string to ULC App on server.

It is not possible pass a JSON from JS to ULCFxBrowser client proxy. However you can stringify the JSON and pass it as shown in snippet below. Moreover the snippet shows how to register a custom handler to handle JS to Java calls:

Code:
import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBorderLayoutPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.development.DevelopmentRunner;
import com.ulcjava.ext.fxbrowser.client.UIFxBrowser;
import com.ulcjava.ext.fxbrowser.server.ULCFxBrowser;

import javax.swing.SwingUtilities;

import java.util.Map;


public class ULCCallJavaFromJSApp extends AbstractApplication {
    private ULCFxBrowser browser;
    
    @Override
    public void start() {
        browser = new MyULCFxBrowser();
        browser.addInvocationTarget(this);
        
        browser.setContent("<html><body><button onclick=\"myFunction()\">Click me</button> <p id=\"demo\"></p>"
                + "<script>function myFunction()" + "{"
                + "var text = '{\"name\":\"John Johnson\",\"street\":\"Oslo West 16\",\"phone\":\"555 1234567\"}';"
                + "var obj = JSON.parse(text);"
                + "document.getElementById(\"demo\").innerHTML = obj.name + \"<br>\" + obj.street + \"<br>\" + obj.phone;"
                + "ulc.invokeULC('testInvokeULC');ulc.invokeULC('testInvokeULCWithParam', obj);"
                + "myHandler.call('testInvokeULCWithParam', JSON.stringify(obj));" + "}" + "</script></body></html>");
        
        ULCButton jsToJavaCall = new ULCButton("JSToJavaCall");
        jsToJavaCall.addActionListener(new IActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                browser.executeScript("ulc.invokeULC('testInvokeULC');");
                browser.executeScript("ulc.invokeULC('testInvokeULCWithParam','test');");
            }
        });
        
        ULCFrame frame = new ULCFrame("ULCFxBrowserApp");
        frame.getContentPane().add(browser, ULCBorderLayoutPane.CENTER);
        frame.getContentPane().add(jsToJavaCall, ULCBorderLayoutPane.SOUTH);
        frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
        frame.setSize(1000, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        DevelopmentRunner.setApplicationClass(ULCCallJavaFromJSApp.class);
        DevelopmentRunner.main(args);
    }
    
    public void testInvokeULC() {
        System.out.println("called.testInvokeULC()");
    }
    
    public void testInvokeULCWithParam(String param) {
        System.out.println("testInvokeULCWithParam(), param:" + param);
    }
    
    class MyULCFxBrowser extends ULCFxBrowser {
        @Override
        protected String typeString() {
            return MyUIFxBrowser.class.getName();
        }
    }
    
    public static class MyUIFxBrowser extends UIFxBrowser {
        @Override
        protected Map<String, Object> createJavaScriptToJavaHandlers() {
            Map<String, Object> handlers = super.createJavaScriptToJavaHandlers();
            handlers.put("myHandler", new CustomJavaHandler());
            return handlers;
        }
        
        public class CustomJavaHandler {
            
            public void call(final String methodName, final Object parameter) {
                System.out.println("CustomJavaHandler.call(), param:" + parameter);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Object[] arguments = parameter != null ? new Object[] {methodName, parameter} : new Object[] {methodName};
                        invokeULC("invokeScriptTargets", arguments);
                    }
                });
            }
        }
    }
}

Thanks

Janak
Reply


Forum Jump: