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