Java tutorial
/* * Copyright 2015 The original authors * * 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. */ package by.bigvova; import by.bigvova.backend.controllers.UserController; import by.bigvova.backend.controllers.UserControllerUtils; import by.bigvova.backend.event.Event.ProfileUpdatedEvent; import by.bigvova.backend.event.EventBus; import by.bigvova.backend.to.UserTo; import by.bigvova.views.AccessDeniedView; import by.bigvova.views.ErrorView; import by.bigvova.views.ProfilePreferencesWindow; import com.google.common.eventbus.Subscribe; import com.vaadin.annotations.Push; import com.vaadin.annotations.Theme; import com.vaadin.annotations.Widgetset; import com.vaadin.navigator.Navigator; import com.vaadin.server.*; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.shared.ui.ui.Transport; import com.vaadin.spring.annotation.SpringUI; import com.vaadin.spring.navigator.SpringViewProvider; import com.vaadin.ui.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.vaadin.spring.security.VaadinSecurity; import org.vaadin.spring.security.util.SecurityExceptionUtils; import org.vaadin.spring.sidebar.components.ValoSideBar; import org.vaadin.spring.sidebar.security.VaadinSecurityItemFilter; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Locale; @SpringUI @Theme("calorinote") @Widgetset("widgetset.WidgetSet") @Push(transport = Transport.WEBSOCKET) public class MainUI extends UI { @Autowired ApplicationContext applicationContext; @Autowired VaadinSecurity vaadinSecurity; @Autowired VaadinSession vaadinSession; @Autowired SpringViewProvider springViewProvider; @Autowired ValoSideBar sideBar; @Autowired UserController controller; @Value("${foodnote.path.to-profile-images}") private String pathToProfileImage; private final EventBus eventBus = new EventBus(); private MenuBar.MenuItem settingsItem; private Resource resource; @Override protected void init(VaadinRequest request) { EventBus.register(this); getPage().setTitle("FoodNote"); UI.getCurrent().setLocale(Locale.forLanguageTag("ru-RU")); // Let's register a custom error handler to make the 'access denied' messages a bit friendlier. setErrorHandler(new DefaultErrorHandler() { @Override public void error(com.vaadin.server.ErrorEvent event) { if (SecurityExceptionUtils.isAccessDeniedException(event.getThrowable())) { Notification.show("Sorry, you don't have access to do that."); } else { super.error(event); } } }); HorizontalLayout layout = new HorizontalLayout(); layout.setSizeFull(); // By adding a security item filter, only views that are accessible to the user will show up in the side bar. sideBar.setItemFilter(new VaadinSecurityItemFilter(vaadinSecurity)); sideBar.setHeader(new CssLayout() { { Label header = new Label("<span>Food</span>Note", ContentMode.HTML); header.setWidth(100, Unit.PERCENTAGE); header.setHeightUndefined(); addComponent(header); addComponent(buildUserMenu()); } }); sideBar.getHeader().setStyleName("branding"); layout.addComponent(sideBar); CssLayout viewContainer = new CssLayout(); viewContainer.setSizeFull(); layout.addComponent(viewContainer); layout.setExpandRatio(viewContainer, 1f); Navigator navigator = new Navigator(this, viewContainer); // Without an AccessDeniedView, the view provider would act like the restricted views did not exist at all. springViewProvider.setAccessDeniedViewClass(AccessDeniedView.class); navigator.addProvider(springViewProvider); navigator.setErrorView(ErrorView.class); navigator.navigateTo(navigator.getState()); setContent(layout); // Call this here because the Navigator must have been configured before the Side Bar can be attached to a UI. } private Component buildUserMenu() { final MenuBar settings = new MenuBar(); settings.addStyleName("user-menu"); final UserTo userTo = controller.getUserTo(); settingsItem = settings.addItem("", resource, null); updateUserMenu(null); settingsItem.addItem("Edit Profile", new MenuBar.Command() { @Override public void menuSelected(final MenuBar.MenuItem selectedItem) { ProfilePreferencesWindow.open(userTo, resource, controller, pathToProfileImage); } }); settingsItem.addSeparator(); settingsItem.addItem("Sign Out", new MenuBar.Command() { @Override public void menuSelected(final MenuBar.MenuItem selectedItem) { vaadinSecurity.logout(); } }); return settings; } @Subscribe private void updateUserMenu(final ProfileUpdatedEvent event) { UserTo userTo = controller.getUserTo(); String imageName = userTo.getImageName(); String fullPathToProfileImage = pathToProfileImage + '/' + imageName; settingsItem.setText(userTo.getName()); String relativePathToProfileImage = UserControllerUtils.getPathToProfileImage(userTo, true); if (Files.exists(Paths.get(fullPathToProfileImage))) { resource = new FileResource(new File(fullPathToProfileImage)); } else { resource = new ThemeResource(relativePathToProfileImage + "profile-pic-300px.jpg"); } settingsItem.setIcon(resource); } public static EventBus getEventbus() { return ((MainUI) getCurrent()).eventBus; } }