1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-26 02:15:31 +02:00

Separated static from instance methods.

This commit is contained in:
David Dykstal 2006-04-27 17:35:47 +00:00
parent b71c9701ae
commit ce1cce7d19

View file

@ -48,52 +48,31 @@ import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
/**
* A base plugin class offering common operations.
*/
public abstract class SystemBasePlugin extends AbstractUIPlugin
{
/**
* Default folder for icons, relative to this plugin's install folder: "icons".
*/
private static final String ICON_PATH = "icons";
// static variables
private static SystemBasePlugin baseInst = null;
/**
* Logger object for logging messages for servicing purposes.
*/
protected static Logger log = null;
// instance variables
private Hashtable imageDescriptorRegistry = new Hashtable();
private ImageRegistry imageRegistry = null;
private boolean headless;
private boolean headlessSet;
// static variables
private static SystemBasePlugin baseInst = null;
/**
* Default folder for icons, relative to this plugin's install folder: "icons".
*/
protected static final String ICON_PATH = "icons";
/**
* Logger object for logging messages for servicing purposes.
*/
protected static Logger log = null;
/**
* Constructor.
*/
public SystemBasePlugin() {
super();
if (baseInst == null) {
baseInst = this;
}
headless = false;
headlessSet = false;
}
// ------------------------
// STATIC HELPER METHODS...
// ------------------------
/**
* Returns the singleton object representing the base plugin.
* @return the singleton object.
@ -102,14 +81,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return baseInst;
}
/**
* Returns the symbolic name of the bundle.
* @return the symbolic name of the bundle.
*/
public String getSymbolicName() {
return getBundle().getSymbolicName();
}
/**
* Returns the active workbench shell.
* @return the active workbench shell.
@ -181,132 +152,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return ResourcesPlugin.getWorkspace();
}
// ----------------------------
// NON-STATIC HELPER METHODS...
// ----------------------------
/**
* Return the fully qualified install directory for this plugin.
*/
protected IPath getInstallLocation() {
IPath prefix = null;
try
{
String filePath = Platform.resolve(getBundle().getEntry("/")).getPath();
prefix = new Path(filePath);
}
catch (Exception e)
{
prefix = new Path(getBundle().getEntry("/").getFile());
}
return prefix;
}
// -------------------------------------
// ABSTRACTUIPLUGIN LIFECYCLE METHODS...
// -------------------------------------
/**
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
// logger
if (log == null) {
log = LoggerFactory.getInst(this);
log.logInfo("Loading " + this.getClass());
}
}
/**
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
logDebugMessage(this.getClass().getName(), "SHUTDOWN");
LoggerFactory.freeInst(this);
super.stop(context);
}
/**
* Returns the Platform UI workbench.
* <p>
* This method exists as a convenience for plugin implementors. The
* workbench can also be accessed by invoking <code>PlatformUI.getWorkbench()</code>.
* </p>
* <p>
* This is an intercept of the AbstractUIPlugin method, so we can do a try/catch around
* it, as it will throw an exception if we are running headless, in which case the
* workbench has not even been started.
* </p>
*/
public IWorkbench getWorkbench()
{
IWorkbench wb = null;
if (headlessSet && headless) // already been here?
return wb;
try {
wb = PlatformUI.getWorkbench();
//wb = super.getWorkbench();
headless = false;
}
catch (Exception exc)
{
/*
IDEWorkbenchAdvisor advisor = new IDEWorkbenchAdvisor();
PlatformUI.createAndRunWorkbench(Display.getDefault(), advisor);
try
{
wb = super.getWorkbench();
}
catch (Exception e)
{
headless = true;
}
*/
headless = true;
}
headlessSet = true;
return wb;
}
// ----------------------------------
// ICON METHODS...
// ----------------------------------
/**
* Initialize the image registry by declaring all of the required
* graphics. Typically this is a series of calls to putImageInRegistry.
* Use getIconPath() to qualify the file name of the icon with its
* relative path.
*/
protected abstract void initializeImageRegistry();
/**
* Parse the given message file that is in the plugin into memory, into a SystemMessageFile object.
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public final SystemMessageFile loadMessageFile(String fileName)
{
SystemMessageFile mf = null;
try
{
IPath path = new Path("$nl$/"+fileName);
URL url = Platform.find(getBundle(), path);
if (url != null) {
url = Platform.resolve(url);
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), getBundle().getEntry("/").getPath());
}
} catch (Exception exc)
{
logError("Error loading message file " + fileName ,exc);
}
return mf;
}
/**
* Helper to get the typical icons path ... usually just "icons/".
*/
@ -315,36 +160,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return ICON_PATH;
}
/**
* Helper method to put an image into the registry
* @param id - an arbitrary ID to assign to this image. Used later when retrieving it.
* @param fileName - the name of the icon file, with extension, relative to this plugin's folder.
*/
protected ImageDescriptor putImageInRegistry(String id, String fileName)
{
ImageDescriptor fid = getPluginImage(fileName);
imageRegistry.put(id, fid);
// Because ImageRegistry only allows you to get an image, and not an ImageDescriptor,
// we have to redundantly save the image descriptors for cases when they are needed,
// such as in WizardPage....
imageDescriptorRegistry.put(id, fid);
if (imageRegistry.get(id) == null)
logError("Error loading image: " + fileName);
return fid;
}
/**
* Retrieve image in this plugin's directory tree, given its file name.
* The file name should be qualified relative to this plugin's bundle. Eg "icons/myicon.gif"
*/
public ImageDescriptor getPluginImage(String fileName)
{
return getPluginImage(getBundle(), fileName);
}
/**
* Retrieve image in any plugin's directory tree, given its file name.
* The file name should be qualified relative to this plugin's bundle. Eg "icons/myicon.gif"
@ -356,83 +171,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return descriptor;
}
/**
* Easy retrieval of image by id
*/
public Image getImage(String key)
{
if (imageRegistry == null) {
imageRegistry = new ImageRegistry();
initializeImageRegistry();
}
Image image = null;
try
{
image = imageRegistry.get(key);
}
catch (Throwable t)
{
logError("...error retrieving image for key: " + key);
}
return image;
}
/**
* Easy retrieval of image descriptor by id
*/
public ImageDescriptor getImageDescriptor(String key)
{
if (imageRegistry == null)
{
imageRegistry = new ImageRegistry();
initializeImageRegistry();
}
ImageDescriptor image = (ImageDescriptor)imageDescriptorRegistry.get(key);
return image;
}
/**
* Returns an image descriptor from the base IDE.
* @see org.eclipse.ui.views.navigator.ResourceNavigatorActionGroup#getImageDescriptor(java.lang.String)
*/
public ImageDescriptor getImageDescriptorFromIDE(String relativePath)
{
String iconPath = "icons/full/"; //$NON-NLS-1$
String key = iconPath + relativePath;
ImageDescriptor descriptor = (ImageDescriptor)imageDescriptorRegistry.get(key);
if (descriptor == null) {
String[] bundleNames = new String[] {"org.eclipse.ui", "org.eclipse.ui.ide"};
for (int i = 0; (i < bundleNames.length) && (descriptor == null); i++) {
String bundleName = bundleNames[i];
Bundle bundle = Platform.getBundle(bundleName);
URL url = bundle.getResource(key);
if (url != null) {
descriptor = ImageDescriptor.createFromURL(url);
}
}
if (descriptor == null) {
descriptor = ImageDescriptor.getMissingImageDescriptor();
}
imageDescriptorRegistry.put(key, descriptor);
}
return descriptor;
// try {
// Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
// URL installURL = bundle.getEntry("/");
// URL url = new URL(installURL, key);
// image = ImageDescriptor.createFromURL(url);
// imageDescriptorRegistry.put(key, image);
// return image;
// } catch (MalformedURLException e) {
// // should not happen
// return ImageDescriptor.getMissingImageDescriptor();
// }
}
// ----------------------------------------
// TRANSLATABLE RESOURCE-RELATED METHODS...
// ----------------------------------------
@ -493,7 +231,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return rb;
}
/**
* Returns the plugin.properties resource bundle associated with the specified plugin descriptor
*
@ -515,8 +252,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return rb;
}
/**
* Sets the default resource bundle for handling cases where strings aren't translated. Called by child class in their
* constructor, say, to load in their resource bundle. Handles
@ -575,64 +310,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return rb;
}
/**
* Sets the resource bundle. Called by child class in their
* constructor, say, to load in their resource bundle. Handles
* rare case when not found by telling user, then dying.
* Note: This is NOT to be used for plugin.properties since Eclipse handles that file differently.
* @param name of .properties file, without the '.properties' specified
* @return ResourceBundle if loaded successfully, null if not.
*/
public final ResourceBundle loadResourceBundle(String fileName)
{
ResourceBundle rb = null;
try {
IPath path = new Path("$nl$/"+fileName+".properties");
URL url = Platform.find(getBundle(), path);
if ( url != null )
{
InputStream in = url.openStream();
rb = new PropertyResourceBundle(in);
in.close();
}
else
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",null);
} catch (IOException e) {
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",e);
}
return rb;
}
/**
* Sets the default resource bundle (so that untranslated strings can be obtained). Called by child class in their
* constructor, say, to load in their resource bundle. Handles
* rare case when not found by telling user, then dying.
* Note: This is NOT to be used for plugin.properties since Eclipse handles that file differently.
* @param name of .properties file, without the '.properties' specified
* @return ResourceBundle if loaded successfully, null if not.
*/
public final ResourceBundle loadDefaultResourceBundle(String fileName)
{
ResourceBundle rb = null;
try {
IPath path = new Path(fileName+".properties");
URL url = Platform.find(getBundle(), path);
if ( url != null )
{
InputStream in = url.openStream();
rb = new PropertyResourceBundle(in);
in.close();
}
else
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",null);
} catch (IOException e) {
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",e);
}
return rb;
}
/**
* Get a string from a given resource bundle.
* If not found, stack trace info is placed in the
@ -650,6 +327,7 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
}
return null;
}
/**
* Get a string from a given resource bundle, with an english string to
* use a default if the given key is not found.
@ -771,30 +449,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return mf;
}
/**
* Parse the given message file that is in the plugin into memory, into a SystemMessageFile object.
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public final SystemMessageFile loadDefaultMessageFile(String fileName)
{
SystemMessageFile mf = null;
try
{
IPath path = new Path(fileName);
URL url = Platform.find(getBundle(), path);
if (url != null) {
url = Platform.resolve(url);
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), getBundle().getEntry("/").getPath());
}
} catch (Exception exc)
{
logError("Error loading message file " + fileName ,exc);
}
return mf;
}
/**
* Retrieve a message from a message file.
* @param msgFile - the system message file containing the message.
@ -825,6 +479,7 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
{
return msgFile.scanForDuplicates();
}
/**
* Generate HTML from this plugin's message file. This is handy for documentation purposes.
* This just calls the {@link org.eclipse.rse.ui.messages.SystemMessageFile#printHTML(String)}
@ -837,21 +492,6 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
return msgFile.printHTML(fullyQualifiedTargetFile);
}
/**
* Put up an error message when a programming error is detected.
* Please note this should never happen in production so we don't translate!
*/
public void showProgrammerErrorMessage(String msg)
{
org.eclipse.swt.widgets.MessageBox mb = new org.eclipse.swt.widgets.MessageBox(null);
//mb.setTitle("Remote Systems Programming Error");
String errmsg = "Programming Error: " + msg;
mb.setMessage(errmsg);
mb.open();
logError(errmsg);
}
// -----------------
// LOGGER METHODS...
// -----------------
@ -912,6 +552,10 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
log.logInfo(message);
}
// -----------------
// LOGGER METHODS...
// -----------------
/**
* Helper method for logging warnings to the RSE-style logging file.
* This file is located in the .metadata subfolder for this plugin.
@ -979,6 +623,366 @@ public abstract class SystemBasePlugin extends AbstractUIPlugin
}
}
/**
* Constructor.
*/
public SystemBasePlugin() {
super();
if (baseInst == null) {
baseInst = this;
}
headless = false;
headlessSet = false;
}
// ------------------------
// STATIC HELPER METHODS...
// ------------------------
/**
* Returns the symbolic name of the bundle.
* @return the symbolic name of the bundle.
*/
public String getSymbolicName() {
return getBundle().getSymbolicName();
}
/**
* Return the fully qualified install directory for this plugin.
*/
protected IPath getInstallLocation() {
IPath prefix = null;
try
{
String filePath = Platform.resolve(getBundle().getEntry("/")).getPath();
prefix = new Path(filePath);
}
catch (Exception e)
{
prefix = new Path(getBundle().getEntry("/").getFile());
}
return prefix;
}
// -------------------------------------
// ABSTRACTUIPLUGIN LIFECYCLE METHODS...
// -------------------------------------
/**
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
// logger
if (log == null) {
log = LoggerFactory.getInst(this);
log.logInfo("Loading " + this.getClass());
}
}
/**
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
logDebugMessage(this.getClass().getName(), "SHUTDOWN");
LoggerFactory.freeInst(this);
super.stop(context);
}
/**
* Returns the Platform UI workbench.
* <p>
* This method exists as a convenience for plugin implementors. The
* workbench can also be accessed by invoking <code>PlatformUI.getWorkbench()</code>.
* </p>
* <p>
* This is an intercept of the AbstractUIPlugin method, so we can do a try/catch around
* it, as it will throw an exception if we are running headless, in which case the
* workbench has not even been started.
* </p>
*/
public IWorkbench getWorkbench()
{
IWorkbench wb = null;
if (headlessSet && headless) // already been here?
return wb;
try {
wb = PlatformUI.getWorkbench();
//wb = super.getWorkbench();
headless = false;
}
catch (Exception exc)
{
/*
IDEWorkbenchAdvisor advisor = new IDEWorkbenchAdvisor();
PlatformUI.createAndRunWorkbench(Display.getDefault(), advisor);
try
{
wb = super.getWorkbench();
}
catch (Exception e)
{
headless = true;
}
*/
headless = true;
}
headlessSet = true;
return wb;
}
// ----------------------------------
// ICON METHODS...
// ----------------------------------
/**
* Initialize the image registry by declaring all of the required
* graphics. Typically this is a series of calls to putImageInRegistry.
* Use getIconPath() to qualify the file name of the icon with its
* relative path.
*/
protected abstract void initializeImageRegistry();
/**
* Helper method to put an image into the registry
* @param id - an arbitrary ID to assign to this image. Used later when retrieving it.
* @param fileName - the name of the icon file, with extension, relative to this plugin's folder.
*/
protected ImageDescriptor putImageInRegistry(String id, String fileName)
{
ImageDescriptor fid = getPluginImage(fileName);
imageRegistry.put(id, fid);
// Because ImageRegistry only allows you to get an image, and not an ImageDescriptor,
// we have to redundantly save the image descriptors for cases when they are needed,
// such as in WizardPage....
imageDescriptorRegistry.put(id, fid);
if (imageRegistry.get(id) == null)
logError("Error loading image: " + fileName);
return fid;
}
/**
* Retrieve image in this plugin's directory tree, given its file name.
* The file name should be qualified relative to this plugin's bundle. Eg "icons/myicon.gif"
*/
public ImageDescriptor getPluginImage(String fileName)
{
return getPluginImage(getBundle(), fileName);
}
/**
* Easy retrieval of image by id
*/
public Image getImage(String key)
{
if (imageRegistry == null) {
imageRegistry = new ImageRegistry();
initializeImageRegistry();
}
Image image = null;
try
{
image = imageRegistry.get(key);
}
catch (Throwable t)
{
logError("...error retrieving image for key: " + key);
}
return image;
}
/**
* Easy retrieval of image descriptor by id
*/
public ImageDescriptor getImageDescriptor(String key)
{
if (imageRegistry == null)
{
imageRegistry = new ImageRegistry();
initializeImageRegistry();
}
ImageDescriptor image = (ImageDescriptor)imageDescriptorRegistry.get(key);
return image;
}
/**
* Returns an image descriptor from the base IDE.
* @see org.eclipse.ui.views.navigator.ResourceNavigatorActionGroup#getImageDescriptor(java.lang.String)
*/
public ImageDescriptor getImageDescriptorFromIDE(String relativePath)
{
String iconPath = "icons/full/"; //$NON-NLS-1$
String key = iconPath + relativePath;
ImageDescriptor descriptor = (ImageDescriptor)imageDescriptorRegistry.get(key);
if (descriptor == null) {
String[] bundleNames = new String[] {"org.eclipse.ui", "org.eclipse.ui.ide"};
for (int i = 0; (i < bundleNames.length) && (descriptor == null); i++) {
String bundleName = bundleNames[i];
Bundle bundle = Platform.getBundle(bundleName);
URL url = bundle.getResource(key);
if (url != null) {
descriptor = ImageDescriptor.createFromURL(url);
}
}
if (descriptor == null) {
descriptor = ImageDescriptor.getMissingImageDescriptor();
}
imageDescriptorRegistry.put(key, descriptor);
}
return descriptor;
// try {
// Bundle bundle = Platform.getBundle(PlatformUI.PLUGIN_ID);
// URL installURL = bundle.getEntry("/");
// URL url = new URL(installURL, key);
// image = ImageDescriptor.createFromURL(url);
// imageDescriptorRegistry.put(key, image);
// return image;
// } catch (MalformedURLException e) {
// // should not happen
// return ImageDescriptor.getMissingImageDescriptor();
// }
}
// ----------------------------------------
// TRANSLATABLE RESOURCE-RELATED METHODS...
// ----------------------------------------
/**
* Sets the resource bundle. Called by child class in their
* constructor, say, to load in their resource bundle. Handles
* rare case when not found by telling user, then dying.
* Note: This is NOT to be used for plugin.properties since Eclipse handles that file differently.
* @param name of .properties file, without the '.properties' specified
* @return ResourceBundle if loaded successfully, null if not.
*/
public final ResourceBundle loadResourceBundle(String fileName)
{
ResourceBundle rb = null;
try {
IPath path = new Path("$nl$/"+fileName+".properties");
URL url = Platform.find(getBundle(), path);
if ( url != null )
{
InputStream in = url.openStream();
rb = new PropertyResourceBundle(in);
in.close();
}
else
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",null);
} catch (IOException e) {
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",e);
}
return rb;
}
/**
* Sets the default resource bundle (so that untranslated strings can be obtained). Called by child class in their
* constructor, say, to load in their resource bundle. Handles
* rare case when not found by telling user, then dying.
* Note: This is NOT to be used for plugin.properties since Eclipse handles that file differently.
* @param name of .properties file, without the '.properties' specified
* @return ResourceBundle if loaded successfully, null if not.
*/
public final ResourceBundle loadDefaultResourceBundle(String fileName)
{
ResourceBundle rb = null;
try {
IPath path = new Path(fileName+".properties");
URL url = Platform.find(getBundle(), path);
if ( url != null )
{
InputStream in = url.openStream();
rb = new PropertyResourceBundle(in);
in.close();
}
else
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",null);
} catch (IOException e) {
logError("SystemBasePlugin - try for resource bundle " + fileName + " not successful!",e);
}
return rb;
}
/**
* Parse the given message file that is in the plugin into memory, into a SystemMessageFile object.
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public final SystemMessageFile loadMessageFile(String fileName)
{
SystemMessageFile mf = null;
try
{
IPath path = new Path("$nl$/"+fileName);
URL url = Platform.find(getBundle(), path);
if (url != null) {
url = Platform.resolve(url);
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), getBundle().getEntry("/").getPath());
}
} catch (Exception exc)
{
logError("Error loading message file " + fileName ,exc);
}
return mf;
}
/**
* Parse the given message file that is in the plugin into memory, into a SystemMessageFile object.
* @param fileName - unqualified name of the .xml message file, inluding the .xml extension.
* @return SystemMessageFile (null if unable to load the file)
*/
public final SystemMessageFile loadDefaultMessageFile(String fileName)
{
SystemMessageFile mf = null;
try
{
IPath path = new Path(fileName);
URL url = Platform.find(getBundle(), path);
if (url != null) {
url = Platform.resolve(url);
mf = new SystemUIMessageFile(/*url.toString()*/url.getPath(), getBundle().getEntry("/").getPath());
}
} catch (Exception exc)
{
logError("Error loading message file " + fileName ,exc);
}
return mf;
}
/**
* Put up an error message when a programming error is detected.
* Please note this should never happen in production so we don't translate!
*/
public void showProgrammerErrorMessage(String msg)
{
org.eclipse.swt.widgets.MessageBox mb = new org.eclipse.swt.widgets.MessageBox(null);
//mb.setTitle("Remote Systems Programming Error");
String errmsg = "Programming Error: " + msg;
mb.setMessage(errmsg);
mb.open();
logError(errmsg);
}
// -----------------
// LOGGER METHODS...
// -----------------
/**
* Get the logger for this plugin. You should not have to directly access
* the logger, since helper methods are already provided in this class.