test

Step 1: (SearchView.java)

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

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

public class SearchView extends Panel {
     public SearchView(final AddressBookApplication app) {
         setCaption("Search contacts");
         setSizeFull();
     }
 }

Step 2: (AddressBookApplication.java)

private SearchView searchView = null;

Step2.1: (AddressBookApplication.java)


    private SearchView getSearchView() {
         if (searchView == null) {
             searchView = new SearchView(this);
         }
         return searchView;
     }

Step 3: (AddressBookApplication.java)

search.addListener((Button.ClickListener) this);

Step 4: (AddressBookApplication.java)

final Button source = event.getButton();
if (source == search) {
    showSearchView();
}

Step 5: (AddressBookApplication.java)

private void showSearchView() {
    setMainComponent(getSearchView());
}

Step 6: (PersonList.java)

/*
          * Make table selectable, react immediatedly to user events, and pass events to the
          * controller (our main application)
          */
         setSelectable(true);
         setImmediate(true);
         addListener((Property.ValueChangeListener) app);
         /* We don't want to allow users to de-select a row */
         setNullSelectionAllowed(false);

Step 7: (AddressBookApplication.java)

 public void valueChange(ValueChangeEvent event) {
         Property property = event.getProperty();
         if (property == personList) {
             Item item = personList.getItem(personList.getValue());
             if (item != personForm.getItemDataSource()) {
                 personForm.setItemDataSource(item);
            }
         }
     }

Step 8: (NavigationTree.java)

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

import com.vaadin.demo.tutorial.addressbook.AddressBookApplication;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Tree;

public class NavigationTree extends Tree {
     public static final Object SHOW_ALL = "Show all";
     public static final Object SEARCH = "Search";

    public NavigationTree(AddressBookApplication app) {
         addItem(SHOW_ALL);
         addItem(SEARCH);

        /*
          * We want items to be selectable but do not want the user to be able to
          * de-select an item.
          */
         setSelectable(true);
         setNullSelectionAllowed(false);

        // Make application handle item click events
         addListener((ItemClickListener) app);

    }
 }

Step 9: (AddressBookApplication.java)

public void itemClick(ItemClickEvent event) {
         if(event.getSource() == tree) {
             Object itemId = event.getItemId();
             if (itemId != null) {
                 if (NavigationTree.SHOW_ALL.equals(itemId)) {
                     showListView();
                 } else if (NavigationTree.SEARCH.equals(itemId)) {
                     showSearchView();
                 }
             }
         }
     }

Step 10: (PersonForm.java)

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

import com.vaadin.demo.tutorial.addressbook.AddressBookApplication;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

 public class PersonForm extends Form implements ClickListener {

    private Button save = new Button("Save", (ClickListener) this);
     private Button cancel = new Button("Cancel", (ClickListener) this);
     private Button edit = new Button("Edit", (ClickListener) this);
     private AddressBookApplication app;

    public PersonForm(AddressBookApplication app) {
         this.app = app;

        // Enable buffering so that commit() must be called for the form
         // before input is written to the data. (Form input is not written
         // immediately through to the underlying object.)
         setWriteThrough(false);
        
         HorizontalLayout footer = new HorizontalLayout();
         footer.setSpacing(true);
         footer.addComponent(save);
         footer.addComponent(cancel);
         footer.addComponent(edit);
         footer.setVisible(false);
         
         setFooter(footer);
     }
    
     public void buttonClick(ClickEvent event) {
       
    }

}

Step 11: (PersonForm.java)

public void buttonClick(ClickEvent event) {
         Button source = event.getButton();

        if (source == save) {
             /* If the given input is not valid there is no point in continuing */
             if (!isValid()) {
                 return;
             }
             commit();
             setReadOnly(true);
         } else if (source == cancel) {
             discard();
             setReadOnly(true);
         } else if (source == edit) {
             setReadOnly(false);
         }
     }

Step 12: (PersonForm.java)

@Override
     public void setItemDataSource(Item newDataSource) {
         if (newDataSource != null) {
             List<Object> orderedProperties = Arrays
                     .asList(PersonContainer.NATURAL_COL_ORDER);
             super.setItemDataSource(newDataSource, orderedProperties);

            setReadOnly(true);
             getFooter().setVisible(true);
         } else {
             super.setItemDataSource(null);
             getFooter().setVisible(false);
         }
     }

    @Override
     public void setReadOnly(boolean readOnly) {
         super.setReadOnly(readOnly);
         save.setVisible(!readOnly);
         cancel.setVisible(!readOnly);
         edit.setVisible(readOnly);
     }

Step 13: (AddressBookApplication.java)

private void addNewContanct() {
         showListView();
         personForm.addContact();
     }

Step 14: (PersonForm.java)

private boolean newContactMode = false;
     private Person newPerson = null;

Step 15: (PersonForm.java)

public void addContact() {
         // Create a temporary item for the form
         newPerson = new Person();
         setItemDataSource(new BeanItem(newPerson));
         newContactMode = true;
         setReadOnly(false);
     }

Step 16: (PersonForm.java)

