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
;
16 import org
.vaadin
.viritin
.FilterableListContainer
;
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
;
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
;
42 * @author a.kohlbecker
46 public class FilterableAnnotationsField
extends CompositeCustomField
<List
<Annotation
>> {
48 private static final long serialVersionUID
= -8258550787601028813L;
50 private CssLayout root
= new CssLayout();
52 private Table table
= new Table();
54 private List
<AnnotationType
> typesFilter
= null;
56 private Annotation emptyDefaultAnnotation
= Annotation
.NewInstance(null, Language
.DEFAULT());
58 private BeanItemContainer
<DefinedTermBase
> typeSelectItemContainer
;
60 private FilterableListContainer
<Annotation
> container
;
62 public FilterableAnnotationsField() {
66 public FilterableAnnotationsField(String caption
) {
69 // annotations are always sets
70 setConverter(new SetToListConverter
<Annotation
>());
72 root
.setWidth(100, Unit
.PERCENTAGE
);
75 table
.setPageLength(1);
76 table
.setColumnHeaderMode(ColumnHeaderMode
.HIDDEN
);
77 table
.setWidth(100, Unit
.PERCENTAGE
);
78 table
.setTableFieldFactory(new DefaultFieldFactory() {
80 private static final long serialVersionUID
= 5437750882205859178L;
83 public Field
<?
> createField(Item item
, Object propertyId
, Component uiContext
) {
85 Field
<?
> field
= createField(propertyId
);
87 field
= super.createField(item
, propertyId
, uiContext
);
94 public Field
<?
> createField(Container container
, Object itemId
, Object propertyId
, Component uiContext
) {
96 Field
<?
> field
= createField(propertyId
);
99 field
= super.createField(container
, itemId
, propertyId
, uiContext
);
104 protected Field
<?
> createField(Object propertyId
) {
105 Field
<?
> field
= null;
106 if(propertyId
.equals("text")){
107 TextArea ta
= new TextArea();
108 ta
.setNullRepresentation("");
109 ta
.setWidth(100, Unit
.PERCENTAGE
);
111 } else if(propertyId
.equals("annotationType")) {
112 ListSelect select
= new ListSelect();
113 select
.setContainerDataSource(typeSelectItemContainer
);
114 select
.setWidth(100, Unit
.PIXELS
);
118 field
.setStyleName(table
.getStyleName());
123 addStyledComponent(table
);
127 public void setAnnotationTypesVisible(AnnotationType
... types
){
128 typesFilter
= Arrays
.asList(types
);
135 protected void addDefaultStyles() {
136 // no default styles here
143 public FieldGroup
getFieldGroup() {
144 // holds a Container instead // TODO can this cause a NPE?
149 public void commit() throws SourceException
, InvalidValueException
{
151 Collection
<Filter
> filters
= container
.getContainerFilters();
153 for(Filter filter
: filters
){
154 container
.addContainerFilter(filter
);
156 System
.err
.println(container
.size());
165 protected List
<Annotation
> getInternalValue() {
166 if(container
== null || container
.getItemIds() == null){
169 return new ArrayList
<>(container
.getItemIds());
174 protected void setInternalValue(List
<Annotation
> newValue
) {
175 boolean hasIncludeFilter
= typesFilter
!= null && !typesFilter
.isEmpty();
176 boolean onlyOneType
= hasIncludeFilter
&& typesFilter
.size() == 1;
178 if(newValue
.isEmpty()){
179 newValue
.add(emptyDefaultAnnotation
);
181 emptyDefaultAnnotation
.setAnnotationType(typesFilter
.get(0));
184 container
= new FilterableListContainer
<Annotation
>(newValue
);
185 if(hasIncludeFilter
){
186 container
.addContainerFilter(new CdmTermFilter
<AnnotationType
>("annotationType", typesFilter
));
188 table
.setContainerDataSource(container
);
190 table
.setVisibleColumns("text");
192 table
.setVisibleColumns("text", "annotationType");
194 table
.setEditable(true);
201 protected Component
initContent() {
202 root
.addComponent(table
);
211 public Class
<?
extends List
<Annotation
>> getType() {
212 return (Class
<?
extends List
<Annotation
>>) new ArrayList
<Annotation
>().getClass();
216 * @param buildTermItemContainer
218 public void setAnnotationTypeItemContainer(BeanItemContainer
<DefinedTermBase
> typeSelectItemContainer
) {
219 this.typeSelectItemContainer
= typeSelectItemContainer
;
226 public void setReadOnly(boolean readOnly
) {
227 super.setReadOnly(readOnly
);
228 setDeepReadOnly(readOnly
, table
, null);