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