Custom Vaadin GWT Component: Javascript <-> Java communication

Hello,

I created an application a while back when ITMill Toolkit 5 was out and I developed a custom GWT component for custom flash charts. I used the CoverFlow example to understand how this would work. All worked well. Now I have upgraded to the latest version of Vaadin 6. I have been able to port most functionality of this custom GWT component to Vaadin, however one important element is not fully working (Javascript to Java communication).

For Java to Javascript communication, I am sending JSON data from the the client to the server and using Java to Javascript communication (as done in the CoverFlow example), to send the data. This works in Vaadin.

For Javascript to Java communication, I am sending URL data (based on where the user clicks in the flash charts) from the client to the server (as also done in the CoverFlow example). Part of this works. When the user clicks on the flash charts, the Flash component will call the ‘sendURL()’ javascript function, and this is working as you will see a message box of the URL received. What is not working, is the call to the Java sendURL() method, i.e. “app.@com.example.ofc2.widgetset.client.ui.VOFC2::sendURL(Ljava/lang/String;)(url);” The javascript doesn’t appear to be calling on the Java method and I am unsure why.

I have compared my code to the new version of the CoverFlow and I think I am doing everything correctly (package name and class name is correct).

Below is the new code for Vaadin (from my com.example.ofc2.widgetset.client.ui.VOFC2.class):


  /**
     * Initialize the native javascript functions needed for the flash <-> GWT
     * communication
     * 
     * @param String
     *                id
     */
    public native void initializeMethods(String id) /*-{
	      var app = this;
	      
	      if($wnd.vaadin.ofc2 == null)
	      	var ofc2 = [];
	      else
	      	var ofc2 = $wnd.vaadin.ofc2;	        
	      
	     	ofc2['sendURL_' + id]
 = function(url) {
	         	app.@com.example.ofc2.widgetset.client.ui.VOFC2::sendURL(Ljava/lang/String;)(url);
	         	
	         	window.alert("URL="+url+",\napp="+app);

	      };
	      
	      $wnd.vaadin.ofc2 = ofc2;	   
	      	    	     
	  }-*/;  




     /**
     * Inform the server of the URL that was sent
     * 
     * @param String
     *                url
     */
    public void sendURL(String url) {
        if (uidlId == null || client == null)
            return;

        client.updateVariable(uidlId, "url", url, true);
    }


And here is the old code from Toolkit 5 (from my com.itmill.incubator.ui.ofc2.gwt.client.ui.IOFC2.class):

 
/**
     * Inform the server of the URL that was sent
     * 
     * @param String
     *                url
     */
    public void sendURL(String url) {
        if (uidlId == null || client == null)
            return;

        client.updateVariable(uidlId, "url", url, true);
    }
    
    /**
     * Initialize the native javascript functions needed for the flash <-> GWT
     * communication
     * 
     * @param String
     *                id
     */
    public native void initializeMethods(String id) /*-{
	      var app = this;
	      
	      if($wnd.itmill.ofc2 == null)
	      	var ofc2 = [];
	      else
	      	var ofc2 = $wnd.itmill.ofc2;	        
	      
	     	ofc2['sendURL_' + id]
 = function(url) {
	         	app.@com.itmill.incubator.ui.ofc2.gwt.client.ui.IOFC2::sendURL(Ljava/lang/String;)(url);
	      };
	      
	      $wnd.itmill.ofc2 = ofc2;	   
	      	    	     
	  }-*/;

Could you guys think of anything I may be doing wrong? I’m not very familiar with the invocation of Java methods from Javascript, perhaps I am not referencing the class correctly in Vaadin?

Thanks for your help,
Ajay

It looks like nobody has any ideas with this one.

I’ve been playing around the Javascript portion of the Java widget, and it looks like there is no error checking which makes debugging extremely difficult. I added some garbage code “fwejwefjewfj”, and it still compiles successfully:

   
/**
     * Initialize the native javascript functions needed for the flash <-> GWT
     * communication
     * 
     * @param String
     *                id
     */
    public native void initializeMethods(String id) /*-{
	      var app = this;
	      
	      if($wnd.vaadin.ofc2 == null)
	      	var ofc2 = [];
	      else
	      	var ofc2 = $wnd.vaadin.ofc2;	        
	      
	     	ofc2['sendURL_' + id]
 = function(url) {
	         	app.@com.example.ofc2.widgetset.client.ui.VOFC2::sendURL(Ljava/lang/String;)(url);
	         	
	         	[b]
fwejwefjewfj;
[/b]
	         	window.alert("NEW NEW NEW()="+url+app.@com.example.ofc2.widgetset.client.ui.VOFC2::sendURL(Ljava/lang/String;)(url));
	         	
	         	window.alert("URL="+url+",\napp="+app);

	      };
	      
	      $wnd.vaadin.ofc2 = ofc2;	   
	      	    	     
	  }-*/;        

Does anybody have any experience invoking Java through Javascript, using any other methods?

Thanks,
Ajay

Hi everyone,

I resolved my own issue :smiley:

It looks like this wasn’t a problem with Javascript ↔ Java communication at all. All of that code was correct. The issue was due to a mistake I made in my updateFromUIDL() function, the UIDL I was using was accidently set to null and causing everything else to fail.

I fixed this and it works now.

Cheers,
Ajay