GWT 1.6: MVP and testing, part 2

posted on June 29, 2009 in coding

As for completeness, I made the previous example in a MVP + eventBus example. This can be download here. Unfortunately, I didn’t build a model class since that was too much overkill.
The procedure is simple and partially explained in the previous topic:

1. Create a (model,) presenter and view class for each custom widget.
2. Presenter: Add a interface of the view…

class AddRowPresenter
{
	/*  ....  */
	interface AddRowWidgetInterface {
		HasClickHandlers getSendButton();
		HasValue<string> getTextBox();
	}
}

… and a method to ‘bind’ the widget…

class AddRowPresenter
{
	/* .... */
	public void bindWidget(AddRowWidgetInterface widget)
	{
		this.widget = widget;
		widget.getSendButton().addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				sendAddRowEvent();
			}
		});
	}
}

… and use the eventBus to send your events.

class AddRowPresenter
{
	/* .... */
	protected void sendAddRowEvent() {
		TableRowEvent event = new TableRowEvent();
		event.setRowName(widget.getTextBox().getValue());
		eventBus.fireEvent(event);
	}
}

The widget could look like this:

class AddRowWidget extends Composite implements AddRowPresenter.AddRowWidgetInterface {
	TextBox rowName = new TextBox();
	Button addRowButton = new Button("send");
 
	/**
	 * Constructor
	 * This is a composite of a textbox and a button
	 */
	public AddRowWidget()
	{
		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.add(rowName);
		mainPanel.add(addRowButton);
		initWidget(mainPanel);
	}
	public HasClickHandlers getSendButton() {
		return this.addRowButton;
	}
	public HasValue<string> getTextBox() {
		return this.rowName;
	}
}

For the code of the receiving widget, it’s easier to just look into the source. Its bindwidget function looks like this:

	public void bindWidget(SpecialTableInterface _table)
	{
		this.table = _table;
		eventBus.addHandler(TableRowEvent.TYPE, new TableRowEventHandler(){
	        public void onAddRow(TableRowEvent event) {
			// This should be a function that uses the model, omitted here since nothing is done with the data
	        	table.addSpecialRow(event.getRowName());
	       	}
	    });
	}

For more information, feel free to ask in the comments.
Next up: building the stockwatcher example (somewhat extended) with an eventBus, MVP, (the command pattern) and GIN.

3 Comments »

  1. Hello,

    I have one note regarding the following snippet:
    public void go(RootPanel rootPanel)

    I think that it’s not a good idea to couple GWT widget and the presenter class. I think that the widget class have to be attached to the root panel instead of presenter.

    The other think that could be optimized is the usage of HasWidgets instead of RootPanel. If you use HasWidgets then you could create a simple test case without the extension of GWTTestCase and etc.

    Comment by mgenov — June 30, 2009 @ 9:15 pm

  2. Thank you for a very comprehensive sample. I have been trying to understand Ray’s slides but unfortunately without luck. Could you make sure to include a command pattern RPC sample in your next post? Thanks

    Comment by johan — July 2, 2009 @ 1:40 pm

  3. Excellent works. Few comments and suggestions for the next StockWatcher.
    (1) StockWatcher has JSONP. It is better to isolate in a ‘hub’ responsible for receiving from Presenter (a) serverUrl, (b) queryAttributes, (c) sessionToken, and (4) handler from bound widget. The hub service does JSONP to serverUrl that set src handler to the bound widget handler
    (2) Presenter takes the above 4 attributes, do the model jso and required processing before sending out the event using the hub service jsonRequest(…) that use getJson(.) as in the StockWatcher.
    (3) The bound widget processes the response after receiving jso from the server.

    The server inspects the token before providing the REST service. The bound widget inspect the token before processing the received response. The command pattern is modeled at the widget action level and somehow connected to action events for a roadmap to undo/redo, etc. This can be a lightweight framework. Manual DI for clear pattern before GIN

    Comment by Duong BaTien — July 3, 2009 @ 6:42 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

-->

Blom Inrichtingen - interieurprojecten in de gezondheidszorg