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.universal.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.swt.widgets.Display;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.internal.intro.universal.IUniversalIntroConstants;
024: import org.eclipse.ui.internal.intro.universal.UniversalIntroPlugin;
025: import org.osgi.framework.Bundle;
026:
027: /**
028: * Convenience class for Images.
029: */
030: public final class ImageUtil {
031:
032: /**
033: * Image keys, to be used by plugin (intro) registry.
034: */
035: // Default images
036: public static final String DEFAULT_ROOT_LINK = "rootLink"; //$NON-NLS-1$
037:
038: public static final String DEFAULT_SMALL_ROOT_LINK = "rootLinkSmall"; //$NON-NLS-1$
039:
040: public static final String DEFAULT_FORM_BG = "formBg"; //$NON-NLS-1$
041:
042: public static final String DEFAULT_LINK = "link"; //$NON-NLS-1$
043:
044: // Standby images
045: public static final String BACK = "back"; //$NON-NLS-1$
046:
047: public static final String HELP_TOPIC = "helpTopic"; //$NON-NLS-1$
048:
049: // Launch bar image
050: public static final String RESTORE_WELCOME = "restoreWelcome"; //$NON-NLS-1$
051:
052: // Viewer images
053: public static final String INTRO_MODEL_LEAF = "leaf"; //$NON-NLS-1$
054:
055: public static final String INTRO_MODEL_CONTAINER = "container"; //$NON-NLS-1$
056:
057: public static final String OPEN_ITNRO_VIEW = "introView"; //$NON-NLS-1$
058:
059: public static final String CONFIG_EXTENSION = "configExtension";//$NON-NLS-1$
060:
061: // Image location
062: public static final String ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$
063:
064: /**
065: * Convenience method to create an image descriptor from the Intro plugin.
066: *
067: * Method assumes that images are under the "icons" directory, so don't
068: * append that directory name for "imageName".
069: */
070: public static ImageDescriptor createImageDescriptor(String imageName) {
071: return createImageDescriptor(Platform
072: .getBundle(IUniversalIntroConstants.PLUGIN_ID),
073: ICONS_PATH + imageName);
074: }
075:
076: /**
077: * Convenience method to create an image descriptor.
078: *
079: */
080: public static ImageDescriptor createImageDescriptor(Bundle bundle,
081: String imageName) {
082: try {
083: URL imageUrl = FileLocator.find(bundle,
084: new Path(imageName), null);
085: if (imageUrl != null) {
086: ImageDescriptor desc = ImageDescriptor
087: .createFromURL(imageUrl);
088: return desc;
089: }
090: } catch (Exception e) {
091: // Should never be here.
092: Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
093: }
094: Log
095: .warning("could not create Image Descriptor for: " + imageName //$NON-NLS-1$
096: + " in bundle: " + bundle.getSymbolicName()); //$NON-NLS-1$
097: return ImageDescriptor.getMissingImageDescriptor();
098: }
099:
100: /**
101: * Convenience method to create an image descriptor.
102: *
103: */
104: public static ImageDescriptor createImageDescriptor(IPath base,
105: String imageName) {
106: try {
107: URL imageUrl = new URL(base.append(imageName).toOSString());
108: if (imageUrl != null) {
109: ImageDescriptor desc = ImageDescriptor
110: .createFromURL(imageUrl);
111: return desc;
112: }
113: } catch (Exception e) {
114: // Should never be here.
115: Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
116: }
117: Log
118: .warning("could not create Image Descriptor for: " + imageName); //$NON-NLS-1$
119: return ImageDescriptor.getMissingImageDescriptor();
120: }
121:
122: /**
123: * Convenience method to create an image from the Intro plugin.
124: *
125: * Method assumes that images are under the "icons" directory, so don't
126: * append that directory name for "imageName".
127: */
128: public static Image createImage(String imageName) {
129: try {
130: ImageDescriptor imageDsc = createImageDescriptor(imageName);
131: return imageDsc.createImage();
132: } catch (Exception e) {
133: // Should never be here.
134: Log.error("could not create Image", e); //$NON-NLS-1$
135: return ImageDescriptor.getMissingImageDescriptor()
136: .createImage();
137: }
138: }
139:
140: /**
141: * Util method for image re-use in Intro Plugin.
142: *
143: * @param key
144: * @return
145: */
146: public static Image getImage(String key) {
147: // INTRO: Image registry should not have the same life span
148: // as the intro plug-in. It should be disposed when
149: // presentation is disposed, otherwise images will
150: // stay around once Inro has been loaded.
151: return UniversalIntroPlugin.getDefault()
152: .getVolatileImageRegistry().get(key);
153: }
154:
155: public static boolean hasImage(String key) {
156: ImageRegistry registry = UniversalIntroPlugin.getDefault()
157: .getVolatileImageRegistry();
158: return (registry.getDescriptor(key) != null);
159: }
160:
161: /**
162: * Register an image descriptor in the Intro Plugin image registry. Has no
163: * effect if the key has already been registered.
164: *
165: * @param key
166: * @param imageName
167: */
168: public static void registerImage(String key, String imageName) {
169: ImageRegistry registry = UniversalIntroPlugin.getDefault()
170: .getVolatileImageRegistry();
171: if (registry.getDescriptor(key) != null)
172: // key has already been registered. do nothing.
173: return;
174: registry.put(key, createImageDescriptor(imageName));
175: }
176:
177: public static void registerImage(String key, Bundle bundle,
178: String imageName) {
179:
180: ImageRegistry registry = UniversalIntroPlugin.getDefault()
181: .getVolatileImageRegistry();
182: if (registry.getDescriptor(key) != null)
183: // key has already been registered. do nothing.
184: return;
185: registry.put(key, createImageDescriptor(bundle, imageName));
186: }
187:
188: public static void registerImage(String key, IPath base,
189: String imageName) {
190: ImageRegistry registry = UniversalIntroPlugin.getDefault()
191: .getVolatileImageRegistry();
192: if (registry.getDescriptor(key) != null)
193: // key has already been registered. do nothing.
194: return;
195: registry.put(key, createImageDescriptor(base, imageName));
196: }
197:
198: /**
199: * Tests for high contrast mode. Returns false if not called from a
200: * UI thread (causes deadlocks during junit tests).
201: *
202: * @return whether or not the display is in high contrast mode
203: */
204: public static boolean isHighContrast() {
205: if (PlatformUI.isWorkbenchRunning()) {
206: Display display = PlatformUI.getWorkbench().getDisplay();
207: if (Display.getCurrent() == display) {
208: return display.getHighContrast();
209: }
210: }
211: return false;
212: }
213: }
|