You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleAddressBook.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.vaadin.demo;
  2. import com.vaadin.Application;
  3. import com.vaadin.data.Property;
  4. import com.vaadin.data.Property.ValueChangeEvent;
  5. import com.vaadin.data.util.IndexedContainer;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Form;
  8. import com.vaadin.ui.HorizontalLayout;
  9. import com.vaadin.ui.SplitPanel;
  10. import com.vaadin.ui.Table;
  11. import com.vaadin.ui.TextField;
  12. import com.vaadin.ui.VerticalLayout;
  13. import com.vaadin.ui.Window;
  14. import com.vaadin.ui.Button.ClickEvent;
  15. public class SimpleAddressBook extends Application {
  16. private static String[] fields = { "First Name", "Last Name", "Company",
  17. "Mobile Phone", "Work Phone", "Home Phone", "Work Email",
  18. "Home Email", "Street", "Zip", "City", "State", "Country" };
  19. private static String[] visibleCols = new String[] { "Last Name", "First Name",
  20. "Company" };
  21. private Table contactList = new Table();
  22. private Form contactEditor = new Form();
  23. private HorizontalLayout bottomLeftCorner = new HorizontalLayout();
  24. private Button contactRemovalButton;
  25. private IndexedContainer addressBookData = createDummyData();
  26. public void init() {
  27. initLayout();
  28. initContactAddRemoveButtons();
  29. initAddressList();
  30. initFilteringControls();
  31. }
  32. private void initLayout() {
  33. SplitPanel splitPanel = new SplitPanel(
  34. SplitPanel.ORIENTATION_HORIZONTAL);
  35. setMainWindow(new Window("Address Book", splitPanel));
  36. VerticalLayout left = new VerticalLayout();
  37. left.setSizeFull();
  38. left.addComponent(contactList);
  39. contactList.setSizeFull();
  40. left.setExpandRatio(contactList, 1);
  41. splitPanel.addComponent(left);
  42. splitPanel.addComponent(contactEditor);
  43. contactEditor.setSizeFull();
  44. contactEditor.getLayout().setMargin(true);
  45. contactEditor.setImmediate(true);
  46. bottomLeftCorner.setWidth("100%");
  47. left.addComponent(bottomLeftCorner);
  48. }
  49. private void initContactAddRemoveButtons() {
  50. // New item button
  51. bottomLeftCorner.addComponent(new Button("+", new Button.ClickListener() {
  52. public void buttonClick(ClickEvent event) {
  53. Object id = contactList.addItem();
  54. contactList.setValue(id);
  55. }
  56. }));
  57. // Remove item button
  58. contactRemovalButton = new Button("-", new Button.ClickListener() {
  59. public void buttonClick(ClickEvent event) {
  60. contactList.removeItem(contactList.getValue());
  61. contactList.select(null);
  62. }
  63. });
  64. contactRemovalButton.setVisible(false);
  65. bottomLeftCorner.addComponent(contactRemovalButton);
  66. }
  67. private String[] initAddressList() {
  68. contactList.setContainerDataSource(addressBookData);
  69. contactList.setVisibleColumns(visibleCols);
  70. contactList.setSelectable(true);
  71. contactList.setImmediate(true);
  72. contactList.addListener(new Property.ValueChangeListener() {
  73. public void valueChange(ValueChangeEvent event) {
  74. Object id = contactList.getValue();
  75. contactEditor.setItemDataSource(id == null ? null : contactList
  76. .getItem(id));
  77. contactRemovalButton.setVisible(id != null);
  78. }
  79. });
  80. return visibleCols;
  81. }
  82. private void initFilteringControls() {
  83. for (final String pn : visibleCols) {
  84. final TextField sf = new TextField();
  85. bottomLeftCorner.addComponent(sf);
  86. sf.setWidth("100%");
  87. sf.setValue(pn);
  88. sf.setImmediate(true);
  89. bottomLeftCorner.setExpandRatio(sf, 1);
  90. sf.addListener(new Property.ValueChangeListener() {
  91. public void valueChange(ValueChangeEvent event) {
  92. addressBookData.removeContainerFilters(pn);
  93. if (sf.toString().length() > 0 && !pn.equals(sf.toString()))
  94. addressBookData.addContainerFilter(pn, sf.toString(),
  95. true, false);
  96. getMainWindow().showNotification(
  97. "" + addressBookData.size() + " matches found");
  98. }
  99. });
  100. }
  101. }
  102. private static IndexedContainer createDummyData() {
  103. String[] fnames = { "Peter", "Alice", "Joshua", "Mike", "Olivia",
  104. "Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene",
  105. "Lisa", "Marge" };
  106. String[] lnames = { "Smith", "Gordon", "Simpson", "Brown", "Clavel",
  107. "Simons", "Verne", "Scott", "Allison", "Gates", "Rowling",
  108. "Barks", "Ross", "Schneider", "Tate" };
  109. IndexedContainer ic = new IndexedContainer();
  110. for (String p : fields)
  111. ic.addContainerProperty(p, String.class, "");
  112. for (int i = 0; i < 1000; i++) {
  113. Object id = ic.addItem();
  114. ic.getContainerProperty(id, "First Name").setValue(
  115. fnames[(int) (fnames.length * Math.random())]);
  116. ic.getContainerProperty(id, "Last Name").setValue(
  117. lnames[(int) (lnames.length * Math.random())]);
  118. }
  119. return ic;
  120. }
  121. }