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.field.AbstractSelectFieldDefinition;
38  import info.magnolia.ui.field.SelectFieldSupport;
39  import info.magnolia.ui.framework.datasource.definition.DatasourceDefinition;
40  
41  import java.lang.reflect.InvocationTargetException;
42  import java.lang.reflect.Method;
43  import java.util.function.Consumer;
44  import java.util.function.Predicate;
45  import java.util.stream.Stream;
46  
47  import javax.inject.Inject;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.vaadin.data.HasDataProvider;
53  import com.vaadin.data.HasFilterableDataProvider;
54  import com.vaadin.data.HasValue;
55  import com.vaadin.data.provider.DataProvider;
56  import com.vaadin.data.provider.DataProviderWrapper;
57  import com.vaadin.data.provider.Query;
58  import com.vaadin.ui.AbstractListing;
59  import com.vaadin.ui.IconGenerator;
60  import com.vaadin.ui.ItemCaptionGenerator;
61  
62  /**
63   * Creates and initializes a selection field based on a field definition.
64   *
65   * @param <D> type of definition
66   * @param <T> type of select option
67   */
68  public abstract class AbstractSelectFieldFactory<D extends AbstractSelectFieldDefinition<T, DatasourceDefinition>, T> extends AbstractFieldFactory<T, D> {
69  
70      protected static final Logger log = LoggerFactory.getLogger(AbstractSelectFieldFactory.class);
71  
72      private final SelectFieldSupport<T> selectFieldSupport;
73  
74      @Inject
75      public AbstractSelectFieldFactory(D definition, ComponentProvider componentProvider, SelectFieldSupport<T> selectFieldSupport) {
76          super(definition, componentProvider);
77          this.selectFieldSupport = selectFieldSupport;
78      }
79  
80      @Override
81      public HasValue<T> createField() {
82          HasValue<T> selectField = super.createField();
83  
84          if (selectField instanceof HasDataProvider) {
85              ((HasDataProvider<T>) selectField).setDataProvider(getDataProvider());
86          }
87  
88          if (selectField instanceof HasFilterableDataProvider) {
89              ((HasFilterableDataProvider) selectField).setDataProvider(getDataProvider());
90          }
91  
92          if (selectField instanceof AbstractListing) {
93              accessGeneratorSetter("setItemCaptionGenerator", ItemCaptionGenerator.class, selectField).accept(getCaptionGenerator());
94              accessGeneratorSetter("setItemIconGenerator", IconGenerator.class, selectField).accept(selectFieldSupport.getIconGenerator());
95          }
96  
97          return selectField;
98      }
99  
100     protected ItemCaptionGenerator<T> getCaptionGenerator() {
101         return selectFieldSupport.getItemCaptionGenerator();
102     }
103 
104     protected DataProvider<T, ?> getDataProvider() {
105         final DataProvider wrappedDataProvider = this.selectFieldSupport.getDataProvider();
106         return new DataProviderWrapper<T, String, Object>(wrappedDataProvider) {
107                     @Override
108                     protected Object getFilter(Query<T, String> query) {
109                         return null;
110                     }
111 
112                     @Override
113                     public Stream<T> fetch(Query<T, String> t) {
114                         return super.fetch(t).filter(getFilterPredicate(t));
115                     }
116 
117                     @Override
118                     public int size(Query<T, String> t) {
119                         return (int) fetch(t).count();
120                     }
121 
122                     private Predicate<T> getFilterPredicate(Query<T, String> t) {
123                         switch (getDefinition().getFilteringMode()) {
124                         case CONTAINS:
125                             return item -> getCaptionGenerator().apply(item).contains(t.getFilter().orElse(""));
126                         case STARTSWITH:
127                             return item -> getCaptionGenerator().apply(item).startsWith(t.getFilter().orElse(""));
128                         default:
129                             return anEnum -> true;
130                         }
131                     }
132                 };
133     }
134 
135     // unfortunately AbstractSingleSelect#setItemCaptionGenerator/#setIconGenerator, unlike AbstractMultiSelect, is not public
136     // see also https://github.com/vaadin/framework/issues/10611
137     private <GENERATOR> Consumer<GENERATOR> accessGeneratorSetter(String methodName, Class<GENERATOR> argType, Object field) {
138         try {
139             final Method method = field.getClass().getMethod(methodName, argType);
140             method.setAccessible(true);
141             return generator -> {
142                 try {
143                     method.invoke(field, generator);
144                 } catch (IllegalAccessException | InvocationTargetException e) {
145                     log.error("An error occurred while trying to invoke method", e);
146                 }
147             };
148         } catch (NoSuchMethodException e) {
149             return generator -> {
150             };
151         } catch (Exception e) {
152             throw new RuntimeException("An error occurred while trying to access method", e);
153         }
154     }
155 }