Can we use Navigator() class with MVP design pattern ?

Hi,
I am currently working on a Vaadin application which follows MVP design pattern. We want to use Navigator class to display absolute URLs for every window we display in our application. When we use MVP design pattern we need to pass parameters from one level to another level (i.e from one class to another class when a new class is loaded). The problem with Navigator is that I am not able to pass any parameter here. It seems like the process of loading a class is handled internally by Navigator. May be I didnt understand the concept of Navigator class properly, I am not sure…
The main purpose is to display a URL hierarchy for every window.
Is there any way to use Navigator() class with MVP design pattern or any other alternatives…?

Thanks,
Kishor

It is possible and very easy.
I usually use cdi-mvp ( but the principle remains the same not matter what MVP implementation you use,
implement both org.vaadin.addon.cdimvp.MVPView, com.vaadin.navigator.View



    // com.vaadin.navigator.View
    @Override
    public void enter(final ViewChangeListener.ViewChangeEvent event) {
        setCurrentParamters(parseParameters(event.getParameters()));
        // MVPView
        enter();
    }

    // A very simplistic parameter parser..
    protected java.util.Map<String, ViewParameter> parseParameters(final String parameters) {
        if ((parameters != null) && (!parameters.isEmpty()) && (parameters.charAt(0) == '/')) {
            return parseParameters(parameters.substring(1));
        }
        final java.util.Map<String, ViewParameter> paramters = new java.util.HashMap<String, ViewParameter>();
        if ((parameters == null) || (parameters.isEmpty())) {
            return paramters;
        }
        final String paraStrings[] = parameters.split("[&]
");
        for (String paramString : paraStrings) {
            final int sepIdx = paramString.indexOf('=');
            if (sepIdx > 0) {
                try {
                    final String name = URLDecoder.decode(paramString.substring(0, sepIdx), "UTF-8");
                    final String value = URLDecoder.decode(paramString.substring(sepIdx + 1, paramString.length()), "UTF-8");
                    addParam(paramters, name, value);
                } catch (UnsupportedEncodingException ex) {
                    logger.log(Level.SEVERE, null, ex);
                }
            } else {
                try {
                    final String name = URLDecoder.decode(paramString, "UTF-8");
                    addParam(paramters, name, name);
                } catch (UnsupportedEncodingException ex) {
                    logger.log(Level.SEVERE, null, ex);
                }
            }
        }
        return paramters;
    }

edit : added note
NOTE the default url that the Navigator uses would be of the form:
https:////#!/
The parseParameters method above assumes you delimit your parameters with & and that you don’t pass multiple parameters with the same name ( though this can easily be addressed )

Hi Nanda,

I have the same doubt.
Book of Vaadin (BoV) proposes MVP for Vaadin applications.
I think MVP is excessive for a web app: at least as it is proposed in the BoV.

More, MVP does not handle the navigation / sub navigation.

For a Vaadin UI, you must have only a Navigator.
Ok, you can redefine the ViewDisplay of navigator but…

Surely we need a good initial design!

Good luck,
francesco cotto

Using MVP with Vaadin is one of the topics that divides opinions.

My personal take is that for a simple application with only a handful of views, and assuming you are not familiar with MVP and how to best implement it with Vaadin, MVP might be overkill. When applications get complicated and/or you have experience with MVP with Vaadin, MVP is often a good (but not only) solution.

Note also that there are many different approaches on how to do MVP with Vaadin, varying in complexity, flexibility, integration with other frameworks, initial learning curve etc.