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