View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.field.factory;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38  import info.magnolia.ui.contentapp.DataFilter;
39  import info.magnolia.ui.field.AbstractSelectFieldDefinition;
40  import info.magnolia.ui.framework.datasource.DatasourceBundle;
41  import info.magnolia.ui.framework.datasource.DatasourceSupport;
42  import info.magnolia.ui.framework.datasource.components.ItemDescriber;
43  import info.magnolia.ui.framework.datasource.components.SelectedItems;
44  
45  import java.lang.reflect.InvocationTargetException;
46  import java.lang.reflect.Method;
47  import java.util.Locale;
48  import java.util.Optional;
49  import java.util.function.Consumer;
50  
51  import javax.inject.Inject;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  import com.vaadin.data.HasDataProvider;
57  import com.vaadin.data.HasFilterableDataProvider;
58  import com.vaadin.data.HasValue;
59  import com.vaadin.data.provider.DataProvider;
60  import com.vaadin.ui.AbstractListing;
61  import com.vaadin.ui.Component;
62  import com.vaadin.ui.ItemCaptionGenerator;
63  
64  /**
65   * Creates and initializes a selection field based on a field definition.
66   *
67   * @param <DEF> type of datasource definition
68   * @param <D> type of definition
69   * @param <T> type of select option
70   * @param <F> select component type - can be a single or a multi select component
71   */
72  public abstract class AbstractSelectFieldFactory<DEF, D extends AbstractSelectFieldDefinition<T, DEF>, T, F extends Component & HasValue<T>> extends AbstractFieldFactory<D, T, F> {
73      protected static final Logger log = LoggerFactory.getLogger(AbstractSelectFieldFactory.class);
74  
75      private final DatasourceBundle<DEF> dataSourceBundle;
76  
77      @Inject
78      public AbstractSelectFieldFactory(ComponentProvider componentProvider, D definition, Locale locale, I18NAuthoringSupport i18NAuthoringSupport, DatasourceSupport datasourceSupport) {
79          super(definition, componentProvider, locale, i18NAuthoringSupport);
80          dataSourceBundle = Optional.ofNullable(definition.getDatasource()).map(datasourceSupport::getDatasourceBundle).orElse(null);
81  
82          if (dataSourceBundle == null) {
83             log.info("Select definition named [{}] doesn't provide a datasource. Option values may not be available.", definition.getName());
84          }
85      }
86  
87      @Override
88      public F createFieldComponent() {
89          final F selectionField = createSelectionField();
90  
91          if (selectionField instanceof HasDataProvider) {
92              getDataProvider().ifPresent(((HasDataProvider<T>) selectionField)::setDataProvider);
93          }
94  
95          if (selectionField instanceof HasFilterableDataProvider) {
96              getDataProvider().ifPresent(((HasFilterableDataProvider<T, DataFilter<DEF>>) selectionField)::setDataProvider);
97          }
98  
99          if (selectionField instanceof AbstractListing) {
100             getItemDescriber().ifPresent(describer -> {
101                 final ItemCaptionGenerator<T> itemCaptionGenerator = item -> describer.describe(SelectedItems.of((T) item));
102                 accessItemCaptionGeneratorSetter(selectionField).accept(itemCaptionGenerator);
103             });
104         }
105 
106         //TODO icon generator
107         return selectionField;
108     }
109 
110     /**
111      * Used to initialize the desired subclass of AbstractSelect field component. Subclasses must override this method.
112      */
113     abstract protected F createSelectionField();
114 
115     protected Optional<? extends ItemDescriber> getItemDescriber() {
116         return Optional.ofNullable(dataSourceBundle)
117                 .flatMap(dsb -> dsb.lookup(ItemDescriber.class, getDefinition().getDatasource()));
118     }
119 
120     protected Optional<? extends DataProvider> getDataProvider() {
121         return Optional.ofNullable(dataSourceBundle)
122                 .flatMap(bundle -> bundle.lookup(DataProvider.class, getDefinition().getDatasource()))
123                 // wrap the data provider in a filterable one
124                 // value is typically a string used to search options, e.g. in a combo box.
125                 // We convert it to a DataFilter cause that's the object data provider will eventually use for filtering
126                 .map(dataProvider -> dataProvider.withConvertedFilter(this::createDataFilter));
127     }
128 
129     private DataFilter<DEF> createDataFilter(Object value) {
130         final DataFilter<DEF> filter = new DataFilter<>();
131         filter.setValue(value);
132         filter.setFilteringMode(getDefinition().getFilteringMode());
133         return filter;
134     }
135 
136     // unfortunately AbstractSingleSelect#setItemCaptionGenerator, unlike AbstractMultiSelect, is not public
137     // see also https://github.com/vaadin/framework/issues/10611
138     private Consumer<ItemCaptionGenerator<T>> accessItemCaptionGeneratorSetter(Object field) {
139         try {
140             final Method method = field.getClass().getMethod("setItemCaptionGenerator", ItemCaptionGenerator.class);
141             method.setAccessible(true);
142             return itemCaptionGenerator -> {
143                 try {
144                     method.invoke(field, itemCaptionGenerator);
145                 } catch (IllegalAccessException | InvocationTargetException e) {
146                     log.error("An error occurred while trying to invoke method", e);
147                 }
148             };
149         } catch (NoSuchMethodException e) {
150             log.error("An error occurred while trying to access method", e);
151             return itemCaptionGenerator -> {};
152         }
153     }
154 }