View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.form.field;
35  
36  import info.magnolia.annotation.deprecation.MgnlDeprecated;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.api.app.AppController;
39  import info.magnolia.ui.api.context.UiContext;
40  import info.magnolia.ui.form.field.definition.LinkFieldDefinition;
41  
42  import java.util.Optional;
43  
44  import org.apache.commons.lang3.StringUtils;
45  
46  import com.vaadin.ui.Alignment;
47  import com.vaadin.ui.Button;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.NativeButton;
50  import com.vaadin.v7.data.util.converter.Converter;
51  import com.vaadin.v7.ui.CustomField;
52  import com.vaadin.v7.ui.HorizontalLayout;
53  import com.vaadin.v7.ui.TextField;
54  import com.vaadin.v7.ui.VerticalLayout;
55  
56  /**
57   * A base custom field comprising a text field and a button placed to its immediate right.
58   * A {@link PropertyTranslator} can be set in order to have a different display and property stored.
59   * For example, display can be the Item path and value stored is the identifier of the Item.
60   * @deprecated since 6.0. Use new framework and {@link info.magnolia.ui.field.LinkField}.
61   */
62  @MgnlDeprecated(since = "6.0")
63  public class LinkField extends CustomField<String> {
64      // Define layout and component
65      private final VerticalLayout rootLayout = new VerticalLayout();
66      private final HorizontalLayout linkLayout = new HorizontalLayout();
67      private final TextField textField = new TextField();
68      private final Button selectButton = new NativeButton();
69      private Component contentPreview;
70      private String buttonCaptionNew;
71      private String buttonCaptionOther;
72      private boolean isFieldEditable;
73  
74      public LinkField() {
75      }
76  
77      /**
78       * @deprecated since 5.5, use {@link #LinkField()} instead, none of arguments are required.
79       */
80      @Deprecated
81      public LinkField(LinkFieldDefinition linkFieldDefinition, ComponentProvider componentProvider) {
82          this();
83      }
84  
85      @Deprecated
86      public LinkField(LinkFieldDefinition linkFieldDefinition, AppController appController, UiContext uiContext, ComponentProvider componentProvider) {
87          this();
88      }
89  
90      @Override
91      protected Component initContent() {
92          addStyleName("linkfield");
93          // Initialize root
94          rootLayout.setSizeFull();
95          rootLayout.setSpacing(true);
96          // Handle Text Field
97          textField.setWidth(100, Unit.PERCENTAGE);
98          textField.setNullRepresentation("");
99          textField.setNullSettingAllowed(true);
100         textField.addValueChangeListener(event ->
101                 setValue(String.valueOf(Optional.ofNullable(event.getProperty().getValue()).orElse(""))));
102 
103         // Handle Link Layout (Text Field & Select Button)
104         linkLayout.setSizeFull();
105         linkLayout.addComponent(textField);
106         linkLayout.setExpandRatio(textField, 1);
107         linkLayout.setComponentAlignment(textField, Alignment.MIDDLE_LEFT);
108         // Only Handle Select button if the Text field is not Read Only.
109         if (!textField.isReadOnly()) {
110             selectButton.addStyleName("magnoliabutton");
111             linkLayout.addComponent(selectButton);
112             linkLayout.setExpandRatio(selectButton, 0);
113             linkLayout.setComponentAlignment(selectButton, Alignment.MIDDLE_RIGHT);
114         }
115         setButtonCaption(StringUtils.EMPTY);
116         rootLayout.addComponent(linkLayout);
117 
118         this.addValueChangeListener(event -> updateComponents(getValue()));
119 
120         updateComponents(getValue());
121         return rootLayout;
122     }
123 
124     public TextField getTextField() {
125         return this.textField;
126     }
127 
128     public Button getSelectButton() {
129         return this.selectButton;
130     }
131 
132     @Override
133     public String getValue() {
134         return super.getValue();
135     }
136 
137     @Override
138     public void setValue(String newValue) throws ReadOnlyException, Converter.ConversionException {
139         super.setValue(newValue);
140     }
141 
142     @Override
143     protected void setInternalValue(String newValue) {
144         super.setInternalValue(newValue);
145         boolean readFlag = textField.isReadOnly();
146         textField.setReadOnly(false);
147         textField.setValue(newValue);
148         textField.setReadOnly(readFlag);
149     }
150 
151     @Override
152     public void setReadOnly(boolean readOnly) {
153         super.setReadOnly(readOnly);
154         textField.setReadOnly(readOnly);
155     }
156 
157     /**
158      * Update the Link component. <br>
159      * - Set text Field as read only if desired. In this case remove the add button.
160      * - If it is not read only. update the button label.
161      */
162     private void updateComponents(String currentValue) {
163         if (!isFieldEditable && StringUtils.isNotBlank(currentValue)) {
164             textField.setReadOnly(true);
165             if (linkLayout.getComponentIndex(selectButton) != -1) {
166                 linkLayout.removeComponent(selectButton);
167             }
168         } else {
169             setButtonCaption(currentValue);
170         }
171     }
172 
173     @Override
174     public Class<String> getType() {
175         return String.class;
176     }
177 
178     private void setButtonCaption(String value) {
179         if (StringUtils.isNotBlank(value)) {
180             selectButton.setCaption(buttonCaptionOther);
181             selectButton.setDescription(buttonCaptionOther);
182         } else {
183             selectButton.setCaption(buttonCaptionNew);
184             selectButton.setDescription(buttonCaptionNew);
185         }
186     }
187 
188     public void setContentPreview(Component contentPreviewComponent) {
189         if (contentPreview != null) {
190             rootLayout.removeComponent(contentPreview);
191         }
192         contentPreviewComponent.setVisible(StringUtils.isNotBlank(textField.getValue()));
193         rootLayout.addComponentAsFirst(contentPreviewComponent);
194         contentPreview = contentPreviewComponent;
195     }
196 
197     /**
198      * Caption section.
199      */
200     public void setButtonCaptionNew(String buttonCaptionNew) {
201         this.buttonCaptionNew = buttonCaptionNew;
202     }
203 
204     public void setButtonCaptionOther(String buttonCaptionOther) {
205         this.buttonCaptionOther = buttonCaptionOther;
206     }
207 
208     public void setTextFieldConverter(Converter textFieldConverter) {
209         this.setConverter(textFieldConverter);
210     }
211 
212     public void setFieldEditable(boolean isFieldEditable) {
213         this.isFieldEditable = isFieldEditable;
214     }
215 
216     @Override
217     public boolean isEmpty() {
218         return StringUtils.isEmpty(getValue());
219     }
220 }