Personal tools

Netbeans:NetbeansGWTLib

From Adapt

Revision as of 23:10, 11 September 2008 by Scsong (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Creating a GWT Library

In the GWT class, there is a GWT.log(String, Throwable) method but no GWT.log(String) method. Time to create a library of useful classes that other GWT applications can share!

Create the library

Create a new library project in NetBeans and add the GWT to the libraries under the project properties. In this example, the project will be called =gwt-util-5=.

Create the utility class:

package edu.umiacs.gwt.util;

import com.google.gwt.core.client.GWT;

public class GWTUtil
{
    public static void log(String message)
    {
        GWT.log(message, null);
    }
}

Create a ModuleName.gwt.xml and place it in the top-most source directory that does not contain code. In this example, the module will be called GWTUtil and it will be placed in the edu.umiacs.gwt directory. The file should contain:

<?xml version="1.0" encoding="UTF-8"?>
<module>
   <inherits name="com.google.gwt.user.User"/>
   <source path="util"/>
</module>

The source element specifies which directories contain translatable code and is relative to the location of the xml file. If not specified, it defaults to client.

Include the library

In the hello world application, use the brand new method! Change:

GWT.log("onModuleLoad()", null);

to:

edu.umiacs.gwt.util.GWTUtil.log("onModuleLoad")

In the project properties of the hello-world project, add the gwt-util project to the libraries.

Include an inherit element in the module xml file of the hello-world project. The module xml file should now be:

<module>
   <inherits name="com.google.gwt.user.User"/>  
   <inherits name="edu.umiacs.gwt.GWTUtil"/>        
   <entry-point class="edu.umiacs.gwt.hello.client.HelloWorldApp"/>      
   <servlet class="edu.umiacs.gwt.hello.server.HelloWorldServiceImpl" path="/hello-service"/>        
</module>

The name is the java-ish path to the xml file, minus the .gwt.xml.

Now include the gwt-util source and class directories to the run and -compile-to-javascript targets in the hello-world build.xml file:

<target name="run" depends="init,compile">
   <java classpath=
"${run.classpath}:${basedir}/build/classes:${basedir}/src:${project.gwt-util-5}/build/classes:${project.gwt-util-5}/src" 
         classname="com.google.gwt.dev.GWTShell" 
         fork="true">
      <arg value="-out"/>
      <arg path="${build.www.dir}/"/>          
      <arg value="edu.umiacs.gwt.hello.HelloWorld/HelloWorld.html"/>
    </java>
</target>
    
<target name="-compile-to-javascript">
   <echo message="Compiling Java to JavaScript"/>
   <java classpath="${run.classpath}:${basedir}/src:${project.gwt-util-5}/src" 
         classname="com.google.gwt.dev.GWTCompiler" 
         fork="true">
      <arg value="-out"/>
      <arg path="${build.dir}/www/"/>
      <arg value="${application.args}"/>
      <arg value="edu.umiacs.gwt.hello.HelloWorld"/>
   </java>
</target>

For more information: GWT Tutorial 5