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.browser.preview.PreviewProvider;
39  import info.magnolia.ui.framework.component.ItemPreviewComponent;
40  import info.magnolia.ui.field.LinkFieldDefinition;
41  import info.magnolia.ui.field.LinkField;
42  import info.magnolia.ui.framework.datasource.DatasourceSupport;
43  import info.magnolia.ui.framework.datasource.components.ItemResolver;
44  import info.magnolia.ui.framework.datasource.components.PropertySetFactory;
45  import info.magnolia.ui.framework.overlay.ChooserController;
46  import info.magnolia.ui.framework.overlay.ChooserController.ChooseResult;
47  
48  import java.util.Locale;
49  import java.util.Optional;
50  
51  import javax.inject.Inject;
52  
53  /**
54   * Creates and initializes a {@link LinkField} field based on a {@link LinkFieldDefinition}.
55   *
56   * @param <T> the item type we want to link to.
57   * @param <DEF> the type of datasource definition.
58   * @param <D> the type of link field definition.
59   */
60  public class LinkFieldFactory<T, DEF, D extends LinkFieldDefinition<T, DEF>> extends AbstractFieldFactory<D, T, LinkField<T>> {
61  
62      private final DatasourceSupport datasourceSupport;
63      private final ChooserController chooserController;
64  
65      @Inject
66      public LinkFieldFactory(D definition, ComponentProvider componentProvider, Locale locale, I18NAuthoringSupport i18nAuthoringSupport, DatasourceSupport datasourceSupport, ChooserController chooserController) {
67          super(definition, componentProvider, locale, i18nAuthoringSupport);
68  
69          Optional.ofNullable(definition.getChooser())
70                  .orElseThrow(() -> new IllegalArgumentException("Could not find a chooser dialog definition at LinkField definition " + definition.getName()));
71  
72          this.datasourceSupport = datasourceSupport;
73          this.chooserController = chooserController;
74      }
75  
76      @Override
77      protected LinkField<T> createFieldComponent() {
78          final DEF datasource = getDefinition().getDatasource();
79  
80          final ItemResolver<T> itemResolver = datasourceSupport.getDatasourceBundle(datasource).lookup(ItemResolver.class, datasource)
81                  .orElseThrow(() -> new IllegalArgumentException("Could not resolve item resolver from definition " + getDefinition().getName()));
82  
83          final LinkField<T> linkField = new LinkField<>(itemResolver);
84  
85          Optional.ofNullable(getDefinition().getPlaceholder()).filter(p -> !isMessageKey(p))
86                  .ifPresent(placeholder -> linkField.setPlaceholder(placeholder));
87  
88          linkField.setButtonCaptionNew(isMessageKey(getDefinition().getButtonSelectNewLabel()) ?
89                  getDefinition().getButtonSelectNewDefaultLabel() : getDefinition().getButtonSelectNewLabel());
90  
91          linkField.setButtonCaptionOther(isMessageKey(getDefinition().getButtonSelectOtherLabel()) ?
92                  getDefinition().getButtonSelectOtherDefaultLabel() : getDefinition().getButtonSelectOtherLabel());
93  
94          linkField.setEditable(getDefinition().isEditable());
95          linkField.setDisableOnClick(true);
96  
97          linkField.addClickListener(event ->
98                  chooserController.openChooser(getDefinition().getChooser(), linkField.getValue()).whenComplete((ChooseResult<T> result, Throwable err) -> {
99  
100                     linkField.setEnabled(true);
101 
102                     if (err != null) {
103                         throw new RuntimeException(err);
104                     }
105 
106                     result.getChoice().ifPresent(linkField::setValue);
107                 }));
108 
109         newItemPreviewComponent().ifPresent(itemPreviewComponent -> {
110             linkField.addValueChangeListener(event -> {
111                 itemPreviewComponent.onValueChange((event.getValue()));
112                 itemPreviewComponent.setVisible(event.getValue() != null);
113             });
114             linkField.setContentPreview(itemPreviewComponent);
115         });
116 
117         return linkField;
118     }
119 
120     protected Optional<? extends ItemPreviewComponent<T>> newItemPreviewComponent() {
121         Class<? extends ItemPreviewComponent<T>> previewComponentClass = Optional.ofNullable(getDefinition().getPreview())
122                 .map(def -> def.getImplementationClass()).orElse(null);
123 
124         return Optional.ofNullable(previewComponentClass).map(clazz -> {
125             final DEF datasource = getDefinition().getDatasource();
126 
127             final PreviewProvider<T> previewProvider = datasourceSupport.getDatasourceBundle(datasource).lookup(PreviewProvider.class, datasource)
128                     .orElse(null);
129 
130             final PropertySetFactory<T> propertySetFactory = datasourceSupport.getDatasourceBundle(datasource).lookup(PropertySetFactory.class, datasource)
131                     .orElseThrow(() -> new IllegalArgumentException("Could not resolve PropertySetFactory for datasource " + datasource));
132 
133             return getComponentProvider().newInstance(clazz, propertySetFactory, previewProvider);
134         });
135     }
136 }