1 package eu
.etaxonomy
.cdm
.vaadin
.model
;
3 import java
.io
.Serializable
;
4 import java
.util
.ArrayList
;
6 import com
.vaadin
.data
.Property
;
7 import com
.vaadin
.data
.Property
.ValueChangeEvent
;
8 import com
.vaadin
.data
.util
.BeanItem
;
9 import com
.vaadin
.data
.util
.BeanItemContainer
;
10 import com
.vaadin
.ui
.Button
;
11 import com
.vaadin
.ui
.Button
.ClickEvent
;
12 import com
.vaadin
.ui
.Button
.ClickListener
;
13 import com
.vaadin
.ui
.CustomComponent
;
14 import com
.vaadin
.ui
.Form
;
15 import com
.vaadin
.ui
.HorizontalLayout
;
16 import com
.vaadin
.ui
.Label
;
17 import com
.vaadin
.ui
.Table
;
18 import com
.vaadin
.ui
.TextField
;
19 import com
.vaadin
.ui
.VerticalLayout
;
21 public class BeanItemContainerExample
extends CustomComponent
{
22 private static final long serialVersionUID
= -6235574698344484522L;
24 public void init (String context
) {
25 VerticalLayout layout
= new VerticalLayout();
27 if ("basic".equals(context
))
29 else if ("addall".equals(context
))
31 else if ("dualbinding".equals(context
))
33 else if ("beanupdate".equals(context
))
35 else if ("nestedbean".equals(context
))
38 layout
.addComponent(new Label("Invalid context: " + context
));
40 setCompositionRoot(layout
);
43 // BEGIN-EXAMPLE: datamodel.container.beanitemcontainer.basic
45 public class Bean
implements Serializable
{
46 private static final long serialVersionUID
= -1520923107014804137L;
49 double energy
; // Energy content in kJ/100g
51 public Bean(String name
, double energy
) {
56 public String
getName() {
60 public void setName(String name
) {
64 public double getEnergy() {
68 public void setEnergy(double energy
) {
73 void basic(VerticalLayout layout
) {
74 // Create a container for such beans
75 BeanItemContainer
<Bean
> beans
=
76 new BeanItemContainer
<Bean
>(Bean
.class);
78 // Add some beans to it
79 beans
.addBean(new Bean("Mung bean", 1452.0));
80 beans
.addBean(new Bean("Chickpea", 686.0));
81 beans
.addBean(new Bean("Lentil", 1477.0));
82 beans
.addBean(new Bean("Common bean", 129.0));
83 beans
.addBean(new Bean("Soybean", 1866.0));
86 Table table
= new Table("Beans of All Sorts", beans
);
87 layout
.addComponent(table
);
88 // END-EXAMPLE: datamodel.container.beanitemcontainer.basic
90 table
.setPageLength(beans
.size());
91 table
.setVisibleColumns(new Object
[]{"name", "energy"});
94 void addall(VerticalLayout layout
) {
95 // BEGIN-EXAMPLE: datamodel.container.beanitemcontainer.addall
96 // Have a collection with some beans
97 ArrayList
<Bean
> beanlist
= new ArrayList
<Bean
>();
98 beanlist
.add(new Bean("Mung bean", 1452.0));
99 beanlist
.add(new Bean("Chickpea", 686.0));
100 beanlist
.add(new Bean("Lentil", 1477.0));
101 beanlist
.add(new Bean("Common bean", 129.0));
102 beanlist
.add(new Bean("Soybean", 1866.0));
104 // Create a container for the beans
105 final BeanItemContainer
<Bean
> beans
=
106 new BeanItemContainer
<Bean
>(Bean
.class);
109 beans
.addAll(beanlist
);
111 // Bind a table to the container
112 Table table
= new Table("Beans of All Sorts", beans
);
113 layout
.addComponent(table
);
115 // Have a button to empty and create some new items
116 Button recreate
= new Button("Recreate");
117 recreate
.addListener(new ClickListener() {
118 private static final long serialVersionUID
= 6823630748713272361L;
121 public void buttonClick(ClickEvent event
) {
122 beans
.removeAllItems();
124 ArrayList
<Bean
> beanlist
= new ArrayList
<Bean
>();
125 beanlist
.add(new Bean("Space bean", 42.0));
126 beanlist
.add(new Bean("Moon bean", 388.0));
128 beans
.addAll(beanlist
);
131 layout
.addComponent(recreate
);
132 // END-EXAMPLE: datamodel.container.beanitemcontainer.addall
134 table
.setPageLength(beans
.size());
135 table
.setVisibleColumns(new Object
[]{"name", "energy"});
138 public static final String dualbindingDescription
=
139 "<h1>Binding a bean both to a Table and a Form</h1>"+
140 "<p>Editing the <b>BeanItem</b> in the form fires value change events, "+
141 "which are communicated to the other components bound to the same data source, in this case the <b>Table</b>.</p>";
143 @SuppressWarnings("unchecked")
144 void dualbinding(VerticalLayout vlayout
) {
145 // BEGIN-EXAMPLE: datamodel.container.beanitemcontainer.dualbinding
146 // Create a container for beans
147 final BeanItemContainer
<Bean
> beans
=
148 new BeanItemContainer
<Bean
>(Bean
.class);
150 // Add some beans to it
151 beans
.addBean(new Bean("Mung bean", 1452.0));
152 beans
.addBean(new Bean("Chickpea", 686.0));
153 beans
.addBean(new Bean("Lentil", 1477.0));
154 beans
.addBean(new Bean("Common bean", 129.0));
155 beans
.addBean(new Bean("Soybean", 1866.0));
157 // A layout for the table and form
158 HorizontalLayout layout
= new HorizontalLayout();
160 // Bind a table to it
161 final Table table
= new Table("Beans of All Sorts", beans
);
162 table
.setVisibleColumns(new Object
[]{"name", "energy"});
163 table
.setPageLength(7);
164 layout
.addComponent(table
);
166 // Create a form for editing a selected or new item.
167 // It is invisible until actually used.
168 final Form form
= new Form();
169 form
.setCaption("Edit Item");
170 form
.setVisible(false);
171 layout
.addComponent(form
);
173 // When the user selects an item, show it in the form
174 table
.addListener(new Property
.ValueChangeListener() {
175 private static final long serialVersionUID
= 1162945655606583495L;
177 public void valueChange(ValueChangeEvent event
) {
178 // Close the form if the item is deselected
179 if (event
.getProperty().getValue() == null) {
180 form
.setVisible(false);
184 // Bind the form to the selected item
185 form
.setItemDataSource(beans
.getItem(table
.getValue()));
186 form
.setVisible(true);
189 table
.setSelectable(true);
190 table
.setImmediate(true);
192 // Creates a new bean for editing in the form before adding
193 // it to the table. Adding is handled after committing
195 final Button newBean
= new Button("New Bean");
196 newBean
.addListener(new Button
.ClickListener() {
197 private static final long serialVersionUID
= -7340189561756261036L;
199 public void buttonClick(ClickEvent event
) {
200 // Create a new bean and bind it to the form
201 Bean bean
= new Bean(null, 0.0);
202 BeanItem
<Bean
> item
= new BeanItem
<Bean
>(bean
);
203 form
.setItemDataSource(item
);
205 // Make the form a bit nicer
206 form
.setVisibleItemProperties(
207 new Object
[]{"name", "energy"});
208 ((TextField
)form
.getField("name"))
209 .setNullRepresentation("");
212 table
.setEnabled(false);
213 newBean
.setEnabled(false);
214 form
.setVisible(true);
218 // When OK button is clicked, commit the form to the bean
219 Button submit
= new Button("OK");
220 submit
.addListener(new Button
.ClickListener() {
221 private static final long serialVersionUID
= 6823630748713272361L;
223 public void buttonClick(ClickEvent event
) {
225 form
.setVisible(false); // and close it
227 // New items have to be added to the container
228 if (table
.getValue() == null) {
229 BeanItem
<Bean
> item
=
230 (BeanItem
<Bean
>) form
.getItemDataSource();
232 // This creates a new BeanItem
233 beans
.addBean(item
.getBean());
235 table
.setEnabled(true);
236 newBean
.setEnabled(true);
240 form
.getFooter().addComponent(submit
);
242 Button cancel
= new Button("Cancel");
243 cancel
.addListener(new Button
.ClickListener() {
244 private static final long serialVersionUID
= -1749148888766063606L;
246 public void buttonClick(ClickEvent event
) {
247 form
.discard(); // Not really necessary
248 form
.setVisible(false); // and close it
249 table
.setEnabled(true);
250 newBean
.setEnabled(true);
253 form
.getFooter().addComponent(cancel
);
254 // END-EXAMPLE: datamodel.container.beanitemcontainer.dualbinding
256 layout
.setSpacing(true);
257 vlayout
.addComponent(layout
);
258 vlayout
.addComponent(newBean
);
261 void beanupdate(VerticalLayout vlayout
) {
262 // BEGIN-EXAMPLE: datamodel.container.beanitemcontainer.beanupdate
263 // Create a container for beans
264 final BeanItemContainer
<Bean
> beans
=
265 new BeanItemContainer
<Bean
>(Bean
.class);
268 // BEGIN-EXAMPLE: datamodel.container.beanitemcontainer.nestedbean
269 /** Bean to be nested */
270 public class EqCoord
implements Serializable
{
271 private static final long serialVersionUID
= -3020840929725381667L;
273 double rightAscension
; /* In angle hours */
274 double declination
; /* In degrees */
276 public EqCoord(double ra
, double decl
) {
277 this.rightAscension
= ra
;
278 this.declination
= decl
;
281 public double getRightAscension() {
282 return rightAscension
;
284 public void setRightAscension(double rightAscension
) {
285 this.rightAscension
= rightAscension
;
288 public double getDeclination() {
291 public void setDeclination(double declination
) {
292 this.declination
= declination
;
296 /** Bean containing a nested bean */
297 public class Star
implements Serializable
{
298 private static final long serialVersionUID
= -312738461368736290L;
301 EqCoord equatorial
; /* Nested bean */
303 public Star(String name
, EqCoord equatorial
) {
305 this.equatorial
= equatorial
;
308 public String
getName() {
311 public void setName(String name
) {
315 /** Access to the nested bean. */
316 public EqCoord
getEquatorial() {
319 public void setEquatorial(EqCoord equatorial
) {
320 this.equatorial
= equatorial
;
324 void nestedbean(VerticalLayout layout
) {
325 // Create a container for beans
326 final BeanItemContainer
<Star
> stars
=
327 new BeanItemContainer
<Star
>(Star
.class);
329 // Declare the nested properties to be used in the container
330 stars
.addNestedContainerProperty("equatorial.rightAscension");
331 stars
.addNestedContainerProperty("equatorial.declination");
334 stars
.addBean(new Star("Sirius", new EqCoord(6.75, 16.71611)));
335 stars
.addBean(new Star("Polaris", new EqCoord(2.52, 89.26417)));
337 // Put them in a table
338 Table table
= new Table("Stars", stars
);
339 table
.setColumnHeader("equatorial.rightAscension", "RA");
340 table
.setColumnHeader("equatorial.declination", "Decl");
341 table
.setPageLength(table
.size());
343 // Have to set explicitly to hide the "equatorial" property
344 table
.setVisibleColumns(new Object
[]{"name",
345 "equatorial.rightAscension", "equatorial.declination"});
347 layout
.addComponent(table
);
349 // END-EXAMPLE: datamodel.container.beanitemcontainer.nestedbean