GWT Indepth

71
GWT In-depth Explained by Rohit Ghatol ([email protected] ) http://pune-gtug.blogspot.com

Transcript of GWT Indepth

Page 1: GWT Indepth

GWT In-depth

Explained by Rohit Ghatol([email protected])

http://pune-gtug.blogspot.com

Page 2: GWT Indepth

Topics Covered

• Short Introduction to GWT• Architectural Overview• Simple Code Example• MVC Code Example• Server Communication using GWT – RPC• Open source Libraries

Page 3: GWT Indepth

Introduction to GWT

Read more on GWT Overview Page

Page 4: GWT Indepth

Key Features

Read more on GWT Overview Page

Page 5: GWT Indepth

Key Features

Read more on GWT Overview Page

Page 6: GWT Indepth
Page 7: GWT Indepth
Page 8: GWT Indepth
Page 9: GWT Indepth
Page 10: GWT Indepth

GWT Documentation Links

• Developer's Guideo Fundamentals o User Interfaces o Remote Procedure Calls o Unit Testing o Internationalization o JavaScript Native Interface (JSNI) o JRE Emulation o GWT Class API

Page 11: GWT Indepth

Install GWT

Page 12: GWT Indepth

E:\Work\GWT-Demo>set PATH=e:\worksoft\gwt-windows-1.4.60;%PATH%

E:\Work\GWT-Demo>projectCreator -eclipse SimpleDemoCreated directory E:\Work\GWT-Demo\srcCreated directory E:\Work\GWT-Demo\testCreated file E:\Work\GWT-Demo\.projectCreated file E:\Work\GWT-Demo\.classpath

E:\Work\GWT-Demo>applicationCreator -eclipse SimpleDemo com.gwt.demo.client.SimpleDemoCreated directory E:\Work\GWT-Demo\src\com\gwt\demoCreated directory E:\Work\GWT-Demo\src\com\gwt\demo\clientCreated directory E:\Work\GWT-Demo\src\com\gwt\demo\publicCreated file E:\Work\GWT-Demo\src\com\gwt\demo\SimpleDemo.gwt.xmlCreated file E:\Work\GWT-Demo\src\com\gwt\demo\public\SimpleDemo.htmlCreated file E:\Work\GWT-Demo\src\com\gwt\demo\client\SimpleDemo.javaCreated file E:\Work\GWT-Demo\SimpleDemo.launchCreated file E:\Work\GWT-Demo\SimpleDemo-shell.cmdCreated file E:\Work\GWT-Demo\SimpleDemo-compile.cmd

Page 13: GWT Indepth
Page 14: GWT Indepth
Page 15: GWT Indepth
Page 16: GWT Indepth
Page 17: GWT Indepth
Page 18: GWT Indepth
Page 19: GWT Indepth
Page 20: GWT Indepth
Page 21: GWT Indepth
Page 22: GWT Indepth

Demo GWT Project

• Site - http://code.google.com/p/gwt-simple-demo/• Download demo project from here

Page 23: GWT Indepth

GWT Project Anatomy

GWT-Demo+src+com/gwt/demoSimpleDemo.gwt.xml- clientSimpleDemo.java- publicSimpleDemo.html

Page 24: GWT Indepth

GWT Project Anatomy

SimpleDemo.gwt.xml

1. Module XML Definition File • Includes Project

Dependencies• Includes Entry Point• Includes RPC Servlet Entry

SimpleDemo.java

1. This is the Entry Point2. Entry Point is like Main Method3. Widgets are added to

RootPanel

SimpleDemo.html

1. This is the final deliverable HTML/JSP/ASP/PHP

• Includes JS file for SimpleDemo• Includes PlaceHolder Elements

Page 25: GWT Indepth

Shuffle Box Example

Page 26: GWT Indepth

Shuffle Box ExampleRootPanel

Page 27: GWT Indepth

Shuffle Box Example

HorizontalPanel

Page 28: GWT Indepth

Shuffle Box Example

VerticalPanel

Page 29: GWT Indepth

Shuffle Box Example

Page 30: GWT Indepth

Shuffle Box Example

Page 31: GWT Indepth

Shuffle Box Examplenew ClickListener(){ public void onClick(Widget sender) {int leftIndex=leftListBox.getSelectedIndex();if(leftIndex==-1){Window.alert("Select an Item from Left List Box");}else{String item=leftListBox.getItemText(leftIndex);leftListBox.removeItem(leftIndex);rightListBox.addItem(item);}}}

Page 32: GWT Indepth

Observable

# List observers+ addObserver(Observer observer)+ removeObserver(Observer observer)+ notifyObservers()

Observer<<Interface>>

+ update(Observable model)

Notify

Register/Unregister

