<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Procbits &#187; GWT</title>
	<atom:link href="http://procbits.com/category/gwt/feed/" rel="self" type="application/rss+xml" />
	<link>http://procbits.com</link>
	<description>source code snippets and other random musings about software</description>
	<lastBuildDate>Wed, 01 Feb 2012 19:41:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='procbits.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Procbits &#187; GWT</title>
		<link>http://procbits.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://procbits.com/osd.xml" title="Procbits" />
	<atom:link rel='hub' href='http://procbits.com/?pushpress=hub'/>
		<item>
		<title>Traversal of GWT Tree</title>
		<link>http://procbits.com/2010/08/09/traversal-of-gwt-tree/</link>
		<comments>http://procbits.com/2010/08/09/traversal-of-gwt-tree/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 15:14:19 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[GWT]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=54</guid>
		<description><![CDATA[Recently, an app I&#8217;m working on needed two trees. I just so happened that I needed to deep copy part of one of the trees to the other tree. The copied tree will then have checkboxes as nodes instead of just Strings. So how would you do this? The first solution that came to mine [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&amp;blog=6893023&amp;post=54&amp;subd=procbits&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, an app I&#8217;m working on needed two trees. I just so happened that I needed to deep copy part of one of the trees to the other tree. The copied tree will then have checkboxes as nodes instead of just Strings.</p>
<p>So how would you do this? The first solution that came to mine is depth-first traversal of the tree. This method could be adapted to any tree structure in just about any language.</p>
<p>We need a callback function for everytime we &#8216;hit&#8217; a tree node. Let&#8217;s create an interface called &#8216;Action&#8217;:<br />
Action.java:<br />
<pre class="brush: java;">
public interface Action&lt;T&gt; {
	public void execute(T object);
}
</pre></p>
<p>Now, we need to create a file called TreeItemUtil.java. This will include the algorithm. The algorithm is really straightforward, it just uses a stack for each node and iterates the stack. If a node has children, they are pushed onto the stack.<br />
TreeItemUtil.java:<br />
<pre class="brush: java;">
public class TreeItemUtil {
	
	public static Collection&lt;TreeItem&gt; fetchChildren(TreeItem ti){
		List&lt;TreeItem&gt; children = new ArrayList&lt;TreeItem&gt;();
		
		for (int i = 0; i &lt; ti.getChildCount(); ++i)
			children.add(ti.getChild(i));
		return children;
	}
	
	public static void traverseDfs(TreeItem root, Action&lt;TreeItem&gt; onNode){
		if (onNode == null)
			throw new IllegalArgumentException(&quot;Must pass an action.&quot;);
		
		Stack&lt;TreeItem&gt; children = new Stack&lt;TreeItem&gt;();
		children.add(root);
		
		while (!children.isEmpty()){
			TreeItem current = children.pop();
			onNode.execute(current);
			List&lt;TreeItem&gt; currentChildren = fetchChildren(current);
			children.addAll(currentChildren);
		}
	}
}
</pre></p>
<p>Here is our TreeItemUtilTest.java that uses Mockito:<br />
<pre class="brush: java;">
public void testTraverseDfs(){
		final StringBuffer sb = new StringBuffer();
		
		TreeItem a = createMockedTI(&quot;a&quot;);
		TreeItem b = createMockedTI(&quot;b&quot;);
		TreeItem c = createMockedTI(&quot;c&quot;);
		TreeItem d = createMockedTI(&quot;d&quot;);
		TreeItem e = createMockedTI(&quot;e&quot;);
		TreeItem f = createMockedTI(&quot;f&quot;);
		TreeItem g = createMockedTI(&quot;g&quot;);
		TreeItem h = createMockedTI(&quot;h&quot;);
		TreeItem i = createMockedTI(&quot;i&quot;);
		TreeItem j = createMockedTI(&quot;j&quot;);
		TreeItem k = createMockedTI(&quot;k&quot;);
		TreeItem l = createMockedTI(&quot;l&quot;);
		TreeItem m = createMockedTI(&quot;m&quot;);
		TreeItem n = createMockedTI(&quot;n&quot;);
		TreeItem o = createMockedTI(&quot;o&quot;);
		TreeItem p = createMockedTI(&quot;p&quot;);
		
		addMockedChildren(a, b, o);
		addMockedChildren(b, c, i);
		addMockedChildren(o, n, p);
		addMockedChildren(c, d);
		addMockedChildren(i, j, m);
		addMockedChildren(d, e);
		addMockedChildren(j, k, l);
		addMockedChildren(e, f, g);
		addMockedChildren(g, h);
		
		TreeItemUtil.traverseDfs(a, new Action&lt;TreeItem&gt;(){
			public void execute(TreeItem current){
				sb.append(current.getText());
			}
		});
		
		//I was assuming it would be leftmost child first, if so, it would be in alphabetical order
		//but it starts with rightmost node first.
		assertEquals(&quot;aopnbimjlkcdeghf&quot;, sb.toString());
	}
	
	private TreeItem createMockedTI(String text){
		TreeItem ti = mock(TreeItem.class);
		when(ti.getText()).thenReturn(text);
		return ti;
	}
	
	private void addMockedChildren(TreeItem parent, TreeItem... children){
		for (int x = 0; x &lt; children.length; ++x){
			when(parent.getChild(x)).thenReturn(children[x]);
			when(children[x].getParentItem()).thenReturn(parent);
		}
		
		when(parent.getChildCount()).thenReturn(children.length);
	}
</pre></p>
<p>Now for the actual algorithm that clones the TreeItem into a TreeItem with checkboxes:<br />
<pre class="brush: java;">
private TreeItem cloneTreeItemsWithCheckboxes(TreeItem root){
		final HashMap&lt;TreeItem, TreeItem&gt; oldNew = new HashMap&lt;TreeItem, TreeItem&gt;();
		
		TreeItemUtil.traverseDfs(root, new Action&lt;TreeItem&gt;(){
			public void execute(TreeItem current){
				TreeItem newTi = new TreeItem(new CheckBox(current.getText()));
				newTi.setUserObject(current.getUserObject());
				oldNew.put(current, newTi);
				
				if (current.getParentItem() != null &amp;&amp; oldNew.containsKey(current.getParentItem())){
					TreeItem newParent = oldNew.get(current.getParentItem());
					newParent.addItem(newTi);
				}
			}
		});
		
		return oldNew.get(root);
	}
</pre></p>
<p>Are you a <a href="http://gitpilot.com" target="_blank">Git</a> user? Let me help you make project management with Git simple. Checkout <a href="http://gitpilot.com" target="_blank">Gitpilot</a>.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a></p>
<p>-JP</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&amp;blog=6893023&amp;post=54&amp;subd=procbits&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2010/08/09/traversal-of-gwt-tree/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding a Close Event When the User Closes the Browser in GWT</title>
		<link>http://procbits.com/2010/08/06/adding-a-close-event-when-the-user-closes-the-browser-in-gwt/</link>
		<comments>http://procbits.com/2010/08/06/adding-a-close-event-when-the-user-closes-the-browser-in-gwt/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 23:23:28 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[GWT]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=45</guid>
		<description><![CDATA[Often times your GWT app will queue up client side RPC events. What if the user clicks close on the browser window or tries to navigate to a new page? The solution is very simple. Simple as pie. If event.setMessage is called, a prompt will be displayed for you. Are you a Git user? Let [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&amp;blog=6893023&amp;post=45&amp;subd=procbits&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Often times your GWT app will queue up client side RPC events. What if the user clicks close on the browser window or tries to navigate to a new page? The solution is very simple.</p>
<p><pre class="brush: java;">
Window.addWindowClosingHandler(new ClosingHandler(){
	@Override public void onWindowClosing(ClosingEvent event) {
		event.setMessage(&quot;If you leave, you may lose data. Continue?&quot;);
	}
});
</pre></p>
<p>Simple as pie. If event.setMessage is called, a prompt will be displayed for you.</p>
<p>Are you a <a href="http://gitpilot.com" target="_blank">Git</a> user? Let me help you make project management with Git simple. Checkout <a href="http://gitpilot.com" target="_blank">Gitpilot</a>.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a></p>
<p>-JP</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&amp;blog=6893023&amp;post=45&amp;subd=procbits&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2010/08/06/adding-a-close-event-when-the-user-closes-the-browser-in-gwt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Read File Contents (Blobs) in GWT and Gears</title>
		<link>http://procbits.com/2009/07/29/read-file-contents-blobs-in-gwt-and-gears/</link>
		<comments>http://procbits.com/2009/07/29/read-file-contents-blobs-in-gwt-and-gears/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 21:24:01 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[GWT]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=19</guid>
		<description><![CDATA[What do you do if you want to read the contents of a file in your GWT application? One possible solution is to use Google Gears. Gears offers a method to allow a user to select file(s). You can then use the returned &#8220;getFiles&#8221; method of the &#8220;OpenFilesEvent&#8221; object to find out what file(s) the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&amp;blog=6893023&amp;post=19&amp;subd=procbits&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What do you do if you want to read the contents of a file in your GWT application? One possible solution is to use Google Gears.</p>
<p>Gears offers a method to allow a user to select file(s). You can then use the returned &#8220;getFiles&#8221; method of the &#8220;OpenFilesEvent&#8221; object to find out what file(s) the user selected. Each &#8220;File&#8221; class has a method called &#8220;getBlob&#8221; that returns a &#8220;Blob&#8221; object. However, each blob object doesn&#8217;t provide any direct way to access the blobs contents. Many <a href="http://www.arcaner.com/2008/09/14/google-gears-blob-hate/">people</a> <a href="http://groups.google.com/group/gears-users/browse_thread/thread/0b4d3933a1ef0e91?fwc=1">haven&#8217;t</a> found a way to read the blob contents.</p>
<p>I wrote a class to access the contents of the blob. The class is called &#8220;BlobReader&#8221; and is very simple to use. The trick lies in the fact that in Javascript we can access the &#8220;getBytes&#8221; method of the blob.</p>
<p>Here is an example reading line by line of a text file:</p>
<pre>try {
	Desktop dt = Factory.getInstance().createDesktop();
	dt.openFiles(new OpenFilesHandler(){
		public void onOpenFiles(OpenFilesEvent event) {
			File[] files = event.getFiles();
			File file = files[0];
			Blob data = file.getBlob();

			BlobReader br = new BlobReader(data);
			while (!br.endOfBlob())
				Window.alert(br.readLine());
			}
		}, true);
} catch (Exception ex){
	Window.alert(ex.toString());
}</pre>
<p>Here is BlobReader.java:</p>
<pre>package com.yourpackage.client;
import com.google.gwt.core.client.JsArrayInteger;
import com.google.gwt.gears.client.blob.Blob;

public class BlobReader {

	private final int CHUNK_SIZE = 1024;

	public BlobReader(Blob blob){
		this.blob = blob;
	}

	private Blob blob;
	public Blob getBlob(){ return this.blob; }

	private int blobPointer = 0;
	public int getBlobPointer(){ return this.blobPointer; }

	public boolean endOfBlob(){
		return (blobPointer &gt;= blob.getLength());
	}

	public byte[] readAllBytes(){
		return getBlobBytes(this.blob);
	}

	public String readAllText(){
		int size = this.blob.getLength();
		StringBuilder sb = new StringBuilder(size);

		JsArrayInteger bytes = getBlobBytes(this.blob, 0, size);
		for (int x = 0; x &lt; size; x++)
			sb.append((char)bytes.get(x));

		return sb.toString();
	}

	public String readLine(){
		int size = CHUNK_SIZE;
		boolean foundEndOfLine = false;

		JsArrayInteger bytes;
		StringBuilder sb = new StringBuilder(CHUNK_SIZE);

		while (!foundEndOfLine &amp;&amp; size &gt; 0){
			int dif = this.blob.getLength() - this.blobPointer; //remainder of the data
			if (dif &lt; CHUNK_SIZE)
				size = dif;
			bytes = getBlobBytes(this.blob, this.blobPointer, size);

			for (int x = 0; x &lt; size; x++){
				blobPointer++;
				int b = bytes.get(x);
				if (b == 10){ //newline
					foundEndOfLine = true;
					break;
				}
				sb.append((char)b);

				if (this.getBlobPointer() % 1000000 == 0)
					Window.alert("" + this.getBlobPointer());
			}
		}

		if (sb.substring(sb.length() - 1) == "\r")
			sb.deleteCharAt(sb.length() - 1);

		return sb.toString();
	}

       public void reset(){
		this.blobPointer = 0;
	}

	private byte[] getBlobBytes(Blob b){
		JsArrayInteger array = getBlobBytes(b, 0, b.getLength());
		byte[] bytes = new byte[array.length()];
		for (int x = 0; x &lt; bytes.length; x++)
			bytes[x] = (byte) array.get(x); //in google docs we are told these are from 0-255

		return bytes;
	}

	private native JsArrayInteger getBlobBytes(Blob b, int offset, int length) /*-{
		return b.getBytes(offset, length);
	}-*/;
}</pre>
<p>Notes:<br />
1) This hasn&#8217;t been thoroughly tested/debugged.<br />
2) At the time of this writing, I&#8217;m using Gears 0.5 with the GWT-Gears 1.2.1. In the current GWT-Gears svn trunk you can see upcoming support for the GWT methods &#8220;getNativeBytes&#8221; and &#8220;getBytes&#8221;</p>
<p>Are you a <a href="http://gitpilot.com" target="_blank">Git</a> user? Let me help you make project management with Git simple. Checkout <a href="http://gitpilot.com" target="_blank">Gitpilot</a>.</p>
<p>Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&amp;blog=6893023&amp;post=19&amp;subd=procbits&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2009/07/29/read-file-contents-blobs-in-gwt-and-gears/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
	</channel>
</rss>
