Java tutorial
/* * Copyright (C) 2013 SGV * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.gvcc.jgoffice; import com.vaadin.annotations.PreserveOnRefresh; import com.vaadin.annotations.Theme; import com.vaadin.annotations.VaadinServletConfiguration; import com.vaadin.navigator.Navigator; import com.vaadin.navigator.ViewChangeListener; import com.vaadin.server.*; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.ui.Alignment; import com.vaadin.ui.Panel; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import java.io.Serializable; import java.util.ArrayList; import net.gvcc.jgoffice.commons.Global; import net.gvcc.jgoffice.components.CommandBar; import net.gvcc.jgoffice.spi.api.IViewRegistration; import net.gvcc.jgoffice.templates.interfaces.IGlobalConst; import net.gvcc.jgoffice.views.EmptyView; import net.gvcc.jgoffice.views.ErrorPage; import net.gvcc.jgoffice.views.LoginView; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import java.util.ServiceLoader; import java.util.StringTokenizer; import net.gvcc.jgoffice.components.FooterBar; import net.gvcc.jgoffice.searchcomponent.interfaces.ISearchForm; /** * The layout for the main site. TThe main page consists of several parts. * The head region (CommandBar), the main area (MainPanel) and the footer (FooterPanel). * * @author ernst_sgv {@code Global.initMainContent(CustomComponent c)} */ @SuppressWarnings("serial") @Theme("jgoffice") @PreserveOnRefresh public class MainUI extends UI implements ViewChangeListener, IGlobalConst, Serializable, ISearchForm { @WebServlet(value = "/*", asyncSupported = true) @VaadinServletConfiguration(productionMode = false, ui = MainUI.class, widgetset = "net.gvcc.jgoffice.AppWidgetSet") public static class Servlet extends VaadinServlet implements SessionInitListener, SessionDestroyListener { @Override protected void servletInitialized() throws ServletException { super.servletInitialized(); getService().addSessionInitListener(this); getService().addSessionDestroyListener(this); } @Override public void sessionDestroy(SessionDestroyEvent event) { // TODO Destroy all the DB Connections System.out.println("Session closed"); } @Override public void sessionInit(SessionInitEvent event) throws ServiceException { System.out.println("Session initialized"); } } private final VerticalLayout mainLayout = new VerticalLayout(); private final VerticalLayout loginLayout = new VerticalLayout(); private final CommandBar commandBar = new CommandBar(); private final FooterBar footerBar = new FooterBar(); private final Panel mainPanel = new Panel(); private final Global global = new Global(); private Navigator mainNavigator; private String loggedInUser; /* * Service provider interface */ private final ServiceLoader<IViewRegistration> viewRegistrations = ServiceLoader.load(IViewRegistration.class); /* * (non-Javadoc) * * @see com.vaadin.ui.UI#init(com.vaadin.server.VaadinRequest) */ @Override protected void init(VaadinRequest request) { Navigator loginNavigator; getPage().setTitle("jGOffice"); initLayoutBeforLogin(); loginNavigator = new Navigator(this, loginLayout); loginNavigator.setErrorView(new ErrorPage()); loginNavigator.addView("LoginView", new LoginView()); loginNavigator.navigateTo("LoginView"); loginNavigator.addViewChangeListener(this); } /* * To be called from the LoginView after a successfully login */ public void initLayoutAfterLogin() { mainLayout.addComponent(getCommandBar()); mainLayout.addComponent(getMainPanel()); mainLayout.addComponent(getFooterBar()); mainLayout.setComponentAlignment(getMainPanel(), Alignment.TOP_CENTER); mainLayout.setComponentAlignment(getCommandBar(), Alignment.TOP_CENTER); mainLayout.setComponentAlignment(getFooterBar(), Alignment.TOP_CENTER); mainLayout.setExpandRatio(getMainPanel(), 1.0f); mainLayout.setHeight("100%"); mainLayout.setImmediate(false); getMainPanel().setWidth(MAINPANEL_WIDTH); getMainPanel().setHeight(MAINPANEL_HEIGHT); getMainPanel().setStyleName("g-jgoffice-module"); getCommandBar().setApplicationTitle(""); getCommandBar().setWidth(MAINPANEL_WIDTH); getFooterBar().setWidth(MAINPANEL_WIDTH); mainNavigator = new Navigator(this, getMainPanel()); mainNavigator.setErrorView(new ErrorPage()); UI.getCurrent().setNavigator(mainNavigator); getMainNavigator().addView("EmptyView", new EmptyView()); // FIXME We load all the available modules. We need a better solution!!! for (IViewRegistration viewRegistration : viewRegistrations) { viewRegistration.addViews(getMainNavigator()); } setWidth("100%"); setHeight("100%"); setContent(mainLayout); getCommandBar().searchComponent.addForm(this); } private void initLayoutBeforLogin() { loginLayout.setImmediate(false); loginLayout.setHeight("100%"); loginLayout.setImmediate(false); loginLayout.setMargin(new MarginInfo(false, true, false, true)); setWidth("100%"); setHeight("100%"); setContent(loginLayout); } @Override public void performSearch(ArrayList<String> searchTokens) { for (String string : searchTokens) { if (string.toUpperCase().contains("#START")) { StringTokenizer t = new StringTokenizer(string, ":"); while (t.hasMoreTokens()) { t.nextToken(); getMainNavigator().navigateTo(t.nextToken().toUpperCase()); } } } } /* * Getter and Setter methods */ @Override public void detach() { System.out.println("UI detach"); super.detach(); } public String getLoggedInUser() { return loggedInUser; } public void setLoggedInUser(String loggedInUser) { this.loggedInUser = loggedInUser; } public Navigator getMainNavigator() { return mainNavigator; } /* * (non-Javadoc) Event Handlers * * @see * com.vaadin.navigator.ViewChangeListener#beforeViewChange(com.vaadin.navigator * .ViewChangeListener.ViewChangeEvent) */ @Override public boolean beforeViewChange(ViewChangeEvent event) { return !((loggedInUser == null) || (loggedInUser.trim().equals(""))); } @Override public void afterViewChange(ViewChangeEvent event) { } private Panel getMainPanel() { return mainPanel; } private FooterBar getFooterBar() { return footerBar; } public Global getGlobal() { return global; } public CommandBar getCommandBar() { return commandBar; } }