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  public class FieldLayout extends VerticalLayout implements Component.Focusable {
59  
60      private final ContextHelp contextHelp;
61  
62      private Label statusLabel;
63      private final Component field;
64      private final String description;
65  
66      public static FieldLayout of(Component field, String description) {
67          return new FieldLayout(field, description);
68      }
69  
70      private FieldLayout(Component field, String description) {
71          Preconditions.checkNotNull(field);
72          this.field = field;
73          this.description = description;
74          this.contextHelp = UI.getCurrent().getExtensions().stream()
75                  .filter(ContextHelp.class::isInstance)
76                  .findFirst()
77                  .map(ContextHelp.class::cast)
78                  .orElse(null);
79          initContent();
80      }
81  
82      private void initContent() {
83          setMargin(false);
84          setVisible(field.isVisible());
85  
86          statusLabel = new Label(null, ContentMode.HTML);
87          statusLabel.setVisible(false);
88          statusLabel.addStyleName("validation-display");
89  
90          Component horizontalLayout = field;
91          if (contextHelp != null && hasTranslatedDescription()) {
92              final Button helpButton = new Button(MagnoliaIcons.HELP_MARK);
93              helpButton.addStyleName("help-button v-button-icon icon-button");
94  
95              contextHelp.extend(UI.getCurrent());
96              contextHelp.addHelpForComponent(helpButton, description);
97  
98              helpButton.addClickListener(event -> {
99                  contextHelp.showHelpFor(helpButton);
100             });
101             HorizontalLayout descriptionLayout = new HorizontalLayout(field, helpButton);
102             descriptionLayout.addStyleName("help-button-field-layout");
103             descriptionLayout.setSizeFull();
104             descriptionLayout.setExpandRatio(field, 1);
105             descriptionLayout.setExpandRatio(helpButton, 0);
106             descriptionLayout.setSpacing(false);
107             horizontalLayout = descriptionLayout;
108         }
109         addComponents(horizontalLayout, statusLabel);
110     }
111 
112     @Override
113     public void setCaption(String caption) {
114         super.setCaption(caption);
115     }
116 
117     @Override
118     public void setRequiredIndicatorVisible(boolean visible) {
119         if (field instanceof AbstractField) {
120             ((AbstractField) field).setRequiredIndicatorVisible(false);
121         }
122         getState().required = visible;
123     }
124 
125     @Override
126     protected boolean isRequiredIndicatorVisible() {
127         return getState().required;
128     }
129 
130     @Override
131     protected FieldLayoutState getState() {
132         return (FieldLayoutState) super.getState();
133     }
134 
135     @Override
136     public void focus() {
137         performTabSelection(field);
138         if (field instanceof Focusable) {
139             ((Focusable) field).focus();
140         }
141     }
142 
143     @Override
144     public int getTabIndex() {
145         return 0;
146     }
147 
148     @Override
149     public void setTabIndex(int tabIndex) {
150     }
151 
152     public Consumer<String> getValidationStatusHandler() {
153         return errorMessage -> {
154             statusLabel.setValue(errorMessage);
155             if (errorMessage.isEmpty()) {
156                 field.removeStyleName("error");
157                 statusLabel.setVisible(false);
158             } else {
159                 field.addStyleName("error");
160                 // Only show the label when validation has failed
161                 statusLabel.setVisible(true);
162             }
163         };
164     }
165 
166     public Component getField() {
167         return field;
168     }
169 
170     private boolean hasTranslatedDescription() {
171         return StringUtils.isNotEmpty(description) && (StringUtils.endsWith(description, ".") || !StringUtils.contains(description, ".") || StringUtils.contains(description, " "));
172     }
173 
174     /*
175      * When focusing field within multiple tabs form we reach TabSheet instance among parents of field
176      */
177     private void performTabSelection(Component f) {
178         Component t = f;
179         do {
180             f = t;
181             t = f.getParent();
182         } while (!(t == null || t instanceof TabSheet));
183         if (t != null) {
184             ((TabSheet) t).setSelectedTab(f);
185         }
186     }
187 }