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