GreetingResponsePresenter.java
01.
package
co.uk.hivedevelopment.greet.client.mvp;
02.
import
net.customware.gwt.presenter.client.EventBus;
03.
import
net.customware.gwt.presenter.client.place.Place;
04.
import
net.customware.gwt.presenter.client.place.PlaceRequest;
05.
import
net.customware.gwt.presenter.client.widget.WidgetDisplay;
06.
import
net.customware.gwt.presenter.client.widget.WidgetPresenter;
07.
import
co.uk.hivedevelopment.greet.shared.event.GreetingSentEvent;
08.
import
co.uk.hivedevelopment.greet.shared.event.GreetingSentEventHandler;
09.
import
com.allen_sauer.gwt.log.client.Log;
10.
import
com.google.gwt.event.dom.client.ClickEvent;
11.
import
com.google.gwt.event.dom.client.ClickHandler;
12.
import
com.google.gwt.event.dom.client.HasClickHandlers;
13.
import
com.google.gwt.user.client.ui.DialogBox;
14.
import
com.google.gwt.user.client.ui.HasHTML;
15.
import
com.google.gwt.user.client.ui.HasText;
16.
import
com.google.inject.Inject;
17.
public
class
GreetingResponsePresenter
extends
WidgetPresenter {
18.
public
interface
Display
extends
WidgetDisplay {
19.
HasText getTextToServer();
20.
HasHTML getServerResponse();
21.
HasClickHandlers getClose();
22.
DialogBox getDialogBox();
23.
}
24.
public
static
final
Place PLACE =
new
Place(
"GreetingResponse"
);
25.
@Inject
26.
public
GreetingResponsePresenter(
final
Display display,
final
EventBus eventBus) {
27.
super
(display, eventBus);
28.
bind();
29.
}
30.
31.
@Override
32.
protected
void
onBind() {
33.
// Add a handler to close the DialogBox
34.
display.getClose().addClickHandler(
new
ClickHandler() {
35.
public
void
onClick(
final
ClickEvent event) {
36.
display.getDialogBox().hide();
37.
// Not sure of a nice place to put these!
38.
// sendButton.setEnabled(true);
39.
// sendButton.setFocus(true);
40.
}
41.
});
42.
eventBus.addHandler(GreetingSentEvent.TYPE,
new
GreetingSentEventHandler() {
43.
@Override
44.
public
void
onGreetingSent(
final
GreetingSentEvent event) {
45.
Log.info(
"Handling GreetingSent event"
);
46.
47.
display.getTextToServer().setText(event.getName());
48.
display.getServerResponse().setHTML(event.getMessage());
49.
display.getDialogBox().show();
50.
}
51.
});
52.
}
53.
@Override
54.
protected
void
onUnbind() {
55.
// Add unbind functionality here for more complex presenters.
56.
}
57.
public
void
refreshDisplay() {
58.
// This is called when the presenter should pull the latest data
59.
// from the server, etc. In this case, there is nothing to do.
60.
}
61.
public
void
revealDisplay() {
62.
// Nothing to do. This is more useful in UI which may be buried
63.
// in a tab bar, tree, etc.
64.
}
65.
/**
66.
* Returning a place will allow this presenter to automatically trigger when
67.
* '#GreetingResponse' is passed into the browser URL.
68.
*/
69.
@Override
70.
public
Place getPlace() {
71.
return
PLACE;
72.
}
73.
@Override
74.
protected
void
onPlaceRequest(
final
PlaceRequest request) {
75.
// this is a popup
76.
}
77.
}
At this point, we're still missing some code but hopefully you should start to see the structure coming together.
Events
Since this is a simple application, we only have one event - the GreetingSent event which has a corresponding handler:
GreetingSent.java
01.
package
co.uk.hivedevelopment.greet.shared.event;
02.
import
com.google.gwt.event.shared.GwtEvent;
03.
public
class
GreetingSentEvent
extends
GwtEvent{
04.
public
static
Type TYPE =
new
Type();
05.
06.
private
final
String name;
07.
private
final
String message;
08.
09.
public
GreetingSentEvent(
final
String name,
final
String message) {
10.
this
.name = name;
11.
this
.message = message;
12.
}
13.
14.
public
String getName() {
15.
return
name;
16.
}
17.
18.
public
String getMessage() {
19.
return
message;
20.
}
21.
22.
@Override
23.
public
Type getAssociatedType() {
24.
return
TYPE;
25.
}
26.
@Override
27.
protected
void
dispatch(
final
GreetingSentEventHandler handler) {
28.
handler.onGreetingSent(
this
);
29.
}
30.
}
GreetingSentHandler.java
1.
package
co.uk.hivedevelopment.greet.shared.event;
2.
import
com.google.gwt.event.shared.EventHandler;
3.
public
interface
GreetingSentEventHandler
extends
EventHandler {
4.
void
onGreetingSent(GreetingSentEvent event);
5.
}
If you now look at the project references for the event and handler you can see where the events are fired and subsequently handled. The components are blissfully unaware of what produced the event and it just has the information that it needs to get the job done. Now imagine if you want to have another component that also reacts to this event, say to update another part of the GUI. Simple, just register another event handler - no spaghetti code.
RPC
Let's define client RPC. As mentioned earlier, we'll not be making the RPC calls directly. Instead we'll use the command pattern and let the gwt-dispatch library handle the underlying server calls. Although this is an implementation of the command pattern, it turns out that there is already a core GWT class called Command, so the authors of the gwt-dispatch library have opted to use Action instead - so "actions" are really "commands".
In our example, we'll define the Action SendGreeting which represent our server request and a SendGreetingResult class to encapsulate the server response:
SendGreeting.java
01.
package
co.uk.hivedevelopment.greet.shared.rpc;
02.
import
net.customware.gwt.dispatch.shared.Action;
03.
public
class
SendGreeting
implements
Action {
04.
private
static
final
long
serialVersionUID = 5804421607858017477L;
05.
private
String name;
06.
@SuppressWarnings
(
"unused"
)
07.
private
SendGreeting() {
08.
}
09.
public
SendGreeting(
final
String name) {
10.
this
.name = name;
11.
}
12.
public
String getName() {
13.
return
name;
14.
}
15.
}
SendGreetingResult.java
01.
package
co.uk.hivedevelopment.greet.shared.rpc;
02.
import
net.customware.gwt.dispatch.shared.Result;
03.
public
class
SendGreetingResult
implements
Result {
04.
private
static
final
long
serialVersionUID = 7917449246674223581L;
05.
private
String name;
06.
private
String message;
07.
public
SendGreetingResult(
final
String name,
final
String message) {
08.
this
.name = name;
09.
this
.message = message;
10.
}
11.
@SuppressWarnings
(
"unused"
)
12.
private
SendGreetingResult() {
13.
}
14.
public
String getName() {
15.
return
name;
16.
}
17.
public
String getMessage() {
18.
return
message;
19.
}
20.
}