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.NativeSelect;
|
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
|
NativeSelect select = new NativeSelect();
|
114
|
select.setContainerDataSource(typeSelectItemContainer);
|
115
|
select.setWidth(100, Unit.PIXELS);
|
116
|
field = select;
|
117
|
}
|
118
|
field.setStyleName(table.getStyleName());
|
119
|
return field;
|
120
|
}
|
121
|
});
|
122
|
|
123
|
addStyledComponent(table);
|
124
|
|
125
|
}
|
126
|
|
127
|
public void setAnnotationTypesVisible(AnnotationType ... types){
|
128
|
typesFilter = Arrays.asList(types);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* {@inheritDoc}
|
133
|
*/
|
134
|
@Override
|
135
|
protected void addDefaultStyles() {
|
136
|
// no default styles here
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* {@inheritDoc}
|
141
|
*/
|
142
|
@Override
|
143
|
public Optional<FieldGroup> getFieldGroup() {
|
144
|
// holds a Container instead
|
145
|
return Optional.empty();
|
146
|
}
|
147
|
|
148
|
@Override
|
149
|
public void commit() throws SourceException, InvalidValueException {
|
150
|
table.commit();
|
151
|
Collection<Filter> filters = container.getContainerFilters();
|
152
|
super.commit();
|
153
|
for(Filter filter : filters){
|
154
|
container.addContainerFilter(filter);
|
155
|
}
|
156
|
System.err.println(container.size());
|
157
|
}
|
158
|
|
159
|
|
160
|
|
161
|
/**
|
162
|
* {@inheritDoc}
|
163
|
*/
|
164
|
@Override
|
165
|
protected List<Annotation> getInternalValue() {
|
166
|
if(container == null || container.getItemIds() == null){
|
167
|
return null;
|
168
|
}
|
169
|
return new ArrayList<>(container.getItemIds());
|
170
|
}
|
171
|
|
172
|
|
173
|
@Override
|
174
|
protected void setInternalValue(List<Annotation> newValue) {
|
175
|
boolean hasIncludeFilter = typesFilter != null && !typesFilter.isEmpty();
|
176
|
boolean onlyOneType = hasIncludeFilter && typesFilter.size() == 1;
|
177
|
|
178
|
if(newValue.isEmpty()){
|
179
|
newValue.add(emptyDefaultAnnotation);
|
180
|
if(onlyOneType){
|
181
|
emptyDefaultAnnotation.setAnnotationType(typesFilter.get(0));
|
182
|
}
|
183
|
}
|
184
|
container = new FilterableListContainer<Annotation>(newValue);
|
185
|
if(hasIncludeFilter){
|
186
|
container.addContainerFilter(new CdmTermFilter<AnnotationType>("annotationType", typesFilter));
|
187
|
}
|
188
|
table.setContainerDataSource(container);
|
189
|
if(onlyOneType){
|
190
|
table.setVisibleColumns("text");
|
191
|
} else {
|
192
|
table.setVisibleColumns("text", "annotationType");
|
193
|
}
|
194
|
table.setEditable(true);
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* {@inheritDoc}
|
199
|
*/
|
200
|
@Override
|
201
|
protected Component initContent() {
|
202
|
root.addComponent(table);
|
203
|
return root;
|
204
|
}
|
205
|
|
206
|
|
207
|
/**
|
208
|
* {@inheritDoc}
|
209
|
*/
|
210
|
@Override
|
211
|
public Class<? extends List<Annotation>> getType() {
|
212
|
return (Class<? extends List<Annotation>>) new ArrayList<Annotation>().getClass();
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* @param buildTermItemContainer
|
217
|
*/
|
218
|
public void setAnnotationTypeItemContainer(BeanItemContainer<DefinedTermBase> typeSelectItemContainer) {
|
219
|
this.typeSelectItemContainer = typeSelectItemContainer;
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* {@inheritDoc}
|
224
|
*/
|
225
|
@Override
|
226
|
public void setReadOnly(boolean readOnly) {
|
227
|
super.setReadOnly(readOnly);
|
228
|
setDeepReadOnly(readOnly, table, null);
|
229
|
}
|
230
|
|
231
|
|
232
|
|
233
|
}
|