Passive view for Ext GWT applications

Seeing “Google Web Toolkit Architecture: Best Practices For Architecting Your GWT App” presentation by Ray Ryan coincided with the start of a new project – an Ext GWT front-end application for one of our clients. We liked the idea of testing front-end code entirely in JRE so we decided to give the model view presenter approach to building front-ends a try. Unfortunately, GXT widgets come with no fine-grained interfaces like GWT’s HasValue. Our display interfaces quickly ended up returning GXT widgets – hard to mock or even not possible to instantiate in JRE due to JSNI. We had to come up with a solution and do it fast. Technical debt had been registered on the project backlog and in a couple of days we had an opportunity to work on this. We developed several simple interfaces that let us compose display interfaces using this common vocabulary and factory class that returns implementations for standard GXT widgets. It is good to think about returned implementations as gateways that provide simple API whose implementation can be easily substituted by some test double. It’s better to look at some actual code, this should make everything more clear. The first of the interfaces is HasValue:

public interface HasValue {
    T getValue();
    void setValue(T value);
}

we have others – for selection, clicking, double clicking …

public interface HasSelected {
    public static interface Handler {
        void onSelected();
    }
    public void addHandler(Handler h);
}

Corresponding factory methods …

public static HasValue createHasValue(final Label label) {
    return new HasValue() {
        @Override
        public void setValue(String value) {
            label.setText(value);
        }

        @Override
        public String getValue() {
            return label.getText();
        }
    };
}
public static HasSelected createHasSelected(final Button button) {
    return new HasSelected() {

        @Override
        public void addHandler(final Handler handler) {
            button.addSelectionListener(new SelectionListener() {

                @Override
                public void componentSelected(ButtonEvent ce) {
                    handler.onSelected();
                }
            });
        }
    };
}

Now it is easy to build Display interfaces:

public class MyPresenter {

    public static interface Display {

        HasSelected getMyButtonSelected();

        HasValue getName();
    }

...
}

and their implementations:

public HasSelected getMyButtonSelected() {
    return DisplayMemberFactory.createHasSelected(myButton);
}

Simple.

You May Also Like

New HTTP Logger Grails plugin

I've wrote a new Grails plugin - httplogger. It logs:

  • request information (url, headers, cookies, method, body),
  • grails dispatch information (controller, action, parameters),
  • response information (elapsed time and body).

It is mostly useful for logging your REST traffic. Full HTTP web pages can be huge to log and generally waste your space. I suggest to map all of your REST controllers with the same path in UrlMappings, e.g. /rest/ and configure this plugin with this path.

Here is some simple output just to give you a taste of it.

17:16:00,331 INFO  filters.LogRawRequestInfoFilter  - 17:16:00,340 INFO  filters.LogRawRequestInfoFilter  - 17:16:00,342 INFO  filters.LogGrailsUrlsInfoFilter  - 17:16:00,731 INFO  filters.LogOutputResponseFilter  - >> #1 returned 200, took 405 ms.
17:16:00,745 INFO filters.LogOutputResponseFilter - >> #1 responded with '{count:0}'
17:18:55,799 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,799 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,800 INFO  filters.LogRawRequestInfoFilter  - 17:18:55,801 INFO  filters.LogOutputResponseFilter  - >> #2 returned 404, took 3 ms.
17:18:55,802 INFO filters.LogOutputResponseFilter - >> #2 responded with ''

Official plugin information can be found on Grails plugins website here: http://grails.org/plugins/httplogger or you can browse code on github: TouK/grails-httplogger.