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 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
94
95
96
97
98
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
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
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
240
241 public static interface IdProvider {
242 List<?> getItemIds();
243 }
244 }