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