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