View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: VisualResource.java
13   * Created: 2009-07-21
14   * Author: kpietak
15   * $Id$
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   * Default implementation of {@link IVisualResource}.
33   *
34   * @author AGH CAST Team
35   */
36  public class VisualResource implements IVisualResource {
37  
38  	private static final String ERROR_MSG_01 = "Cannot create image: "; //$NON-NLS-1$
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  	 * Constructor with all required parameters.
62  	 *
63  	 * @param id
64  	 *            resource id
65  	 * @param label
66  	 *            resource label
67  	 * @param description
68  	 *            resource description
69  	 */
70  	public VisualResource(String id, String label, String description) {
71  		this(id, label, description, null, null, null);
72  	}
73  
74  	/**
75  	 * Constructor.
76  	 *
77  	 * @param id
78  	 *            resource id
79  	 * @param label
80  	 *            resource label
81  	 * @param description
82  	 *            resource description
83  	 * @param tags
84  	 *            resource tags
85  	 * @param groupId
86  	 *            resource group id
87  	 * @param type
88  	 *            type of described element or data set
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 	 * Registers image descriptor for default image with specified size. Descriptor and size cannot be equal to
102 	 * <code>null</code>. If an image descriptor has been already registered, it is overridden with a new one.
103 	 *
104 	 * @param size
105 	 *            image size
106 	 * @param descriptor
107 	 *            image descriptor
108 	 * @return image descriptor which was registered with the same size before (overridden one), <code>null</code> if
109 	 *         there was no such image
110 	 * @throws ResourceException
111 	 *             when image cannot be created
112 	 * @throws IllegalArgumentException
113 	 *             when at least one of the given arguments is <code>null</code>
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)"); //$NON-NLS-1$
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 	 * Registers image descriptor for variant with specified size. All arguments cannot be <code>null</code>. If an
136 	 * image descriptor has been already registered, it is overridden with a new one.
137 	 *
138 	 * @param variant
139 	 *            id of image variant
140 	 * @param size
141 	 *            image size
142 	 * @param descriptor
143 	 *            image descriptor
144 	 * @return image descriptor for specified variant which was registered with the same size before (overridden one),
145 	 *         <code>null</code> if there was no such image
146 	 * @throws ResourceException
147 	 *             when image cannot be created
148 	 * @throws IllegalArgumentException
149 	 *             when at least one of the given arguments is <code>null</code>
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 " + //$NON-NLS-1$
157 			        "with null argument(s)"); //$NON-NLS-1$
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 	 * {@inheritDoc}
181 	 *
182 	 * @see pl.edu.agh.cast.resource.IVisualResource#getDescription()
183 	 */
184 	@Override
185 	public String getDescription() {
186 		return description;
187 	}
188 
189 	/**
190 	 * {@inheritDoc}
191 	 *
192 	 * @see pl.edu.agh.cast.resource.IVisualResource#getGroupId()
193 	 */
194 	@Override
195 	public String getGroupId() {
196 		return groupId;
197 	}
198 
199 	/**
200 	 * {@inheritDoc}
201 	 *
202 	 * @see pl.edu.agh.cast.resource.IVisualResource#getId()
203 	 */
204 	@Override
205 	public String getId() {
206 		return id;
207 	}
208 
209 	/**
210 	 * {@inheritDoc}
211 	 *
212 	 * @see pl.edu.agh.cast.resource.IVisualResource#getImage()
213 	 */
214 	@Override
215 	public Image getImage() {
216 		if (getAvailableDefaultImageSizes().size() == 1) { // if we have only one image - return it
217 			for (ImageSize size : getAvailableDefaultImageSizes()) {
218 				return getImage(size); // we can return here as we have only one element in the collection
219 			}
220 		}
221 		return getImage(ImageSize.getDefaultSize());
222 
223 	}
224 
225 	/**
226 	 * {@inheritDoc}
227 	 *
228 	 * @see pl.edu.agh.cast.resource.IVisualResource#getImage(pl.edu.agh.cast.resource.ImageSize)
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 " + //$NON-NLS-1$
238 			                "size use getImage method with no parameters"); //$NON-NLS-1$
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 	 * {@inheritDoc}
255 	 *
256 	 * @see pl.edu.agh.cast.resource.IVisualResource#getImage(java.lang.String)
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"); //$NON-NLS-1$
262 		}
263 		if (getAvailableVariantImageSizes(variant).size() == 1) {// if we have only one image - return it
264 			for (ImageSize size : getAvailableVariantImageSizes(variant)) {
265 				return getImage(variant, size);
266 			}
267 		}
268 		return getImage(variant, ImageSize.getDefaultSize());
269 	}
270 
271 	/**
272 	 * {@inheritDoc}
273 	 *
274 	 * @see pl.edu.agh.cast.resource.IVisualResource#getImage(java.lang.String, pl.edu.agh.cast.resource.ImageSize)
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"); //$NON-NLS-1$
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 	 * {@inheritDoc}
303 	 *
304 	 * @see pl.edu.agh.cast.resource.IVisualResource#getLabel()
305 	 */
306 	@Override
307 	public String getLabel() {
308 		return label;
309 	}
310 
311 	/**
312 	 * {@inheritDoc}
313 	 *
314 	 * @see pl.edu.agh.cast.resource.IVisualResource#getTags()
315 	 */
316 	@Override
317 	public List<String> getTags() {
318 		return tags;
319 	}
320 
321 	/**
322 	 * {@inheritDoc}
323 	 *
324 	 * @see pl.edu.agh.cast.resource.IVisualResource#getType()
325 	 */
326 	@Override
327 	public Type getType() {
328 		return type;
329 	}
330 
331 	/**
332 	 * {@inheritDoc}
333 	 *
334 	 * @see pl.edu.agh.cast.resource.IVisualResource#dispose()
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 	 * @param id
362 	 *            the id to set
363 	 */
364 	protected void setId(String id) {
365 		if (id == null || id.isEmpty()) {
366 			throw new IllegalArgumentException("Resource cannot have null or empty id"); //$NON-NLS-1$
367 		}
368 		this.id = id;
369 	}
370 
371 	/**
372 	 * @param label
373 	 *            the label to set
374 	 */
375 	protected void setLabel(String label) {
376 		if (label == null) {
377 			throw new IllegalArgumentException("Resource cannot have null label"); //$NON-NLS-1$
378 		}
379 		this.label = label;
380 	}
381 
382 	/**
383 	 * @param description
384 	 *            the description to set
385 	 */
386 	protected void setDescription(String description) {
387 		this.description = description;
388 	}
389 
390 	/**
391 	 * @param tags
392 	 *            the tags to set
393 	 */
394 	protected void setTags(List<String> tags) {
395 		this.tags = tags;
396 	}
397 
398 	/**
399 	 * @param groupId
400 	 *            the groupId to set
401 	 */
402 	protected void setGroupId(String groupId) {
403 		this.groupId = groupId;
404 	}
405 
406 	/**
407 	 * @param type
408 	 *            the type to set
409 	 */
410 	protected void setType(Type type) {
411 		this.type = type;
412 	}
413 
414 	// BEGIN CODE - Helper methods
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 	// END CODE - Helper methods
437 }