View Javadoc
1   /**
2    * This file Copyright (c) 2015-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.dam.external.app.field;
35  
36  import info.magnolia.cms.beans.config.MIMEMapping;
37  import info.magnolia.dam.api.Asset;
38  import info.magnolia.dam.app.ui.field.configuration.PreviewComponentProvider;
39  import info.magnolia.dam.external.app.field.definition.AssetMediaFieldDefinition;
40  import info.magnolia.objectfactory.ComponentProvider;
41  
42  import java.io.InputStream;
43  import java.text.SimpleDateFormat;
44  import java.util.Date;
45  
46  import org.apache.commons.lang3.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  import com.vaadin.server.Resource;
51  import com.vaadin.server.StreamResource;
52  import com.vaadin.ui.Button;
53  import com.vaadin.ui.Button.ClickEvent;
54  import com.vaadin.ui.Component;
55  import com.vaadin.ui.CssLayout;
56  import com.vaadin.ui.Image;
57  import com.vaadin.v7.data.Item;
58  import com.vaadin.v7.data.util.BeanItem;
59  import com.vaadin.v7.shared.ui.label.ContentMode;
60  import com.vaadin.v7.ui.CustomField;
61  import com.vaadin.v7.ui.Label;
62  
63  /**
64   * A base custom field comprising of an image and expand button.
65   */
66  public class AssetMediaField extends CustomField<BeanItem> {
67  
68      public static final String ICON_TAG = "<span class=\"file-preview preview-image %s v-label-icon-element\"></span>";
69      private static final String SEARCH_ICON = "<span class=\"icon-search\"></span>";
70      private static final Logger log = LoggerFactory.getLogger(AssetMediaField.class);
71      private CssLayout root;
72      protected Image embedded = new Image();
73  
74      protected final ComponentProvider componentProvider;
75      protected final AssetMediaFieldDefinition definition;
76      protected final Item relatedFieldItem;
77  
78      public AssetMediaField(AssetMediaFieldDefinition definition, Item relatedFieldItem, ComponentProvider componentProvider) {
79          this.componentProvider = componentProvider;
80          this.definition = definition;
81          this.relatedFieldItem = relatedFieldItem;
82      }
83  
84      @Override
85      protected Component initContent() {
86          root = new CssLayout();
87          root.setWidth("150px");
88          root.setHeight("150px");
89          setImmediate(false);
90          initFields();
91          return root;
92      }
93  
94      protected void initFields() {
95          if (relatedFieldItem instanceof BeanItem && ((BeanItem) relatedFieldItem).getBean() instanceof Asset) {
96              initFields((BeanItem<Asset>) relatedFieldItem);
97          }
98      }
99  
100     protected void initFields(BeanItem<Asset> beanItem) {
101         if (beanItem == null || beanItem.getBean() == null) {
102             return;
103         }
104         // Get Asset
105         final Asset asset = beanItem.getBean();
106         if (asset.getMimeType().startsWith("image/")) {
107             final Resource imageResource = getResource(asset);
108 
109             Button lightboxButton = new Button();
110             lightboxButton.addStyleName("lightbox-button");
111             lightboxButton.setHtmlContentAllowed(true);
112             lightboxButton.setCaption(SEARCH_ICON);
113             root.addComponent(lightboxButton);
114 
115             embedded = new Image("", imageResource);
116             embedded.addStyleName("preview-image");
117             root.addComponent(embedded);
118 
119             lightboxButton.addClickListener(new Button.ClickListener() {
120                 @Override
121                 public void buttonClick(ClickEvent event) {
122                     Class<? extends PreviewComponentProvider> previewActionClass = definition.getPreviewComponentProviderClass();
123                     // Launch Lightbox component
124                     if (previewActionClass != null) {
125                         PreviewComponentProvider implementation = componentProvider.newInstance(previewActionClass);
126                         implementation.open(imageResource);
127                     } else {
128                         log.warn("No preview component defined.");
129                     }
130                 }
131             });
132         } else {
133             Label icon = getIcon(asset);
134             icon.addStyleName("preview-image");
135             root.addComponent(icon);
136         }
137     }
138 
139     private Label getIcon(Asset asset) {
140         String mimeType = asset.getMimeType();
141         if (StringUtils.isBlank(mimeType)) {
142             return new Label(String.format(ICON_TAG, MIMEMapping.DEFAULT_ICON_STYLE), ContentMode.HTML);
143         }
144         String icon = MIMEMapping.getMIMETypeIconStyle(mimeType);
145         return new Label(String.format(ICON_TAG, icon), ContentMode.HTML);
146     }
147 
148     /**
149      * Create a resource from the content stream.
150      */
151     protected Resource getResource(final Asset asset) {
152         final String mimeType = asset.getMimeType();
153         StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {
154             @Override
155             public InputStream getStream() {
156                 return asset.getAssetProvider().getAsset(asset.getItemKey()).getContentStream();
157             }
158         };
159 
160         final Resource resource = new StreamResource(streamSource, makeImageFilename()) {
161             @Override
162             public String getMIMEType() {
163                 return mimeType;
164             }
165         };
166         return resource;
167     }
168 
169     /* Creates a image file name with a timestamp.
170      * See (https://vaadin.com/forum#!/thread/870107).
171      */
172     private String makeImageFilename() {
173         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
174         String timestamp = df.format(new Date());
175         return "connectors-" + timestamp;
176     }
177 
178     @Override
179     public Class<? extends BeanItem> getType() {
180         return (Class<? extends BeanItem>) BeanItem.class;
181     }
182 }