KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > model > ExternalToolsImages


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.externaltools.internal.model;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.resource.CompositeImageDescriptor;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.ImageRegistry;
20 import org.eclipse.swt.graphics.Image;
21
22 /**
23  * The images provided by the external tools plugin.
24  */

25 public class ExternalToolsImages {
26
27     /**
28      * The image registry containing <code>Image</code>s.
29      */

30     private static ImageRegistry imageRegistry;
31     
32     /**
33      * The registry for composite images
34      */

35     private static ImageDescriptorRegistry imageDescriptorRegistry;
36
37     /* Declare Common paths */
38     private static URL JavaDoc ICON_BASE_URL= null;
39
40     static {
41         String JavaDoc pathSuffix = "icons/full/"; //$NON-NLS-1$
42
ICON_BASE_URL= ExternalToolsPlugin.getDefault().getBundle().getEntry(pathSuffix);
43     }
44
45     // Use IPath and toOSString to build the names to ensure they have the slashes correct
46
private final static String JavaDoc OBJECT= "obj16/"; //basic colors - size 16x16 //$NON-NLS-1$
47

48     /**
49      * Declare all images
50      */

51     private static void declareImages() {
52         // Objects
53
declareRegistryImage(IExternalToolConstants.IMG_TAB_MAIN, OBJECT + "main_tab.gif"); //$NON-NLS-1$
54
}
55
56     /**
57      * Declare an Image in the registry table.
58      * @param key The key to use when registering the image
59      * @param path The path where the image can be found. This path is relative to where
60      * this plugin class is found (i.e. typically the packages directory)
61      */

62     private final static void declareRegistryImage(String JavaDoc key, String JavaDoc path) {
63         ImageDescriptor desc= ImageDescriptor.getMissingImageDescriptor();
64         try {
65             desc= ImageDescriptor.createFromURL(makeIconFileURL(path));
66         } catch (MalformedURLException JavaDoc me) {
67         }
68         imageRegistry.put(key, desc);
69     }
70     
71     /**
72      * Returns the ImageRegistry.
73      */

74     public static ImageRegistry getImageRegistry() {
75         if (imageRegistry == null) {
76             initializeImageRegistry();
77         }
78         return imageRegistry;
79     }
80
81     /**
82      * Initialize the image registry by declaring all of the required
83      * graphics. This involves creating JFace image descriptors describing
84      * how to create/find the image should it be needed.
85      * The image is not actually allocated until requested.
86      *
87      * Prefix conventions
88      * Wizard Banners WIZBAN_
89      * Preference Banners PREF_BAN_
90      * Property Page Banners PROPBAN_
91      * Color toolbar CTOOL_
92      * Enable toolbar ETOOL_
93      * Disable toolbar DTOOL_
94      * Local enabled toolbar ELCL_
95      * Local Disable toolbar DLCL_
96      * Object large OBJL_
97      * Object small OBJS_
98      * View VIEW_
99      * Product images PROD_
100      * Misc images MISC_
101      *
102      * Where are the images?
103      * The images (typically gifs) are found in the same location as this plugin class.
104      * This may mean the same package directory as the package holding this class.
105      * The images are declared using this.getClass() to ensure they are looked up via
106      * this plugin class.
107      * @see org.eclipse.jface.resource.ImageRegistry
108      */

109     public static ImageRegistry initializeImageRegistry() {
110         imageRegistry= new ImageRegistry(ExternalToolsPlugin.getStandardDisplay());
111         declareImages();
112         return imageRegistry;
113     }
114
115     /**
116      * Returns the <code>Image<code> identified by the given key,
117      * or <code>null</code> if it does not exist.
118      */

119     public static Image getImage(String JavaDoc key) {
120         return getImageRegistry().get(key);
121     }
122     
123     /**
124      * Returns the <code>ImageDescriptor<code> identified by the given key,
125      * or <code>null</code> if it does not exist.
126      */

127     public static ImageDescriptor getImageDescriptor(String JavaDoc key) {
128         return getImageRegistry().getDescriptor(key);
129     }
130     
131     private static URL JavaDoc makeIconFileURL(String JavaDoc iconPath) throws MalformedURLException JavaDoc {
132         if (ICON_BASE_URL == null) {
133             throw new MalformedURLException JavaDoc();
134         }
135             
136         return new URL JavaDoc(ICON_BASE_URL, iconPath);
137     }
138     
139     /**
140      * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
141      * are retrieved from the *lcl16 folders.
142      */

143     public static void setLocalImageDescriptors(IAction action, String JavaDoc iconName) {
144         setImageDescriptors(action, "lcl16", iconName); //$NON-NLS-1$
145
}
146     
147     private static void setImageDescriptors(IAction action, String JavaDoc type, String JavaDoc relPath) {
148         
149         try {
150             ImageDescriptor id= ImageDescriptor.createFromURL(makeIconFileURL("d" + type, relPath)); //$NON-NLS-1$
151
if (id != null)
152                 action.setDisabledImageDescriptor(id);
153         } catch (MalformedURLException JavaDoc e) {
154             ExternalToolsPlugin.getDefault().log(e);
155         }
156
157         try {
158             ImageDescriptor id= ImageDescriptor.createFromURL(makeIconFileURL("c" + type, relPath)); //$NON-NLS-1$
159
if (id != null)
160                 action.setHoverImageDescriptor(id);
161         } catch (MalformedURLException JavaDoc e) {
162             ExternalToolsPlugin.getDefault().log(e);
163         }
164
165         action.setImageDescriptor(create("e" + type, relPath)); //$NON-NLS-1$
166
}
167     
168     private static URL JavaDoc makeIconFileURL(String JavaDoc prefix, String JavaDoc name) throws MalformedURLException JavaDoc {
169         if (ICON_BASE_URL == null) {
170             throw new MalformedURLException JavaDoc();
171         }
172         
173         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(prefix);
174         buffer.append('/');
175         buffer.append(name);
176         return new URL JavaDoc(ICON_BASE_URL, buffer.toString());
177     }
178     
179     private static ImageDescriptor create(String JavaDoc prefix, String JavaDoc name) {
180         try {
181             return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
182         } catch (MalformedURLException JavaDoc e) {
183             ExternalToolsPlugin.getDefault().log(e);
184             return ImageDescriptor.getMissingImageDescriptor();
185         }
186     }
187     
188     /**
189      * Returns the image for the given composite descriptor.
190      */

191     public static Image getImage(CompositeImageDescriptor imageDescriptor) {
192         if (imageDescriptorRegistry == null) {
193             imageDescriptorRegistry = new ImageDescriptorRegistry();
194         }
195         return imageDescriptorRegistry.get(imageDescriptor);
196     }
197     
198     public static void disposeImageDescriptorRegistry() {
199         if (imageDescriptorRegistry != null) {
200             imageDescriptorRegistry.dispose();
201         }
202     }
203 }
204
Popular Tags