<?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>/home/nick &#187; Programming</title>
	<atom:link href="http://whatwouldnickdo.com/wordpress/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://whatwouldnickdo.com/wordpress</link>
	<description>Discussions on Programming, Linux, and whatever else.</description>
	<lastBuildDate>Thu, 20 Aug 2009 16:20:54 +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>iBatis &#8211; Mapping Database NULL</title>
		<link>http://whatwouldnickdo.com/wordpress/491/ibatis-mapping-database-null/</link>
		<comments>http://whatwouldnickdo.com/wordpress/491/ibatis-mapping-database-null/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 00:49:14 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[iBatis]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=491</guid>
		<description><![CDATA[iBatis is a framework used to map a SQL database to POJOs (Plain Old Java Objects).&#160; iBatis is one framework of many out there, others include Hibernate.&#160; Recently I&#8217;ve been tasked with a new webapp which will connect to a very old legacy database.&#160; One of the first things I decided to do was look [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ibatis.apache.org/">iBatis</a> is a framework used to map a SQL database to <a href="http://en.wikipedia.org/wiki/POJO">POJOs</a> (Plain Old Java Objects).&nbsp; iBatis is one framework of many out there, others include <a href="http://www.hibernate.org/">Hibernate</a>.&nbsp; Recently I&#8217;ve been tasked with a new webapp which will connect to a very old legacy database.&nbsp; One of the first things I decided to do was look through the database and figure out what data I needed, and then design and create java objects to hold said data.&nbsp; I chose iBatis for this particular project because I was dealing with a legacy database which I had little control over, and iBatis can give me the flexibility I need in case I need to write complex and specific SQL to construct my objects.&nbsp; <br />
<span id="more-491"></span><br />
<strong><span style="font-size: medium;">The issue:</span></strong><br />
One thing I noticed about the database was that there are NULLs littered everywhere, and using a default value for columns was barely used at all.&nbsp; So there are plenty of columns with a data type of <strong>int</strong>, <strong>float</strong> and <strong>bit</strong> (I&#8217;m using a MS SQL Server database) that have NULLs in there.&nbsp; Now, if I was recoding other applications that updated and inserted into these places, I would have checked data and inserted a default value, as well as maybe altering the column to allow no NULLs and set a default value, but I can&#8217;t do that.&nbsp; These tables I&#8217;m using will get mapped to a POJO, and of course I want the ints and booleans in the database mapped to ints and booleans in my POJO.&nbsp; But what happens if one of those database ints or booleans has a NULL?&nbsp; Well, java will try to set the int or boolean to a null (which can&#8217;t be done) and it throws an exception instead&#8230;an <strong>IllegalArgumentException</strong> to be exact.</p>
<p><strong><span style="font-size: medium;">The quick fix:</span></strong><br />
Going through the iBatis docs can sometimes be tedious.&nbsp; Fortunately, the solution is simple.&nbsp; If you open your xml file dealing with the object in question, and go to your <strong>resultMap</strong> element for the class, you&#8217;ll typically have a <strong>result</strong> element for each column you&#8217;re dealing with.&nbsp; Add an attribute called <strong>nullValue</strong> with a value to give the object in question in case a NULL is present.&nbsp; It&#8217;s that simple.&nbsp; </p>
<p><strong><span style="font-size: medium;">Here&#8217;s an example&#8230;</span></strong><br />
You have a table called Employee with several columns and create a POJO like below to hold that data:</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> Employee <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> login<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> password<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> authorityType<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> passwordExpired<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// ... constructors and setter/getter methods here...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Let&#8217;s say that authorityType and passwordExpired were NULL values in the database.&nbsp; Adding the nullValue attribute to the xml file like so would solve your issue:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resultMap</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;EmployeeResult&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;Employee&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;id&quot;</span>              <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;id&quot;</span>                               <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;login&quot;</span>           <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;login&quot;</span>                            <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;password&quot;</span>        <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;password&quot;</span>                         <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;authorityType&quot;</span>   <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;authorityType&quot;</span>   <span style="color: #000066;">nullValue</span>=<span style="color: #ff0000;">&quot;1&quot;</span>    <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;passwordExpired&quot;</span> <span style="color: #000066;">column</span>=<span style="color: #ff0000;">&quot;passwordExpired&quot;</span> <span style="color: #000066;">nullValue</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resultMap<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/491/ibatis-mapping-database-null/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Performance: Grid vs FlexTable</title>
		<link>http://whatwouldnickdo.com/wordpress/401/performance-grid-vs-flextable/</link>
		<comments>http://whatwouldnickdo.com/wordpress/401/performance-grid-vs-flextable/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 23:22:00 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=401</guid>
		<description><![CDATA[I&#8217;ve been having some problems in GWT with a table pulling 100+ rows at a reasonable speed using the FlexTable, but before making massive changes by switching to a Grid I ran some tests comparing the two.&#160; I originally found this issue when testing on IE for the first time after having no problems with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been having some problems in <a href="http://code.google.com/webtoolkit/">GWT</a> with a table pulling 100+ rows at a reasonable speed using the FlexTable, but before making massive changes by switching to a Grid I ran some tests comparing the two.&nbsp; I originally found this issue when testing on <a href="http://en.wikipedia.org/wiki/Internet_Explorer">IE</a> for the first time after having no problems with <a href="http://www.mozilla.com/en-US/firefox/">FF</a>.&nbsp; <br />
<span id="more-401"></span></p>
<p>Here&#8217;s the basic code used:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">long</span> startTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> names.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    NameData nd <span style="color: #339933;">=</span> names<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    grid.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, nd.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    grid.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">1</span>, <span style="color: #003399;">Double</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>nd.<span style="color: #006633;">getD1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    grid.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Double</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>nd.<span style="color: #006633;">getD2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    grid.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">3</span>, <span style="color: #003399;">Integer</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>nd.<span style="color: #006633;">getRank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
gridLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Grid: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;ms&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
startTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> names.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    NameData nd <span style="color: #339933;">=</span> names<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    flex.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">0</span>, nd.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    flex.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">1</span>, <span style="color: #003399;">Double</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>nd.<span style="color: #006633;">getD1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    flex.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">2</span>, <span style="color: #003399;">Double</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>nd.<span style="color: #006633;">getD2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    flex.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>i, <span style="color: #cc66cc;">3</span>, <span style="color: #003399;">Integer</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>nd.<span style="color: #006633;">getRank</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
flexLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FlexTable: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;ms&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m pulling from an array of 100 which holds some data that&#8217;s used to populate the cells.&nbsp; The creation of the tables is done before hand, I&#8217;m not sure if this gives an unfair advantage to the Grid, but that&#8217;s the way I&#8217;m doing it.</p>
<p>Try the <a title="test app yourself here" href="http://whatwouldnickdo.com/gridvflex/GridVFlex.html" id="ojdg">test app yourself here</a> .<br />
<span><br />
</span><span><b>Results</b><br />
What I&#8217;m finding is that the Grid does beat the FlexTable, but not by much with a small data set.&nbsp; Larger sets seem to widen the gap.&nbsp; My guess for this is that since FlexTable has the ability to add rows (Grid is sized from the start) that it works much like how other resizable constructs work such as Java&#8217;s ArrayList. My reasoning for this is that the first time the test is run the speed is very slow on the FlexTable (especially in IE).&nbsp; Repeated test runs after the initial are faster and I think this is because the table was being sized the first time as rows were added, and didn&#8217;t need to be resized during the following tests. </p>
<p>The other thing I&#8217;m finding is that performace varies based on browser.&nbsp; I&#8217;m seeing FF run reasonably well.&nbsp; With IE the Grid runs about the same as FF but the FlexTable runs considerably slower.&nbsp; I also tested Opera and was stunned as it blew both others out of the water.&nbsp; </p>
<p>Here&#8217;s some of the tests run with two different machines (one linux and one Windows):</p>
<p>
</span><b><span>MS Windows Machine </span></b><br />
<span></p>
<div>
<table class="zeroBorder" id="m.j4" width="400" border="1" bordercolor="#444444" cellpadding="3" cellspacing="0" height="125">
<tbody>
<tr>
<td width="20%">
</td>
<td width="20%" bgcolor="#d0e0e3">FF
</td>
<td width="20%" bgcolor="#d0e0e3">FF
</td>
<td width="20%" bgcolor="#d0e0e3">IE
</td>
<td width="20%" bgcolor="#d0e0e3">IE
</td>
</tr>
<tr align="right">
<td width="20%">
</td>
<td width="20%" bgcolor="#d0e0e3">Grid
</td>
<td width="20%" bgcolor="#d0e0e3">FlexTable
</td>
<td width="20%" bgcolor="#d0e0e3">Grid
</td>
<td width="20%" bgcolor="#d0e0e3">FlexTable
</td>
</tr>
<tr>
<td width="20%" bgcolor="#d0e0e3">Run 1
</td>
<td width="20%" align="right">147ms
</td>
<td width="20%" align="right">198ms
</td>
<td width="20%" align="right">156ms
</td>
<td width="20%" align="right">1297ms
</td>
</tr>
<tr>
<td width="20%" bgcolor="#d0e0e3">Run 2
</td>
<td width="20%" align="right">131ms
</td>
<td width="20%" align="right">158ms
</td>
<td width="20%" align="right">156ms
</td>
<td width="20%" align="right">203ms
</td>
</tr>
<tr>
<td width="20%" bgcolor="#d0e0e3">Run 3
</td>
<td width="20%" align="right">150ms
</td>
<td width="20%" align="right">176ms
</td>
<td width="20%" align="right">156ms
</td>
<td width="20%" align="right">172ms
</td>
</tr>
</tbody>
</table>
</div>
<p>
</span><b>Linux Machine</b><br />
<span></span><span></span></p>
<div>
<table class="zeroBorder" id="o4n2" width="400" border="1" bordercolor="#444444" cellpadding="3" cellspacing="0" height="125">
<tbody>
<tr>
<td width="20%">
</td>
<td width="20%" bgcolor="#d0e0e3">FF
</td>
<td width="20%" bgcolor="#d0e0e3">FF
</td>
<td width="20%" bgcolor="#d0e0e3">Opera
</td>
<td width="20%" bgcolor="#d0e0e3">Opera
</td>
</tr>
<tr>
<td width="20%">
</td>
<td width="20%" bgcolor="#d0e0e3">Grid
</td>
<td width="20%" bgcolor="#d0e0e3">FlexTable
</td>
<td width="20%" bgcolor="#d0e0e3">Grid
</td>
<td width="20%" bgcolor="#d0e0e3">FlexTable
</td>
</tr>
<tr>
<td width="20%" bgcolor="#d0e0e3">Run 1
</td>
<td width="20%" align="right">89ms
</td>
<td width="20%" align="right">244ms
</td>
<td width="20%" align="right">37ms
</td>
<td width="20%" align="right">118ms
</td>
</tr>
<tr>
<td width="20%" bgcolor="#d0e0e3">Run 2
</td>
<td width="20%" align="right">85ms
</td>
<td width="20%" align="right">115ms
</td>
<td width="20%" align="right">31ms
</td>
<td width="20%" align="right">41ms
</td>
</tr>
<tr>
<td width="20%" bgcolor="#d0e0e3">Run 3
</td>
<td width="20%" align="right">86ms
</td>
<td width="20%" align="right">115ms
</td>
<td width="20%" align="right">28ms
</td>
<td width="20%" align="right">41ms
</td>
</tr>
</tbody>
</table>
</div>
<p><span></span></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/401/performance-grid-vs-flextable/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>GWT Right-click Context Menu</title>
		<link>http://whatwouldnickdo.com/wordpress/370/gwt-right-click-context-menu/</link>
		<comments>http://whatwouldnickdo.com/wordpress/370/gwt-right-click-context-menu/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 01:12:27 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web-dev]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=370</guid>
		<description><![CDATA[The Google Web Toolkit has been out for a while now, and yet there is still basic functionality that is missing from the toolkit.  Don&#8217;t get me started on the lack of draggable/resizable columns for the FlexTable, because that&#8217;s a rant and a half.  Given that GWT&#8217;s event handling model isn&#8217;t bad, you&#8217;d think they&#8217;d [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://code.google.com/webtoolkit/">Google Web Toolkit</a> has been out for a while now, and yet there is still basic functionality that is missing from the toolkit.  Don&#8217;t get me started on the lack of draggable/resizable columns for the FlexTable, because that&#8217;s a rant and a half.  Given that GWT&#8217;s event handling model isn&#8217;t bad, you&#8217;d think they&#8217;d have included from the get-go the ability to handle right-clicks and bringing up a <a href="http://en.wikipedia.org/wiki/Context_menu">context menu or popup menu</a>.  Well, even with 1.6 on the doorstep it seems they forgot again or just don&#8217;t care.   Now some people will spout out &#8220;web apps don&#8217;t need or shouldn&#8217;t have right-clicks handled or context menus overridden&#8221;&#8230;&#8230;and for those I say STFU!  Web apps are used for more than just banking, news, forums and dare I say blogs.  The browser is becoming the new medium for running applications and just because an application is running in a browser doesn&#8217;t mean we should limit functionality.  That&#8217;s about as narrow minded as saying that we&#8217;ve only had one mouse button for this long, why add a second one?  Duh!</p>
<p>Anyway, enough with the blabbing.  I&#8217;ve put together a simple example to add a right click context menu and override the default browser context menu using GWT. <br />
<span id="more-370"></span><br />
In the box below you can try it out, right-click in there and you can demo it.</p>
<table>
<tr>
<td><iframe src="http://whatwouldnickdo.com/rightclickex/RightClickEx.html" width="300" frameborder="0" height="310">  &amp;amp;lt;p&amp;amp;gt;Your browser does not support iframes.&amp;amp;lt;/p&amp;amp;gt; </iframe></td>
<td>
</td>
</tr>
</table>
<p>In case the iframe doesn&#8217;t show up in your browser you can see and try the example <a href="http://whatwouldnickdo.com/rightclickex/RightClickEx.html">here</a>.<br />
Now here&#8217;s how it&#8217;s done.</p>
<p>I used a DeckPanel to switch between several panels. The popup context menu is used to choose.  To allow the DeckPanel to catch the right-click event (and you can also listen for double-clicks and several other things) I extended the DeckPanel.   For simplicity I added &#8220;Adv&#8221; (Advanced) in front of the several classes I&#8217;ve extended so this one will be <strong>AdvDeckPanel</strong>.  The first thing to do in the constructor is add <strong>sinkEvents()</strong>. Then we&#8217;ll override <strong>onBrowserEvent()</strong> in the class.  Here&#8217;s the 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> AdvDeckPanel<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;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  sinkEvents<span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span>.<span style="color: #006633;">ONMOUSEUP</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">ONDBLCLICK</span> <span style="color: #339933;">|</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">ONCONTEXTMENU</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onBrowserEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;onBrowserEvent&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  event.<span style="color: #006633;">cancelBubble</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//This will stop the event from being propagated</span>
  event.<span style="color: #006633;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>DOM.<span style="color: #006633;">eventGetType</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">ONMOUSEUP</span><span style="color: #339933;">:</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>DOM.<span style="color: #006633;">eventGetButton</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">BUTTON_LEFT</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Event.BUTTON_LEFT&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        listener.<span style="color: #006633;">onClick</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>DOM.<span style="color: #006633;">eventGetButton</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">BUTTON_RIGHT</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Event.BUTTON_RIGHT&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        listener.<span style="color: #006633;">onRightClick</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">ONDBLCLICK</span><span style="color: #339933;">:</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #003399;">Event</span>.<span style="color: #006633;">ONCONTEXTMENU</span><span style="color: #339933;">:</span>
      GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Event.ONCONTEXTMENU&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Do nothing</span>
  <span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//end switch</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice the two lines in onBrowserEvent():<br />
<strong> event.cancelBubble(true);<br />
event.preventDefault();</strong></p>
<p>These are the two lines that tell the browser not to show it&#8217;s default context popup menu.  Also note that overriding the default context menu doesn&#8217;t work in all browsers, I&#8217;m not sure if this is a bug in GWT.  If you&#8217;re using firefox then you&#8217;ll have no problems, with IE you may need to add the following to your html:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;body oncontextmenu=&quot;return false;&quot;&gt;</pre></div></div>

<p>Other than that there&#8217;s just a switch statement that checks the event type, in this case we&#8217;re interested with <strong>ONMOUSEUP</strong>, and we&#8217;ll call the listener&#8217;s <strong>onClick() </strong>or <strong>onRightClick()</strong> based on the Event&#8217;s fields.</p>
<p>AdvDeckPanel also has a reference to AdvClickListener which looks like:</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> AdvClickListener <span style="color: #000000; font-weight: bold;">extends</span> ClickListener <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span>Widget sender, <span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">void</span> onRightClick<span style="color: #009900;">&#40;</span>Widget sender, <span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This reference is basically the same as ClickListener, but has a separate method to handle the right-click.  I also pass the <strong>Event</strong> object so I can get the x and y from the click so the context menu shows up at that location instead of the top-left of the screen.</p>
<p>AdvDeckPanel implements <strong>AdvClickNotifier</strong> which does the same thing as GWT&#8217;s <strong>SourcesClickEvents</strong> interface, but handles the <strong>AdvClickListener</strong> instead.</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> AdvClickNotifier <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addClickListener<span style="color: #009900;">&#40;</span>AdvClickListener listener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeClickListener<span style="color: #009900;">&#40;</span>AdvClickListener listener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So, once you have your widget (in this case the AdvDeckPanel) and the listeners set up to handle the right-click, then we add in the code to build the popup menu and commands that go with it.  I put all of this in the <strong>EntryPoint</strong>.  I have three widgets (panels) and a <strong>Command</strong> for each like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> AdvDeckPanel deckPanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AdvDeckPanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">private</span> PopupPanel popupPanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PopupPanel<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> VerticalPanel defaultPanel <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>
<span style="color: #000000; font-weight: bold;">private</span> SimplePanel imagePanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimplePanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> SimplePanel sponserPanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimplePanel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Command showAlertCommand <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Command<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> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    deckPanel.<span style="color: #006633;">showWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    popupPanel.<span style="color: #006633;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Window</span>.<span style="color: #006633;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hope this example helps.&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><span style="color: #339933;">;</span>
&nbsp;
Command showImageCommand <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Command<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> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    deckPanel.<span style="color: #006633;">showWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    popupPanel.<span style="color: #006633;">hide</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: #339933;">;</span>
&nbsp;
Command showSponserCommand <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Command<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> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    deckPanel.<span style="color: #006633;">showWidget</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    popupPanel.<span style="color: #006633;">hide</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: #339933;">;</span></pre></div></div>

<p>A <strong>Command</strong> is called when the <strong>MenuItem</strong> is clicked.</p>
<p>Now the code to build the menu, link the commands, and handle the right-click:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> createPopupMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">MenuBar</span> popupMenuBar <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">MenuBar</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">MenuItem</span> alertItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">MenuItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Show alert&quot;</span>, <span style="color: #000066; font-weight: bold;">true</span>, showAlertCommand<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">MenuItem</span> imageItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">MenuItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Show Oliver &quot;</span>, <span style="color: #000066; font-weight: bold;">true</span>, showImageCommand<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">MenuItem</span> sponserItem <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">MenuItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Show sponser &quot;</span>, <span style="color: #000066; font-weight: bold;">true</span>, showSponserCommand<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  popupPanel.<span style="color: #006633;">setStyleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;popup&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  alertItem.<span style="color: #006633;">addStyleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;popup-item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  imageItem.<span style="color: #006633;">addStyleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;popup-item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  sponserItem.<span style="color: #006633;">addStyleName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;popup-item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  popupMenuBar.<span style="color: #006633;">addItem</span><span style="color: #009900;">&#40;</span>alertItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  popupMenuBar.<span style="color: #006633;">addItem</span><span style="color: #009900;">&#40;</span>imageItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  popupMenuBar.<span style="color: #006633;">addItem</span><span style="color: #009900;">&#40;</span>sponserItem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  popupMenuBar.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  popupPanel.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>popupMenuBar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onRightClick<span style="color: #009900;">&#40;</span>Widget sender, <span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> DOM.<span style="color: #006633;">eventGetClientX</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> DOM.<span style="color: #006633;">eventGetClientY</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  popupPanel.<span style="color: #006633;">setPopupPosition</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  popupPanel.<span style="color: #006633;">show</span><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>Lastly, to make the menu actually look like a popup menu I modified the CSS like so:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.popup</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">gray</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">gray</span> <span style="color: #993333;">gray</span> <span style="color: #993333;">gray</span> <span style="color: #993333;">gray</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #933;">3px</span> <span style="color: #933;">3px</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">solid</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.popup-item</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">80</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>What this does is makes it so the border isn&#8217;t the thick default GWT blue, and uses a thin border with the right and bottom borders a bit thicker.  This gives the popup that shadowed look.</p>
<p>A couple of books worth checking out are: <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FGWT-Action-Easy-Google-Toolkit%2Fdp%2F1933988231%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1250784594%26sr%3D8-2&#038;tag=homenick-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">GWT in Action</a><img src="http://www.assoc-amazon.com/e/ir?t=homenick-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />and <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FGWT-Practice-Robert-Cooper%2Fdp%2F1933988290%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1250784594%26sr%3D8-1&#038;tag=homenick-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">GWT in Practice</a><img src="http://www.assoc-amazon.com/e/ir?t=homenick-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /><br />
<a href="http://whatwouldnickdo.com/wordpress/blog-attachments/right-click-gwt.7z">Here&#8217;s the source</a> and more links.</p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/370/gwt-right-click-context-menu/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>GWT Yellow Fade Technique (YFT)</title>
		<link>http://whatwouldnickdo.com/wordpress/306/gwt-yellow-fade-technique-yft/</link>
		<comments>http://whatwouldnickdo.com/wordpress/306/gwt-yellow-fade-technique-yft/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 21:43:21 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web-dev]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=306</guid>
		<description><![CDATA[This is a quick example of a fading technique which notifies that a change or update in your GWT app has happened.&#160; Seems like every ajax site out there uses a technique similar to this to notify the user that something has happened.&#160; As I write this post, Google Docs flashes up a yellow &#8220;Saving&#8230;&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick example of a fading technique which notifies that a change or update in your <a href="http://code.google.com/webtoolkit/">GWT</a> app has happened.<span id="more-306"></span>&nbsp; Seems like every ajax site out there uses a technique similar to this to notify the user that something has happened.&nbsp; As I write this post, Google Docs flashes up a yellow &#8220;Saving&#8230;&#8221; box for a couple of seconds to let me know my document was automatically saved.&nbsp; This &#8220;Saving&#8230;&#8221; flash box functionality is actually something I was about to put into an application, so I did some searches to see what kind of information was out there.&nbsp; I found nothing on doing it in GWT, actually all you&#8217;ll see out there is a bunch of finger pointing and chest bashing about who came up with the technique first (who cares?).&nbsp; I put together a really fast example (which you can see and try below) and a handler that you can plug into your own app to accomplish the same thing.&nbsp;</p>
<p>
<iframe src="http://whatwouldnickdo.com/YFT/YellowFadeTech.html" width="300" frameborder="0" height="270">  &amp;amp;lt;p&amp;amp;gt;Your browser does not support iframes.&amp;amp;lt;/p&amp;amp;gt; </iframe><br />
In case the iframe doesn&#8217;t show up in your browser you can see and try the example <a href="http://whatwouldnickdo.com/YFT/YellowFadeTech.html">here</a>.<br />
<br />
First, let me explain how it&#8217;s all done.&nbsp; Using a list of colors, in this case different shades of yellow, you add and remove styles (a color) of a widget.&nbsp; The colors are stored in the CSS file.&nbsp; The change is triggered by a timer.&nbsp; The <b>YellowFadeHandler</b> class has an inner class called <b>FadeObject</b> which holds a color and a <b>com.google.gwt.user.client.ui.UIObject</b>. This UIObject is the super class of all major GWT widgets and has the methods:</p>
<pre><b>addStyleName</b>(java.lang.String&nbsp;style)
<span style="font-family: Verdana;">and</span>
<b>removeStyleName</b>(java.lang.String&nbsp;style)
</pre>
<p><strong>The FadeObject class</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> FadeObject <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> UIObject uiObj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> color <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> FadeObject<span style="color: #009900;">&#40;</span>UIObject uiObj<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;">uiObj</span> <span style="color: #339933;">=</span> uiObj<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">uiObj</span>.<span style="color: #006633;">addStyleName</span><span style="color: #009900;">&#40;</span>yftStyles<span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> nextColor<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;">if</span> <span style="color: #009900;">&#40;</span>color <span style="color: #339933;">==</span> transparent_color<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		uiObj.<span style="color: #006633;">removeStyleName</span><span style="color: #009900;">&#40;</span>yftStyles<span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		color<span style="color: #339933;">++;</span>
		uiObj.<span style="color: #006633;">addStyleName</span><span style="color: #009900;">&#40;</span>yftStyles<span style="color: #009900;">&#91;</span>color<span style="color: #009900;">&#93;</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;">int</span> getColor<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> color<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;">boolean</span> isTransparent<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;">if</span> <span style="color: #009900;">&#40;</span>color <span style="color: #339933;">==</span> transparent_color<span style="color: #009900;">&#41;</span> <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: #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>
<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end class FadeObject</span></pre></div></div>

<p>
<strong>The YellowFadeHandler class</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>FadeObject<span style="color: #339933;">&gt;</span> fadeObjectList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>FadeObject<span style="color: #339933;">&gt;</span><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> YellowFadeHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Timer</span> t <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timer</span><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> run<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;">if</span> <span style="color: #009900;">&#40;</span>fadeObjectList.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Iterator<span style="color: #339933;">&lt;</span>FadeObject<span style="color: #339933;">&gt;</span> iter <span style="color: #339933;">=</span> fadeObjectList.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> iter.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          FadeObject fObj <span style="color: #339933;">=</span> iter.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          fObj.<span style="color: #006633;">nextColor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fObj.<span style="color: #006633;">isTransparent</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>
            <span style="color: #666666; font-style: italic;">//if it hits the transparent color then fading is done so remove it.</span>
            iter.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fade object removed&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">continue</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;">&#125;</span><span style="color: #339933;">;</span>
    t.<span style="color: #006633;">scheduleRepeating</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> add<span style="color: #009900;">&#40;</span>UIObject obj<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>obj <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    FadeObject fo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FadeObject<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fadeObjectList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, fo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fade object added&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>My YellowFadeHandler class holds a List of these FadeObjects.&nbsp; When the timer fires, it iterates through the list and changes (removes the old and adds the new) styles of the widgets.&nbsp; When the style is changed to the last color in the chain (transparent), the widget is then removed from the list.&nbsp; So to have a widget go through the yellow fade, all that needs to be done is call handler.add(widget);<br />
Super simple.  <a href="http://whatwouldnickdo.com/wordpress/blog-attachments/yft-test.7z">Here&#8217;s the source.</a><br />
A couple of books worth checking out are: <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FGWT-Action-Easy-Google-Toolkit%2Fdp%2F1933988231%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1250784594%26sr%3D8-2&#038;tag=homenick-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">GWT in Action</a><img src="http://www.assoc-amazon.com/e/ir?t=homenick-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />and <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FGWT-Practice-Robert-Cooper%2Fdp%2F1933988290%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1250784594%26sr%3D8-1&#038;tag=homenick-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">GWT in Practice</a><img src="http://www.assoc-amazon.com/e/ir?t=homenick-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/306/gwt-yellow-fade-technique-yft/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Eclipse C/C++ and Linux Libraries</title>
		<link>http://whatwouldnickdo.com/wordpress/328/eclipse-cdt-and-linux-libraries/</link>
		<comments>http://whatwouldnickdo.com/wordpress/328/eclipse-cdt-and-linux-libraries/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 15:50:39 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=328</guid>
		<description><![CDATA[This is a really quick tutorial on configuring Eclipse CDT (C/C++ Development Tools) so you can work with shared libraries.  A lot of this may be basic for some readers, but for a programmer that&#8217;s used to Visual Studio and a Windows dll environment it may not.  Windows uses DLL (Dynamic link library) with the [...]]]></description>
			<content:encoded><![CDATA[<p>This is a really quick tutorial on configuring <a href="http://www.eclipse.org/cdt/">Eclipse CDT</a> (C/C++ Development Tools) so you can work with <a href="http://en.wikipedia.org/wiki/Shared_library#Shared_libraries">shared libraries</a>.  A lot of this may be basic for some readers, but for a programmer that&#8217;s used to Visual Studio and a Windows dll environment it may not.  <span id="more-328"></span>Windows uses DLL (Dynamic link library) with the .dll extension.  The closest linux equivalent is the shared library which uses a .so extension.   I&#8217;m going to use <a href="http://en.wikipedia.org/wiki/Libavcodec">libavcodec.so</a> as the shared library to be used in my example.  This library is part of the <a href="http://ffmpeg.org/">ffmpeg</a> project.</p>
<p>Assuming you already have Eclipse CDT, the first thing you need to do is create a new project.  After starting Eclipse, click on <strong>File-&gt;New-&gt;C Project</strong> and you&#8217;ll get a dialog box to choose the type of project (executable, static library, shared library, etc).  For this example, I&#8217;m choosing <strong>Executable </strong>and <strong>Hello World ANSI C Project</strong>.  Put in a name and click <strong>Finish</strong>.</p>
<p><a href="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/01-lec-new.png"><img class="size-medium wp-image-331 alignnone" title="01-lec-new" src="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/01-lec-new-300x264.png" alt="01-lec-new" width="300" height="264" /></a></p>
<p>Next you&#8217;ll see you have a new project in the left-side panel with a .c file containing a simple hello world program.</p>
<p><a href="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/02-lec-project.png"><img class="alignnone size-full wp-image-333" title="02-lec-project" src="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/02-lec-project.png" alt="02-lec-project" width="268" height="186" /></a></p>
<p>Now in my example I&#8217;ll add the line <strong>#include &lt;avcodec.h&gt;</strong> to set up my include.  If you need to add an include directory (where your header files are located) then in the Eclipse menu go to <strong>Project-&gt;Properties</strong> then open the <strong>C/C++ Build</strong> branch and click on <strong>Settings</strong>.  On the right side click on the <strong>Directories </strong>branch of <strong>GCC C Compiler</strong>.  You can then add directories to your include path.</p>
<p><a href="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/03-lec-include.png"><img class="alignnone size-medium wp-image-336" title="03-lec-include" src="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/03-lec-include-300x277.png" alt="03-lec-include" width="300" height="277" /></a></p>
<p>In this same dialog you can click on <strong>Libraries </strong>in the <strong>GCC C Linker</strong> branch to add any libraries to the project for linking.  The library I&#8217;m adding is the <strong>libavcodec.so</strong> shared library.  To add this (notice in the screenshot) I only need to add <strong>avcodec</strong>, without the prefix <strong>lib</strong> and <strong>.so</strong> extension.  This might be new to Windows programmers.  Since you&#8217;re adding a library it&#8217;s assumed to have the lib and .so in the name.</p>
<p><a href="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/04-lec-libs.png"><img class="alignnone size-medium wp-image-337" title="04-lec-libs" src="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/04-lec-libs-300x207.png" alt="04-lec-libs" width="300" height="207" /></a></p>
<p><iframe src="http://rcm.amazon.com/e/cm?t=homenick-20&#038;o=1&#038;p=13&#038;l=st1&#038;mode=books&#038;search=programming%20linux&#038;fc1=B85B5A&#038;lt1=_blank&#038;lc1=3366FF&#038;bg1=FAFAFA&#038;f=ifr" marginwidth="0" marginheight="0" width="468" height="60" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe></p>
<p>Now let&#8217;s just add a couple of lines to the main() so we can test a compile with the libavcodec library.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;avcodec.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   avcodec_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   avcodec_register_all<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then to run it, in the menu toolbar click on the run arrow and choose <strong>Run As-&gt;C/C++ Local Application</strong>.<br />
<a href="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/05-lec-run.png"><img class="alignnone size-medium wp-image-338" title="05-lec-run" src="http://whatwouldnickdo.com/wordpress/wp-content/uploads/2009/02/05-lec-run-300x108.png" alt="05-lec-run" width="300" height="108" /></a><br />
Getting no errors means success.</p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/328/eclipse-cdt-and-linux-libraries/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Inserting javascript into GWT using JSNI</title>
		<link>http://whatwouldnickdo.com/wordpress/294/inserting-javascript-into-gwt-using-jsni/</link>
		<comments>http://whatwouldnickdo.com/wordpress/294/inserting-javascript-into-gwt-using-jsni/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 01:01:44 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web-dev]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=294</guid>
		<description><![CDATA[Sure the whole point of using GWT is so you don&#8217;t have to mess with that abortion known as javascript, but sometimes you may need (or actually want) to use javascript for something in your GWT application.  For this we use JavaScript Native Interface (JSNI).  JSNI uses the native keyword, so if you&#8217;ve used JNI [...]]]></description>
			<content:encoded><![CDATA[<p>Sure the whole point of using <a id="bocv" title="GWT" href="http://code.google.com/webtoolkit/">GWT</a> is so you don&#8217;t have to mess with that abortion known as javascript, but sometimes you may need (or actually want) to use javascript for something in your GWT application.  For this we use <a id="qwkz" title="JavaScript Native Interface (JSNI)" href="http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&amp;s=google-web-toolkit-doc-1-5&amp;t=DevGuideJavaScriptNativeInterface">JavaScript Native Interface (JSNI)</a>.  JSNI uses the <strong>native </strong>keyword, so if you&#8217;ve used <a id="j9bn" title="JNI" href="http://en.wikipedia.org/wiki/Java_Native_Interface">JNI</a> (Java Native Interface) in the past you&#8217;ll have little to get used to.</p>
<p>I needed to use JSNI recently.  Since GWT doesn&#8217;t provide a way to bring the browser to the front or top like javascript does with it&#8217;s <strong>self.focus();</strong> I was able to use JSNI to call that line of javascript.  Here&#8217;s a quick example:</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;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #000066; font-weight: bold;">void</span> bringToFront<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
  self.focus();
}-*/</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then all you need to do is call the bringToFront() method like you normally would.  Along with noticing in the <strong>native </strong>keyword, you&#8217;ll immediate see that the block looks commented out.  JSNI has two tokens to mark the beginning and end of your javascript the <strong>/*-{</strong> marks the begin and <strong>}-*/</strong> marks the end.  Everything in between those tokens are assumed to be javascript.</p>
<p>There are more examples on <a id="w.ro" title="GWT JSNI site" href="http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&amp;s=google-web-toolkit-doc-1-5&amp;t=DevGuideJavaScriptFromJava">GWT JSNI site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/294/inserting-javascript-into-gwt-using-jsni/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple Charts in GWT with gchart</title>
		<link>http://whatwouldnickdo.com/wordpress/264/simple-charts-in-gwt-with-gchart/</link>
		<comments>http://whatwouldnickdo.com/wordpress/264/simple-charts-in-gwt-with-gchart/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 01:17:10 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=264</guid>
		<description><![CDATA[I&#8217;ve been tasked with adding some simple charts to a GWT project and after looking at several APIs I&#8217;ve settled with trying out gchart.  A couple of reasons for this choice is: gchart is 100% GWT (meaning it&#8217;s not a javascript wrapper) It runs on the client-side. Some other charting APIs actually build the chart [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been tasked with adding some simple charts to a <a id="t8w3" title="GWT" href="http://code.google.com/webtoolkit/">GWT</a> project and after looking at several APIs I&#8217;ve settled with trying out <a id="bi4y" title="gchart" href="http://code.google.com/p/gchart/">gchart</a>.  A couple of reasons for this choice is:</p>
<ul>
<li>gchart is 100% GWT (meaning it&#8217;s not a javascript wrapper)</li>
<li>It runs on the client-side. Some other charting APIs actually build the chart on the server-side as an image and return the link for the image.  This can be an advantage for some situations, so you may need to think about if you want a client-side or server-side render.</li>
<li>Open source under <span>Apache License, Version 2.0</span></li>
<li>Very simple to use.</li>
</ul>
<p>I put together a very simple example, and it shows how quickly you can build a chart.  The example is a compound interest financial calculator shown below (go ahead and try it out).  It has a few fields to enter your initial amount, annual contributions, interest rate, number of compounds per year and the number of years.  After clicking on the calculate button it will show your future amount and chart it.</p>
<p><iframe frameborder="0" src ="http://whatwouldnickdo.com/CompoundInterest/RetCalc.html" width=440px" height="390px">
<p>Your browser does not support iframes.</p>
<p> </iframe><br />
In case the iframe doesn&#8217;t show up in your browser you can see and try the charting example <a href="http://whatwouldnickdo.com/CompoundInterest/RetCalc.html">here</a>.</p>
<p>The simplicity of this charting API is the fact that the charts are GWT widgets.  Here&#8217;s the code for my compounding interest chart:</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> AmountChart <span style="color: #000000; font-weight: bold;">extends</span> GChart <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> AmountChart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		setChartSize<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">240</span>, <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setPadding<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;4px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getXAxis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setAxisLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Years&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getYAxis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setAxisLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addPoint<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> x, <span style="color: #000066; font-weight: bold;">double</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		GWT.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;point added (&quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span>y<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;)&quot;</span>, <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getCurve<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addPoint</span><span style="color: #009900;">&#40;</span>x, y<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 all I did was add it to the DecoratedTabPanel, put in the event listeners, the <a href="http://en.wikipedia.org/wiki/Compound_interest">formula (which I got from wiki)</a> and done.<br />
You can download the full <a href="http://whatwouldnickdo.com/wordpress/blog-attachments/compoundinterest.7z">source here</a>, and the entire application is client side.</p>
<p><em><strong>Updated 2008-01-28:</strong> I updated the iframe that the form is in to be more friendly with IE.  Also made a change recommended by John Gunther to remedy a problem with IE.  Compiled using gchart 2.4 and I updated the downloadable source.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/264/simple-charts-in-gwt-with-gchart/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>GWT Hosted Mode and Log4j</title>
		<link>http://whatwouldnickdo.com/wordpress/186/gwt-hosted-mode-and-log4j/</link>
		<comments>http://whatwouldnickdo.com/wordpress/186/gwt-hosted-mode-and-log4j/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 01:53:02 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[web-dev]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=186</guid>
		<description><![CDATA[This is a short tutorial on getting log4j to run in the embedded Tomcat included with GWT.  Before I go on I need to say that log4j will only work on the server-side. GWT doesn&#8217;t support logging with log4j on the client side (yet&#8230;maybe). To log on the client side you&#8217;ll still have to use [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short tutorial on getting <a href="http://logging.apache.org/log4j/">log4j</a> to run in the embedded Tomcat included with <a href="http://code.google.com/webtoolkit/">GWT</a>.   Before I go on I need to say that log4j will only work on the server-side.  GWT doesn&#8217;t support logging with log4j on the client side (yet&#8230;maybe).  To log on the client side you&#8217;ll still have to use GWT.log()</p>
<p>There are four things we&#8217;ll need to accomplish:<br />
<a href="#log4j.properties">1.  Place the log4j.properties file</a><br />
<a href="#servlet">2.  Write a servlet that will initialize the logger.</a><br />
<a href="#web.xml">3.  Modify the web.xml file.</a><br />
<a href="#scripts">4.  Modify the GWT -shell and -compile scripts.</a></p>
<h4><big><a name="log4j.properties">1.  Place the log4j.properties file</a></big></h4>
<p>Under <strong>&lt;gwt-project&gt;/tomcat/webapps/ROOT/WEB-INF/</strong> create a “<strong>classes</strong>” dir.   The “classes” dir is where you put the log4j.properties file.  Here’s the log4j.properties file I’m using:<br />
<span style="font-family: Courier New;">log4j.rootCategory=DEBUG, dest1, dest3</span></p>
<p><span style="font-family: Courier New;">! Log to the console<br />
log4j.appender.dest1=org.apache.log4j.ConsoleAppender<br />
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.dest1.layout.ConversionPattern=%-5p %d{HH:mm:ss.SSS} [%-15.15t] [%-25.25c{1}] %m%n</span></p>
<p><span style="font-family: Courier New;">! LOG TO A FILE<br />
log4j.appender.dest3=org.apache.log4j.RollingFileAppender<br />
log4j.appender.dest3.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.dest3.layout.ConversionPattern=%-5p %d{EEE MMM dd HH:mm:ss.SSS zzz yyyy} [%-15.15t] [%-25.25c{1}] %m%n</span></p>
<p><span style="font-family: Courier New;">! Specify the file name<br />
log4j.appender.dest3.File=./logs/hosted-log4j.log</span></p>
<p><span style="font-family: Courier New;">! Control the maximum log file size<br />
log4j.appender.dest3.MaxFileSize=3000KB<br />
log4j.appender.dest3.MaxBackupIndex=3<br />
</span></p>
<h4><big><a name="servlet">2.  Write a servlet that will initialize the logger.</a></big></h4>
<p>Next you&#8217;ll want to have a servlet that will load the properties file and init the logger.  This file (<span style="font-family: &quot;Courier New&quot;; color: black;"><span style="font-size: x-small;">Log4JInitServlet.java</span></span>) will go in the <strong>&lt;gwt-project&gt;/src/</strong> dir.</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.nick.example.server</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.PropertyConfigurator</span><span style="color: #339933;">;</span>
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
* Log4JInitServlet
*
* This class should get loaded first (based on the web.xml),
* so it can init the logger.
*
* @author Nick
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Log4JInitServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>Log4JInitServlet.<span style="color: #000000; font-weight: bold;">class</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> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log4JInitServlet init() starting.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> log4jfile <span style="color: #339933;">=</span> getInitParameter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;log4j-properties&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;log4jfile: &quot;</span><span style="color: #339933;">+</span>log4jfile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>log4jfile <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">String</span> propertiesFilename <span style="color: #339933;">=</span> getServletContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRealPath</span><span style="color: #009900;">&#40;</span>log4jfile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      PropertyConfigurator.<span style="color: #006633;">configure</span><span style="color: #009900;">&#40;</span>propertiesFilename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;logger configured.&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;">else</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error setting up logger.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Log4JInitServlet init() done.&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>This servlet should be loaded first, which can be accomplished by modifying the web.xml file.</p>
<h4><big><a name="web.xml">3.  Modify the web.xml file.</a></big></h4>
<p>This file is located in &lt;gwt-project&gt;/tomcat/webapps/ROOT/WEB-INF/<br />
My web.xml file looks like:</p>
<pre class="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app&gt;
  &lt;servlet&gt;
    &lt;servlet-name&gt;Log4JInitServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.nick.example.server.Log4JInitServlet&lt;/servlet-class&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;log4j-properties&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/classes/log4j.properties&lt;/param-value&gt;
     &lt;/init-param&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet&gt;
    &lt;servlet-name&gt;shell&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.google.gwt.dev.shell.GWTShellServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;shell&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
&lt;/web-app&gt;</pre>
<p>Notice the line <strong>&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;</strong>, which tells tomcat to load the Log4JInitServlet first.</p>
<h4><big><a name="scripts">4.  Modify the GWT -shell and -compile scripts.</a></big></h4>
<p>Modify your gwt –shell and –compile files to add the log4j jar into the class path.  Here’s my &lt;project&gt;-shell.cmd file.  Take note of the log4j jar that I added.</p>
<p>[Windows]<span style="font-family: Courier New;"><br />
@java -Xmx256M -cp &#8220;%~dp0\src;%~dp0\bin;C:/lib/gwt-windows-1.5.3/gwt-user.jar;C:/lib/gwt-windows-1.5.3/gwt-dev-windows.jar;C:/lib/gwt-windows-1.5.3/log4j-1.2.12.jar&#8221; com.google.gwt.dev.GWTShell -out &#8220;%~dp0\www&#8221; %* com.nick.example.HostedLog4j/HostedLog4j.html</span></p>
<p>[linux]<span style="font-family: Courier New;"><br />
#!/bin/sh<br />
APPDIR=`dirname $0`;<br />
java  -Xmx256M -cp &#8220;$APPDIR/src:$APPDIR/bin:$HOME/projects/gwt-linux-1.5.3/gwt-user.jar:$HOME/projects/gwt-linux-1.5.3/gwt-dev-linux.jar:$HOME/projects/lib/log4j-1.2.15.jar&#8221; com.google.gwt.dev.GWTShell -out &#8220;$APPDIR/www&#8221; &#8220;$@&#8221; com.nick.example.HostedLog4j/HostedLog4j.html;</span></p>
<p>Make sure to change the &lt;project&gt;-compile.cmd in the same way.<br />
You can then run your &lt;gwt-project&gt;-shell.cmd and the logger should work.   It will create a log directory in under &lt;gwt-project&gt; and place your log file inside there.</p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/186/gwt-hosted-mode-and-log4j/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java EchoServer Example</title>
		<link>http://whatwouldnickdo.com/wordpress/216/java-echoserver-example/</link>
		<comments>http://whatwouldnickdo.com/wordpress/216/java-echoserver-example/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 03:27:36 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://whatwouldnickdo.com/wordpress/?p=216</guid>
		<description><![CDATA[I&#8217;m using this post to experiment with syntax highlighting in my blog.  Recently a friend needed a program to echo data on a socket while he was testing an internet appliance and some other network enabled hardware so I figured I&#8217;d just use that code as my example. The EchoServer will open a listen socket [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using this post to experiment with syntax highlighting in my blog.  Recently a friend needed a program to echo data on a socket while he was testing an internet appliance and some other network enabled hardware so I figured I&#8217;d just use that code as my example.</p>
<p>The EchoServer will open a listen socket on port 4444.  Compile it, run it and then to test it you can open a command prompt and type <strong>telnet localhost 4444</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.ServerSocket</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.Socket</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author NickC
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EchoServer <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> BUFFER_SIZE <span style="color: #339933;">=</span> <span style="color: #cc66cc;">256</span><span style="color: #339933;">;</span> 
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> listenPort <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4444</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">ServerSocket</span> serverSock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ServerSocket</span><span style="color: #009900;">&#40;</span>listenPort<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listening on port &quot;</span><span style="color: #339933;">+</span>listenPort<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">Socket</span> sock <span style="color: #339933;">=</span> serverSock.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;new connection &quot;</span> <span style="color: #339933;">+</span> sock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #003399;">InputStream</span> sockIn <span style="color: #339933;">=</span> sock.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">OutputStream</span> sockOut <span style="color: #339933;">=</span> sock.<span style="color: #006633;">getOutputStream</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;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span>BUFFER_SIZE<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>count <span style="color: #339933;">=</span> sockIn.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><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>count <span style="color: #339933;">&gt;=</span> buffer.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               count <span style="color: #339933;">=</span> buffer.<span style="color: #006633;">length</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            count <span style="color: #339933;">=</span>  sockIn.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echoing &quot;</span><span style="color: #339933;">+</span>count<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; bytes&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            sockOut.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buffer, <span style="color: #cc66cc;">0</span>, count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            sockOut.<span style="color: #006633;">flush</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;">&#125;</span></pre></div></div>

<p>Source in a file: <a href="http://www.whatwouldnickdo.com/wordpress/blog-attachments/EchoServer.java">EchoServer.java</a></p>
]]></content:encoded>
			<wfw:commentRss>http://whatwouldnickdo.com/wordpress/216/java-echoserver-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
