|
Page 2 of 6
src: This folder
contains all the generated source and configuration files for the applications,
contained in the familiar Java package structure, with the root package being
com.packtpub.gwtbook.hellogwt. This package name was deduced by
applicationCreator
from the fully qualified class name that we provided as a parameter to it. The
generated files under this directory are:
compacktpubgwtbookhellogwtHelloGWT.gwt.xml: This is the
project module--an XML file that holds the entire configuration needed by a GWT
project. The
inherits tag specifies modules inherited by this module. In this
simple case, we are inheriting only the functionality provided by the
User
module, which is built into the GWT. On more complex projects, module
inheritance provides a nice way to reuse pieces of functionality. The
EntryPoint
refers to the class that will be instantiated by the GWT framework when the
module is loaded. This is the class name provided to the
applicationCreator
command, when we created the project. The following code can be found in this
file:
compacktpubgwtbookhellogwtclientHelloGWT.java: This is the
entry point for our application. It extends the
EntryPoint class, and when the
HelloGWT
module is loaded by the GWT framework, this class is instantiated and its
onModuleLoad()
method is automatically called. In this generated class, the
onModuleLoad()
method creates a button and a label, and then adds them to the page. It
also adds a click listener for the button. We will be modifying the code in
HellowGWT.java
to create a new application later in this chapter. The current
code in this file is as follows:
package com.packtpub.gwtbook.hellogwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
/** Entry point classes define onModuleLoad(). */
public class HelloGWT implements EntryPoint
{
/** This is the entry point method. */
public void onModuleLoad()
{
final Button button = new Button("Click me");
final Label label = new Label();
button.addClickListener(new ClickListener()
{
public void onClick(Widget sender)
{
if (label.getText().equals(""))
label.setText("Hello World!");
else
label.setText("");
}
}
//Assume that the host HTML has elements defined whose
//IDs are "slot1", "slot2". In a real app, you probably
//would not want to hard-code IDs. Instead, you could,
//for example, search for all elements with a
//particular CSS class and replace them with widgets.
RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label);
}
compacktpubgwtbookhellogwtpublicHelloGWT.html:
This is a generated HTML page that loads the
HelloGWT application and is
referred to as the host
page, as this is the web page that hosts the
HelloGWT
application. Even though this HTML file is deceptively simple, there are some
points that you need to be aware of:
o
Firstly, it contains a meta tag that points to the
HelloGWT
module directory. This tag is the connection between the HTML page and the
HelloGWT
application. The following code represents this connection:
content='com.packtpub.gwtbook.hellogwt.HelloGWT'>
o
Secondly, the script tag imports code from the gwt.js
file. This file contains the code (shown below) required to bootstrap the GWT
framework. It uses the configuration in the
HelloGWT.gwt.xml file, and then
dynamically loads the JavaScript created by compiling the
HelloGWT.java
file to present the application. The
gwt.js file does not exist when we
generate the skeleton project. It is generated by the GWT framework when we run
the application in hosted mode or when we compile the application.
HelloGWT-compile.cmd: This file
contains a command script for compiling the application into HTML and
JavaScript.
HelloGWT-shell.cmd:
This file contains a command script for running the application in the hosted
mode.
There is a well-defined
relationship between these generated files. The
HelloGWT.html file is the host page
that loads the
gwt.js file.
There's More!
The
applicationCreator provides options
to control several parameters for a
new application. You can see these options by executing it from the following
command line:
applicationCreator.cmd -help

PAGE 2 OF 6
|