Project

General

Profile

Download (7.91 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.component.common;
10

    
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.Collection;
14
import java.util.List;
15
import java.util.Optional;
16

    
17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19
import org.vaadin.viritin.FilterableListContainer;
20

    
21
import com.vaadin.data.Container;
22
import com.vaadin.data.Container.Filter;
23
import com.vaadin.data.Item;
24
import com.vaadin.data.Validator.InvalidValueException;
25
import com.vaadin.data.fieldgroup.FieldGroup;
26
import com.vaadin.data.util.BeanItemContainer;
27
import com.vaadin.ui.Button;
28
import com.vaadin.ui.Component;
29
import com.vaadin.ui.CssLayout;
30
import com.vaadin.ui.DefaultFieldFactory;
31
import com.vaadin.ui.Field;
32
import com.vaadin.ui.NativeSelect;
33
import com.vaadin.ui.Table;
34
import com.vaadin.ui.Table.ColumnHeaderMode;
35
import com.vaadin.ui.TextArea;
36

    
37
import eu.etaxonomy.cdm.model.common.Annotation;
38
import eu.etaxonomy.cdm.model.common.AnnotationType;
39
import eu.etaxonomy.cdm.model.common.Language;
40
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
41
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
42
import eu.etaxonomy.cdm.vaadin.util.converter.SetToListConverter;
43
import eu.etaxonomy.cdm.vaadin.util.filter.CdmTermFilter;
44
import eu.etaxonomy.vaadin.component.CompositeCustomField;
45

    
46
/**
47
 * @author a.kohlbecker
48
 * @since Jun 20, 2018
49
 *
50
 */
51
public class FilterableAnnotationsField extends CompositeCustomField<List<Annotation>> {
52

    
53
    protected static final Logger logger = LoggerFactory.getLogger(FilterableAnnotationsField.class);
54

    
55
    private static final long serialVersionUID = -8258550787601028813L;
56

    
57
    private CssLayout root = new CssLayout();
58

    
59
    private Table table = new Table();
60

    
61
    private Button newButton = ButtonFactory.CREATE_NEW.createButton();
62

    
63
    private List<AnnotationType> typesFilter = null;
64

    
65
    private BeanItemContainer<DefinedTermBase> typeSelectItemContainer;
66

    
67
    private FilterableListContainer<Annotation> container;
68

    
69
    private boolean withNewButton;
70

    
71
    public FilterableAnnotationsField() {
72
        this(null);
73
    }
74

    
75
    public FilterableAnnotationsField(String caption) {
76

    
77
        setCaption(caption);
78
        // annotations are always sets
79
        setConverter(new SetToListConverter<Annotation>());
80

    
81
        root.setWidth(100, Unit.PERCENTAGE);
82

    
83
        // setup table
84
        table.setPageLength(1);
85
        table.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
86
        table.setWidth(100,  Unit.PERCENTAGE);
87
        table.setTableFieldFactory(new DefaultFieldFactory() {
88

    
89
            private static final long serialVersionUID = 5437750882205859178L;
90

    
91
            @Override
92
            public Field<?> createField(Item item, Object propertyId, Component uiContext) {
93

    
94
                Field<?> field = createField(propertyId);
95
                if(field == null) {
96
                    field = super.createField(item, propertyId, uiContext);
97
                }
98
                return field;
99

    
100
            }
101

    
102
            @Override
103
            public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
104

    
105
                Field<?> field = createField(propertyId);
106

    
107
                if(field == null) {
108
                    field = super.createField(container, itemId, propertyId, uiContext);
109
                }
110
                return field;
111
            }
112

    
113
            protected Field<?> createField(Object propertyId) {
114
                Field<?> field = null;
115
                if(propertyId.equals("text")){
116
                    TextArea ta = new TextArea();
117
                    ta.setNullRepresentation("");
118
                    ta.setWidth(100,  Unit.PERCENTAGE);
119
                    field = ta;
120
                } else if(propertyId.equals("annotationType")) {
121
                    NativeSelect select = new NativeSelect();
122
                    select.setContainerDataSource(typeSelectItemContainer);
123
                    select.setWidth(100, Unit.PIXELS);
124
                    field = select;
125
                }
126
                field.setStyleName(table.getStyleName());
127
                return field;
128
            }
129
        });
130

    
131
        addStyledComponent(table);
132

    
133
    }
134

    
135
    public void setAnnotationTypesVisible(AnnotationType ... types){
136
        typesFilter = Arrays.asList(types);
137
    }
138

    
139
    /**
140
     * {@inheritDoc}
141
     */
142
    @Override
143
    protected void addDefaultStyles() {
144
        // no default styles here
145
    }
146

    
147
    /**
148
     * {@inheritDoc}
149
     */
150
    @Override
151
    public Optional<FieldGroup> getFieldGroup() {
152
        // holds a Container instead
153
        return Optional.empty();
154
    }
155

    
156
    @Override
157
    public void commit() throws SourceException, InvalidValueException {
158
        table.commit();
159
        Collection<Filter> filters = container.getContainerFilters();
160
        super.commit();
161
        for(Filter filter : filters){
162
            container.addContainerFilter(filter);
163
        }
164
        logger.debug("container.size: " + container.size());
165
    }
166

    
167

    
168

    
169
    /**
170
     * {@inheritDoc}
171
     */
172
    @Override
173
    protected List<Annotation> getInternalValue() {
174
        if(container == null || container.getItemIds() == null){
175
            return null;
176
        }
177
        return new ArrayList<>(container.getItemIds());
178
    }
179

    
180

    
181
    @Override
182
    protected void setInternalValue(List<Annotation> newValue) {
183

    
184
        boolean hasIncludeFilter = typesFilter != null && !typesFilter.isEmpty();
185
        boolean onlyOneType = hasIncludeFilter && typesFilter.size() == 1;
186

    
187
        if(newValue.isEmpty()){
188
            Annotation emptyDefaultAnnotation = newInstance();
189
            newValue.add(emptyDefaultAnnotation );
190
            if(onlyOneType){
191
                emptyDefaultAnnotation.setAnnotationType(typesFilter.get(0));
192
            }
193
        }
194
        container = new FilterableListContainer<Annotation>(newValue);
195
        if(hasIncludeFilter){
196
            container.addContainerFilter(new CdmTermFilter<AnnotationType>("annotationType", typesFilter));
197
        }
198
        table.setContainerDataSource(container);
199
        if(onlyOneType){
200
            table.setVisibleColumns("text");
201
        } else {
202
            table.setVisibleColumns("text", "annotationType");
203
        }
204
        table.setEditable(true);
205
        if(newValue.size() > 1){
206
            table.setPageLength(2);
207
        }
208
    }
209

    
210
    /**
211
     * {@inheritDoc}
212
     */
213
    @Override
214
    protected Component initContent() {
215
        root.addComponentAsFirst(table);
216

    
217
        newButton.addClickListener(e -> addAnnotation());
218
        withNewButton(true);
219
        return root;
220
    }
221

    
222

    
223
    /**
224
     * @return
225
     */
226
    private void addAnnotation() {
227
        container.addItem(newInstance());
228
        if(container.size() > 1){
229
            table.setPageLength(2);
230
        }
231
    }
232

    
233
    /**
234
     * @return
235
     */
236
    private Annotation newInstance() {
237
        return Annotation.NewInstance(null, Language.DEFAULT());
238
    }
239

    
240
    /**
241
     * {@inheritDoc}
242
     */
243
    @Override
244
    public Class<? extends List<Annotation>> getType() {
245
        return (Class<? extends List<Annotation>>) new ArrayList<Annotation>().getClass();
246
    }
247

    
248
    /**
249
     * @param buildTermItemContainer
250
     */
251
    public void setAnnotationTypeItemContainer(BeanItemContainer<DefinedTermBase> typeSelectItemContainer) {
252
        this.typeSelectItemContainer = typeSelectItemContainer;
253
    }
254

    
255
    /**
256
     * {@inheritDoc}
257
     */
258
    @Override
259
    public void setReadOnly(boolean readOnly) {
260
        super.setReadOnly(readOnly);
261
        setDeepReadOnly(readOnly, table, null);
262
    }
263

    
264
    public void withNewButton(boolean withNewButton) {
265
        if(this.withNewButton != withNewButton){
266
            if(!this.withNewButton){
267
                root.addComponent(newButton);
268
            } else {
269
                root.removeComponent(newButton);
270
            }
271
            this.withNewButton = withNewButton;
272
        }
273
    }
274

    
275

    
276

    
277

    
278

    
279

    
280
}
(2-2/8)