Project

General

Profile

Download (6.79 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.vaadin.viritin.FilterableListContainer;
18

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

    
34
import eu.etaxonomy.cdm.model.common.Annotation;
35
import eu.etaxonomy.cdm.model.common.AnnotationType;
36
import eu.etaxonomy.cdm.model.common.Language;
37
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
38
import eu.etaxonomy.cdm.vaadin.util.converter.SetToListConverter;
39
import eu.etaxonomy.cdm.vaadin.util.filter.CdmTermFilter;
40
import eu.etaxonomy.vaadin.component.CompositeCustomField;
41

    
42
/**
43
 * @author a.kohlbecker
44
 * @since Jun 20, 2018
45
 *
46
 */
47
public class FilterableAnnotationsField extends CompositeCustomField<List<Annotation>> {
48

    
49
    private static final long serialVersionUID = -8258550787601028813L;
50

    
51
    private CssLayout root = new CssLayout();
52

    
53
    private Table table = new Table();
54

    
55
    private List<AnnotationType> typesFilter = null;
56

    
57
    private Annotation emptyDefaultAnnotation = Annotation.NewInstance(null, Language.DEFAULT());
58

    
59
    private BeanItemContainer<DefinedTermBase> typeSelectItemContainer;
60

    
61
    private FilterableListContainer<Annotation> container;
62

    
63
    public FilterableAnnotationsField() {
64
        this(null);
65
    }
66

    
67
    public FilterableAnnotationsField(String caption) {
68

    
69
        setCaption(caption);
70
        // annotations are always sets
71
        setConverter(new SetToListConverter<Annotation>());
72

    
73
        root.setWidth(100, Unit.PERCENTAGE);
74

    
75
        // setup table
76
        table.setPageLength(1);
77
        table.setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
78
        table.setWidth(100,  Unit.PERCENTAGE);
79
        table.setTableFieldFactory(new DefaultFieldFactory() {
80

    
81
            private static final long serialVersionUID = 5437750882205859178L;
82

    
83
            @Override
84
            public Field<?> createField(Item item, Object propertyId, Component uiContext) {
85

    
86
                Field<?> field = createField(propertyId);
87
                if(field == null) {
88
                    field = super.createField(item, propertyId, uiContext);
89
                }
90
                return field;
91

    
92
            }
93

    
94
            @Override
95
            public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
96

    
97
                Field<?> field = createField(propertyId);
98

    
99
                if(field == null) {
100
                    field = super.createField(container, itemId, propertyId, uiContext);
101
                }
102
                return field;
103
            }
104

    
105
            protected Field<?> createField(Object propertyId) {
106
                Field<?> field = null;
107
                if(propertyId.equals("text")){
108
                    TextArea ta = new TextArea();
109
                    ta.setNullRepresentation("");
110
                    ta.setWidth(100,  Unit.PERCENTAGE);
111
                    field = ta;
112
                } else if(propertyId.equals("annotationType")) {
113
                    ListSelect select = new ListSelect();
114
                    select.setContainerDataSource(typeSelectItemContainer);
115
                    select.setWidth(100, Unit.PIXELS);
116
                    select.setRows(1);
117
                    field = select;
118
                }
119
                field.setStyleName(table.getStyleName());
120
                return field;
121
            }
122
        });
123

    
124
        addStyledComponent(table);
125

    
126
    }
127

    
128
    public void setAnnotationTypesVisible(AnnotationType ... types){
129
        typesFilter = Arrays.asList(types);
130
    }
131

    
132
    /**
133
     * {@inheritDoc}
134
     */
135
    @Override
136
    protected void addDefaultStyles() {
137
        // no default styles here
138
    }
139

    
140
    /**
141
     * {@inheritDoc}
142
     */
143
    @Override
144
    public Optional<FieldGroup> getFieldGroup() {
145
        // holds a Container instead
146
        return Optional.empty();
147
    }
148

    
149
    @Override
150
    public void commit() throws SourceException, InvalidValueException {
151
        table.commit();
152
        Collection<Filter> filters = container.getContainerFilters();
153
        super.commit();
154
        for(Filter filter : filters){
155
            container.addContainerFilter(filter);
156
        }
157
        System.err.println(container.size());
158
    }
159

    
160

    
161

    
162
    /**
163
     * {@inheritDoc}
164
     */
165
    @Override
166
    protected List<Annotation> getInternalValue() {
167
        if(container == null || container.getItemIds() == null){
168
            return null;
169
        }
170
        return new ArrayList<>(container.getItemIds());
171
    }
172

    
173

    
174
    @Override
175
    protected void setInternalValue(List<Annotation> newValue) {
176
        boolean hasIncludeFilter = typesFilter != null && !typesFilter.isEmpty();
177
        boolean onlyOneType = hasIncludeFilter && typesFilter.size() == 1;
178

    
179
        if(newValue.isEmpty()){
180
            newValue.add(emptyDefaultAnnotation);
181
            if(onlyOneType){
182
                emptyDefaultAnnotation.setAnnotationType(typesFilter.get(0));
183
            }
184
        }
185
        container = new FilterableListContainer<Annotation>(newValue);
186
        if(hasIncludeFilter){
187
            container.addContainerFilter(new CdmTermFilter<AnnotationType>("annotationType", typesFilter));
188
        }
189
        table.setContainerDataSource(container);
190
        if(onlyOneType){
191
            table.setVisibleColumns("text");
192
        } else {
193
            table.setVisibleColumns("text", "annotationType");
194
        }
195
        table.setEditable(true);
196
    }
197

    
198
    /**
199
     * {@inheritDoc}
200
     */
201
    @Override
202
    protected Component initContent() {
203
        root.addComponent(table);
204
        return root;
205
    }
206

    
207

    
208
    /**
209
     * {@inheritDoc}
210
     */
211
    @Override
212
    public Class<? extends List<Annotation>> getType() {
213
        return (Class<? extends List<Annotation>>) new ArrayList<Annotation>().getClass();
214
    }
215

    
216
    /**
217
     * @param buildTermItemContainer
218
     */
219
    public void setAnnotationTypeItemContainer(BeanItemContainer<DefinedTermBase> typeSelectItemContainer) {
220
        this.typeSelectItemContainer = typeSelectItemContainer;
221
    }
222

    
223
    /**
224
     * {@inheritDoc}
225
     */
226
    @Override
227
    public void setReadOnly(boolean readOnly) {
228
        super.setReadOnly(readOnly);
229
        setDeepReadOnly(readOnly, table, null);
230
    }
231

    
232

    
233

    
234
}
(2-2/8)