Project

General

Profile

Download (6.6 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

    
16
import org.vaadin.viritin.FilterableListContainer;
17

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

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

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

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

    
50
    Class<List<Annotation>> type = (Class<List<Annotation>>)new ArrayList<Annotation>().getClass();
51

    
52
    private CssLayout root = new CssLayout();
53

    
54
    private Table table = new Table();
55

    
56
    private List<AnnotationType> typesFilter = null;
57

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

    
60
    private BeanItemContainer<DefinedTermBase> typeSelectItemContainer;
61

    
62
    private FilterableListContainer<Annotation> container;
63

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

    
68
    public FilterableAnnotationsField(String caption) {
69

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

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

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

    
82
            private static final long serialVersionUID = 5437750882205859178L;
83

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

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

    
93
            }
94

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

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

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

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

    
125
        addStyledComponent(table);
126

    
127
    }
128

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

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

    
141
    /**
142
     * {@inheritDoc}
143
     */
144
    @Override
145
    public FieldGroup getFieldGroup() {
146
        // holds a Container instead // TODO can this cause a NPE?
147
        return null;
148
    }
149

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

    
161

    
162

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

    
174

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

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

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

    
208

    
209
    /**
210
     * {@inheritDoc}
211
     */
212
    @Override
213
    public Class<List<Annotation>> getType() {
214
        return type;
215
    }
216

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

    
224

    
225
}
(2-2/8)