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.

CreatingAMasterDetailsViewForEditingPersons.asciidoc 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. ---
  2. title: Creating A Master Details View For Editing Persons
  3. order: 28
  4. layout: page
  5. ---
  6. [[creating-a-master-details-view-for-editing-persons]]
  7. = Creating a master details view for editing persons
  8. [[set-up]]
  9. Set-up
  10. ~~~~~~
  11. In this tutorial we go through a standard use case where you have a bean
  12. and a backend ready with create, read, update and delete capabilities on
  13. that bean. You want to create a view where you can view all the beans
  14. and edit them. This example is an address book where you edit person
  15. information. The bean and the backend that we're going to use looks like
  16. this:
  17. [[person]]
  18. Person
  19. ^^^^^^
  20. [source,java]
  21. ....
  22. public class Person {
  23. private int id = -1;
  24. private String firstName = "";
  25. private String lastName = "";
  26. private Address address = new Address();
  27. private String phoneNumber = "";
  28. private String email = "";
  29. private Date dateOfBirth = null;
  30. private String comments = "";
  31. ....
  32. [[ibackend]]
  33. IBackend
  34. ^^^^^^^^
  35. [source,java]
  36. ....
  37. public interface IBackend {
  38. public List<Person> getPersons();
  39. public void storePerson(Person person);
  40. public void deletePerson(Person person);
  41. }
  42. ....
  43. The view will contain a table, with all the persons in it, and a form
  44. for editing a single person. Additionally the table will have buttons
  45. too add or remove persons and the form will have buttons to save and
  46. discard changes. The UI wireframe looks like this:
  47. image:img/master%20detail%20wireframe.jpg[Master detail UI wireframe]
  48. [[building-the-basic-ui]]
  49. Building the basic UI
  50. ~~~~~~~~~~~~~~~~~~~~~
  51. We start off with creating a basic UIfor our application
  52. [source,java]
  53. ....
  54. public class AddressFormsUI extends UI {
  55. @Override
  56. protected void init(VaadinRequest request) {
  57. VerticalLayout mainLayout = new VerticalLayout();
  58. mainLayout.setSpacing(true);
  59. mainLayout.setMargin(true);
  60. mainLayout.addComponent(new Label("Hello Vaadiners!"));
  61. setContent(mainLayout);
  62. }
  63. }
  64. ....
  65. The first things that we want to add to it is the table and the form.
  66. The table should be selectable and immediate so that we're able to pass
  67. person objects from it to the form. I will create all the fields for our
  68. person editor by hand to get more flexibility in how the fields will be
  69. built and laid out. You could also let Vaadin `FieldGroup` take care of
  70. creating the standard fields with the `buildAndBind` -methods if you don't
  71. need to customize them.
  72. [source,java]
  73. ....
  74. package com.example.addressforms;
  75. import com.vaadin.server.VaadinRequest;
  76. import com.vaadin.ui.Component;
  77. import com.vaadin.ui.DateField;
  78. import com.vaadin.ui.GridLayout;
  79. import com.vaadin.ui.UI;
  80. import com.vaadin.ui.Table;
  81. import com.vaadin.ui.TextArea;
  82. import com.vaadin.ui.TextField;
  83. import com.vaadin.ui.VerticalLayout;
  84. public class AddressFormsUI extends UI {
  85. private GridLayout form;
  86. private Table table;
  87. @Override
  88. protected void init(VaadinRequest request) {
  89. VerticalLayout mainLayout = new VerticalLayout();
  90. mainLayout.setSpacing(true);
  91. mainLayout.setMargin(true);
  92. mainLayout.addComponent(buildTable());
  93. mainLayout.addComponent(buildForm());
  94. setContent(mainLayout);
  95. }
  96. private Component buildTable() {
  97. table = new Table(null);
  98. table.setWidth("500px");
  99. table.setSelectable(true);
  100. table.setImmediate(true);
  101. return table;
  102. }
  103. private Component buildForm() {
  104. form = new GridLayout(2, 3);
  105. TextField firstName = new TextField("First name:");
  106. TextField lastName = new TextField("Last name:");
  107. TextField phoneNumber = new TextField("Phone Number:");
  108. TextField email = new TextField("E-mail address:");
  109. DateField dateOfBirth = new DateField("Date of birth:");
  110. TextArea comments = new TextArea("Comments:");
  111. form.addComponent(firstName);
  112. form.addComponent(lastName);
  113. form.addComponent(phoneNumber);
  114. form.addComponent(email);
  115. form.addComponent(dateOfBirth);
  116. form.addComponent(comments);
  117. return form;
  118. }
  119. }
  120. ....
  121. image:img/table%20and%20form.png[Address form]
  122. We also want the add, remove, save and discard buttons so let's create
  123. them as well. I've positioned the add and remove above the table and
  124. save and discard below the form.
  125. [source,java]
  126. ....
  127. private GridLayout form;
  128. private HorizontalLayout tableControls;
  129. private Table table;
  130. private HorizontalLayout formControls;
  131. @Override
  132. protected void init(VaadinRequest request) {
  133. VerticalLayout mainLayout = new VerticalLayout();
  134. mainLayout.setSpacing(true);
  135. mainLayout.setMargin(true);
  136. mainLayout.addComponent(buildTableControls());
  137. mainLayout.addComponent(buildTable());
  138. mainLayout.addComponent(buildForm());
  139. mainLayout.addComponent(buildFormControls());
  140. setContent(mainLayout);
  141. }
  142. ...
  143. private Component buildTableControls() {
  144. tableControls = new HorizontalLayout();
  145. Button add = new Button("Add");
  146. Button delete = new Button("Delete");
  147. tableControls.addComponent(add);
  148. tableControls.addComponent(delete);
  149. return tableControls;
  150. }
  151. private Component buildFormControls() {
  152. formControls = new HorizontalLayout();
  153. Button save = new Button("Save");
  154. Button discard = new Button("Discard");
  155. formControls.addComponent(save);
  156. formControls.addComponent(discard);
  157. return formControls;
  158. }
  159. ....
  160. The buttons doesn't do anything yet but we have all the components that
  161. we need in the view now.
  162. image:img/buttons%20added.png[Address form with add, delete, save and discard buttons]
  163. [[connecting-the-backend-to-the-view]]
  164. Connecting the backend to the view
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. The backend reference is store as a field so that all methods have
  167. access to it.
  168. [source,java]
  169. ....
  170. ...
  171. private IBackend backend;
  172. @Override
  173. protected void init(VaadinRequest request) {
  174. backend = new Backend();
  175. ...
  176. ....
  177. Then we have to build a container for the table. I will do it in a
  178. separate method from the table building so that it can be rebuilt for
  179. refreshing the table after the initial rendering. We call this method
  180. once in the initial rendering as well on every button click that
  181. modifies the list of persons. A good choice of container in this case is
  182. the `BeanItemContainer` where we specify to the table which columns we
  183. want to show, and sort the table based on the name.
  184. [source,java]
  185. ....
  186. ...
  187. private Component buildTable() {
  188. table = new Table(null);
  189. table.setSelectable(true);
  190. table.setImmediate(true);
  191. updateTableData();
  192. return table;
  193. }
  194. ...
  195. private void updateTableData() {
  196. List<Person> persons = backend.getPersons();
  197. BeanItemContainer<Person> container = new BeanItemContainer<Person>(
  198. Person.class, persons);
  199. table.setContainerDataSource(container);
  200. table.setVisibleColumns(new String[] { "firstName", "lastName",
  201. "phoneNumber", "email", "dateOfBirth" });
  202. table.setColumnHeaders(new String[] { "First name", "Last name",
  203. "Phone number", "E-mail address", "Date of birth" });
  204. table.sort(new Object[] { "firstName", "lastName" }, new boolean[] {
  205. true, true });
  206. }
  207. ...
  208. ....
  209. To get the data from the selected person's data into the fields, and the
  210. changes back into the bean, we will use a FieldGroup. The `FieldGroup`
  211. should be defined as class variable and it should bind the fields that
  212. is initialized in `buildForm()`.
  213. [source,java]
  214. ....
  215. ...
  216. private FieldGroup fieldGroup = new FieldGroup();
  217. ...
  218. private Component buildForm() {
  219. form = new GridLayout(2, 3);
  220. TextField firstName = new TextField("First name:");
  221. TextField lastName = new TextField("Last name:");
  222. TextField phoneNumber = new TextField("Phone Number:");
  223. TextField email = new TextField("E-mail address:");
  224. DateField dateOfBirth = new DateField("Date of birth:");
  225. TextArea comments = new TextArea("Comments:");
  226. fieldGroup.bind(firstName, "firstName");
  227. fieldGroup.bind(lastName, "lastName");
  228. fieldGroup.bind(phoneNumber, "phoneNumber");
  229. fieldGroup.bind(email, "email");
  230. fieldGroup.bind(dateOfBirth, "dateOfBirth");
  231. fieldGroup.bind(comments, "comments");
  232. form.addComponent(firstName);
  233. form.addComponent(lastName);
  234. form.addComponent(phoneNumber);
  235. form.addComponent(email);
  236. form.addComponent(dateOfBirth);
  237. form.addComponent(comments);
  238. return form;
  239. }
  240. ....
  241. Additionally the table requires a value change listener and the
  242. currently selected person in the table has to be passed to the
  243. `FieldGroup`.
  244. [source,java]
  245. ....
  246. private Component buildTable() {
  247. ...
  248. table.addValueChangeListener(new ValueChangeListener() {
  249. public void valueChange(ValueChangeEvent event) {
  250. editPerson((Person) table.getValue());
  251. }
  252. });
  253. ...
  254. }
  255. ...
  256. private void editPerson(Person person) {
  257. if (person == null) {
  258. person = new Person();
  259. }
  260. BeanItem<Person> item = new BeanItem<Person>(person);
  261. fieldGroup.setItemDataSource(item);
  262. }
  263. ....
  264. [[putting-the-buttons-in-use]]
  265. Putting the buttons in use
  266. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  267. Last thing we have to do is implement all the buttons that we have in
  268. the application. Add should create a new Person object and give it to
  269. the form. Delete should tell the backend to remove the selected person
  270. and update the table. Save should store the changes into the bean and
  271. the bean into the backend and update the table. Discard should reset the
  272. form.
  273. [source,java]
  274. ....
  275. private Component buildTableControls() {
  276. tableControls = new HorizontalLayout();
  277. Button add = new Button("Add", new ClickListener() {
  278. public void buttonClick(ClickEvent event) {
  279. editPerson(new Person());
  280. }
  281. });
  282. Button delete = new Button("Delete", new ClickListener() {
  283. public void buttonClick(ClickEvent event) {
  284. backend.deletePerson((Person) table.getValue());
  285. updateTableData();
  286. }
  287. });
  288. tableControls.addComponent(add);
  289. tableControls.addComponent(delete);
  290. return tableControls;
  291. }
  292. private Component buildFormControls() {
  293. formControls = new HorizontalLayout();
  294. Button save = new Button("Save", new ClickListener() {
  295. public void buttonClick(ClickEvent event) {
  296. try {
  297. fieldGroup.commit();
  298. backend.storePerson(((BeanItem<Person>) fieldGroup
  299. .getItemDataSource()).getBean());
  300. updateTableData();
  301. editPerson(null);
  302. } catch (CommitException e) {
  303. e.printStackTrace();
  304. }
  305. }
  306. });
  307. Button discard = new Button("Discard", new ClickListener() {
  308. public void buttonClick(ClickEvent event) {
  309. fieldGroup.discard();
  310. }
  311. });
  312. formControls.addComponent(save);
  313. formControls.addComponent(discard);
  314. return formControls;
  315. }
  316. ....
  317. image:img/database%20connected.png[Form with database connected]
  318. That's it! Now you have a full working CRUD view with total control over
  319. the components and layouts. A little theming and layout adjustments and
  320. it is ready for production.
  321. You might have noticed that the person bean contains a reference to
  322. another bean, a address, which is not editable here. The tutorial
  323. link:CreatingACustomFieldForEditingTheAddressOfAPerson.asciidoc[Creating a custom field for editing the address of a person] goes
  324. through on how to edit beans within beans with a `CustomField`, which can
  325. be used directly as a field for the `FieldGroup`.