How to make your GWT application plug-able? - gwt

I am writing (with my team) an GWT application, which parses and represent some domain specific language - for example, plays media presentation with text, video and UI controls. So the application has a set of components: ones - for holding model, ones - for control routines (controllers), and of course we have classes for View.
Now we have a problem - make it all plug-able, in the sense of:
should be one core plugin, which make all common stuff. This coer block must be an JavaScript file (one for every permutation)
should be ability to extend core classes, write custom ones - and compile it to separate JS file (one for every permutation)
Every plugin must registers (export it's classes etc) itself to the core platform, so it could be used.
Problems:
How to compile the custom stuff
separately ?
How to load plugins ?
For the second one problem i've found http://code.google.com/p/gwt-exporter/, that exports GWT classes to outer world, so they could be invoked from pure JS.
Also I have an idea to create new module for new plugin, so it will be compiled to separate file (first problem).
Have you an experience of building such architecture, have you some best practices in this case ?

I have experimented with this same question since GWT 1.5 and every time I come up with a more elegant solution they change the linker and break it. The only way that I have come up with that would work independent of linker design is to do exactly what you are talking about and create a new module for ever plug-in. Then use GWT exporter to create an abstract class that plugins must extend that would have an abstract method that would take the root element of the plugin passed to it by the core and populate it. The issue with this method is all plug-in modules must be added to the DOM on the initial load of the page because since 2.0 the iFrame linker relies on a page load event so dynamically added modules wont fully load. So because of this you will want to have the exported population method wrapped in runAsync so that you aren't downloading modules till you use them.
Edit:
Here is a rough example of what I am talking about. Please be aware that I haven't done any GWT in a couple years and there may be a better way of doing this by now.
public final class PluginManager
{
public static final PluginManager INSTANCE = new PluginManager();
private PluginManager()
{
}
private static native void loadPlugin( AbstractPlugin plugin )
/*-{
if (!$wnd.Plugins) {
$wnd.Plugins = {};
}
var name = plugin.#com.example.PluginManager.AbstractPlugin::getName()();
$wnd.Plugins[name] = $entry(plugin.#com.example.PluginManager.AbstractPlugin::load(Ljava/lang/String;));
}-*/;
private static native void unloadPlugin( AbstractPlugin plugin )
/*-{
if ($wnd.Plugins) {
var name = plugin.#com.example.PluginManager.AbstractPlugin::getName()();
delete $wnd.Plugins[name];
}
}-*/;
private static native JsArrayString getPlugins()
/*-{
if ($wnd.Plugins) {
return Object.keys($wnd.Plugins);
}
return undefined;
}-*/;
public static abstract class AbstractPlugin implements EntryPoint
{
#Override
public final void onModuleLoad()
{
PluginManager.INSTANCE.loadPlugin( this );
}
protected final void unload()
{
PluginManager.INSTANCE.unloadPlugin( this );
}
protected abstract String getName();
protected abstract void load( String rootPanelId );
}
}

Related

Ability to access Dart class from native code

I am in the process of migrating from Android/JNI to Flutter/Dart/FFI to build native apps. Currently in Android I have a Java class (ScanningHelper.java) which is modified during the program lifecycle. During native initialization, I send a jobject type using which in my C/C++ code, I am able to call different member functions or even access variables directly (depending on access).
Is something similar possible with Dart FFI? The closest I have come to is possibly creating a struct in native and then using multiple functions to set/get data from native. I though have a feeling that the amount of code to be written will be high including memory clean up on Dart every time a string/const char* is returned.
Appreciate any direction to accomplish something like below without needing to duplicate implementation across both languages.
// ScanningHelper.java
public class ScanningHelper {
private String _mediaStorageDirectory;
public void setMediaStorageDirectory(String path) { _mediaStorageDirectory = path; }
public String getAppMediaStorageDirectory() { return _mediaStorageDirectory; }
public double latitude;
public double longitude;
...
byte[] image1Bytes;
}
I have some standard methods defined like getStringFromObjectMethod which allows reuse across different classes and return types;
// JNIMethods.cpp
std::string mediaDirectory = JNIMethods::getStringFromObjectMethod(jniEnv,_scanningHelper,"getAppMediaStorageDirectory"));

Dagger and Object Graph Scoping with Flow/Mortar

