📄 eclipsemecoreplugin.java
字号:
/**
* Copyright (c) 2003-2005 Craig Setera
* All Rights Reserved.
* Licensed under the Eclipse Public License - v 1.0
* For more information see http://www.eclipse.org/legal/epl-v10.html
*/
package eclipseme.core.internal;
import java.io.File;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IStatusHandler;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.hook.sourceMapper.SourceMapperAccess;
import eclipseme.core.internal.preprocessor.PreprocessedSourceMapper;
import eclipseme.core.model.LibrarySpecification;
import eclipseme.core.model.Version;
/**
* The main plugin class to be used in the workbench.
* <p />
* Copyright (c) 2003-2005 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.25 $
* <br>
* $Date: 2007/01/20 19:23:00 $
* <br>
* @author Craig Setera
*/
public class EclipseMECorePlugin
extends Plugin
implements IEclipseMECoreConstants
{
private class MigrationJob extends Job {
/** Constructor */
MigrationJob() {
super("EclipseME Migration");
setPriority(SHORT);
setSystem(true);
}
/**
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
*/
protected IStatus run(IProgressMonitor monitor) {
IStatus status = null;
try {
doMigration(monitor);
status = OK_STATUS;
} catch (CoreException e) {
status = e.getStatus();
}
return status;
}
}
//The shared instance.
private static EclipseMECorePlugin plugin;
/** Status code for which a UI prompter is registered. */
private static final IStatus OK_STATUS =
new Status(IStatus.OK, "eclipseme.core", 0, "", null);
/** Status code for which a UI prompter is registered. */
private static final IStatus PROMPTER_STATUS = new Status(
IStatus.INFO,
"org.eclipse.debug.ui", // TODO This should probably not be done....
200, "", null);
//Resource bundle.
private ResourceBundle resourceBundle;
// The configuration specifications provided by extension points
private LibrarySpecification[] configSpecs;
// The profile specifications provided by extension points
private LibrarySpecification[] profileSpecs;
/**
* The constructor.
*/
public EclipseMECorePlugin() {
super();
plugin = this;
try {
resourceBundle =
ResourceBundle.getBundle("eclipseme.core.EclipseMEPluginResources");
} catch (MissingResourceException x) {
resourceBundle = null;
}
}
/**
* Return the configuration specifications provided by the plugin
* extension points.
*
* @return the configuration specifications
* @throws CoreException
*/
public static LibrarySpecification[] getConfigurationSpecifications()
throws CoreException
{
EclipseMECorePlugin plugin = getDefault();
if (plugin.configSpecs == null) {
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(
IEclipseMECoreConstants.PLUGIN_ID,
IEclipseMECoreConstants.J2ME_CONFIGURATIONS_ID);
IConfigurationElement[] configElements = point.getConfigurationElements();
plugin.configSpecs = new LibrarySpecification[configElements.length];
for (int i = 0; i < configElements.length; i++) {
plugin.configSpecs[i] = createComponentSpecification(configElements[i]);
}
}
return plugin.configSpecs;
}
/**
* Returns the shared instance.
*/
public static EclipseMECorePlugin getDefault() {
return plugin;
}
/**
* Get the deployment directory name the user has specified in the preferences.
*
* @return
*/
public static String getDeploymentDirectoryName() {
return getDefault().getPluginPreferences().getString(PREF_DEPLOYMENT_DIR);
}
/**
* Return the current version associated with this plugin.
* @return the plugin version
*/
public static String getPluginVersion() {
Bundle bundle = EclipseMECorePlugin.getDefault().getBundle();
return (String) bundle.getHeaders().get(Constants.BUNDLE_VERSION);
}
/**
* Return the profile specifications provided by the plugin
* extension points.
*
* @return the profile specifications
* @throws CoreException
*/
public static LibrarySpecification[] getProfileSpecifications()
throws CoreException
{
EclipseMECorePlugin plugin = getDefault();
if (plugin.profileSpecs == null) {
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(
IEclipseMECoreConstants.PLUGIN_ID,
IEclipseMECoreConstants.J2ME_PROFILES_ID);
IConfigurationElement[] configElements = point.getConfigurationElements();
plugin.profileSpecs = new LibrarySpecification[configElements.length];
for (int i = 0; i < configElements.length; i++) {
plugin.profileSpecs[i] = createComponentSpecification(configElements[i]);
}
}
return plugin.profileSpecs;
}
/**
* Return the File instance representing the Proguard implementation
* jar file as defined by the user preferences.
* This File is not guaranteed to exist.
*
* @return
*/
public static File getProguardJarFile() {
String proguardDirPref = getDefault().getPluginPreferences().getString(PREF_PROGUARD_DIR);
File proguardDir = new File(proguardDirPref);
File proguardLibDir = new File(proguardDir, "lib");
File proguardJar = new File(proguardLibDir, PROGUARD_JAR);
return proguardJar;
}
/**
* Return the preferences that are specific to the project and plugin.
*
* @param context
* @return
*/
public static IEclipsePreferences getProjectPreferences(IProject context) {
ProjectScope projectScope = new ProjectScope(context);
return projectScope.getNode(IEclipseMECoreConstants.PLUGIN_ID);
}
/**
* Return a boolean preference scoped to the specified project where
* possible, otherwise falling back to instance scope.
*
* @param project
* @param key
* @return
*/
public static boolean getProjectBooleanPreference(IProject project, String key) {
IScopeContext[] searchContexts = new IScopeContext[] {
new ProjectScope(project),
new InstanceScope(),
new DefaultScope(),
};
IPreferencesService service = Platform.getPreferencesService();
return service.getBoolean(IEclipseMECoreConstants.PLUGIN_ID, key, false, searchContexts);
}
/**
* Return a String preference scoped to the specified project where
* possible, otherwise falling back to instance scope.
*
* @param project
* @param key
* @return
*/
public static String getProjectStringPreference(IProject project, String key) {
IScopeContext[] searchContexts = new IScopeContext[] {
new ProjectScope(project),
new InstanceScope(),
new DefaultScope(),
};
IPreferencesService service = Platform.getPreferencesService();
return service.getString(IEclipseMECoreConstants.PLUGIN_ID, key, null, searchContexts);
}
/**
* Get the resources directory name the user has specified in the preferences.
* This directory will automatically be added to the emulator classpath when
* running midlets from source.
*
* @return
*/
public static String getResourcesDirectoryName() {
return getDefault().getPluginPreferences().getString(PREF_RESOURCES_DIR);
}
/**
* Create a component specification for the specified configuration
* element
* @param element
* @return
* @throws CoreException
*/
private static LibrarySpecification createComponentSpecification(IConfigurationElement element)
throws CoreException
{
String id = element.getAttribute("id");
String name = element.getAttribute("name");
String versionString = element.getAttribute("version");
Version version = new Version(versionString);
LibrarySpecification specification = new LibrarySpecification();
specification.setIdentifier(id);
specification.setName(name);
specification.setVersion(version);
return specification;
}
/**
* Returns the workspace instance.
*/
public static IWorkspace getWorkspace() {
return ResourcesPlugin.getWorkspace();
}
/**
* Get the verified output directory name the user has
* specified in the preferences.
*
* @return
*/
public static String getVerifiedOutputDirectoryName() {
return getDefault().getPluginPreferences().getString(PREF_VERIFIED_DIR);
}
/**
* Log the specified message.
*/
public static void log(int severity, String message) {
log(severity, message, null);
}
/**
* Log the specified exception.
*/
public static void log(int severity, Throwable throwable) {
log(severity, throwable.getMessage(), throwable);
}
/**
* Log the specified message and exception
*/
public static void log(int severity, String message, Throwable throwable) {
if (message == null) message = throwable.getMessage();
if (message == null) message = "No Message";
EclipseMECorePlugin plugin = EclipseMECorePlugin.getDefault();
String id = IEclipseMECoreConstants.PLUGIN_ID;
Status status =
new Status(severity, id, IStatus.OK, message, throwable);
plugin.getLog().log(status);
}
/**
* Creates a new status object for our plugin. The created status has no children.
*
* @param severity the severity; one of <code>OK</code>,
* <code>ERROR</code>, <code>INFO</code>, or <code>WARNING</code>
* @param code the plug-in-specific status code, or <code>OK</code>
* @param message a human-readable message, localized to the
* current locale
*/
public static IStatus newStatus(int severity, int code, String message)
{
return newStatus(severity, code, message, null);
}
/**
* Creates a new status object for our plugin. The created status has no children.
*
* @param severity the severity; one of <code>OK</code>,
* <code>ERROR</code>, <code>INFO</code>, or <code>WARNING</code>
* @param code the plug-in-specific status code, or <code>OK</code>
* @param message a human-readable message, localized to the
* current locale
* @param exception a low-level exception, or <code>null</code> if not
* applicable
*/
public static IStatus newStatus(int severity, int code, String message, Throwable exception)
{
return new Status(severity, PLUGIN_ID, code, message, exception);
}
/**
* Recursively set the resources in the specified container and all resources
* within that container as derived.
*
* @param container
* @throws CoreException
*/
public static void setResourcesAsDerived(IContainer container)
throws CoreException
{
if (container.exists()) {
// Mark this folder first...
container.setDerived(true);
// Recursively handle the members of the directory
IResource[] resources = container.members();
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];
if (resource instanceof IContainer) {
setResourcesAsDerived((IContainer) resource);
} else {
resource.setDerived(true);
}
}
}
}
/**
* Attempt to prompt on a status object. If prompting fails,
* a CoreException will be thrown.
*
* @param status
* @param source
* @return
* @throws CoreException
*/
public static Object statusPrompt(IStatus status, Object source)
throws CoreException
{
Object result = null;
IStatusHandler prompterStatus =
DebugPlugin.getDefault().getStatusHandler(PROMPTER_STATUS);
if (prompterStatus == null) {
// if there is no handler, throw the exception
throw new CoreException(status);
} else {
result = prompterStatus.handleStatus(status, source);
}
return result;
}
/**
* Throw a new CoreException wrapped around the specified
* exception.
*
* @param severity
* @param code
* @param exception
* @throws CoreException
*/
public static void throwCoreException(
int severity,
int code,
Throwable exception)
throws CoreException
{
// Make sure we create a valid status object
String message = null;
if (exception != null) {
message = exception.getMessage();
}
if (message == null) message = "[No Message]";
IStatus status = new Status(
severity,
IEclipseMECoreConstants.PLUGIN_ID,
code,
message,
exception);
throw new CoreException(status);
}
/**
* Throw a new CoreException wrapped around the specified
* String.
*
* @param severity
* @param code
* @param message
* @throws CoreException
*/
public static void throwCoreException(
int severity,
int code,
String message)
throws CoreException
{
if (message == null) message = "[No Message]";
IStatus status = new Status(
severity,
IEclipseMECoreConstants.PLUGIN_ID,
code,
message,
null);
throw new CoreException(status);
}
/**
* Returns the plugin's resource bundle,
*
* @uml.property name="resourceBundle"
*/
public ResourceBundle getResourceBundle() {
return resourceBundle;
}
/**
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
// Install the preprocessor source mapper
SourceMapperAccess.setSourceMapper(new PreprocessedSourceMapper());
// Do version to version migration
(new MigrationJob()).schedule(5000L);
}
/**
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
super.stop(context);
}
/**
* Execute the necessary migration steps.
*
* @param monitor
* @throws CoreException
*/
private void doMigration(IProgressMonitor monitor)
throws CoreException
{
ResourcesPlugin.getWorkspace().run(new PreprocessedProjectMigrationRunnable(), monitor);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -