2 * Copyright (C) 2017 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
.vaadin
.component
;
11 import org
.vaadin
.viritin
.fields
.LazyComboBox
.FilterableCountProvider
;
12 import org
.vaadin
.viritin
.fields
.LazyComboBox
.FilterablePagingProvider
;
14 import com
.vaadin
.data
.Property
;
15 import com
.vaadin
.data
.fieldgroup
.FieldGroup
;
16 import com
.vaadin
.data
.util
.converter
.Converter
.ConversionException
;
17 import com
.vaadin
.ui
.Button
;
18 import com
.vaadin
.ui
.Button
.ClickListener
;
19 import com
.vaadin
.ui
.Component
;
20 import com
.vaadin
.ui
.CssLayout
;
21 import com
.vaadin
.ui
.themes
.ValoTheme
;
23 import eu
.etaxonomy
.cdm
.vaadin
.component
.ButtonFactory
;
24 import eu
.etaxonomy
.cdm
.vaadin
.event
.NestedButtonStateUpdater
;
27 * @author a.kohlbecker
31 public class ToOneRelatedEntityCombobox
<V
extends Object
> extends CompositeCustomField
<V
>
32 implements ToOneRelatedEntityField
<V
>, ReloadableSelect
, EntitySupport
<V
> {
34 private static final long serialVersionUID
= 6277565876657520311L;
36 public static final String PRIMARY_STYLE
= "v-related-entity-combobox";
38 private Class
<V
> type
;
40 private CssLayout container
= new CssLayout();
42 private ReloadableLazyComboBox
<V
> lazySelect
;
44 private Button addButton
= ButtonFactory
.CREATE_NEW
.createButton();
45 private Button editButton
= ButtonFactory
.EDIT_ITEM
.createButton();
47 private NestedButtonStateUpdater
<V
> buttonUpdater
;
49 public ToOneRelatedEntityCombobox(String caption
, Class
<V
> type
){
52 lazySelect
= new ReloadableLazyComboBox
<V
>(type
);
53 addStyledComponents(lazySelect
, addButton
, editButton
);
54 addSizedComponents(lazySelect
, container
);
55 lazySelect
.addValueChangeListener(e
-> {
56 // update the itemContainer immediately so that the edit button acts on the chosen item
66 protected Component
initContent() {
67 container
.addComponents(lazySelect
, addButton
, editButton
);
68 setPrimaryStyleName(PRIMARY_STYLE
);
77 public Class
<?
extends V
> getType() {
85 protected void addDefaultStyles() {
86 container
.addStyleName(ValoTheme
.LAYOUT_COMPONENT_GROUP
);
93 public FieldGroup
getFieldGroup() {
100 public ReloadableLazyComboBox
<V
> getSelect() {
107 public void loadFrom(FilterablePagingProvider
<V
> filterablePagingProvider
, FilterableCountProvider filterableCountProvider
, int pageLength
) {
108 lazySelect
.loadFrom(filterablePagingProvider
, filterableCountProvider
, pageLength
);
113 * reload the selected entity from the persistent storage
116 public void reload() {
117 getSelect().reload();
124 public void setAddButtonEnabled(boolean enabled
) {
125 addButton
.setEnabled(enabled
);
133 public void addClickListenerAddEntity(ClickListener listener
) {
134 addButton
.addClickListener(listener
);
141 public void setEditButtonEnabled(boolean enabled
) {
142 editButton
.setEnabled(enabled
);
150 public void addClickListenerEditEntity(ClickListener listener
) {
151 editButton
.addClickListener(listener
);
156 public void replaceEntityValue(V bean
){
157 lazySelect
.replaceEntityValue(bean
);
161 public void selectNewItem(V bean
){
166 * Returns always currently selected item by
171 public V
getValue() {
173 return lazySelect
.getValue();
181 public void setValue(V newFieldValue
) throws com
.vaadin
.data
.Property
.ReadOnlyException
, ConversionException
{
182 lazySelect
.refresh();
183 lazySelect
.setValue(newFieldValue
);
184 lazySelect
.markAsDirty();
188 public void setPropertyDataSource(Property newDataSource
) {
189 lazySelect
.setPropertyDataSource(newDataSource
);
190 if(buttonUpdater
!= null){
191 buttonUpdater
.updateButtons(lazySelect
.getValue());
199 public Property
getPropertyDataSource() {
200 return lazySelect
.getPropertyDataSource();
207 public void setReadOnly(boolean readOnly
) {
208 super.setReadOnly(readOnly
);
209 setDeepReadOnly(readOnly
, getContent(), null);
210 if(buttonUpdater
!= null){
211 buttonUpdater
.updateButtons(lazySelect
.getValue());
219 public void setNestedButtonStateUpdater(NestedButtonStateUpdater
<V
> buttonUpdater
) {
220 this.buttonUpdater
= buttonUpdater
;
221 lazySelect
.addValueChangeListener(buttonUpdater
);