Project

General

Profile

Download (12.5 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin.model;
2

    
3
import java.io.Serializable;
4
import java.util.ArrayList;
5

    
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;
20

    
21
public class BeanItemContainerExample extends CustomComponent  {
22
    private static final long serialVersionUID = -6235574698344484522L;
23

    
24
    public void init (String context) {
25
        VerticalLayout layout = new VerticalLayout();
26

    
27
        if ("basic".equals(context))
28
            basic(layout);
29
        else if ("addall".equals(context))
30
            addall(layout);
31
        else if ("dualbinding".equals(context))
32
            dualbinding(layout);
33
        else if ("beanupdate".equals(context))
34
            beanupdate(layout);
35
        else if ("nestedbean".equals(context))
36
            nestedbean(layout);
37
        else
38
            layout.addComponent(new Label("Invalid context: " + context));
39
        
40
        setCompositionRoot(layout);
41
    }
42
    
43
    // BEGIN-EXAMPLE: datamodel.container.beanitemcontainer.basic
44
    // Here is a bean
45
    public class Bean implements Serializable {
46
        private static final long serialVersionUID = -1520923107014804137L;
47

    
48
        String name;
49
        double energy; // Energy content in kJ/100g
50
        
51
        public Bean(String name, double energy) {
52
            this.name   = name;
53
            this.energy = energy;
54
        }
55
        
56
        public String getName() {
57
            return name;
58
        }
59
        
60
        public void setName(String name) {
61
            this.name = name;
62
        }
63
        
64
        public double getEnergy() {
65
            return energy;
66
        }
67
        
68
        public void setEnergy(double energy) {
69
            this.energy = energy;
70
        }
71
    }
72
    
73
    void basic(VerticalLayout layout) {
74
        // Create a container for such beans
75
        BeanItemContainer<Bean> beans =
76
            new BeanItemContainer<Bean>(Bean.class);
77
        
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));
84

    
85
        // Bind a table to it
86
        Table table = new Table("Beans of All Sorts", beans);
87
        layout.addComponent(table);
88
        // END-EXAMPLE: datamodel.container.beanitemcontainer.basic
89
        
90
        table.setPageLength(beans.size());
91
        table.setVisibleColumns(new Object[]{"name", "energy"});
92
    }
93
    
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));
103
        
104
        // Create a container for the beans
105
        final BeanItemContainer<Bean> beans =
106
            new BeanItemContainer<Bean>(Bean.class);
107
        
108
        // Add them all
109
        beans.addAll(beanlist);
110

    
111
        // Bind a table to the container
112
        Table table = new Table("Beans of All Sorts", beans);
113
        layout.addComponent(table);
114
        
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;
119

    
120
            @Override
121
            public void buttonClick(ClickEvent event) {
122
                beans.removeAllItems();
123

    
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));
127
                
128
                beans.addAll(beanlist);
129
            }
130
        });
131
        layout.addComponent(recreate);
132
        // END-EXAMPLE: datamodel.container.beanitemcontainer.addall
133
        
134
        table.setPageLength(beans.size());
135
        table.setVisibleColumns(new Object[]{"name", "energy"});
136
    }
137
    
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>";
142

    
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);
149
        
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));
156

    
157
        // A layout for the table and form
158
        HorizontalLayout layout = new HorizontalLayout();
159

    
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);
165
        
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);
172
        
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;
176

    
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);
181
                    return;
182
                }
183

    
184
                // Bind the form to the selected item
185
                form.setItemDataSource(beans.getItem(table.getValue()));
186
                form.setVisible(true);
187
            }
188
        });
189
        table.setSelectable(true);
190
        table.setImmediate(true);
191

    
192
        // Creates a new bean for editing in the form before adding
193
        // it to the table. Adding is handled after committing
194
        // the form.
195
        final Button newBean = new Button("New Bean");
