View Javadoc
1   /**
2    * This file Copyright (c) 2020 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.framework.layout.field;
35  
36  import info.magnolia.icons.MagnoliaIcons;
37  import info.magnolia.objectfactory.annotation.Multibinding;
38  import info.magnolia.ui.field.EditorPropertyDefinition;
39  import info.magnolia.ui.field.MultiFieldDefinition;
40  import info.magnolia.ui.field.ValueBoundProperty;
41  
42  import java.util.Optional;
43  
44  import javax.inject.Inject;
45  
46  import org.apache.commons.lang3.StringUtils;
47  import org.vaadin.jonatan.contexthelp.ContextHelp;
48  
49  import com.vaadin.ui.AbstractField;
50  import com.vaadin.ui.Button;
51  import com.vaadin.ui.Component;
52  import com.vaadin.ui.CustomField;
53  import com.vaadin.ui.Label;
54  import com.vaadin.ui.TabSheet;
55  import com.vaadin.ui.UI;
56  
57  /**
58   * Component to be displayed together with each form field.
59   * Register in a module descriptor and the component will be included automatically.
60   * You can scope your custom component to an app in the module descriptor declaration (e.g. {@code <id>app-pages-app</id>}).
61   *
62   * @see info.magnolia.ui.framework.layout.field.FieldLayoutComponent.Description Description as an example.
63   * @see info.magnolia.ui.framework.layout.field.FieldLayoutComponent.ValidationLabel ValidationLabel as an example.
64   */
65  @Multibinding
66  public interface FieldLayoutComponent extends Component {
67  
68      /**
69       * Initialisation of component might react according to the field definition or with the inner field itself.
70       */
71      void init(EditorPropertyDefinition definition, Component innerField);
72  
73      default Position getPosition() {
74          return Position.horizontal;
75      }
76  
77      enum Position {
78          /**
79           * Component to be displayed below each form field.
80           */
81          vertical,
82          /**
83           * Component to be displayed next to each form field.
84           */
85          horizontal
86      }
87  
88      class ValidationLabel extends Label implements FieldLayoutComponent {
89  
90          private Component field;
91  
92          @Override
93          public void init(EditorPropertyDefinition definition, Component innerField) {
94              this.field = innerField;
95              addStyleName("validation-display");
96          }
97  
98          @Override
99          public Position getPosition() {
100             return Position.vertical;
101         }
102 
103         @Override
104         public void setValue(String value) {
105             super.setValue(value);
106             if (value.isEmpty()) {
107                 Optional.ofNullable(field).ifPresent(component -> component.removeStyleName("error"));
108                 setVisible(false);
109             } else {
110                 Optional.ofNullable(field).ifPresent(component -> component.addStyleName("error"));
111                 // Only show the label when validation has failed
112                 setVisible(true);
113             }
114         }
115     }
116 
117     class Description extends Button implements FieldLayoutComponent {
118 
119         private final UI ui;
120 
121         @Inject
122         Description(UI ui) {
123             this.ui = ui;
124         }
125 
126         @Override
127         public void init(EditorPropertyDefinition definition, Component innerField) {
128             if (StringUtils.isNotEmpty(definition.getDescription())) {
129                 setIcon(MagnoliaIcons.HELP_MARK);
130                 addStyleName("help-button v-button-icon icon-button");
131                 ContextHelp contextHelp = ui.getExtensions().stream()
132                         .filter(ContextHelp.class::isInstance)
133                         .findFirst()
134                         .map(ContextHelp.class::cast)
135                         .orElseGet(() -> {
136                             final ContextHelp help = new ContextHelp();
137                             help.extend(ui);
138                             return help;
139                         });
140                 addClickListener(event -> contextHelp.showHelpFor(this));
141                 contextHelp.addHelpForComponent(this, definition.getDescription());
142             } else {
143                 setVisible(false);
144             }
145         }
146     }
147 
148     class Root<T> extends CustomField<T> {
149 
150         private final Component root;
151         private final Component field;
152 
153         @Inject
154         Root(Component mainField, Component root, EditorPropertyDefinition definition) {
155             this.field = mainField;
156             this.root = root;
157             this.addStyleName("form-field-layout");
158             mainField.addStyleName("hide-caption");
159 
160             setCaption(StringUtils.isEmpty(field.getCaption()) ? null : field.getCaption());
161             setVisible(field.isVisible());
162             relayRequirementIndicator(mainField, definition);
163             getContent();
164         }
165 
166         /**
167          * Let the field wrapper take over the requirement indicator rendering.
168          */
169         private void relayRequirementIndicator(Component mainField, EditorPropertyDefinition definition) {
170             if (definition instanceof ValueBoundProperty) {
171                 setRequiredIndicatorVisible(((ValueBoundProperty<?>) definition).isRequired());
172                 if (mainField instanceof AbstractField) {
173                     ((AbstractField<?>) mainField).setRequiredIndicatorVisible(false);
174                 }
175             } else if (definition instanceof MultiFieldDefinition) {
176                 setRequiredIndicatorVisible(((MultiFieldDefinition<?>) definition).isRequired());
177             }
178         }
179 
180         @Override
181         protected Component initContent() {
182             return root;
183         }
184 
185         @Override
186         protected void doSetValue(T value) {
187         }
188 
189         @Override
190         public T getValue() {
191             return null;
192         }
193 
194         @Override
195         public void focus() {
196             performTabSelection(field);
197             if (field instanceof Focusable) {
198                 ((Focusable) field).focus();
199             }
200         }
201 
202         /*
203          * When focusing field within multiple tabs form we reach TabSheet instance among parents of field
204          */
205         private void performTabSelection(Component f) {
206             Component t = f;
207             do {
208                 f = t;
209                 t = f.getParent();
210             } while (!(t == null || t instanceof TabSheet));
211             if (t != null) {
212                 ((TabSheet) t).setSelectedTab(f);
213             }
214         }
215     }
216 }