View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.workbench.thumbnail;
35  
36  import info.magnolia.ui.imageprovider.ImageProvider;
37  import info.magnolia.ui.workbench.container.Refreshable;
38  import info.magnolia.ui.workbench.thumbnail.ThumbnailContainer.ThumbnailItem;
39  
40  import java.util.Arrays;
41  import java.util.Collection;
42  import java.util.List;
43  
44  import com.vaadin.v7.data.Item;
45  import com.vaadin.v7.data.Property;
46  import com.vaadin.v7.data.util.AbstractInMemoryContainer;
47  import com.vaadin.v7.data.util.AbstractProperty;
48  
49  /**
50   * Container that provides thumbnails lazily.
51   *
52   * @deprecated since 5.3.10 in favor of {@link JcrThumbnailContainer}, this container
53   *             should be avoided as it loads the items eagerly.
54   */
55  public class ThumbnailContainer extends AbstractInMemoryContainer<Object, Object, ThumbnailItem> implements Refreshable, info.magnolia.ui.vaadin.layout.data.ThumbnailContainer {
56  
57      public static final String THUMBNAIL_PROPERTY_ID = "thumbnail";
58  
59      private final ImageProvider imageProvider;
60  
61      private IdProvider idProvider;
62  
63      private int thumbnailWidth = 0;
64  
65      private int thumbnailHeight = 0;
66  
67      public ThumbnailContainer(ImageProvider imageProvider, IdProvider idProvider) {
68          super();
69          this.imageProvider = imageProvider;
70          this.idProvider = idProvider;
71      }
72  
73      @Override
74      public Collection<String> getContainerPropertyIds() {
75          return Arrays.asList(THUMBNAIL_PROPERTY_ID);
76      }
77  
78      @Override
79      public ThumbnailContainerProperty getContainerProperty(Object itemId, Object propertyId) {
80          if (THUMBNAIL_PROPERTY_ID.equals(propertyId)) {
81              return new ThumbnailContainerProperty(itemId, imageProvider);
82          }
83          return null;
84      }
85  
86      @Override
87      public Class<?> getType(Object propertyId) {
88          if (THUMBNAIL_PROPERTY_ID.equals(propertyId)) {
89              return Object.class;
90          }
91          return null;
92      }
93  
94  
95      /**
96       * @return a List of JCR identifiers for all the nodes recursively found
97       * under <code>initialPath</code>. This method is called in {@link ThumbnailViewImpl#refresh()}. You can override it, if
98       * you need a different strategy than the default one to fetch the
99       * identifiers of the nodes for which thumbnails need to be
100      * displayed.
101      * @see info.magnolia.ui.vaadin.layout.LazyLayout#refresh()
102      */
103     protected List<?> getAllIdentifiers() {
104         return idProvider.getItemIds();
105     }
106 
107 
108     @Override
109     public void refresh() {
110         getAllItemIds().clear();
111         getAllItemIds().addAll(getAllIdentifiers());
112     }
113 
114     @Override
115     public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) {
116         throw new UnsupportedOperationException();
117     }
118 
119     @Override
120     public Item addItem(Object itemId) throws UnsupportedOperationException {
121         throw new UnsupportedOperationException();
122     }
123 
124     @Override
125     public Object addItem() throws UnsupportedOperationException {
126         throw new UnsupportedOperationException();
127     }
128 
129     @Override
130     public boolean removeItem(Object itemId) throws UnsupportedOperationException {
131         throw new UnsupportedOperationException();
132     }
133 
134     @Override
135     public boolean removeAllItems() throws UnsupportedOperationException {
136         throw new UnsupportedOperationException();
137     }
138 
139     @Override
140     public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException {
141         throw new UnsupportedOperationException();
142     }
143 
144     @Override
145     protected ThumbnailItem getUnfilteredItem(Object itemId) {
146         return new ThumbnailItem(String.valueOf(itemId));
147     }
148 
149     public ImageProvider getImageProvider() {
150         return imageProvider;
151     }
152 
153     public void setThumbnailHeight(int thumbnailHeight) {
154         this.thumbnailHeight = thumbnailHeight;
155     }
156 
157     public void setThumbnailWidth(int thumbnailWidth) {
158         this.thumbnailWidth = thumbnailWidth;
159     }
160 
161     public int getThumbnailHeight() {
162         return thumbnailHeight;
163     }
164 
165     public int getThumbnailWidth() {
166         return thumbnailWidth;
167     }
168 
169     /**
170      * ThumbnailContainer property. Can have a Resource or a String as value.
171      */
172     public class ThumbnailContainerProperty extends AbstractProperty<Object> {
173 
174         private Object resourceId;
175 
176         private final ImageProvider imageProvider;
177 
178         public ThumbnailContainerProperty(final Object resourceId, ImageProvider imageProvider) {
179             this.resourceId = resourceId;
180             this.imageProvider = imageProvider;
181         }
182 
183         @Override
184         public Object getValue() {
185             if (imageProvider == null) {
186                 return null;
187             }
188             return imageProvider.getThumbnailResource(resourceId, ImageProvider.THUMBNAIL_GENERATOR);
189         }
190 
191         @Override
192         public void setValue(Object newValue) throws ReadOnlyException {
193             this.resourceId = newValue;
194         }
195 
196         @Override
197         public Class<Object> getType() {
198             return Object.class;
199         }
200     }
201 
202     /**
203      * Thumbnail Item.
204      */
205     public class ThumbnailItem implements Item {
206 
207         private final Object id;
208 
209         public ThumbnailItem(final Object id) {
210             this.id = id;
211         }
212 
213         @Override
214         public Property<?> getItemProperty(Object id) {
215             if (THUMBNAIL_PROPERTY_ID.equals(id)) {
216                 return new ThumbnailContainerProperty(this.id, imageProvider);
217             }
218             return null;
219         }
220 
221         public Object getItemId() {
222             return id;
223         }
224 
225         @Override
226         public Collection<?> getItemPropertyIds() {
227             return Arrays.asList(THUMBNAIL_PROPERTY_ID);
228         }
229 
230         @Override
231         public boolean addItemProperty(Object id, @SuppressWarnings("rawtypes") Property property) throws UnsupportedOperationException {
232             throw new UnsupportedOperationException();
233         }
234 
235         @Override
236         public boolean removeItemProperty(Object id) throws UnsupportedOperationException {
237             throw new UnsupportedOperationException();
238         }
239     }
240 
241     /**
242      * Provides a list of thumbnail item ids.
243      */
244     public static interface IdProvider {
245         List<?> getItemIds();
246     }
247 
248     @Override
249     public Object getThumbnailPropertyId() {
250         return THUMBNAIL_PROPERTY_ID;
251     }
252 
253     @Override
254     public Property getThumbnailProperty(Object itemId) {
255         return getContainerProperty(itemId, THUMBNAIL_PROPERTY_ID);
256     }
257 
258     @Override
259     public Property getCaptionProperty(Object itemId) {
260         throw new UnsupportedOperationException();
261     }
262 
263 }