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.i18nsystem.I18nizer;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.ui.chooser.definition.ChooserDefinition;
40  import info.magnolia.ui.datasource.ItemResolver;
41  import info.magnolia.ui.dialog.DialogDefinitionRegistry;
42  import info.magnolia.ui.editor.ItemPreviewDefinition;
43  import info.magnolia.ui.field.LinkField;
44  import info.magnolia.ui.field.LinkFieldDefinition;
45  import info.magnolia.ui.field.SelectFieldSupport;
46  import info.magnolia.ui.framework.overlay.ChooserController;
47  import info.magnolia.ui.framework.overlay.ChooserController.ChooseResult;
48  import info.magnolia.ui.preview.ItemPreviewComponent;
49  
50  import java.util.Optional;
51  import java.util.concurrent.CompletableFuture;
52  
53  import javax.inject.Inject;
54  
55  import com.vaadin.data.HasValue;
56  import com.vaadin.ui.ComboBox;
57  
58  /**
59   * Creates and initializes a {@link LinkField} field based on a {@link LinkFieldDefinition}.
60   *
61   * @param <D> the type of link field definition.
62   */
63  public class LinkFieldFactory<T, D extends LinkFieldDefinition<T>> extends ComboBoxFieldFactory<T, LinkFieldDefinition<T>> {
64  
65      private final ChooserController chooserController;
66      private final DialogDefinitionRegistry dialogDefinitionRegistry;
67      private final I18nizer i18nizer;
68  
69      @Inject
70      public LinkFieldFactory(D definition, ComponentProvider componentProvider, ChooserController chooserController, SelectFieldSupport<T> selectFieldSupport, DialogDefinitionRegistry dialogDefinitionRegistry, I18nizer i18nizer) {
71          super(definition, componentProvider, selectFieldSupport);
72          this.dialogDefinitionRegistry = dialogDefinitionRegistry;
73          this.i18nizer = i18nizer;
74          this.chooserController = chooserController;
75      }
76  
77      /**
78       * @deprecated since 6.2.3. Use {@link #LinkFieldFactory(info.magnolia.ui.field.LinkFieldDefinition, info.magnolia.objectfactory.ComponentProvider, info.magnolia.ui.framework.overlay.ChooserController, info.magnolia.ui.field.SelectFieldSupport, info.magnolia.ui.dialog.DialogDefinitionRegistry, info.magnolia.i18nsystem.I18nizer)} instead.
79       */
80      @Deprecated
81      public LinkFieldFactory(D definition, ComponentProvider componentProvider, ChooserController chooserController, SelectFieldSupport<T> selectFieldSupport) {
82          this(definition, componentProvider, chooserController, selectFieldSupport, Components.getComponent(DialogDefinitionRegistry.class), Components.getComponent(I18nizer.class));
83      }
84  
85      /**
86       * @deprecated since 6.2.1. Use {@link #LinkFieldFactory(info.magnolia.ui.field.LinkFieldDefinition, info.magnolia.objectfactory.ComponentProvider, info.magnolia.ui.framework.overlay.ChooserController, info.magnolia.ui.field.SelectFieldSupport, info.magnolia.ui.dialog.DialogDefinitionRegistry, info.magnolia.i18nsystem.I18nizer)} instead.
87       */
88      @Deprecated
89      public LinkFieldFactory(LinkFieldDefinition<T> definition, ComponentProvider componentProvider, ItemResolver<T> itemResolver, ChooserController chooserController) {
90          this((D) definition, componentProvider, chooserController, Components.getComponent(SelectFieldSupport.class));
91      }
92  
93      @Override
94      public HasValue<T> createField() {
95          LinkField<T> linkField = new LinkField<>((ComboBox<T>) super.createField());
96          linkField.setCaption(getDefinition().getLabel());
97          linkField.setButtonCaptionNew(getDefinition().getButtonSelectNewLabel());
98          linkField.setButtonCaptionOther(getDefinition().getButtonSelectOtherLabel());
99          linkField.setEditable(getDefinition().isEditable());
100         linkField.setDisableOnClick(true);
101         linkField.addClickListener(event -> openChooser(linkField));
102 
103         newItemPreviewComponent().ifPresent(itemPreviewComponent -> {
104             linkField.addValueChangeListener(event -> {
105                 T item = event.getValue();
106                 itemPreviewComponent.onValueChange(item);
107                 itemPreviewComponent.setVisible(item != null);
108             });
109             linkField.setContentPreview(itemPreviewComponent);
110         });
111 
112         return linkField;
113     }
114 
115     CompletableFuture<ChooseResult<T>> openChooser(LinkField<T> linkField) {
116         ChooserDefinition<T, ?> chooser = getDefinition().getChooser(); //compatibility
117         if (chooser == null) {
118             chooser = (ChooserDefinition<T, ?>) i18nizer.decorate(dialogDefinitionRegistry.getProvider(definition.getChooserId()).get());
119         }
120         return chooserController.openChooser(chooser, linkField.getValue())
121                 .whenComplete((ChooseResult<T> result, Throwable err) -> {
122                     linkField.setEnabled(true);
123                     if (err != null) {
124                         throw new RuntimeException(err);
125                     }
126                     result.getChoice().ifPresent(linkField::setValue);
127                 });
128     }
129 
130     protected Optional<? extends ItemPreviewComponent<T>> newItemPreviewComponent() {
131         return Optional.ofNullable(getDefinition().getPreview())
132                 .map(ItemPreviewDefinition::getImplementationClass)
133                 .map(clazz -> componentProvider.newInstance(clazz));
134     }
135 
136 }