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