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: Images.java
13   * Created: 2007-00-00
14   * Author: cast
15   * $Id: Images.java 3070 2009-07-28 14:49:20Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.util;
19  
20  import java.lang.reflect.Field;
21  import java.net.URL;
22  
23  import org.apache.log4j.Logger;
24  import org.eclipse.core.runtime.FileLocator;
25  import org.eclipse.core.runtime.Path;
26  import org.eclipse.core.runtime.Platform;
27  import org.eclipse.jface.resource.ImageDescriptor;
28  import org.eclipse.jface.resource.ImageRegistry;
29  import org.eclipse.swt.graphics.Image;
30  
31  import pl.edu.agh.cast.Activator;
32  import pl.edu.agh.cast.CastApplication;
33  
34  /**
35   * Convenience class for storing references to image descriptors used by the README tool.
36   *
37   * @author AGH CAST Team
38   */
39  public final class Images {
40  
41  	private static Logger log = Activator.getLogger();
42  
43  	private static URL baseURL = FileLocator.find(Platform.getBundle(CastApplication.ID), new Path("."), null); //$NON-NLS-1$
44  
45  	private static ImageRegistry registry;
46  
47  	private static Images instance;
48  
49  	private static final String ICON_PATH = "icons/"; //$NON-NLS-1$
50  
51  	// /////////////////////////////////////////////////////////////////
52  	// applications
53  	// /////////////////////////////////////////////////////////////////
54  
55  	/**
56  	 * Project icon image filename.
57  	 */
58  	public static final String PROJECT = "prj_obj.gif"; //$NON-NLS-1$
59  
60  	/**
61  	 * File icon image filename.
62  	 */
63  	public static final String FILE = "file_obj.gif"; //$NON-NLS-1$
64  
65  	/**
66  	 * Folder icon image filename.
67  	 */
68  	public static final String FOLDER = "fldr_obj.gif"; //$NON-NLS-1$
69  
70  	/**
71  	 * Template icon image filename.
72  	 */
73  	public static final String TEMPLATE = "template.gif"; //$NON-NLS-1$
74  
75  	/**
76  	 * Templates icon image filename.
77  	 */
78  	public static final String TEMPLATES = "templates.gif"; //$NON-NLS-1$
79  
80  	/**
81  	 * Other icon image filename.
82  	 */
83  	public static final String OTHER_FILE = "other_file.gif"; //$NON-NLS-1$
84  
85  	/**
86  	 * Group icon image filename.
87  	 */
88  	public static final String GROUP = "outline/group.gif"; //$NON-NLS-1$
89  
90  	/**
91  	 * Node icon image filename.
92  	 */
93  	public static final String NODE = "outline/clock.gif"; //$NON-NLS-1$
94  
95  	/**
96  	 * Connection icon image filename.
97  	 */
98  	public static final String CONNECTION = "outline/connection.gif"; //$NON-NLS-1$
99  
100 	/**
101 	 * Data set icon image filename.
102 	 */
103 	public static final String DATA = "bill.gif"; //$NON-NLS-1$
104 
105 	// TODO replace the entity icon with default NodeIcon
106 	/**
107 	 * Entity large icon image filename.
108 	 */
109 	public static final String ENTITY_BIG = "people/entity.png"; //$NON-NLS-1$
110 
111 	/**
112 	 * Entity small icon image filename.
113 	 */
114 	public static final String ENTITY_SMALL = "people/entity_small.png"; //$NON-NLS-1$
115 
116 	/**
117 	 * Filter icon image filename.
118 	 */
119 	public static final String FILTER = "view/data/filter.gif"; //$NON-NLS-1$
120 
121 	/**
122 	 * Empty checkbox icon image filename.
123 	 */
124 	public static final String COMMON_CHECKBOX = "common/checkboxcleared.gif"; //$NON-NLS-1$
125 
126 	/**
127 	 * Selected checkbox icon image filename.
128 	 */
129 	public static final String COMMON_CHECKBOX_SELECTED = "common/checkboxselected.gif"; //$NON-NLS-1$
130 
131 	/**
132 	 * Fit zoom icon image filename.
133 	 */
134 	public static final String FIT_ZOOM = "editor/fit_zoom.gif"; //$NON-NLS-1$
135 
136 	/**
137 	 * Zoom out icon image filename.
138 	 */
139 	public static final String ZOOM_OUT = "editor/zoom_out.gif"; //$NON-NLS-1$
140 
141 	/**
142 	 * Returns single, shared instance of {@link Images}.
143 	 *
144 	 * @return single, shared instance of {@link Images}
145 	 */
146 	public static Images getInstance() {
147 		if (instance == null) {
148 			instance = new Images();
149 		}
150 		return instance;
151 	}
152 
153 	private Images() {
154 		log.debug("BASE_URL: " + baseURL); //$NON-NLS-1$
155 
156 		String[] imageIDs = new String[] { PROJECT, FILE, FOLDER, GROUP, NODE, CONNECTION, DATA, ENTITY_BIG,
157 		        ENTITY_SMALL, FILTER, TEMPLATE, TEMPLATES, OTHER_FILE, COMMON_CHECKBOX, COMMON_CHECKBOX_SELECTED,
158 		        FIT_ZOOM, ZOOM_OUT };
159 
160 		registry = new ImageRegistry(CastApplication.getDisplay());
161 
162 		for (String imageId : imageIDs) {
163 			ImageDescriptor descriptor = createImageDescriptor(ICON_PATH + imageId);
164 			registry.put(imageId, descriptor);
165 		}
166 	}
167 
168 	/**
169 	 * Returns {@link ImageDescriptor} with given Id.
170 	 *
171 	 * @param descriptorId
172 	 *            descriptor id
173 	 * @return {@link ImageDescriptor} with given Id
174 	 */
175 	public ImageDescriptor getDescriptor(String descriptorId) {
176 		return registry.getDescriptor(descriptorId);
177 	}
178 
179 	/**
180 	 * Returns {@link Image} for given descriptor Id.
181 	 *
182 	 * @param descriptorId
183 	 *            descriptor id
184 	 * @return {@link Image} for given descriptor Id
185 	 */
186 	public Image get(String descriptorId) {
187 		return registry.get(descriptorId);
188 	}
189 
190 	/**
191 	 * Puts image from given file into registry.
192 	 *
193 	 * @param descriptorId
194 	 *            image descriptor id
195 	 * @param path
196 	 *            path to image file
197 	 */
198 	public void put(String descriptorId, String path) {
199 		registry.put(descriptorId, createImageDescriptor(path));
200 	}
201 
202 	/**
203 	 * Puts {@link ImageDescriptor} into registry.
204 	 *
205 	 * @param descriptorId
206 	 *            image descriptor id
207 	 * @param descriptor
208 	 *            {@link ImageDescriptor}
209 	 */
210 	public void putDescriptor(String descriptorId, ImageDescriptor descriptor) {
211 		registry.put(descriptorId, descriptor);
212 	}
213 
214 	/**
215 	 * Utility method to create an <code>ImageDescriptor</code> from a path to a file.
216 	 */
217 	private ImageDescriptor createImageDescriptor(String path) {
218 		ImageDescriptor result = ImageDescriptor.createFromFile(CastApplication.class, path);
219 		return result;
220 	}
221 
222 	/**
223 	 * Gets a friendly display name for image with given Id.
224 	 *
225 	 * @param imageId
226 	 *            image Id
227 	 * @return image friendly name
228 	 *
229 	 * @deprecated To be removed. Use <code>NodeIconProvider.getFriendlyName()</code> instead.
230 	 */
231 	@Deprecated
232 	public String getFriendlyName(String imageId) {
233 		String propertyImageId = imageId.replaceAll("[\\.\\/\\\\]", "_"); //$NON-NLS-1$ //$NON-NLS-2$
234 		try {
235 			Field friendlyName = Messages.class.getField("Image_FriendlyName_" + propertyImageId); //$NON-NLS-1$
236 			return friendlyName != null ? (String)friendlyName.get(null) : imageId;
237 		} catch (Exception e) {
238 			return imageId;
239 		}
240 	}
241 
242 }