misc 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 import org.eclipse.jface.action.IAction;
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.resource.ImageRegistry;
10 import org.eclipse.swt.graphics.Image;
11
12 public class PHPUiImages {
13
14         protected static final String NAME_PREFIX = "net.sourceforge.phpdt.internal.ui.";
15         protected static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();
16   
17         protected static URL iconBaseURL;
18
19         static {
20                 String pathSuffix = "icons/";
21                 try {
22                         iconBaseURL = new URL(PHPeclipsePlugin.getDefault().getDescriptor().getInstallURL(), pathSuffix);
23                 } catch (MalformedURLException e) {
24                         PHPeclipsePlugin.log(e);
25                 }
26         }
27
28         protected static final ImageRegistry IMAGE_REGISTRY = new ImageRegistry();
29
30         protected static final String OBJ_PREFIX = "obj16";
31         protected static final String OVR_PREFIX = "ovr16";
32         protected static final String CTOOL_PREFIX = "ctool16";
33
34   public static final String IMG_CLASS = NAME_PREFIX + "class_obj.gif";
35   public static final String IMG_BUILTIN = NAME_PREFIX + "builtin_obj.gif";
36   public static final String IMG_FUN = NAME_PREFIX + "fun_obj.gif";
37   public static final String IMG_VAR = NAME_PREFIX + "var_obj.gif";
38         public static final String IMG_OBJS_ERROR = NAME_PREFIX + "error_obj.gif";
39         public static final String IMG_OBJS_WARNING = NAME_PREFIX + "warning_obj.gif";
40         public static final String IMG_OBJS_INFO = NAME_PREFIX + "info_obj.gif";
41         public static final String IMG_CTOOLS_PHP_PAGE = NAME_PREFIX + "php_page.gif";
42         public static final String IMG_CTOOLS_PHP = NAME_PREFIX + "php.gif";
43   
44   public static final String IMG_OBJS_TEMPLATE= NAME_PREFIX + "template_obj.gif";   
45
46   public static final ImageDescriptor DESC_CLASS = createManaged(OBJ_PREFIX, IMG_CLASS);
47   public static final ImageDescriptor DESC_BUILTIN = createManaged(OBJ_PREFIX, IMG_BUILTIN);
48   public static final ImageDescriptor DESC_FUN = createManaged(OBJ_PREFIX, IMG_FUN);
49         public static final ImageDescriptor DESC_VAR = createManaged(OBJ_PREFIX, IMG_VAR);
50   public static final ImageDescriptor DESC_OBJS_ERROR = createManaged(OBJ_PREFIX, IMG_OBJS_ERROR);
51         public static final ImageDescriptor DESC_OBJS_WARNING = createManaged(OBJ_PREFIX, IMG_OBJS_WARNING);
52         public static final ImageDescriptor DESC_OBJS_INFO = createManaged(OBJ_PREFIX, IMG_OBJS_INFO);
53         public static final ImageDescriptor DESC_CTOOL_PHP_PAGE = createManaged(CTOOL_PREFIX, IMG_CTOOLS_PHP_PAGE);
54         public static final ImageDescriptor DESC_CTOOL_PHP = createManaged(CTOOL_PREFIX, IMG_CTOOLS_PHP);
55
56   public static final ImageDescriptor DESC_OBJS_TEMPLATE= createManaged(OBJ_PREFIX, IMG_OBJS_TEMPLATE);
57
58         /**
59          * Returns the image managed under the given key in this registry.
60          * 
61          * @param key the image's key
62          * @return the image managed under the given key
63          */
64         public static Image get(String key) {
65                 return IMAGE_REGISTRY.get(key);
66         }
67
68         /**
69          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
70          * are retrieved from the *tool16 folders.
71          */
72         public static void setToolImageDescriptors(IAction action, String iconName) {
73                 setImageDescriptors(action, "tool16", iconName);
74         }
75
76         /**
77          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
78          * are retrieved from the *lcl16 folders.
79          */
80         public static void setLocalImageDescriptors(IAction action, String iconName) {
81                 setImageDescriptors(action, "lcl16", iconName);
82         }
83
84         public static ImageRegistry getImageRegistry() {
85                 return IMAGE_REGISTRY;
86         }
87
88         //---- Helper methods to access icons on the file system --------------------------------------
89
90         protected static void setImageDescriptors(IAction action, String type, String relPath) {
91
92                 try {
93                         ImageDescriptor id = ImageDescriptor.createFromURL(makeIconFileURL("d" + type, relPath));
94                         if (id != null)
95                                 action.setDisabledImageDescriptor(id);
96                 } catch (MalformedURLException e) {}
97
98                 try {
99                         ImageDescriptor id = ImageDescriptor.createFromURL(makeIconFileURL("c" + type, relPath));
100                         if (id != null)
101                                 action.setHoverImageDescriptor(id);
102                 } catch (MalformedURLException e) {}
103
104                 action.setImageDescriptor(create("e" + type, relPath));
105         }
106
107         protected static ImageDescriptor createManaged(String prefix, String name) {
108                 try {
109                         ImageDescriptor result = ImageDescriptor.createFromURL(makeIconFileURL(prefix, name.substring(NAME_PREFIX_LENGTH)));
110                         IMAGE_REGISTRY.put(name, result);
111                         return result;
112                 } catch (MalformedURLException e) {
113                         return ImageDescriptor.getMissingImageDescriptor();
114                 }
115         }
116
117         protected static ImageDescriptor create(String prefix, String name) {
118                 try {
119                         return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
120                 } catch (MalformedURLException e) {
121                         return ImageDescriptor.getMissingImageDescriptor();
122                 }
123         }
124
125         protected static URL makeIconFileURL(String prefix, String name) throws MalformedURLException {
126                 if (iconBaseURL == null)
127                         throw new MalformedURLException();
128
129                 StringBuffer buffer = new StringBuffer(prefix);
130                 buffer.append('/');
131                 buffer.append(name);
132                 return new URL(iconBaseURL, buffer.toString());
133         }
134 }