<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rich Internet Applications (RIA) &#187; GWT</title>
	<atom:link href="http://www.canoo.com/blog/category/gwt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.canoo.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 18 Jan 2012 14:30:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>HTML5 for iOS</title>
		<link>http://www.canoo.com/blog/2011/11/01/html5-for-ios/</link>
		<comments>http://www.canoo.com/blog/2011/11/01/html5-for-ios/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 10:36:30 +0000</pubDate>
		<dc:creator>Erik Jan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iPad]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2330</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/11/01/html5-for-ios/";</script>My grandfather used to say: &#8220;Makkelijker gezegd dan gedaan&#8221; (easier said then done). So when I talked about how HTML5 could be the new platform in-depended development paradigm, in this previous post, I better come with some real world examples instead of only saying it. So that is exactly what I&#8217;ve been doing. My wife [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/11/01/html5-for-ios/";</script><p>My grandfather used to say: &#8220;<em>Makkelijker gezegd dan gedaan</em>&#8221; (easier said then done). So when I talked about how HTML5 could be the new platform in-depended development paradigm, in this <a href="http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/">previous post</a>, I better come with some real world examples instead of only saying it.</p>
<p>So that is exactly what I&#8217;ve been doing. My wife is a bit of an apple fan woman. And she has a book that she would like to publish. She couldn&#8217;t find a publisher, so the next best thing would be to put her book on an iPad, but you still need an publisher to put something on the iBook store and publishers are still stuck in the dark ages. So we&#8217;ve decided to make an application out of her book. Now I&#8217;ve written some objective-c code before and I must say it wasn&#8217;t the best experience I&#8217;ve had. Xcode at that time was awful, it was like writing software 10 years ago. So I started to look for alternatives. It could be a simple html page, but how to create a native iPad application out of that?</p>
<p>I&#8217;ve found something that I&#8217;m really exited about. <a href="http://playn.googlecode.com">Playn</a> is a cross-platform game abstraction library for writing games that compile to multiple platforms one of these is html using gwt. Now if I use this in combination with <a href="http://phonegap.com/">phonegap</a> then I can create a iPad app that can also run on android based pads. Not only that I could make it interactive add a game to the book and best of all do it in Java.</p>
<p>If you think about it, for companies this makes a lot of sense. Unless your companies key platform is iOS, having developers in-house that have objective-c knowledge is expensive. Also hiring external company to build an iPhone app is expensive and they have to work together with you to integrate your existing architecture. So having something like this where one can use existing knowhow to create a android and iPhone solution that works on both platforms is a huge cost saver.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/11/01/html5-for-ios/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/11/01/html5-for-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GWT and HTML5 Canvas, the future of the web?</title>
		<link>http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/</link>
		<comments>http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 11:35:52 +0000</pubDate>
		<dc:creator>Erik Jan</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java RIA]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2217</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/";</script>The future of computing lies in mobile computing, more and more devices come on the market, tablets and phones that will connect through the Internet as technology is getting cheaper and wifi and 3g more common. That means our software will also need to run on these handheld devices, because our clients will want their [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/";</script><p>The future of computing lies in mobile computing, more and more devices come on the market, tablets and phones that will connect through the Internet as technology is getting cheaper and wifi and 3g more common. That means our software will also need to run on these handheld devices, because our clients will want their services on all platforms. This is what Java set out to do 15 years ago, so that we have the ability to write software once and run it everywhere. The problem is these new platforms, that I believe will make up a large part of the future have, until now, no Java. It would be great if we can find something that will give us the ability to write our software only once.</p>
<p>I believe that the technology that is going to deliver us this promise is called HTML5. More and more applications will run inside you browser. I&#8217;m typing this article in google docs, which is a perfect example of how applications will evolve. No need to install anything or maintain up to date versions, even an operating system could be very minimalistic (<a href="http://chrome.blogspot.com/search/label/chrome%20os">google chrome</a>). So Google is obviously sharing this vision, but also Microsoft is building it&#8217;s next version of office on something that is based on their web-browser.</p>
<p>Sadly the development language of HTML5 Javascript and not Java. Java has been around a long time and there are a lot of libraries that people have made. Would be cool if we can still use these, the answer is <a href="http://code.google.com/webtoolkit/">Googles Web Toolkit</a>. GWT provides a way to code in Java and then translate that code into Javascript. This way we can still use all that great stuff that others made.</p>
<p>A good example of this future is <a href="http://chrome.angrybirds.com/">Angry Birds</a>, you have probably heard of this game. To create this game they have taken the java implementation of <a href="http://box2d.org/">box2d</a> called <a href="http://www.jbox2d.org/">jbox2d</a> and created a javascript version of that with the help of GWT. Once that is done you can use <a href="http://en.wikipedia.org/wiki/Canvas_element">HTML5 Canvas</a> to draw the Birds and calculate their positions if they are falling. Now because they have build it this way with HTML5 this game could also work on an iPhone, but even better because android is based on java you could create from this source a android version with only one line of code.</p>
<p>Have a look at some of the great things people are building with the HTML5, be sure that your browser supports it.</p>
<ol>
<li>Drawing program <a href="http://muro.deviantart.com/">deviantART</a></li>
<li>Old style game with new technology <a href="http://www.kevs3d.co.uk/dev/arena5/">area5</a></li>
<li><a href="http://www.youtube.com/watch?v=XhMN0wlITLk">Quake GWT HTML5 port</a></li>
</ol>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/08/25/gwt-and-html5-canvas-the-future-of-the-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code generation in GWT with Deferred Binding (CDI-like events)</title>
		<link>http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/</link>
		<comments>http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 11:22:30 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[code generation]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[event bus]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2176</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/";</script>code { font-size: 12px; overflow: auto; } If you read my series on GWT dependency injection (parts: first, second and third), maybe you remember that, in part 3, I mentioned how convenient would be to reduce the boilerplate required to define events in GWT. I also mentioned how elegant I find the event definition in [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>If you read my series on GWT dependency injection (parts: <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin'>first</a>, <a href='http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii'>second</a> and <a href='http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii'>third</a>), maybe you remember that, in part 3, I mentioned how convenient would be to reduce the boilerplate required to define events in GWT. I also mentioned how elegant I find the event definition in CDI. Wouldn&#8217;t it be nice to have a lightweight event model like that in GWT?</p>
