KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > 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.universal.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.swt.widgets.Display;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.internal.intro.universal.IUniversalIntroConstants;
25 import org.eclipse.ui.internal.intro.universal.UniversalIntroPlugin;
26 import org.osgi.framework.Bundle;
27
28 /**
29  * Convenience class for Images.
30  */

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

36     // Default images
37
public static final String JavaDoc DEFAULT_ROOT_LINK = "rootLink"; //$NON-NLS-1$
38

39     public static final String JavaDoc DEFAULT_SMALL_ROOT_LINK = "rootLinkSmall"; //$NON-NLS-1$
40

41     public static final String JavaDoc DEFAULT_FORM_BG = "formBg"; //$NON-NLS-1$
42

43     public static final String JavaDoc DEFAULT_LINK = "link"; //$NON-NLS-1$
44

45     // Standby images
46
public static final String JavaDoc BACK = "back"; //$NON-NLS-1$
47

48     public static final String JavaDoc HELP_TOPIC = "helpTopic"; //$NON-NLS-1$
49

50     // Launch bar image
51
public static final String JavaDoc RESTORE_WELCOME = "restoreWelcome"; //$NON-NLS-1$
52

53     // Viewer images
54
public static final String JavaDoc INTRO_MODEL_LEAF = "leaf"; //$NON-NLS-1$
55

56     public static final String JavaDoc INTRO_MODEL_CONTAINER = "container"; //$NON-NLS-1$
57

58     public static final String JavaDoc OPEN_ITNRO_VIEW = "introView"; //$NON-NLS-1$
59

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

62     // Image location
63
public static final String JavaDoc ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$
64

65     /**
66      * Convenience method to create an image descriptor from the Intro plugin.
67      *
68      * Method assumes that images are under the "icons" directory, so don't
69      * append that directory name for "imageName".
70      */

71     public static ImageDescriptor createImageDescriptor(String JavaDoc imageName) {
72         return createImageDescriptor(Platform
73                 .getBundle(IUniversalIntroConstants.PLUGIN_ID), ICONS_PATH + imageName);
74     }
75
76     /**
77      * Convenience method to create an image descriptor.
78      *
79      */

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

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

123     public static Image createImage(String JavaDoc imageName) {
124         try {
125             ImageDescriptor imageDsc = createImageDescriptor(imageName);
126             return imageDsc.createImage();
127         } catch (Exception JavaDoc e) {
128             // Should never be here.
129
Log.error("could not create Image", e); //$NON-NLS-1$
130
return ImageDescriptor.getMissingImageDescriptor().createImage();
131         }
132     }
133
134     /**
135      * Util method for image re-use in Intro Plugin.
136      *
137      * @param key
138      * @return
139      */

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

162     public static void registerImage(String JavaDoc key, String JavaDoc imageName) {
163         ImageRegistry registry = UniversalIntroPlugin.getDefault()
164                 .getVolatileImageRegistry();
165         if (registry.getDescriptor(key) != null)
166             // key has already been registered. do nothing.
167
return;
168         registry.put(key, createImageDescriptor(imageName));
169     }
170
171     public static void registerImage(String JavaDoc key, Bundle bundle, String JavaDoc imageName) {
172
173         ImageRegistry registry = UniversalIntroPlugin.getDefault()
174                 .getVolatileImageRegistry();
175         if (registry.getDescriptor(key) != null)
176             // key has already been registered. do nothing.
177
return;
178         registry.put(key, createImageDescriptor(bundle, imageName));
179     }
180
181     public static void registerImage(String JavaDoc key, IPath base, String JavaDoc imageName) {
182         ImageRegistry registry = UniversalIntroPlugin.getDefault()
183                 .getVolatileImageRegistry();
184         if (registry.getDescriptor(key) != null)
185             // key has already been registered. do nothing.
186
return;
187         registry.put(key, createImageDescriptor(base, imageName));
188     }
189     
190     /**
191      * Tests for high contrast mode. Returns false if not called from a
192      * UI thread (causes deadlocks during junit tests).
193      *
194      * @return whether or not the display is in high contrast mode
195      */

196     public static boolean isHighContrast() {
197         if (PlatformUI.isWorkbenchRunning()) {
198             Display display = PlatformUI.getWorkbench().getDisplay();
199             if (Display.getCurrent() == display) {
200                 return display.getHighContrast();
201             }
202         }
203         return false;
204     }
205 }
206
Popular Tags