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

Digging Deeper into GWT

Embed Size (px)

Citation preview

Page 1: Digging Deeper into GWT

GWT In-depth

Explained by Rohit Ghatol([email protected])

http://pune-gtug.blogspot.com

Page 2: Digging Deeper into GWT

Topics Covered

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

Page 3: Digging Deeper into GWT

Introduction to GWT

Read more on GWT Overview Page

Page 4: Digging Deeper into GWT

Key Features

Read more on GWT Overview Page

Page 5: Digging Deeper into GWT

Key Features

Read more on GWT Overview Page

Page 6: Digging Deeper into GWT
Page 7: Digging Deeper into GWT
Page 8: Digging Deeper into GWT
Page 9: Digging Deeper into GWT
Page 10: Digging Deeper into GWT

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: Digging Deeper into GWT

Install GWT

Page 12: Digging Deeper into GWT

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: Digging Deeper into GWT
Page 14: Digging Deeper into GWT
Page 15: Digging Deeper into GWT
Page 16: Digging Deeper into GWT
Page 17: Digging Deeper into GWT
Page 18: Digging Deeper into GWT
Page 19: Digging Deeper into GWT
Page 20: Digging Deeper into GWT
Page 21: Digging Deeper into GWT
Page 22: Digging Deeper into GWT

Demo GWT Project

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

Page 23: Digging Deeper into GWT

GWT Project Anatomy

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

Page 24: Digging Deeper into GWT

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: Digging Deeper into GWT

Shuffle Box Example

Page 26: Digging Deeper into GWT

Shuffle Box ExampleRootPanel

Page 27: Digging Deeper into GWT

Shuffle Box Example

HorizontalPanel

Page 28: Digging Deeper into GWT

Shuffle Box Example

VerticalPanel

Page 29: Digging Deeper into GWT

Shuffle Box Example

Page 30: Digging Deeper into GWT

Shuffle Box Example

Page 31: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

MVC Demo

Page 34: Digging Deeper into GWT

MVC Demo

Page 35: Digging Deeper into GWT

MVC Demo

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

TempModel

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

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Page 36: Digging Deeper into GWT

MVC Demo

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

TempModel

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

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Renders

Page 37: Digging Deeper into GWT

MVC Demo

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

TempModel

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

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Register

Page 38: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

MVC Demo source files

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

Page 41: Digging Deeper into GWT

RPC Demo

Page 42: Digging Deeper into GWT

RPC Demo

Page 43: Digging Deeper into GWT

RPC Demo

Page 44: Digging Deeper into GWT

RPC Classes Explained

LocationService<<Interface>>

List getStates(String country)

LocationServiceImpl

List getStates(String country)

RemoteService<<Interface>>

List getStates(String country)

RemoteServiceServlet

LocationServiceAsync<<Interface>>

Void getStates(String country, AsyncCallback callback)

LocationServiceUtil

public static LocationServiceAsync getInstance()

GWT

MagicGlue

GWT Classes

Page 45: Digging Deeper into GWT

RPC Classes Explained

AsyncCallback<<Interface>>

void onSuccess(Object result);

void onFailure(Throwable caught);

Page 46: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

RPC related Links

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

Page 50: Digging Deeper into GWT

Extending Widgets for more events

onBrowserEvent(Event event)

Widget

FocusWidget

ListBox

Page 51: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

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: Digging Deeper into GWT

Extending Widgets for more eventsSource files

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

Page 57: Digging Deeper into GWT

Open Source GWT Libraries Demos

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

Page 58: Digging Deeper into GWT

MyGWT

Page 59: Digging Deeper into GWT

GWT-DND