Project

General

Profile

Revision 06c249eb

ID06c249ebc0359beacdb3ffa8df5abbd5095ef2c5
Parent 76910e45
Child 6702c7e1

Added by Andreas Kohlbecker about 2 years ago

ref #6169 better name for IdAndString class: EntityReference
- also deleting an example class

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/model/BeanItemContainerExample.java
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
}
src/main/java/eu/etaxonomy/cdm/vaadin/model/EntityReference.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.model;
10

  
11
public class EntityReference {
12
    int id;
13
    String label;
14

  
15
    public EntityReference(int id, String label) {
16
        this.id = id;
17
        this.label = label;
18
    }
19

  
20
    public int getId() {
21
        return id;
22
    }
23

  
24
    public String getLabel() {
25
        return label;
26
    }
27
}
src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverter.java
25 25
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
26 26
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
27 27
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
28
import eu.etaxonomy.cdm.vaadin.view.registration.IdAndString;
28
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
29 29
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
30 30

  
31 31
/**
......
44 44
    private final String separator = ", ";
45 45

  
46 46
    private Collection<TypeDesignationBase> typeDesignations;
47
    private Map<TypeDesignationStatusBase<?>, Collection<IdAndString>> orderedStringsByType;
48
    private LinkedHashMap<String, Collection<IdAndString>> orderedRepresentations = new LinkedHashMap<>();
49
    private IdAndString typifiedName;
47
    private Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByType;
48
    private LinkedHashMap<String, Collection<EntityReference>> orderedRepresentations = new LinkedHashMap<>();
49
    private EntityReference typifiedName;
50 50

  
51 51
    private String finalString = null;
52 52

  
......
60 60
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
61 61
        this.typeDesignations = typeDesignations;
62 62
        orderedStringsByType = new HashMap<>();
63
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), new IdAndString(td.getId(), stringify(td))));
63
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), new EntityReference(td.getId(), stringify(td))));
64 64
        orderedRepresentations = buildOrderedRepresentations();
65 65
        this.typifiedName = findTypifiedName();
66 66
    }
......
106 106
        return this;
107 107
    }
108 108

  
109
    public Map<String, Collection<IdAndString>> getOrderedTypeDesignationRepresentations() {
109
    public Map<String, Collection<EntityReference>> getOrderedTypeDesignationRepresentations() {
110 110
        return orderedRepresentations;
111 111
    }
112 112

  
......
116 116
     * @return
117 117
     * @throws RegistrationValidationException
118 118
     */
119
    private IdAndString findTypifiedName() throws RegistrationValidationException {
119
    private EntityReference findTypifiedName() throws RegistrationValidationException {
120 120

  
121 121
        List<String> problems = new ArrayList<>();
122 122

  
......
154 154
        }
155 155

  
156 156
        if(typifiedName != null){
157
            return new IdAndString(typifiedName.getId(), typifiedName.getTitleCache());
157
            return new EntityReference(typifiedName.getId(), typifiedName.getTitleCache());
158 158
        }
159 159
        return null;
160 160
    }
......
173 173
    /**
174 174
     * @return the title cache of the typifying name or <code>null</code>
175 175
     */
176
    public IdAndString getTypifiedName() {
176
    public EntityReference getTypifiedName() {
177 177

  
178 178
       return typifiedName;
179 179

  
......
264 264
        return sb.toString();
265 265
    }
266 266

  
267
    private void putString(TypeDesignationStatusBase<?> status, IdAndString idAndString){
267
    private void putString(TypeDesignationStatusBase<?> status, EntityReference idAndString){
268 268
        // the cdm orderd term bases are ordered invers, fixing this for here
269 269
        if(status == null){
270 270
            status = SpecimenTypeDesignationStatus.TYPE();
271 271
        }
272 272
        if(!orderedStringsByType.containsKey(status)){
273
            orderedStringsByType.put(status, new ArrayList<IdAndString>());
273
            orderedStringsByType.put(status, new ArrayList<EntityReference>());
274 274
        }
275 275
        orderedStringsByType.get(status).add(idAndString);
276 276
    }
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/IdAndString.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.view.registration;
10

  
11
public class IdAndString {
12
    int id;
13
    String label;
14

  
15
    public IdAndString(int id, String label) {
16
        this.id = id;
17
        this.label = label;
18
    }
19

  
20
    public int getId() {
21
        return id;
22
    }
23

  
24
    public String getLabel() {
25
        return label;
26
    }
27
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationDTO.java
26 26
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
27 27
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
28 28
import eu.etaxonomy.cdm.model.reference.IReference;
29
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
29 30
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter;
30 31

  
31 32
public class RegistrationDTO{
......
42 43

  
43 44
    private String submitterUserName = null;
44 45

  
45
    private IdAndString name = null;
46
    private EntityReference name = null;
46 47

  
47 48
    private TypeDesignationConverter typeDesignationConverter;
48 49

  
......
71 72
        if(hasName(reg)){
72 73
            citation = reg.getName().getNomenclaturalReference();
73 74
            citationDetail = reg.getName().getNomenclaturalMicroReference();
74
            name = new IdAndString(reg.getName().getId(), reg.getName().getTitleCache());
75
            name = new EntityReference(reg.getName().getId(), reg.getName().getTitleCache());
75 76
        }
76 77
        if(hasTypifications(reg)){
77 78
            if(!reg.getTypeDesignations().isEmpty()){
......
221 222
        return citation == null ? null : citation.getId();
222 223
    }
223 224

  
224
    public IdAndString getTypifiedName() {
225
    public EntityReference getTypifiedName() {
225 226
        return typeDesignationConverter != null ? typeDesignationConverter.getTypifiedName() : null;
226 227
    }
227 228

  
228
    public IdAndString getName() {
229
    public EntityReference getName() {
229 230
        return name;
230 231
    }
231 232

  
232
    public Map<String, Collection<IdAndString>> getTypeDesignations() {
233
    public Map<String, Collection<EntityReference>> getTypeDesignations() {
233 234
        return typeDesignationConverter != null ? typeDesignationConverter.getOrderedTypeDesignationRepresentations() : null;
234 235
    }
235 236

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)