KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > model > AntUIImages


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 Corporation - initial API and implementation
10  * John-Mason P. Shackelford (john-mason.shackelford@pearson.com) - bug 49380, 49445, 53547
11  *******************************************************************************/

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

29 public class AntUIImages {
30
31     /**
32      * The image registry containing <code>Image</code>s.
33      */

34     private static ImageRegistry imageRegistry;
35     
36     /**
37      * The registry for composite images
38      */

39     private static ImageDescriptorRegistry imageDescriptorRegistry;
40     
41     /**
42      * A table of all the <code>ImageDescriptor</code>s.
43      */

44     private static Map JavaDoc imageDescriptors;
45
46     /* Declare Common paths */
47     private static URL JavaDoc ICON_BASE_URL= null;
48
49     static {
50         String JavaDoc pathSuffix = "icons/full/"; //$NON-NLS-1$
51
ICON_BASE_URL= AntUIPlugin.getDefault().getBundle().getEntry(pathSuffix);
52     }
53
54     // Use IPath and toOSString to build the names to ensure they have the slashes correct
55
private final static String JavaDoc LOCALTOOL= "elcl16/"; //basic colors - size 16x16 //$NON-NLS-1$
56
private final static String JavaDoc OBJECT= "obj16/"; //basic colors - size 16x16 //$NON-NLS-1$
57
private final static String JavaDoc OVR= "ovr16/"; //basic colors - size 7x8 //$NON-NLS-1$
58
private final static String JavaDoc WIZ= "wizban/"; //$NON-NLS-1$
59

60     /**
61      * Declare all images
62      */

63     private static void declareImages() {
64         // Ant Editor images
65
declareRegistryImage(IAntUIConstants.IMG_PROPERTY, OBJECT + "property_obj.gif"); //$NON-NLS-1$
66
declareRegistryImage(IAntUIConstants.IMG_TASK_PROPOSAL, OBJECT + "task_obj.gif"); //$NON-NLS-1$
67
declareRegistryImage(IAntUIConstants.IMG_TEMPLATE_PROPOSAL, OBJECT + "template_obj.gif"); //$NON-NLS-1$
68

69         // Ant View Actions
70
declareRegistryImage(IAntUIConstants.IMG_ANT, OBJECT + "ant.gif"); //$NON-NLS-1$
71
declareRegistryImage(IAntUIConstants.IMG_REMOVE, LOCALTOOL + "remove_co.gif"); //$NON-NLS-1$
72
declareRegistryImage(IAntUIConstants.IMG_REMOVE_ALL, LOCALTOOL + "removeAll_co.gif"); //$NON-NLS-1$
73
declareRegistryImage(IAntUIConstants.IMG_ADD, LOCALTOOL + "add_co.gif"); //$NON-NLS-1$
74
declareRegistryImage(IAntUIConstants.IMG_RUN, LOCALTOOL + "run_tool.gif"); //$NON-NLS-1$
75
declareRegistryImage(IAntUIConstants.IMG_SEARCH, LOCALTOOL + "search.gif"); //$NON-NLS-1$
76
declareRegistryImage(IAntUIConstants.IMG_FILTER_INTERNAL_TARGETS, LOCALTOOL + "filter_internal_targets.gif"); //$NON-NLS-1$
77
declareRegistryImage(IAntUIConstants.IMG_FILTER_IMPORTED_ELEMENTS, LOCALTOOL + "filter_imported_elements.gif"); //$NON-NLS-1$
78
declareRegistryImage(IAntUIConstants.IMG_FILTER_PROPERTIES, LOCALTOOL + "filter_properties.gif"); //$NON-NLS-1$
79
declareRegistryImage(IAntUIConstants.IMG_FILTER_TOP_LEVEL, LOCALTOOL + "filter_top_level.gif"); //$NON-NLS-1$
80
declareRegistryImage(IAntUIConstants.IMG_LINK_WITH_EDITOR, LOCALTOOL + "synced.gif"); //$NON-NLS-1$
81

82         declareRegistryImage(IAntUIConstants.IMG_SORT_OUTLINE, LOCALTOOL + "alpha_mode.gif"); //$NON-NLS-1$
83
declareRegistryImage(IAntUIConstants.IMG_REFRESH, LOCALTOOL + "refresh.gif"); //$NON-NLS-1$
84
// Ant View Labels
85
declareRegistryImage(IAntUIConstants.IMG_ANT_PROJECT, OBJECT + "ant_buildfile.gif"); //$NON-NLS-1$
86
declareRegistryImage(IAntUIConstants.IMG_ANT_TARGET, OBJECT + "targetpublic_obj.gif"); //$NON-NLS-1$
87
declareRegistryImage(IAntUIConstants.IMG_ANT_TARGET_INTERNAL, OBJECT + "targetinternal_obj.gif"); //$NON-NLS-1$
88
declareRegistryImage(IAntUIConstants.IMG_ANT_DEFAULT_TARGET, OBJECT + "defaulttarget_obj.gif"); //$NON-NLS-1$
89
declareRegistryImage(IAntUIConstants.IMG_ANT_TARGET_ERROR, OBJECT + "ant_target_err.gif"); //$NON-NLS-1$
90
declareRegistryImage(IAntUIConstants.IMG_ANT_MACRODEF, OBJECT + "macrodef_obj.gif"); //$NON-NLS-1$
91
declareRegistryImage(IAntUIConstants.IMG_ANT_IMPORT, OBJECT + "import_obj.gif"); //$NON-NLS-1$
92
declareRegistryImage(IAntUIConstants.IMG_ANT_BUILD_TAB, OBJECT + "build_tab.gif"); //$NON-NLS-1$
93
//ANT object
94
declareRegistryImage(IAntUIConstants.IMG_TAB_ANT_TARGETS, LOCALTOOL + "ant_targets.gif"); //$NON-NLS-1$
95
declareRegistryImage(IAntUIConstants.IMG_TAB_CLASSPATH, OBJECT + "classpath.gif"); //$NON-NLS-1$
96
declareRegistryImage(IAntUIConstants.IMG_JAR_FILE, OBJECT + "jar_l_obj.gif"); //$NON-NLS-1$
97
declareRegistryImage(IAntUIConstants.IMG_ANT_TYPE, OBJECT + "type.gif"); //$NON-NLS-1$
98

99         declareRegistryImage(IAntUIConstants.IMG_IMPORT_WIZARD_BANNER, WIZ + "importbuildfile_wiz.gif"); //$NON-NLS-1$
100

101         // Overlays
102
declareRegistryImage(IAntUIConstants.IMG_OVR_ERROR, OVR + "error_co.gif"); //$NON-NLS-1$
103
declareRegistryImage(IAntUIConstants.IMG_OVR_WARNING, OVR + "warning_co.gif"); //$NON-NLS-1$
104
declareRegistryImage(IAntUIConstants.IMG_OVR_IMPORT, OVR + "import_co.gif"); //$NON-NLS-1$
105
}
106
107     /**
108      * Declare an Image in the registry table.
109      * @param key The key to use when registering the image
110      * @param path The path where the image can be found. This path is relative to where
111      * this plugin class is found (i.e. typically the packages directory)
112      */

113     private final static void declareRegistryImage(String JavaDoc key, String JavaDoc path) {
114         ImageDescriptor desc= ImageDescriptor.getMissingImageDescriptor();
115         try {
116             desc= ImageDescriptor.createFromURL(makeIconFileURL(path));
117         } catch (MalformedURLException JavaDoc me) {
118         }
119         imageRegistry.put(key, desc);
120         imageDescriptors.put(key, desc);
121     }
122     
123     /**
124      * Returns the ImageRegistry.
125      */

126     public static ImageRegistry getImageRegistry() {
127         if (imageRegistry == null) {
128             initializeImageRegistry();
129         }
130         return imageRegistry;
131     }
132
133     /**
134      * Initialize the image registry by declaring all of the required
135      * graphics. This involves creating JFace image descriptors describing
136      * how to create/find the image should it be needed.
137      * The image is not actually allocated until requested.
138      *
139      * Prefix conventions
140      * Wizard Banners WIZBAN_
141      * Preference Banners PREF_BAN_
142      * Property Page Banners PROPBAN_
143      * Color toolbar CTOOL_
144      * Enable toolbar ETOOL_
145      * Disable toolbar DTOOL_
146      * Local enabled toolbar ELCL_
147      * Local Disable toolbar DLCL_
148      * Object large OBJL_
149      * Object small OBJS_
150      * View VIEW_
151      * Product images PROD_
152      * Misc images MISC_
153      *
154      * Where are the images?
155      * The images (typically gifs) are found in the same location as this plugin class.
156      * This may mean the same package directory as the package holding this class.
157      * The images are declared using this.getClass() to ensure they are looked up via
158      * this plugin class.
159      * @see org.eclipse.jface.resource.ImageRegistry
160      */

161     public static ImageRegistry initializeImageRegistry() {
162         imageRegistry= new ImageRegistry(AntUIPlugin.getStandardDisplay());
163         imageDescriptors = new HashMap JavaDoc(30);
164         declareImages();
165         return imageRegistry;
166     }
167
168     /**
169      * Returns the <code>Image<code> identified by the given key,
170      * or <code>null</code> if it does not exist.
171      */

172     public static Image getImage(String JavaDoc key) {
173         return getImageRegistry().get(key);
174     }
175     
176     /**
177      * Returns the <code>ImageDescriptor<code> identified by the given key,
178      * or <code>null</code> if it does not exist.
179      */

180     public static ImageDescriptor getImageDescriptor(String JavaDoc key) {
181         if (imageDescriptors == null) {
182             initializeImageRegistry();
183         }
184         return (ImageDescriptor)imageDescriptors.get(key);
185     }
186     
187     private static URL JavaDoc makeIconFileURL(String JavaDoc iconPath) throws MalformedURLException JavaDoc {
188         if (ICON_BASE_URL == null) {
189             throw new MalformedURLException JavaDoc();
190         }
191             
192         return new URL JavaDoc(ICON_BASE_URL, iconPath);
193     }
194     
195     /**
196      * Sets the three image descriptors for enabled, disabled, and hovered to an action. The actions
197      * are retrieved from the *lcl16 folders.
198      */

199     public static void setLocalImageDescriptors(IAction action, String JavaDoc iconName) {
200         setImageDescriptors(action, "lcl16", iconName); //$NON-NLS-1$
201
}
202     
203     private static void setImageDescriptors(IAction action, String JavaDoc type, String JavaDoc relPath) {
204         
205         try {
206             ImageDescriptor id= ImageDescriptor.createFromURL(makeIconFileURL("d" + type, relPath)); //$NON-NLS-1$
207
if (id != null)
208                 action.setDisabledImageDescriptor(id);
209         } catch (MalformedURLException JavaDoc e) {
210             AntUIPlugin.log(e);
211         }
212
213         try {
214             ImageDescriptor id= ImageDescriptor.createFromURL(makeIconFileURL("c" + type, relPath)); //$NON-NLS-1$
215
if (id != null)
216                 action.setHoverImageDescriptor(id);
217         } catch (MalformedURLException JavaDoc e) {
218             AntUIPlugin.log(e);
219         }
220
221         action.setImageDescriptor(create("e" + type, relPath)); //$NON-NLS-1$
222
}
223     
224     private static URL JavaDoc makeIconFileURL(String JavaDoc prefix, String JavaDoc name) throws MalformedURLException JavaDoc {
225         if (ICON_BASE_URL == null) {
226             throw new MalformedURLException JavaDoc();
227         }
228         
229         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(prefix);
230         buffer.append('/');
231         buffer.append(name);
232         return new URL JavaDoc(ICON_BASE_URL, buffer.toString());
233     }
234     
235     private static ImageDescriptor create(String JavaDoc prefix, String JavaDoc name) {
236         try {
237             return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
238         } catch (MalformedURLException JavaDoc e) {
239             AntUIPlugin.log(e);
240             return ImageDescriptor.getMissingImageDescriptor();
241         }
242     }
243     
244     /**
245      * Returns the image for the given composite descriptor.
246      */

247     public static Image getImage(CompositeImageDescriptor imageDescriptor) {
248         if (imageDescriptorRegistry == null) {
249             imageDescriptorRegistry = new ImageDescriptorRegistry();
250         }
251         return imageDescriptorRegistry.get(imageDescriptor);
252     }
253     
254     public static void disposeImageDescriptorRegistry() {
255         if (imageDescriptorRegistry != null) {
256             imageDescriptorRegistry.dispose();
257         }
258     }
259 }
Popular Tags