1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.resource;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.swt.graphics.Image;
28
29 import pl.edu.agh.cast.data.model.Type;
30
31
32
33
34
35
36 public class VisualResource implements IVisualResource {
37
38 private static final String ERROR_MSG_01 = "Cannot create image: ";
39
40 private String id;
41
42 private String label;
43
44 private String description;
45
46 private List<String> tags;
47
48 private String groupId;
49
50 private Type type;
51
52 private Map<ImageSize, ImageDescriptor> defaultImageDescriptors;
53
54 private Map<ImageSize, Image> defaultImages;
55
56 private Map<String, Map<ImageSize, ImageDescriptor>> variantImageDescriptors;
57
58 private Map<String, Map<ImageSize, Image>> variantImages;
59
60
61
62
63
64
65
66
67
68
69
70 public VisualResource(String id, String label, String description) {
71 this(id, label, description, null, null, null);
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public VisualResource(String id, String label, String description, List<String> tags, String groupId, Type type) {
91 initialize();
92 setId(id);
93 setLabel(label);
94 setDescription(description);
95 setTags(tags);
96 setGroupId(groupId);
97 setType(type);
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public ImageDescriptor registerDefaultImageDesriptor(ImageSize size, ImageDescriptor descriptor)
116 throws ResourceException {
117 assert defaultImageDescriptors != null;
118 if (size == null || descriptor == null) {
119 throw new IllegalArgumentException("Cannot register image descriptor with null argument(s)");
120 }
121 Image img = descriptor.createImage(false);
122 if (img != null) {
123 defaultImages.put(size, img);
124 return defaultImageDescriptors.put(size, descriptor);
125 } else {
126 StringBuilder stringBuilder = new StringBuilder();
127 stringBuilder.append(ERROR_MSG_01);
128 stringBuilder.append(descriptor.toString());
129 throw new ResourceException(stringBuilder.toString());
130 }
131
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public ImageDescriptor registerVariantImageDescriptor(String variant, ImageSize size, ImageDescriptor descriptor)
152 throws ResourceException {
153 assert variantImageDescriptors != null;
154 assert variantImages != null;
155 if (variant == null || variant.isEmpty() || size == null || descriptor == null) {
156 throw new IllegalArgumentException("Cannot register variants of image descriptor " +
157 "with null argument(s)");
158 }
159 if (!variantImageDescriptors.containsKey(variant)) {
160 variantImageDescriptors.put(variant, new HashMap<ImageSize, ImageDescriptor>());
161 }
162 if (!variantImages.containsKey(variant)) {
163 variantImages.put(variant, new HashMap<ImageSize, Image>());
164 }
165
166 Image img = descriptor.createImage(false);
167 if (img != null) {
168 variantImages.get(variant).put(size, img);
169 return variantImageDescriptors.get(variant).put(size, descriptor);
170 } else {
171 StringBuilder stringBuilder = new StringBuilder();
172 stringBuilder.append(ERROR_MSG_01);
173 stringBuilder.append(descriptor);
174 throw new ResourceException(stringBuilder.toString());
175 }
176
177 }
178
179
180
181
182
183
184 @Override
185 public String getDescription() {
186 return description;
187 }
188
189
190
191
192
193
194 @Override
195 public String getGroupId() {
196 return groupId;
197 }
198
199
200
201
202
203
204 @Override
205 public String getId() {
206 return id;
207 }
208
209
210
211
212
213
214 @Override
215 public Image getImage() {
216 if (getAvailableDefaultImageSizes().size() == 1) {
217 for (ImageSize size : getAvailableDefaultImageSizes()) {
218 return getImage(size);
219 }
220 }
221 return getImage(ImageSize.getDefaultSize());
222
223 }
224
225
226
227
228
229
230 @Override
231 public Image getImage(ImageSize size) {
232 assert defaultImageDescriptors != null;
233 assert defaultImages != null;
234
235 if (size == null) {
236 throw new IllegalArgumentException(
237 "Cannot return image with null size. If you want to get image with default " +
238 "size use getImage method with no parameters");
239 }
240 if (defaultImages.containsKey(size)) {
241 return defaultImages.get(size);
242 } else if (defaultImageDescriptors.containsKey(size)) {
243 Image img = defaultImageDescriptors.get(size).createImage();
244 if (img != null) {
245 defaultImages.put(size, img);
246 }
247 return img;
248 }
249
250 return null;
251 }
252
253
254
255
256
257
258 @Override
259 public Image getImage(String variant) {
260 if (variant == null || variant.isEmpty()) {
261 throw new IllegalArgumentException("Cannot get image with null or empty variant key");
262 }
263 if (getAvailableVariantImageSizes(variant).size() == 1) {
264 for (ImageSize size : getAvailableVariantImageSizes(variant)) {
265 return getImage(variant, size);
266 }
267 }
268 return getImage(variant, ImageSize.getDefaultSize());
269 }
270
271
272
273
274
275
276 @Override
277 public Image getImage(String variant, ImageSize size) {
278 assert variantImageDescriptors != null;
279 assert variantImages != null;
280
281 if (variant == null || variant.isEmpty() || size == null) {
282 throw new IllegalArgumentException("Cannot get image with null variant or size");
283 }
284 if (variantImages.containsKey(variant) && variantImages.get(variant).containsKey(size)) {
285 return variantImages.get(variant).get(size);
286 } else if (variantImageDescriptors.containsKey(variant)
287 && variantImageDescriptors.get(variant).containsKey(size)) {
288 Image img = variantImageDescriptors.get(variant).get(size).createImage();
289 if (img != null) {
290 if (!variantImages.containsKey(variant)) {
291 variantImages.put(variant, new HashMap<ImageSize, Image>());
292 }
293 variantImages.get(variant).put(size, img);
294 }
295 return img;
296 }
297
298 return null;
299 }
300
301
302
303
304
305
306 @Override
307 public String getLabel() {
308 return label;
309 }
310
311
312
313
314
315
316 @Override
317 public List<String> getTags() {
318 return tags;
319 }
320
321
322
323
324
325
326 @Override
327 public Type getType() {
328 return type;
329 }
330
331
332
333
334
335
336 @Override
337 public void dispose() {
338
339 for (ImageSize size : defaultImages.keySet()) {
340 Image img = defaultImages.get(size);
341 if (img != null && !img.isDisposed()) {
342 img.dispose();
343 }
344 }
345 defaultImages.clear();
346
347 for (Map<ImageSize, Image> map : variantImages.values()) {
348 for (ImageSize size : map.keySet()) {
349 Image img = map.get(size);
350 if (img != null && !img.isDisposed()) {
351 img.dispose();
352 }
353 }
354 map.clear();
355 }
356 variantImages.clear();
357
358 }
359
360
361
362
363
364 protected void setId(String id) {
365 if (id == null || id.isEmpty()) {
366 throw new IllegalArgumentException("Resource cannot have null or empty id");
367 }
368 this.id = id;
369 }
370
371
372
373
374
375 protected void setLabel(String label) {
376 if (label == null) {
377 throw new IllegalArgumentException("Resource cannot have null label");
378 }
379 this.label = label;
380 }
381
382
383
384
385
386 protected void setDescription(String description) {
387 this.description = description;
388 }
389
390
391
392
393
394 protected void setTags(List<String> tags) {
395 this.tags = tags;
396 }
397
398
399
400
401
402 protected void setGroupId(String groupId) {
403 this.groupId = groupId;
404 }
405
406
407
408
409
410 protected void setType(Type type) {
411 this.type = type;
412 }
413
414
415
416 private void initialize() {
417 defaultImageDescriptors = new HashMap<ImageSize, ImageDescriptor>();
418 defaultImages = new HashMap<ImageSize, Image>();
419 variantImageDescriptors = new HashMap<String, Map<ImageSize, ImageDescriptor>>();
420 variantImages = new HashMap<String, Map<ImageSize, Image>>();
421 }
422
423 private Set<ImageSize> getAvailableDefaultImageSizes() {
424 return defaultImageDescriptors.keySet();
425 }
426
427 @SuppressWarnings("unchecked")
428 private Set<ImageSize> getAvailableVariantImageSizes(String variant) {
429 assert variant != null && !variant.isEmpty();
430 if (variantImageDescriptors.containsKey(variant)) {
431 return variantImageDescriptors.get(variant).keySet();
432 }
433 return (Set<ImageSize>)Collections.EMPTY_SET;
434 }
435
436
437 }