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.factory;
35  
36  import info.magnolia.objectfactory.ComponentProvider;
37  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
38  import info.magnolia.ui.field.FieldDefinition;
39  
40  import java.util.Locale;
41  
42  import javax.inject.Inject;
43  
44  import org.apache.commons.lang3.StringUtils;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import com.vaadin.data.HasValue;
49  import com.vaadin.server.Sizeable;
50  import com.vaadin.ui.AbstractComponent;
51  import com.vaadin.ui.Component;
52  
53  import lombok.AccessLevel;
54  import lombok.Getter;
55  
56  
57  /**
58   * Abstract FieldFactory implementations.
59   *
60   * @param <D> definition type
61   * @param <T> field value type
62   * @param <F> field component type
63   */
64  @Getter(AccessLevel.PROTECTED)
65  public abstract class AbstractFieldFactory<D extends FieldDefinition<T>, T, F extends Component & HasValue<T>> implements FieldFactory<T> {
66  
67      private static final Logger log = LoggerFactory.getLogger(AbstractFieldFactory.class);
68  
69      private ComponentProvider componentProvider;
70      private final D definition;
71      private final Locale locale;
72      private final I18NAuthoringSupport i18NAuthoringSupport;
73  
74      @Inject
75      public AbstractFieldFactory(D definition, ComponentProvider componentProvider, Locale locale, I18NAuthoringSupport i18NAuthoringSupport) {
76          this.componentProvider = componentProvider;
77          this.definition = definition;
78          this.locale = locale;
79          this.i18NAuthoringSupport = i18NAuthoringSupport;
80      }
81  
82      protected abstract F createFieldComponent();
83  
84      @Override
85      public F createField() {
86          // Create the Vaadin field
87          F field = createFieldComponent();
88          if (AbstractComponent.class.isAssignableFrom(field.getClass())) {
89              AbstractComponent abstractComponent = (AbstractComponent) field;
90              abstractComponent.setLocale(locale);
91  
92              if (StringUtils.isNotBlank(getDefinition().getStyleName())) {
93                  field.addStyleName(getDefinition().getStyleName());
94              }
95              field.setWidth(100, Sizeable.Unit.PERCENTAGE);
96              setFieldCaption(field);
97          }
98          field.setRequiredIndicatorVisible(getDefinition().isRequired());
99          field.setReadOnly(getDefinition().isReadOnly());
100 
101         return field;
102     }
103 
104     private void setFieldCaption(Component field) {
105         // Set field caption and append locale indicator if needed
106         if (StringUtils.isNotBlank(getDefinition().getLabel())) {
107             String caption = getDefinition().getLabel();
108             if (locale != null && getDefinition().isI18n() && !i18NAuthoringSupport.isDefaultLocale(locale)) {
109                 caption = String.format("%s (%s)", caption, locale.toString());
110             }
111             field.setCaption(caption);
112         }
113     }
114 
115     /**
116      * Field factories may use this method to check whether an @i18nText config property has an actual translation, or is a generated key.
117      * <p>By default, if no translation is found, these properties contain the longest key to provide such a translation.
118      *
119      * @returns true if the given string contains dots, does not end with a dot, and does not contain spaces
120      */
121     protected boolean isMessageKey(String key) {
122         return !StringUtils.endsWith(key, ".") && StringUtils.contains(key, ".") && !StringUtils.contains(key, " ");
123     }
124 }