Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / PHPUiImages.java
1 package net.sourceforge.phpdt.internal.ui;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
7
8 import org.eclipse.jface.action.IAction;
9 import org.eclipse.jface.resource.ImageDescriptor;
10 import org.eclipse.jface.resource.ImageRegistry;
11 import org.eclipse.swt.graphics.Image;
12
13 public class PHPUiImages {
14
15         protected static final String NAME_PREFIX =
16                 "net.sourceforge.phpdt.internal.ui.";
17         protected static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();
18
19         protected static URL iconBaseURL;
20
21         static {
22                 String pathSuffix = "icons/";
23                 try {
24                         iconBaseURL =
25                                 new URL(
26                                         PHPeclipsePlugin
27                                                 .getDefault()
28                                                 .getDescriptor()
29                                                 .getInstallURL(),
30                                         pathSuffix);
31                 } catch (MalformedURLException e) {
32                         PHPeclipsePlugin.log(e);
33                 }
34         }
35
36         protected static final ImageRegistry IMAGE_REGISTRY = new ImageRegistry();
37
38         protected static final String OBJ_PREFIX = "obj16";
39         protected static final String OVR_PREFIX = "ovr16";
40         protected static final String CTOOL_PREFIX = "ctool16";
41
42         public static final String IMG_OBJS_GHOST= NAME_PREFIX + "ghost.gif";   
43         public static final String IMG_CLASS = NAME_PREFIX + "class_obj.gif";
44         public static final String IMG_DEFINE = NAME_PREFIX + "define_obj.gif";
45         public static final String IMG_BUILTIN = NAME_PREFIX + "builtin_obj.gif";
46         public static final String IMG_FUN = NAME_PREFIX + "fun_obj.gif";
47         public static final String IMG_INC = NAME_PREFIX + "impc_obj.gif";
48         public static final String IMG_VAR = NAME_PREFIX + "var_obj.gif";
49         public static final String IMG_OBJS_ERROR = NAME_PREFIX + "error_obj.gif";
50         public static final String IMG_OBJS_WARNING =
51                 NAME_PREFIX + "warning_obj.gif";
52         public static final String IMG_OBJS_INFO = NAME_PREFIX + "info_obj.gif";
53         public static final String IMG_OBJS_INTERFACE= NAME_PREFIX + "int_obj.gif";                     //$NON-NLS-1$
54         public static final String IMG_CTOOLS_PHP_PAGE =
55                 NAME_PREFIX + "php_page.gif";
56         public static final String IMG_CTOOLS_PHP = NAME_PREFIX + "php.gif";
57
58         public static final String IMG_OBJS_TEMPLATE =
59                 NAME_PREFIX + "template_obj.gif";
60
61         public static final String IMG_CLEAR = NAME_PREFIX + "clear.gif"; 
62         
63         public static final ImageDescriptor DESC_OBJS_GHOST= createManaged(OBJ_PREFIX, IMG_OBJS_GHOST);
64         
65         public static final ImageDescriptor DESC_CLASS =
66                 createManaged(OBJ_PREFIX, IMG_CLASS);
67         public static final ImageDescriptor DESC_DEFINE =
68                 createManaged(OBJ_PREFIX, IMG_DEFINE);
69         public static final ImageDescriptor DESC_BUILTIN =
70                 createManaged(OBJ_PREFIX, IMG_BUILTIN);
71         public static final ImageDescriptor DESC_FUN =
72                 createManaged(OBJ_PREFIX, IMG_FUN);
73         public static final ImageDescriptor DESC_INC =
74                 createManaged(OBJ_PREFIX, IMG_INC);
75         public static final ImageDescriptor DESC_VAR =
76                 createManaged(OBJ_PREFIX, IMG_VAR);
77         public static final ImageDescriptor DESC_OBJS_ERROR =
78                 createManaged(OBJ_PREFIX, IMG_OBJS_ERROR);
79         public static final ImageDescriptor DESC_OBJS_WARNING =
80                 createManaged(OBJ_PREFIX, IMG_OBJS_WARNING);
81         public static final ImageDescriptor DESC_OBJS_INFO =
82                 createManaged(OBJ_PREFIX, IMG_OBJS_INFO);
83         public static final ImageDescriptor DESC_CTOOL_PHP_PAGE =
84                 createManaged(CTOOL_PREFIX, IMG_CTOOLS_PHP_PAGE);
85         public static final ImageDescriptor DESC_CTOOL_PHP =
86                 createManaged(CTOOL_PREFIX, IMG_CTOOLS_PHP);
87
88         public static final ImageDescriptor DESC_OBJS_TEMPLATE =
89                 createManaged(OBJ_PREFIX, IMG_OBJS_TEMPLATE);
90                 
91         public static final ImageDescriptor DESC_CLEAR =
92                         createManaged(OBJ_PREFIX, IMG_CLEAR);
93         
94         private static final String T_OBJ= "obj16";             //$NON-NLS-1$
95         private static final String T_WIZBAN= "wizban";         //$NON-NLS-1$
96         
97         public static final ImageDescriptor DESC_WIZBAN_NEWCLASS= create(T_WIZBAN, "newclass_wiz.gif");                         //$NON-NLS-1$
98         public static final ImageDescriptor DESC_OBJS_INTERFACE= createManaged(T_OBJ, IMG_OBJS_INTERFACE);
99         
100         /**
101          * Returns the image managed under the given key in this registry.
102          * 
103          * @param key the image's key
104          * @return the image managed under the given key
105          */
106         public static Image get(String key) {
107                 return IMAGE_REGISTRY.get(key);
108         }
109
110         /**
111          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
112          * are retrieved from the *tool16 folders.
113          */
114         public static void setToolImageDescriptors(
115                 IAction action,
116                 String iconName) {
117                 setImageDescriptors(action, "tool16", iconName);
118         }
119
120         /**
121          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
122          * are retrieved from the *lcl16 folders.
123          */
124         public static void setLocalImageDescriptors(
125                 IAction action,
126                 String iconName) {
127                 setImageDescriptors(action, "lcl16", iconName);
128         }
129
130         public static ImageRegistry getImageRegistry() {
131                 return IMAGE_REGISTRY;
132         }
133
134         //---- Helper methods to access icons on the file system --------------------------------------
135
136         protected static void setImageDescriptors(
137                 IAction action,
138                 String type,
139                 String relPath) {
140
141                 try {
142                         ImageDescriptor id =
143                                 ImageDescriptor.createFromURL(
144                                         makeIconFileURL("d" + type, relPath));
145                         if (id != null)
146                                 action.setDisabledImageDescriptor(id);
147                 } catch (MalformedURLException e) {
148                 }
149
150                 try {
151                         ImageDescriptor id =
152                                 ImageDescriptor.createFromURL(
153                                         makeIconFileURL("c" + type, relPath));
154                         if (id != null)
155                                 action.setHoverImageDescriptor(id);
156                 } catch (MalformedURLException e) {
157                 }
158
159                 action.setImageDescriptor(create("e" + type, relPath));
160         }
161
162         protected static ImageDescriptor createManaged(
163                 String prefix,
164                 String name) {
165                 try {
166                         ImageDescriptor result =
167                                 ImageDescriptor.createFromURL(
168                                         makeIconFileURL(
169                                                 prefix,
170                                                 name.substring(NAME_PREFIX_LENGTH)));
171                         IMAGE_REGISTRY.put(name, result);
172                         return result;
173                 } catch (MalformedURLException e) {
174                         return ImageDescriptor.getMissingImageDescriptor();
175                 }
176         }
177
178         protected static ImageDescriptor create(String prefix, String name) {
179                 try {
180                         return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
181                 } catch (MalformedURLException e) {
182                         return ImageDescriptor.getMissingImageDescriptor();
183                 }
184         }
185
186         protected static URL makeIconFileURL(String prefix, String name)
187                 throws MalformedURLException {
188                 if (iconBaseURL == null)
189                         throw new MalformedURLException();
190
191                 StringBuffer buffer = new StringBuffer(prefix);
192                 buffer.append('/');
193                 buffer.append(name);
194                 return new URL(iconBaseURL, buffer.toString());
195         }
196 }