View Source for Observable.java View Source for Observer.java

Page 33: GWT Indepth

MVC Demo

Page 34: GWT Indepth

MVC Demo

Page 35: GWT Indepth

MVC Demo

# temperature+ setTemp(int temp)+int getTemp()

TempModel

# observers+ addObserver()+removeObserver()+ notifyObserver()

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Page 36: GWT Indepth

MVC Demo

# temperature+ setTemp(int temp)+int getTemp()

TempModel

# observers+ addObserver()+removeObserver()+ notifyObserver()

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Renders

Page 37: GWT Indepth

MVC Demo

# temperature+ setTemp(int temp)+int getTemp()

TempModel

# observers+ addObserver()+removeObserver()+ notifyObserver()

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Register

Page 38: GWT Indepth

MVC Demo

# temperature+ setTemp(int temp)+int getTemp()

TempModel

# observers+ addObserver()+removeObserver()+ notifyObserver()

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

User clicked increment button

Changes

Page 39: GWT Indepth

MVC Demo

# temperature+ setTemp(int temp)+int getTemp()

TempModel

# observers+ addObserver()+removeObserver()+ notifyObserver()

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Notifies

All the Views Update themselves using update() method

Page 40: GWT Indepth

MVC Demo source files

• FahrView.java• CelsView.java• ThermoView.java• TempModel.java

Page 41: GWT Indepth

RPC Demo

Page 42: GWT Indepth

RPC Demo

Page 43: GWT Indepth

RPC Demo

Page 44: GWT Indepth

RPC Classes Explained

LocationService<<Interface>>

List getStates(String country)

LocationServiceImpl

List getStates(String country)

RemoteService<<Interface>>

RemoteServiceServlet

LocationServiceAsync<<Interface>>

Void getStates(String country, AsyncCallback callback)

LocationServiceUtil

public static LocationServiceAsync getInstance()

GWT

MagicGlue

GWT Classes

Page 45: GWT Indepth

RPC Classes Explained

AsyncCallback<<Interface>>

void onSuccess(Object result);

void onFailure(Throwable caught);

Page 46: GWT Indepth

LocationServiceUtil Code

• Use GWT Glue to link LocationService and LocationServiceAsync classes

LocationServiceAsync serviceAsync = (LocationServiceAsync) GWT.create(LocationService.class);

• Set the Service’s Entry point (aka url)((ServiceDefTarget) serviceAsync).setServiceEntryPoint(GWT.getModuleBaseURL()+"/LocationService");

Page 47: GWT Indepth

LocationServiceUtil Code

public class LocationServiceUtil {public static LocationServiceAsync createService() {LocationServiceAsync serviceAsync = (LocationServiceAsync) GWT.create(LocationService.class);

((ServiceDefTarget) serviceAsync).setServiceEntryPoint(GWT.getModuleBaseURL()+"/LocationService");return serviceAsync;}}

Page 48: GWT Indepth

RPC Client Code

LocationServiceAsync serviceAsync = LocationServiceUtil.createService();serviceAsync.getStates(country, new AsyncCallback() {

public void onFailure(Throwable caught) { Window.alert("System Error!"); }

public void onSuccess(Object result) { List list = (List) result; statesListBoxModel.setEntries(list);

}});

Page 49: GWT Indepth

RPC related Links

• Complete source code• LocationService.java• LocationServiceAsync.java• LocationServiceImpl.java• LocationServiceUtil.java• Client Code• RPC Tutorial

Page 50: GWT Indepth

Extending Widgets for more events

onBrowserEvent(Event event)

Widget

FocusWidget

ListBox

Page 51: GWT Indepth

Extending Widgets for more events

• Some Facts about ListBox widgeto Following Listeners can be added

Change Focus Keyboard

o Extends FocusWidget which extends Widgeto ListBox does not receive a DoubleClick event

as it never sinks it.

Page 52: GWT Indepth

Extending Widgets for more events

• Some Facts about Widget classo A Widget needs to sink events in its

constructor to receive ito Widget class has an important method

void onBrowserEvent(Event event) which handles all the events (eventually)

Page 53: GWT Indepth

Extending Widgets for more events

• Some Facts about Widget classo If a Widget needs to receive a DoubleClick

event it needs to call sinkEvents(Event.ONDBLCLICK)

o If a Widget needs to handle this new DoubleClick event it needs to override onBrowserEvent method and handle this event

Page 54: GWT Indepth

Extending Widgets for more events

# dblClickListenerCollection+ AdvListBox()# onBrowserEvent(Event event)

AdvListBox

ListBox

addDblClickListener()removeDblClickListener()fireDblClickEvent(Widget sender)

DblClickListenerCollection

ArrayList

Void onDoubleClick(Widget sender)

