View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 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.dam.external.app.field;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.dam.api.Asset;
38  import info.magnolia.dam.api.metadata.AssetMetadata;
39  import info.magnolia.dam.api.metadata.AssetMetadataDefinition;
40  import info.magnolia.dam.api.metadata.AssetMetadataRegistry;
41  import info.magnolia.dam.external.app.field.definition.AssetMetadataFieldDefinition;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
44  import info.magnolia.ui.form.field.definition.FieldDefinition;
45  import info.magnolia.ui.form.field.factory.FieldFactory;
46  import info.magnolia.ui.form.field.factory.FieldFactoryFactory;
47  
48  import java.util.List;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  import com.vaadin.ui.AbstractOrderedLayout;
53  import com.vaadin.ui.Component;
54  import com.vaadin.ui.FormLayout;
55  import com.vaadin.ui.TabSheet;
56  import com.vaadin.v7.data.Item;
57  import com.vaadin.v7.data.util.BeanItem;
58  import com.vaadin.v7.ui.CustomField;
59  import com.vaadin.v7.ui.Field;
60  import com.vaadin.v7.ui.VerticalLayout;
61  
62  /**
63   * A field displaying all supported metadata in a tabularized sheet.
64   */
65  public class AssetMetadataField extends CustomField<BeanItem> {
66  
67      protected final FieldFactoryFactory fieldFactoryFactory;
68      protected final I18nContentSupport i18nContentSupport;
69      protected final ComponentProvider componentProvider;
70      protected final AssetMetadataFieldDefinition definition;
71      protected final Item relatedFieldItem;
72      protected final AssetMetadataRegistry metadataRegistry;
73      protected AbstractOrderedLayout root;
74      private TabSheet tabsheet;
75  
76      public AssetMetadataField(AssetMetadataFieldDefinition definition, Item relatedFieldItem, FieldFactoryFactory fieldFactoryFactory, ComponentProvider componentProvider, AssetMetadataRegistry metadataRegistry) {
77          this.definition = definition;
78          this.fieldFactoryFactory = fieldFactoryFactory;
79          this.componentProvider = componentProvider;
80          this.relatedFieldItem = relatedFieldItem;
81          this.metadataRegistry = metadataRegistry;
82          this.i18nContentSupport = null;
83      }
84  
85      /**
86       * @deprecated since 2.1.5 - use {@link #AssetMetadataField(AssetMetadataFieldDefinition, Item, FieldFactoryFactory, ComponentProvider, AssetMetadataRegistry)} instead; field/factory never cared about i18nContentSupport anyway.
87       */
88      @Deprecated
89      public AssetMetadataField(AssetMetadataFieldDefinition definition, FieldFactoryFactory fieldFactoryFactory, I18nContentSupport i18nContentSupport, ComponentProvider componentProvider, AssetMetadataRegistry metadataRegistry, Item relatedFieldItem) {
90          this(definition, relatedFieldItem, fieldFactoryFactory, componentProvider, metadataRegistry);
91      }
92  
93      @Override
94      protected Component initContent() {
95          // Initialize root
96          root = new FormLayout();
97          setWidth(100, Unit.PERCENTAGE);
98          setHeight(-1, Unit.PIXELS);
99          addStyleName("switchablefield");
100         root.setWidth(100, Unit.PERCENTAGE);
101         root.setHeight(-1, Unit.PIXELS);
102         root.setSpacing(true);
103         tabsheet = new TabSheet();
104         tabsheet.setHeight(250, Unit.PIXELS);
105         // Initialize Existing field
106         initFields();
107         return root;
108     }
109 
110     protected void initFields() {
111         if (relatedFieldItem instanceof BeanItem && ((BeanItem) relatedFieldItem).getBean() instanceof Asset) {
112             initFields((BeanItem<Asset>) relatedFieldItem);
113         }
114     }
115 
116     protected void initFields(BeanItem<Asset> beanItem) {
117         root.removeAllComponents();
118         if (beanItem == null || beanItem.getBean() == null) {
119             return;
120         }
121         // Get Asset
122         Asset asset = beanItem.getBean();
123 
124         BeanToFieldDefinition convertor = new BeanToFieldDefinition();
125         for (AssetMetadataDefinition metadataDefinition : metadataRegistry.getAll()) {
126             if (asset.supports(metadataDefinition.getMetadataClass())) {
127                 // Handle TabSheet
128                 VerticalLayout sheet = createSheet();
129                 tabsheet.addTab(sheet, metadataDefinition.getNamespace());
130                 // Handle fields
131                 List<ConfiguredFieldDefinition> fields = convertor.getFiledDefinition(metadataDefinition.getMetadataClass());
132                 AssetMetadata metadata = asset.getMetadata(metadataDefinition.getMetadataClass());
133                 BeanItem<AssetMetadata> metadataBeanItem = new BeanItem<AssetMetadata>(metadata);
134                 for (ConfiguredFieldDefinition fieldDefinition : fields) {
135                     Field<?> field = createLocalField(fieldDefinition, metadataBeanItem, false);
136                     sheet.addComponent(field);
137                 }
138             }
139         }
140         root.addComponent(tabsheet);
141     }
142 
143     protected VerticalLayout createSheet() {
144         VerticalLayout sheet = new VerticalLayout();
145         sheet.setWidth(100, Unit.PERCENTAGE);
146         sheet.setHeight(-1, Unit.PIXELS);
147         sheet.setSpacing(true);
148         return sheet;
149     }
150 
151     protected Field<?> createLocalField(FieldDefinition fieldDefinition, Object item, boolean setCaptionToNull) {
152         FieldFactory fieldfactory = fieldFactoryFactory.createFieldFactory(fieldDefinition, item);
153         fieldfactory.setComponentProvider(componentProvider);
154         // FIXME change i18n setting : MGNLUI-1548
155         if (fieldDefinition instanceof ConfiguredFieldDefinition) {
156             ((ConfiguredFieldDefinition) fieldDefinition).setI18nBasename(definition.getI18nBasename());
157         }
158         Field<?> field = fieldfactory.createField();
159         //TODO: are we sure?
160 //        if (field instanceof AbstractComponent) {
161 //            ((AbstractComponent) field).setImmediate(true);
162 //        }
163         // Set Caption if desired
164         if (setCaptionToNull) {
165             field.setCaption(null);
166         } else if (StringUtils.isNotBlank(fieldDefinition.getLabel())) {
167             field.setCaption(fieldDefinition.getLabel());
168         }
169 
170         field.setWidth(100, Unit.PERCENTAGE);
171         return field;
172     }
173 
174     @Override
175     public Class<? extends BeanItem> getType() {
176         return (Class<? extends BeanItem>) BeanItem.class;
177     }
178 }