Package root

Source Code of root.MyVaadinUI

package root;

import root.view.form.MahasiswaForm;
import root.view.list.MahasiswaList;
import root.entity.Mahasiswa;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.annotations.Theme;
import com.vaadin.data.Property;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Theme("mytheme")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {

    public static ApplicationContext CONTEXT;

    @Override
    protected void init(VaadinRequest request) {

        CONTEXT = WebApplicationContextUtils.
                getRequiredWebApplicationContext(VaadinServlet.getCurrent().getServletContext());

        try {
            EntityManagerFactory emf = (EntityManagerFactory) CONTEXT.getBean("entityManagerFactory");
            System.out.println(emf);

        } catch (Exception e) {
            e.printStackTrace();
        }

        final VerticalLayout root = new VerticalLayout();

        final MahasiswaForm form = new MahasiswaForm();
        final MahasiswaList list = new MahasiswaList();

        form.setMargin(true);
        form.getSaveButton().addClickListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                form.save();
                ((JPAContainer)list.getMhsTable().getContainerDataSource()).refresh();
                form.clear();
                list.getMhsTable().setValue(null);
            }
        });

        list.getMhsTable().addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                if (event.getProperty().getValue() != null) {
                    Object mhsId = event.getProperty().getValue();
                    Mahasiswa selectedMahasiswa = createEntityManager().find(Mahasiswa.class, mhsId);
                    form.setMahasiswa(selectedMahasiswa);
                } else {
                    form.clear();
                }
            }
        });

        root.addComponent(form);
        root.addComponent(list);

        setContent(root);

    }

    public static UI getCurrentUI() {
        return MyVaadinUI.getCurrent();
    }
   
    public static <T> JPAContainer<T> make(Class<T> entityClass){
        JPAContainer container = JPAContainerFactory.
                make(entityClass,
                MyVaadinUI.CONTEXT.getBean(EntityManagerFactory.class).createEntityManager());
        container.setAutoCommit(true);
        return container;
    }
   
    public static EntityManager createEntityManager(){
        return getEntityManagerFactory().createEntityManager();
    }
   
    public static EntityManagerFactory getEntityManagerFactory(){
        return CONTEXT.getBean(EntityManagerFactory.class);
    }
}
TOP

Related Classes of root.MyVaadinUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.