Project

General

Profile

Download (4.7 KB) Statistics
| Branch: | Tag: | Revision:
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.vaadin.component;
10

    
11
import java.util.List;
12

    
13
import com.vaadin.data.fieldgroup.FieldGroup;
14
import com.vaadin.data.util.BeanItemContainer;
15
import com.vaadin.server.FontAwesome;
16
import com.vaadin.ui.AbstractField;
17
import com.vaadin.ui.Button;
18
import com.vaadin.ui.Component;
19
import com.vaadin.ui.CssLayout;
20
import com.vaadin.ui.GridLayout;
21
import com.vaadin.ui.themes.ValoTheme;
22

    
23
/**
24
 * @author a.kohlbecker
25
 * @since May 11, 2017
26
 *
27
 */
28
public class FieldListEditor<V extends Object, F extends AbstractField<V>>  extends CompositeCustomField<List<V>> {
29

    
30
    private static final long serialVersionUID = 4670707714503199599L;
31

    
32
    private Class<F> fieldType;
33

    
34
    private Class<V> itemType;
35

    
36
    private FieldGroup parentFieldGroup = null;
37

    
38
    //NOTE: Managing the item
39
    //      IDs makes BeanContainer more complex to use, but it is necessary in some cases where the
40
    //      equals() or hashCode() methods have been reimplemented in the bean.
41
    //      TODO CdmBase an a reimplemented equals method, do we need to use the BeanContainer instead?
42
    private BeanItemContainer<V> beans;
43

    
44
   //private LinkedList<V> itemList = new LinkedList<>();
45

    
46
    private int GRID_COLS = 2;
47

    
48
    private GridLayout grid = new GridLayout(GRID_COLS,1);
49

    
50
    public  FieldListEditor(Class<V> itemType, Class<F> fieldType, String caption){
51
        this.fieldType = fieldType;
52
        this.itemType = itemType;
53
        setCaption(caption);
54
        beans = new BeanItemContainer<V>(itemType);
55
    }
56

    
57
    private Component buttonGroup(){
58
        Button add = new Button(FontAwesome.PLUS);
59
        Button remove = new Button(FontAwesome.MINUS);
60
        Button moveUp = new Button(FontAwesome.ARROW_UP);
61
        Button moveDown = new Button(FontAwesome.ARROW_DOWN);
62
        CssLayout buttonGroup = new CssLayout(add, remove, moveUp, moveDown);
63
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
64

    
65
        addStyledComponents(add, remove, moveUp, moveDown);
66

    
67
        return buttonGroup;
68
    }
69

    
70
    /**
71
     * {@inheritDoc}
72
     */
73
    @Override
74
    protected Component initContent() {
75
        grid.setColumnExpandRatio(0, 1.0f);
76
        return grid;
77
    }
78

    
79
    /**
80
     * {@inheritDoc}
81
     */
82
    @Override
83
    public Class getType() {
84
        return List.class;
85
    }
86

    
87
    /**
88
     * {@inheritDoc}
89
     */
90
    @Override
91
    protected void setInternalValue(List<V> newValue) {
92
        super.setInternalValue(newValue);
93

    
94
        beans.addAll(newValue);
95

    
96
        grid.setRows(newValue.size());
97
        int row = 0;
98
        for(V val : newValue){
99
            row = addNewRow(row, val);
100
        }
101
    }
102

    
103
    /**
104
     * @param row
105
     * @param val
106
     * @return
107
     */
108
    protected int addNewRow(int row, V val) {
109
        try {
110
            F field = fieldType.newInstance();
111
            addStyledComponent(field);
112
            field.setWidth(100, Unit.PERCENTAGE);
113
            field.setValue(val);
114
            grid.addComponent(field, 0, row);
115
            grid.addComponent(buttonGroup(), 1, row);
116
            nestFieldGroup(field);
117
            row++;
118
        } catch (InstantiationException e) {
119
            // TODO Auto-generated catch block
120
            e.printStackTrace();
121
        } catch (IllegalAccessException e) {
122
            // TODO Auto-generated catch block
123
            e.printStackTrace();
124
        }
125
        return row;
126
    }
127

    
128
    /**
129
     * @param field
130
     */
131
    protected void nestFieldGroup(F field) {
132
        if(NestedFieldGroup.class.isAssignableFrom(fieldType) && parentFieldGroup != null){
133
            ((NestedFieldGroup)field).registerParentFieldGroup(parentFieldGroup);
134
        }
135
    }
136

    
137
    /**
138
     * {@inheritDoc}
139
     */
140
    @Override
141
    public void setWidth(String width) {
142
        super.setWidth(width);
143
        grid.setWidth(width);
144
    }
145

    
146
    @Override
147
    public void setWidth(float width, Unit unit){
148
        super.setWidth(width, unit);
149
        if(grid != null){
150
            grid.setWidth(width, unit);
151
        }
152
    }
153

    
154
    /**
155
     * {@inheritDoc}
156
     */
157
    @Override
158
    protected void addDefaultStyles() {
159
        // no default styles
160
    }
161

    
162
    /**
163
     * {@inheritDoc}
164
     * <p>
165
     * However, this class has no local fieldGroup but must delegate to the nested NestedFieldGroup
166
     * if there are any. This happens in {@link #nestFieldGroup(AbstractField)}.
167
     * <p>
168
     */
169
    @Override
170
    public FieldGroup getFieldGroup() {
171
        return null;
172
    }
173

    
174
    @Override
175
    public void registerParentFieldGroup(FieldGroup parent) {
176
        parentFieldGroup = parent;
177
    }
178

    
179

    
180

    
181

    
182

    
183

    
184

    
185

    
186
}
(2-2/5)