gwt in-depth

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

Upload: nemo

Post on 02-Feb-2016

33 views

Category:

Documents


1 download

DESCRIPTION

GWT In-depth. Explained by Rohit Ghatol ( [email protected] ) http://pune-gtug.blogspot.com. Topics Covered. Short Introduction to GWT Architectural Overview Simple Code Example MVC Code Example Server Communication using GWT – RPC Open source Libraries. Introduction to GWT. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: GWT In-depth

GWT In-depth

Explained by Rohit Ghatol([email protected])

http://pune-gtug.blogspot.com

Page 2: GWT In-depth

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 In-depth

Introduction to GWT

Read more on GWT Overview Page

Page 4: GWT In-depth

Key Features

Read more on GWT Overview Page

Page 5: GWT In-depth

Key Features

Read more on GWT Overview Page

Page 6: GWT In-depth
Page 7: GWT In-depth
Page 8: GWT In-depth
Page 9: GWT In-depth
Page 12: GWT In-depth

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

Demo GWT Project

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

Page 24: GWT In-depth

GWT Project Anatomy

SimpleDemo.gwt.xml

1. Module XML Definition File 2. Includes Project Dependencies3. Includes Entry Point4. 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 2. Includes JS file for SimpleDemo3. Includes PlaceHolder Elements

Page 25: GWT In-depth

Shuffle Box Example

Page 26: GWT In-depth

Shuffle Box ExampleRootPanel

Page 27: GWT In-depth

Shuffle Box Example

HorizontalPanel

Page 28: GWT In-depth

Shuffle Box Example

VerticalPanel

Page 29: GWT In-depth

Shuffle Box Example

Page 30: GWT In-depth

Shuffle Box Example

Page 31: GWT In-depth

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 In-depth

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 In-depth

MVC Demo

Page 34: GWT In-depth

MVC Demo

Page 35: GWT In-depth

MVC Demo

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

TempModel

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

Observerable

FahrView CelsView ThermoView

update(Observable mode)

Observer

Page 36: GWT In-depth

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 In-depth

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 In-depth

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 In-depth

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 41: GWT In-depth

RPC Demo

Page 42: GWT In-depth

RPC Demo

Page 43: GWT In-depth

RPC Demo

Page 44: GWT In-depth

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: GWT In-depth

RPC Classes Explained

AsyncCallback<<Interface>>

void onSuccess(Object result);

void onFailure(Throwable caught);

Page 46: GWT In-depth

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 In-depth

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 In-depth

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 50: GWT In-depth

Extending Widgets for more events

onBrowserEvent(Event event)

Widget

FocusWidget

ListBox

Page 51: GWT In-depth

Extending Widgets for more events

• Some Facts about ListBox widget– Following Listeners can be added• Change• Focus• Keyboard

– Extends FocusWidget which extends Widget– ListBox does not receive a DoubleClick event as it

never sinks it.

Page 52: GWT In-depth

Extending Widgets for more events

• Some Facts about Widget class– A Widget needs to sink events in its constructor to

receive it– Widget class has an important method• void onBrowserEvent(Event event) which handles all

the events (eventually)

Page 53: GWT In-depth

Extending Widgets for more events

• Some Facts about Widget class– If a Widget needs to receive a DoubleClick event it

needs to call sinkEvents(Event.ONDBLCLICK)

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

Page 54: GWT In-depth

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 In-depth

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 In-depth

Extending Widgets for more eventsSource files• AdvListBox.java • AdvListBoxDemo.java • DoubleClickListener.java • DoubleClickListenerCollection.java

Page 57: GWT In-depth

Open Source GWT Libraries Demos

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

Page 58: GWT In-depth

MyGWT

Page 59: GWT In-depth

GWT-DND