Friday, 4 June 2010

Getting Started with SmartGWT for awesome GWT interfaces


Introduction to SmartGWT

I recently started working with SmartGWT, a framework based on GWT that provides a comprehensive widget library for your application UI as well as assistance for server-side for data management. You can take look at its pretty functionality at the SmartGWT showcase. I have prepared a short “getting started” guide on how to integrate the library with your GWT project (I assume you have already installed the GWT SDK and the Google plugin for Eclipse). Note that Smart GWT is compatible with GWT 1.5.3 , GWT 1.6.4, GWT 1.7.x and GWT 2.0.x.

Creating the GWT project

First step is to download the library from the downloads section. The version I will be using for this tutorial is 2.1 (download directly from here). Extract the ZIP file and in the new directory you will find the framework's documentation, some samples, “Hello World” examples and of course the necessary JAR files. Open the JavaDocs in a new browser tab.

Next we create a new “Web Application Project” in Eclipse. Choose the profound name “SmartGWTIntroProject” for the project and the appropriate packaging. Make sure that the “Use Google Web Toolkt” checkbox is checked (Google's App Engine is not needed, so do not select that). The wizard will look something like this:



Adding the SmartGWT library

After the project skeleton is created, browse through your filesystem to the project's location and create a new folder named “lib”. Copy the “smartgwt.jar” file from the extracted ZIP to the newly created folder and refresh the project in Eclipse, so that the new file appears there. Then configure your project's classpath to include the JAR file (Project → Properties → Java Build Path → Add JARs...). Standard stuff so far. The expanded project in Eclipse should look like this:



Then edit the module xml file (named “SmartGWTIntroProject.gwt.xml”) and add the following line after the standard “inherits” declarations:

<inherits name="com.smartgwt.SmartGwt"/>

The module xml file will be:

<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='smartgwtintroproject'>
<!-- Inherit the core Web Toolkit stuff.                        -->
<inherits name='com.google.gwt.user.User'/>

<!-- Inherit the default GWT style sheet.  You can change       -->
<!-- the theme of your GWT application by uncommenting          -->
<!-- any one of the following lines.                            -->
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->

<!-- Other module inherits                                      -->

<inherits name="com.smartgwt.SmartGwt"/>

<!-- Specify the app entry point class.                         -->
<entry-point class='com.javacodegeeks.smartgwt.client.client.SmartGWTIntroProject'/>

<!-- Specify the paths for translatable code                    -->
<source path='client'/>
<source path='shared'/>

</module>

This allows GWT to know that your application will be using the SmartGWT library.

UPDATE: The 'com.google.gwt.user.theme.standard.Standard' declaration should be removed or commented out like above, since it conflicts with certain SmartGWT styles.

After this, locate the main HTML inside the “war” folder. Edit that and add the following line before the compiled module declaration :

<script>var isomorphicDir="smartgwtintroproject/sc/";</script>

UPDATE: From version 2.2, there is no longer need to define the isomorpihcDir value. Check the Release Notes for Smart GWT 2.2. However, for this tutorial (version 2.1 used) the declaration is needed.

In the same file, scroll down and find the following lines:

<td id="nameFieldContainer"></td>
<td id="sendButtonContainer"></td>

Replace those with the following:

<td id="formContainer"></td>
<td id="buttonContainer"></td>

Those are the HTML elements that will hold the text item and the button that we will add later.

The full HTML file follows:

<!doctype html>
<!-- The DOCTYPE declaration above will set the    -->
<!-- browser's rendering engine into               -->
<!-- "Standards Mode". Replacing this declaration  -->
<!-- with a "Quirks Mode" doctype may lead to some -->
<!-- differences in layout.                        -->

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--                                                               -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!--                                                               -->
<link type="text/css" rel="stylesheet" href="SmartGWTIntroProject.css">

<!--                                           -->
<!-- Any title is fine                         -->
<!--                                           -->
<title>Web Application Starter Project</title>

<!--                                           -->
<!-- This script loads your compiled module.   -->
<!-- If you add any GWT meta tags, they must   -->
<!-- be added before this line.                -->
<!--                                           -->
<script>var isomorphicDir="smartgwtintroproject/sc/";</script>
<script type="text/javascript" language="javascript" src="smartgwtintroproject/smartgwtintroproject.nocache.js"></script>
</head>

<!--                                           -->
<!-- The body can have arbitrary html, or      -->
<!-- you can leave the body empty if you want  -->
<!-- to create a completely dynamic UI.        -->
<!--                                           -->
<body>

<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>

<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">

Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>

</noscript>

<h1>
Web Application Starter Project</h1>

<table align="center">
<tr>
<td colspan="2" style="font-weight:bold;">Please enter your name:</td>        
</tr>

<tr>
<td id="formContainer"></td>
<td id="buttonContainer"></td>
</tr>

<tr>
<td colspan="2" style="color:red;" id="errorLabelContainer"></td>
</tr>

</table>

</body>
</html>

Creating the application entrypoint

When a GWT is created via Eclipse, a lot of auto-generated files are created. One of those is the main Java file in the “client” package, which works as the application's entrypoint. So, remove the generated code and add the following:

package com.javacodegeeks.smartgwt.client.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;

public class SmartGWTIntroProject implements EntryPoint {
    
    public void onModuleLoad() {
        
        final DynamicForm form = new DynamicForm();
        final TextItem textItem = new TextItem();
        textItem.setTitle("Name");
        form.setFields(textItem);
        final IButton button = new IButton("Hello");
        
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                String name = textItem.getValue().toString();
                SC.say("Hello " + name);
            }
        });
        
        RootPanel.get("formContainer").add(form);
        RootPanel.get("buttonContainer").add(button);
        
    }

}

