{"id":113,"date":"2006-05-21T13:28:37","date_gmt":"2006-05-21T18:28:37","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/index.php\/2006\/05\/21\/google-web-toolkit-tutorial-the-break-down\/"},"modified":"2009-08-18T13:52:41","modified_gmt":"2009-08-18T20:52:41","slug":"google-web-toolkit-tutorial-the-break-down","status":"publish","type":"post","link":"https:\/\/juixe.com\/techknow\/index.php\/2006\/05\/21\/google-web-toolkit-tutorial-the-break-down\/","title":{"rendered":"Google Web Toolkit Tutorial: The Break Down"},"content":{"rendered":"<p>There was a lot of buzz at JavaOne regarding the Google Web Toolkit (GWT).  Basically, GWT allows a developer to write a AJAX powered web application in Java!!  The Java code gets &#8216;compiled&#8217; to a set of plain old text files containing your JavaScript code.  For example, the GWT will translate your rollover actions written in Java to JavaScript functions.  This confused me for a second so I&#8217;ll stress again that you compile your web application written in Java to plain old JavaScript and HTML that can run in any browser.<\/p>\n<p>What this means is that if you are a Java developer you don&#8217;t have to learn JavaScript and all the idiosyncrasies of every browser to develop a stable and reliable AJAX web application.<\/p>\n<p>To get <a href=\"http:\/\/code.google.com\/webtoolkit\/gettingstarted.html\">started<\/a> download the GWT from Google, configure it with Eclipse, and create a new web application.<\/p>\n<p>I created and imported into Eclipse a project called WebApp.  In my Package Explorer under webapp &gt; src &gt; com.juixe.gwt I have a public directory that contains my WebApp.html.  This file was created by the applicationCreator batch file provided with the GWT.  For the purpose of this walk through I modified my WebApp.html as follows:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;HTML&gt;\r\n  &lt;HEAD&gt;\r\n    &lt;TITLE&gt;HTML for WebApp&lt;\/TITLE&gt;\r\n    &lt;!-- You need this to reference your GWT module --&gt;\r\n    &lt;META name='gwt:module' content='com.juixe.gwt.WebApp'&gt;\r\n\r\n    &lt;SCRIPT language=&quot;javascript&quot; src=&quot;gwt.js&quot;&gt;\r\n    &lt;\/SCRIPT&gt;\r\n\r\n    &lt;STYLE&gt;&lt;\/STYLE&gt;\r\n  &lt;\/HEAD&gt;\r\n\r\n  &lt;BODY&gt;\r\n    &lt;H1&gt;My WebApp&lt;\/H1&gt;\r\n    Any text or HTML goes here.\r\n    &lt;DIV id=&quot;changeOnRollOver&quot;&gt;&lt;\/DIV&gt;\r\n  &lt;\/BODY&gt;\r\n&lt;\/HTML&gt;\r\n<\/pre>\n<p>The HTML is simple to describe itself.  I have a body that can contain any HTML.  I can use CSS style sheets.  The only tag that seems out of place it he meta tag.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;meta name='gwt:module' content='com.juixe.gwt.WebApp'&gt;\r\n<\/pre>\n<p>What I would like to do at this time is write some some GWT based Java code to modify the text inside this DIV identified as changeOnRollOver.  To do this I had to modify WebApp.java file under webapp &gt; src &gt; com.juixe.gwt.client.  Here is the code for WebApp.java:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/**\r\n * Entry point classes define onModuleLoad().\r\n *\/\r\npublic class WebApp implements EntryPoint {\r\n\r\n   \/**\r\n    * This is the entry point method.\r\n    *\/\r\n   public void onModuleLoad() {\r\n\r\n        \/\/ Create Label widget\r\n      final Label roller = new Label(\\&quot;Default value\\&quot;);\r\n\r\n        \/\/ Associate HTML element to GWT widget\r\n      RootPanel.get(\\&quot;changeOnRollOver\\&quot;).add(roller);\r\n\r\n        \/\/ Add mouse lister to label widget\r\n      roller.addMouseListener(new MouseListener() {\r\n         public void onMouseEnter(Widget sender) {\r\n            roller.setText(\\&quot;Entered element...\\&quot;);\r\n         }\r\n\r\n         public void onMouseLeave(Widget sender) {\r\n            roller.setText(\\&quot;Leaving element...\\&quot;);\r\n         }\r\n\r\n         \/\/ Do nothing\r\n         public void onMouseDown(Widget sender, int x, int y) {}\r\n\r\n         \/\/ Do nothing\r\n         public void onMouseMove(Widget sender, int x, int y) {}\r\n\r\n         \/\/ Do nothing\r\n         public void onMouseUp(Widget sender, int x, int y) {}\r\n      });\r\n   }\r\n}\r\n<\/pre>\n<p>Let me break this code down.  This WebApp class needs to implement the onModuleLoad method of the EntryPoint interface.  This is called when you page loads.  I think of this method as the equivalent to the JavaScript onload event handler.  The first thing I do in the onModuleLoad is create a GWT Label widget called roller.  There are a lot of widgets such as labels, buttons, check boxes, tab panels, menu bars, trees, and a whole slough of other widgets.  After I instantiated a Label associates the roller Label I just created to my DIV identified as changeOnRollOver in my HTML file.  You can associate any GWT widget to any HTML element using code similar to the following:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nRootPanel.get(elementNameStr).add(gwtWidgetObject);\r\n<\/pre>\n<p>The last thing to go over is how to wire functions to widget events.  In the above code, I register a MouseListener to it via it&#8217;s addMouseListener method.  For this example I am not interested in all the mouse listener methods but I still need to implement them.  I am only interested in the onMouseEnter and onMouseLeave events.  For this events I will just change the text of the roller Label to indicate if I am entering the element or leaving it.<\/p>\n<p>Here is a suggestion to the GWT team:  Add a MouseAdapter to the API, please!!!<\/p>\n<p>I can run and debug this code from Eclipse or I could compile it using the WebApp-compile batch file to create the JavaScript and HTML files required to test this in my favorite browser.  The WebApp-compile batch file will create a www folder under my webapp project (you might need to refresh you project in Eclipse).<\/p>\n<p>This a somewhat trivial example but if I wanted to do the same thing in notepad using just HTML and JavaScript this is how it would look:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;HTML&gt;\r\n  &lt;HEAD&gt;\r\n    &lt;TITLE&gt;HTML for WebApp&lt;\/TITLE&gt;\r\n    &lt;STYLE&gt;&lt;\/STYLE&gt;\r\n  &lt;\/HEAD&gt;\r\n\r\n  &lt;BODY&gt;\r\n    &lt;H1&gt;My WebApp&lt;\/H1&gt;\r\n    Any text or HTML goes here.\r\n    &lt;DIV\r\n      onmouseover=\\&quot;this.innerHTML='entered'\\&quot;\r\n      onmouseout=\\&quot;this.innerHTML = 'left'\\&quot;&gt;\r\n    &amp;nbsp;\r\n    &lt;\/DIV&gt;\r\n  &lt;\/BODY&gt;\r\n&lt;\/HTML&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There was a lot of buzz at JavaOne regarding the Google Web Toolkit (GWT). Basically, GWT allows a developer to write a AJAX powered web application in Java!! The Java code gets &#8216;compiled&#8217; to a set of plain old text files containing your JavaScript code. For example, the GWT will translate your rollover actions written [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[13,14,15,16,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-1P","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/113"}],"collection":[{"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":2,"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":835,"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions\/835"}],"wp:attachment":[{"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}