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