001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.intro.impl.util;
011:
012: import java.net.URL;
013:
014: import org.eclipse.core.runtime.FileLocator;
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.Path;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.jface.resource.ImageRegistry;
020: import org.eclipse.swt.graphics.Image;
021: import org.eclipse.ui.internal.intro.impl.IIntroConstants;
022: import org.eclipse.ui.internal.intro.impl.IntroPlugin;
023: import org.osgi.framework.Bundle;
024:
025: /**
026: * Convenience class for Images.
027: */
028: public final class ImageUtil {
029:
030: /**
031: * Image keys, to be used by plugin (intro) registry.
032: */
033: // Default images
034: public static final String DEFAULT_ROOT_LINK = "rootLink"; //$NON-NLS-1$
035: public static final String DEFAULT_SMALL_ROOT_LINK = "rootLinkSmall"; //$NON-NLS-1$
036: public static final String DEFAULT_FORM_BG = "formBg"; //$NON-NLS-1$
037: public static final String DEFAULT_LINK = "link"; //$NON-NLS-1$
038:
039: // Standby images
040: public static final String BACK = "back"; //$NON-NLS-1$
041: public static final String HELP_TOPIC = "helpTopic"; //$NON-NLS-1$
042:
043: // Launch bar image
044: public static final String RESTORE_WELCOME = "restoreWelcome"; //$NON-NLS-1$
045:
046: // Viewer images
047: public static final String INTRO_MODEL_LEAF = "leaf"; //$NON-NLS-1$
048: public static final String INTRO_MODEL_CONTAINER = "container"; //$NON-NLS-1$
049: public static final String OPEN_ITNRO_VIEW = "introView"; //$NON-NLS-1$
050:
051: public static final String CONFIG_EXTENSION = "configExtension";//$NON-NLS-1$
052:
053: // Image location
054: public static final String ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$
055:
056: /**
057: * Convenience method to create an image descriptor from the Intro plugin.
058: *
059: * Method assumes that images are under the "icons" directory, so don't
060: * append that directory name for "imageName".
061: */
062: public static ImageDescriptor createImageDescriptor(String imageName) {
063: return createImageDescriptor(Platform
064: .getBundle(IIntroConstants.PLUGIN_ID), ICONS_PATH
065: + imageName);
066: }
067:
068: /**
069: * Convenience method to create an image descriptor.
070: *
071: */
072: public static ImageDescriptor createImageDescriptor(Bundle bundle,
073: String imageName) {
074: try {
075: URL imageUrl = FileLocator.find(bundle,
076: new Path(imageName), null);
077: if (imageUrl != null) {
078: ImageDescriptor desc = ImageDescriptor
079: .createFromURL(imageUrl);
080: return desc;
081: }
082: } catch (Exception e) {
083: // Should never be here.
084: Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
085: }
086: Log
087: .warning("could not create Image Descriptor for: " + imageName //$NON-NLS-1$
088: + " in bundle: " + bundle.getSymbolicName()); //$NON-NLS-1$
089: return ImageDescriptor.getMissingImageDescriptor();
090: }
091:
092: /**
093: * Convenience method to create an image descriptor.
094: *
095: */
096: public static ImageDescriptor createImageDescriptor(IPath base,
097: String imageName) {
098: try {
099: URL imageUrl = new URL(base.append(imageName).toOSString());
100: if (imageUrl != null) {
101: ImageDescriptor desc = ImageDescriptor
102: .createFromURL(imageUrl);
103: return desc;
104: }
105: } catch (Exception e) {
106: // Should never be here.
107: Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
108: }
109: Log
110: .warning("could not create Image Descriptor for: " + imageName); //$NON-NLS-1$
111: return ImageDescriptor.getMissingImageDescriptor();
112: }
113:
114: /**
115: * Convenience method to create an image from the Intro plugin.
116: *
117: * Method assumes that images are under the "icons" directory, so don't
118: * append that directory name for "imageName".
119: */
120: public static Image createImage(String imageName) {
121: try {
122: ImageDescriptor imageDsc = createImageDescriptor(imageName);
123: return imageDsc.createImage();
124: } catch (Exception e) {
125: // Should never be here.
126: Log.error("could not create Image", e); //$NON-NLS-1$
127: return ImageDescriptor.getMissingImageDescriptor()
128: .createImage();
129: }
130: }
131:
132: /**
133: * Util method for image re-use in Intro Plugin.
134: *
135: * @param key
136: * @return
137: */
138: public static Image getImage(String key) {
139: // INTRO: Image registry should not have the same life span
140: // as the intro plug-in. It should be disposed when
141: // presentation is disposed, otherwise images will
142: // stay around once Inro has been loaded.
143: return IntroPlugin.getDefault().getVolatileImageRegistry().get(
144: key);
145: }
146:
147: public static boolean hasImage(String key) {
148: ImageRegistry registry = IntroPlugin.getDefault()
149: .getVolatileImageRegistry();
150: return (registry.getDescriptor(key) != null);
151: }
152:
153: /**
154: * Register an image descriptor in the Intro Plugin image registry. Has no
155: * effect if the key has already been registered.
156: *
157: * @param key
158: * @param imageName
159: */
160: public static void registerImage(String key, String imageName) {
161: ImageRegistry registry = IntroPlugin.getDefault()
162: .getVolatileImageRegistry();
163: if (registry.getDescriptor(key) != null)
164: // key has already been registered. do nothing.
165: return;
166: registry.put(key, createImageDescriptor(imageName));
167: }
168:
169: public static void registerImage(String key, Bundle bundle,
170: String imageName) {
171:
172: ImageRegistry registry = IntroPlugin.getDefault()
173: .getVolatileImageRegistry();
174: if (registry.getDescriptor(key) != null)
175: // key has already been registered. do nothing.
176: return;
177: registry.put(key, createImageDescriptor(bundle, imageName));
178: }
179:
180: public static void registerImage(String key, IPath base,
181: String imageName) {
182: ImageRegistry registry = IntroPlugin.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: }
|