<?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>ryan norris &#187; GWT</title>
	<atom:link href="http://www.ryannorris.com/category/gwt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ryannorris.com</link>
	<description>managing software teams and delivering great results</description>
	<lastBuildDate>Fri, 25 Jun 2010 13:44:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>No, You Can&#8217;t Override JavaScriptObject.equals() in GWT.  Not Yours.</title>
		<link>http://www.ryannorris.com/2010/01/22/no-you-cant-overload-javascriptobject-equals-in-gwt-not-yours/</link>
		<comments>http://www.ryannorris.com/2010/01/22/no-you-cant-overload-javascriptobject-equals-in-gwt-not-yours/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:24:30 +0000</pubDate>
		<dc:creator>Ryan Norris</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hashCode]]></category>
		<category><![CDATA[ood]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.ryannorris.com/?p=152</guid>
		<description><![CDATA[Sigh. For reasons that may or may not be sensible, equals() and hashCode() are finalized in GWT&#8217;s JavaScriptObject. This is an unfortunate discovery.  While there is no inherent equals() or hashCode() in the Javascript language itself (unless I&#8217;ve missed something) that doesn&#8217;t mean GWT couldn&#8217;t make one for me.  JavaScriptObject#equals(&#8230;) instead calls super.equals().  Which is [...]]]></description>
			<content:encoded><![CDATA[<p>Sigh.  For reasons that may or may not be sensible, <code>equals()</code> and <code>hashCode()</code> are finalized in GWT&#8217;s JavaScriptObject.</p>
<p><span id="more-152"></span>This is an unfortunate discovery.  While there is no inherent equals() or hashCode() in the Javascript language itself (unless I&#8217;ve missed something) that doesn&#8217;t mean GWT couldn&#8217;t make one for me.  JavaScriptObject#equals(&#8230;) instead calls super.equals().  Which is really Object#equals(&#8230;) and that&#8217;s really just triple-equality (==).  In object oriented programming, this is what is technically referred to as <em>an </em><em>absolute pain in the ass.</em> It renders JRE emulation of java.util.Set around JSO&#8217;s almost useless.  And it really highlights something that I&#8217;ve had in my head a while now:</p>
<p>If you can&#8217;t use GWT-RPC, you use JSON at your own peril.  JavaScriptOverlay types, while nifty in concept are simply not first-class objects in GWT.  They&#8217;re more akin to a struct or map.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryannorris.com/2010/01/22/no-you-cant-overload-javascriptobject-equals-in-gwt-not-yours/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Java Idiom, Lousy GWT Bug</title>
		<link>http://www.ryannorris.com/2010/01/15/common-java-idiom-lousy-gwt-bug/</link>
		<comments>http://www.ryannorris.com/2010/01/15/common-java-idiom-lousy-gwt-bug/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 04:49:29 +0000</pubDate>
		<dc:creator>Ryan Norris</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java Development]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[strategypattern]]></category>

		<guid isPermaLink="false">http://www.ryannorris.com/?p=145</guid>
		<description><![CDATA[I have to get around to shipping this one to the GWT committers, but this one bit me hard. Let&#8217;s say you have interface Driveable: public interface Driveable &#123; public void accelerate&#40;&#41;; &#160; public void brake&#40;&#41;; &#160; public void tuneRadio&#40;&#41;; &#125; Awesome. Now let&#8217;s say you have class AbstractDriveable: public abstract class AbstractDriveable implements Driveable [...]]]></description>
			<content:encoded><![CDATA[<p>I have to get around to shipping this one to the GWT committers, but this one bit me hard.</p>
<p><span id="more-145"></span>Let&#8217;s say you have interface Driveable:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Driveable <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> accelerate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> brake<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> tuneRadio<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Awesome. Now let&#8217;s say you have class AbstractDriveable:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractDriveable <span style="color: #000000; font-weight: bold;">implements</span> Driveable <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> accelerate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003399;">Window</span>.<span style="color: #006633;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Vroom vroom!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> brake<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003399;">Window</span>.<span style="color: #006633;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Screeeeeech!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Totally cool.  And then you love your favorite FM station in ConcreteDriveable:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteDriveable <span style="color: #000000; font-weight: bold;">extends</span> AbstractDriveable <span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> tuneRadio<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003399;">Window</span>.<span style="color: #006633;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rockin' the oldies!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then we launch into development mode with whatever our application is, and&#8230;something explodes.</p>
<p>So we debug, and upon looking at the code that cleverly calls <code>tuneRadio</code> polymorphically &#8211; we notice that our debugger drops into a catch block for a <code>NoSuchMethodError</code>.</p>
<p>It appears that GWT seems to lose track of this common idiom &#8211; use an interface to define behavior, realize that interface on an abstract class to create default behaviors, and then strategize those behaviors in concrete implementations.</p>
<p>The workaround is simple: redeclare your unrealized interface method in the abstract class.  But this is a pretty nasty little bug.  The GWT guys know this, but it&#8217;s rather critical to GWT that it follow the practices of Java as best as possible.  Otherwise it&#8217;s YAL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryannorris.com/2010/01/15/common-java-idiom-lousy-gwt-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RPC Objects in GWT Now MUST be Serializable</title>
		<link>http://www.ryannorris.com/2009/12/13/rpc-objects-in-gwt-now-must-be-serializable/</link>
		<comments>http://www.ryannorris.com/2009/12/13/rpc-objects-in-gwt-now-must-be-serializable/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 04:42:15 +0000</pubDate>
		<dc:creator>Ryan Norris</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java Development]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.ryannorris.com/?p=134</guid>
		<description><![CDATA[Okay, so maybe it&#8217;s always been this way and I&#8217;ve just not been paying attention &#8211; but it appears that GWT 2.0 now requires one to make RPC objects that will go across the wire explicitly implement java.io.Serializable. A bland RPC implementation that was based on past work I&#8217;ve done was barfing all over me [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so maybe it&#8217;s always been this way and I&#8217;ve just not been paying attention &#8211; but it appears that GWT 2.0 now requires one to make RPC objects that will go across the wire explicitly implement <code>java.io.Serializable</code>.</p>
<p>A bland RPC implementation that was based on past work I&#8217;ve done was barfing all over me at GWT compile-time today:</p>
<pre><span>Rebinding com.<span>ryannorris</span>.staffing.client.project.<span>ProjectCreatorService</span>

Invoking com.google.<span>gwt</span>.<span>dev</span>.<span>javac</span>.<span>StandardGeneratorContext</span>@9b32fe

Generating client proxy for remote service interface 'com.<span>ryannorris</span>.staffing.client.project.ProjectCreatorService'

[ERROR] com.<span>ryannorris</span>.staffing.client.Action&lt;T&gt; has no available <span>instantiable</span> subtypes. (reached via com.<span>ryannorris</span>.staffing.client.Action&lt;T&gt;)

[ERROR]    subtype com.<span>ryannorris</span>.staffing.client.Action&lt;T&gt; is not <span>instantiable</span>

[ERROR]    subtype com.<span>ryannorris</span>.staffing.client.project.<span>ProjectCreatorAction</span> is not assignable to 'com.google.<span>gwt</span>.user.client.<span>rpc</span>.IsSerializable' or 'java.<span>io</span>.Serializable' nor does it have a custom field <span>serializer</span> (reached via com.<span>ryannorris</span>.staffing.client.Action&lt;T&gt;)

[ERROR] com.<span>ryannorris</span>.staffing.client.project.<span>ProjectCreatorAction</span> is not assignable to 'com.google.<span>gwt</span>.user.client.<span>rpc</span>.IsSerializable' or 'java.<span>io</span>.Serializable' nor does it have a custom field <span>serializer</span> (reached via com.<span>ryannorris</span>.staffing.client.Action&lt;T&gt;)</span></pre>
<p>I can&#8217;t say I ever had the pleasure of dealing with this bit of attention to detail from the GWT Compiler in the past.  And while I will readily admit I haven&#8217;t read every change to the underlying architecture that comes with 2.0, this would seem to be a piece that might be worthy of some special mention.</p>
<p><span>It is, of cour<span>se</span>, nearly common sen<span>se</span> to make sure that your RPC objects can go across the wire in the proper way, and thus simply respecting the semantics of java.<span>io</span>.<span>Serializable</span> the same way we would in EJB per <span>se</span>, is pretty reasonable.  Just a little more heads up would have been nice.</span></p>
<p><strong>Update 1: </strong>There seems to be some mention of <a title="RPC Compilation Feedback" href="http://code.google.com/p/google-web-toolkit/issues/detail?id=3461" target="_blank">fixing compiler feedback around RPC serialization here</a>.  But this defect doesn&#8217;t suggest changes that might have actually altered behavior or requirements of the compiler since 1.7.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryannorris.com/2009/12/13/rpc-objects-in-gwt-now-must-be-serializable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple GWT Validation Framework Using Great Design Patterns and MVP</title>
		<link>http://www.ryannorris.com/2009/11/27/validation-in-gwt-with-mvp-and-patterns/</link>
		<comments>http://www.ryannorris.com/2009/11/27/validation-in-gwt-with-mvp-and-patterns/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 13:51:08 +0000</pubDate>
		<dc:creator>Ryan Norris</dc:creator>
				<category><![CDATA[Enterprise Architecture]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[google web toolkit]]></category>
		<category><![CDATA[mvp]]></category>
		<category><![CDATA[ria]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.ryannorris.com/?p=113</guid>
		<description><![CDATA[I&#8217;ve been writing a bit about Google Web Toolkit lately. It undeniably is disrupting traditional browser-based RIA development. But it does lack some features out of the box that most developers have grown accustomed to from frameworks like Flex and Ext. Field validation is one such feature. While the gwt-validation project exists to solve this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing a bit about Google Web Toolkit lately.  It undeniably is disrupting traditional browser-based RIA development.  But it does lack some features out of the box that most developers have grown accustomed to from frameworks like Flex and Ext.  Field validation is one such feature.  While the gwt-validation project exists to solve this very problem, an approach leveraging a good chunk of the existing GWT infrastructure can give you a robust, test-driven, and MVP-friendly approach for validation.<span id="more-113"></span></p>
<p>To get started, let&#8217;s break down our validation needs along the standard lines that we see in frameworks today.  Widgets need to be able to be <em>Validatable</em>.  That is to say, Widgets must be able to be externally seen as having the capability to execute logic which validates their current state.  In my approach, I&#8217;ve chosen to <em>strategize</em> my widgets rather than visit them in order to validate their data.  This decision is largely to enforce contract-driven development and ensure a high level of testability.</p>
<p><strong>Validatable</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Validatable <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Execute validation on the implementing component
	 *
	 * @return true if validation succeeded, false if validation failed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We also now need an interface that will provide the rules for how validation logic should occur.  Something that is <em>Validatable </em>should be able to validate itself with multiple sets of rules.  Our <strong>Validator</strong> interface gives us this flexible cardinality, and has a familiar ring to it.</p>
<p><strong>Validator</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Validator <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Execute the implemented validation logic
	 *
	 * @return true if validation succeeded, false otherwise
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Yes, these two interfaces are the same besides their name.  They are both command interfaces, allowing us to abstract logic behind a single method that we can cope with polymorphically.  The idea remember is that <em>Validatable</em> components can re-use many different <em>Validator</em> implementations.</p>
<p>So let&#8217;s write a simple <em>Validator </em>and <em>Validatable </em>interface.  In this case, we&#8217;ll be validating date input.</p>
<p><strong>DateFormatValidator</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.validator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.i18n.client.DateTimeFormat</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.HasValue</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.Validator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DateFormatValidator <span style="color: #000000; font-weight: bold;">implements</span> Validator <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> HasValue<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> _value<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> DateTimeFormat _format<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Create a validator that will verify that the given string is compliant
	 * with the pattern
	 *
	 * @param format
	 *            The format to validate against, per the specification in
	 *            {@link DateTimeFormat}
	 * @param value
	 *            The value to validate
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> DateFormatValidator<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> pattern, HasValue<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_format <span style="color: #339933;">=</span> DateTimeFormat.<span style="color: #006633;">getFormat</span><span style="color: #009900;">&#40;</span>pattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Set the value to be validated
	 *
	 * @param value a string, nominally representing a date
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span>HasValue<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_value <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * (non-Javadoc)
	 *
	 * @see com.ryannorris.gwt.validation.client.Validator#validate()
	 */</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			_format.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>_value.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// the value couldn't be parsed by the pattern, return false</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, now I have a <em>Validator. </em>How do I now make a widget, let&#8217;s say a simple TextBox, <em>Validatable?</em></p>
<p>Harkon back earlier when I said I was chosing to <em>strategize</em> my Widgets.  That&#8217;s only partially true.  Because we&#8217;re aiming for good TDD, and hence we want to rely on dependency injection &#8211; we will in fact be decorating them as well.  We will <em>strategize </em>around their ability to be validated, while we will <em>decorate </em>them with the <em>Validators</em> that interest us.  This is important, as we wouldn&#8217;t want to have 20 different TextBox implementations that handle various <em>Validation</em> permutations.  We would rather one, <em>decoratable</em> TextBox that can be managed by the presenter in our MVP implementation.  So what does our initial stab at a <em>ValidatableTextBox </em>look like?</p>
<p><strong>ValidatableTextBox</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.widget</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.TextBox</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.Validatable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.validator.DateFormatValidator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ValidatableTextBox <span style="color: #000000; font-weight: bold;">extends</span> TextBox <span style="color: #000000; font-weight: bold;">implements</span> Validatable <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		DateFormatValidator validator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateFormatValidator<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mm/DD/yyyy&quot;</span>,
				getText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> validator.<span style="color: #006633;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wait, you say.  <em>This isn&#8217;t at all what you just described!  This is a text box that will only execute one validator for one date pattern! </em>Well, you&#8217;re right.  We need to find a way to enable decoration of our TextBox in a way that can be nicely tested and extensible.  GWT makes extensive use of decorator pattern, and really strives for testability.  We see this in the various <em>Has</em> interfaces, which are added to classes to give them additional behaviors (such as <em>HasText </em>or <em>HasClickHandler</em>).  So, we will follow suit and create our decorator interface <em>HasValidators.</em></p>
<p><strong>HasValidators</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> HasValidators <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Add a validator to the decorated component
	 *
	 * @param validator The validator to be added
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addValidator<span style="color: #009900;">&#40;</span>Validator validator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And now our updated <em>ValidatableTextBox.</em></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.widget</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.TextBox</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.HasValidators</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.Validatable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.Validator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ValidatableTextBox <span style="color: #000000; font-weight: bold;">extends</span> TextBox <span style="color: #000000; font-weight: bold;">implements</span> Validatable, HasValidators <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Set</span> _validators<span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addValidator<span style="color: #009900;">&#40;</span>Validator validator<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_validators.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>validator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">boolean</span> valid <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Validator validator <span style="color: #339933;">:</span> _validators<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> validator.<span style="color: #006633;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				valid <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> valid<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, that&#8217;s pretty sweet. I have a collection of <em>Validators</em> as a member of the component &#8211; in fact I have a set of them as to ensure no <em>Validator</em> exists more than once on the component.  My <em>validate</em> method now loops through the set of validators and maintains the local state of the validation, allowing the entire validation to fail if any one <em>Validator</em> fails.</p>
<p>A highly testable <em>passive view</em> implementation with this component may look something like:</p>
<p><strong>MySampleValidatableView</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * 
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.event.dom.client.HasKeyPressHandlers</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.user.client.ui.HasValue</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author ryan
 * 
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MySampleValidatableView <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Get the {@link HasValidators} behavior of the date box.
	 * 
	 * @return a {@link HasValidators} behavior
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> HasValidators getDateValidators<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Get the {@link Validatable} behavior of the date box.
	 * 
	 * @return a {@link Validatable} behavior
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Validatable getDateValidatable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Return the {@link HasValue} behavior of the date box. This allows us to
	 * access the value of the control.
	 * 
	 * @return a {@link HasValue} behavior
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> HasValue<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Return the {@link HasKeyPressHandlers} behavior of the date box. When the
	 * user types a key in the field, we will use this event to validate the
	 * current value;
	 * 
	 * @return
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> HasKeyPressHandlers getDateKeyUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Toggle the visual state of the date box so the user receives feedback
	 * when their date is valid
	 * 
	 * @param valid
	 *            Whether or not the control should reflect a valid date entry
	 *            (true) or an invalid one (false)
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> toggleValidDateBox<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">boolean</span> valid<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A lot going on here, and it&#8217;s seems verbose &#8211; but it&#8217;s not, it&#8217;s just highly testable!  We need to be able to access the various behaviors of our text box so that we can test how our presenter<em> collaborates </em>with these behaviors.  We also need to be able to push the passive display functionality back to our view implementation so that our unit tests don&#8217;t inadvertently require a hosted environment to run.  So our presenter needs to be built to:</p>
<ol>
<li>Create a <em>DateFormatValidator</em> with the appropriate pattern and a reference to the component holding the text value to validate;</li>
<li><em>Decorate</em> the component in the view through the <em>HasValidators</em> behavior;</li>
<li>Execute validation whenever the user changes the value of the widget through the <em>HasKeyPressHandlers</em> interface;</li>
<li>Alter the visual display of the component by calling the <em>toggleValidDateBox</em> method.</li>
</ol>
<p>So last but not least, our presenter:</p>
<p><strong>MySamplePresenter</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.event.dom.client.KeyPressEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gwt.event.dom.client.KeyPressHandler</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ryannorris.gwt.validation.client.validator.DateFormatValidator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySamplePresenter <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> MySamplePresenter<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> MySampleValidatableView view<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		DateFormatValidator validator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateFormatValidator<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mm/DD/yyyy&quot;</span>,
				view.<span style="color: #006633;">getDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		view.<span style="color: #006633;">getDateValidators</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addValidator</span><span style="color: #009900;">&#40;</span>validator<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		view.<span style="color: #006633;">getDateKeyUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addKeyPressHandler</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> KeyPressHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onKeyPress<span style="color: #009900;">&#40;</span>KeyPressEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				view.<span style="color: #006633;">toggleValidDateBox</span><span style="color: #009900;">&#40;</span>view.<span style="color: #006633;">getDateValidatable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So there you have it &#8211; a highly testable base for validation in a GWT application using MVP.</p>
<p>In my next post, I&#8217;ll add an event framework to validation, allowing asynchronous validation and abstraction between overall error handling and validation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryannorris.com/2009/11/27/validation-in-gwt-with-mvp-and-patterns/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