public void buttonClick(ClickEvent event) {
         Button source = event.getButton();

        if (source == save) {
             /* If the given input is not valid there is no point in continuing */
             if (!isValid()) {
                 return;
             }

            commit();
             if (newContactMode) {
                 /* We need to add the new person to the container */
                 Item addedItem = app.getDataSource().addItem(newPerson);
                 /*
                  * We must update the form to use the Item from our datasource
                  * as we are now in edit mode
                  */
                 setItemDataSource(addedItem);

                newContactMode = false;
             }
             setReadOnly(true);
         } else if (source == cancel) {
             if (newContactMode) {
                 newContactMode = false;
                 setItemDataSource(null);
             } else {
                 discard();
             }
             setReadOnly(true);
         } else if (source == edit) {
             setReadOnly(false);
         }
     }

Step 17: (PersonForm.java)

public void setItemDataSource(Item newDataSource) {
         newContactMode = false;
         ...
     }

Step 18: (SearchView.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.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class SearchView extends Panel {

    private TextField tf;
     private NativeSelect fieldToSearch;
     private CheckBox saveSearch;
     private TextField searchName;
     private AddressBookApplication app;

    public SearchView(final AddressBookApplication app) {
         this.app = app;
        
         setCaption("Search contacts");
         setSizeFull();

        /* Use a FormLayout as main layout for this Panel */
         FormLayout formLayout = new FormLayout();
         setContent(formLayout);

        /* Create UI components */
         tf = new TextField("Search term");
         fieldToSearch = new NativeSelect("Field to search");
         saveSearch = new CheckBox("Save search");
         searchName = new TextField("Search name");
         Button search = new Button("Search");

        /* Initialize fieldToSearch */
         for (int i = 0; i < PersonContainer.NATURAL_COL_ORDER.length; i++) {
             fieldToSearch.addItem(PersonContainer.NATURAL_COL_ORDER[i]
);
             fieldToSearch.setItemCaption(PersonContainer.NATURAL_COL_ORDER[i]
,
                     PersonContainer.COL_HEADERS_ENGLISH[i]
);
         }

        fieldToSearch.setValue("lastName");
         fieldToSearch.setNullSelectionAllowed(false);

        /* Initialize save checkbox */
         saveSearch.setValue(true);

        /* Add all the created components to the form */
         addComponent(tf);
         addComponent(fieldToSearch);
         addComponent(saveSearch);
         addComponent(searchName);
         addComponent(search);
     }
}

Step 19: (SearchView.java)

saveSearch.setImmediate(true);
 saveSearch.addListener(new ClickListener() {
     public void buttonClick(ClickEvent event) {
         searchName.setVisible(event.getButton().booleanValue());
     }
 });

Step 20: (SearchFilter.java)

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

import java.io.Serializable;

public class SearchFilter implements Serializable {
 
      private final String term;
      private final Object propertyId;
      private String searchName;
 
      public SearchFilter(Object propertyId, String searchTerm, String name) {
            this.propertyId = propertyId;
            this.term = searchTerm;
            this.searchName = name;
      }
 
      // + getters
}

Step 21: (SearchView.java)

search.addListener(new Button.ClickListener() {
      public void buttonClick(ClickEvent event) {
                performSearch();
      }

});

Step 22: (SearchView.java)

 private void performSearch() {
         String searchTerm = (String) tf.getValue();
         SearchFilter searchFilter = new SearchFilter(fieldToSearch.getValue(),
                 searchTerm, (String) searchName.getValue());
         if (saveSearch.booleanValue()) {
             app.saveSearch(searchFilter);
         }
         app.search(searchFilter);
     }

Step 23: (AddressBookApplication.java)

public void search(SearchFilter searchFilter) {
      // clear previous filters
      getDataSource().removeAllContainerFilters();
      // filter contacts with given filter
      getDataSource().addContainerFilter(searchFilter.getPropertyId(),
                  searchFilter.getTerm(), true, false);
      showListView();
}

Step 24: (AddressBookApplication.java)

public void itemClick(ItemClickEvent event) {
        if (event.getSource() == tree) {
            Object itemId = event.getItemId();
            if (itemId != null) {
                if (NavigationTree.SHOW_ALL.equals(itemId)) {
                    // clear previous filters
                    getDataSource().removeAllContainerFilters();
                    showListView();
                } else if (NavigationTree.SEARCH.equals(itemId)) {
                    showSearchView();
                } else if (itemId instanceof SearchFilter) {
                    search((SearchFilter) itemId);
                }
            }
        }
}

Step 25: (AddressBookApplication.java)

public void saveSearch(SearchFilter searchFilter) {
         tree.addItem(searchFilter);
         tree.setParent(searchFilter, NavigationTree.SEARCH);
         // mark the saved search as a leaf (cannot have children)
         tree.setChildrenAllowed(searchFilter, false);
         // make sure "Search" is expanded
         tree.expandItem(NavigationTree.SEARCH);
         // select the saved search
         tree.setValue(searchFilter);
     }

Step 26: (SearchFilter.java)

 @Override
     public String toString() {
         return getSearchName();
     }

Step 27: (AddressBookApplication.java)

public void itemClick(ItemClickEvent event) {
            if(event.getSource() == tree) {
[...]

      } else if (itemId instanceof SearchFilter) {
                              search((SearchFilter) itemId);
      }
[...]

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////