Reborn Reborn - 14 days ago 5
Java Question

How To Override Vaadin Navigator properly

I just extended Vaadin's navigator and overriden some some methods:

public class MyNavigator extends Navigator {

final SpringViewProvider viewProvider;

public MyNavigator(UI ui, SingleComponentContainer container, SpringViewProvider viewProvider) {
super(ui, container);
this.viewProvider = viewProvider;
addProvider(this.viewProvider);
}

@Override
public void navigateTo(String navigationState) {
if(ViewTokens.AVAILABLE_TOKENS.contains(navigationState)) {
if(viewProvider.getView(navigationState) == null) {
String uriFragment = parseViewToken();
if(uriFragment.equals(navigationState)) {
super.navigateTo(ViewTokens.HOME);
}
} else {
//if nothing found, redirect
super.navigateTo(ViewTokens.HOME);
}
}
}

private String parseViewToken() {
<ommitted>
}


}

This is how I set up Navigator:

@SpringUI(path="/")
@Theme(ValoTheme.THEME_NAME)
public class MyFancyUI extends UI {

@Autowired
SpringViewProvider viewProvider;

@Override
protected void init(VaadinRequest vaadinRequest) {
MyNavigator navigator = new MyNavigator(UI.getCurrent(), this, viewProvider);
setNavigator(navigator);
navigator.navigateTo(ViewTokens.SIGNIN);
}


}

And this is how i attempt to get navigator and redirect view to another:

@UIScope
@SpringView(name=ViewTokens.HOME)
public class SplashScreen extends VerticalLayout implements View {
private ComponentHelper componentHelper;
private Button toLoginPage;

public SplashScreen() {
initComponents();
buildSplashView();
}

protected void initComponents() {
componentHelper = ComponentHelper.getInstance();
toLoginPage = componentHelper.createFriendlyButton("To Login Page",220, 60);
toLoginPage.addClickListener(this::redirect);
}

private void buildSplashView() {
addComponent(toLoginPage);
setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
setHeight(100, Unit.PERCENTAGE);
}

public void redirect(Button.ClickEvent event) {
getUI().getNavigator().navigateTo(ViewTokens.SIGNIN);
}

@Override
public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent){

}
}


And for some reason, button doesn't react to click event.

Replacing to standard Navigator - it works.

What i'm doing wrong?

Thanks for suggestions

Answer

I would have thought that you could use Vaadin's Navigator and a View Change Listener like this:

        navigator.addViewChangeListener(new ViewChangeListener() {

        @Override
        public boolean beforeViewChange(ViewChangeEvent event) {
            // check you're happy to change

            if (ViewTokens.AVAILABLE_TOKENS.contains(navigationState)) {
                if (viewProvider.getView(event.getViewName()) == null) {
                    String uriFragment = parseViewToken();
                    if (uriFragment.equals(event.getViewName())) {
                        navigator.navigateTo(ViewTokens.HOME);
                        return false;
                    }
                } else {
                    // if nothing found, redirect
                    navigator.navigateTo(ViewTokens.HOME);
                    return false;
                }
                // Let the view change happen.
                return true;
            }

        }

        @Override
        public void afterViewChange(ViewChangeEvent event) {

        }
    });

If you return false in the beforeViewChange it will stop the view change from happening.