Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.vphakala; import com.vaadin.data.fieldgroup.BeanFieldGroup; import com.vaadin.data.util.BeanItemContainer; import com.vaadin.server.FontAwesome; import com.vaadin.server.Page; import com.vaadin.ui.*; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; /** * * @author FIVELHAKA */ public class CustomerUI extends VerticalLayout { private CustomerService service; private Customer customer; private Grid grid = new Grid(); private TextField firstName = new TextField("First name"); private TextField lastName = new TextField("Last name"); private TextField email = new TextField("Email"); private Button create = new Button("", e -> createCustomer()); private Button update = new Button("", e -> updateCustomer()); private Button read = new Button("", e -> readCustomer()); private Button delete = new Button("", e -> deleteCustomer()); public CustomerUI(CustomerService service) { super(); this.service = service; updateGrid(); grid.setColumns("firstName", "lastName"); grid.addSelectionListener(e -> updateForm()); Toolbar toolbar = new Toolbar("Customer", read, create, update, delete); addComponents(toolbar, firstName, lastName, email, grid); setMargin(true); setSpacing(true); setCaption("Customer"); } private void updateGrid() { List customers = service.findAll(); grid.setContainerDataSource(new BeanItemContainer<>(Customer.class, customers)); setFormVisible(true); } private void updateForm() { if (grid.getSelectedRows().isEmpty()) { setFormVisible(false); } else { customer = (Customer) grid.getSelectedRow(); BeanFieldGroup.bindFieldsUnbuffered(customer, this); if (customer.getEmail() == null) { email.clear(); } setFormVisible(true); } } private void setFormVisible(boolean visible) { firstName.setVisible(visible); lastName.setVisible(visible); email.setVisible(visible); } public void createCustomer() { if (service.create(new Customer(0L, firstName.getValue(), lastName.getValue(), email.getValue())) == true) { Notification.show("create OK"); updateGrid(); firstName.clear(); lastName.clear(); email.clear(); } else { Notification.show("Create failed"); } } public void updateCustomer() { if (grid.getSelectedRows().isEmpty()) { new Notification("Update: failed", "<br/>Select a customer to be <i>updated </i>", Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent()); } else { service.update(customer); updateGrid(); } } public void readCustomer() { List customers = service.read(firstName.getValue(), lastName.getValue()); if (customers.isEmpty() == true) { Notification.show("Read: not found"); } else { customer = (Customer) customers.get(0); BeanFieldGroup.bindFieldsUnbuffered(customer, this); } } public void deleteCustomer() { if (grid.getSelectedRows().isEmpty()) { new Notification("Delete: failed", "<br/>Select a customer to be <i>deleted</i>", Notification.Type.HUMANIZED_MESSAGE, true).show(Page.getCurrent()); } else { customer = (Customer) grid.getSelectedRow(); service.delete(customer); updateGrid(); } } public TextField getEmail() { return email; } }