<?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>webspin.be</title>
	<atom:link href="http://www.webspin.be/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.webspin.be</link>
	<description>writing about code</description>
	<lastBuildDate>Mon, 29 Jun 2009 19:35:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GWT 1.6: MVP and testing, part 2</title>
		<link>http://www.webspin.be/?p=151</link>
		<comments>http://www.webspin.be/?p=151#comments</comments>
		<pubDate>Mon, 29 Jun 2009 19:35:43 +0000</pubDate>
		<dc:creator>Maarten</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.webspin.be/?p=151</guid>
		<description><![CDATA[As for completeness, I made the previous example in a MVP + eventBus example. This can be download here. Unfortunately, I didn&#8217;t build a model class since that was too much overkill.
The procedure is simple and partially explained in the previous topic:

1. Create a (model,) presenter and view class for each custom widget.
2. Presenter: Add [...]]]></description>
			<content:encoded><![CDATA[<p>As for completeness, I made the previous example in a MVP + eventBus example. This can be download <a href="http://www.webspin.be/code/downloads/SandBox.zip"  onClick="javascript: pageTracker._trackPageview('/code/downloads/SandBox.zip');">here</a>. Unfortunately, I didn&#8217;t build a model class since that was too much overkill.<br />
The procedure is simple and partially explained in the previous topic:<br />
<span id="more-151"></span><br />
1. Create a (model,) presenter and view class for each custom widget.<br />
2. Presenter: Add a interface of the view&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AddRowPresenter
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/*  ....  */</span>
    <span style="color: #000000; font-weight: bold;">interface</span> AddRowWidgetInterface <span style="color: #009900;">&#123;</span>
        HasClickHandlers getSendButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        HasValue<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> getTextBox<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>&#8230; and a method to &#8216;bind&#8217; the widget&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AddRowPresenter
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* .... */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bindWidget<span style="color: #009900;">&#40;</span>AddRowWidgetInterface widget<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">widget</span> <span style="color: #339933;">=</span> widget<span style="color: #339933;">;</span>
        widget.<span style="color: #006633;">getSendButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addClickHandler</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ClickHandler<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>ClickEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                sendAddRowEvent<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><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>&#8230; and use the eventBus to send your events.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AddRowPresenter
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* .... */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> sendAddRowEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        TableRowEvent event <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TableRowEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        event.<span style="color: #006633;">setRowName</span><span style="color: #009900;">&#40;</span>widget.<span style="color: #006633;">getTextBox</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<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>
        eventBus.<span style="color: #006633;">fireEvent</span><span style="color: #009900;">&#40;</span>event<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>The widget could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AddRowWidget <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Composite</span> <span style="color: #000000; font-weight: bold;">implements</span> AddRowPresenter.<span style="color: #006633;">AddRowWidgetInterface</span> <span style="color: #009900;">&#123;</span>
    TextBox rowName <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextBox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Button</span> addRowButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Button</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;send&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Constructor
     * This is a composite of a textbox and a button
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> AddRowWidget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        VerticalPanel mainPanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> VerticalPanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mainPanel.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>rowName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mainPanel.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>addRowButton<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        initWidget<span style="color: #009900;">&#40;</span>mainPanel<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> HasClickHandlers getSendButton<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;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">addRowButton</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> HasValue<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> getTextBox<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;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rowName</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For the code of the receiving widget, it&#8217;s easier to just look into the source. Its bindwidget function looks like this:</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: #000066; font-weight: bold;">void</span> bindWidget<span style="color: #009900;">&#40;</span>SpecialTableInterface _table<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">table</span> <span style="color: #339933;">=</span> _table<span style="color: #339933;">;</span>
        eventBus.<span style="color: #006633;">addHandler</span><span style="color: #009900;">&#40;</span>TableRowEvent.<span style="color: #006633;">TYPE</span>, <span style="color: #000000; font-weight: bold;">new</span> TableRowEventHandler<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAddRow<span style="color: #009900;">&#40;</span>TableRowEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// This should be a function that uses the model, omitted here since nothing is done with the data</span>
                table.<span style="color: #006633;">addSpecialRow</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getRowName</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></pre></div></div>

<p>For more information, feel free to ask in the comments.<br />
Next up: building the stockwatcher example (somewhat extended) with an eventBus, MVP, (the command pattern) and GIN.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webspin.be/?feed=rss2&amp;p=151</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>GWT 1.6 MVP and testing, part 1</title>
		<link>http://www.webspin.be/?p=130</link>
		<comments>http://www.webspin.be/?p=130#comments</comments>
		<pubDate>Thu, 25 Jun 2009 12:02:30 +0000</pubDate>
		<dc:creator>Maarten</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.webspin.be/?p=130</guid>
		<description><![CDATA[After successfully building the eventBus, I decided to extend the application to use the MVP (Model-View-Presenter) pattern. The use of this pattern is nicely explained on the google testing blog. Since it&#8217;s quite a new way of doing this, there is very little example code available.
Since the gwt best practices are mostly focused on testing, [...]]]></description>
			<content:encoded><![CDATA[<p>After successfully building the eventBus, I decided to extend the application to use the MVP (Model-View-Presenter) pattern. The use of this pattern is nicely explained on the <a href="http://googletesting.blogspot.com/2009/02/with-all-sport-drug-scandals-of-late.html">google testing blog</a>. Since it&#8217;s quite a new way of doing this, there is very little example code available.<br />
Since the gwt best practices are mostly focused on testing, and testing is an invaluable part of application design, we&#8217;ll write the test first. I&#8217;m not going in detail about Test Driven Development, there are plenty of good <a href="http://www.amazon.com/gp/product/0321146530?ie=UTF8&#038;tag=httpwwwwebspi-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321146530">books</a><img src="http://www.assoc-amazon.com/e/ir?t=httpwwwwebspi-20&#038;l=as2&#038;o=1&#038;a=0321146530" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> around.<br />
<span id="more-130"></span><br />
I&#8217;ll start with the code straight from the I/O session slides, since that&#8217;s what we want.<br />
1. The MockClickEvent, a fake ClickEvent we can send:</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> MockClickEvent <span style="color: #000000; font-weight: bold;">extends</span> ClickEvent <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Easy. And the MockHasClickHandlers class, implementing the minimum required:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MockHasClickHandlers <span style="color: #000000; font-weight: bold;">implements</span> HasClickHandlers <span style="color: #009900;">&#123;</span>
    ClickHandler lastClickHandler<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> HandlerRegistration addClickHandler<span style="color: #009900;">&#40;</span>ClickHandler handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        lastClickHandler <span style="color: #339933;">=</span> handler<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> HandlerRegistration<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</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> fireEvent<span style="color: #009900;">&#40;</span>GwtEvent<span style="color: #339933;">&lt;?&gt;</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we have a fake click event we can send, and a fake class that can be clicked on, aka a fake button. Great.<br />
2. We&#8217;re also going to send a String to the table, to fill the added row with. This could be an example of sending values with events.<br />
For this fake string to work, we also need a mock textfield (also only minimum required code):</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> MockHasValue <span style="color: #000000; font-weight: bold;">implements</span> HasValue<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> lastValue<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getValue<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;">return</span> lastValue<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> setValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastValue</span> <span style="color: #339933;">=</span> value<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> setValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value, <span style="color: #000066; font-weight: bold;">boolean</span> fireEvents<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        setValue<span style="color: #009900;">&#40;</span>value<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> HandlerRegistration addValueChangeHandler<span style="color: #009900;">&#40;</span>
            ValueChangeHandler<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</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> fireEvent<span style="color: #009900;">&#40;</span>GwtEvent<span style="color: #339933;">&lt;?&gt;</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All this we combine in one mock AddRowWidget:</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> MockAddRowWidget <span style="color: #000000; font-weight: bold;">implements</span> AddRowWidgetInterface <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// the Button</span>
    MockHasClickHandlers save <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockHasClickHandlers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// The Textfield</span>
    MockHasValue value <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockHasValue<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> HasClickHandlers getSendButton<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;">return</span> save<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> HasValue<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> getTextValue<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;">return</span> value<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So what do we have so far?<br />
A mock Composite that contains:<br />
- a mock button on which can be (mock) clicked.<br />
- a mock textfield with a value string.</p>
<p>Next: we need a mock table, implementing the SpecialTable interface (a custom object that has a addRow function), that can listen to the mock event and react accordingly:</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> MockSpecialTableWidget <span style="color: #000000; font-weight: bold;">implements</span> SpecialTableInterface <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> rowValue<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addSpecialRow<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">rowValue</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Almost done! All the ingredients are in place, let&#8217;s create our test:</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: #000066; font-weight: bold;">void</span> testAddRow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// initialize our mock objects</span>
        MockAddRowWidget mockWidget <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockAddRowWidget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        MockSpecialTableWidget mockTableWidget <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockSpecialTableWidget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        create an eventBus
        HandlerManager eventBus <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HandlerManager<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// the Presenter class of the listening table binds the (mock) table widget to the eventBus</span>
        <span style="color: #666666; font-style: italic;">// with a tableRowEventHandler</span>
        SpecialTablePresenter specialTablePresenter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SpecialTablePresenter<span style="color: #009900;">&#40;</span>mockTableWidget, eventBus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// same for the AddRowPresenter: binding (clicking on) the widget to firing the TableRowEvent with the eventBus</span>
        AddRowPresenter addRowPresenter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AddRowPresenter<span style="color: #009900;">&#40;</span>mockWidget, eventBus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// set TextField value</span>
        mockWidget.<span style="color: #006633;">value</span>.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testString&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// fake a click on the mock button</span>
        mockWidget.<span style="color: #006633;">save</span>.<span style="color: #006633;">lastClickHandler</span>.<span style="color: #006633;">onClick</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MockClickEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Did it pass through?</span>
        assertTrue<span style="color: #009900;">&#40;</span>mockWidget.<span style="color: #006633;">value</span>.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> mockTableWidget.<span style="color: #006633;">rowValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>And result: green bar!<br />
<img src="http://www.webspin.be/assets/unitTest1.png" alt="unit test 1" /></p>
<p>This code is only completely relevant if combined with the MVP code.which will be shown in a next post. Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webspin.be/?feed=rss2&amp;p=130</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>GWT: capturing mouse position, drag and drop</title>
		<link>http://www.webspin.be/?p=65</link>
		<comments>http://www.webspin.be/?p=65#comments</comments>
		<pubDate>Sat, 20 Jun 2009 15:58:14 +0000</pubDate>
		<dc:creator>Maarten</dc:creator>
				<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://www.webspin.be/?p=65</guid>
		<description><![CDATA[If you need to use drag and drop in your GWT application, there is a wonderful library available: gwt-dnd. Check out the demo: it offers almost everything one might expect from drag&#038;drop.
Sometimes though, such a vast and feature-rich library is too much. I needed to create a custom scrollbar, and to do this with gwt-dnd [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to use drag and drop in your GWT application, there is a wonderful library available: <a href="http://code.google.com/p/gwt-dnd/">gwt-dnd</a>. Check out the <a href="http://allen-sauer.com/com.allen_sauer.gwt.dnd.demo.DragAndDropDemo/DragAndDropDemo.html">demo</a>: it offers almost everything one might expect from drag&#038;drop.<br />
Sometimes though, such a vast and feature-rich library is too much. I needed to create a custom scrollbar, and to do this with gwt-dnd is <a href="http://groups.google.com/group/gwt-dnd/browse_thread/thread/6361bcbdd4a5a3e9/3f3fc4aed394ab88?lnk=gst&#038;q=constraint#3f3fc4aed394ab88">not that simple</a>, since its constraint panel only affects the dropzone while the draggable element can still be dragged anywhere in the main AbsolutePanel.<br />
So I looked for another way, and in fact it isn&#8217;t hard at all:<br />
<span id="more-65"></span><br />
1. GWT 1.6 introduced a new way to capture mouse movement and any other native DOM event.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Event</span>.<span style="color: #006633;">addNativePreviewHandler</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NativePreviewHandler<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onPreviewNativeEvent<span style="color: #009900;">&#40;</span>NativePreviewEvent event<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>event.<span style="color: #006633;">getNativeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mousemove&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">int</span> xCoord <span style="color: #339933;">=</span> event.<span style="color: #006633;">getNativeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClientX</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> yCoord <span style="color: #339933;">=</span> event.<span style="color: #006633;">getNativeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClientY</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>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>2. Implementing drag&#038;drop brings another problem: only elements that are children of an AbsolutePanel can be moved, with the function <a href="http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/ui/AbsolutePanel.html#setWidgetPosition(com.google.gwt.user.client.ui.Widget,%20int,%20int)">setWidgetPosition()</a>. As a result your object can only be dragged within this AbsolutePanel. This can be overcome (although I&#8217;m afraid it&#8217;s not best practice) by using DOM.setStyleAttribute:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Event</span>.<span style="color: #006633;">addNativePreviewHandler</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NativePreviewHandler<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onPreviewNativeEvent<span style="color: #009900;">&#40;</span>NativePreviewEvent event<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>event.<span style="color: #006633;">getNativeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mousemove&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
<span style="display:block;background-color: #ffc;">            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dragging <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span></span><span style="display:block;background-color: #ffc;">            <span style="color: #009900;">&#123;</span></span><span style="display:block;background-color: #ffc;">                DOM.<span style="color: #006633;">setStyleAttribute</span><span style="color: #009900;">&#40;</span>dragObject.<span style="color: #006633;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;left&quot;</span>, <span style="color: #009900;">&#40;</span>dragObject.<span style="color: #006633;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAbsoluteLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> event.<span style="color: #006633;">getNativeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClientX</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: #0000ff;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span><span style="display:block;background-color: #ffc;">                DOM.<span style="color: #006633;">setStyleAttribute</span><span style="color: #009900;">&#40;</span>dragObject.<span style="color: #006633;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;top&quot;</span>, <span style="color: #009900;">&#40;</span>dragObject.<span style="color: #006633;">getElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getAbsoluteTop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> event.<span style="color: #006633;">getNativeEvent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClientY</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: #0000ff;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span></span><span style="display:block;background-color: #ffc;">            <span style="color: #009900;">&#125;</span></span>        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Put a handler on your object which defines dragging, and add a &#8216;dragging = false&#8217; to the mouseup event, and there it is: basic drag and drop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webspin.be/?feed=rss2&amp;p=65</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building a GWT 1.6 application: Managing events with an eventBus</title>
		<link>http://www.webspin.be/?p=5</link>
		<comments>http://www.webspin.be/?p=5#comments</comments>
		<pubDate>Fri, 19 Jun 2009 21:23:13 +0000</pubDate>
		<dc:creator>Maarten</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[gwt]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.webspin.be/blog/?p=5</guid>
		<description><![CDATA[GWT 1.6 gets rid of all the Listener classes, and replaces them with the new Handler classes. For a comparison of the two, Jason from Lemming Technology Blog has a good overview.
At Google I/O, Ray Ryan talks about best practices for architecting your GWT app. There he mentions using an Eventbus to manage your events, [...]]]></description>
			<content:encoded><![CDATA[<p>GWT 1.6 gets rid of all the Listener classes, and replaces them with the new Handler classes. For a comparison of the two, Jason from <a href="http://lemnik.wordpress.com/2009/03/04/gwts-new-event-model-handlers-in-gwt-16/">Lemming Technology Blog</a> has a good overview.</p>
<p>At Google I/O, Ray Ryan talks about <a href="http://code.google.com/intl/nl/events/io/sessions/GoogleWebToolkitBestPractices.html">best practices for architecting your GWT app</a>. There he mentions using an Eventbus to manage your events, and even provides some example code. Still, it all seemed a bit confusing, so I made a little <a href="/code/downloads/eventBusTest.zip" onClick="javascript: pageTracker._trackPageview('/code/downloads/eventBusTest.zip');">testapplication</a> to test it out.<br />
<span id="more-5"></span><br />
Some points to consider:</p>
<p>1. The only thing passed around is the eventBus itself. This can be done with Dependency Injection, with a Registry Pattern, or whatever floats your boat. In this case DI is used.<br />
2. For each new type of event added, a new event class must be created:</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> TableRowEvent <span style="color: #000000; font-weight: bold;">extends</span> GwtEvent<span style="color: #339933;">&lt;</span>tableRowEventHandler<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     *  For each new event, a new event type must also be specified,
     *  with which the event can be registered
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> GwtEvent.<span style="color: #006633;">Type</span><span style="color: #339933;">&lt;</span>tableRowEventHandler<span style="color: #339933;">&gt;</span> TYPE
        <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GwtEvent.<span style="color: #006633;">Type</span><span style="color: #339933;">&lt;</span>tableRowEventHandler<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">/*
     * required
     */</span>
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> dispatch<span style="color: #009900;">&#40;</span>TableRowEventHandler handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        handler.<span style="color: #006633;">onAddRow</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*
     * required
     */</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> GwtEvent.<span style="color: #006633;">Type</span><span style="color: #339933;">&lt;</span>tableRowEventHandler<span style="color: #339933;">&gt;</span> getAssociatedType<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;">return</span> TYPE<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>3. &#8230; and associated eventHandler Interface:</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> TableRowEventHandler <span style="color: #000000; font-weight: bold;">extends</span> EventHandler <span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * What to do when a TableRowEvent is called
     * @param addTableRowEvent
     */</span>
    <span style="color: #000066; font-weight: bold;">void</span> onAddRow<span style="color: #009900;">&#40;</span>TableRowEvent addTableRowEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>4. Add the handler to the listening object:</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> SpecialTable <span style="color: #000000; font-weight: bold;">extends</span> FlexTable <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// local reference to the eventBus</span>
    HandlerManager eventBus<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> listenToAddRowEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// registering the object to the eventBus</span>
        eventBus.<span style="color: #006633;">addHandler</span><span style="color: #009900;">&#40;</span>TableRowEvent.<span style="color: #006633;">TYPE</span>, <span style="color: #000000; font-weight: bold;">new</span> TableRowEventHandler<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAddRow<span style="color: #009900;">&#40;</span>TableRowEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// do something</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></pre></div></div>

<p>5. Fire the event on the specified occasion:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">HandlerManager eventBus<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// in this case: a function in a button</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> initialize<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;">this</span>.<span style="color: #006633;">addClickHandler</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ClickHandler<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;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>ClickEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// creates a new event, if desired additional data can be sent</span>
            TableRowEvent sent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TableRowEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            eventBus.<span style="color: #006633;">fireEvent</span><span style="color: #009900;">&#40;</span>sent<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></pre></div></div>

<p>And there it is, your eventBus is up and running! And since both action(the button) and reaction(the table) are now completely separated, the timing of the event is no longer imporant.<br />
Next Up:<br />
- <a href="http://www.webspin.be/2009/06/gwt-1-6-mvp-and-testing-part-1/">Adding testing</a>.<br />
- Adding MVP.<br />
- working with <a href="http://code.google.com/p/google-gin/">GIN</a>.<br />
- &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webspin.be/?feed=rss2&amp;p=5</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