So I've been spending a lot of time with Mortar and Flow this weekend, and I think I've finally worked most of it out. I've found that it's a little bit more complex than I originally thought, mostly because I haven't quite gotten my mind around Dagger's ObjectGraph Scoping, which Mortar relies on heavily. I've read as much as I could find on this on Dagger's site, but I find information on this subject lacking when it relates specifically to Dagger.
So I have a few questions:
1. I see examples of them scoping #Singleton's:
#Layout(R.layout.screen_friend)
public class FriendScreen implements Blueprint {
#Override public String getMortarScopeName() {
return getClass().getName();
}
#Override public Object getDaggerModule() {
return new Module();
}
#dagger.Module(
injects = FriendView.class
)
static class Module {
}
#Singleton
public static class Presenter extends ViewPresenter<TestView> {
#Inject
public Presenter() {
Log.d("FriendScreen", "Friend Presenter Created");
}
#Override protected void onLoad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
}
}
Is Presenter in this case scoped to this Module specifically because it's an inner class?
2. How can I make sure that an instance of it is only created in this Object Graph but not the global application object graph?
2. What if the Presenter was too big, and I wanted to move it to it's own separate class? How would I scope it just to this module?
3. I noticed that some of the Module classes in their examples are static, and others aren't. Does this have any effect on scoping?
4. Where can I read more to better understand Dagger's Object Graph. I need to get a better understanding of includes, injects, addsTo and how those are used in ObjectGraph creation
etc:
#dagger.Module( //
includes = ActionBarModule.class,
injects = MainView.class,
addsTo = ApplicationModule.class, //
library = true //
)
I don't believe presenters are scoped to a module since on a rotation they are preserved. the #Singleton annotation also leads me to believe that a presenter is on a global graph and just binds to a view when an activity recreates itself.
Here's a good scoping source.
Effective Java has a fantastic explanation about static inner vs non static inner vs anonymous classes if you want to learn more about those.

create and return gwt widget from java code

I am new to GWT and I need to reslove this problem. I need to create a widget with gwt-links. To make it happened, I need to fetch some data from an ontology, and then based on the result, create the graph. The problem is that, when I mix java code with gwt code it doesn't want to compile.
The question is, how do I create a widged explained above that will be placed in a http page?
The code looks like this now :
public class Example1 {
#Override
public void draw() {
// Create the elements
String ontology = Ontology.get(1);
Widget labelHello = new BoxLabel(ontology);
controller.addWidget(labelHello,25,115);
// Add DnD logic
PickupDragController dragController = new PickupDragController(controller.getView(), true);
dragController.makeDraggable(labelHello);
}
public Widget asWidget() {
return controller.getView();
}
}
the Ontology.get() doesn't want to compile.
GWT can't compile just any java code.
These are the packages that are emulated by GWT: pls read
The code that can't be translated to javascript, (the stuff that is not emulated) you must handle on the server side.
GWT projects uses three packages (by default)
com.myapp.client
com.myapp.shared
com.myapp.server
By default everything within the shared and client package will be compiled to JavaScript.
Every Class, which is imported into a Class, which is inside the shared and client package must be:
emulated by GWT
Compilable to GWT (and inside the client or shared package)
Compilable to GWT (and the package must be whitelists in *.gwt.xml)
Uf you code ISolver can be compiled to JavaScript you will have to create a module.gwt.xml and inheritt your project from this module. This may enable the GWT-compiler to compile ISolver (and its implementation) to JavaScript.
If your code can't be compiled to GWT you will have to write a remote-service to make the calculation.
I don't really want to compile the code to javascript. All i want to do is to create a GWT widget ( which is a graph).
To create the widget, I need to do some calculations and repository fetch. I dont want to translate it( repository fetching), I just need to use it in order to create the model of the graph.
Basically, this is something I want to do:
String l = Repository.getLabel(); // Some advanced calculations that use many JavaSE classes;
GWTWidget widget = new GWTWidget(l); // widget, that will be displayed on a page.
but when I put something in method onModuleLoad it doesn't compile.
This is probably a simple question, but I'm not really related with GWT and I'm forced to remake someone's work.
public void onModuleLoad() {
System.out.println("tes");
VerticalPanel mainPanel = new VerticalPanel();
RootPanel.get().add(mainPanel);
//
ISolver solver = null;
System.out.println("TEST2");
}
ERROR: Line 53: No source code is available for type pr.ISolver; did you forget to inherit a required module?
ERROR: Unable to find type 'client.Link'
ERROR: Hint: Previous compiler errors may have made this type unavailable
and other lines sthat start with " No source code..".
ISolver is an interface, but I dont want to translate it. I want to use it for calculations.

Add new Constructor to an existing Java Class via AspectJ

Trying to clean up some nasty code, for which we dont have the source code. Imagine something like this:
public class Driver{
private String paramA;
private String paramB;
new Driver(HugeAndOverbloatedObject object)
{
paramA = object.getSubObject4711().getParamX();
paramB = object.getSubObject4712().getParamY();
}
}
This third library has this all over the place: tight coupling via constructors, eventhough the classes are hardly related. The rude combination of private members and forced constructor inheritance make the extension of the code virtually impossible without creating "sloppy" constructor parameter objects.
So I am trying to manipulate the classes via AspectJ and compile time weaving, so I can slim down on the constructors, to something like this:
Driver driver = new Driver("paramA", "paramB");
I think this should be possible, and I have made some progress. If I have something like this:
public aspect NewConstructor {
Driver.new(String parameterA, String parameterB){
//New Constructor Code
}
}
and run this through the weaver I actually find a new constructor in the driver, but not quite as I expected.
Issue: Unexpected third Parameter in the woven class
I was hoping I can invoke it with two parameters:
new Driver("paramA", "paramB")
Instead I need to invoke it with three parameters:
new Driver("paramA", "paramB", new NewConstructor())
Why do I need to instantiate a new instance of the aspect and pass it as an argument? Can this be prevented?
Something odd is going on here. You should not need to add the aspect as a third argument to the constructor. In fact, when I try this myself using the following class and aspect, I do not get any compile errors:
Java class:
package pack;
public class Driver {
public static void main(String[] args) {
new Driver("paramA", "paramB");
}
}
Aspect:
package pack;
public aspect NewConstructor {
public pack.Driver.new(String parameterA, String parameterB){
}
}
Are your Java class and aspect in different projects? Are you using an aspect path and/or in path? Are you using load time weaving?
If after doing a full build of your project you still see the probem, it's worth raising a bug for AspectJ.

