Package com.example.addressbook

Source Code of com.example.addressbook.AddressbookApplication

package com.example.addressbook;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanContainer;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class AddressbookApplication extends Application {

    private static String[] fields = { "First Name", "Last Name", "Company",
            "Mobile Phone", "Work Phone", "Home Phone", "Work Email",
            "Home Email", "Street", "Zip", "City", "State", "Country" };
    private static String[] visibleCols = new String[] { "Last Name",
            "First Name", "Company" };

    private Table contactList = new Table();
    private Form contactEditor = new Form();
    private HorizontalLayout bottomLeftCorner = new HorizontalLayout();
    private Button contactRemovalButton;
    private IndexedContainer addressBookData = createDummyData();

    @Override
    public void init() {
        initLayout();
        initContactAddRemoveButtons();
        initAddressList();
        initFilteringControls();
      
    }

    private void initLayout() {
        SplitPanel splitPanel = new SplitPanel(
                SplitPanel.ORIENTATION_HORIZONTAL);
        setMainWindow(new Window("Address Book", splitPanel));
        VerticalLayout left = new VerticalLayout();
        left.setSizeFull();
        left.addComponent(contactList);
        contactList.setSizeFull();
        left.setExpandRatio(contactList, 1);
        splitPanel.addComponent(left);
        splitPanel.addComponent(contactEditor);
        contactEditor.setCaption("Contact details editor");
        contactEditor.setSizeFull();
        contactEditor.getLayout().setMargin(true);
        contactEditor.setImmediate(true);
        bottomLeftCorner.setWidth("100%");
        left.addComponent(bottomLeftCorner);
    }

    private void initContactAddRemoveButtons() {
        // New item button
        bottomLeftCorner.addComponent(new Button("+",
                new Button.ClickListener() {
                    public void buttonClick(ClickEvent event) {
                        // Add new contact "John Doe" as the first item
                        Object id = ((IndexedContainer) contactList
                                .getContainerDataSource()).addItemAt(0);
                        contactList.getItem(id).getItemProperty("First Name")
                                .setValue("John");
                        contactList.getItem(id).getItemProperty("Last Name")
                                .setValue("Doe");

                        // Select the newly added item and scroll to the item
                        contactList.setValue(id);
                        contactList.setCurrentPageFirstItemId(id);
                    }
                }));

        // Remove item button
        contactRemovalButton = new Button("-", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                contactList.removeItem(contactList.getValue());
                contactList.select(null);
            }
        });
        contactRemovalButton.setVisible(false);
        bottomLeftCorner.addComponent(contactRemovalButton);
    }

    private String[] initAddressList() {
        contactList.setContainerDataSource(addressBookData);
        contactList.setVisibleColumns(visibleCols);
        contactList.setSelectable(true);
        contactList.setImmediate(true);
        contactList.addListener(new Property.ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                Object id = contactList.getValue();
                contactEditor.setItemDataSource(id == null ? null : contactList
                        .getItem(id));
                contactRemovalButton.setVisible(id != null);
            }
        });
        return visibleCols;
    }

    private void initFilteringControls() {
        for (final String pn : visibleCols) {
            final TextField sf = new TextField();
            bottomLeftCorner.addComponent(sf);
            sf.setWidth("100%");
            sf.setInputPrompt(pn);
            sf.setImmediate(true);
            bottomLeftCorner.setExpandRatio(sf, 1);
            sf.addListener(new Property.ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    addressBookData.removeContainerFilters(pn);
                    if (sf.toString().length() > 0 && !pn.equals(sf.toString())) {
                        addressBookData.addContainerFilter(pn, sf.toString(),
                                true, false);
                    }
                    getMainWindow().showNotification(
                            "" + addressBookData.size() + " matches found");
                }
            });
        }
    }

    private static IndexedContainer createDummyData() {

        String[] fnames = { "Peter", "Alice", "Joshua", "Mike", "Olivia",
                "Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene",
                "Lisa", "Marge" };
        String[] lnames = { "Smith", "Gordon", "Simpson", "Brown", "Clavel",
                "Simons", "Verne", "Scott", "Allison", "Gates", "Rowling",
                "Barks", "Ross", "Schneider", "Tate" };

        EntityManager em = EntityUtil.getEntityManager();
     
        em.getTransaction().begin();
        Query query = em.createQuery("select count(*) from Person");
        Number countResult=(Number) query.getSingleResult();
       
        if  (countResult.longValue() < 1) {
            
             // Create dummy data by randomly combining first and last names
             for (int i = 0; i < 1000; i++) {
               Person aPerson = new Person();
                 aPerson.setFirstName(fnames[(int) (fnames.length * Math.random())]);
                 aPerson.setLastName(lnames[(int) (lnames.length * Math.random())]);
                 em.persist(aPerson);
             }
            
        }
        //System.out.println("************ COUNTRESULT:"+  countResult);
        em.getTransaction().commit();
        IndexedContainer personContainer = new IndexedContainer();
       
        for (String p : fields) {
            personContainer.addContainerProperty(p, String.class, "");
        }
        ArrayList<Person> people= (ArrayList<Person>) em.createQuery("from Person").getResultList();
       
        for (Person person : people) {
          //BeanItem<Person> personItem = new BeanItem<Person>(person);
         
          Object id = personContainer.addItem();
            personContainer.getContainerProperty(id, "First Name").setValue(person.getFirstName());
            personContainer.getContainerProperty(id, "Last Name").setValue(person.getLastName());
            personContainer.getContainerProperty(id, "Mobile Phone").setValue(person.getMobilePhone());
    }
       
        em.close();
        return personContainer;
    }

}
TOP

Related Classes of com.example.addressbook.AddressbookApplication

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.