651e9c0c2fa6003dd269e28a8dd254801e0dfacb
[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_CLASS = NAME_PREFIX + "class_obj.gif";
43         public static final String IMG_DEFINE = NAME_PREFIX + "define_obj.gif";
44         public static final String IMG_BUILTIN = NAME_PREFIX + "builtin_obj.gif";
45         public static final String IMG_FUN = NAME_PREFIX + "fun_obj.gif";
46         public static final String IMG_INC = NAME_PREFIX + "impc_obj.gif";
47         public static final String IMG_VAR = NAME_PREFIX + "var_obj.gif";
48         public static final String IMG_OBJS_ERROR = NAME_PREFIX + "error_obj.gif";
49         public static final String IMG_OBJS_WARNING =
50                 NAME_PREFIX + "warning_obj.gif";
51         public static final String IMG_OBJS_INFO = NAME_PREFIX + "info_obj.gif";
52         public static final String IMG_CTOOLS_PHP_PAGE =
53                 NAME_PREFIX + "php_page.gif";
54         public static final String IMG_CTOOLS_PHP = NAME_PREFIX + "php.gif";
55
56         public static final String IMG_OBJS_TEMPLATE =
57                 NAME_PREFIX + "template_obj.gif";
58
59         public static final String IMG_CLEAR = NAME_PREFIX + "clear.gif"; 
60         
61         public static final ImageDescriptor DESC_CLASS =
62                 createManaged(OBJ_PREFIX, IMG_CLASS);
63         public static final ImageDescriptor DESC_DEFINE =
64                 createManaged(OBJ_PREFIX, IMG_DEFINE);
65         public static final ImageDescriptor DESC_BUILTIN =
66                 createManaged(OBJ_PREFIX, IMG_BUILTIN);
67         public static final ImageDescriptor DESC_FUN =
68                 createManaged(OBJ_PREFIX, IMG_FUN);
69         public static final ImageDescriptor DESC_INC =
70                 createManaged(OBJ_PREFIX, IMG_INC);
71         public static final ImageDescriptor DESC_VAR =
72                 createManaged(OBJ_PREFIX, IMG_VAR);
73         public static final ImageDescriptor DESC_OBJS_ERROR =
74                 createManaged(OBJ_PREFIX, IMG_OBJS_ERROR);
75         public static final ImageDescriptor DESC_OBJS_WARNING =
76                 createManaged(OBJ_PREFIX, IMG_OBJS_WARNING);
77         public static final ImageDescriptor DESC_OBJS_INFO =
78                 createManaged(OBJ_PREFIX, IMG_OBJS_INFO);
79         public static final ImageDescriptor DESC_CTOOL_PHP_PAGE =
80                 createManaged(CTOOL_PREFIX, IMG_CTOOLS_PHP_PAGE);
81         public static final ImageDescriptor DESC_CTOOL_PHP =
82                 createManaged(CTOOL_PREFIX, IMG_CTOOLS_PHP);
83
84         public static final ImageDescriptor DESC_OBJS_TEMPLATE =
85                 createManaged(OBJ_PREFIX, IMG_OBJS_TEMPLATE);
86                 
87         public static final ImageDescriptor DESC_CLEAR =
88                         createManaged(OBJ_PREFIX, IMG_CLEAR);
89
90         /**
91          * Returns the image managed under the given key in this registry.
92          * 
93          * @param key the image's key
94          * @return the image managed under the given key
95          */
96         public static Image get(String key) {
97                 return IMAGE_REGISTRY.get(key);
98         }
99
100         /**
101          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
102          * are retrieved from the *tool16 folders.
103          */
104         public static void setToolImageDescriptors(
105                 IAction action,
106                 String iconName) {
107                 setImageDescriptors(action, "tool16", iconName);
108         }
109
110         /**
111          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
112          * are retrieved from the *lcl16 folders.
113          */
114         public static void setLocalImageDescriptors(
115                 IAction action,
116                 String iconName) {
117                 setImageDescriptors(action, "lcl16", iconName);
118         }
119
120         public static ImageRegistry getImageRegistry() {
121                 return IMAGE_REGISTRY;
122         }
123
124         //---- Helper methods to access icons on the file system --------------------------------------
125
126         protected static void setImageDescriptors(
127                 IAction action,
128                 String type,
129                 String relPath) {
130
131                 try {
132                         ImageDescriptor id =
133                                 ImageDescriptor.createFromURL(
134                                         makeIconFileURL("d" + type, relPath));
135                         if (id != null)
136                                 action.setDisabledImageDescriptor(id);
137                 } catch (MalformedURLException e) {
138                 }
139
140                 try {
141                         ImageDescriptor id =
142                                 ImageDescriptor.createFromURL(
143                                         makeIconFileURL("c" + type, relPath));
144                         if (id != null)
145                                 action.setHoverImageDescriptor(id);
146                 } catch (MalformedURLException e) {
147                 }
148
149                 action.setImageDescriptor(create("e" + type, relPath));
150         }
151
152         protected static ImageDescriptor createManaged(
153                 String prefix,
154                 String name) {
155                 try {
156                         ImageDescriptor result =
157                                 ImageDescriptor.createFromURL(
158                                         makeIconFileURL(
159                                                 prefix,
160                                                 name.substring(NAME_PREFIX_LENGTH)));
161                         IMAGE_REGISTRY.put(name, result);
162                         return result;
163                 } catch (MalformedURLException e) {
164                         return ImageDescriptor.getMissingImageDescriptor();
165                 }
166         }
167
168         protected static ImageDescriptor create(String prefix, String name) {
169                 try {
170                         return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
171                 } catch (MalformedURLException e) {
172                         return ImageDescriptor.getMissingImageDescriptor();
173                 }
174         }
175
176         protected static URL makeIconFileURL(String prefix, String name)
177                 throws MalformedURLException {
178                 if (iconBaseURL == null)
179                         throw new MalformedURLException();
180
181                 StringBuffer buffer = new StringBuffer(prefix);
182                 buffer.append('/');
183                 buffer.append(name);
184                 return new URL(iconBaseURL, buffer.toString());
185         }
186 }