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