`
fishyu0817
  • 浏览: 109978 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Google Web Toolkit (GWT) MVP Example (2)

阅读更多

GreetingPresenter.java

view source
<object id="highlighter_516673_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
001.package co.uk.hivedevelopment.greet.client.mvp;
002.import net.customware.gwt.dispatch.client.DispatchAsync;
003.import net.customware.gwt.presenter.client.DisplayCallback;
004.import net.customware.gwt.presenter.client.EventBus;
005.import net.customware.gwt.presenter.client.place.Place;
006.import net.customware.gwt.presenter.client.place.PlaceRequest;
007.import net.customware.gwt.presenter.client.widget.WidgetDisplay;
008.import net.customware.gwt.presenter.client.widget.WidgetPresenter;
009.import co.uk.hivedevelopment.greet.shared.event.GreetingSentEvent;
010.import co.uk.hivedevelopment.greet.shared.rpc.SendGreeting;
011.import co.uk.hivedevelopment.greet.shared.rpc.SendGreetingResult;
012.import com.allen_sauer.gwt.log.client.Log;
013.import com.google.gwt.event.dom.client.ClickEvent;
014.import com.google.gwt.event.dom.client.ClickHandler;
015.import com.google.gwt.event.dom.client.HasClickHandlers;
016.import com.google.gwt.user.client.Window;
017.import com.google.gwt.user.client.ui.HasValue;
018.import com.google.inject.Inject;
019.public class GreetingPresenter extends WidgetPresenter {
020. /**
021.  * The message displayed to the user when the server cannot be reached or
022.  * returns an error.
023.  */
024. private static final String SERVER_ERROR = "An error occurred while "
025.  + "attempting to contact the server. Please check your network "
026.  + "connection and try again.";
027.  
028. public interface Display extends WidgetDisplay {
029.  public HasValue getName();
030.  public HasClickHandlers getSend();
031. }
032. public static final Place PLACE = new Place("Greeting");
033.  
034. private final DispatchAsync dispatcher;
035. // FUDGE FACTOR! Although this is not used, having Gin pass the object
036. // to this class will force its instantiation and therefore will make the
037. // response presenter listen for events (via bind()). This is not a very good way to
038. // achieve this, but I wanted to put something together quickly - sorry!
039. private final GreetingResponsePresenter greetingResponsePresenter;
040. @Inject
041. public GreetingPresenter(final Display display,
042.     final EventBus eventBus,
043.     final DispatchAsync dispatcher,
044.     final GreetingResponsePresenter greetingResponsePresenter) {
045.  super(display, eventBus);
046.   
047.  this.dispatcher = dispatcher;
048.   
049.  this.greetingResponsePresenter = greetingResponsePresenter;
050.   
051.  bind();
052. }
053.  
054. /**
055.  * Try to send the greeting message
056.  */
057. private void doSend() {
058.  Log.info("Calling doSend");
059.   
060.  dispatcher.execute(new SendGreeting(display.getName().getValue()), new DisplayCallback(display) {
061.   @Override
062.   protected void handleFailure(final Throwable cause) {
063.    Log.error("Handle Failure:", cause);
064.     
065.    Window.alert(SERVER_ERROR);
066.   }
067.   @Override
068.   protected void handleSuccess(final SendGreetingResult result) {
069.    // take the result from the server and notify client interested components
070.    eventBus.fireEvent(new GreetingSentEvent(result.getName(), result.getMessage()));
071.   }
072.    
073.  });
074. }
075. @Override
076. protected void onBind() {
077.  // 'display' is a final global field containing the Display passed into the constructor.
078.  display.getSend().addClickHandler(new ClickHandler() {
079.   public void onClick(final ClickEvent event) {
080.    doSend();
081.   }
082.  });
083. }
084. @Override
085. protected void onUnbind() {
086.  // Add unbind functionality here for more complex presenters.
087. }
088. public void refreshDisplay() {
089.  // This is called when the presenter should pull the latest data
090.  // from the server, etc. In this case, there is nothing to do.
091. }
092. public void revealDisplay() {
093.  // Nothing to do. This is more useful in UI which may be buried
094.  // in a tab bar, tree, etc.
095. }
096. /**
097.  * Returning a place will allow this presenter to automatically trigger when
098.  * '#Greeting' is passed into the browser URL.
099.  */
100. @Override
101. public Place getPlace() {
102.  return PLACE;
103. }
104. @Override
105. protected void onPlaceRequest(final PlaceRequest request) {
106.  // Grab the 'name' from the request and put it into the 'name' field.
107.  // This allows a tag of '#Greeting;name=Foo' to populate the name
108.  // field.
109.  final String name = request.getParameter("name", null);
110.   
111.  if (name != null) {
112.   display.getName().setValue(name);
113.  }
114. }
115.}

GreetingResponseView.java

view source
<object id="highlighter_657638_clipboard" title="copy to clipboard" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="16" height="16" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash"> </object>
print?
01.package co.uk.hivedevelopment.greet.client.mvp;
02.import net.customware.gwt.presenter.client.widget.WidgetDisplay;
03.import com.google.gwt.event.dom.client.HasClickHandlers;
04.import com.google.gwt.user.client.ui.Button;
05.import com.google.gwt.user.client.ui.DialogBox;
06.import com.google.gwt.user.client.ui.HTML;
07.import com.google.gwt.user.client.ui.HasHTML;
08.import com.google.gwt.user.client.ui.HasText;
09.import com.google.gwt.user.client.ui.Label;
10.import com.google.gwt.user.client.ui.VerticalPanel;
11.import com.google.gwt.user.client.ui.Widget;
12.public class GreetingResponseView extends DialogBox implements GreetingResponsePresenter.Display {
13. private final Label textToServerLabel;
14. private final HTML serverResponseLabel;
15. private final Button closeButton;
16. public GreetingResponseView() {
17.  setText("Remote Procedure Call");
18.  setAnimationEnabled(true);
19.  closeButton = new Button("Close");
20.  // We can set the id of a widget by accessing its Element
21.  closeButton.getElement().setId("closeButton");
22.  textToServerLabel = new Label();
23.  serverResponseLabel = new HTML();
24.  final VerticalPanel dialogVPanel = new VerticalPanel();
25.  dialogVPanel.addStyleName("dialogVPanel");
26.  dialogVPanel.add(new HTML("Sending name to the server:"));
27.  dialogVPanel.add(textToServerLabel);
28.  dialogVPanel.add(new HTML("Server replies:"));
29.  dialogVPanel.add(serverResponseLabel);
30.  dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
31.  dialogVPanel.add(closeButton);
32.  setWidget(dialogVPanel);
33. }
34. public HasText getTextToServer() {
35.  return textToServerLabel;
36. }
37. public HasHTML getServerResponse() {
38.  return serverResponseLabel;
39. }
40. public HasClickHandlers getClose() {
41.  return closeButton;
42. }
43. public DialogBox getDialogBox() {
44.  return this;
45. }
46. /**
47.  * Returns this widget as the {@link WidgetDisplay#asWidget()} value.
48.  */
49. public Widget asWidget() {
50.  return this;
51. }
52. @Override
53. public void startProcessing() {
54. }
55. @Override
56. public void stopProcessing() {
57. }
58.}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics