1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
52
53
54
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
98
99
100
101
102
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
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
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
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 }