DblClickListener<<Interface>>

0..*

1

Page 55: GWT Indepth

Extending Widgets for more events

public class AdvListBox extends ListBox {private DoubleClickListenerCollection dblClickListeners = new DoubleClickListenerCollection();

public AdvListBox() {this.sinkEvents(Event.ONDBLCLICK);}

public void onBrowserEvent(Event event) {super.onBrowserEvent(event);if(DOM.eventGetType(event)==Event.ONDBLCLICK){dblClickListeners.fireDoubleClickEvent(this);}}

public void add(DoubleClickListener listener) {dblClickListeners.add(listener);}

public void remove(DoubleClickListener listener) {dblClickListeners.remove(listener);}}

AdvListBox.java Source

Page 56: GWT Indepth

Extending Widgets for more eventsSource files

• AdvListBox.java • AdvListBoxDemo.java • DoubleClickListener.java • DoubleClickListenerCollection.java

Page 57: GWT Indepth

J2EE Components

• Components are JSP 2.0 Tag Files

• Components are configured using attributes

• Concept of ComponentLoader at GWT

Page 58: GWT Indepth

Target Code

<widget:loader/>

<table><tr><td><widget:colorPicker id=“cp1” color=“red”/></td></tr><tr><td><widget:navTree id=“menu1=“ key=“menuTree” /></td></tr></table>

Page 59: GWT Indepth

Data Provided to Components

• Simple and Static Datao Provided using attributes

• Complex and Dynamic Datao Provided using RPCo Key is used to identify context at Server sideo Key is provided to client as attribute

Page 60: GWT Indepth

Complex and Dynamic Data

ServerBrowser

TreeRPCServlet

menu

roles

conf

menu

conf

roles

Page 61: GWT Indepth

ComponentLoader@GWT

• Is an Entry Point• Keeps a map of widget name Vs widget

factories• Hunts for DOM elements with attribute

“widget” (placeholders)• Replaces place holder with instances of

Widget

Page 62: GWT Indepth

Target Code

<widget:loader/>

<table><tr><td><widget:colorPicker id=“cp1” color=“red”/></td></tr><tr><td><widget:navTree id=“menu1” key=“menuTree” /></td></tr></table>

<script src=“com.xyz.ComponentLoader.nocachejs”/>

<div id=“cp1” widget=“ColorPicker” color=“${color}”></div>

<div widget=“Tree” id=“menu1” key=“${key}” readonly=“true”></div>

Page 63: GWT Indepth

ComponentLoader

GWT’s ComponentLoader Script

Body

..

..div id=“cp1” widget=“ColorPicker”

div id=“menu1” widget=“Tree”

ColorPicker cpFactory

Tree treeFactory

RootPanel.get(“cp1”).add(cpFactory.getWidget());

RootPanel.get(“menu1”).add(treeFactory.getWidget());

Page 64: GWT Indepth

Building Gadgets using GWT

• Download gwt-gadgets-0.0.290.zip• Add it your project path• Create your gadget by extending Gadget• Build • You will get <gadget>.xml instead of html

file• Load this on Igoogle

Refer HelloGadget sample in this zip

Page 65: GWT Indepth

Wrapping JavaScript Frameworks

• Interface with legacy or 3rd party JavaScript frameworkso E. g Google Gadgets, OpenSocial (Orkut

apps)

• Pull JavaScript library in GWT world

Page 66: GWT Indepth

Wrap using JavaScriptObject

Case Study Wrap OpenSocial apis

var dataRequest = opensocial.newDataRequest();

dataRequest.add(“myfriends”,dataRequest. newFetchPeopleRequest(“VIEWER_FRIENDS”);

dataRequest.send(function(resp){resp.get(“myfriends”).doSomething();});

Page 67: GWT Indepth

Wrap using JavaScriptObject

Replicate OpenSocial using simple JSNI

public class OpenSocial{public native DataRequest newDataRequest()/*-{return $wnd.opensocial.newDataRequest();}-*/;}

Page 68: GWT Indepth

Wrap using JavaScriptObject

Create DataRequest by extending JavaScriptObjectpublic class DataRequest extends JavaScriptObject{public final DataRequestItem newFetchPeopleRequest(Id idSpec) {return newFetchPeopleRequest(this, idSpec.toString());}private final native DataRequestItem newFetchPeopleRequest(DataRequest obj,String idSpec)/*-{return obj.newFetchPeopleRequest(idSpec);}-*/;}

Page 69: GWT Indepth

Open Source GWT Libraries Demos

• GWT-EXT – Many Rich UI widgets• GWT-DND – Meant for drag and drop

Page 70: GWT Indepth

EXT GWT

Page 71: GWT Indepth

GWT-DND