2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.component
.common
;
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
;
17 import org
.apache
.logging
.log4j
.LogManager
;
18 import org
.apache
.logging
.log4j
.Logger
;
19 import org
.vaadin
.viritin
.FilterableListContainer
;
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
;
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
;
47 * @author a.kohlbecker
50 public class FilterableAnnotationsField
extends CompositeCustomField
<List
<Annotation
>> {
52 private static final long serialVersionUID
= -8258550787601028813L;
53 protected static final Logger logger
= LogManager
.getLogger();
55 private CssLayout root
= new CssLayout();
57 private Table table
= new Table();
59 private Button newButton
= ButtonFactory
.CREATE_NEW
.createButton();
61 private List
<AnnotationType
> typesFilter
= null;
63 private BeanItemContainer
<DefinedTermBase
> typeSelectItemContainer
;
65 private FilterableListContainer
<Annotation
> container
;
67 private boolean withNewButton
;
69 public FilterableAnnotationsField() {
73 public FilterableAnnotationsField(String caption
) {
76 // annotations are always sets
77 setConverter(new SetToListConverter
<>());
79 root
.setWidth(100, Unit
.PERCENTAGE
);
82 table
.setPageLength(1);
83 table
.setColumnHeaderMode(ColumnHeaderMode
.HIDDEN
);
84 table
.setWidth(100, Unit
.PERCENTAGE
);
85 table
.setTableFieldFactory(new DefaultFieldFactory() {
87 private static final long serialVersionUID
= 5437750882205859178L;
90 public Field
<?
> createField(Item item
, Object propertyId
, Component uiContext
) {
92 Field
<?
> field
= createField(propertyId
);
94 field
= super.createField(item
, propertyId
, uiContext
);
101 public Field
<?
> createField(Container container
, Object itemId
, Object propertyId
, Component uiContext
) {
103 Field
<?
> field
= createField(propertyId
);
106 field
= super.createField(container
, itemId
, propertyId
, uiContext
);
111 protected Field
<?
> createField(Object propertyId
) {
112 Field
<?
> field
= null;
113 if(propertyId
.equals("text")){
114 TextArea ta
= new TextArea();
115 ta
.setNullRepresentation("");
116 ta
.setWidth(100, Unit
.PERCENTAGE
);
118 } else if(propertyId
.equals("annotationType")) {
119 NativeSelect select
= new NativeSelect();
120 select
.setNullSelectionAllowed(false); //#10538
121 select
.setContainerDataSource(typeSelectItemContainer
);
122 select
.setWidth(100, Unit
.PIXELS
);
124 //the value is overriden by the row record new value setting of the surrounding table, so no need to set the value here
125 // select.select(AnnotationType.INTERNAL()); //AnnotationType.TECHNICAL()
130 field
.setStyleName(table
.getStyleName());
136 addStyledComponent(table
);
140 public void setAnnotationTypesVisible(AnnotationType
... types
){
141 typesFilter
= Arrays
.asList(types
);
145 protected void addDefaultStyles() {
146 // no default styles here
150 public Optional
<FieldGroup
> getFieldGroup() {
151 // holds a Container instead
152 return Optional
.empty();
156 public void commit() throws SourceException
, InvalidValueException
{
158 Collection
<Filter
> filters
= container
.getContainerFilters();
160 for(Filter filter
: filters
){
161 container
.addContainerFilter(filter
);
163 logger
.debug("container.size: " + container
.size());
167 protected List
<Annotation
> getInternalValue() {
168 if(container
== null || container
.getItemIds() == null){
171 return new ArrayList
<>(container
.getItemIds());
176 protected void setInternalValue(List
<Annotation
> newValue
) {
178 boolean hasIncludeFilter
= typesFilter
!= null && !typesFilter
.isEmpty();
179 boolean onlyOneType
= hasIncludeFilter
&& typesFilter
.size() == 1;
181 if(newValue
.isEmpty()){
182 Annotation emptyDefaultAnnotation
= newInstance();
183 newValue
.add(emptyDefaultAnnotation
);
185 emptyDefaultAnnotation
.setAnnotationType(typesFilter
.get(0));
188 container
= new FilterableListContainer
<>(newValue
);
189 if(hasIncludeFilter
){
190 container
.addContainerFilter(new CdmTermFilter
<>("annotationType", typesFilter
));
192 table
.setContainerDataSource(container
);
194 table
.setVisibleColumns("text");
196 table
.setVisibleColumns("text", "annotationType");
198 table
.setEditable(true);
199 if(newValue
.size() > 1){
200 table
.setPageLength(2);
205 protected Component
initContent() {
206 root
.addComponentAsFirst(table
);
208 newButton
.addClickListener(e
-> addAnnotation());
213 private void addAnnotation() {
214 container
.addItem(newInstance());
215 if(container
.size() > 1){
216 table
.setPageLength(2);
220 private Annotation
newInstance() {
221 //internal according to #10522
222 return Annotation
.NewInstance(null, AnnotationType
.INTERNAL(), Language
.DEFAULT());
226 public Class
<?
extends List
<Annotation
>> getType() {
227 return (Class
<?
extends List
<Annotation
>>) new ArrayList
<>().getClass();
230 public void setAnnotationTypeItemContainer(BeanItemContainer
<DefinedTermBase
> typeSelectItemContainer
) {
231 this.typeSelectItemContainer
= typeSelectItemContainer
;
235 public void setReadOnly(boolean readOnly
) {
236 super.setReadOnly(readOnly
);
237 setDeepReadOnly(readOnly
, table
, null);
240 public void withNewButton(boolean withNewButton
) {
241 if(this.withNewButton
!= withNewButton
){
242 if(!this.withNewButton
){
243 root
.addComponent(newButton
);
245 root
.removeComponent(newButton
);
247 this.withNewButton
= withNewButton
;