001: package org.eclipse.ui.examples.presentation;
002:
003: import java.net.MalformedURLException;
004: import java.net.URL;
005: import java.util.MissingResourceException;
006: import java.util.ResourceBundle;
007: import org.eclipse.jface.resource.ImageDescriptor;
008: import org.eclipse.jface.resource.ImageRegistry;
009: import org.eclipse.swt.graphics.Image;
010: import org.eclipse.ui.plugin.AbstractUIPlugin;
011: import org.osgi.framework.BundleContext;
012:
013: /**
014: * The main plugin class to be used in the desktop.
015: */
016: public class PresentationPlugin extends AbstractUIPlugin {
017: //The shared instance.
018: private static PresentationPlugin plugin;
019: //Resource bundle.
020: private ResourceBundle resourceBundle;
021:
022: /**
023: * The constructor.
024: */
025: public PresentationPlugin() {
026: super ();
027: plugin = this ;
028: try {
029: resourceBundle = ResourceBundle
030: .getBundle("org.eclipse.ui.examples.presentation.PresentationPluginResources"); //$NON-NLS-1$
031: } catch (MissingResourceException x) {
032: resourceBundle = null;
033: }
034: }
035:
036: /**
037: * Returns the given image. The image will be managed by the plugin's
038: * image registry.
039: *
040: * @param imageName a pathname relative to the icons directory of
041: * this project.
042: */
043: public Image getImage(String imageName) {
044: ImageRegistry reg = getImageRegistry();
045:
046: Image result = reg.get(imageName);
047:
048: if (result != null) {
049: return result;
050: }
051:
052: result = getImageDescriptor(imageName).createImage();
053:
054: reg.put(imageName, result);
055:
056: return result;
057: }
058:
059: /**
060: * Returns the given image descriptor. The caller will be responsible
061: * for deallocating the image if it creates the image from the descriptor
062: *
063: * @param imageName is a pathname relative to the icons directory
064: * within this project.
065: */
066: public ImageDescriptor getImageDescriptor(String imageName) {
067: ImageDescriptor desc;
068: try {
069: desc = ImageDescriptor.createFromURL(new URL(plugin
070: .getBundle().getEntry("/"), //$NON-NLS-1$
071: "icons/" + imageName)); //$NON-NLS-1$
072: } catch (MalformedURLException e) {
073: desc = ImageDescriptor.getMissingImageDescriptor();
074: }
075:
076: return desc;
077: }
078:
079: /**
080: * This method is called upon plug-in activation
081: */
082: public void start(BundleContext context) throws Exception {
083: super .start(context);
084: }
085:
086: /**
087: * This method is called when the plug-in is stopped
088: */
089: public void stop(BundleContext context) throws Exception {
090: super .stop(context);
091: }
092:
093: /**
094: * Returns the shared instance.
095: */
096: public static PresentationPlugin getDefault() {
097: return plugin;
098: }
099:
100: /**
101: * Returns the string from the plugin's resource bundle,
102: * or 'key' if not found.
103: */
104: public static String getResourceString(String key) {
105: ResourceBundle bundle = PresentationPlugin.getDefault()
106: .getResourceBundle();
107: try {
108: return (bundle != null) ? bundle.getString(key) : key;
109: } catch (MissingResourceException e) {
110: return key;
111: }
112: }
113:
114: /**
115: * Returns the plugin's resource bundle,
116: */
117: public ResourceBundle getResourceBundle() {
118: return resourceBundle;
119: }
120: }
|