Calling GWT Java function from JavaScript

I am a newcomer to GWT and JavaScript.
There are similar question of this type I have tried to follow, but I keep failing.
I have a GWT app, I need to call a Java function from Javascript( on the onclick of a href tag, in particular.) Following is what I have done.
public class JSNITest {
public static void handleAnchorClick(int a , int b) {
Window.alert("Current row and Column is " + a + " " + b);
}
public static native void exportMyFunction()/*-{
$wnd.handleAnchorClick = function(param1,param2){
#company.package.class.JSNITest::handleAnchorClick(*)(param1,param2);
}-*/;
}
And in the HTML,
link
(a1 , a2) are two integer variables in my code.
I have also called EnclosingClass.exportMyFunction() in the entry point function.
I keep running into various kinds of exceptions(No Such class exception). Can someone please correct me?
Regards
Let me explain a bit more about exporting GWT stuff to the JS world. You have several options to do that, but I will focus on three methods.
[EDITED]
0- JsInterop: GWT maintainers are working in a new feature to easily export java methods to javascript, and wrap javascript objects. The feature was very experimental in 2.7.0 lacking some features, but in 2.8.0 will be almost functional. Please take a look to the Design Document, and other discussions in the mailing list.
[END]
1- JSNI: The first one is to write your own jsni, in this case you have to be aware about the possible mistakes you could make. Basically these mistakes are because you have to know how to deal with types. In your case if you want to get a javascript array (like you are asking in your comment below), the solution could be:
public static native void exportMyFunction()/*-{
$wnd.handleAnchorClick = #company.package.class.JSNITest::handleAnchorClick(*);
}-*/;
public static void handleAnchorClick(JsArrayMixed args) {
Window.alert("Current row and Column is " +
args.getNumber(0) + " " + args.getNumber(1));
}
public void onModuleLoad() {
exportMyFunction();
}
//javascript code
window.handleAnchorClick([1,2])
Note that JSNI only allows you to pass primitive types (except long) and JavaScriptObject objects. So when passing a javascript array, you have to receive it with a JavaScriptObject like in the example. In this case, since javascript only uses a type for numbers, args.getNumber will return always a double, and you have to convert in java.
2- gwt-exporter For exporting large projects, or when you need to handle complex objects and classes I'd rather use gwt-exporter
static class MyClass implements Exportable {
#Export("$wnd.handleAnchorClick")
public static void handleAnchorClick(double[] args) {
Window.alert("Current row and Column is " +args[0] + " " + args[1]);
}
}
public void onModuleLoad() {
GWT.create(MyClass.class);
}
//javascript code
window.handleAnchorClick([1,2])
gwt-exporter will deal with any kind of primitive types (even with long) myfunc(long[] args), with var-args myfunc(long...args), it supports method overload, and much more.
3- gwtquery Finally if you prefer gwtquery, you can use a technique to add function properties to any js object like window
// The GQuery Properties object is able to wrap a java Function object
// into an js property.
Properties wnd = window.cast();
wnd.setFunction("handleAnchorClick", new Function() {
public void f() {
// Get the js arguments[] array
JsArrayMixed args = arguments(0);
// Get the first element of the arguments[] array
JsArrayMixed ary = args.getObject(0);
Window.alert("Current row and Column is " +
ary.getNumber(0) + " " + ary.getNumber(1));
}
});
//javascript code
window.handleAnchorClick([1,2])
With gquery you can use the gwt JsArrayMixed class which always returns a number as a double, or you can use a JsCache which allows to convert numbers to any other numeric type in java ((JsCache)ary.get(1, Integer.class)
As a summary, I would rather use gwt-exporter as the first option because it is specialized in handling this problematic. As a second option, I would use gquery which is a serious complement to gwt. Finally, I would avoid to use hand-written jsni when possible, Javascript is normally a source of issues and mistakes (think that the main goal of gwt is not to deal with js).
You should consider GWT exporter. You might even consider waiting because GWT 2.8 should come out pretty soon. It was supposed to come out some time around The begging of 2015. 2015 has already begun and they're demonstrating at GWT.create right now so it should come out any day now. If you can't wait then you can either use experimental is interop, JSNI like the top answer says to or GWT exporter. JSNI is more complicated and involves a lot of boiler plate code so if you have to do a lot of js interop I recommend GWT-exporter.