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