views:

190

answers:

1

Hi, I have the following requirement: Based on some user input, I need to generate a HTML form that the user can embed on a separate Web application. I thought on doing this with GWT since I'm familiar with it.

I'm clear on the input parsing and widget generation part. What I don't know how to do is how to export the root widget's (most probably a Panel) compiled code, so the user can take the code and include it in some other page.

Something like:

String rootPanelCode = rootPanel.exportCode();
Dialog codeDialog = new DialogBox();
codeDialog.setText(rootPanelCode);

Then the user copies the displayed code in some HTML file:

<script type="text/javascript" language="javascript">
    //copied code goes here
</script>

Requiring a particular <div id="required_id" /> in the HTML file is not a problem. Or maybe javascript code is not enough, and the user is required to download a zip file with js and html files, copy those to a directory and reference them in the page. This again is not a problem.

Is my use case possible with GWT?

Thanks in advance.

+1  A: 

I'd say... no :) Mainly because when a GWT application is started it first runs the bootstrap file that in turn chooses the particular permutation for the current browser. So the code that you would get might include some stuff that wouldn't work in all browsers. This might be side stepped by providing some sort of "lightweight" boostrap file/method to download but I doubt that would work.

Besides, the JS code you get is heavily optimized (and with GWT 2.0 the JS file contains JS, CSS and even images), for example, when possible strings are put into variables for performance reasons - but these variables are usually grouped together and put in one place in the compiled JS file, so even if you could somehow get to the code that creates your form, it could contain references to some undefined variables. In other words, the compiled code is meant to be used as a whole.

A more "elegant" solution (and more importantly, feasible with GWT ;)) is to export the form to some sort of abstract form/language, maybe JSON, so that you could parse/recreate it easily in the other web app:

{
    "form1": [
        { "label1": "value1" },
        { "label2": "value2" } 
    ]
}  

Hmm, I just thought of a possible hack.. With the right use of code splitting it might be possible to separate the code responsible for the form creation - but that would make it maybe easier to "export", it's not a complete solution (and I wouldn't recommend it.. just an interesting/possible hack).

Igor Klimer
Thanks for the insight. I wouldn't want to get into convoluted hacks to get it to work, but the JSON idea looks promising. I just wanted to be sure if there was anything in GWT that would allow me to do it directly.
Cesar