View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.factory;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.ui.form.field.SwitchableField;
39  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
40  import info.magnolia.ui.form.field.definition.FieldDefinition;
41  import info.magnolia.ui.form.field.definition.OptionGroupFieldDefinition;
42  import info.magnolia.ui.form.field.definition.SelectFieldDefinition;
43  import info.magnolia.ui.form.field.definition.SwitchableFieldDefinition;
44  
45  import java.util.HashMap;
46  
47  import javax.inject.Inject;
48  
49  import org.apache.commons.beanutils.BeanUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import com.rits.cloning.Cloner;
54  import com.vaadin.data.Item;
55  import com.vaadin.ui.AbstractSelect;
56  import com.vaadin.ui.Field;
57  
58  /**
59   * Creates and initializes a SwitchableField field based on a field definition.<br>
60   * Switchable field has two components: <br>
61   * - A select section configured based on the Options list of the definition<br>
62   * - A field section configured based on the Fields list of the definition<br>
63   * The link between select and fields is based on the association of: <br>
64   * - The String property defined into the value property of the definition (value = date) <br>
65   * and<br>
66   * - The Field name defined into the Fields set (Date field named date).
67   * 
68   * @param <D> definition type
69   */
70  public class SwitchableFieldFactory<D extends FieldDefinition> extends AbstractFieldFactory<SwitchableFieldDefinition, String> {
71      private static final Logger log = LoggerFactory.getLogger(SwitchableFieldFactory.class);
72      private FieldFactoryFactory fieldFactoryFactory;
73      private I18nContentSupport i18nContentSupport;
74      private ComponentProvider componentProvider;
75  
76      private HashMap<String, Field<?>> fieldMap;
77      private AbstractSelect selectField;
78  
79      @Inject
80      public SwitchableFieldFactory(SwitchableFieldDefinition definition, Item relatedFieldItem, FieldFactoryFactory fieldFactoryFactory, I18nContentSupport i18nContentSupport, ComponentProvider componentProvider) {
81          super(definition, relatedFieldItem);
82          this.fieldFactoryFactory = fieldFactoryFactory;
83          this.componentProvider = componentProvider;
84          this.i18nContentSupport = i18nContentSupport;
85      }
86  
87      @Override
88      protected Field<String> createFieldComponent() {
89          try {
90              // Create the selection Field
91              this.selectField = createSelectionField();
92  
93              // Create the related fieldMap
94              this.fieldMap = createFieldSet();
95  
96          } catch (Exception e) {
97              log.warn("Not able to create a SwitchableField");
98              throw new RuntimeException(e);
99          }
100         return new SwitchableField(fieldMap, selectField);
101     }
102 
103     /**
104      * Create a RadioSelect or a NormalSelect Field based on the definition.<br>
105      */
106     private AbstractSelect createSelectionField() throws Exception {
107         // Create the correct definition class
108         SelectFieldDefinition selectDefinition = null;
109         if (definition.getSelectionType().equals("radio")) {
110             selectDefinition = new OptionGroupFieldDefinition();
111         } else {
112             selectDefinition = new SelectFieldDefinition();
113         }
114         // Copy options to the newly created select definition. definition
115         BeanUtils.copyProperties(selectDefinition, definition);
116         // Create the field
117         AbstractSelect field = (AbstractSelect) createLocalField(selectDefinition);
118         field.addStyleName("horizontal");
119         field.setImmediate(true);
120 
121         return field;
122     }
123 
124     /**
125      * Create a Field Map.<br>
126      * - key : Field name. Should be the same as the related select value.<br>
127      * -value : Related Field. Created based on the definition coming from the Fields Definition list.
128      */
129     private HashMap<String, Field<?>> createFieldSet() {
130 
131         HashMap<String, Field<?>> localFieldMap = new HashMap<String, Field<?>>();
132         // Iterate and create the related fields
133         for (ConfiguredFieldDefinition fieldDefinition : definition.getFields()) {
134             // As the definition is a singleton & as we set the name of the definition, this has to be done on a Clone
135             // else this change will be propagated every time the dialog is open
136             // (First call: fieldName = type+name = typename, Second dialog initialization fieldName = type+typename = typetypename)
137             ConfiguredFieldDefinition fieldDefinitionClone = new Cloner().deepClone(fieldDefinition);
138             String name = fieldDefinitionClone.getName();
139             fieldDefinitionClone.setName(definition.getName() + fieldDefinition.getName());
140             // Create the field
141             Field<?> field = createLocalField(fieldDefinitionClone);
142             localFieldMap.put(name, field);
143         }
144 
145         return localFieldMap;
146     }
147 
148     private Field<?> createLocalField(ConfiguredFieldDefinition fieldDefinition) {
149         FieldFactory formField = fieldFactoryFactory.createFieldFactory(fieldDefinition, item);
150         formField.setComponentProvider(componentProvider);
151         formField.setI18nContentSupport(i18nContentSupport);
152         Field<?> field = formField.createField();
153         field.setCaption(null);
154         return field;
155     }
156 
157 }