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