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