KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > util > ImageUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.internal.intro.impl.util;
12
13 import java.net.URL JavaDoc;
14
15 import org.eclipse.core.runtime.FileLocator;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.resource.ImageRegistry;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.ui.internal.intro.impl.IIntroConstants;
23 import org.eclipse.ui.internal.intro.impl.IntroPlugin;
24 import org.osgi.framework.Bundle;
25
26 /**
27  * Convenience class for Images.
28  */

29 public final class ImageUtil {
30
31     /**
32      * Image keys, to be used by plugin (intro) registry.
33      */

34     // Default images
35
public static final String JavaDoc DEFAULT_ROOT_LINK = "rootLink"; //$NON-NLS-1$
36
public static final String JavaDoc DEFAULT_SMALL_ROOT_LINK = "rootLinkSmall"; //$NON-NLS-1$
37
public static final String JavaDoc DEFAULT_FORM_BG = "formBg"; //$NON-NLS-1$
38
public static final String JavaDoc DEFAULT_LINK = "link"; //$NON-NLS-1$
39

40     // Standby images
41
public static final String JavaDoc BACK = "back"; //$NON-NLS-1$
42
public static final String JavaDoc HELP_TOPIC = "helpTopic"; //$NON-NLS-1$
43

44     // Launch bar image
45
public static final String JavaDoc RESTORE_WELCOME = "restoreWelcome"; //$NON-NLS-1$
46

47     // Viewer images
48
public static final String JavaDoc INTRO_MODEL_LEAF = "leaf"; //$NON-NLS-1$
49
public static final String JavaDoc INTRO_MODEL_CONTAINER = "container"; //$NON-NLS-1$
50
public static final String JavaDoc OPEN_ITNRO_VIEW = "introView"; //$NON-NLS-1$
51

52     public static final String JavaDoc CONFIG_EXTENSION = "configExtension";//$NON-NLS-1$
53

54     // Image location
55
public static final String JavaDoc ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$
56

57     /**
58      * Convenience method to create an image descriptor from the Intro plugin.
59      *
60      * Method assumes that images are under the "icons" directory, so don't
61      * append that directory name for "imageName".
62      */

63     public static ImageDescriptor createImageDescriptor(String JavaDoc imageName) {
64         return createImageDescriptor(Platform
65             .getBundle(IIntroConstants.PLUGIN_ID), ICONS_PATH + imageName);
66     }
67     
68     /**
69      * Convenience method to create an image descriptor.
70      *
71      */

72     public static ImageDescriptor createImageDescriptor(Bundle bundle,
73             String JavaDoc imageName) {
74         try {
75             URL JavaDoc imageUrl = FileLocator.find(bundle, new Path(imageName), null);
76             if (imageUrl != null) {
77                 ImageDescriptor desc = ImageDescriptor.createFromURL(imageUrl);
78                 return desc;
79             }
80         } catch (Exception JavaDoc e) {
81             // Should never be here.
82
Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
83
}
84         Log.warning("could not create Image Descriptor for: " + imageName //$NON-NLS-1$
85
+ " in bundle: " + bundle.getSymbolicName()); //$NON-NLS-1$
86
return ImageDescriptor.getMissingImageDescriptor();
87     }
88     
89     /**
90      * Convenience method to create an image descriptor.
91      *
92      */

93     public static ImageDescriptor createImageDescriptor(IPath base,
94             String JavaDoc imageName) {
95         try {
96             URL JavaDoc imageUrl = new URL JavaDoc(base.append(imageName).toOSString());
97             if (imageUrl != null) {
98                 ImageDescriptor desc = ImageDescriptor.createFromURL(imageUrl);
99                 return desc;
100             }
101         } catch (Exception JavaDoc e) {
102             // Should never be here.
103
Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
104
}
105         Log.warning("could not create Image Descriptor for: " + imageName); //$NON-NLS-1$
106
return ImageDescriptor.getMissingImageDescriptor();
107     }
108
109     /**
110      * Convenience method to create an image from the Intro plugin.
111      *
112      * Method assumes that images are under the "icons" directory, so don't
113      * append that directory name for "imageName".
114      */

115     public static Image createImage(String JavaDoc imageName) {
116         try {
117             ImageDescriptor imageDsc = createImageDescriptor(imageName);
118             return imageDsc.createImage();
119         } catch (Exception JavaDoc e) {
120             // Should never be here.
121
Log.error("could not create Image", e); //$NON-NLS-1$
122
return ImageDescriptor.getMissingImageDescriptor().createImage();
123         }
124     }
125
126     /**
127      * Util method for image re-use in Intro Plugin.
128      *
129      * @param key
130      * @return
131      */

132     public static Image getImage(String JavaDoc key) {
133         // INTRO: Image registry should not have the same life span
134
// as the intro plug-in. It should be disposed when
135
// presentation is disposed, otherwise images will
136
// stay around once Inro has been loaded.
137
return IntroPlugin.getDefault().getVolatileImageRegistry().get(key);
138     }
139
140     public static boolean hasImage(String JavaDoc key) {
141         ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
142         return (registry.getDescriptor(key) != null);
143     }
144
145     /**
146      * Register an image descriptor in the Intro Plugin image registry. Has no
147      * effect if the key has already been registered.
148      *
149      * @param key
150      * @param imageName
151      */

152     public static void registerImage(String JavaDoc key, String JavaDoc imageName) {
153         ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
154         if (registry.getDescriptor(key) != null)
155             // key has already been registered. do nothing.
156
return;
157         registry.put(key, createImageDescriptor(imageName));
158     }
159
160     public static void registerImage(String JavaDoc key, Bundle bundle, String JavaDoc imageName) {
161
162         ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
163         if (registry.getDescriptor(key) != null)
164             // key has already been registered. do nothing.
165
return;
166         registry.put(key, createImageDescriptor(bundle, imageName));
167     }
168     
169     public static void registerImage(String JavaDoc key, IPath base, String JavaDoc imageName) {
170         ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
171         if (registry.getDescriptor(key) != null)
172             // key has already been registered. do nothing.
173
return;
174         registry.put(key, createImageDescriptor(base, imageName));
175     }
176 }
177
Popular Tags