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.AbstractLinkField;
44  import info.magnolia.ui.field.LinkField;
45  import info.magnolia.ui.field.LinkFieldDefinition;
46  import info.magnolia.ui.field.SelectFieldSupport;
47  import info.magnolia.ui.field.TextLinkField;
48  import info.magnolia.ui.framework.overlay.ChooserController;
49  import info.magnolia.ui.framework.overlay.ChooserController.ChooseResult;
50  import info.magnolia.ui.preview.ItemPreviewComponent;
51  
52  import java.util.Optional;
53  import java.util.concurrent.CompletableFuture;
54  
55  import javax.inject.Inject;
56  
57  import com.vaadin.data.HasValue;
58  import com.vaadin.data.provider.DataProvider;
59  import com.vaadin.ui.ComboBox;
60  
61  /**
62   * Creates and initializes a {@link LinkField} field based on a {@link LinkFieldDefinition}.
63   *
64   * @param <D> the type of link field definition.
65   */
66  public class LinkFieldFactory<T, D extends LinkFieldDefinition<T>> extends ComboBoxFieldFactory<T, LinkFieldDefinition<T>> {
67  
68      static final String NO_DROPDOWN_BUTTON = "no-dropdown-button";
69      static final String WITH_DROPDOWN_BUTTON = "with-dropdown-button";
70  
71      private final ChooserController chooserController;
72      private final DialogDefinitionRegistry dialogDefinitionRegistry;
73      private final I18nizer i18nizer;
74  
75      @Inject
76      public LinkFieldFactory(D definition, ComponentProvider componentProvider, ChooserController chooserController, SelectFieldSupport<T> selectFieldSupport, DialogDefinitionRegistry dialogDefinitionRegistry, I18nizer i18nizer) {
77          super(definition, componentProvider, selectFieldSupport);
78          this.dialogDefinitionRegistry = dialogDefinitionRegistry;
79          this.i18nizer = i18nizer;
80          this.chooserController = chooserController;
81      }
82  
83      /**
84       * @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.
85       */
86      @Deprecated
87      public LinkFieldFactory(D definition, ComponentProvider componentProvider, ChooserController chooserController, SelectFieldSupport<T> selectFieldSupport) {
88          this(definition, componentProvider, chooserController, selectFieldSupport, Components.getComponent(DialogDefinitionRegistry.class), Components.getComponent(I18nizer.class));
89      }
90  
91      /**
92       * @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.
93       */
94      @Deprecated
95      public LinkFieldFactory(LinkFieldDefinition<T> definition, ComponentProvider componentProvider, ItemResolver<T> itemResolver, ChooserController chooserController) {
96          this((D) definition, componentProvider, chooserController, Components.getComponent(SelectFieldSupport.class));
97      }
98  
99      @Override
100     public HasValue<T> createField() {
101         final AbstractLinkField<T, T> linkField;
102         if (getDefinition().isShowOptions() || !definition.isTextInputAllowed()) {
103             linkField = new LinkField<>((ComboBox<T>) super.createField());
104         } else {
105             linkField = componentProvider.newInstance(TextLinkField.class);
106         }
107         linkField.setCaption(getDefinition().getLabel());
108         linkField.setButtonCaptionNew(getDefinition().getButtonSelectNewLabel());
109         linkField.setEditable(getDefinition().isEditable());
110         linkField.setReadOnly(getDefinition().isReadOnly());
111         linkField.setDisableOnClick(true);
112         linkField.addClickListener(event -> openChooser(linkField));
113         linkField.addStyleName(getDefinition().isShowOptions() ? WITH_DROPDOWN_BUTTON : NO_DROPDOWN_BUTTON);
114         newItemPreviewComponent().ifPresent(linkField::setContentPreview);
115         return linkField;
116     }
117 
118     @Override
119     protected DataProvider<T, String> getDataProvider() {
120         return getDefinition().isShowOptions() ? super.getDataProvider() : EmptyDataProvider.getInstance();
121     }
122 
123     CompletableFuture<ChooseResult<T>> openChooser(AbstractLinkField<T, T> linkField) {
124         ChooserDefinition<T, ?> chooser = getDefinition().getChooser(); //compatibility
125         if (chooser == null) {
126             chooser = (ChooserDefinition<T, ?>) i18nizer.decorate(dialogDefinitionRegistry.getProvider(definition.getChooserId()).get());
127         }
128         return chooserController.openChooser(chooser, linkField.getSelectedItem().orElse(null))
129                 .whenComplete((ChooseResult<T> result, Throwable err) -> {
130                     linkField.setEnabled(true);
131                     if (err != null) {
132                         throw new RuntimeException(err);
133                     }
134                     result.getChoice().ifPresent(linkField::setSelectedItem);
135                 });
136     }
137 
138     protected Optional<? extends ItemPreviewComponent<T>> newItemPreviewComponent() {
139         return Optional.ofNullable(getDefinition().getPreview())
140                 .map(ItemPreviewDefinition::getImplementationClass)
141                 .map(clazz -> componentProvider.newInstance(clazz));
142     }
143 }