<p>Let&#8217;s see first how an event is defined and handled in CDI:<br />
<pre><code>
public class CDISample {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;private Event&lt;String&gt; event;

&nbsp;&nbsp;&nbsp;&nbsp;public void handler(@Observes String payload) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(payload);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.fire(&quot;Hello event world!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
As you can grasp from the code, to define a new type of event in CDI it is enough to define an injected field of the parametrized class Event and the CDI container will do all the wiring for us. The parametrized type of the event defines its payload type by mean of the type parameter. To listen to the event, we just need to define a method that receives a parameter of the event payload type  and annotate it with &#8220;@Observes&#8221;.</p>
<p>While exactly this is maybe not doable in GWT (at least not yet), what about something like this?:<br />
<pre><code>
public class GWTSample {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;private SampleEvent event;

&nbsp;&nbsp;&nbsp;&nbsp;public void execute() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.dispatchTo(new Handler&lt;String&gt;() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(String payload) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Window.alert(payload);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;event.fire(&quot;Hello event world!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public interface SampleEvent extends Event&lt;String&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
While I can agree with you that it is not as elegant as the events approach in CDI, it is (IMO) a big improvement compared with, for instance, the ping / pong event definition in the demo application of my GIN series (source code <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part0.zip'>here</a>):<br />
<pre><code>
public class PingEvent extends GwtEvent&lt;Handler&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;public static Type&lt;Handler&gt; TYPE = new Type&lt;Handler&gt;();

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;public Type&lt;Handler&gt; getAssociatedType() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return TYPE;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void dispatch(Handler handler) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler.onEvent(this);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public interface Handler extends EventHandler {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void onEvent(PingEvent event);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
With the new approach, the resulting &#8220;PingEvent&#8221; would look like this:<br />
<pre><code>
public interface PingEvent extends Event&lt;Void&gt; {}
</code></pre><br />
If you are wondering why not to avoid defining the &#8220;PingEvent&#8221; class itself, two are the reasons:</p>
<ul>
<li>First, the events have the same payload type (Void in this case), and therefore we need explicit types to distinguish the ping event from the pong event. This also happens in CDI where we would define explicit payload types instead.</li>
<li>Second, in this implementation we will be using GWT&#8217;s deferred binding and, during the code generation phase, we need to know the parametrized type. Because of Java generics limitations, for this last it is necessary to define a new parametrized type which captures the payload type in its type parameter (wrapper).</li>
</ul>
<p>Did I convince you? If I did, let&#8217;s go on and see how to use deferred binding and dependency injection to achieve it.</p>
<h3>Letting GWT generate the boilerplate</h3>
<p>Deferred binding in GWT is a really powerful feature implemented in the GWT compiler to substitute class implementations depending in environment properties like the user agent. This way, while the developer has the illusion of using the same classes independently of the browser, the deferred binding mechanism allows to select the adequate implementation for each browser.</p>
<p>Far from being static, deferred binding allows to generate code on the fly by mean of a generator class. This happens when the developer invokes the &#8220;GWT.create()&#8221; method on an interface. For the operation to succeed, a generator for the interface must exist. This will be invoked during GWT compilation time and its result (one or more Java classes) will be compiled and linked into the resulting JS application.</p>
<p>To configure which generator should be used for which interface, it is necessary to add a new &#8220;generate-with&#8221; entry in the GWT module descriptor. In the case of the demo application, the following code has been added to the &#8220;PingPong.gwt.xml&#8221; file:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;&lt;generate-with class=&quot;com.canoo.gwt.events.server.eventsfwk.EventGenerator&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;when-type-assignable class=&quot;com.canoo.gwt.events.client.eventsfwk.Event&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/generate-with&gt;
...
</code></pre></p>
<p>The contents of the &#8220;Event&#8221; and &#8220;EventGenerator&#8221; classes are the following:<br />
<pre><code>
public interface Event&lt;T&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;public void fire(T payload);

&nbsp;&nbsp;&nbsp;&nbsp;public void dispatchTo(Handler&lt;T&gt; callback);

&nbsp;&nbsp;&nbsp;&nbsp;public interface Handler&lt;T&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(T payload);
&nbsp;&nbsp;&nbsp;&nbsp;}
}

public class EventGenerator extends Generator {
&nbsp;&nbsp;&nbsp;&nbsp;private static final String EVENT_IMPL = &quot;EventImpl&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;private static final CodeTemplate GWT_SIMPLE_EVENT_TEMPLATE = new GwtSimpleEventTemplate();
&nbsp;&nbsp;&nbsp;&nbsp;private static final CodeTemplate EVENT_IMPL_TEMPLATE = new EventImplTemplate();

&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String typeName) throws UnableToCompleteException {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TypeOracle typeOracle = generatorContext.getTypeOracle();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType parentEventType = typeOracle.findType(Event.class.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType eventType = typeOracle.findType(typeName);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType payloadType = findPayloadType(eventType, parentEventType);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String gwtSimpleEventImplName = generateGwtSimpleEvent(treeLogger, generatorContext, eventType, payloadType);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String eventImplTypeName = generateEventImpl(treeLogger, generatorContext, eventType, payloadType, gwtSimpleEventImplName);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return qualifyInGwtEvents(eventImplTypeName);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private static JClassType findPayloadType(JClassType eventType, JClassType parentEventType) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JClassType interfaces[] = eventType.getImplementedInterfaces();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (JClassType anInterface : interfaces) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((anInterface instanceof JParameterizedType) &amp;&amp; (anInterface.getErasedType() == parentEventType.getErasedType())) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JParameterizedType parametrizedType = (JParameterizedType) anInterface;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return parametrizedType.getTypeArgs()[0];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new IllegalStateException(&quot;Payload type not found for: &#039;&quot; + eventType + &quot;&#039;.&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private String generateGwtSimpleEvent(TreeLogger treeLogger, GeneratorContext generatorContext, JClassType eventType, JClassType payloadType) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String gwtSimpleEventImplName = getGwtSimpleEventImplName(eventType);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter gwtSimpleEventPrintWriter = generatorContext.tryCreate(treeLogger, getEventsPackageName(), gwtSimpleEventImplName);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GWT_SIMPLE_EVENT_TEMPLATE.generate(gwtSimpleEventPrintWriter, getEventsPackageName(), gwtSimpleEventImplName, payloadType.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generatorContext.commit(treeLogger, gwtSimpleEventPrintWriter);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return gwtSimpleEventImplName;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private String generateEventImpl(TreeLogger treeLogger, GeneratorContext generatorContext, JClassType eventType, JClassType payloadType, String gwtSimpleEventImplName) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String eventImplTypeName = getEventImplTypeName(eventType);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String eventTypeName = getEventTypeName(eventType);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter eventPrintWriter = generatorContext.tryCreate(treeLogger, getEventsPackageName(), eventImplTypeName);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EVENT_IMPL_TEMPLATE.generate(eventPrintWriter, getEventsPackageName(), eventType.getQualifiedSourceName(), eventImplTypeName, eventTypeName, gwtSimpleEventImplName, payloadType.getName());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generatorContext.commit(treeLogger, eventPrintWriter);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return eventImplTypeName;
&nbsp;&nbsp;&nbsp;&nbsp;}

... (some methods omitted)
</code></pre></p>
<p>While the &#8220;Event&#8221; class contains the interface definition for our events and lives in the &#8220;client&#8221; package of our GWT application, the &#8220;EventGenerator&#8221; is a GWT specific class that resides in the &#8220;server&#8221; package of our GWT application. In order to compile the code, we will need to add a new dependency to our project: &#8220;gwt-dev-&lt;version&gt;.jar&#8221;.</p>
<p>This generator class, by mean of some templates included in the source code, generates the boilerplate classes and saves us the tedious work of writing these classes. If you want to see this in detail, please refer to the &#8220;eventsfwk&#8221; packages in the client and server part of the application (source code <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part1.zip'>here</a>).</p>
<h3>Using GIN to bring everything together</h3>
<p>Following what I mentioned until now, it seems that if we define an interface extending the &#8220;Event&#8221; interface (ex: &#8220;SampleEvent&#8221;) and we use the generator to generate and wire the code by mean of &#8220;GWT.create(SampleEvent.class)&#8221;, we should be able to use the result as shown in the &#8220;GWTSample&#8221; execute method. Not really.</p>
<p>One piece that is missing in the puzzle is which event bus should the underlying GWT events use. We could create an own one without loosing functionality but a better thing that we can do is to provide an injection point and provide a mechanism to make our events participate in the application&#8217;s dependency injection context and use the application&#8217;s event bus instead of an own instance.</p>
<p>To achieve this, we have created a GIN module in the &#8220;eventsfwk&#8221; package:<br />
<pre><code>
public class EventsModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestStaticInjection(GwtSimpleEvent.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>This module should be installed in the application&#8217;s dependency context and it will inject an static field in the &#8220;GwtSimpleEvent&#8221; class which is the event bus used for our improved events. To install it in our application, the injection module has been modified like this (note the line &#8220;install(EventsModule.class);&#8221;):<br />
<pre><code>
@GinModules(Module.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static class Module extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new EventsModule());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new GinFactoryModuleBuilder().build(Factory.class));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;PingText&quot;)).to(&quot;Ping&quot;);

... (code omitted)
}
</code></pre></p>
<p>This means that, when the dependency injection module is initialized at the beginning of the application execution, the previously configured event bus will be statically injected in the &#8220;GwtSimpleEvent&#8221; class used by all the generated event classes.</p>
<p>I can not read minds, but maybe you are also wondering why the event fields annotated with &#8220;@Inject&#8221; get generated and injected. All in all, we don&#8217;t call anywhere &#8220;GWT.create()&#8221; to use the generator class.</p>
<p>Well, we are not doing it but GIN does. Whenever GIN finds a dependency (something annotated with &#8220;@Inject&#8221;) and that dependency has not explicitly been configured (bound in the GIN modules), GIN invokes &#8220;GWT.create()&#8221; as fallback strategy. This simple mechanism allows us to generate and inject our event in only one step.</p>
<p>Wow! This post has been &#8220;hardcore&#8221; GWT, don&#8217;t you think? Maybe even too &#8220;hardcore&#8221;. Please, just let me know if you like it and what do you think about the approach.</p>
<p>See you in a future post!</p>
<p>The source code for the initial version of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part0.zip'>here</a>. The final version <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part1.zip'>here</a>. To see any of the applications working, unzip the corresponding file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/07/04/code-generation-in-gwt-with-deferred-binding-cdi-like-events/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>GWT Dependency Injection recipes using GIN (III)</title>
		<link>http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/</link>
		<comments>http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 10:25:32 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2161</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/";</script>code { font-size: 12px; overflow: auto; } This is the third part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the first and second parts, you maybe should do it before reading this third and last article. In this article, we will introduce two GIN [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>This is the third part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/'>first</a> and <a href='http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/'>second</a> parts, you maybe should do it before reading this third and last article.</p>
<p>In this article, we will introduce two GIN features: &#8220;constants binding&#8221; and &#8220;static injection&#8221;.<br />
To do this with an example, let&#8217;s refactor the &#8220;serve button&#8221; in the &#8220;Simulator&#8221; main class of the second part into an own class:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;injector.getRacket().serve();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
...
</code></pre></p>
<p>Here the new extracted &#8220;ServeView&#8221; class:<br />
<pre><code>
public class ServeView extends Button {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public ServeView(final Racket racket, @Named(&quot;ServeText&quot;) String serveText) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(serveText);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.serve();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>As you can see, the constructor of this new class is annotated with the &#8220;@Inject&#8221; annotation and receives two parameters. One of them is the racket and the other is the button&#8217;s label. In the case of the racket, it is already defined as an injectable dependency in the injector interface and in the case of the button&#8217;s label, it should be a configurable constant.<br />
GIN, by mean of &#8220;constants binding&#8221;, performs the binding of injectable dependencies to constants.<br />
Because the type of the &#8220;serveText&#8221; is String, we need to be more specific here and therefore we have annotated this dependency with the &#8220;@Named&#8221; annotation. To configure the constant value to bind to, we need to add the following line into our &#8220;InjectorModule&#8221; class:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;ServeText&quot;)).to(&quot;Serve!&quot;);
...
</code></pre></p>
<p>To let the other constants in the application be configured in the same way and to introduce the last GIN feature (&#8220;static injection&#8221;), let&#8217;s now modify the &#8220;Simulator&#8221; main class as follows:<br />
<pre><code>
public class Simulator implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;static ServeView SERVE_VIEW;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;static AssistedInjectionFactory FACTORY;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;@Named(&quot;PingText&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;static String PING_TEXT;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;@Named(&quot;PongText&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;static String PONG_TEXT;

&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initInjection();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(SERVE_VIEW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(FACTORY.createPingPongView(PING_TEXT, PingEvent.class));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(FACTORY.createPingPongView(PONG_TEXT, PongEvent.class));
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private void initInjection() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GWT.create(Injector.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>And our &#8220;InjectorModule&#8221; like this:<br />
<pre><code>
public class InjectorModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;PingText&quot;)).to(&quot;Ping&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;PongText&quot;)).to(&quot;Pong&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bindConstant().annotatedWith(named(&quot;ServeText&quot;)).to(&quot;Serve!&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;requestStaticInjection(Simulator.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre></p>
<p>The &#8220;static injection&#8221; allows us to inject the static fields of a class. This happens by mean of the method &#8220;requestStaticInjection&#8221; of the GIN module class and, in our application, we use it to inject the static dependencies of the &#8220;Simulator&#8221; class.</p>
<p>This way, even when the &#8220;Simulator&#8221; class does not participate in the dependency injection context, its static members will be injected making the explicit references to the injector not required.</p>
<p>This has the side-effect of making the &#8220;Injector&#8221; getters no more necessary leaving our injector interface &#8220;empty&#8221;:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
}
</code></pre></p>
<p>Please note that, in order to get the dependency injection context configured and initialized, it is required to instantiate the &#8220;Injector&#8221; interface by mean of the &#8220;GWT.create()&#8221; method as part of the application initialization. This is done in our case within the &#8220;initInjection&#8221; method in the &#8220;Simulator&#8221; main class.</p>
<h3>Conclusions</h3>
<p>If you have read the 3 articles of this series, maybe you can remember how our demo application looked like at the beginning. While the functionality and appearance has not changed at all, the intern structure of the application is completely different.</p>
<p>In my opinion, dependency injection allows a much cleaner structure, enables configuring the application in an elegant and easy way and, when used together with an event bus, produces low-coupled high-modular applications.</p>
<p>Of course, &#8220;there is no free lunch&#8221; and using GIN means also that the development team has to learn new concepts and introduce a new framework in the application. Anyway,  dependency injection is an already proven concept and GIN is being intensively used in most of the new GWT projects and GWT frameworks.</p>
<p>While some minimal application structure optimizations could still be applied to the application, the thing that I still find improvable in it is the verbosity of the GWT events definitions. This requires writing some boilerplate classes to ensure the type safety (event handlers), and also the definition of the own event classes is too verbose, being possible to generate them automatically in most of the cases by using either GWT&#8217;s deferred binding or a JDK 6 annotation processor. If you know how the events in CDI are defined, very probably you will agree with me that such an approach would be much more elegant and concise.</p>
<p>A solution for that, even when it requires dependency injection, is more related to other topics and therefore a subject for a different thread. If you got curious, stay tuned for a future post on code generation in GWT! <img src='http://www.canoo.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   </p>
<p>The source code of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part3.zip'>here</a>. To see the application working, unzip the file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<p>As always, I hope that you enjoyed reading this series of articles as much as I did writing it and hope to see you soon in a future post!</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>GWT Dependency Injection recipes using GIN (II)</title>
		<link>http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/</link>
		<comments>http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 10:43:52 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2101</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/";</script>code { font-size: 12px; overflow: auto; } This is the second part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the first part, there we explained how to integrate GIN in an existing GWT sample application. In this second part, we will continue enhancing the [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>This is the second part of a series about Dependency Injection in Google Web Toolkit using GIN. If you have not yet read the <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/'>first part</a>, there we explained how to integrate GIN in an existing GWT sample application. In this second part, we will continue enhancing the sample application while explaining other types of injection supported by GIN.</p>
<h3>Encapsulating the event bus</h3>
<p>In the first part of this series, we configured the GWT event bus used in the original application to be injected in some of the application elements. Using an event bus is a &#8220;best practice&#8221; that helps communicating the different components in the application while keeping a low coupling between them. The point here was to show how to declare and configure a first dependency using the &#8220;injector&#8221; and the &#8220;module&#8221;. Because the class &#8220;SimpleEventBus&#8221; is provided by GWT, it was not possible to annotate the code of the class with &#8220;@Singleton&#8221; to set the &#8220;injection scope&#8221;. Instead, we use the Java DSL provided by GIN.<br />
In order to see how to do the same with annotations, let&#8217;s use now an application class (instead of a framework class) where we can use the annotations.<br />
As you probably noticed, we are using the event bus to emulate the &#8220;ping-pong&#8221; strokes. Instead of such a technical artifact, let&#8217;s create a &#8220;racket&#8221;:<br />
<pre><code>
@Singleton
public class Racket {
&nbsp;&nbsp;&nbsp;&nbsp;private final EventBus fEventBus;

&nbsp;&nbsp;&nbsp;&nbsp;private GwtEvent&lt;?&gt; fLastEvent;

&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public Racket(EventBus eventBus) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus = eventBus;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public void serve() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fLastEvent = null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hit();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public void hit() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus.fireEvent(getNextEvent());
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private GwtEvent&lt;?&gt; getNextEvent() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (fLastEvent == null || fLastEvent instanceof PongEvent) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fLastEvent = new PingEvent();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fLastEvent = new PongEvent();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return fLastEvent;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;public void onStroke(Class&lt;?&gt; pingPongEvent, final Command command) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (pingPongEvent == PingEvent.class) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus.addHandler(PingEvent.TYPE, new PingEventHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(PingEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command.execute();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fEventBus.addHandler(PongEvent.TYPE, new PongEventHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onEvent(PongEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command.execute();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
As you can see at the code, our class is annotated with &#8220;@Singleton&#8221; to indicate the injection scope and &#8220;@Inject&#8221; to get the event bus injected in the constructor. The class is a more adequate wrapper for the event handling and offers three methods: &#8220;serve&#8221;, &#8220;hit()&#8221; and &#8220;onStroke()&#8221;.<br />
To configure the injection please adjust the &#8220;injector&#8221; class like this:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static final Injector INSTANCE = GWT.create(Injector.class);

&nbsp;&nbsp;&nbsp;&nbsp;public Racket getRacket();

&nbsp;&nbsp;&nbsp;&nbsp;public PingView getPingView();

&nbsp;&nbsp;&nbsp;&nbsp;public PongView getPongView();
}
</code></pre><br />
This time, we only need to declare the dependency in the &#8220;Injector&#8221; interface (the method will be invoked from the &#8220;Simulator&#8221; class) but no additional configuration is required and therefore no changes should be made in the &#8220;InjectorModule&#8221; class.<br />
Because now the event bus will not be directly accessed from the &#8220;Simulator&#8221; class, we have removed it from the &#8220;Injector&#8221; interface. But, let&#8217;s see the rest of the code changes:<br />
<pre><code>
public class PingView extends Label {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public PingView(final Racket racket) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.onStroke(PongEvent.class, new Command() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void execute() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(&quot;Pong&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Timer() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void run() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(&quot;&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.hit();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.schedule(1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
The &#8220;PongView&#8221; class is almost identical and will not be shown here.<br />
<pre><code>
public class Simulator implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Injector injector = Injector.INSTANCE;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(injector.getPingView());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(injector.getPongView());

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;injector.getRacket().serve();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(button);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Recapitulating what we have seen so far:</p>
<ul>
<li>GIN allows to define &#8220;injectors&#8221; by mean of interfaces (&#8220;Injector&#8221; interface) that can be configured using one or more &#8220;modules&#8221; each (&#8220;InjectorModule&#8221; class).</li>
<li>If we need to configure an injection aspect we can use annotations in the class code or the Java DSL in the module.</li>
<li>In at least one point of the application we will need to access the &#8220;injector&#8221; to retrieve a top-level injected object (ex: &#8220;Racket&#8221;, &#8220;PingView&#8221;, &#8220;PongView&#8221; ). For these objects to be accessible through the injector, we need to declare them in the &#8220;injector&#8221; interface.</li>
<li>Any other dependency that is declared to be injected (annotated with @Inject) will be resolved by GIN using the algorithm described in the first part of this series and does not need to be declared in the &#8220;injector&#8221; interface (ex: &#8220;EventBus&#8221;).</li>
</ul>
<p>If you are familiar with GUICE, you probably already know other ways of configuring dependencies and injection. Let&#8217;s see in the next point how to avoid the repetition in the views by using &#8220;assisted injection&#8221;.</p>
<h3>Using assisted injection to avoid code repetition</h3>
<p>One of the main applications of dependency injection is to avoid coding factory classes and singletons and also centralizing the application&#8217;s configuration values. This is a very powerful tool to configure application-wide objects like services, which typically are either &#8220;singletons&#8221; or &#8220;prototypes&#8221; and scarce.</p>
<p>When working with data, persistency tools like JPA assist us with the task of generating Java objects for our numerous domain model entities. These objects have normally no behavior or just a little which is typically coded in the &#8220;entity&#8221; classes. But, what happens if we want to inject our services in some of these entities to have a richer domain model? Normally, we have to tackle this task ourselves and inject the services by hand. This happens because such objects do not by default participate in the injection context (unless we are using JEE or weaving).</p>
<p>While this not a difficult task, it is a purely programmatic one and breaks one of the principles of dependency injection: declarative dependencies definition.<br />
In GUICE (and therefore in GIN) there is a concept called &#8220;assisted injection&#8221; that helps us to create objects which require dependencies from the injection context but also instance specific property values. Basically, it consists of using a factory interface with methods to instantiate the objects as managed beans (injected objects) receiving each method only the object&#8217;s specific properties as parameters. The object constructors receive of course not only the object properties but also the dependencies who should be retrieved from the dependency injection context.</p>
<p>Because an image is worth a thousand words, let&#8217;s see this with an example. What we would like to achieve here is to have only one view class (&#8220;PingPongView&#8221;) instead of two specific ones that have almost the same code. If we inspect both view classes, the differences are: to which event each reacts and which text each shows. If we refactor these values as constructor parameters, we have the following resulting class:<br />
<pre><code>
public class PingPongView extends Label {
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public PingPongView(final Racket racket, @Assisted final String text, @Assisted Class&lt;? extends GwtEvent&gt; eventClass) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.onStroke(eventClass, new Command() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void execute() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(text);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Timer() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void run() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setText(&quot;&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;racket.hit();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}.schedule(1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Probably you already noticed the @Assisted annotation in the new extracted parameters. It just indicates GIN that the non-annotated parameters should be resolved as dependencies and that the annotated ones should be provided though parameters in a factory method of the assisted injection factory:<br />
<pre><code>
public interface AssistedInjectionFactory {
&nbsp;&nbsp;&nbsp;&nbsp;public PingPongView createPingPongView(String text, Class&lt;? extends GwtEvent&gt; eventClass);
}
</code></pre><br />
To install this factory in our dependency injection context we need to modify our GIN module class like this:<br />
<pre><code>
public class InjectorModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
And in the injector class, we have to expose the factory as any other dependency:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static final Injector INSTANCE = GWT.create(Injector.class);

&nbsp;&nbsp;&nbsp;&nbsp;public Racket getRacket();
&nbsp;&nbsp;&nbsp;&nbsp;public AssistedInjectionFactory getFactory();
}
</code></pre><br />
And last but not least, let&#8217;s use the assisted injection in our application:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Injector injector = Injector.INSTANCE;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final AssistedInjectionFactory factory = injector.getFactory();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(factory.createPingPongView(&quot;Ping&quot;, PingEvent.class));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(factory.createPingPongView(&quot;Pong&quot;, PongEvent.class));
...
</code></pre></p>
<p>In this article, we have applied new dependency injection recipes to our GWT demo application. I hope that they can help you give a better structure to your GWT applications and also learn dependency injection features and its &#8220;best practices&#8221;.</p>
<p>The source code of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/06/Part2.zip'>here</a>. To see the application working, unzip the file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<p>The other parts of this series can be found <a href='http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin'>here</a> and <a href='http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii'>here</a>.</p>
<p>I hope that you enjoyed reading this article as much as I did writing it.<br />
See you soon!</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GWT Dependency Injection recipes using GIN</title>
		<link>http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/</link>
		<comments>http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 11:10:06 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Sample Apps]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[GIN]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=2046</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/";</script>code { font-size: 12px; overflow: auto; } Dependency Injection is already a well-known and discussed topic in the Java world and there are a good number of well established frameworks and libraries supporting this concept. While for many developers it is a must, some others seem not to be so convinced and think that it [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/";</script><style type="text/css">
    code {
        font-size: 12px;
        overflow: auto;
    }
</style>
<p>Dependency Injection is already a well-known and discussed topic in the Java world and there are a good number of well established frameworks and libraries supporting this concept. While for many developers it is a must, some others seem not to be so convinced and think that it does not bring that much value to a project. Of course, it depends on the project, but with the time, it seems that its benefits have been widely accepted and support for it has been incorporated in almost every piece of software where it seemed to make sense.</p>
<p>This is also the case of <a href='http://code.google.com/webtoolkit/'>Google Web Toolkit</a> where <a href='http://code.google.com/p/google-gin/'>GIN</a> provides dependency injection support on the client side.<br />
GIN is a mainly a GWT-based version of the Google&#8217;s homegrown dependency injection framework <a href='http://code.google.com/p/google-guice/'>GUICE</a>. But because the GWT applications run on top of the browser JavaScript engine instead of on the JVM, some of the GUICE features are not available in GIN. On the other hand, GWT provides some poweful features for code generation that have enabled a meaningful subset of the GUICE features to be implemented in GWT.</p>
<p>Instead of writing an introduction tutorial to the GIN capabilities, the purpose of this article series is to illustrate some of the GIN features while applying some recipes to an existing application.  </p>
<p>Maybe you already know this <a href='http://tv.jetbrains.net/videocontent/gwt-event-bus-basics'>little screencast</a> where Hamlet D&#8217;Arcy explains how to decouple GWT components by mean of an &#8220;event bus&#8221; in a little &#8220;ping-pong&#8221; simulation. If not, maybe you would like to spend five minutes watching the screencast and, once you are done, read on and learn how to apply some dependency injection techniques to improve this little application.</p>
<p>The source code of the application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/04/Part0.zip'>here</a>. To see the application working, unzip the file, change to the folder where the Maven pom file is stored and type the command: &#8220;mvn clean gwt:run&#8221;. After the GWT &#8220;Development mode&#8221; application starts, click on the &#8220;Launch Default Browser&#8221; button.</p>
<h3>Adding GIN to the application</h3>
<p>In order to start using GIN in the sample application, the first task that we should accomplish is adding the GIN dependencies to the project. Because the build of the application is Maven-based, we need simply to add the following dependency to the Maven pom file:<br />
<pre><code>
&lt;dependency&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;groupId&gt;com.google.gwt.inject&lt;/groupId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;artifactId&gt;gin&lt;/artifactId&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;version&gt;1.5.0&lt;/version&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;
</code></pre><br />
Note: If you experience issues while using GIN 1.5 with GWT versions previous to 2.2, consider using GIN 1.0. With GWT version 2.2 and superior ones use GIN 1.5 instead.</p>
<p>Ok, and what now? First, let&#8217;s have a look at the &#8220;main&#8221; class of our GWT application, the &#8220;Simulator&#8221; class:<br />
<pre><code>
public class Simulator implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final EventBus bus = new SimpleEventBus();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(new PingView(bus));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(new PongView(bus));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bus.fireEvent(new PingEvent());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(button);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
The first &#8220;dependency&#8221; that we can identify here is the &#8220;event bus&#8221;. Because an event bus is heavily used in a GWT application to enable component communication with low coupling, it would be an improvement to avoid instantiating the event bus explicitly and having to expose the unique instance used in some class (&#8220;singleton&#8221;). To achieve this, we need to create two new elements: the GIN injector and at least one GIN module.<br />
The GIN injector is an interface that will be instantiated by mean of the GWT deferred binding functionality (&#8220;GWT.create()&#8221; factory method) and that basically declare the &#8220;managed beans&#8221; of our GWT application. These are the dependencies that can be directly accessed using an instance of the injector or that will be used by the injector to satisfy required dependencies while performing injection:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static final Injector INSTANCE = GWT.create(Injector.class);

&nbsp;&nbsp;&nbsp;&nbsp;public EventBus getEventBus();
}
</code></pre><br />
The &#8220;InjectorModule&#8221; class referred in the &#8220;@GinModules&#8221; annotation is the so called &#8220;GIN Module&#8221;. In this class, we will configure how the instances of the managed beans should be created:<br />
<pre><code>
public class InjectorModule extends AbstractGinModule {
&nbsp;&nbsp;&nbsp;&nbsp;@Override
&nbsp;&nbsp;&nbsp;&nbsp;protected void configure() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Having a look at both files, it is not difficult to understand what we are doing. In the injector and by mean of the &#8220;getEventBus&#8221; method, we have <strong>declared an injectable dependency</strong> of the interface type &#8220;EventBus&#8221;. In the module and by mean of the GIN Java DSL, we have <strong>provided an implementation</strong> for the dependency and also specified that, no matter how many times we call the &#8220;getEventBus&#8221; method in the injector, always the same unique instance should be returned. This is also part of the dependency injection configuration process and is sometimes referred as &#8220;scope binding&#8221;.<br />
Please, take care of not using different injector instances within the application because, in that case, you would get different event bus instances (one per injector instance).</p>
<p>Let&#8217;s see now how to use the injector in the &#8220;Simulator&#8221; class to perform the injection:<br />
<pre><code>
public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;final Injector injector = Injector.INSTANCE;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(new PingView(injector.getEventBus()));
&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(new PongView(injector.getEventBus()));

&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;injector.getEventBus().fireEvent(new PingEvent());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(button);
}
</code></pre><br />
If you compare this new version of the &#8220;Simulator&#8221; class with the previous one, there is not yet a major improvement in the code structure and not even a reduction in the number of code lines. The main improvement so far, is that the instantiation of the event bus is configured in a central point and that the implementation selected is not known outside this configuration class. Anyway, nothing that cannot be done with a simple &#8220;factory&#8221; class.<br />
Where comes then the benefit of using dependency injection? OK, let&#8217;s know move the instantiation of the &#8220;view components&#8221; to the injector. For this, adjust the classes in the following way:<br />
<pre><code>
@GinModules(InjectorModule.class)
public interface Injector extends Ginjector {
&nbsp;&nbsp;&nbsp;&nbsp;public static final Injector INSTANCE = GWT.create(Injector.class);
&nbsp;&nbsp;&nbsp;&nbsp;public EventBus getEventBus();
&nbsp;&nbsp;&nbsp;&nbsp;public PingView getPingView();
&nbsp;&nbsp;&nbsp;&nbsp;public PongView getPongView();
}
</code></pre><br />
Then, in the &#8220;Simulator&#8221; class, use the injector to also create the views like this:<br />
<pre><code>
public class Simulator implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Injector injector = Injector.INSTANCE;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pingSlot&quot;).add(injector.getPingView());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;pongSlot&quot;).add(injector.getPongView());

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final Button button = new Button(&quot;Serve!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.addClickHandler(new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;button.setVisible(false);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;injector.getEventBus().fireEvent(new PingEvent());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get(&quot;buttonSlot&quot;).add(button);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
If the &#8220;PingView&#8221; and &#8220;PongView&#8221; classes had a default constructor, it would suffice with the previous two changes for the application to work normally. GIN would be, in that case, smart enough to find the classes and create the instances. But, because that is not the case and both classes expect a communication event bus passed as parameter in the constructor, we need to annotate the constructor with the &#8220;@Inject&#8221; annotation. Then, GIN will try to resolve the parameters in the constructor using a simple dependency resolution mechanism: if there is a method in the injector interface that returns the class of the parameter, this method will be used to obtain an instance of that class. If there is not such a declared method in the injector, but the parameter class has a default constructor, a new instance will then be instantiated. The little detail here is that the instantiation will be done using <a href='http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html'>GWT deferred binding</a> (&#8220;GWT.create()&#8221; factory method), giving GWT the possibility of &#8220;enhancing&#8221; the code, instead of simply using the default constructor of the class.<br />
This way, if every of the constructor parameters can be resolved as dependencies (fulfill at least one of the previous conditions), GIN can create an instance with its dependencies injected and without requiring additional configuration.</p>
<p>To make the application work, add the &#8220;@Inject&#8221; annotation to the constructors of both view components as shown in this example:<br />
<pre><code>
...
&nbsp;&nbsp;&nbsp;&nbsp;@Inject
&nbsp;&nbsp;&nbsp;&nbsp;public PongView(final EventBus bus) {
...
</code></pre><br />
Note: GUICE, and therefore GIN, support the JSR-330 standard annotations for dependency injection (package: javax.inject.*) which should be a preferred option in comparison to proprietary annotations with the same semantic but defined in non-standard packages (package: com.google.inject.*). This makes possible to re-arrange the dependencies without changing the code (the type will be loaded from the classpath without being relevant in which JAR file or classpath entry it is located).</p>
<p>With these changes in the sample application, we have learnt:</p>
<ul>
<li>how to configure GIN using an injector interface and a module class.</li>
<li>How to define a first dependency: the event bus.</li>
<li>How to select an implementation for the dependency: the &#8220;SimpleEventBus&#8221; class.</li>
<li>How to bind this dependency to a context: the application context in this case (also referred sometimes as &#8220;singleton scope&#8221;).</li>
<li>And finally, how to get the dependency injected in a constructor (what is only one of the many ways of doing dependency injection in GIN).</li>
</ul>
<p>For this series of articles, we chose intentionally a simple application where to apply some simple dependency injection recipes. If you are not yet convinced of its benefits, don&#8217;t abandon yet! We have just started and in the following articles, we will explore some other possibilities and use cases.</p>
<p>I hope that you enjoyed reading the article and also to see you in the next part.</p>
<p>The other parts of this series can be found <a href='http://www.canoo.com/blog/2011/06/14/gwt-dependency-injection-recipes-using-gin-ii'>here</a> and <a href='http://www.canoo.com/blog/2011/06/20/gwt-dependency-injection-recipes-using-gin-iii'>here</a>.</p>
<p>The source code of the dependency-enabled application can be downloaded <a href='http://www.canoo.com/blog/wp-content/uploads/2011/04/Part1.zip'>here</a>. To execute the application, simply follow the same steps described above for the initial version.</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Screencast: GWT Event Bus Basics</title>
		<link>http://www.canoo.com/blog/2011/02/25/screencast-gwt-event-bus-basics/</link>
		<comments>http://www.canoo.com/blog/2011/02/25/screencast-gwt-event-bus-basics/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 15:10:31 +0000</pubDate>
		<dc:creator>Hamlet</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hamlet]]></category>
		<category><![CDATA[Screencast]]></category>

		<guid isPermaLink="false">http://www.canoo.com/blog/?p=1893</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/02/25/screencast-gwt-event-bus-basics/";</script>Wheee! I made a screencast. Happy Friday to you too. This screencast is about the GWT Event Bus. It explains how an Event Bus can be used to loosely couple controllers or views in MVC applications, and includes live coding of a GWT using the SimpleEventBus client. The source is all available on GitHub. And [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2011/02/25/screencast-gwt-event-bus-basics/";</script><p>Wheee! I made a screencast. Happy Friday to you too.
<div>
</div>
<div>This screencast is about the GWT Event Bus. It explains how an Event Bus can be used to loosely couple controllers or views in MVC applications, and includes live coding of a GWT using the SimpleEventBus client. The source is all <a href="https://github.com/HamletDRC/presentations/tree/master/presopatterns/samples/GwtEventDemo">available on GitHub</a>. And as always, <a href="http://www.dzone.com/links/gwt_event_bus_basics.html">up votes are appreciated</a>.</p>
<p>If it doesn&#8217;t play correctly, you may want to launch it from the <a href="http://tv.jetbrains.net/videocontent/gwt-event-bus-basics">JetBrains.tv site</a>. </p>
<p><object width="400" height="300" id="_player" name="_player" data="http://tv.jetbrains.net/sites/default/files/flowplayer3/flowplayer-3.2.2.swf" type="application/x-shockwave-flash"><param name="movie" value="http://tv.jetbrains.net/sites/default/files/flowplayer3/flowplayer-3.2.2.swf"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="flashvars" value="config={&quot;clip&quot;:{&quot;baseUrl&quot;:&quot;http://tv.jetbrains.net&quot;,&quot;scaling&quot;:&quot;orig&quot;,&quot;autoPlay&quot;:false,&quot;autoBuffering&quot;:true,&quot;url&quot;:&quot;sites/default/files/videos/converted/gwtevents.flv&quot;},&quot;plugins&quot;:{&quot;controls&quot;:{&quot;stop&quot;:true}},&quot;playlist&quot;:[{&quot;baseUrl&quot;:&quot;http://tv.jetbrains.net&quot;,&quot;scaling&quot;:&quot;orig&quot;,&quot;autoPlay&quot;:false,&quot;autoBuffering&quot;:true,&quot;url&quot;:&quot;http://tv.jetbrains.net/sites/default/files/videos/converted/gwtevents.flv&quot;}]}"></object></p>
<p>&#8220;<span class="Apple-style-span" style="font-family: Verdana, sans-serif; font-size: 14px; color: rgb(73, 73, 73); line-height: 20px; ">This screencast explains why MVC applications benefit from an event bus, and it demonstrates how to create, wire, and respond to events in Google Web Toolkit (GWT). Not bad for just six and a half minutes.&#8221; Too much fun. </span></div>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2011/02/25/screencast-gwt-event-bus-basics/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2011/02/25/screencast-gwt-event-bus-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GWT UiBinder: Better Web App Separation of Concerns</title>
		<link>http://www.canoo.com/blog/2010/04/26/gwt-uibinder-better-web-app-separation-of-concerns/</link>
		<comments>http://www.canoo.com/blog/2010/04/26/gwt-uibinder-better-web-app-separation-of-concerns/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 09:44:10 +0000</pubDate>
		<dc:creator>alberto</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[alberto]]></category>
		<category><![CDATA[ria]]></category>

		<guid isPermaLink="false">http://canoo.com/blog/?p=1057</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2010/04/26/gwt-uibinder-better-web-app-separation-of-concerns/";</script>One of the newest features introduced in Google Web Toolkit 2.0 is the &#8220;UiBinder&#8220;. This new way of building views allows the developer to use a declarative approach when doing the layout of a GWT application. In case you have not heard about it, GWT is a set of tools that enables Java developers to [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2010/04/26/gwt-uibinder-better-web-app-separation-of-concerns/";</script><p>One of the newest features introduced in Google Web Toolkit 2.0 is the &#8220;<a href="http://code.google.com/webtoolkit/doc/latest/DevGuideUiBinder.html">UiBinder</a>&#8220;. This new way of building views allows the developer to use a declarative approach when doing the layout of a GWT application. </p>
<p>In case you have not heard about it, GWT is a set of tools that enables Java developers to develop &#8220;rich web applications&#8221; in the Java language. The term &#8220;rich web application&#8221; implies two things:</p>
<ul>
<li>Desktop-like interaction, where a user action has immediate feedback (e.g.: input validation, search box terms suggestions, results highlighting, etc) without requiring a server round-trip (executing an HTTP request with the result of reloading the page being browsed). In the world of web applications, this is typically accomplished using Ajax (what is also what GWT does).</li>
<li>An application running within a browser, but using native browser technologies (HTML, CSS and JavaScript). If we were talking about applications held in browser plug-ins and running out-of-process within their own virtual machines or by mean of run-times, I would prefer to use the more general term &#8220;rich internet applications&#8221;. These are launched from a web browser but behave as black boxes for other user agents (e.g.: web crawlers).</li>
</ul>
<p>GWT allows us is write Ajax-based &#8220;rich web applications&#8221; using the Java language instead of the traditional JavaScript. GWT takes your Java code and compiles into JavaScript, HTML, and CSS. So while you write the program in Java, it is translated into a traditional web application. </p>
<h3>Building user interfaces in GWT</h3>
<p>The most common approach to building desktop application interfaces is using visual components called &#8220;widgets&#8221;. For developers with Swing experience, GWT <em>seems</em> a marvellous step forward because it allows you to leverage most of your knowledge with far less effort than the alternatives. I say seems because this can also be of disadvantage, as I&#8217;ll show with an example.<br />
If we had to create a web interface for a simple web search application, the most common way of doing the interface would be an HTML form for submitting the query and an HTML list to show the results:<br />
<pre><pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">&lt;html&gt;
&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Search application&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;searchBox&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Search:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=&quot;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label for=&quot;queryInput&quot;&gt;Query:&lt;/label&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input id=&quot;queryInput&quot; type=&quot;text&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Go!&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;
&lt;/div&gt;
&lt;div id=&quot;searchResults&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Results:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul id=&quot;results&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 1&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 2&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 3&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></pre><br />
In the case of GWT, a Swing developer might write widget and layout code like this:<br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchGwt implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Widget searchBox = createSearchBox(&quot;Query:&quot;, &quot;Go!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Panel searchResults = createSearchResults();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VerticalPanel mainContainer = new VerticalPanel();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(new Label(&quot;Search:&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(searchBox);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(new Label(&quot;Results:&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(searchResults);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get().add(mainContainer);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private Widget createSearchBox(String inputLabel, String buttonLabel) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HorizontalPanel searchBox = new HorizontalPanel();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchBox.add(new Label(inputLabel));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchBox.add(new TextBox());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchBox.add(new Button(buttonLabel));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return searchBox;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private Panel createSearchResults() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VerticalPanel searchResults = new VerticalPanel();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 1; i &lt;= 3; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchResults.add(new Label(&quot;Result &quot; + i));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return searchResults;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
This approach is a valid one and, with some CSS styling, produces a neat interface, but it introduces a too complicated DOM structure that does not scale well for bigger applications:<br />
<pre><pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">&lt;html&gt;
&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Search GWT application&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- GWT script and iframe tags omitted --&gt;
&lt;div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;gwt-Label&quot;&gt;Search:&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tbody&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td align=&quot;left&quot; style=&quot;vertical-align: top;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;gwt-Label&quot;&gt;Query:&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td align=&quot;left&quot; style=&quot;vertical-align: top;&quot;&gt;&lt;input type=&quot;text&quot; tabindex=&quot;0&quot; class=&quot;gwt-TextBox&quot;/&gt;&lt;/td&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td align=&quot;left&quot; style=&quot;vertical-align: top;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;button type=&quot;button&quot; tabindex=&quot;0&quot; class=&quot;gwt-Button&quot;&gt;Go!&lt;/button&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tbody&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;gwt-Label&quot;&gt;Results:&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;gwt-Label&quot;&gt;Result 1&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;gwt-Label&quot;&gt;Result 2&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class=&quot;gwt-Label&quot;&gt;Result 3&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></pre><br />
Compare the handcrafted HTML tags with the DOM produced using GWT widgets, and the first one is simpler. One of the reasons of the extra verbosity of the GWT one is the use of &#8220;HorizontalPanel&#8221; to layout the search box horizontally. This is a panel based in tables and it introduces a &#8220;too verbose&#8221; HTML table for the layout. In the case of a horizontal layout, we can also use a FlowPanel, altogether with some CSS styles, to avoid the search box components stacking vertically (which is the default layout for the HTML &#8220;div&#8221; elements that a &#8220;FlowPanel&#8221; produces). The problem is that this approach mixes widgets for layout with CSS for layout (not for styling) what stops being a pure widget approach.<br />
The next thing to notice in the DOM produced by GWT is the excessive presence of GWT CSS classes (e.g.: &#8220;gwt-Label&#8221;, &#8220;gwt-TextBox&#8221;). While they are most times useful when it comes to style the widgets, they are many times not used or they do not fit always our CSS selectors strategy.<br />
Lastly, there is a lack of HTML semantics in the DOM produced by GWT. Almost every element is a &#8220;div&#8221;. From the point of view of web standards, it is much more convenient to use an HTML list for the results as it is to use a collection of &#8220;div&#8221; elements. Producing HTML labels associated with form fields (search box label) is also a usability and accessibility improvement. Using tables for layout should be avoided.<br />
For a real-world application that use input fields, trees, tables, &#8220;tab panels&#8221; or splitters, etc&#8230; all in the same screen, it is a good practice to get rid of the intermediate superfluous DOM elements. It reduces drastically the amount of &#8220;glue&#8221; elements, making the DOM much more compact and faster to modify. We should avoid &#8220;glue&#8221; and automatic generated code, because it needs to be compiled, loaded, executed and maintained. In the case of an overly verbose DOM structure, CSS issues may appear because of the complex structure interactions. Fixing them is akin to fixing bugs in generated code: very difficult!</p>
<h3>Mixing HTML and Widgets</h3>
<p>So how can we mix widgets and html properly? Before GWT version 2.0, the most common way was to use &#8220;RootPanel.get(&#8216;someId&#8217;)&#8221; to access an HTML element in the application host page, and then create an object there to attach the widgets to (ie. a &#8220;RootPanel&#8221;). If we need to embed only a few widgets in the host page, this technique suffices. But doing this in a real application with a large number of widgets becomes complex and slow.<br />
UiBinder scales better because it does not inject widgets into the HTML of the host page. Instead, you declare your layout in a stand-alone HTML file that can be composed with other components as many times as necessary to build more complex interfaces. Composition entails componentization, allowing the developer to create subparts of the user interface (UI components) that can be packaged, re-used and tested in isolation.<br />
But let us see how with an example:<br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchGwt implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SearchComponent searchComponent = new SearchComponent();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get().add(searchComponent);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
<pre><pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">&lt;ui:UiBinder xmlns:ui=&#039;urn:ui:com.google.gwt.uibinder&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:g=&#039;urn:import:com.google.gwt.user.client.ui&#039;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:HTMLPanel&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchBox&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Search:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=&quot;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label for=&quot;queryInput&quot;&gt;Query:&lt;/label&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input id=&quot;queryInput&quot; type=&quot;text&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Go!&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchResults&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Results:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul id=&quot;results&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 1&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 2&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 3&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ul&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/g:HTMLPanel&gt;
&lt;/ui:UiBinder&gt;</pre></pre><br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchComponent extends Composite {
&nbsp;&nbsp;&nbsp;&nbsp;interface Binder extends UiBinder&lt;Widget, SearchComponent&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private static Binder uiBinder = GWT.create(Binder.class);

&nbsp;&nbsp;&nbsp;&nbsp;public SearchComponent() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initWidget(uiBinder.createAndBindUi(this));
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Our example has been split into three files: the first one is the GWT entry point, that now only instantiates and inserts our new UI component at the interface top level. The second is an XML file representing the view and is required by UiBinder to act as a mark-up template. The third is the Java class implementing the component logic (that as of now is almost non-existent) and invoking UiBinder.<br />
If you run this code so far, you will notice that now the appearance is exactly the same as in the handcrafted HTML and that there is not yet real integration between the view of the component (mark-up template) and the logic of the component (Java class). Let us fix this by substituting the HTML input field with a GWT &#8220;TextBox&#8221;:<br />
<pre><pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">&lt;ui:UiBinder xmlns:ui=&#039;urn:ui:com.google.gwt.uibinder&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:g=&#039;urn:import:com.google.gwt.user.client.ui&#039;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:HTMLPanel&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchBox&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Search:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=&quot;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label for=&quot;queryInput&quot;&gt;Query:&lt;/label&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:TextBox ui:field=&quot;queryInput&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Go!&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchResults&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Results:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul id=&quot;results&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;Result 1&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li&gt;Result 2&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li&gt;Result 3&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ul&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/g:HTMLPanel&gt;
&lt;/ui:UiBinder&gt;</pre></pre><br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchComponent extends Composite {
&nbsp;&nbsp;&nbsp;&nbsp;interface Binder extends UiBinder&lt;Widget, SearchComponent&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private static Binder uiBinder = GWT.create(Binder.class);

&nbsp;&nbsp;&nbsp;&nbsp;@UiField
&nbsp;&nbsp;&nbsp;&nbsp;TextBox queryInput;

&nbsp;&nbsp;&nbsp;&nbsp;public SearchComponent() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initWidget(uiBinder.createAndBindUi(this));
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Notice the changes in the template where the HTML input field has been substituted by &#8220;<g:TextBox...>&#8221; and also the little change in the Java class that now has a field of type &#8220;TextBox&#8221; annotated with &#8220;UiField&#8221;. When the Java class is instantiated, the UiBinder creates and injects the &#8220;TextBox&#8221; using the matching field name and &#8220;ui:field&#8221; attribute. The result is almost the same DOM structure with only some extra attribute values generated by GWT. Note that we also eliminated the &#8220;id&#8221; HTML attribute. This last change is because of a current limitation in UiBinder which does not allow defining both attributes in the same element. This issue can be solved setting the id attribute in the constructor after the UiBinder has been invoked.<br />
As next step, let us now integrate the results list by writing out some simple HTML list elements:<br />
<pre><pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">&lt;ui:UiBinder xmlns:ui=&#039;urn:ui:com.google.gwt.uibinder&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:g=&#039;urn:import:com.google.gwt.user.client.ui&#039;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:HTMLPanel&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchBox&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Search:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action=&quot;&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label for=&quot;queryInput&quot;&gt;Query:&lt;/label&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:TextBox ui:field=&quot;queryInput&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Go!&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchResults&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Results:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul ui:field=&quot;results&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/g:HTMLPanel&gt;
&lt;/ui:UiBinder&gt;</pre></pre><br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchComponent extends Composite {
&nbsp;&nbsp;&nbsp;&nbsp;interface Binder extends UiBinder&lt;Widget, SearchComponent&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private static Binder uiBinder = GWT.create(Binder.class);

&nbsp;&nbsp;&nbsp;&nbsp;@UiField
&nbsp;&nbsp;&nbsp;&nbsp;TextBox queryInput;
&nbsp;&nbsp;&nbsp;&nbsp;@UiField
&nbsp;&nbsp;&nbsp;&nbsp;UListElement results;

&nbsp;&nbsp;&nbsp;&nbsp;public SearchComponent() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initWidget(uiBinder.createAndBindUi(this));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queryInput.getElement().setId(&quot;queryInput&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 1; i &lt;= 3; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results.appendChild(createResultsItem(&quot;Result &quot; + i));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private Element createResultsItem(String value) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIElement result = Document.get().createLIElement();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.setInnerHTML(value);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
The changes in the UI Binder template is almost the same as the previous one with a subtle difference: in the previous step, we integrated a GWT widget (&#8220;TextBox&#8221;) replacing the HTML input element. In this second case, we performed a to &#8220;bind&#8221; the &#8220;ul&#8221; HTML element from the view because there is no GWT list widget that matches a simple HTML list. Instead of using the API of a GWT widget, we are using the GWT DOM API to directly modify it.</p>
<h3>Adding behaviour</h3>
<p>Let us now go back to the first GWT approach and modify it to create the results when clicking the button. This is what your pre-UiBinder code might look like:<br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchGwt implements EntryPoint {
&nbsp;&nbsp;&nbsp;&nbsp;public void onModuleLoad() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Panel searchResults = new FlowPanel();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Widget searchBox = createSearchBox(&quot;Query:&quot;, &quot;Go!&quot;, searchResults);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FlowPanel mainContainer = new FlowPanel();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(new Label(&quot;Search:&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(searchBox);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(new Label(&quot;Results:&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mainContainer.add(searchResults);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RootPanel.get().add(mainContainer);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private Widget createSearchBox(String inputLabel, String buttonLabel,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Panel searchResults) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HorizontalPanel searchBox = new HorizontalPanel();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchBox.add(new Label(inputLabel));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchBox.add(new TextBox());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchBox.add(new Button(buttonLabel,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createClickHandler(searchResults)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return searchBox;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private ClickHandler createClickHandler(final Panel searchResults) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new ClickHandler() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public void onClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 1; i &lt;= 3; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;searchResults.add(new Label(&quot;Result &quot; + i));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
Here we introduced a &#8220;ClickHandler&#8221; that modifies the results container with some fake ones when the search button is clicked.<br />
Doing the same with the UiBinder approach results on the following code:<br />
<pre><pre style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">&lt;ui:UiBinder xmlns:ui=&#039;urn:ui:com.google.gwt.uibinder&#039;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:g=&#039;urn:import:com.google.gwt.user.client.ui&#039;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:HTMLPanel&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchBox&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Search:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;label for=&quot;queryInput&quot;&gt;Query:&lt;/label&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:TextBox ui:field=&quot;queryInput&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:Button ui:field=&quot;searchButton&quot; text=&quot;Go!&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id=&quot;searchResults&quot;&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&gt;Results:&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul ui:field=&quot;results&quot;/&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/g:HTMLPanel&gt;
&lt;/ui:UiBinder&gt;</pre></pre><br />
<pre><code style="border: 1px dashed #999999; padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: #000000; background-color: #eeeeee; font-size: 12px; line-height: 14px; width: 100%;">
public class SearchComponent extends Composite {
&nbsp;&nbsp;&nbsp;&nbsp;interface Binder extends UiBinder&lt;Widget, SearchComponent&gt; {
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private static Binder uiBinder = GWT.create(Binder.class);

&nbsp;&nbsp;&nbsp;&nbsp;@UiField
&nbsp;&nbsp;&nbsp;&nbsp;TextBox queryInput;
&nbsp;&nbsp;&nbsp;&nbsp;@UiField
&nbsp;&nbsp;&nbsp;&nbsp;Button searchButton;
&nbsp;&nbsp;&nbsp;&nbsp;@UiField
&nbsp;&nbsp;&nbsp;&nbsp;UListElement results;

&nbsp;&nbsp;&nbsp;&nbsp;public SearchComponent() {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initWidget(uiBinder.createAndBindUi(this));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;queryInput.getElement().setId(&quot;queryInput&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;@UiHandler(&quot;searchButton&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;void buttonClick(ClickEvent event) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 1; i &lt;= 3; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;results.appendChild(createResultsItem(&quot;Result &quot; + i));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;private Element createResultsItem(String value) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIElement result = Document.get().createLIElement();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result.setInnerHTML(value);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
</code></pre><br />
We introduced a &#8220;Button&#8221; GWT widget in the mark-up template together with its corresponding field in the Java class. We also moved the code creating the results to its own method annotated with &#8220;UiHandler&#8221; (take into account that the signature must be compatible with a &#8220;ClickHandler&#8221;). Finally, we removed the &#8220;form&#8221; element from the mark-up (which is not required at all in an Ajax application).<br />
One of the core design principles in the development world is the benefits of separating logic and presentation. What would happen if now we wanted to change the place where the results appear in relation to the search box? Or, if we wanted to get rid of the &#8220;Results:&#8221; label because it is not necessary? In the first GWT approach taken, the way in which the widgets are created and nested is reflected in the Java code together with our logic, making necessary changes to this code. Also &#8220;static&#8221; presentational elements, like the &#8220;Results:&#8221; label, are embedded in the code where. Applying the separation of concerns principle tells us that they do not belong there. Making a change to either presentation or logic necessarily requires changing <em>both</em> presentation and logic. In the &#8220;UiBinder&#8221; approach, because of its inherent separation of concerns, these changes would happen only in the mark-up template or in the domain logic, not in both.</p>
<h3>Conclusions</h3>
<ul>
<li>When building a web application, no matter which framework you use, never forget that you are still using HTML, CSS and JavaScript. The simpler the DOM produced, the faster the application will perform and the less CSS issues you will find.</li>
<li>Web applications have web pages! Always try to use the most semantic HTML structure you can. It improves SEO, usability and accessibility and usually produces compact DOM structures.</li>
<li>Even when you program in your favourite language and do everything in code by mean of widgets, do not forget the advantages of principles like presentation and logic separation. Using UiBinder in GWT produces twice the number of files but each is simpler, cleaner, easy to manage, and easier to maintain.</li>
<li>With presentation separation, you can let your design team produce a HTML prototype of the different parts of your application that can be easily reused afterwards or even in parallel. Two teams being able to work in parallel reduces the project time span and using prototypes reduces the project risks.</li>
<li>Because different tasks require different skills, let the developers code and the designers deal with HTML and CSS. They all will be much happier and the application will be and look much better.</li>
</ul>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2010/04/26/gwt-uibinder-better-web-app-separation-of-concerns/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2010/04/26/gwt-uibinder-better-web-app-separation-of-concerns/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Jazoon &#8217;09: RIA and Security</title>
		<link>http://www.canoo.com/blog/2009/06/23/jazoon-09-ria-and-security/</link>
		<comments>http://www.canoo.com/blog/2009/06/23/jazoon-09-ria-and-security/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 13:54:50 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java RIA]]></category>
		<category><![CDATA[Jazoon]]></category>
		<category><![CDATA[Rich Internet Applications]]></category>

		<guid isPermaLink="false">http://canoo.com/blog/?p=451</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2009/06/23/jazoon-09-ria-and-security/";</script>Session title: RIA Security: Broken by Design From: Joonas Lehtinen, CEO IT Mill IT Mill is the creator of Vaadin: A 100% Java tool for RIA. Joonas outlines a spectrum of complexity from Basic site to 3D games examples: Web Sites (Wikipedia), AJAX Sugar (Facebook), Full RIA He divides „Full RIA“ divide into client side [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2009/06/23/jazoon-09-ria-and-security/";</script><p>Session title: RIA Security: Broken by Design<br />
From: Joonas Lehtinen, CEO IT Mill</p>
<p>IT Mill is the creator of Vaadin: A 100% Java tool for RIA.</p>
<p>Joonas outlines a spectrum of complexity from Basic site to 3D games examples:<br />
Web Sites (Wikipedia), AJAX Sugar (Facebook), Full RIA</p>
<p>He divides „Full RIA“ divide into client side vs. Server driven. Gives a crash course in GWT.</p>
<p>Vaadin: Apparently 100% Java and server driven, which sounds an awful lot like ULC at this stage&#8230; But here’s a difference: It builds on GWT and relies on JavaScript on the client-side.</p>
<p>He goes on to present a bunch of development rules:</p>
<p>Rule #1: Don&#8217;t trust the browser<br />
Rule #2: Complexity is a hiding place for bugs<br />
Rule #3: Large surface give more opportunities for attack. This surface has increased with Web 2.0.</p>
<p> </p>
<p style="text-align: center;"><a href="http://canoo.com/blog/wp-content/uploads/2009/06/p1010092.jpg"><img class="alignnone size-medium wp-image-453" title="p1010092" src="http://canoo.com/blog/wp-content/uploads/2009/06/p1010092-300x200.jpg" alt="" width="300" height="200" /></a></p>
<p> </p>
<p>Difference between GWT and Vaadin architectures is that GWT relies on the client invoking a server-side Web Service API, whereas Vaadin renders the client&#8217;s view on the server.</p>
<p>Erm&#8230; he then offers the cures for the problems (Rules above)&#8230; which I miss because the explanation is compressed into around 5s.</p>
<p>I&#8217;m starting to dislike this presentation at this point. Because here comes another artificial security issue scenario&#8230; which <em>guess which product</em> solves. And I thought product placement in Hollywood movies was irritating.</p>
<p>The issues he raises are legitimate, but the lack of objectivity is obscuring the message. And as I write the presenter is debugging JavaScript which depends on analysing the DOM on the client side &#8211; I&#8217;m not sure if he&#8217;s now analysing the problem or trying to fix it!?</p>
<p>I am formally declaring myself lost at this stage. At least I hope the other attendees are getting something out of this presentation, which has lost focus IMO.</p>
<p>He continues with a discussion about attacking at the transport level, inserting new data on the fly. But come on: A secure transaction in this technical setting will operate under HTTPS, which in most instances will deal with this kind of attack. Unless, of course, that&#8217;s something else I missed.</p>
<p>I think I need a coffee!!!</p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2009/06/23/jazoon-09-ria-and-security/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2009/06/23/jazoon-09-ria-and-security/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Computerworld.ch on GWT and UltraLightClient</title>
		<link>http://www.canoo.com/blog/2008/08/04/computerworldch-on-gwt-and-ultralightclient/</link>
		<comments>http://www.canoo.com/blog/2008/08/04/computerworldch-on-gwt-and-ultralightclient/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 09:37:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[UltraLightClient]]></category>
		<category><![CDATA[computerworld.ch]]></category>
		<category><![CDATA[GWT vs. ULC]]></category>

		<guid isPermaLink="false">http://canoo.com/blog/?p=277</guid>
		<description><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2008/08/04/computerworldch-on-gwt-and-ultralightclient/";</script>Here is a link to an older Computerworld.ch article comparing Google Web Toolkit and Canoo&#8217;s Java RIA library, UltraLightClient (in German only). Google Web Toolkit und Ultra Light Client Die Anbindung an das Internet und ein Browser gehören heute zur Standardausrüstung eines jeden Arbeitsplatzrechners oder Heim-PCs. Stefan Thomas Viele neue Anwendungen werden darum als HTML-basierte [...]]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://www.canoo.com/blog/2008/08/04/computerworldch-on-gwt-and-ultralightclient/";</script><p>Here is a link to an <a href="http://www.computerworld.ch/aktuell/developerworld/43649/index1.html">older Computerworld.ch article</a> comparing <a href="http://code.google.com/webtoolkit/overview.html">Google Web Toolkit</a> and Canoo&#8217;s Java RIA library, <a href="http://www.canoo.com/ulc/">UltraLightClient</a> (<em>in German only</em>).</p>
<p><a href="http://www.computerworld.ch/aktuell/developerworld/43649/index1.html"><img src="http://canoo.com/blog/wp-content/uploads/2008/08/computerworldch-300x44.jpg" alt="Computerworld online" title="computerworldch" width="200"  /></a></p>
<blockquote><p>Google Web Toolkit und Ultra Light Client</p>
<p>Die Anbindung an das Internet und ein Browser gehören heute zur Standardausrüstung eines jeden Arbeitsplatzrechners oder Heim-PCs.</p>
<p>Stefan Thomas</p>
<p>Viele neue Anwendungen werden darum als HTML-basierte Web-Anwendungen implementiert, die leicht verteilt und aktualisiert werden können und von überall, zu jeder Zeit genutzt werden können. Durch den Einsatz von AJAX-Technologien konnte die Benutzerfreundlichkeit der Oberflächen in den letzten Jahren deutlich gesteigert werden, allerdings bei gleichzeitig wachsender Komplexität in der Anwendungsentwicklung. Bibliotheken, Toolkits und Frameworks versprechen hier eine Erleichterung. Beispielsweise durch die Abstraktion der unterschiedlichen JavaScript-Implementierungen der Browser oder durch vorgefertigte, komplexere Bedienelemente wie Menübalken, Tabreiter oder Baumstrukturen.
</p></blockquote>
<p>The article was written by UltraLightClient customer <a href="http://idaplan.com/en/index.html">Dr. Stefan Thomas</a>. Dr. Thomas is an independent consultant, that has developed two large corporate web applications with <a href="http://www.canoo.com/ulc/ ">Canoo&#8217;s UltraLightClient</a> for a global retailer.</p>
<p><strong>GWT vs. ULC:</strong></p>
<p>Comparing the two is a bit like comparing apples and pears (<a href="http://www.computerworld.ch/aktuell/developerworld/43649/index1.html">the article</a> discusses this). On the other hand, the two widget libraries can be used for similar web applications. And both of them address Java developers. </p>
<p>See also the corresponding product websites:<br />
<a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a><br />
<a href="http://www.canoo.com/ulc/">Canoo UltraLightClient</a></p>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><div style="float: left; width: 140px; height: 21px; overflow: hidden; position: relative; left: 8px;"><script>//<![CDATA[
reddit_url="http://www.canoo.com/blog/2008/08/04/computerworldch-on-gwt-and-ultralightclient/";
//]]&gt;
</script><script language="javascript" src="http://reddit.com/button.js?t=1"></script></div>]]></content:encoded>
			<wfw:commentRss>http://www.canoo.com/blog/2008/08/04/computerworldch-on-gwt-and-ultralightclient/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

