com.mcparland.john.vaadin_mvn_arch.MyUI.java Source code

Java tutorial

Introduction

Here is the source code for com.mcparland.john.vaadin_mvn_arch.MyUI.java

Source

package com.mcparland.john.vaadin_mvn_arch;

/*
 * #%L
 * vaadin_mvn_arch-ui
 * %%
 * Copyright (C) 2014 John McParland
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import com.mcparland.john.vaadin_mvn_arch.samples.MainScreen;
import com.mcparland.john.vaadin_mvn_arch.samples.authentication.AccessControl;
import com.mcparland.john.vaadin_mvn_arch.samples.authentication.BasicAccessControl;
import com.mcparland.john.vaadin_mvn_arch.samples.authentication.LoginScreen;
import com.mcparland.john.vaadin_mvn_arch.samples.authentication.LoginScreen.LoginListener;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.Responsive;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.UI;

/**
 * 
 */
@Theme("mytheme")
@Widgetset("com.mcparland.john.vaadin_mvn_arch.MyAppWidgetset")
public class MyUI extends UI {

    /**
     * The serialVersionUID.
     */
    private static final long serialVersionUID = 1L;

    private AccessControl accessControl = new BasicAccessControl();

    @SuppressWarnings("serial")
    @Override
    protected void init(VaadinRequest vaadinRequest) {
        Responsive.makeResponsive(this);
        setLocale(vaadinRequest.getLocale());
        getPage().setTitle("My");
        if (!accessControl.isUserSignedIn()) {
            setContent(new LoginScreen(accessControl, new LoginListener() {

                @Override
                public void loginSuccessful() {
                    showMainView();
                }
            }));
        } else {
            showMainView();
        }
    }

    protected void showMainView() {
        setContent(new MainScreen(MyUI.this));
        getNavigator().navigateTo(getNavigator().getState());
    }

    public static MyUI get() {
        return (MyUI) UI.getCurrent();
    }

    public AccessControl getAccessControl() {
        return accessControl;
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {

        /**
         * The serialVersionUID.
         */
        private static final long serialVersionUID = 1L;

        @Override
        protected void servletInitialized() throws ServletException {
            super.servletInitialized();
            /*
             * Configure the viewport meta tags appropriately on mobile devices.
             * Instead of device based scaling (default), using responsive
             * layouts.
             * 
             * If using Vaadin TouchKit, this is done automatically and it is
             * sufficient to have an empty servlet class extending
             * TouchKitServlet.
             */
            getService().addSessionInitListener(new ViewPortSessionInitListener());
        }
    }
}