Change Eclipse-LazyStart: true to Bundle-ActivationPolicy: lazy (upgrade to The Equin...
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / internal / ImageResource.java
1 /**
2  * Copyright (c) 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser.internal;
12
13 import java.net.URL;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.resource.ImageRegistry;
19 import org.eclipse.swt.graphics.Image;
20
21 /**
22  * Utility class to handle image resources.
23  */
24 public class ImageResource {
25         // the image registry
26         private static ImageRegistry imageRegistry;
27
28         // map of image descriptors since these
29         // will be lost by the image registry
30         private static Map imageDescriptors;
31
32         // base urls for images
33         private static URL ICON_BASE_URL;
34
35         static {
36                 try {
37                         String pathSuffix = "icons/";
38                         ICON_BASE_URL = WebBrowserUIPlugin.getInstance().getBundle()
39                                         .getEntry(pathSuffix);
40                 } catch (Exception e) {
41                         Trace.trace(Trace.SEVERE, "Could not set icon base URL", e);
42                 }
43         }
44
45         private static Image[] busyImages;
46
47         private static final String URL_CLCL = "clcl16/";
48
49         private static final String URL_ELCL = "elcl16/";
50
51         private static final String URL_DLCL = "dlcl16/";
52
53         private static final String URL_OBJ = "obj16/";
54
55         // --- constants for images ---
56         // toolbar images
57         public static final String IMG_CLCL_NAV_BACKWARD = "IMG_CLCL_NAV_BACKWARD";
58
59         public static final String IMG_CLCL_NAV_FORWARD = "IMG_CLCL_NAV_FORWARD";
60
61         public static final String IMG_CLCL_NAV_STOP = "IMG_CLCL_NAV_STOP";
62
63         public static final String IMG_CLCL_NAV_REFRESH = "IMG_CLCL_NAV_REFRESH";
64
65         public static final String IMG_CLCL_NAV_GO = "IMG_CLCL_NAV_GO";
66
67         public static final String IMG_CLCL_NAV_FAVORITES = "cfavorites";
68
69         public static final String IMG_CLCL_NAV_HOME = "IMG_CLCL_NAV_HOME";
70
71         public static final String IMG_CLCL_NAV_PRINT = "IMG_CLCL_NAV_PRINT";
72
73         public static final String IMG_ELCL_NAV_BACKWARD = "IMG_ELCL_NAV_BACKWARD";
74
75         public static final String IMG_ELCL_NAV_FORWARD = "IMG_ELCL_NAV_FORWARD";
76
77         public static final String IMG_ELCL_NAV_STOP = "IMG_ELCL_NAV_STOP";
78
79         public static final String IMG_ELCL_NAV_REFRESH = "IMG_ELCL_NAV_REFRESH";
80
81         public static final String IMG_ELCL_NAV_GO = "IMG_ELCL_NAV_GO";
82
83         public static final String IMG_ELCL_NAV_FAVORITES = "efavorites";
84
85         public static final String IMG_ELCL_NAV_HOME = "IMG_ELCL_NAV_HOME";
86
87         public static final String IMG_ELCL_NAV_PRINT = "IMG_ELCL_NAV_PRINT";
88
89         public static final String IMG_DLCL_NAV_BACKWARD = "IMG_DLCL_NAV_BACKWARD";
90
91         public static final String IMG_DLCL_NAV_FORWARD = "IMG_DLCL_NAV_FORWARD";
92
93         public static final String IMG_DLCL_NAV_STOP = "IMG_DLCL_NAV_STOP";
94
95         public static final String IMG_DLCL_NAV_REFRESH = "IMG_DLCL_NAV_REFRESH";
96
97         public static final String IMG_DLCL_NAV_GO = "IMG_DLCL_NAV_GO";
98
99         public static final String IMG_DLCL_NAV_FAVORITES = "dfavorites";
100
101         public static final String IMG_DLCL_NAV_HOME = "IMG_DLCL_NAV_HOME";
102
103         public static final String IMG_DLCL_NAV_PRINT = "IMG_DLCL_NAV_PRINT";
104
105         // general object images
106         public static final String IMG_INTERNAL_BROWSER = "internalBrowser";
107
108         public static final String IMG_EXTERNAL_BROWSER = "externalBrowser";
109
110         public static final String IMG_FAVORITE = "favorite";
111
112         /**
113          * Cannot construct an ImageResource. Use static methods only.
114          */
115         private ImageResource() {
116         }
117
118         /**
119          * Returns the busy images for the Web browser.
120          * 
121          * @return org.eclipse.swt.graphics.Image[]
122          */
123         public static Image[] getBusyImages() {
124                 return busyImages;
125         }
126
127         /**
128          * Return the image with the given key.
129          * 
130          * @param key
131          *            java.lang.String
132          * @return org.eclipse.swt.graphics.Image
133          */
134         public static Image getImage(String key) {
135                 if (imageRegistry == null)
136                         initializeImageRegistry();
137                 return imageRegistry.get(key);
138         }
139
140         /**
141          * Return the image descriptor with the given key.
142          * 
143          * @param key
144          *            java.lang.String
145          * @return org.eclipse.jface.resource.ImageDescriptor
146          */
147         public static ImageDescriptor getImageDescriptor(String key) {
148                 if (imageRegistry == null)
149                         initializeImageRegistry();
150                 return (ImageDescriptor) imageDescriptors.get(key);
151         }
152
153         /**
154          * Initialize the image resources.
155          */
156         protected static void initializeImageRegistry() {
157                 imageRegistry = new ImageRegistry();
158                 imageDescriptors = new HashMap();
159
160                 // load Web browser images
161                 registerImage(IMG_ELCL_NAV_BACKWARD, URL_ELCL + "nav_backward.gif");
162                 registerImage(IMG_ELCL_NAV_FORWARD, URL_ELCL + "nav_forward.gif");
163                 registerImage(IMG_ELCL_NAV_STOP, URL_ELCL + "nav_stop.gif");
164                 registerImage(IMG_ELCL_NAV_REFRESH, URL_ELCL + "nav_refresh.gif");
165                 registerImage(IMG_ELCL_NAV_GO, URL_ELCL + "nav_go.gif");
166                 registerImage(IMG_ELCL_NAV_FAVORITES, URL_ELCL + "add_favorite.gif");
167                 registerImage(IMG_ELCL_NAV_HOME, URL_ELCL + "nav_home.gif");
168                 registerImage(IMG_ELCL_NAV_PRINT, URL_ELCL + "nav_print.gif");
169
170                 registerImage(IMG_CLCL_NAV_BACKWARD, URL_CLCL + "nav_backward.gif");
171                 registerImage(IMG_CLCL_NAV_FORWARD, URL_CLCL + "nav_forward.gif");
172                 registerImage(IMG_CLCL_NAV_STOP, URL_CLCL + "nav_stop.gif");
173                 registerImage(IMG_CLCL_NAV_REFRESH, URL_CLCL + "nav_refresh.gif");
174                 registerImage(IMG_CLCL_NAV_GO, URL_CLCL + "nav_go.gif");
175                 registerImage(IMG_CLCL_NAV_FAVORITES, URL_CLCL + "add_favorite.gif");
176                 registerImage(IMG_CLCL_NAV_HOME, URL_CLCL + "nav_home.gif");
177                 registerImage(IMG_CLCL_NAV_PRINT, URL_CLCL + "nav_print.gif");
178
179                 registerImage(IMG_DLCL_NAV_BACKWARD, URL_DLCL + "nav_backward.gif");
180                 registerImage(IMG_DLCL_NAV_FORWARD, URL_DLCL + "nav_forward.gif");
181                 registerImage(IMG_DLCL_NAV_STOP, URL_DLCL + "nav_stop.gif");
182                 registerImage(IMG_DLCL_NAV_REFRESH, URL_DLCL + "nav_refresh.gif");
183                 registerImage(IMG_DLCL_NAV_GO, URL_DLCL + "nav_go.gif");
184                 registerImage(IMG_DLCL_NAV_FAVORITES, URL_DLCL + "add_favorite.gif");
185                 registerImage(IMG_DLCL_NAV_HOME, URL_DLCL + "nav_home.gif");
186                 registerImage(IMG_DLCL_NAV_PRINT, URL_DLCL + "nav_print.gif");
187
188                 registerImage(IMG_INTERNAL_BROWSER, URL_OBJ + "internal_browser.gif");
189                 registerImage(IMG_EXTERNAL_BROWSER, URL_OBJ + "external_browser.gif");
190
191                 registerImage(IMG_FAVORITE, URL_OBJ + "favorite.gif");
192
193                 // busy images
194                 busyImages = new Image[13];
195                 for (int i = 0; i < 13; i++) {
196                         registerImage("busy" + i, URL_OBJ + "frames/frame" + (i + 1) + ".gif");
197                         busyImages[i] = getImage("busy" + i);
198                 }
199         }
200
201         /**
202          * Register an image with the registry.
203          * 
204          * @param key
205          *            java.lang.String
206          * @param partialURL
207          *            java.lang.String
208          */
209         private static void registerImage(String key, String partialURL) {
210                 try {
211                         ImageDescriptor id = ImageDescriptor.createFromURL(new URL(
212                                         ICON_BASE_URL, partialURL));
213                         imageRegistry.put(key, id);
214                         imageDescriptors.put(key, id);
215                 } catch (Exception e) {
216                         Trace.trace(Trace.WARNING, "Error registering image " + key
217                                         + " from " + partialURL, e);
218                 }
219         }
220 }