Chapter 4 v17 - (12 Steps)

Chapter 4

File 1:

(AddressBookApplication.java)


File 2:

(Person.java)


File 3:

(PersonContainer.java)


File 4:

(PersonList.java)


1 - STEPS


Step 1:

(Person.java)

[color=#ff00f6]

package com.vaadin.demo.tutorial.addressbook.data;

import java.io.Serializable;

public class Person implements Serializable {
          private String firstName = "";
          private String lastName = "";
          private String email = "";
          private String phoneNumber = "";
          private String streetAddress = "";
          private Integer postalCode = null;
          private String city = "";
 
// + setters and getters for all fields
}

[/color]

Step 2:

(Person.java)


[color=#0000ff]
public class Person implements Serializable {
          private String firstName = "";
          private String lastName = "";
          private String email = "";
          private String phoneNumber = "";
          private String streetAddress = "";
          private Integer postalCode = null;
          private String city = "";
[/color]
[color=#ff00f6]
 
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public String getStreetAddress() {
        return streetAddress;
    }
    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }
    public Integer getPostalCode() {
        return postalCode;
    }
    public void setPostalCode(Integer postalCode) {
        this.postalCode = postalCode;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
[/color]
[color=#0000ff]
}
[/color]

Step 3:

(PersonContainer.java)

[color=#ff00f6]

package com.vaadin.demo.tutorial.addressbook.data;

import java.io.Serializable;

import com.vaadin.data.util.BeanItemContainer;

public class PersonContainer extends BeanItemContainer<Person> implements
          Serializable {
 
      public PersonContainer() throws InstantiationException,
      IllegalAccessException {
            super(Person.class);
      }
}

[/color]

Step 4:

(AddressBookApplication.java)

[color=#0000ff]

    // Lazyly created ui references
    private ListView listView = null;
    private PersonList personList = null;
    private PersonForm personForm = null;
    private HelpWindow helpWindow = null;
    private SharingOptions sharingOptions = null;
[/color]

[color=#ff00f6]
    private PersonContainer dataSource = PersonContainer.createWithTestData();
[/color]

[color=#0000ff]
    @Override
    public void init() {
        buildMainLayout();
[/color]

Step 5:

(AddressBookApplication.java)


[color=#0000ff]
    private SharingOptions getSharingOptions() {
        if (sharingOptions == null) {
            sharingOptions = new SharingOptions();
        }
        return sharingOptions;
    }
[/color]
[color=#ff00f6]

    public PersonContainer getDataSource() {
        return dataSource;
    }
[/color]

[color=#0000ff]
}
[/color]

Step 6:

(AddressBookApplication.java)


[color=#0000ff]
package com.vaadin.demo.tutorial.addressbook;

import com.vaadin.Application;
[/color]
[color=#ff00f6]
import com.vaadin.demo.tutorial.addressbook.data.PersonContainer;
[/color]
[color=#0000ff]
import com.vaadin.demo.tutorial.addressbook.ui.HelpWindow;
import com.vaadin.demo.tutorial.addressbook.ui.ListView;
import com.vaadin.demo.tutorial.addressbook.ui.NavigationTree;
import com.vaadin.demo.tutorial.addressbook.ui.PersonForm;
import com.vaadin.demo.tutorial.addressbook.ui.PersonList;
import com.vaadin.demo.tutorial.addressbook.ui.SharingOptions;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
[/color]

Step 7:

(PersonList.java)


[color=#0000ff]
public class PersonList extends Table {
      public PersonList(
[/color][color=#ff00f6]
[color=#ff00f6]
AddressBookApplication app
[/color]
[/color][color=#0000ff]
) {
            setSizeFull();
[/color]
            [color=#ff00f6]
setContainerDataSource(app.getDataSource());
[/color]
      [color=#0000ff]
}
}
[/color]

Step 8:

(PersonContainer.java)


[color=#0000ff]
public class PersonContainer extends BeanItemContainer<Person> implements
          Serializable {

[/color]
[color=#ff00f6]
     public static final Object[] NATURAL_COL_ORDER = new Object[]
 {
             "firstName", "lastName", "email", "phoneNumber", "streetAddress",
             "postalCode", "city" };
     public static final String[] COL_HEADERS_ENGLISH = new String[]
 {
             "First name", "Last name", "Email", "Phone number",
             "Street Address", "Postal Code", "City" };
[/color]

 
 [color=#0000ff]
     public PersonContainer() throws InstantiationException,
      IllegalAccessException {
            super(Person.class);
      }
}
[/color]

Step 9:

(PersonContainer.java)


[color=#0000ff]
    public PersonContainer() throws InstantiationException,
            IllegalAccessException {
        super(Person.class);
    }
[/color]

[color=#ff00f6]
    public static PersonContainer createWithTestData() {
        final String[] fnames = { "Peter", "Alice", "Joshua", "Mike", "Olivia",
                "Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene",
                "Lisa", "Marge" };
        final String[] lnames = { "Smith", "Gordon", "Simpson", "Brown",
                "Clavel", "Simons", "Verne", "Scott", "Allison", "Gates",
                "Rowling", "Barks", "Ross", "Schneider", "Tate" };
        final String cities[] = { "Amsterdam", "Berlin", "Helsinki",
                "Hong Kong", "London", "Luxemburg", "New York", "Oslo",
                "Paris", "Rome", "Stockholm", "Tokyo", "Turku" };
        final String streets[] = { "4215 Blandit Av.", "452-8121 Sem Ave",
                "279-4475 Tellus Road", "4062 Libero. Av.", "7081 Pede. Ave",
                "6800 Aliquet St.", "P.O. Box 298, 9401 Mauris St.",
                "161-7279 Augue Ave", "P.O. Box 496, 1390 Sagittis. Rd.",
                "448-8295 Mi Avenue", "6419 Non Av.",
                "659-2538 Elementum Street", "2205 Quis St.",
                "252-5213 Tincidunt St.", "P.O. Box 175, 4049 Adipiscing Rd.",
                "3217 Nam Ave", "P.O. Box 859, 7661 Auctor St.",
                "2873 Nonummy Av.", "7342 Mi, Avenue",
                "539-3914 Dignissim. Rd.", "539-3675 Magna Avenue",
                "Ap #357-5640 Pharetra Avenue", "416-2983 Posuere Rd.",
                "141-1287 Adipiscing Avenue", "Ap #781-3145 Gravida St.",
                "6897 Suscipit Rd.", "8336 Purus Avenue", "2603 Bibendum. Av.",
                "2870 Vestibulum St.", "Ap #722 Aenean Avenue",
                "446-968 Augue Ave", "1141 Ultricies Street",
                "Ap #992-5769 Nunc Street", "6690 Porttitor Avenue",
                "Ap #105-1700 Risus Street",
                "P.O. Box 532, 3225 Lacus. Avenue", "736 Metus Street",
                "414-1417 Fringilla Street", "Ap #183-928 Scelerisque Road",
                "561-9262 Iaculis Avenue" };
        PersonContainer c = null;
        Random r = new Random(0);
        try {
            c = new PersonContainer();
            for (int i = 0; i < 100; i++) {
                Person p = new Person();
                p.setFirstName(fnames[r.nextInt(fnames.length)]
);
                p.setLastName(lnames[r.nextInt(lnames.length)]
);
                p.setCity(cities[r.nextInt(cities.length)]
);
                p.setEmail(p.getFirstName().toLowerCase() + "."
                        + p.getLastName().toLowerCase() + "@vaadin.com");
                p.setPhoneNumber("+358 02 555 " + r.nextInt(10) + r.nextInt(10)
                        + r.nextInt(10) + r.nextInt(10));
                int n = r.nextInt(100000);
                if (n < 10000) {
                    n += 10000;
                }
                p.setPostalCode(n);
                p.setStreetAddress(streets[r.nextInt(streets.length)]
);
                c.addItem(p);
            }
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return c;
    }
[/color]

[color=#0000ff]
}
[/color]

Step 10:

(PersonContainer.java)


[color=#0000ff]
package com.vaadin.demo.tutorial.addressbook.data;

import java.io.Serializable;
[/color]
[color=#ff00f6]
import java.util.Random;
[/color]
[color=#0000ff]

import com.vaadin.data.util.BeanItemContainer;

public class PersonContainer extends BeanItemContainer<Person> implements
        Serializable {
[/color]

Step 11:

(PersonList.java)


[color=#0000ff]
public class PersonList extends Table {
    public PersonList(AddressBookApplication app) {
        setSizeFull();
        setContainerDataSource(app.getDataSource());
[/color]

[color=#ff00f6]
        setVisibleColumns(PersonContainer.NATURAL_COL_ORDER);
        setColumnHeaders(PersonContainer.COL_HEADERS_ENGLISH);
[/color]
[color=#0000ff]
    }

}
[/color]

Step 12:

(PersonList.java)


[color=#0000ff]
package com.vaadin.demo.tutorial.addressbook.ui;
[/color]

[color=#ff00f6]
import com.vaadin.demo.tutorial.addressbook.AddressBookApplication;
import com.vaadin.demo.tutorial.addressbook.data.PersonContainer;
[/color]
[color=#0000ff]
import com.vaadin.ui.Table;

public class PersonList extends Table {
[/color]

Aquesta linea de codi et pot donar la tabarra. Et deixo a continuacio la bona.

import com.vaadin.demo.tutorial.addressbook.AddressBookApplication;

import com.example.addressbook.AddressbookApplication;


2 - FINALS


File 1:

(AddressBookApplication.java)


FINAL

(AddressBookApplication.java)


package com.vaadin.demo.tutorial.addressbook;

import com.vaadin.Application;
import com.vaadin.demo.tutorial.addressbook.data.PersonContainer;
import com.vaadin.demo.tutorial.addressbook.ui.HelpWindow;
import com.vaadin.demo.tutorial.addressbook.ui.ListView;
import com.vaadin.demo.tutorial.addressbook.ui.NavigationTree;
import com.vaadin.demo.tutorial.addressbook.ui.PersonForm;
import com.vaadin.demo.tutorial.addressbook.ui.PersonList;
import com.vaadin.demo.tutorial.addressbook.ui.SharingOptions;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class AddressBookApplication extends Application {

    private NavigationTree tree = new NavigationTree();

    private Button newContact = new Button("Add contact");
    private Button search = new Button("Search");
    private Button share = new Button("Share");
    private Button help = new Button("Help");
    private SplitPanel horizontalSplit = new SplitPanel(
            SplitPanel.ORIENTATION_HORIZONTAL);

    // Lazyly created ui references
    private ListView listView = null;
    private PersonList personList = null;
    private PersonForm personForm = null;
    private HelpWindow helpWindow = null;
    private SharingOptions sharingOptions = null;

    private PersonContainer dataSource = PersonContainer.createWithTestData();

    @Override
    public void init() {
        buildMainLayout();
        setMainComponent(getListView());
    }

    private void buildMainLayout() {
        setMainWindow(new Window("Address Book Demo application"));

        VerticalLayout layout = new VerticalLayout();
        layout.setSizeFull();

        layout.addComponent(createToolbar());
        layout.addComponent(horizontalSplit);
        layout.setExpandRatio(horizontalSplit, 1);

        horizontalSplit.setSplitPosition(200, SplitPanel.UNITS_PIXELS);
        horizontalSplit.setFirstComponent(tree);

        getMainWindow().setContent(layout);
    }

    private HorizontalLayout createToolbar() {
        HorizontalLayout lo = new HorizontalLayout();
        lo.addComponent(newContact);
        lo.addComponent(search);
        lo.addComponent(share);
        lo.addComponent(help);

        return lo;
    }

    private void setMainComponent(Component c) {
        horizontalSplit.setSecondComponent(c);
    }

    /*
     * View getters exist so we can lazily generate the views, resulting in
     * faster application startup time.
     */
    private ListView getListView() {
        if (listView == null) {
            personList = new PersonList(this);
            personForm = new PersonForm();
            listView = new ListView(personList, personForm);
        }
        return listView;
    }

    private HelpWindow getHelpWindow() {
        if (helpWindow == null) {
            helpWindow = new HelpWindow();
        }
        return helpWindow;
    }

    private SharingOptions getSharingOptions() {
        if (sharingOptions == null) {
            sharingOptions = new SharingOptions();
        }
        return sharingOptions;
    }

    public PersonContainer getDataSource() {
        return dataSource;
    }

}

File 2:

(Person.java)


FINAL

(Person.java)


package com.vaadin.demo.tutorial.addressbook.data;

import java.io.Serializable;

public class Person implements Serializable {
    private String firstName = "";
    private String lastName = "";
    private String email = "";
    private String phoneNumber = "";
    private String streetAddress = "";
    private Integer postalCode = null;
    private String city = "";

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName
     *            the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName
     *            the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email
     *            the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * @param phoneNumber
     *            the phoneNumber to set
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * @return the streetAddress
     */
    public String getStreetAddress() {
        return streetAddress;
    }

    /**
     * @param streetAddress
     *            the streetAddress to set
     */
    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }

    /**
     * @return the postalCode
     */
    public Integer getPostalCode() {
        return postalCode;
    }

    /**
     * @param postalCode
     *            the postalCode to set
     */
    public void setPostalCode(Integer postalCode) {
        this.postalCode = postalCode;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city
     *            the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

}

File 3:

(PersonContainer.java)


FINAL

(PersonContainer.java)


package com.vaadin.demo.tutorial.addressbook.data;

import java.io.Serializable;
import java.util.Random;

import com.vaadin.data.util.BeanItemContainer;

public class PersonContainer extends BeanItemContainer<Person> implements
        Serializable {

    /**
     * Natural property order for Person bean. Used in tables and forms.
     */
    public static final Object[] NATURAL_COL_ORDER = new Object[]
 {
            "firstName", "lastName", "email", "phoneNumber", "streetAddress",
            "postalCode", "city" };
    
    /**
     * "Human readable" captions for properties in same order as in
     * NATURAL_COL_ORDER.
     */
    public static final String[] COL_HEADERS_ENGLISH = new String[]
 {
            "First name", "Last name", "Email", "Phone number",
            "Street Address", "Postal Code", "City" };

    public PersonContainer() throws InstantiationException,
            IllegalAccessException {
        super(Person.class);
    }

    public static PersonContainer createWithTestData() {
        final String[] fnames = { "Peter", "Alice", "Joshua", "Mike", "Olivia",
                "Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene",
                "Lisa", "Marge" };
        final String[] lnames = { "Smith", "Gordon", "Simpson", "Brown",
                "Clavel", "Simons", "Verne", "Scott", "Allison", "Gates",
                "Rowling", "Barks", "Ross", "Schneider", "Tate" };
        final String cities[] = { "Amsterdam", "Berlin", "Helsinki",
                "Hong Kong", "London", "Luxemburg", "New York", "Oslo",
                "Paris", "Rome", "Stockholm", "Tokyo", "Turku" };
        final String streets[] = { "4215 Blandit Av.", "452-8121 Sem Ave",
                "279-4475 Tellus Road", "4062 Libero. Av.", "7081 Pede. Ave",
                "6800 Aliquet St.", "P.O. Box 298, 9401 Mauris St.",
                "161-7279 Augue Ave", "P.O. Box 496, 1390 Sagittis. Rd.",
                "448-8295 Mi Avenue", "6419 Non Av.",
                "659-2538 Elementum Street", "2205 Quis St.",
                "252-5213 Tincidunt St.", "P.O. Box 175, 4049 Adipiscing Rd.",
                "3217 Nam Ave", "P.O. Box 859, 7661 Auctor St.",
                "2873 Nonummy Av.", "7342 Mi, Avenue",
                "539-3914 Dignissim. Rd.", "539-3675 Magna Avenue",
                "Ap #357-5640 Pharetra Avenue", "416-2983 Posuere Rd.",
                "141-1287 Adipiscing Avenue", "Ap #781-3145 Gravida St.",
                "6897 Suscipit Rd.", "8336 Purus Avenue", "2603 Bibendum. Av.",
                "2870 Vestibulum St.", "Ap #722 Aenean Avenue",
                "446-968 Augue Ave", "1141 Ultricies Street",
                "Ap #992-5769 Nunc Street", "6690 Porttitor Avenue",
                "Ap #105-1700 Risus Street",
                "P.O. Box 532, 3225 Lacus. Avenue", "736 Metus Street",
                "414-1417 Fringilla Street", "Ap #183-928 Scelerisque Road",
                "561-9262 Iaculis Avenue" };
        PersonContainer c = null;
        Random r = new Random(0);
        try {
            c = new PersonContainer();
            for (int i = 0; i < 100; i++) {
                Person p = new Person();
                p.setFirstName(fnames[r.nextInt(fnames.length)]
);
                p.setLastName(lnames[r.nextInt(lnames.length)]
);
                p.setCity(cities[r.nextInt(cities.length)]
);
                p.setEmail(p.getFirstName().toLowerCase() + "."
                        + p.getLastName().toLowerCase() + "@vaadin.com");
                p.setPhoneNumber("+358 02 555 " + r.nextInt(10) + r.nextInt(10)
                        + r.nextInt(10) + r.nextInt(10));
                int n = r.nextInt(100000);
                if (n < 10000) {
                    n += 10000;
                }
                p.setPostalCode(n);
                p.setStreetAddress(streets[r.nextInt(streets.length)]
);
                c.addItem(p);
            }
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return c;
    }

}

File 4:

(PersonList.java)


FINAL

(PersonList.java)


package com.vaadin.demo.tutorial.addressbook.ui;

import com.vaadin.demo.tutorial.addressbook.AddressBookApplication;
import com.vaadin.demo.tutorial.addressbook.data.PersonContainer;
import com.vaadin.ui.Table;

public class PersonList extends Table {
    public PersonList(AddressBookApplication app) {
        setSizeFull();
        setContainerDataSource(app.getDataSource());

        setVisibleColumns(PersonContainer.NATURAL_COL_ORDER);
        setColumnHeaders(PersonContainer.COL_HEADERS_ENGLISH);
    }

}

3 - INICIALS


File 1:

(AddressBookApplication.java)


INICIO

[color=#0000ff]

package com.vaadin.demo.tutorial.addressbook;

import com.vaadin.Application;
import com.vaadin.demo.tutorial.addressbook.ui.HelpWindow;
import com.vaadin.demo.tutorial.addressbook.ui.ListView;
import com.vaadin.demo.tutorial.addressbook.ui.NavigationTree;
import com.vaadin.demo.tutorial.addressbook.ui.PersonForm;
import com.vaadin.demo.tutorial.addressbook.ui.PersonList;
import com.vaadin.demo.tutorial.addressbook.ui.SharingOptions;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class AddressBookApplication extends Application {

	private NavigationTree tree = new NavigationTree();

	private Button newContact = new Button("Add contact");
	private Button search = new Button("Search");
	private Button share = new Button("Share");
	private Button help = new Button("Help");
	private SplitPanel horizontalSplit = new SplitPanel(
			SplitPanel.ORIENTATION_HORIZONTAL);

	// Lazyly created ui references
	private ListView listView = null;
	private PersonList personList = null;
	private PersonForm personForm = null;
	private HelpWindow helpWindow = null;
	private SharingOptions sharingOptions = null;

	@Override
	public void init() {
		buildMainLayout();
		setMainComponent(getListView());

		// uncomment following lines to test other views and windows
		// getMainWindow().addWindow(getHelpWindow());
		// getMainWindow().addWindow(getSharingOptions());
	}

	private void buildMainLayout() {
		setMainWindow(new Window("Address Book Demo application"));

		VerticalLayout layout = new VerticalLayout();
		layout.setSizeFull();

		layout.addComponent(createToolbar());
		layout.addComponent(horizontalSplit);
		layout.setExpandRatio(horizontalSplit, 1);

		horizontalSplit.setSplitPosition(200, SplitPanel.UNITS_PIXELS);
		horizontalSplit.setFirstComponent(tree);

		getMainWindow().setContent(layout);
	}

	private HorizontalLayout createToolbar() {
		HorizontalLayout lo = new HorizontalLayout();
		lo.addComponent(newContact);
		lo.addComponent(search);
		lo.addComponent(share);
		lo.addComponent(help);

		return lo;
	}

	private void setMainComponent(Component c) {
		horizontalSplit.setSecondComponent(c);
	}

	/*
	 * View getters exist so we can lazily generate the views, resulting in
	 * faster application startup time.
	 */
	private ListView getListView() {
		if (listView == null) {
			personList = new PersonList();
			personForm = new PersonForm();
			listView = new ListView(personList, personForm);
		}
		return listView;
	}

	private HelpWindow getHelpWindow() {
		if (helpWindow == null) {
			helpWindow = new HelpWindow();
		}
		return helpWindow;
	}

	private SharingOptions getSharingOptions() {
		if (sharingOptions == null) {
			sharingOptions = new SharingOptions();
		}
		return sharingOptions;
	}
}


[/color]

File 2:

(Person.java)


INICIO

[color=#0000ff]

NUEVO

[/color]

File 3:

(PersonContainer.java)


INICIO

[color=#0000ff]

NUEVO

[/color]

File 4:

(PersonList.java)


INICIO

[color=#0000ff]

package com.vaadin.demo.tutorial.addressbook.ui;

import com.vaadin.ui.Table;

public class PersonList extends Table {
	public PersonList() {
		[color=#ff0000]
// create some dummy data
		addContainerProperty("First Name", String.class, "Mark");
		addContainerProperty("Last Name", String.class, "Smith");
		addItem();
		addItem();
[/color]
		setSizeFull();
	}
}

[/color]