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