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