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.vaadin.form.field;
35  
36  import info.magnolia.icons.MagnoliaIcons;
37  import info.magnolia.ui.vaadin.gwt.client.form.field.FieldLayoutState;
38  
39  import java.util.function.Consumer;
40  
41  import org.apache.commons.lang3.StringUtils;
42  import org.vaadin.jonatan.contexthelp.ContextHelp;
43  
44  import com.google.common.base.Preconditions;
45  import com.vaadin.shared.ui.ContentMode;
46  import com.vaadin.ui.AbstractField;
47  import com.vaadin.ui.Button;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.HorizontalLayout;
50  import com.vaadin.ui.Label;
51  import com.vaadin.ui.TabSheet;
52  import com.vaadin.ui.UI;
53  import com.vaadin.ui.VerticalLayout;
54  
55  /**
56   * A field layout for a field, supporting displaying the error message.
57   *
58   * @deprecated since 6.2.2. Use FieldComponentLayoutProducers from the framework instead.
59   */
60  @Deprecated
61  public class FieldLayout extends VerticalLayout implements Component.Focusable {
62  
63      private final ContextHelp contextHelp;
64      private final boolean isRequired;
65  
66      private Label statusLabel;
67      private final Component field;
68      private final String description;
69  
70      public static FieldLayout of(Component field, String description) {
71         return of(field, description, false);
72      }
73  
74      public static FieldLayout of(Component field, String description, boolean isRequired) {
75          return new FieldLayout(field, description, isRequired);
76      }
77  
78      private FieldLayout(Component field, String description, boolean isRequired) {
79          this.isRequired = isRequired;
80          Preconditions.checkNotNull(field);
81          this.field = field;
82          this.description = description;
83          this.contextHelp = UI.getCurrent().getExtensions().stream()
84                  .filter(ContextHelp.class::isInstance)
85                  .findFirst()
86                  .map(ContextHelp.class::cast)
87                  .orElse(null);
88          initContent();
89      }
90  
91      private void initContent() {
92          setMargin(false);
93          setVisible(field.isVisible());
94  
95          statusLabel = new Label(null, ContentMode.HTML);
96          statusLabel.setVisible(false);
97          statusLabel.addStyleName("validation-display");
98  
99          Component horizontalLayout = field;
100         if (contextHelp != null && StringUtils.isNotEmpty(description)) {
101             final Button helpButton = new Button(MagnoliaIcons.HELP_MARK);
102             helpButton.addStyleName("help-button v-button-icon icon-button");
103 
104             contextHelp.extend(UI.getCurrent());
105             contextHelp.addHelpForComponent(helpButton, description);
106 
107             helpButton.addClickListener(event -> {
108                 contextHelp.showHelpFor(helpButton);
109             });
110             HorizontalLayout descriptionLayout = new HorizontalLayout(field, helpButton);
111             descriptionLayout.addStyleName("help-button-field-layout");
112             descriptionLayout.setSizeFull();
113             descriptionLayout.setExpandRatio(field, 1);
114             descriptionLayout.setExpandRatio(helpButton, 0);
115             descriptionLayout.setSpacing(false);
116             horizontalLayout = descriptionLayout;
117         }
118 
119         setCaption(field.getCaption());
120         setRequiredIndicatorVisible(isRequired);
121 
122         addComponents(horizontalLayout, statusLabel);
123     }
124 
125     @Override
126     public void setCaption(String caption) {
127         super.setCaption(caption);
128     }
129 
130     @Override
131     public void setRequiredIndicatorVisible(boolean visible) {
132         if (field instanceof AbstractField) {
133             ((AbstractField) field).setRequiredIndicatorVisible(false);
134         }
135         getState().required = visible;
136     }
137 
138     @Override
139     protected boolean isRequiredIndicatorVisible() {
140         return getState().required;
141     }
142 
143     @Override
144     protected FieldLayoutState getState() {
145         return (FieldLayoutState) super.getState();
146     }
147 
148     @Override
149     public void focus() {
150         performTabSelection(field);
151         if (field instanceof Focusable) {
152             ((Focusable) field).focus();
153         }
154     }
155 
156     @Override
157     public int getTabIndex() {
158         return 0;
159     }
160 
161     @Override
162     public void setTabIndex(int tabIndex) {
163     }
164 
165     public Consumer<String> getValidationStatusHandler() {
166         return errorMessage -> {
167             statusLabel.setValue(errorMessage);
168             if (errorMessage.isEmpty()) {
169                 field.removeStyleName("error");
170                 statusLabel.setVisible(false);
171             } else {
172                 field.addStyleName("error");
173                 // Only show the label when validation has failed
174                 statusLabel.setVisible(true);
175             }
176         };
177     }
178 
179     public Component getField() {
180         return field;
181     }
182 
183     /*
184      * When focusing field within multiple tabs form we reach TabSheet instance among parents of field
185      */
186     private void performTabSelection(Component f) {
187         Component t = f;
188         do {
189             f = t;
190             t = f.getParent();
191         } while (!(t == null || t instanceof TabSheet));
192         if (t != null) {
193             ((TabSheet) t).setSelectedTab(f);
194         }
195     }
196 }