Fixes for bug #1326088
[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  * Utility class to handle image resources.
22  */
23 public class ImageResource {
24         // the image registry
25         private static ImageRegistry imageRegistry;
26
27         // map of image descriptors since these
28         // will be lost by the image registry
29         private static Map imageDescriptors;
30
31         // base urls for images
32         private static URL ICON_BASE_URL;
33
34         static {
35                 try {
36                         String pathSuffix = "icons/";
37                         ICON_BASE_URL = WebBrowserUIPlugin.getInstance().getBundle().getEntry(pathSuffix);
38                 } catch (Exception e) {
39                         Trace.trace(Trace.SEVERE, "Could not set icon base URL", e);
40                 }
41         }
42
43         private static Image[] busyImages;
44
45         private static final String URL_CLCL = "clcl16/";
46         private static final String URL_ELCL = "elcl16/";
47         private static final String URL_DLCL = "dlcl16/";
48
49         private static final String URL_OBJ = "obj16/";
50
51         // --- constants for images ---
52         // toolbar images
53         public static final String IMG_CLCL_NAV_BACKWARD = "IMG_CLCL_NAV_BACKWARD";
54         public static final String IMG_CLCL_NAV_FORWARD = "IMG_CLCL_NAV_FORWARD";
55         public static final String IMG_CLCL_NAV_STOP = "IMG_CLCL_NAV_STOP";
56         public static final String IMG_CLCL_NAV_REFRESH = "IMG_CLCL_NAV_REFRESH";
57         public static final String IMG_CLCL_NAV_GO = "IMG_CLCL_NAV_GO";
58         public static final String IMG_CLCL_NAV_FAVORITES = "cfavorites";
59         public static final String IMG_CLCL_NAV_HOME = "IMG_CLCL_NAV_HOME";
60         public static final String IMG_CLCL_NAV_PRINT = "IMG_CLCL_NAV_PRINT";
61
62         public static final String IMG_ELCL_NAV_BACKWARD = "IMG_ELCL_NAV_BACKWARD";
63         public static final String IMG_ELCL_NAV_FORWARD = "IMG_ELCL_NAV_FORWARD";
64         public static final String IMG_ELCL_NAV_STOP = "IMG_ELCL_NAV_STOP";
65         public static final String IMG_ELCL_NAV_REFRESH = "IMG_ELCL_NAV_REFRESH";
66         public static final String IMG_ELCL_NAV_GO = "IMG_ELCL_NAV_GO";
67         public static final String IMG_ELCL_NAV_FAVORITES = "efavorites";
68         public static final String IMG_ELCL_NAV_HOME = "IMG_ELCL_NAV_HOME";
69         public static final String IMG_ELCL_NAV_PRINT = "IMG_ELCL_NAV_PRINT";
70
71         public static final String IMG_DLCL_NAV_BACKWARD = "IMG_DLCL_NAV_BACKWARD";
72         public static final String IMG_DLCL_NAV_FORWARD = "IMG_DLCL_NAV_FORWARD";
73         public static final String IMG_DLCL_NAV_STOP = "IMG_DLCL_NAV_STOP";
74         public static final String IMG_DLCL_NAV_REFRESH = "IMG_DLCL_NAV_REFRESH";
75         public static final String IMG_DLCL_NAV_GO = "IMG_DLCL_NAV_GO";
76         public static final String IMG_DLCL_NAV_FAVORITES = "dfavorites";
77         public static final String IMG_DLCL_NAV_HOME = "IMG_DLCL_NAV_HOME";
78         public static final String IMG_DLCL_NAV_PRINT = "IMG_DLCL_NAV_PRINT";
79
80         // general object images
81         public static final String IMG_INTERNAL_BROWSER = "internalBrowser";
82         public static final String IMG_EXTERNAL_BROWSER = "externalBrowser";
83         public static final String IMG_FAVORITE = "favorite";
84
85         /**
86          * Cannot construct an ImageResource. Use static methods only.
87          */
88         private ImageResource() { }
89
90         /**
91          * Returns the busy images for the Web browser.
92          *
93          * @return org.eclipse.swt.graphics.Image[]
94          */
95         public static Image[] getBusyImages() {
96                 return busyImages;
97         }
98
99         /**
100          * Return the image with the given key.
101          *
102          * @param key java.lang.String
103          * @return org.eclipse.swt.graphics.Image
104          */
105         public static Image getImage(String key) {
106                 if (imageRegistry == null)
107                         initializeImageRegistry();
108                 return imageRegistry.get(key);
109         }
110
111         /**
112          * Return the image descriptor with the given key.
113          *
114          * @param key java.lang.String
115          * @return org.eclipse.jface.resource.ImageDescriptor
116          */
117         public static ImageDescriptor getImageDescriptor(String key) {
118                 if (imageRegistry == null)
119                         initializeImageRegistry();
120                 return (ImageDescriptor) imageDescriptors.get(key);
121         }
122
123         /**
124          * Initialize the image resources.
125          */
126         protected static void initializeImageRegistry() {
127                 imageRegistry = new ImageRegistry();
128                 imageDescriptors = new HashMap();
129         
130                 // load Web browser images
131                 registerImage(IMG_ELCL_NAV_BACKWARD, URL_ELCL + "nav_backward.gif");
132                 registerImage(IMG_ELCL_NAV_FORWARD, URL_ELCL + "nav_forward.gif");
133                 registerImage(IMG_ELCL_NAV_STOP, URL_ELCL + "nav_stop.gif");
134                 registerImage(IMG_ELCL_NAV_REFRESH, URL_ELCL + "nav_refresh.gif");
135                 registerImage(IMG_ELCL_NAV_GO, URL_ELCL + "nav_go.gif");
136                 registerImage(IMG_ELCL_NAV_FAVORITES, URL_ELCL + "add_favorite.gif");
137                 registerImage(IMG_ELCL_NAV_HOME, URL_ELCL + "nav_home.gif");
138                 registerImage(IMG_ELCL_NAV_PRINT, URL_ELCL + "nav_print.gif");
139         
140                 registerImage(IMG_CLCL_NAV_BACKWARD, URL_CLCL + "nav_backward.gif");
141                 registerImage(IMG_CLCL_NAV_FORWARD, URL_CLCL + "nav_forward.gif");
142                 registerImage(IMG_CLCL_NAV_STOP, URL_CLCL + "nav_stop.gif");
143                 registerImage(IMG_CLCL_NAV_REFRESH, URL_CLCL + "nav_refresh.gif");
144                 registerImage(IMG_CLCL_NAV_GO, URL_CLCL + "nav_go.gif");
145                 registerImage(IMG_CLCL_NAV_FAVORITES, URL_CLCL + "add_favorite.gif");
146                 registerImage(IMG_CLCL_NAV_HOME, URL_CLCL + "nav_home.gif");
147                 registerImage(IMG_CLCL_NAV_PRINT, URL_CLCL + "nav_print.gif");
148         
149                 registerImage(IMG_DLCL_NAV_BACKWARD, URL_DLCL + "nav_backward.gif");
150                 registerImage(IMG_DLCL_NAV_FORWARD, URL_DLCL + "nav_forward.gif");
151                 registerImage(IMG_DLCL_NAV_STOP, URL_DLCL + "nav_stop.gif");
152                 registerImage(IMG_DLCL_NAV_REFRESH, URL_DLCL + "nav_refresh.gif");
153                 registerImage(IMG_DLCL_NAV_GO, URL_DLCL + "nav_go.gif");
154                 registerImage(IMG_DLCL_NAV_FAVORITES, URL_DLCL + "add_favorite.gif");
155                 registerImage(IMG_DLCL_NAV_HOME, URL_DLCL + "nav_home.gif");
156                 registerImage(IMG_DLCL_NAV_PRINT, URL_DLCL + "nav_print.gif");
157         
158                 registerImage(IMG_INTERNAL_BROWSER, URL_OBJ + "internal_browser.gif");
159                 registerImage(IMG_EXTERNAL_BROWSER, URL_OBJ + "external_browser.gif");
160                 
161                 registerImage(IMG_FAVORITE, URL_OBJ + "favorite.gif");
162                 
163                 // busy images
164                 busyImages = new Image[13];
165                 for (int i = 0; i < 13; i++) {
166                         registerImage("busy" + i, URL_OBJ + "frames" + java.io.File.separator + "frame" + (i+1) + ".gif");
167                         busyImages[i] = getImage("busy" + i);
168                 }
169         }
170
171         /**
172          * Register an image with the registry.
173          *
174          * @param key java.lang.String
175          * @param partialURL java.lang.String
176          */
177         private static void registerImage(String key, String partialURL) {
178                 try {
179                         ImageDescriptor id = ImageDescriptor.createFromURL(new URL(ICON_BASE_URL, partialURL));
180                         imageRegistry.put(key, id);
181                         imageDescriptors.put(key, id);
182                 } catch (Exception e) {
183                         Trace.trace(Trace.WARNING, "Error registering image " + key + " from " + partialURL, e);
184                 }
185         }
186 }