forked from yeshodha02/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressbookUI.java
More file actions
149 lines (131 loc) · 5.45 KB
/
AddressbookUI.java
File metadata and controls
149 lines (131 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.edurekademo.tutorial.addressbook;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.edurekademo.tutorial.addressbook.backend.Contact;
import com.edurekademo.tutorial.addressbook.backend.ContactService;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.data.util.BeanItemContainer;
import com.vaadin.v7.ui.Grid;
import com.vaadin.v7.ui.TextField;
/* User Interface written in Java.
*
* Define the user interface shown on the Vaadin generated web page by extending the UI class.
* By default, a new UI instance is automatically created when the page is loaded. To reuse
* the same instance, add @PreserveOnRefresh.
*/
@Title("Addressbook")
@Theme("valo")
@Widgetset("com.vaadin.v7.Vaadin7WidgetSet")
public class AddressbookUI extends UI {
/*
* Hundreds of widgets. Vaadin's user interface components are just Java
* objects that encapsulate and handle cross-browser support and
* client-server communication. The default Vaadin components are in the
* com.vaadin.ui package and there are over 500 more in
* vaadin.com/directory.
*/
TextField filter = new TextField();
Grid contactList = new Grid();
Button newContact = new Button("New contact");
// ContactForm is an example of a custom component class
ContactForm contactForm = new ContactForm();
// ContactService is a in-memory mock DAO that mimics
// a real-world datasource. Typically implemented for
// example as EJB or Spring Data based service.
ContactService service = ContactService.createDemoService();
/*
* The "Main method".
*
* This is the entry point method executed to initialize and configure the
* visible user interface. Executed on every browser reload because a new
* instance is created for each web page loaded.
*/
@Override
protected void init(VaadinRequest request) {
configureComponents();
buildLayout();
}
private void configureComponents() {
/*
* Synchronous event handling.
*
* Receive user interaction events on the server-side. This allows you
* to synchronously handle those events. Vaadin automatically sends only
* the needed changes to the web page without loading a new page.
*/
newContact.addClickListener(e -> contactForm.edit(new Contact()));
filter.setInputPrompt("Filter contacts...");
filter.addTextChangeListener(e -> refreshContacts(e.getText()));
contactList
.setContainerDataSource(new BeanItemContainer<>(Contact.class));
contactList.setColumnOrder("firstName", "lastName", "email");
contactList.removeColumn("id");
contactList.removeColumn("birthDate");
contactList.removeColumn("phone");
contactList.setSelectionMode(Grid.SelectionMode.SINGLE);
contactList.addSelectionListener(
e -> contactForm.edit((Contact) contactList.getSelectedRow()));
refreshContacts();
}
/*
* Robust layouts.
*
* Layouts are components that contain other components. HorizontalLayout
* contains TextField and Button. It is wrapped with a Grid into
* VerticalLayout for the left side of the screen. Allow user to resize the
* components with a SplitPanel.
*
* In addition to programmatically building layout in Java, you may also
* choose to setup layout declaratively with Vaadin Designer, CSS and HTML.
*/
private void buildLayout() {
HorizontalLayout actions = new HorizontalLayout(filter, newContact);
actions.setWidth("100%");
filter.setWidth("100%");
actions.setExpandRatio(filter, 1);
VerticalLayout left = new VerticalLayout(actions, contactList);
left.setSizeFull();
contactList.setSizeFull();
left.setExpandRatio(contactList, 1);
HorizontalLayout mainLayout = new HorizontalLayout(left, contactForm);
mainLayout.setSizeFull();
mainLayout.setExpandRatio(left, 1);
// Split and allow resizing
setContent(mainLayout);
}
/*
* Choose the design patterns you like.
*
* It is good practice to have separate data access methods that handle the
* back-end access and/or the user interface updates. You can further split
* your code into classes to easier maintenance. With Vaadin you can follow
* MVC, MVP or any other design pattern you choose.
*/
void refreshContacts() {
refreshContacts(filter.getValue());
}
private void refreshContacts(String stringFilter) {
contactList.setContainerDataSource(new BeanItemContainer<>(
Contact.class, service.findAll(stringFilter)));
contactForm.setVisible(false);
}
/*
* Deployed as a Servlet or Portlet.
*
* You can specify additional servlet parameters like the URI and UI class
* name and turn on production mode when you have finished developing the
* application.
*/
@WebServlet(urlPatterns = "/*")
@VaadinServletConfiguration(ui = AddressbookUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}