Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Refactor main view and UI event bus components
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-baiborodine committed Feb 2, 2018
1 parent c4f618b commit 217109d
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 136 deletions.
65 changes: 65 additions & 0 deletions src/main/java/com/kiroule/jpetstore/vaadinspring/ui/AppUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kiroule.jpetstore.vaadinspring.ui;

import com.kiroule.jpetstore.vaadinspring.ui.event.UIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasLogger;
import com.kiroule.jpetstore.vaadinspring.ui.util.NavBarButtonUpdater;
import com.kiroule.jpetstore.vaadinspring.ui.util.PageTitleUpdater;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.Widgetset;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.navigator.SpringViewProvider;
import com.vaadin.ui.UI;

import org.springframework.beans.factory.annotation.Autowired;

/**
* @author Igor Baiborodine
*/
@Title("JPetStore Vaadin 8 Spring Demo")
@Theme("jpetstoretheme")
@Widgetset("com.vaadin.v7.Vaadin7WidgetSet")
@SpringUI
public class AppUI extends UI implements HasLogger {

private static final long serialVersionUID = 4670701701584923650L;

private final SpringViewProvider viewProvider;
private final MainView mainView;
private final UIEventBus uiEventBus;
private final PageTitleUpdater pageTitleUpdater;
private final NavBarButtonUpdater navBarButtonUpdater;

@Autowired
public AppUI(SpringViewProvider viewProvider, MainView mainView, UIEventBus uiEventBus,
PageTitleUpdater pageTitleUpdater, NavBarButtonUpdater navBarButtonUpdater) {
this.viewProvider = viewProvider;
this.mainView = mainView;
this.uiEventBus = uiEventBus;
this.pageTitleUpdater = pageTitleUpdater;
this.navBarButtonUpdater = navBarButtonUpdater;
}

public static AppUI getCurrent() {
return (AppUI) UI.getCurrent();
}

public static UIEventBus getUiEventBus() {
return getCurrent().uiEventBus;
}

@Override
protected void init(VaadinRequest request) {
setContent(mainView);

Navigator navigator = new Navigator(this, mainView.getViewContainer());
navigator.addProvider(viewProvider);
navigator.addViewChangeListener(navBarButtonUpdater);
navigator.addViewChangeListener(pageTitleUpdater);

uiEventBus.register(mainView);
getLogger().info("App UI initialized");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kiroule.jpetstore.vaadinspring.ui;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import com.kiroule.jpetstore.vaadinspring.domain.Account;
Expand All @@ -17,27 +16,20 @@
import com.kiroule.jpetstore.vaadinspring.ui.menu.TopNavBar;
import com.kiroule.jpetstore.vaadinspring.ui.util.CurrentAccount;
import com.kiroule.jpetstore.vaadinspring.ui.util.CurrentCart;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasLogger;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasUIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.util.NavBarButtonUpdater;
import com.kiroule.jpetstore.vaadinspring.ui.util.PageTitleUpdater;
import com.kiroule.jpetstore.vaadinspring.ui.view.AccountView;
import com.kiroule.jpetstore.vaadinspring.ui.view.AuthRequiredView;
import com.kiroule.jpetstore.vaadinspring.ui.view.CartView;
import com.kiroule.jpetstore.vaadinspring.ui.view.HomeView;
import com.kiroule.jpetstore.vaadinspring.ui.view.ItemListView;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.Widgetset;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinSession;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.navigator.SpringViewProvider;
import com.vaadin.ui.UI;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.UIScope;
import com.vaadin.v7.ui.HorizontalLayout;
import com.vaadin.v7.ui.VerticalLayout;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import static com.kiroule.jpetstore.vaadinspring.ui.menu.TopNavBar.SIGNIN_BUTTON_URI;
Expand All @@ -47,76 +39,44 @@
/**
* @author Igor Baiborodine
*/
@Title("JPetStore Vaadin 8 Spring Demo")
@Theme("jpetstoretheme")
@Widgetset("com.vaadin.v7.Vaadin7WidgetSet")
@SpringUI
public class MainUI extends UI {
@SpringComponent
@UIScope
class MainView extends HorizontalLayout implements HasLogger, HasUIEventBus {

private static final long serialVersionUID = 4670701701584923650L;
private final static Logger logger = LoggerFactory.getLogger(MainUI.class);
private static final long serialVersionUID = 7419653252582861360L;

@Autowired
private SpringViewProvider viewProvider;
@Autowired
private TopNavBar topNavBar;
@Autowired
private LeftNavBar leftNavBar;
@Autowired
private PageTitleUpdater pageTitleUpdater;
@Autowired
private NavBarButtonUpdater navBarButtonUpdater;
@Autowired
private CatalogService catalogService;

private EventBus eventBus;
private final LeftNavBar leftNavBar;
private final TopNavBar topNavBar;
private final CatalogService catalogService;
private final NavBarButtonUpdater navBarButtonUpdater;
private final VerticalLayout viewLayout = new VerticalLayout();
private final VerticalLayout viewContainer = new VerticalLayout();

public static MainUI getCurrent() {
return (MainUI) UI.getCurrent();
}

public static EventBus getEventBus() {
return getCurrent().eventBus;
}

@Override
protected void init(VaadinRequest request) {
initEventBus();
initMainContent();
logger.info("Finished initialization of main UI");
}

private void initEventBus() {
eventBus = new EventBus((throwable, subscriberExceptionContext) -> {
logger.error("Subscriber event error: ", throwable);
});
eventBus.register(this);
}

private void initMainContent() {

HorizontalLayout contentLayout = new HorizontalLayout();
contentLayout.setSizeFull();
setContent(contentLayout);
@Autowired
public MainView(LeftNavBar leftNavBar, TopNavBar topNavBar,
CatalogService catalogService, NavBarButtonUpdater navBarButtonUpdater) {
super();
this.leftNavBar = leftNavBar;
this.topNavBar = topNavBar;
this.catalogService = catalogService;
this.navBarButtonUpdater = navBarButtonUpdater;

contentLayout.addComponent(leftNavBar);
setSizeFull();
addComponent(this.leftNavBar);

VerticalLayout viewLayout = new VerticalLayout();
viewLayout.setSizeFull();
contentLayout.addComponent(viewLayout);
contentLayout.setExpandRatio(viewLayout, 1.0f);
addComponent(viewLayout);
setExpandRatio(viewLayout, 1.0f);
viewLayout.addComponent(this.topNavBar);

viewLayout.addComponent(topNavBar);

VerticalLayout viewContainer = new VerticalLayout();
viewContainer.setSizeFull();
viewLayout.addComponent(viewContainer);
viewLayout.setExpandRatio(viewContainer, 1.0f);
getLogger().info("Main view initialized");
}

Navigator navigator = new Navigator(this, viewContainer);
navigator.addProvider(viewProvider);
navigator.addViewChangeListener(navBarButtonUpdater);
navigator.addViewChangeListener(pageTitleUpdater);
public VerticalLayout getViewContainer() {
return viewContainer;
}

@Subscribe
Expand All @@ -134,7 +94,7 @@ public void userLoggedIn(UILoginEvent event) {
navBarButtonUpdater.setButtonVisible(SIGNOUT_BUTTON_URI, true);
navBarButtonUpdater.clear();

String state = getNavigator().getState();
String state = AppUI.getCurrent().getNavigator().getState();
String viewName = state.equals(AuthRequiredView.VIEW_NAME) ? HomeView.VIEW_NAME : state;
navigateTo(viewName); // reloading the current view to display the banner
}
Expand All @@ -153,7 +113,7 @@ public void updateAccount(UIUpdateAccountEvent event) {
public void logout(UILogoutEvent event) {

// Don't invalidate the underlying HTTP session if you are using it for something else
getPage().setLocation("/"); // redirect to the Home page
AppUI.getCurrent().getPage().setLocation("/"); // redirect to the Home page
VaadinSession.getCurrent().getSession().invalidate();
VaadinSession.getCurrent().close();
}
Expand All @@ -170,7 +130,7 @@ public void addItemToCart(UIAddItemToCartEvent event) {

// If an item added from the Item list view, redirect to the Cart view;
// otherwise reload the current view which could be either Cart or Confirm Order
String state = getNavigator().getState();
String state = AppUI.getCurrent().getNavigator().getState();
String viewName = state.contains(ItemListView.VIEW_NAME) ? CartView.VIEW_NAME : state;
navigateTo(viewName);
}
Expand All @@ -180,18 +140,18 @@ public void removeItemFromCart(UIRemoveItemFromCartEvent event) {

Cart cart = (Cart) CurrentCart.get(SHOPPING_CART);
cart.removeItemById(event.getItem().getItemId());
String viewName = CurrentCart.isEmpty() ? CartView.VIEW_NAME : getNavigator().getState();
String viewName = CurrentCart.isEmpty() ? CartView.VIEW_NAME : AppUI.getCurrent().getNavigator().getState();
navigateTo(viewName);
}

@Subscribe
public void changeCartItemQuantity(UIChangeCartItemQuantityEvent event) {
Cart cart = (Cart) CurrentCart.get(SHOPPING_CART);
cart.changeQuantityByItemId(event.getItem().getItemId(), event.getDiff());
navigateTo(getNavigator().getState());
navigateTo(AppUI.getCurrent().getNavigator().getState());
}

private void navigateTo(String viewName) {
getNavigator().navigateTo(viewName);
AppUI.getCurrent().getNavigator().navigateTo(viewName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.kiroule.jpetstore.vaadinspring.ui.converter.BooleanConverter;
import com.kiroule.jpetstore.vaadinspring.ui.converter.CurrencyConverter;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIChangeCartItemQuantityEvent;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIRemoveItemFromCartEvent;
import com.kiroule.jpetstore.vaadinspring.ui.form.ProductItemForm;
import com.kiroule.jpetstore.vaadinspring.ui.theme.JPetStoreTheme;
import com.kiroule.jpetstore.vaadinspring.ui.util.CurrentCart;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasUIEventBus;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Button;
Expand All @@ -33,7 +33,7 @@
*/
@SpringComponent
@ViewScope
public class CartItemListTable extends MTable<CartItem> {
public class CartItemListTable extends MTable<CartItem> implements HasUIEventBus {

private static final long serialVersionUID = 6841591708524361792L;

Expand Down Expand Up @@ -71,7 +71,7 @@ public CartItemListTable() {
if (CurrentCart.isEmpty()) {
return;
}
UIEventBus.post(new UIRemoveItemFromCartEvent(cartItem.getItem()));
getUIEventBus().post(new UIRemoveItemFromCartEvent(cartItem.getItem()));
}
));

Expand Down Expand Up @@ -101,7 +101,7 @@ private TextField createQuantityField(CartItem cartItem) {
}
if (valid) {
Integer newQuantity = Integer.valueOf((String) event.getProperty().getValue());
UIEventBus.post(new UIChangeCartItemQuantityEvent(
getUIEventBus().post(new UIChangeCartItemQuantityEvent(
cartItem.getItem(), newQuantity - cartItem.getQuantity()));
} else {
Notification.show("Numeric values only", Notification.Type.ERROR_MESSAGE);
Expand All @@ -121,7 +121,7 @@ private TextField createQuantityField(CartItem cartItem) {
// return;
// }
// Integer newQuantity = event.getSource().getValue();
// UIEventBus.post(new UIChangeCartItemQuantityEvent(cartItem.getItem(), newQuantity - cartItem.getQuantity()));
// getUIEventBus().post(new UIChangeCartItemQuantityEvent(cartItem.getItem(), newQuantity - cartItem.getQuantity()));
// });
// return quantityStepper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kiroule.jpetstore.vaadinspring.ui.event.UIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.form.ProductItemForm;
import com.kiroule.jpetstore.vaadinspring.ui.theme.JPetStoreTheme;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasUIEventBus;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Button;
Expand All @@ -21,12 +22,14 @@
*/
@SpringComponent
@ViewScope
public class ItemListTable extends MTable<Item> {
public class ItemListTable extends MTable<Item> implements HasUIEventBus {

private static final long serialVersionUID = -2847546238729364925L;

@Autowired
private ProductItemForm productItemForm;
@Autowired
private UIEventBus uiEventBus;

public ItemListTable() {

Expand All @@ -44,7 +47,7 @@ public ItemListTable() {
});
withGeneratedColumn("description", item -> item.getAttribute1() + " " + item.getProduct().getName());
withGeneratedColumn("addToCart",
item -> new Button("Add to Cart", event -> UIEventBus.post(new UIAddItemToCartEvent(item))));
item -> new Button("Add to Cart", event -> getUIEventBus().post(new UIAddItemToCartEvent(item))));
setConverter("listPrice", new CurrencyConverter());
withFullWidth();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.kiroule.jpetstore.vaadinspring.ui.component;

import com.kiroule.jpetstore.vaadinspring.domain.Product;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.event.UINavigationEvent;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasUIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.view.ItemListView;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
Expand All @@ -15,7 +15,7 @@
*/
@SpringComponent
@ViewScope
public class ProductListTable extends MTable<Product> {
public class ProductListTable extends MTable<Product> implements HasUIEventBus {

private static final long serialVersionUID = 2029031508462137840L;

Expand All @@ -27,7 +27,7 @@ public ProductListTable() {
withGeneratedColumn("productId", entity -> {
String uri = ItemListView.VIEW_NAME + "/" + entity.getProductId();
Button inventoryButton = new Button(entity.getProductId(),
event -> UIEventBus.post(new UINavigationEvent(uri)));
event -> getUIEventBus().post(new UINavigationEvent(uri)));
inventoryButton.setData(entity.getProductId());
inventoryButton.addStyleName("link");
return inventoryButton;
Expand Down
Loading

0 comments on commit 217109d

Please sign in to comment.