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