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.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
51
52
53
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
97
98
99
100
101
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
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
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
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 }