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