fix #7588 updating buttons on setDataSource
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / vaadin / component / ToOneRelatedEntityCombobox.java
1 /**
2 * Copyright (C) 2017 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.vaadin.component;
10
11 import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
12 import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
13
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;
22
23 import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
24 import eu.etaxonomy.cdm.vaadin.event.NestedButtonStateUpdater;
25
26 /**
27 * @author a.kohlbecker
28 * @since May 24, 2017
29 *
30 */
31 public class ToOneRelatedEntityCombobox<V extends Object> extends CompositeCustomField<V>
32 implements ToOneRelatedEntityField<V>, ReloadableSelect, EntitySupport<V> {
33
34 private static final long serialVersionUID = 6277565876657520311L;
35
36 public static final String PRIMARY_STYLE = "v-related-entity-combobox";
37
38 private Class<V> type;
39
40 private CssLayout container = new CssLayout();
41
42 private ReloadableLazyComboBox<V> lazySelect;
43
44 private Button addButton = ButtonFactory.CREATE_NEW.createButton();
45 private Button editButton = ButtonFactory.EDIT_ITEM.createButton();
46
47 private NestedButtonStateUpdater<V> buttonUpdater;
48
49 public ToOneRelatedEntityCombobox(String caption, Class<V> type){
50 this.type = type;
51 setCaption(caption);
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
57 lazySelect.commit();
58 });
59 }
60
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 protected Component initContent() {
67 container.addComponents(lazySelect, addButton, editButton);
68 setPrimaryStyleName(PRIMARY_STYLE);
69 addDefaultStyles();
70 return container;
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @Override
77 public Class<? extends V> getType() {
78 return type;
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 @Override
85 protected void addDefaultStyles() {
86 container.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override
93 public FieldGroup getFieldGroup() {
94 return null;
95 }
96
97 /**
98 * @return the select
99 */
100 public ReloadableLazyComboBox<V> getSelect() {
101 return lazySelect;
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 public void loadFrom(FilterablePagingProvider<V> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength) {
108 lazySelect.loadFrom(filterablePagingProvider, filterableCountProvider, pageLength);
109
110 }
111
112 /**
113 * reload the selected entity from the persistent storage
114 */
115 @Override
116 public void reload() {
117 getSelect().reload();
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 @Override
124 public void setAddButtonEnabled(boolean enabled) {
125 addButton.setEnabled(enabled);
126 }
127
128
129 /**
130 * {@inheritDoc}
131 */
132 @Override
133 public void addClickListenerAddEntity(ClickListener listener) {
134 addButton.addClickListener(listener);
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 @Override
141 public void setEditButtonEnabled(boolean enabled) {
142 editButton.setEnabled(enabled);
143 }
144
145
146 /**
147 * {@inheritDoc}
148 */
149 @Override
150 public void addClickListenerEditEntity(ClickListener listener) {
151 editButton.addClickListener(listener);
152 }
153
154
155 @Override
156 public void replaceEntityValue(V bean){
157 lazySelect.replaceEntityValue(bean);
158 }
159
160 @Override
161 public void selectNewItem(V bean){
162 setValue(bean);
163 }
164
165 /**
166 * Returns always currently selected item by
167 *
168 * {@inheritDoc}
169 */
170 @Override
171 public V getValue() {
172 lazySelect.commit();
173 return lazySelect.getValue();
174 }
175
176
177 /**
178 * {@inheritDoc}
179 */
180 @Override
181 public void setValue(V newFieldValue) throws com.vaadin.data.Property.ReadOnlyException, ConversionException {
182 lazySelect.refresh();
183 lazySelect.setValue(newFieldValue);
184 lazySelect.markAsDirty();
185 }
186
187 @Override
188 public void setPropertyDataSource(Property newDataSource) {
189 lazySelect.setPropertyDataSource(newDataSource);
190 if(buttonUpdater != null){
191 buttonUpdater.updateButtons(lazySelect.getValue());
192 }
193 }
194
195 /**
196 * {@inheritDoc}
197 */
198 @Override
199 public Property getPropertyDataSource() {
200 return lazySelect.getPropertyDataSource();
201 }
202
203 /**
204 * {@inheritDoc}
205 */
206 @Override
207 public void setReadOnly(boolean readOnly) {
208 super.setReadOnly(readOnly);
209 setDeepReadOnly(readOnly, getContent(), null);
210 if(buttonUpdater != null){
211 buttonUpdater.updateButtons(lazySelect.getValue());
212 }
213 }
214
215 /**
216 * {@inheritDoc}
217 */
218 @Override
219 public void setNestedButtonStateUpdater(NestedButtonStateUpdater<V> buttonUpdater) {
220 this.buttonUpdater = buttonUpdater;
221 lazySelect.addValueChangeListener(buttonUpdater);
222 }
223
224
225 }