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

Commit

Permalink
Switch to vaadin-spring's UIEventBus
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-baiborodine committed Feb 3, 2018
1 parent ee229ac commit 4e4750a
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.vaadin.spring.events.annotation.EnableEventBus;

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@Import({DataSourceConfig.class, ServiceConfig.class})
@EnableEventBus
public class JPetStore6Application {

public static void main(String[] args) {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/kiroule/jpetstore/vaadinspring/ui/AppUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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;
Expand All @@ -14,6 +13,9 @@
import com.vaadin.ui.UI;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.spring.events.EventBus;

import javax.annotation.PreDestroy;

/**
* @author Igor Baiborodine
Expand All @@ -28,12 +30,12 @@ public class AppUI extends UI implements HasLogger {

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

@Autowired
public AppUI(SpringViewProvider viewProvider, MainView mainView, UIEventBus uiEventBus,
public AppUI(SpringViewProvider viewProvider, MainView mainView, EventBus.UIEventBus uiEventBus,
PageTitleUpdater pageTitleUpdater, NavBarButtonUpdater navBarButtonUpdater) {
this.viewProvider = viewProvider;
this.mainView = mainView;
Expand All @@ -46,7 +48,7 @@ public static AppUI getCurrent() {
return (AppUI) UI.getCurrent();
}

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

Expand All @@ -59,7 +61,7 @@ protected void init(VaadinRequest request) {
navigator.addViewChangeListener(navBarButtonUpdater);
navigator.addViewChangeListener(pageTitleUpdater);

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

import com.google.common.eventbus.Subscribe;

import com.kiroule.jpetstore.vaadinspring.domain.Account;
import com.kiroule.jpetstore.vaadinspring.domain.Cart;
import com.kiroule.jpetstore.vaadinspring.service.CatalogService;
Expand Down Expand Up @@ -31,6 +29,7 @@
import com.vaadin.v7.ui.VerticalLayout;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.spring.events.annotation.EventBusListenerMethod;

import static com.kiroule.jpetstore.vaadinspring.ui.menu.TopNavBar.SIGNIN_BUTTON_URI;
import static com.kiroule.jpetstore.vaadinspring.ui.menu.TopNavBar.SIGNOUT_BUTTON_URI;
Expand Down Expand Up @@ -79,12 +78,12 @@ public VerticalLayout getViewContainer() {
return viewContainer;
}

@Subscribe
@EventBusListenerMethod
public void navigateTo(UINavigationEvent event) {
navigateTo(event.getViewName());
}

@Subscribe
@EventBusListenerMethod
public void userLoggedIn(UILoginEvent event) {

CurrentAccount.set(event.getAccount());
Expand All @@ -99,7 +98,7 @@ public void userLoggedIn(UILoginEvent event) {
navigateTo(viewName); // reloading the current view to display the banner
}

@Subscribe
@EventBusListenerMethod
public void updateAccount(UIUpdateAccountEvent event) {

Account account = event.getAccount();
Expand All @@ -109,7 +108,7 @@ public void updateAccount(UIUpdateAccountEvent event) {
navigateTo(AccountView.VIEW_NAME);
}

@Subscribe
@EventBusListenerMethod
public void logout(UILogoutEvent event) {

// Don't invalidate the underlying HTTP session if you are using it for something else
Expand All @@ -118,7 +117,7 @@ public void logout(UILogoutEvent event) {
VaadinSession.getCurrent().close();
}

@Subscribe
@EventBusListenerMethod
public void addItemToCart(UIAddItemToCartEvent event) {

if (CurrentCart.isEmpty()) {
Expand All @@ -135,7 +134,7 @@ public void addItemToCart(UIAddItemToCartEvent event) {
navigateTo(viewName);
}

@Subscribe
@EventBusListenerMethod
public void removeItemFromCart(UIRemoveItemFromCartEvent event) {

Cart cart = (Cart) CurrentCart.get(SHOPPING_CART);
Expand All @@ -144,7 +143,7 @@ public void removeItemFromCart(UIRemoveItemFromCartEvent event) {
navigateTo(viewName);
}

@Subscribe
@EventBusListenerMethod
public void changeCartItemQuantity(UIChangeCartItemQuantityEvent event) {
Cart cart = (Cart) CurrentCart.get(SHOPPING_CART);
cart.changeQuantityByItemId(event.getItem().getItemId(), event.getDiff());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public CartItemListTable() {
if (CurrentCart.isEmpty()) {
return;
}
getUIEventBus().post(new UIRemoveItemFromCartEvent(cartItem.getItem()));
getUIEventBus().publish(this, 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());
getUIEventBus().post(new UIChangeCartItemQuantityEvent(
getUIEventBus().publish(this, 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();
// getUIEventBus().post(new UIChangeCartItemQuantityEvent(cartItem.getItem(), newQuantity - cartItem.getQuantity()));
// getUIEventBus().publish(this, new UIChangeCartItemQuantityEvent(cartItem.getItem(), newQuantity - cartItem.getQuantity()));
// });
// return quantityStepper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.kiroule.jpetstore.vaadinspring.domain.Item;
import com.kiroule.jpetstore.vaadinspring.ui.converter.CurrencyConverter;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIAddItemToCartEvent;
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;
Expand All @@ -28,8 +27,6 @@ public class ItemListTable extends MTable<Item> implements HasUIEventBus {

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

public ItemListTable() {

Expand All @@ -47,7 +44,7 @@ public ItemListTable() {
});
withGeneratedColumn("description", item -> item.getAttribute1() + " " + item.getProduct().getName());
withGeneratedColumn("addToCart",
item -> new Button("Add to Cart", event -> getUIEventBus().post(new UIAddItemToCartEvent(item))));
item -> new Button("Add to Cart", event -> getUIEventBus().publish(this, new UIAddItemToCartEvent(item))));
setConverter("listPrice", new CurrencyConverter());
withFullWidth();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ProductListTable() {
withGeneratedColumn("productId", entity -> {
String uri = ItemListView.VIEW_NAME + "/" + entity.getProductId();
Button inventoryButton = new Button(entity.getProductId(),
event -> getUIEventBus().post(new UINavigationEvent(uri)));
event -> getUIEventBus().publish(this, new UINavigationEvent(uri)));
inventoryButton.setData(entity.getProductId());
inventoryButton.addStyleName("link");
return inventoryButton;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
import com.kiroule.jpetstore.vaadinspring.domain.Item;
import com.kiroule.jpetstore.vaadinspring.ui.converter.CurrencyConverter;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIAddItemToCartEvent;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIEventBus;
import com.kiroule.jpetstore.vaadinspring.ui.theme.JPetStoreTheme;
import com.kiroule.jpetstore.vaadinspring.ui.util.HasUIEventBus;
import com.vaadin.v7.shared.ui.label.ContentMode;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.ViewScope;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.UI;
import com.vaadin.v7.shared.ui.label.ContentMode;
import com.vaadin.v7.ui.Label;
import com.vaadin.v7.ui.TextField;
import com.vaadin.ui.UI;

import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.viritinv7.MBeanFieldGroup;
import org.vaadin.viritin.button.MButton;
import org.vaadin.viritinv7.fields.MTextField;
import org.vaadin.viritinv7.form.AbstractForm;
import org.vaadin.viritin.layouts.MFormLayout;
import org.vaadin.viritin.layouts.MVerticalLayout;
import org.vaadin.viritinv7.MBeanFieldGroup;
import org.vaadin.viritinv7.fields.MTextField;
import org.vaadin.viritinv7.form.AbstractForm;

import javax.annotation.PostConstruct;

Expand All @@ -34,9 +32,6 @@ public class ProductItemForm extends AbstractForm<Item> implements HasUIEventBus

private static final long serialVersionUID = -3035656440388295692L;

@Autowired
private UIEventBus uiEventBus;

private Label image = new Label();
private TextField itemId = new MTextField("ID");
private TextField itemDescription = new MTextField("Description");
Expand All @@ -52,7 +47,7 @@ public void init() {
listPrice.setConverter(new CurrencyConverter());
addToCartButton.addClickListener(event -> {
UI.getCurrent().removeWindow(getPopup());
getUIEventBus().post(new UIAddItemToCartEvent(getEntity()));
getUIEventBus().publish(this, new UIAddItemToCartEvent(getEntity()));
}
);
addToCartButton.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Component createContent(TextField usernameField, PasswordField passwordFi
Button createAccountButton = new Button("Start Here!");
createAccountButton.addStyleName(JPetStoreTheme.BUTTON_LINK);
createAccountButton.addClickListener(event -> {
getUIEventBus().post(new UINavigationEvent(NewAccountView.VIEW_NAME));
getUIEventBus().publish(this, new UINavigationEvent(NewAccountView.VIEW_NAME));
UI.getCurrent().removeWindow(popup);
});
newCustomerLayout.add(createAccountButton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void init() {
private void addButton(String categoryId, String displayName) {

String uri = ProductListView.VIEW_NAME + "/" + categoryId;
Button viewButton = new Button(displayName, click -> getUIEventBus().post(new UINavigationEvent(uri)));
Button viewButton = new Button(displayName, click -> getUIEventBus().publish(LeftNavBar.this, new UINavigationEvent(uri)));
navBarButtonUpdater.mapButtonToUri(uri, viewButton);

viewButton.addStyleName(JPetStoreTheme.MENU_ITEM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void handleAction(Object sender, Object target) {
try {
Account account = loginService.login(loginEvent.getLoginParameter("username"),
loginEvent.getLoginParameter("password"));
getUIEventBus().post(new UILoginEvent(account));
getUIEventBus().publish(this, new UILoginEvent(account));
UI.getCurrent().removeWindow(popup);
} catch (LoginException e) {
Notification.show(e.getMessage(), Notification.Type.WARNING_MESSAGE);
Expand All @@ -101,7 +101,7 @@ public void handleAction(Object sender, Object target) {
signoutButton = addButton(SIGNOUT_BUTTON_URI, "Sign out", event -> {
signoutButton.setVisible(false);
signinButton.setVisible(true);
getUIEventBus().post(new UILogoutEvent());
getUIEventBus().publish(this, new UILogoutEvent());
});
signoutButton.setVisible(CurrentAccount.isLoggedIn());

Expand All @@ -118,12 +118,12 @@ private void searchProducts(String keyword) {
.show(Page.getCurrent());
} else {
String uri = SearchView.VIEW_NAME + "/" + keyword.trim().toLowerCase().replaceAll("%", "");
getUIEventBus().post(new UINavigationEvent(uri));
getUIEventBus().publish(this, new UINavigationEvent(uri));
}
}

private Button addButton(String uri, String caption) {
return addButton(uri, caption, event -> getUIEventBus().post(new UINavigationEvent(uri)));
return addButton(uri, caption, event -> getUIEventBus().publish(TopNavBar.this, new UINavigationEvent(uri)));
}

private Button addButton(String uri, String caption, Button.ClickListener listener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.kiroule.jpetstore.vaadinspring.ui.util;

import com.kiroule.jpetstore.vaadinspring.ui.AppUI;
import com.kiroule.jpetstore.vaadinspring.ui.event.UIEventBus;

import org.vaadin.spring.events.EventBus;

/**
* @author Igor Baiborodine
*/
public interface HasUIEventBus {

default UIEventBus getUIEventBus() {
default EventBus.UIEventBus getUIEventBus() {
return AppUI.getUiEventBus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public abstract class AbstractView extends MVerticalLayout implements View, HasU
public void enter(ViewChangeListener.ViewChangeEvent event) {

if (ViewConfigUtil.isAuthRequired(this.getClass()) && !CurrentAccount.isLoggedIn()) {
getUIEventBus().post(new UINavigationEvent(AuthRequiredView.VIEW_NAME));
getUIEventBus().publish(this, new UINavigationEvent(AuthRequiredView.VIEW_NAME));
return;
}
executeOnEnter(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ void init() {
accountService.updateAccount(account);
accountForm.clear();
showConfirmation("Your account has been updated.");
getUIEventBus().post(new UIUpdateAccountEvent(account));
getUIEventBus().publish(AccountView.this, new UIUpdateAccountEvent(account));
} catch (Throwable t) {
Notification.show("An error occurred while updating account: " + t.getMessage(), ERROR_MESSAGE);
}
});
accountForm.setResetHandler(account -> getUIEventBus().post(new UINavigationEvent(OrderListView.VIEW_NAME)));
accountForm.setResetHandler(account -> getUIEventBus().publish(this, new UINavigationEvent(OrderListView.VIEW_NAME)));

Panel contentPanel = new Panel(accountForm);
addComponents(initTitleLabel(), contentPanel, accountForm.getToolbar(EDIT));
Expand Down
Loading

0 comments on commit 4e4750a

Please sign in to comment.