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
.vaadin
.viritin
.FilterableListContainer
;
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
.Button
;
26 import com
.vaadin
.ui
.Component
;
27 import com
.vaadin
.ui
.CssLayout
;
28 import com
.vaadin
.ui
.DefaultFieldFactory
;
29 import com
.vaadin
.ui
.Field
;
30 import com
.vaadin
.ui
.NativeSelect
;
31 import com
.vaadin
.ui
.Table
;
32 import com
.vaadin
.ui
.Table
.ColumnHeaderMode
;
33 import com
.vaadin
.ui
.TextArea
;
35 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
36 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
38 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
39 import eu
.etaxonomy
.cdm
.vaadin
.component
.ButtonFactory
;
40 import eu
.etaxonomy
.cdm
.vaadin
.util
.converter
.SetToListConverter
;
41 import eu
.etaxonomy
.cdm
.vaadin
.util
.filter
.CdmTermFilter
;
42 import eu
.etaxonomy
.vaadin
.component
.CompositeCustomField
;
45 * @author a.kohlbecker
49 public class FilterableAnnotationsField
extends CompositeCustomField
<List
<Annotation
>> {
51 private static final long serialVersionUID
= -8258550787601028813L;
53 private CssLayout root
= new CssLayout();
55 private Table table
= new Table();
57 private Button newButton
= ButtonFactory
.CREATE_NEW
.createButton();
59 private List
<AnnotationType
> typesFilter
= null;
61 private BeanItemContainer
<DefinedTermBase
> typeSelectItemContainer
;
63 private FilterableListContainer
<Annotation
> container
;
65 private boolean withNewButton
;
67 public FilterableAnnotationsField() {
71 public FilterableAnnotationsField(String caption
) {
74 // annotations are always sets
75 setConverter(new SetToListConverter
<Annotation
>());
77 root
.setWidth(100, Unit
.PERCENTAGE
);
80 table
.setPageLength(1);
81 table
.setColumnHeaderMode(ColumnHeaderMode
.HIDDEN
);
82 table
.setWidth(100, Unit
.PERCENTAGE
);
83 table
.setTableFieldFactory(new DefaultFieldFactory() {
85 private static final long serialVersionUID
= 5437750882205859178L;
88 public Field
<?
> createField(Item item
, Object propertyId
, Component uiContext
) {
90 Field
<?
> field
= createField(propertyId
);
92 field
= super.createField(item
, propertyId
, uiContext
);
99 public Field
<?
> createField(Container container
, Object itemId
, Object propertyId
, Component uiContext
) {
101 Field
<?
> field
= createField(propertyId
);
104 field
= super.createField(container
, itemId
, propertyId
, uiContext
);
109 protected Field
<?
> createField(Object propertyId
) {
110 Field
<?
> field
= null;
111 if(propertyId
.equals("text")){
112 TextArea ta
= new TextArea();
113 ta
.setNullRepresentation("");
114 ta
.setWidth(100, Unit
.PERCENTAGE
);
116 } else if(propertyId
.equals("annotationType")) {
117 NativeSelect select
= new NativeSelect();
118 select
.setContainerDataSource(typeSelectItemContainer
);
119 select
.setWidth(100, Unit
.PIXELS
);
122 field
.setStyleName(table
.getStyleName());
127 addStyledComponent(table
);
131 public void setAnnotationTypesVisible(AnnotationType
... types
){
132 typesFilter
= Arrays
.asList(types
);
139 protected void addDefaultStyles() {
140 // no default styles here
147 public Optional
<FieldGroup
> getFieldGroup() {
148 // holds a Container instead
149 return Optional
.empty();
153 public void commit() throws SourceException
, InvalidValueException
{
155 Collection
<Filter
> filters
= container
.getContainerFilters();
157 for(Filter filter
: filters
){
158 container
.addContainerFilter(filter
);
160 System
.err
.println(container
.size());
169 protected List
<Annotation
> getInternalValue() {
170 if(container
== null || container
.getItemIds() == null){
173 return new ArrayList
<>(container
.getItemIds());
178 protected void setInternalValue(List
<Annotation
> newValue
) {
180 boolean hasIncludeFilter
= typesFilter
!= null && !typesFilter
.isEmpty();
181 boolean onlyOneType
= hasIncludeFilter
&& typesFilter
.size() == 1;
183 if(newValue
.isEmpty()){
184 Annotation emptyDefaultAnnotation
= newInstance();
185 newValue
.add(emptyDefaultAnnotation
);
187 emptyDefaultAnnotation
.setAnnotationType(typesFilter
.get(0));
190 container
= new FilterableListContainer
<Annotation
>(newValue
);
191 if(hasIncludeFilter
){
192 container
.addContainerFilter(new CdmTermFilter
<AnnotationType
>("annotationType", typesFilter
));
194 table
.setContainerDataSource(container
);
196 table
.setVisibleColumns("text");
198 table
.setVisibleColumns("text", "annotationType");
200 table
.setEditable(true);
201 if(newValue
.size() > 1){
202 table
.setPageLength(2);
210 protected Component
initContent() {
211 root
.addComponentAsFirst(table
);
213 newButton
.addClickListener(e
-> addAnnotation());
222 private void addAnnotation() {
223 container
.addItem(newInstance());
224 if(container
.size() > 1){
225 table
.setPageLength(2);
232 private Annotation
newInstance() {
233 return Annotation
.NewInstance(null, Language
.DEFAULT());
240 public Class
<?
extends List
<Annotation
>> getType() {
241 return (Class
<?
extends List
<Annotation
>>) new ArrayList
<Annotation
>().getClass();
245 * @param buildTermItemContainer
247 public void setAnnotationTypeItemContainer(BeanItemContainer
<DefinedTermBase
> typeSelectItemContainer
) {
248 this.typeSelectItemContainer
= typeSelectItemContainer
;
255 public void setReadOnly(boolean readOnly
) {
256 super.setReadOnly(readOnly
);
257 setDeepReadOnly(readOnly
, table
, null);
260 public void withNewButton(boolean withNewButton
) {
261 if(this.withNewButton
!= withNewButton
){
262 if(!this.withNewButton
){
263 root
.addComponent(newButton
);
265 root
.removeComponent(newButton
);
267 this.withNewButton
= withNewButton
;