View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.form.field;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.ui.imageprovider.ImageProvider;
38  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
39  
40  import javax.jcr.Node;
41  import javax.jcr.RepositoryException;
42  
43  import org.apache.commons.lang3.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  import com.vaadin.server.ExternalResource;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.Image;
50  import com.vaadin.v7.shared.ui.label.ContentMode;
51  import com.vaadin.v7.ui.CustomField;
52  import com.vaadin.v7.ui.Field;
53  import com.vaadin.v7.ui.HorizontalLayout;
54  import com.vaadin.v7.ui.Label;
55  
56  /**
57   * A base custom field comprising of a Thumbnail and related image information.
58   * This Field is waiting for path Node value.
59   */
60  public class ThumbnailField extends CustomField<String> {
61      private static final Logger log = LoggerFactory.getLogger(ThumbnailField.class);
62      private HorizontalLayout layout;
63      private Label label;
64      protected Image embedded = new Image();
65  
66      private ImageProvider imageThumbnailProvider;
67      private String workspace;
68  
69      private String currentIdentifier = "";
70  
71      public ThumbnailField(ImageProvider imageThumbnailProvider, String workspace) {
72          this.imageThumbnailProvider = imageThumbnailProvider;
73          this.workspace = workspace;
74          this.layout = new HorizontalLayout();
75  
76          label = new Label("", ContentMode.HTML);
77          label.addStyleName("thumbnail-info");
78  
79          addStyleName("thumbnail-field");
80          setSizeUndefined();
81  
82      }
83  
84      @Override
85      protected Component initContent() {
86          layout.addComponent(label);
87          layout.addComponent(embedded);
88          return layout;
89      }
90  
91      @Override
92      public Class<String> getType() {
93          return String.class;
94      }
95  
96      /**
97       * Create a value change listener in order to refresh the View.
98       */
99      public void ValueChangeListener(Field<?> field) {
100         field.addValueChangeListener(new ValueChangeListener() {
101             @Override
102             public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
103                 setLabelAndImage(event.getProperty().getValue().toString());
104             }
105         });
106     }
107 
108     /**
109      * Set the Label And Image.
110      */
111     public void setLabelAndImage(String nodePath) {
112         try {
113             if (StringUtils.isEmpty(nodePath)) {
114                 return;
115             }
116             Node parentNode = MgnlContext.getJCRSession(workspace).getNode(nodePath);
117             String uuid = parentNode.getIdentifier();
118 
119             if (!currentIdentifier.equals(uuid)) {
120                 // Set Text info
121                 label.setValue(createFieldDetail(parentNode));
122                 // Set Thumbnail
123                 String path = imageThumbnailProvider.getPortraitPath(new JcrNodeAdapter(parentNode));
124                 if (layout.getComponentIndex(embedded) != -1) {
125                     layout.removeComponent(embedded);
126                 }
127                 embedded = path != null ? new Image("", new ExternalResource(path)) : new Image(null);
128                 layout.addComponent(embedded);
129             }
130         } catch (RepositoryException e) {
131             log.warn("Not able to refresh the Thumbnail Field view for the following Node path: {}", nodePath, e);
132         }
133     }
134 
135     /**
136      * Create a field detail displayed after the thumbnail.
137      */
138     public String createFieldDetail(Node parentNode) throws RepositoryException {
139         return "";
140     }
141 }