From: <jsa...@us...> - 2008-11-12 19:40:37
|
Revision: 93 http://flexotask.svn.sourceforge.net/flexotask/?rev=93&view=rev Author: jsauerbach Date: 2008-11-12 19:40:34 +0000 (Wed, 12 Nov 2008) Log Message: ----------- Respond immediately when the compliance level of the project changes by adding or removing the generic library from the classpath container Modified Paths: -------------- trunk/flexotask-editor/src/com/ibm/realtime/flexotask/editor/FlexotaskClasspathInitializer.java Modified: trunk/flexotask-editor/src/com/ibm/realtime/flexotask/editor/FlexotaskClasspathInitializer.java =================================================================== --- trunk/flexotask-editor/src/com/ibm/realtime/flexotask/editor/FlexotaskClasspathInitializer.java 2008-11-12 17:01:21 UTC (rev 92) +++ trunk/flexotask-editor/src/com/ibm/realtime/flexotask/editor/FlexotaskClasspathInitializer.java 2008-11-12 19:40:34 UTC (rev 93) @@ -20,16 +20,24 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ProjectScope; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; +import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; import org.eclipse.jdt.core.ClasspathContainerInitializer; import org.eclipse.jdt.core.IClasspathContainer; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.pde.core.plugin.IPluginBase; import org.eclipse.pde.core.plugin.IPluginElement; import org.eclipse.pde.core.plugin.IPluginExtension; @@ -38,6 +46,9 @@ import org.eclipse.pde.core.plugin.IPluginObject; import org.eclipse.pde.core.plugin.ISharedPluginModel; import org.eclipse.pde.core.plugin.PluginRegistry; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; import org.osgi.framework.Bundle; import org.osgi.framework.Version; @@ -48,7 +59,7 @@ /** The id of this container */ public static final String CONTAINER_ID= "com.ibm.realtime.flexotask.editor.classpath"; - /** The two-segment path used to designate this container in some contexts */ + /** The two-segment path used to designate this container in some contexts */ public final static IPath CONTAINER_PATH = new Path(CONTAINER_ID).append("1.0"); /** The id of the realtime analysis plugin and hence its bundle */ @@ -59,18 +70,23 @@ /** The id of the flexotask generic plugin and hence its bundle */ public final static String FLEXOTASK_GENERIC_PLUGIN = "com.ibm.realtime.flexotask.generic"; - + /** File that is used as the debugging assistance file (built by all flexotask projects that may be put in a classpath container) */ public static final String DEBUG_FILE = "debug.zip"; + /** The preference key that indicates the Java compliance level */ + private final static String COMPLIANCE_KEY = "org.eclipse.jdt.core.compiler.compliance"; + /** Special flag to allow the mechanism for running under a debugger itself to be debugged */ private static final boolean DEBUG_DEBUGGING = Boolean.valueOf(System.getProperty("FLEXOTASK_DEBUG_DEBUGGING")); - private static final String SOURCE_EXTENSION_POINT = "org.eclipse.pde.core.source"; + /** The extension point for adding sources to the PDE */ + private static final String SOURCE_EXTENSION_POINT = "org.eclipse.pde.core.source"; + /** The set of source paths provided by the PDE */ private static IPath[] sourcePaths; - /** + /** * A 0-argument constructor is required */ public FlexotaskClasspathInitializer() {} @@ -83,8 +99,21 @@ !CONTAINER_ID.equals(containerPath.segment(0))) { return; } - List<IClasspathEntry> entryList = new ArrayList<IClasspathEntry>(); - String compliance = project.getOption("org.eclipse.jdt.core.compiler.compliance", true); + reInitialize(containerPath, project); + IEclipsePreferences preferences = new ProjectScope(project.getProject()).getNode(JavaCore.PLUGIN_ID); + preferences.addPreferenceChangeListener(new ComplianceChangeListener(containerPath, project)); + } + + /** + * Working subroutine of initialize(), also called when the compliance preference of the project changes + * @param containerPath the container path to use + * @param project the JavaProject to use + * @throws CoreException if anything goes wrong + */ + private void reInitialize(IPath containerPath, IJavaProject project) throws CoreException + { + List<IClasspathEntry> entryList = new ArrayList<IClasspathEntry>(); + String compliance = project.getOption(COMPLIANCE_KEY, true); if (compliance.compareTo("1.4") > 0) { addPluginClasspath(entryList, FLEXOTASK_GENERIC_PLUGIN); } @@ -95,7 +124,7 @@ IClasspathContainer container = new FlexotaskClasspath(entries, containerPath); JavaCore.setClasspathContainer(containerPath, new IJavaProject[]{project}, new IClasspathContainer[]{container}, null); - } + } /** * Utility to add a plugin's contribution to an accumulating classpath @@ -191,8 +220,8 @@ } return null; } - - /** + + /** * Convert a source archive name to a full relative path using Eclipse conventions for source plugins. * This closely resembles a similar function in the PDE that is not accessible from outside the PDE * implementation. @@ -216,7 +245,7 @@ return null; } } - + /** * Compute all the paths to places that might have source. This code is based on similar code in the * PDE that is not accessible from outside. It is not actually as complete since it does not take into @@ -253,4 +282,54 @@ } return sourcePaths; } + + /** + * A PreferenceChangeListener to notice when the compliance level of a project changes + */ + private final class ComplianceChangeListener implements IPreferenceChangeListener + { + /** The container path to use for reinitialization */ + private IPath containerPath; + + /** The project whose changes are being listened for */ + private IJavaProject project; + + /** + * Make a new ComplianceChangeListener + * @param containerPath container path to use for reinitialization + * @param project project whose changes are being listened for + */ + ComplianceChangeListener(IPath containerPath, IJavaProject project) + { + this.containerPath = containerPath; + this.project = project; + } + + /* (non-Javadoc) + * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent) + */ + public void preferenceChange(PreferenceChangeEvent event) + { + String key = event.getKey(); + if (COMPLIANCE_KEY.equals(key)) { + try { + reInitialize(containerPath, project); + } catch (CoreException e) { + IStatus status = e.getStatus(); + if (status == null) { + status = new Status(Status.ERROR, EEditPlugin.ID, e.getMessage(), e); + } + ResourcesPlugin.getPlugin().getLog().log(status); + final IStatus istatus = status; + Display display = PlatformUI.getWorkbench().getDisplay(); + final Shell shell = display.getActiveShell(); + display.syncExec(new Runnable() { + public void run() { + ErrorDialog.openError(shell, "Error updating classpath", istatus.getMessage(), istatus); + } + }); + } + } + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |