View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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 org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import com.google.common.base.Function;
50  import com.google.common.base.Optional;
51  import com.google.common.base.Predicates;
52  import com.google.common.collect.Iterators;
53  import com.vaadin.data.Item;
54  import com.vaadin.data.Property;
55  import com.vaadin.data.util.PropertysetItem;
56  import com.vaadin.ui.Alignment;
57  import com.vaadin.ui.Button;
58  import com.vaadin.ui.Button.ClickEvent;
59  import com.vaadin.ui.Button.ClickListener;
60  import com.vaadin.ui.Component;
61  import com.vaadin.ui.Field;
62  import com.vaadin.ui.HasComponents;
63  import com.vaadin.ui.HorizontalLayout;
64  import com.vaadin.ui.NativeButton;
65  import com.vaadin.ui.VerticalLayout;
66  
67  /**
68   * Generic Multi Field.<br>
69   * This generic MultiField allows to handle a Field Set. It handle :<br>
70   * - The creation of new Field<br>
71   * - The removal of Field<br>
72   * The Field is build based on a generic {@link ConfiguredFieldDefinition}.<br>
73   * 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>
74   */
75  public class MultiField extends AbstractCustomMultiField<MultiValueFieldDefinition, PropertysetItem> {
76  
77      private static final Logger log = LoggerFactory.getLogger(MultiField.class);
78  
79      private final ConfiguredFieldDefinition fieldDefinition;
80  
81      private final Button addButton = new NativeButton();
82      private String buttonCaptionAdd;
83      private String buttonCaptionRemove;
84      private String buttonCaptionMoveUp = "Move Up";
85      private String buttonCaptionMoveDown = "Move Down";
86  
87      public MultiField(MultiValueFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, ComponentProvider componentProvider, Item relatedFieldItem, I18NAuthoringSupport i18nAuthoringSupport) {
88          super(definition, fieldFactoryFactory, componentProvider, relatedFieldItem, i18nAuthoringSupport);
89          this.fieldDefinition = definition.getField();
90          // Only propagate read only if the parent definition is read only
91          if (definition.isReadOnly()) {
92              fieldDefinition.setReadOnly(true);
93          }
94      }
95  
96      /**
97       * @deprecated since 5.3.5 removing i18nContentSupport dependency (actually unused way before that). Besides, fields should use i18nAuthoringSupport for internationalization.
98       */
99      @Deprecated
100     public MultiField(MultiValueFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, I18nContentSupport i18nContentSupport, ComponentProvider componentProvider, Item relatedFieldItem) {
101         this(definition, fieldFactoryFactory, componentProvider, relatedFieldItem, null);
102     }
103 
104     @Override
105     protected Component initContent() {
106         // Init root layout
107         addStyleName("linkfield");
108         root = new VerticalLayout();
109         root.setSpacing(true);
110         root.setWidth(100, Unit.PERCENTAGE);
111         root.setHeight(-1, Unit.PIXELS);
112 
113         // Init addButton
114         addButton.setCaption(buttonCaptionAdd);
115         addButton.addStyleName("magnoliabutton");
116         addButton.addClickListener(new Button.ClickListener() {
117             @Override
118             public void buttonClick(ClickEvent event) {
119                 int newPropertyId;
120                 Property<?> property = null;
121 
122                 Transformer<?> transformer = ((TransformedProperty<?>) getPropertyDataSource()).getTransformer();
123                 PropertysetItem item = (PropertysetItem) getPropertyDataSource().getValue();
124 
125                 if (transformer instanceof MultiTransformer) {
126                     // create property and find its propertyId
127                     property = ((MultiTransformer) transformer).createProperty();
128                     newPropertyId = findPropertyId(item, property);
129                 } else {
130                     // get next propertyId based on property count
131                     newPropertyId = item.getItemPropertyIds().size();
132                 }
133 
134                 if (newPropertyId == -1) {
135                     log.warn("Could not resolve new propertyId; cannot add new multifield entry to item '{}'.", item);
136                     return;
137                 }
138 
139                 root.addComponent(createEntryComponent(newPropertyId, property), root.getComponentCount() - 1);
140             }
141         });
142 
143         // Initialize Existing field
144         initFields();
145 
146         return root;
147     }
148 
149     /**
150      * Initialize the MultiField. <br>
151      * Create as many configured Field as we have related values already stored.
152      */
153     @Override
154     protected void initFields(PropertysetItem newValue) {
155         root.removeAllComponents();
156         for (Object propertyId : newValue.getItemPropertyIds()) {
157             Property<?> property = newValue.getItemProperty(propertyId);
158             root.addComponent(createEntryComponent(propertyId, property));
159         }
160         if (!this.definition.isReadOnly()) {
161             root.addComponent(addButton);
162         }
163     }
164 
165     /**
166      * Create a single element.<br>
167      * This single element is composed of:<br>
168      * - a configured field <br>
169      * - a remove Button<br>
170      */
171     private Component createEntryComponent(Object propertyId, Property<?> property) {
172         final HorizontalLayout layout = new HorizontalLayout();
173         layout.setWidth(100, Unit.PERCENTAGE);
174         layout.setHeight(-1, Unit.PIXELS);
175 
176         final Field<?> field = createLocalField(fieldDefinition, property, true); // creates property datasource if given property is null
177         layout.addComponent(field);
178 
179         // bind the field's property to the item
180         if (property == null) {
181             property = field.getPropertyDataSource();
182             ((PropertysetItem) getPropertyDataSource().getValue()).addItemProperty(propertyId, property);
183         }
184         final Property<?> propertyReference = property;
185         // set layout to full width
186         layout.setWidth(100, Unit.PERCENTAGE);
187 
188         // distribute space in favour of field over delete button
189         layout.setExpandRatio(field, 1);
190         if (definition.isReadOnly()) {
191             return layout;
192         }
193 
194         // move up Button
195         Button moveUpButton = new Button();
196         moveUpButton.setHtmlContentAllowed(true);
197         moveUpButton.setCaption("<span class=\"" + "icon-arrow2_n" + "\"></span>");
198         moveUpButton.addStyleName("inline");
199         moveUpButton.setDescription(buttonCaptionMoveUp);
200         moveUpButton.addClickListener(new Button.ClickListener() {
201 
202             @Override
203             public void buttonClick(Button.ClickEvent event) {
204                 onMove(layout, propertyReference, true);
205             }
206         });
207 
208         // move down Button
209         Button moveDownButton = new Button();
210         moveDownButton.setHtmlContentAllowed(true);
211         moveDownButton.setCaption("<span class=\"" + "icon-arrow2_s" + "\"></span>");
212         moveDownButton.addStyleName("inline");
213         moveDownButton.setDescription(buttonCaptionMoveDown);
214         moveDownButton.addClickListener(new Button.ClickListener() {
215 
216             @Override
217             public void buttonClick(Button.ClickEvent event) {
218                 onMove(layout, propertyReference, false);
219             }
220         });
221 
222 
223         // Delete Button
224         Button deleteButton = new Button();
225         deleteButton.setHtmlContentAllowed(true);
226         deleteButton.setCaption("<span class=\"" + "icon-trash" + "\"></span>");
227         deleteButton.addStyleName("inline");
228         deleteButton.setDescription(buttonCaptionRemove);
229         deleteButton.addClickListener(new ClickListener() {
230 
231             @Override
232             public void buttonClick(ClickEvent event) {
233                 onDelete(layout, propertyReference);
234             }
235         });
236 
237         layout.addComponents(moveUpButton, moveDownButton, deleteButton);
238 
239         // make sure button stays aligned with the field and not with the optional field label when used
240         layout.setComponentAlignment(deleteButton, Alignment.BOTTOM_RIGHT);
241         layout.setComponentAlignment(moveUpButton, Alignment.BOTTOM_RIGHT);
242         layout.setComponentAlignment(moveDownButton, Alignment.BOTTOM_RIGHT);
243 
244         return layout;
245     }
246 
247     @Override
248     public Class<? extends PropertysetItem> getType() {
249         return PropertysetItem.class;
250     }
251 
252     /**
253      * Caption section.
254      */
255     public void setButtonCaptionAdd(String buttonCaptionAdd) {
256         this.buttonCaptionAdd = buttonCaptionAdd;
257     }
258 
259     public void setButtonCaptionRemove(String buttonCaptionRemove) {
260         this.buttonCaptionRemove = buttonCaptionRemove;
261     }
262 
263     /**
264      * Ensure that id of the {@link PropertysetItem} stay coherent.<br>
265      * Assume that we have 3 values 0:a, 1:b, 2:c, and 1 is removed <br>
266      * If we just remove 1, the {@link PropertysetItem} will contain 0:a, 2:c, .<br>
267      * But we should have : 0:a, 1:c, .
268      */
269     private void removeValueProperty(int fromIndex) {
270         getValue().removeItemProperty(fromIndex);
271         int valuesSize = getValue().getItemPropertyIds().size();
272         if (fromIndex == valuesSize) {
273             return;
274         }
275         while (fromIndex < valuesSize) {
276             int toIndex = fromIndex;
277             fromIndex +=1;
278             getValue().addItemProperty(toIndex, getValue().getItemProperty(fromIndex));
279             getValue().removeItemProperty(fromIndex);
280         }
281     }
282 
283     /**
284      * Switches two properties. We have to clone the original {@link PropertysetItem} to re-arrange the ordering.
285      */
286     private void switchItemProperties(Object firstPropertyId, Object secondPropertyId) {
287         Property propertyFirst = getValue().getItemProperty(firstPropertyId);
288         Property propertySecond = getValue().getItemProperty(secondPropertyId);
289 
290         try {
291             PropertysetItem storedValues = (PropertysetItem) getValue().clone();
292             if (storedValues != null) {
293                 for (Object propertyId : storedValues.getItemPropertyIds()) {
294                     getValue().removeItemProperty(propertyId);
295                     if (propertyId == firstPropertyId) {
296                         getValue().addItemProperty(firstPropertyId, propertySecond);
297                     } else if (propertyId == secondPropertyId) {
298                         getValue().addItemProperty(secondPropertyId, propertyFirst);
299                     } else {
300                         getValue().addItemProperty(propertyId, storedValues.getItemProperty(propertyId));
301                     }
302                 }
303                 getPropertyDataSource().setValue(getValue());
304             }
305         } catch (CloneNotSupportedException e) {
306             log.error("Unable to switch properties on MultiField. Unable to clone PropertysetItem.", e);
307         }
308 
309     }
310 
311     private void onDelete(Component layout, Property<?> propertyReference) {
312         root.removeComponent(layout);
313         Transformer<?> transformer = ((TransformedProperty<?>) getPropertyDataSource()).getTransformer();
314 
315         // get propertyId to delete, this might have changed since initialization above (see #removeValueProperty)
316         Object propertyId = findPropertyId(getValue(), propertyReference);
317 
318         if (transformer instanceof MultiTransformer) {
319             ((MultiTransformer) transformer).removeProperty(propertyId);
320         } else {
321             if (propertyId.getClass().isAssignableFrom(Integer.class)) {
322                 removeValueProperty((Integer) propertyId);
323             } else {
324                 log.error("Property id {} is not an integer and as such property can't be removed", propertyId);
325             }
326             getPropertyDataSource().setValue(getValue());
327         }
328     }
329 
330     /**
331      * Takes care of moving a field up or down. Tries hard not to assume much about the layout, so we're iterating over parents
332      * and component types to make sure we're dealing with Fields.
333      */
334     private void onMove(Component layout, Property<?> propertyReference, boolean moveUp) {
335         int currentPosition = root.getComponentIndex(layout);
336         int switchPosition = currentPosition + (moveUp ? -1 : 1);
337 
338         Field[] fields = Iterators.toArray(Iterators.filter(Iterators.transform(root.iterator(), new Function<Component, Field>() {
339             @Override
340             public Field apply(Component input) {
341                 if (input instanceof HasComponents) {
342                     Optional<Component> field = Iterators.tryFind(((HasComponents) input).iterator(), Predicates.instanceOf(Field.class));
343                     if (field.isPresent()) {
344                         return (Field) field.get();
345                     }
346                 }
347                 return null;
348             }
349         }), Predicates.notNull()), Field.class);
350 
351         if (moveUp && currentPosition != 0 || (!moveUp && currentPosition != fields.length - 1)) {
352             Field switchField = fields[switchPosition];
353             Object currentPropertyId = MultiField.this.findPropertyId(getValue(), propertyReference);
354             Object switchPropertyId = MultiField.this.findPropertyId(getValue(), switchField.getPropertyDataSource());
355 
356             root.replaceComponent(root.getComponent(currentPosition), root.getComponent(switchPosition));
357             switchItemProperties(currentPropertyId, switchPropertyId);
358         }
359     }
360 }