View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.form.field;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
39  import info.magnolia.ui.form.field.definition.CompositeFieldDefinition;
40  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
41  import info.magnolia.ui.form.field.definition.Layout;
42  import info.magnolia.ui.form.field.factory.FieldFactoryFactory;
43  
44  import com.vaadin.ui.Component;
45  import com.vaadin.v7.data.Item;
46  import com.vaadin.v7.data.util.PropertysetItem;
47  import com.vaadin.v7.ui.Field;
48  import com.vaadin.v7.ui.HorizontalLayout;
49  import com.vaadin.v7.ui.VerticalLayout;
50  
51  /**
52   * Generic Composite Field.<br>
53   * This generic Composite Field allows to handle multiple {@link ConfiguredFieldDefinition} as a single Field:<br>
54   * The Field is build based on a generic {@link ConfiguredFieldDefinition}.<br>
55   * The Field values are handle by a configured {@link info.magnolia.ui.form.field.transformer.Transformer} dedicated to create/retrieve properties as {@link PropertysetItem}.<br>
56   *
57   * @deprecated since 6.2 - use {@link info.magnolia.ui.editor.FormView} instead.
58   *
59   * @see <a href="https://documentation.magnolia-cms.com/display/DOCS62/Upgrading+to+Magnolia+6.2.x">Upgrading to Magnolia 6.2.x</a>
60   */
61  @Deprecated
62  public class CompositeField extends AbstractCustomMultiField<CompositeFieldDefinition, PropertysetItem> {
63  
64      public CompositeField(CompositeFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, ComponentProvider componentProvider, Item relatedFieldItem, I18NAuthoringSupport i18nAuthoringSupport) {
65          super(definition, fieldFactoryFactory, componentProvider, relatedFieldItem, i18nAuthoringSupport);
66      }
67  
68      /**
69       * @deprecated since 5.3.5 removing i18nContentSupport dependency (actually unused way before that). Besides, fields should use i18nAuthoringSupport for internationalization.
70       */
71      @Deprecated
72      public CompositeField(CompositeFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, I18nContentSupport i18nContentSupport, ComponentProvider componentProvider, Item relatedFieldItem) {
73          this(definition, fieldFactoryFactory, componentProvider, relatedFieldItem, componentProvider.getComponent(I18NAuthoringSupport.class));
74      }
75  
76      @Override
77      protected Component initContent() {
78          // Init root layout
79          addStyleName("linkfield");
80          addStyleName("force-show-field-captions");
81          if (definition.getLayout() == Layout.horizontal) {
82              root = new HorizontalLayout();
83          } else {
84              root = new VerticalLayout();
85          }
86          root.setWidth(100, Unit.PERCENTAGE);
87          root.setSpacing(true);
88  
89          // Initialize Existing field
90          initFields();
91          return root;
92      }
93  
94      @Override
95      protected void initFields(PropertysetItem fieldValues) {
96          root.removeAllComponents();
97  
98          for (ConfiguredFieldDefinition fieldDefinition : definition.getFields()) {
99              // Only propagate read only if the parent definition is read only
100             if (definition.isReadOnly()) {
101                 fieldDefinition.setReadOnly(true);
102             }
103             Field<?> compositeField = createLocalField(fieldDefinition, fieldValues.getItemProperty(fieldDefinition.getName()), false);
104             if (fieldValues.getItemProperty(fieldDefinition.getName()) == null) {
105                 fieldValues.addItemProperty(fieldDefinition.getName(), compositeField.getPropertyDataSource());
106             }
107             compositeField.setWidth(100, Unit.PERCENTAGE);
108 
109             root.addComponent(compositeField);
110         }
111     }
112 
113     @Override
114     public Class<? extends PropertysetItem> getType() {
115         return PropertysetItem.class;
116     }
117 }