Make sure that the imported packages are as shown above, because SmartGWT uses classes with names same with those of the core GWT framework.

Launching the application
Next, we are ready to launch our application. Choose Run → Run As → Web Application and use your favourite browser to access the provided URL:

http://127.0.0.1:8888/SmartGWTIntroProject.html?gwt.codesvr=127.0.0.1:9997

You should be able to see the following:



That's it. Now you are ready to create some cool applications, powered by SmartGWT. You can find the Eclipse project here (some files have been removed from the project).

This was just a short guide on how to add SmartGWT to your project. In the following posts I am going to create a full application based on SmartGWT in order to show you some of its wonderful capabilities. Stay tuned.

Related Articles :



15 comments:

  1. Hi, You should remove com.google.gwt.user.theme.standard.Standard from your module as it conflicts with certain styles. Also with Smart GWT 2.2 setting isomorphicDir is no longer required.

    ReplyDelete
  2. Hi Sanjiv,

    Thanks a lot for your observations, really helpful. I have modified the post accordingly.

    Regards,
    Fabrizio

    ReplyDelete
  3. For our brand new enterprise project we just choosen extGWT because SmartGWT looked still a bit too young and lacks enterprise support, but it sure looks promising. Let's see in one year !

    ReplyDelete
  4. Um, Frederic, Enterprise support has been available for SmartGWT since day one and it's rather hard to miss:

    http://www.smartclient.com/services/index.jsp

    Furthermore, SmartGWT is based on SmartClient. SmartClient has been in production deployments running high volume financial transactions for several years before the ExtGWT project was even started.

    And SmartGWT is already running applications at the world's largest banks, life sciences companies, defense contractors, and similar institutions.

    Did you get confused about the product name? It doesn't seem like you know what SmartGWT is.

    ReplyDelete
  5. This a nice article for those new to Smart-GWT. Thanks for the time you took to write it.

    I'd be interested in a further article on your integration experiences if you have the time.

    ReplyDelete
  6. Hi,

    There are some very good smartGWT tutorials here -> http://uptick.com.au/blog and here -> http://hilloldebnath.byethost3.com/2009/08/29/smartgwt-a-getting-started-guide

    Cheers
    Mark

    ReplyDelete
  7. How does this compare to Vaadin?

    ReplyDelete
  8. I have detailed two tutorials for beginners for using maven-gwt-plugin with smartgwt enterprise version.

    http://opengambit.com/kiranscorner/?p=42
    http://opengambit.com/kiranscorner/?p=55

    ReplyDelete
  9. I am running into this error:
    com.javacodegeeks.smartgwt.client.SmartGwtIntroProjectcom.javacodegeeks.smartgwt.client.SmartGwtIntroProject
    [ERROR] Unable to find 'com/javacodegeeks/smartgwt/client/SmartGwtIntroProjectcom/javacodegeeks/smartgwt/client/SmartGwtIntroProject.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?

    Any insight is highly appreciated.
    Thanks
    Kumar

    ReplyDelete
  10. Hi Kumar,
    From the error you posted it seems that your project's structure has been messed up. Did you follow the tutorial step by step? Please note that the whole Eclise Project is provided for download. Use that to get started and then you can make your own modifications.
    Let me know how it goes.

    ReplyDelete
  11. Hi Fab,
    I imported the Eclipse project and tried. It works as explained here. I do not know what I messed up.
    Thank you!
    -Kumar

    ReplyDelete
  12. For no apparent reason the The Button text "Hello" is displayed out side the button. I am using Chrome browser.
    Do you know why?
    Thanks
    Kumar

    ReplyDelete
  13. Thanks for the great post.

    I want to manipulate in memory xml and then i want to assign it to ListGrid. How is it possible?

    ReplyDelete
  14. Checked with AppEngine-1.5.2 & gwt-2.3.0 - works fine, thanks!

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...