Some simple improvements
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / debug / ui / PHPDebugUiImages.java
1 package net.sourceforge.phpdt.debug.ui;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
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 PHPDebugUiImages {
14
15         protected static final String NAME_PREFIX = "net.sourceforge.phpdt.debug.ui.";
16         protected static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();
17         protected static URL iconBaseURL;
18
19         
20         static {
21                 iconBaseURL= PHPDebugUiPlugin.getDefault().getBundle().getEntry("/icons/"); //$NON-NLS-1$
22         }
23 /*      
24         static {
25                 String pathSuffix = "icons/";
26                 try {
27                         iconBaseURL = new URL(PHPDebugUiPlugin.getDefault().getDescriptor().getInstallURL(), pathSuffix);
28                 } catch (MalformedURLException e) {
29                         PHPDebugUiPlugin.log(e);
30                 }
31         }
32 */
33         protected static final ImageRegistry IMAGE_REGISTRY = new ImageRegistry();
34
35         protected static final String CTOOL_PREFIX = "ctool16";
36         protected static final String EVIEW_PREFIX = "eview16";
37
38         public static final String IMG_EVIEW_ARGUMENTS_TAB = NAME_PREFIX + "arguments_tab.gif";
39
40         public static final ImageDescriptor DESC_EVIEW_ARGUMENTS_TAB = createManaged(EVIEW_PREFIX, IMG_EVIEW_ARGUMENTS_TAB);
41
42         /**
43          * Returns the image managed under the given key in this registry.
44          * 
45          * @param key the image's key
46          * @return the image managed under the given key
47          */
48         public static Image get(String key) {
49                 return IMAGE_REGISTRY.get(key);
50         }
51
52         /**
53          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
54          * are retrieved from the *tool16 folders.
55          */
56         public static void setToolImageDescriptors(IAction action, String iconName) {
57                 setImageDescriptors(action, "tool16", iconName);
58         }
59
60         /**
61          * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
62          * are retrieved from the *lcl16 folders.
63          */
64         public static void setLocalImageDescriptors(IAction action, String iconName) {
65                 setImageDescriptors(action, "lcl16", iconName);
66         }
67
68         public static ImageRegistry getImageRegistry() {
69                 return IMAGE_REGISTRY;
70         }
71
72         //---- Helper methods to access icons on the file system --------------------------------------
73
74         protected static void setImageDescriptors(IAction action, String type, String relPath) {
75
76                 try {
77                         ImageDescriptor id = ImageDescriptor.createFromURL(makeIconFileURL("d" + type, relPath));
78                         if (id != null)
79                                 action.setDisabledImageDescriptor(id);
80                 } catch (MalformedURLException e) {}
81
82                 try {
83                         ImageDescriptor id = ImageDescriptor.createFromURL(makeIconFileURL("c" + type, relPath));
84                         if (id != null)
85                                 action.setHoverImageDescriptor(id);
86                 } catch (MalformedURLException e) {}
87
88                 action.setImageDescriptor(create("e" + type, relPath));
89         }
90
91         protected static ImageDescriptor createManaged(String prefix, String name) {
92                 try {
93                         ImageDescriptor result = ImageDescriptor.createFromURL(makeIconFileURL(prefix, name.substring(NAME_PREFIX_LENGTH)));
94                         IMAGE_REGISTRY.put(name, result);
95                         return result;
96                 } catch (MalformedURLException e) {
97                         return ImageDescriptor.getMissingImageDescriptor();
98                 }
99         }
100
101         protected static ImageDescriptor create(String prefix, String name) {
102                 try {
103                         return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
104                 } catch (MalformedURLException e) {
105                         return ImageDescriptor.getMissingImageDescriptor();
106                 }
107         }
108
109         protected static URL makeIconFileURL(String prefix, String name) throws MalformedURLException {
110                 if (iconBaseURL == null)
111                         throw new MalformedURLException();
112
113                 StringBuffer buffer = new StringBuffer(prefix);
114                 buffer.append('/');
115                 buffer.append(name);
116                 return new URL(iconBaseURL, buffer.toString());
117         }
118 }