merged trunk into branch
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / ImageResources.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.model;
11
12 import java.net.MalformedURLException;
13 import java.net.URI;
14 import java.net.URL;
15
16 import org.eclipse.core.runtime.FileLocator;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jface.resource.DeviceResourceException;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.jface.resource.ImageRegistry;
22 import org.eclipse.swt.graphics.Image;
23
24 import eu.etaxonomy.taxeditor.store.StoreUtil;
25 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
26
27 /**
28 * <p>ImageResources class.</p>
29 *
30 * @author n.hoffmann
31 * @created 24.03.2009
32 * @version 1.0
33 */
34 public class ImageResources {
35
36 private static ImageResources instance;
37
38 /**
39 * <p>Getter for the field <code>instance</code>.</p>
40 *
41 * @return a {@link eu.etaxonomy.taxeditor.model.ImageResources} object.
42 */
43 public static ImageResources getInstance(){
44 if(instance == null){
45 instance = new ImageResources();
46 }
47 return instance;
48 }
49
50
51 /* ***************************************************************************************
52 ICONS
53 ************************************************************************************** */
54 /** Constant <code>ADD_ICON="add_icon"</code> */
55 public static final String ADD_ICON = "add_icon";
56 /** Constant <code>TRASH_ICON="trash_icon"</code> */
57 public static final String TRASH_ICON = "trash_icon";
58 /** Constant <code>BROWSE_ICON="browse_icon"</code> */
59 public static final String BROWSE_ICON = "browse_icon";
60 /** Constant <code>EDIT_ICON="edit_icon"</code> */
61 public static final String EDIT_ICON = "edit_icon";
62 /** Constant <code>EDIT_LOGO_ICON="edit_logo_icon"</code> */
63 public static final String EDIT_LOGO_ICON = "edit_logo_icon";
64 /** Constant <code>WARNING_ICON="warning_icon"</code> */
65 public static final String WARNING_ICON = "warning_icon";
66 /** Constant <code>BLACK_SQUARE_ICON="black_square_icon"</code> */
67 public static final String BLACK_SQUARE_ICON = "black_square_icon";
68 /** Constant <code>HOMOTYPIC_SYN_ICON="homotypic_syn_icon"</code> */
69 public static final String HOMOTYPIC_SYN_ICON = "homotypic_syn_icon";
70 /** Constant <code>HOMOTYPIC_SYN_ORIGINAL_ICON="homotypic_syn_original_icon"</code> */
71 public static final String HOMOTYPIC_SYN_ORIGINAL_ICON = "homotypic_syn_original_icon";
72 /** Constant <code>HETEROTYPIC_SYN_ICON="heterotypic_syn_icon"</code> */
73 public static final String HETEROTYPIC_SYN_ICON = "heterotypic_syn_icon";
74 public static final String NOM_INVAL_NUD_SYN_ICON = "nom_inval_nud_syn_icon";
75 public static final String HOMOTYPIC_SYNONYM_IN_HETEROTYPIC_GROUP_ICON = "homotypic_synonym_in_heterotypic_group_icon";
76 /** Constant <code>HETEROTYPIC_SYN_ORIGINAL_ICON="heterotypic_syn_original_icon"</code> */
77 public static final String HETEROTYPIC_SYN_ORIGINAL_ICON = "heterotypic_syn_original_icon";
78 /** Constant <code>MISAPPLIED_NAME_ICON="misapplied_icon"</code> */
79 public static final String MISAPPLIED_NAME_ICON = "misapplied_icon";
80 /** Constant <code>CONCEPT_ICON="concept_icon"</code> */
81 public static final String CONCEPT_ICON = "concept_icon";
82 /** Constant <code>AUTONYM_ICON="autonym_icon"</code> */
83 public static final String AUTONYM_ICON = "autonym_icon";
84 /** Constant <code>BASIONYM_ICON="basionym_icon"</code> */
85 public static final String BASIONYM_ICON = "basionym_icon";
86 /** Constant <code>ORTHOGRAPHIC_VARIANT_ICON="orthographic_variants_icon"</code> */
87 public static final String ORTHOGRAPHIC_VARIANT_ICON = "orthographic_variants_icon";
88 /** Constant <code>DB_ICON="db_icon"</code> */
89 public static final String DB_ICON = "db_icon";
90 /** Constant <code>MOVE_ICON="move_icon"</code> */
91 public static final String MOVE_ICON = "move_icon";
92 /** Constant <code>ACTIVE_DELETE_ICON="active_delete_icon"</code> */
93 public static final String ACTIVE_DELETE_ICON = "active_delete_icon";
94 /** Constant <code>SYNONYM_TO_TAXON_ICON="synoynm_to_taxon_icon"</code> */
95 public static final String SYNONYM_TO_TAXON_ICON = "synoynm_to_taxon_icon";
96 /** Constant <code>OPEN_TAXON_ICON="open_taxon_icon"</code> */
97 public static final String OPEN_TAXON_ICON = "open_taxon_icon";
98 /** Constant <code>ADD_CHILD_TAXON_ICON="add_taxon_icon"</code> */
99 public static final String ADD_CHILD_TAXON_ICON = "add_taxon_icon";
100 /** Constant <code>SWAP_SYNONYM_AND_TAXON_ICON="swap_syn_and_tax_icon"</code> */
101 public static final String SWAP_SYNONYM_AND_TAXON_ICON = "swap_syn_and_tax_icon";
102 /** Constant <code>QUICK_ADD_ICON="quick_add_icon"</code> */
103 public static final String QUICK_ADD_ICON = "quick_add_icon";
104 /** Constant <code>TAXON_TO_SYNONYM_ICON="tax_to_syn_icon"</code> */
105 public static final String TAXON_TO_SYNONYM_ICON = "tax_to_syn_icon";
106 /** Constant <code>ERROR_ANNOTATION_ICON="error_annotation_icon"</code> */
107 public static final String ERROR_ANNOTATION_ICON = "error_annotation_icon";
108 /** Constant <code>EDIT_BITMAP_ICON="edit_bitmap_icon"</code> */
109 public static final String EDIT_BITMAP_ICON = "edit_bitmap_icon";
110 /** Constant <code>IMG_DATASOURCE_CONNECTED="IMG_DATASOURCE_CONNECTED"</code> */
111 public static final String IMG_DATASOURCE_CONNECTED = "IMG_DATASOURCE_CONNECTED";
112 /** Constant <code>IMG_DATASOURCE_DISCONNECTED="IMG_DATASOURCE_DISCONNECTED"</code> */
113 public static final String IMG_DATASOURCE_DISCONNECTED = "IMG_DATASOURCE_DISCONNECTED";
114 /** Constant <code>IMG_NOT_FOUND="IMG_NOT_FOUND"</code> */
115 public static final String IMG_NOT_FOUND = "IMG_NOT_FOUND";
116 /** Constant <code>WARNING_ANNOTATION_ICON="warning"</code> */
117 public static final String WARNING_ANNOTATION_ICON = "warning";
118 /** Constant <code>THREE_PRONGED_EQUAL="3_pronged_equal"</code> */
119 public static final String THREE_PRONGED_EQUAL = "3_pronged_equal";
120 /** Constant <code>TWO_PRONGED_EQUAL="2_pronged_equal"</code> */
121 public static final String TWO_PRONGED_EQUAL = "2_pronged_equal";
122 /** Constant <code>MISAPPLIED_NAME="misapplied_name"</code> */
123 public static final String MISAPPLIED_NAME = "misapplied_name";
124 /** Constant <code>ORPHANED_TAXON="orphaned_taxon"</code> */
125 public static final String ORPHANED_TAXON = "orphaned_taxon";
126 /** Constant <code>EXPAND_ALL="expand_all"</code> */
127 public static final String EXPAND_ALL = "expand_all";
128 /** Constant <code>COLLAPSE_ALL="collapse_all"</code> */
129 public static final String COLLAPSE_ALL = "collapse_all";
130
131
132
133 /***************************************************************************
134 * IMAGE REGISTRY
135 **************************************************************************/
136 private ImageRegistry imageRegistry;
137
138 /**
139 * <p>getImageDescriptor</p>
140 *
141 * @param key a {@link java.lang.String} object.
142 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
143 */
144 public static ImageDescriptor getImageDescriptor(String key) {
145 return getInstance().getImageRegistry().getDescriptor(key);
146 }
147
148 /**
149 * <p>getImage</p>
150 *
151 * @param key a {@link java.lang.String} object.
152 * @return a {@link org.eclipse.swt.graphics.Image} object.
153 */
154 public static Image getImage(String key) {
155 return getInstance().getImageRegistry().get(key);
156 }
157
158 /**
159 * Gets an image for the given URL. Adds the image to the registry as needed.
160 *
161 * @param uri a {@link java.net.URI} object.
162 * @return a {@link org.eclipse.swt.graphics.Image} object.
163 */
164 public static Image getImage(URI uri) {
165
166 String key = uri.toString();
167 ImageRegistry registry = getInstance().getImageRegistry();
168
169 Image image = registry.get(key);
170
171 if (image == null) {
172 ImageDescriptor desc;
173 try {
174 desc = ImageDescriptor.createFromURL(new URL(key));
175 registry.put(key, desc);
176 image = registry.get(key);
177 } catch (DeviceResourceException e) {
178 return getImageNotFound();
179 } catch (MalformedURLException e) {
180 // should never happen
181 } catch (Exception e) {
182 StoreUtil.error(ImageResources.class, e);
183 }
184
185 // logger.warn(ImageDescriptor.getMissingImageDescriptor().getClass());
186 // logger.warn(desc.getClass());
187 //
188 // if (ImageDescriptor.getMissingImageDescriptor().equals(desc)) {
189 // logger.warn("img not found");
190 // }
191
192 }
193
194 return image;
195 }
196
197 /**
198 * <p>getImageNotFound</p>
199 *
200 * @return a {@link org.eclipse.swt.graphics.Image} object.
201 */
202 public static Image getImageNotFound() {
203 return getImage(ImageResources.IMG_NOT_FOUND);
204 }
205
206 /**
207 * <p>Getter for the field <code>imageRegistry</code>.</p>
208 *
209 * @return a {@link org.eclipse.jface.resource.ImageRegistry} object.
210 */
211 public ImageRegistry getImageRegistry() {
212 if (imageRegistry == null) {
213 imageRegistry = TaxeditorStorePlugin.getDefault().createImageRegistry();
214 initializeImageRegistry(imageRegistry);
215 }
216 return imageRegistry;
217 }
218
219 /**
220 * <p>initializeImageRegistry</p>
221 *
222 * @param registry a {@link org.eclipse.jface.resource.ImageRegistry} object.
223 */
224 protected void initializeImageRegistry(ImageRegistry registry) {
225 registerImage(registry, ADD_ICON,
226 "add_no_bg.gif");
227 registerImage(registry, TRASH_ICON,
228 "trash.gif");
229 registerImage(registry, EDIT_ICON,
230 "text.gif");
231 registerImage(registry, EDIT_LOGO_ICON,
232 "edit_16x16.ico");
233 registerImage(registry, WARNING_ICON,
234 "warn_tsk.gif");
235 registerImage(registry, BLACK_SQUARE_ICON,
236 "accepted_small.gif");
237 registerImage(registry, HOMOTYPIC_SYN_ICON,
238 "homosyn_no_bg.gif");
239 registerImage(registry, HOMOTYPIC_SYN_ORIGINAL_ICON,
240 "homosyn_original_no_bg.gif");
241 registerImage(registry, HETEROTYPIC_SYN_ICON,
242 "heterosyn_no_bg.gif");
243 registerImage(registry, NOM_INVAL_NUD_SYN_ICON,
244 "nominval_nud_no_bg.gif");
245 registerImage(registry, HETEROTYPIC_SYN_ORIGINAL_ICON,
246 "heterosyn_original_no_bg.gif");
247 registerImage(registry, HOMOTYPIC_SYNONYM_IN_HETEROTYPIC_GROUP_ICON,
248 "homotypic_syn_hetero_group.gif");
249 registerImage(registry, MISAPPLIED_NAME_ICON,
250 "misapplied_no_bg.gif");
251 registerImage(registry, CONCEPT_ICON,
252 "concept_no_bg.gif");
253 registerImage(registry, AUTONYM_ICON,
254 "autonym_no_bg.gif");
255 registerImage(registry, BASIONYM_ICON,
256 "basionym_no_bg.gif");
257 registerImage(registry, ORTHOGRAPHIC_VARIANT_ICON,
258 "orthovariant_no_bg.gif");
259 registerImage(registry, DB_ICON,
260 "db.gif");
261 registerImage(registry, MOVE_ICON,
262 "correction_change.gif");
263 registerImage(registry, ACTIVE_DELETE_ICON,
264 "delete_edit.gif");
265 registerImage(registry, SYNONYM_TO_TAXON_ICON,
266 "change.gif");
267 registerImage(registry, OPEN_TAXON_ICON,
268 "open.gif");
269 registerImage(registry, ADD_CHILD_TAXON_ICON,
270 "new_child.gif");
271 registerImage(registry,
272 SWAP_SYNONYM_AND_TAXON_ICON, "swap2.gif");
273 registerImage(registry, QUICK_ADD_ICON,
274 "quick_add.gif");
275 registerImage(registry, TAXON_TO_SYNONYM_ICON,
276 "tax_to_syn.gif");
277 registerImage(registry, ERROR_ANNOTATION_ICON,
278 "error_co.gif");
279 registerImage(registry, EDIT_BITMAP_ICON,
280 "256color_16x16.bmp");
281 registerImage(registry, IMG_DATASOURCE_CONNECTED,
282 "prj_obj.gif");
283 registerImage(registry, IMG_DATASOURCE_DISCONNECTED,
284 "cprj_obj.gif");
285 registerImage(registry, IMG_NOT_FOUND,
286 "pic_not_found.PNG");
287 registerImage(registry, WARNING_ANNOTATION_ICON,
288 "warning_co.gif");
289 registerImage(registry, THREE_PRONGED_EQUAL,
290 "3_pronged_equal_small.GIF");
291 registerImage(registry, TWO_PRONGED_EQUAL,
292 "2_pronged_equal_small.GIF");
293 registerImage(registry, MISAPPLIED_NAME,
294 "ma_12x12.gif");
295 registerImage(registry, ORPHANED_TAXON,
296 "orphaned.gif");
297 registerImage(registry, EXPAND_ALL,
298 "expandAll.jpg");
299 registerImage(registry, COLLAPSE_ALL,
300 "collapseall.gif");
301 registerImage(registry, BROWSE_ICON,
302 "prj_obj.gif");
303 }
304
305 private void registerImage(ImageRegistry registry, String key,
306 String fileName) {
307 try {
308 IPath path = new Path("icons/" + fileName); //$NON-NLS-1$
309 URL url = FileLocator.find(TaxeditorStorePlugin.getDefault().getBundle(), path, null);
310 if (url != null) {
311 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
312 registry.put(key, desc);
313 }
314 } catch (Exception e) {
315 }
316 }
317 }