View Javadoc
1   /**
2    * This file Copyright (c) 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.ui.preview;
35  
36  
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.contentapp.browser.preview.PreviewProvider;
39  import info.magnolia.ui.datasource.PropertySetFactory;
40  
41  import java.util.Map;
42  import java.util.Optional;
43  
44  import javax.inject.Inject;
45  
46  import org.apache.commons.collections4.MapUtils;
47  
48  import com.vaadin.data.PropertySet;
49  import com.vaadin.server.Resource;
50  import com.vaadin.ui.Component;
51  import com.vaadin.ui.CustomComponent;
52  import com.vaadin.ui.FormLayout;
53  import com.vaadin.ui.HorizontalLayout;
54  import com.vaadin.ui.Image;
55  import com.vaadin.ui.Layout;
56  
57  /**
58   * Abstract implementation of {@link ItemPreviewComponent}.
59   * An item preview usually consists of two parts
60   * <ul>
61   *     <li> image preview: an image, if any, representing the item
62   *     <li> details: e.g. item properties and their respective values
63   * </ul>
64   *
65   * @param <T> the item type to preview.
66   */
67  public abstract class AbstractItemPreviewComponent<T> extends CustomComponent implements ItemPreviewComponent<T> {
68  
69      private Component itemDetail;
70      private Component itemPreview;
71      private final Layout rootLayout;
72      private final PreviewProvider<T> previewProvider;
73      private final SimpleTranslator i18n;
74      private final PropertySet<T> propertySet;
75  
76  
77      @Inject
78      public AbstractItemPreviewComponent(SimpleTranslator i18n, PropertySetFactory<T> propertySetFactory, PreviewProvider<T> previewProvider, Map<String, Class> properties) {
79          if (MapUtils.isEmpty(properties)) {
80              throw new IllegalArgumentException("Map containing properties to be displayed can't be empty or null.");
81          }
82          this.previewProvider = previewProvider;
83          this.i18n = i18n;
84          this.propertySet = propertySetFactory.withProperties(properties);
85          this.rootLayout = new HorizontalLayout();
86          setCompositionRoot(rootLayout);
87      }
88  
89      @Override
90      public void onValueChange(T item) {
91          clearRootLayout();
92          Optional.ofNullable(item).ifPresent(i -> {
93              itemDetail = refreshItemDetail(i);
94              itemPreview = refreshItemPreview(i);
95              refreshRootLayout();
96          });
97      }
98  
99      /**
100      * Refreshes the item detail.
101      * This method is triggered by {@link ItemPreviewComponent#onValueChange(Object)} in case of value changes.
102      */
103     protected Component refreshItemDetail(T item) {
104         FormLayout itemDetail = new FormLayout();
105         itemDetail.addStyleName("file-details");
106 
107         getPropertySet().getProperties()
108                 .map(property -> this.createDetailComponent(property.getName(), property.getGetter().apply(item)))
109                 .forEach(itemDetail::addComponent);
110 
111         return itemDetail;
112     }
113 
114     /**
115      * @return typically a Label displaying an i18n-ised, human-readable property name and its value.
116      */
117     protected abstract Component createDetailComponent(String propertyName, Object value);
118 
119     /**
120      * Refreshes the item preview.
121      * This method is triggered by {@link ItemPreviewComponent#onValueChange(Object)}} in case of value changes.
122      */
123     protected Component refreshItemPreview(T item) {
124         return getPreviewProvider().getResource(item).map(this::createPreviewImageComponent).orElse(null);
125     }
126 
127     protected void clearRootLayout() {
128         rootLayout.setVisible(false);
129         rootLayout.removeAllComponents();
130         removeStyleName("done");
131     }
132 
133     protected void refreshRootLayout() {
134         rootLayout.setVisible(true);
135         addStyleName("done");
136 
137         Optional.ofNullable(itemPreview).ifPresent(rootLayout::addComponent);
138         Optional.ofNullable(itemDetail).ifPresent(rootLayout::addComponent);
139     }
140 
141     protected PropertySet<T> getPropertySet() {
142         return propertySet;
143     }
144 
145     protected SimpleTranslator getI18n() {
146         return i18n;
147     }
148 
149     protected PreviewProvider<T> getPreviewProvider() {
150         return previewProvider;
151     }
152 
153     private Image createPreviewImageComponent(Resource res) {
154         final Image image =  new Image("", res);
155         image.addStyleName("file-preview-area");
156         return image;
157     }
158 }