View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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.ConfiguredFieldDefinition;
40  import info.magnolia.ui.form.field.definition.MultiValueFieldDefinition;
41  import info.magnolia.ui.form.field.factory.FieldFactoryFactory;
42  import info.magnolia.ui.form.field.transformer.TransformedProperty;
43  import info.magnolia.ui.form.field.transformer.Transformer;
44  import info.magnolia.ui.form.field.transformer.multi.MultiTransformer;
45  
46  import java.util.Iterator;
47  
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.data.Item;
52  import com.vaadin.data.Property;
53  import com.vaadin.data.util.PropertysetItem;
54  import com.vaadin.ui.Alignment;
55  import com.vaadin.ui.Button;
56  import com.vaadin.ui.Button.ClickEvent;
57  import com.vaadin.ui.Button.ClickListener;
58  import com.vaadin.ui.Component;
59  import com.vaadin.ui.Field;
60  import com.vaadin.ui.HorizontalLayout;
61  import com.vaadin.ui.NativeButton;
62  import com.vaadin.ui.VerticalLayout;
63  
64  /**
65   * Generic Multi Field.<br>
66   * This generic MultiField allows to handle a Field Set. It handle :<br>
67   * - The creation of new Field<br>
68   * - The removal of Field<br>
69   * The Field is build based on a generic {@link ConfiguredFieldDefinition}.<br>
70   * 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>
71   */
72  public class MultiField extends AbstractCustomMultiField<MultiValueFieldDefinition, PropertysetItem> {
73  
74      private static final Logger log = LoggerFactory.getLogger(MultiField.class);
75  
76      private final ConfiguredFieldDefinition fieldDefinition;
77  
78      private final Button addButton = new NativeButton();
79      private String buttonCaptionAdd;
80      private String buttonCaptionRemove;
81  
82      public MultiField(MultiValueFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, ComponentProvider componentProvider, Item relatedFieldItem, I18NAuthoringSupport i18nAuthoringSupport) {
83          super(definition, fieldFactoryFactory, componentProvider, relatedFieldItem, i18nAuthoringSupport);
84          this.fieldDefinition = definition.getField();
85      }
86  
87      /**
88       * @deprecated since 5.3.5 removing i18nContentSupport dependency (actually unused way before that). Besides, fields should use i18nAuthoringSupport for internationalization.
89       */
90      @Deprecated
91      public MultiField(MultiValueFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, I18nContentSupport i18nContentSupport, ComponentProvider componentProvider, Item relatedFieldItem) {
92          this(definition, fieldFactoryFactory, componentProvider, relatedFieldItem, null);
93      }
94  
95      @Override
96      protected Component initContent() {
97          // Init root layout
98          addStyleName("linkfield");
99          root = new VerticalLayout();
100         root.setSpacing(true);
101         root.setWidth(100, Unit.PERCENTAGE);
102         root.setHeight(-1, Unit.PIXELS);
103 
104         // Init addButton
105         addButton.setCaption(buttonCaptionAdd);
106         addButton.addStyleName("magnoliabutton");
107         addButton.addClickListener(new Button.ClickListener() {
108             @Override
109             public void buttonClick(ClickEvent event) {
110 
111                 int newPropertyId = -1;
112                 Property<?> property = null;
113 
114                 Transformer<?> transformer = ((TransformedProperty<?>) getPropertyDataSource()).getTransformer();
115                 PropertysetItem item = (PropertysetItem) getPropertyDataSource().getValue();
116 
117                 if (transformer instanceof MultiTransformer) {
118                     // create property and find its propertyId
119                     property = ((MultiTransformer) transformer).createProperty();
120                     newPropertyId = findPropertyId(item, property);
121                 } else {
122                     // get next propertyId based on property count
123                     newPropertyId = item.getItemPropertyIds().size();
124                 }
125 
126                 if (newPropertyId == -1) {
127                     log.warn("Could not resolve new propertyId; cannot add new multifield entry to item '{}'.", item);
128                     return;
129                 }
130 
131                 root.addComponent(createEntryComponent(newPropertyId, property), root.getComponentCount() - 1);
132             };
133         });
134 
135         // Initialize Existing field
136         initFields();
137 
138         return root;
139     }
140 
141     /**
142      * Initialize the MultiField. <br>
143      * Create as many configured Field as we have related values already stored.
144      */
145     @Override
146     protected void initFields(PropertysetItem newValue) {
147         root.removeAllComponents();
148         Iterator<?> it = newValue.getItemPropertyIds().iterator();
149         while (it.hasNext()) {
150             Object propertyId = it.next();
151             Property<?> property = newValue.getItemProperty(propertyId);
152             root.addComponent(createEntryComponent(propertyId, property));
153         }
154         root.addComponent(addButton);
155     }
156 
157     /**
158      * Create a single element.<br>
159      * This single element is composed of:<br>
160      * - a configured field <br>
161      * - a remove Button<br>
162      */
163     private Component createEntryComponent(Object propertyId, Property<?> property) {
164 
165         HorizontalLayout layout = new HorizontalLayout();
166         layout.setWidth(100, Unit.PERCENTAGE);
167         layout.setHeight(-1, Unit.PIXELS);
168 
169         Field<?> field = createLocalField(fieldDefinition, property, true); // creates property datasource if given property is null
170         layout.addComponent(field);
171 
172         // bind the field's property to the item
173         if (property == null) {
174             property = field.getPropertyDataSource();
175             ((PropertysetItem) getPropertyDataSource().getValue()).addItemProperty(propertyId, property);
176         }
177         final Property<?> propertyReference = property;
178 
179         // Delete Button
180         Button deleteButton = new Button();
181         deleteButton.setHtmlContentAllowed(true);
182         deleteButton.setCaption("<span class=\"" + "icon-trash" + "\"></span>");
183         deleteButton.addStyleName("inline");
184         deleteButton.setDescription(buttonCaptionRemove);
185         deleteButton.addClickListener(new ClickListener() {
186 
187             @Override
188             public void buttonClick(ClickEvent event) {
189                 Component layout = event.getComponent().getParent();
190                 root.removeComponent(layout);
191                 Transformer<?> transformer = ((TransformedProperty<?>) getPropertyDataSource()).getTransformer();
192 
193                 // get propertyId to delete, this might have changed since initialization above (see #removeValueProperty)
194                 Object propertyId = findPropertyId(getValue(), propertyReference);
195 
196                 if (transformer instanceof MultiTransformer) {
197                     ((MultiTransformer) transformer).removeProperty(propertyId);
198                 } else {
199                     if (propertyId != null && propertyId.getClass().isAssignableFrom(Integer.class)) {
200                         removeValueProperty((Integer) propertyId);
201                     } else {
202                         log.error("Property id {} is not an integer and as such property can't be removed", propertyId);
203                     }
204                     getPropertyDataSource().setValue(getValue());
205                 }
206             }
207         });
208         layout.addComponent(deleteButton);
209 
210         // set layout to full width
211         layout.setWidth(100, Unit.PERCENTAGE);
212 
213         // distribute space in favour of field over delete button
214         layout.setExpandRatio(field, 1);
215         layout.setExpandRatio(deleteButton, 0);
216 
217         // make sure button stays aligned with the field and not with the optional field label when used
218         layout.setComponentAlignment(deleteButton, Alignment.BOTTOM_RIGHT);
219 
220         return layout;
221     }
222 
223 
224     @Override
225     public Class<? extends PropertysetItem> getType() {
226         return PropertysetItem.class;
227     }
228 
229     /**
230      * Caption section.
231      */
232     public void setButtonCaptionAdd(String buttonCaptionAdd) {
233         this.buttonCaptionAdd = buttonCaptionAdd;
234     }
235 
236     public void setButtonCaptionRemove(String buttonCaptionRemove) {
237         this.buttonCaptionRemove = buttonCaptionRemove;
238     }
239 
240     /**
241      * Ensure that id of the {@link PropertysetItem} stay coherent.<br>
242      * Assume that we have 3 values 0:a, 1:b, 2:c, and 1 is removed <br>
243      * If we just remove 1, the {@link PropertysetItem} will contain 0:a, 2:c, .<br>
244      * But we should have : 0:a, 1:c, .
245      */
246     private void removeValueProperty(int fromIndex) {
247         getValue().removeItemProperty(fromIndex);
248         int toIndex = fromIndex;
249         int valuesSize = getValue().getItemPropertyIds().size();
250         if (fromIndex == valuesSize) {
251             return;
252         }
253         while (fromIndex < valuesSize) {
254             toIndex = fromIndex;
255             fromIndex +=1;
256             getValue().addItemProperty(toIndex, getValue().getItemProperty(fromIndex));
257             getValue().removeItemProperty(fromIndex);
258         }
259     }
260 
261 }