196
        newBean.addListener(new Button.ClickListener() {
197
            private static final long serialVersionUID = -7340189561756261036L;
198

    
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);
204

    
205
                // Make the form a bit nicer
206
                form.setVisibleItemProperties(
207
                        new Object[]{"name", "energy"});
208
                ((TextField)form.getField("name"))
209
                        .setNullRepresentation("");
210
                
211
                table.select(null);
212
                table.setEnabled(false);
213
                newBean.setEnabled(false);
214
                form.setVisible(true);
215
            }
216
        });
217

    
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;
222

    
223
            public void buttonClick(ClickEvent event) {
224
                form.commit();
225
                form.setVisible(false); // and close it
226
                
227
                // New items have to be added to the container
228
                if (table.getValue() == null) {
229
                    BeanItem<Bean> item =
230
                        (BeanItem<Bean>) form.getItemDataSource();
231

    
232
                    // This creates a new BeanItem
233
                    beans.addBean(item.getBean());
234
                    
235
                    table.setEnabled(true);
236
                    newBean.setEnabled(true);
237
                }
238
            }
239
        });
240
        form.getFooter().addComponent(submit);
241

    
242
        Button cancel = new Button("Cancel");
243
        cancel.addListener(new Button.ClickListener() {
244
            private static final long serialVersionUID = -1749148888766063606L;
245

    
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);
251
            }
252
        });
253
        form.getFooter().addComponent(cancel);
254
        // END-EXAMPLE: datamodel.container.beanitemcontainer.dualbinding
255

    
256
        layout.setSpacing(true);
257
        vlayout.addComponent(layout);
258
        vlayout.addComponent(newBean);
259
    }
260

    
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);
266
    }
267

    
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;
272

    
273
        double rightAscension; /* In angle hours */
274
        double declination;    /* In degrees     */
275
        
276
        public EqCoord(double ra, double decl) {
277
            this.rightAscension = ra;
278
            this.declination    = decl;
279
        }
280
        
281
        public double getRightAscension() {
282
            return rightAscension;
283
        }
284
        public void setRightAscension(double rightAscension) {
285
            this.rightAscension = rightAscension;
286
        }
287

    
288
        public double getDeclination() {
289
            return declination;
290
        }
291
        public void setDeclination(double declination) {
292
            this.declination = declination;
293
        }
294
    }
295

    
296
    /** Bean containing a nested bean */
297
    public class Star implements Serializable {
298
        private static final long serialVersionUID = -312738461368736290L;
299

    
300
        String  name;
301
        EqCoord equatorial; /* Nested bean */
302
        
303
        public Star(String name, EqCoord equatorial) {
304
            this.name       = name;
305
            this.equatorial = equatorial;
306
        }
307
        
308
        public String getName() {
309
            return name;
310
        }
311
        public void setName(String name) {
312
            this.name = name;
313
        }
314

    
315
        /** Access to the nested bean. */
316
        public EqCoord getEquatorial() {
317
            return equatorial;
318
        }
319
        public void setEquatorial(EqCoord equatorial) {
320
            this.equatorial = equatorial;
321
        }
322
    }
323
    
324
    void nestedbean(VerticalLayout layout) {
325
        // Create a container for beans
326
        final BeanItemContainer<Star> stars =
327
            new BeanItemContainer<Star>(Star.class);
328
        
329
        // Declare the nested properties to be used in the container
330
        stars.addNestedContainerProperty("equatorial.rightAscension");
331
        stars.addNestedContainerProperty("equatorial.declination");
332
        
333
        // Add some items
334
        stars.addBean(new Star("Sirius",  new EqCoord(6.75, 16.71611)));
335
        stars.addBean(new Star("Polaris", new EqCoord(2.52, 89.26417)));
336
        
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());
342
        
343
        // Have to set explicitly to hide the "equatorial" property
344
        table.setVisibleColumns(new Object[]{"name",
345
            "equatorial.rightAscension", "equatorial.declination"});
346
        
347
        layout.addComponent(table);
348
    }        
349
    // END-EXAMPLE: datamodel.container.beanitemcontainer.nestedbean
350
}
(1-1/7)