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: AbstractVisualResourceProvider.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.LinkedList;
21  import java.util.List;
22  
23  import org.apache.log4j.Logger;
24  import org.eclipse.jface.resource.ImageDescriptor;
25  
26  import pl.edu.agh.cast.Activator;
27  
28  /**
29   * Abstract implementation of {@link IVisualResourcesProvider} which introduce helper methods for creating new
30   * providers.
31   *
32   * @author AGH CAST Team
33   */
34  public abstract class AbstractVisualResourcesProvider implements IVisualResourcesProvider {
35  
36  	private static final String EXTENSION_SEPARATOR = "."; //$NON-NLS-1$
37  	
38  	private static final String DIRECTORY_SEPARATOR = "/";  //$NON-NLS-1$
39  
40  	private static final String LOG_MESSAGE_0 = "An image %1$s in %2$s created for resource %3$s (registered image size %4$s)."; //$NON-NLS-1$
41  
42  	private static final String LOG_MESSAGE_1 = "Cannot find image in the following path: %1$s."; //$NON-NLS-1$
43  
44  	private List<IVisualResource> resources;
45  
46  	/**
47  	 * Logger.
48  	 */
49  	protected static Logger log = Activator.getLogger();
50  
51  	/**
52  	 * Initializes provider with given entries.
53  	 *
54  	 * @param location
55  	 *            defines the base path for image file lookup (see {@link
56  	 *            org.eclipse.jface.resource.ImageDescriptor.createFromFile(Class, String)} <code>location</code>
57  	 *            parameter)
58  	 * @param entries
59  	 *            list of entries which describe resources
60  	 */
61  	protected void initialize(Class<?> location, List<VisualResourceEntry> entries) {
62  		if (resources == null) {
63  			resources = new LinkedList<IVisualResource>();
64  		}
65  		if (entries == null) {
66  			throw new IllegalArgumentException("The entries list cannot be empty"); //$NON-NLS-1$
67  		}
68  		for (VisualResourceEntry entry : entries) {
69  			resources.add(createVisualResource(location, entry));
70  		}
71  	}
72  
73  	private IVisualResource createVisualResource(Class<?> location, VisualResourceEntry entry) {
74  		assert entry != null;
75  		VisualResource res = new VisualResource(entry.getId(), entry.getLabel(), entry.getDescription(), entry
76  		        .getTags(), entry.getGroupId(), entry.getType());
77  
78  		if (entry.getImageName() == null || entry.getImagesPath() == null) {
79  			// images are not defined, just return resource at it is
80  			return res;
81  		}
82  
83  		// register default icon
84  		for (ImageSize size : ImageSize.values()) {
85  			String path = createIconPath(entry.getImagesPath(), entry.getImageName(), size, entry.getImageExtension());
86  			try {
87  				res.registerDefaultImageDesriptor(size, ImageDescriptor.createFromFile(location, path));
88  				log.info(String.format(LOG_MESSAGE_0, entry.getImageName(), path, res.getLabel(), size.toString()));
89  			} catch (ResourceException e) {
90  				log.debug(String.format(LOG_MESSAGE_1, path));
91  			}
92  		}
93  
94  		// read other variants with all size
95  		if (entry.getImageVariants() != null) {
96  			for (String variant : entry.getImageVariants()) {
97  				for (ImageSize size : ImageSize.values()) {
98  					String path = createIconPath(entry.getImagesPath(), entry.getImageName(), variant, size, entry
99  					        .getImageExtension());
100 					try {
101 						res.registerVariantImageDescriptor(variant, size, ImageDescriptor
102 						        .createFromFile(location, path));
103 						log.info(String.format(LOG_MESSAGE_0, entry.getImageName(), path, res.getLabel(), size
104 						        .toString()));
105 					} catch (ResourceException e) {
106 						log.debug(String.format(LOG_MESSAGE_1, path));
107 					}
108 				}
109 			}
110 		}
111 
112 		return res;
113 	}
114 
115 	private String createIconPath(String path, String name, ImageSize size, String extension) {
116 		StringBuilder str = new StringBuilder();
117 		str.append(path);
118 		if (!path.endsWith(DIRECTORY_SEPARATOR)) {
119 			str.append(DIRECTORY_SEPARATOR);
120 		}
121 		str.append(name);
122 		if (!size.getNameExtension().isEmpty()) {
123 			str.append(IVisualResource.NAME_SEPARATOR);
124 			str.append(size.getNameExtension());
125 		}
126 		if (!extension.startsWith(EXTENSION_SEPARATOR)) {
127 			str.append(EXTENSION_SEPARATOR);
128 		}
129 		str.append(extension != null ? extension : VisualResourceEntry.RESOURCE_EXTENSION_PNG);
130 
131 		return str.toString();
132 	}
133 
134 	private String createIconPath(String path, String name, String variant, ImageSize size, String extension) {
135 		StringBuilder str = new StringBuilder();
136 		str.append(path);
137 		if (!path.endsWith(DIRECTORY_SEPARATOR)) {
138 			str.append(DIRECTORY_SEPARATOR);
139 		}
140 		str.append(name);
141 		str.append(IVisualResource.NAME_SEPARATOR);
142 		str.append(variant);
143 		if (!size.getNameExtension().isEmpty()) {
144 			str.append(IVisualResource.NAME_SEPARATOR);
145 			str.append(size.getNameExtension());
146 		}
147 		if (!extension.startsWith(EXTENSION_SEPARATOR)) {
148 			str.append(EXTENSION_SEPARATOR);
149 		}
150 		str.append(extension != null ? extension : VisualResourceEntry.RESOURCE_EXTENSION_PNG);
151 
152 		return str.toString();
153 	}
154 
155 	/**
156 	 * {@inheritDoc}
157 	 *
158 	 * @see pl.edu.agh.cast.resource.IVisualResourcesProvider#getVisualResources()
159 	 */
160 	@Override
161 	public List<IVisualResource> getVisualResources() {
162 		return resources;
163 	}
164 
165 }