intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.ui / src / net / sourceforge / phpeclipse / js / ui / editors / JSImages.java
1 /*
2  * Copyright (c) 2002-2004 Adrian Dinu and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Adrian Dinu - initial implementation
10  *     Alex Fitzpatrick - additional images
11  *     Christopher Lenz - migration to use the plugin's image registry
12  * 
13  * $Id: JSImages.java,v 1.1 2004-09-02 18:23:49 jsurfer Exp $
14  */
15
16 package net.sourceforge.phpeclipse.js.ui.editors;
17
18 import java.net.URL;
19
20 import net.sourceforge.phpeclipse.js.ui.JSUIPlugin;
21
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.resource.ImageRegistry;
24 import org.eclipse.swt.graphics.Image;
25
26 /**
27  * Convenience class for storing references to image descriptors used by the JS
28  * editor.
29  */
30 public class JSImages {
31
32         public static final String ICON_VAR =
33                 "global_variable.gif"; //$NON-NLS-1$
34         public static final String ICON_FUNCTION =
35                 "func.gif"; //$NON-NLS-1$
36         public static final String ICON_CLASS =
37                 "class_obj.gif"; //$NON-NLS-1$
38         public static final String ICON_DYNAMIC_CLASS =
39                 "dyn_class_obj.gif"; //$NON-NLS-1$
40         public static final String ICON_CLASS_METHOD =
41                 "class_method.gif"; //$NON-NLS-1$
42         public static final String ICON_INSTANCE_METHOD =
43                 "instance_method.gif"; //$NON-NLS-1$
44         public static final String ICON_CLASS_VAR =
45                 "class_variable.gif"; //$NON-NLS-1$
46         public static final String ICON_INSTANCE_VAR =
47                 "instance_variable.gif"; //$NON-NLS-1$
48
49         /**
50          * Returns the image for the specified key, or <tt>null</tt> if no image for
51          * that key is found.
52          * 
53          * @param key the key under which the image was registered
54          * @return the image, or <tt>null</tt> if none
55          */
56         public static Image get(String key) {
57                 return JSUIPlugin.getDefault().getImageRegistry().get(key);
58         }
59
60         /**
61          * Returns the image descriptor for the specified key, or <tt>null</tt> if
62          * no image for that key is found.
63          * 
64          * @param key the key under which the image was registered
65          * @return the image descriptor, or <tt>null</tt> if none
66          */
67         public static ImageDescriptor getDescriptor(String key) {
68                 return JSUIPlugin.getDefault().getImageRegistry().getDescriptor(key);
69         }
70
71         /**
72          * Initializes the given image registry with all images provided through
73          * this class.
74          * 
75          * @param reg the registry to initialize
76          */
77         public static void initializeRegistry(ImageRegistry reg) {
78                 reg.put(ICON_VAR, createImageDescriptor(ICON_VAR));
79                 reg.put(ICON_FUNCTION, createImageDescriptor(ICON_FUNCTION));
80                 reg.put(ICON_CLASS, createImageDescriptor(ICON_CLASS));
81                 reg.put(ICON_DYNAMIC_CLASS,     createImageDescriptor(ICON_DYNAMIC_CLASS));
82                 reg.put(ICON_CLASS_METHOD, createImageDescriptor(ICON_CLASS_METHOD));
83                 reg.put(ICON_INSTANCE_METHOD,
84                                 createImageDescriptor(ICON_INSTANCE_METHOD));
85                 reg.put(ICON_CLASS_VAR, createImageDescriptor(ICON_CLASS_VAR));
86                 reg.put(ICON_INSTANCE_VAR, createImageDescriptor(ICON_INSTANCE_VAR));
87         }
88
89         /**
90          * Utility method to create an <code>ImageDescriptor</code> from a path to a
91          * file.
92          * 
93          * @param path the full path to the image file
94          * @return the image descriptor
95          */
96         private static ImageDescriptor createImageDescriptor(String path) {
97                 try {
98                         URL url = JSUIPlugin.getDefault().getBundle()
99                                 .getEntry("/icons/" + path); //$NON-NLS-1$
100                         return ImageDescriptor.createFromURL(url);
101                 } catch (IllegalStateException e) {
102                         return ImageDescriptor.getMissingImageDescriptor();
103                 }
104         }
105 }