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;
35  
36  import info.magnolia.ui.framework.datasource.components.ItemResolver;
37  
38  import java.util.Objects;
39  import java.util.Optional;
40  
41  import com.vaadin.ui.Button;
42  import com.vaadin.ui.Component;
43  import com.vaadin.ui.CustomField;
44  import com.vaadin.ui.HorizontalLayout;
45  import com.vaadin.ui.NativeButton;
46  import com.vaadin.ui.TextField;
47  import com.vaadin.ui.VerticalLayout;
48  
49  
50  /**
51   * A custom field displaying a text field and a button for bringing up a component enabling to choose an item.
52   *
53   * @param <T> item type to choose.
54   */
55  public class LinkField<T> extends CustomField<T> {
56  
57      private final VerticalLayout rootLayout = new VerticalLayout();
58      private final HorizontalLayout linkLayout = new HorizontalLayout();
59      private final TextField textField = new TextField();
60      private final Button selectButton = new NativeButton();
61      private final ItemResolver<T> itemResolver;
62  
63      private T value;
64  
65      private boolean editable;
66      private Component contentPreview;
67      private String buttonCaptionNew;
68      private String buttonCaptionOther;
69  
70      public LinkField(ItemResolver<T> itemResolver) {
71          Objects.requireNonNull(itemResolver);
72          this.itemResolver = itemResolver;
73      }
74  
75      @Override
76      protected Component initContent() {
77          addStyleName("linkfield");
78  
79          rootLayout.setSizeFull();
80          rootLayout.setSpacing(true);
81  
82          textField.setWidth(100, Unit.PERCENTAGE);
83          textField.addValueChangeListener(event -> {
84              // null value means "remove the currently selected link"
85              T value = itemResolver.getItemByPath(event.getValue()).orElse(null);
86              setValue(value);
87          });
88  
89          linkLayout.setSizeFull();
90          linkLayout.addComponent(textField);
91          linkLayout.setExpandRatio(textField, 1);
92  
93          if (!textField.isReadOnly()) {
94              selectButton.addStyleName("magnoliabutton");
95              linkLayout.addComponent(selectButton);
96              linkLayout.setExpandRatio(selectButton, 0);
97          }
98  
99          rootLayout.addComponent(linkLayout);
100 
101         addValueChangeListener(event -> updateComponents());
102         updateComponents();
103 
104         return rootLayout;
105     }
106 
107     @Override
108     public void setReadOnly(boolean readOnly) {
109         super.setReadOnly(readOnly);
110         textField.setReadOnly(readOnly);
111     }
112 
113     @Override
114     protected void doSetValue(T value) {
115         this.value = value;
116         // user is shown the item's path
117         Optional.ofNullable(value).ifPresent(val -> textField.setValue(itemResolver.getPath(val)));
118     }
119 
120     @Override
121     public T getValue() {
122         return value;
123     }
124 
125     @Override
126     public boolean isEmpty() {
127         return getValue() == null;
128     }
129 
130     public boolean isEditable() {
131         return editable;
132     }
133 
134     public void setEditable(boolean editable) {
135         this.editable = editable;
136     }
137 
138     public void setButtonCaptionNew(String buttonCaptionNew) {
139         this.buttonCaptionNew = buttonCaptionNew;
140     }
141 
142     public void setButtonCaptionOther(String buttonCaptionOther) {
143         this.buttonCaptionOther = buttonCaptionOther;
144     }
145 
146     public void setPlaceholder(String placeholder) {
147         textField.setPlaceholder(placeholder);
148     }
149 
150     public void setDisableOnClick(boolean disable) {
151         selectButton.setDisableOnClick(disable);
152     }
153 
154     public void addClickListener(Button.ClickListener buttonClickListener) {
155         selectButton.addClickListener(buttonClickListener);
156     }
157 
158     @Override
159     public void setEnabled(boolean enabled) {
160         super.setEnabled(enabled);
161         textField.setEnabled(enabled);
162         selectButton.setEnabled(enabled);
163     }
164 
165     public void setContentPreview(Component contentPreviewComponent) {
166         if (contentPreview != null) {
167             rootLayout.removeComponent(contentPreview);
168         }
169         rootLayout.addComponentAsFirst(contentPreviewComponent);
170         contentPreview = contentPreviewComponent;
171     }
172 
173     /*
174      * Set text field as read only if desired. In this case, remove the add button, else update the button label.
175      */
176     private void updateComponents() {
177         if (!isEditable() && !isEmpty()) {
178             textField.setReadOnly(true);
179             if (linkLayout.getComponentIndex(selectButton) != -1) {
180                 linkLayout.removeComponent(selectButton);
181             }
182         } else {
183             setButtonCaptionAndDescription();
184 
185         }
186     }
187 
188     private void setButtonCaptionAndDescription() {
189         final String caption = isEmpty() ? buttonCaptionNew : buttonCaptionOther;
190         selectButton.setCaption(caption);
191         selectButton.setDescription(caption);
192     }
193 }