How to reveal the functionality of a class in GWT

I have a class library written in Java and I want to convert it to Javascript. All of the methods are pretty simple and mostly related to collection management. I have this GameControl class that I could create, and I want its methods to be exposed to other Javascript code on the page.

I was thinking to use GWT. I have a working project in GWT that compiles, but I can't figure out how to expose my instance (+ functionality) of the GameControl class.

I thought using JSNI to expose my object should work, but it doesn't. This is a short version of what it looks like right now:

GameEntryPoint.java

import com.google.gwt.core.client.EntryPoint;

public class GameEntryPoint implements EntryPoint {

    private GameControl _gameControl;

    @Override
    public void onModuleLoad() {
        _gameControl = new GameControl();
        expose();
    }


    public native void expose()/*-{
        $wnd.game = this.@game.client.GameEntryPoint::_gameControl;
    }-*/;

}

      

GameControl.java

package game.client;
public class GameControl {
    public boolean isEmpty(int id){
        // does stuff...
        return true;
    }   
}

      

So GWT does indeed compile the code, and I see that there is an object GameControl_0

that is being built and set to $wnd.game

, but there is no method isEmpty()

.

My expected end result is to have window.game

both an instance GameControl

with all public methods GameControl

.

How can i do this?

Edit As an answer @jusio

, using JSNI to expose properties window

clearly works, but it was too verbose. I am trying to solve gwt-exporter. Now I have

GameEntryPoint.java

package game.client;

import org.timepedia.exporter.client.ExporterUtil;
import com.google.gwt.core.client.EntryPoint;

public class GameEntryPoint implements EntryPoint {

    @Override
    public void onModuleLoad() {
        ExporterUtil.exportAll();
    }

}

      

RoadServer.java

package game.client;

import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.ExportPackage;
import org.timepedia.exporter.client.Exportable;


@ExportPackage("game")
@Export("RoadServer")
public class RoadServer implements Exportable {
    int _index;
    int _id;
    public RoadServer(int index,int id){
        this._id=id;
        this._index=index;
    }
}

      

but still none of the codes are exported (in particular not RoadServer

).

+3


source to share


2 answers


You have only displayed a copy GameControl

. If you want to expose other methods, you will have to expose them as well. For example:

 public native void expose()/*-{
        var control = this.@game.client.GameEntryPoint::_gameControl;   
        var gameInstance = {
            gameControl: control,
            isEmpty:function(param){
              control.@game.client.GameEntryPoint::isEmpty(*)(param);   
            }  

        }


        $wnd.game = gameInstance;
    }-*/;

      



There is also a structure called gwt-exporter , this can make your job easier

+6


source


This can help.



http://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en

+1


source







All Articles