### Eclipse Workspace Patch 1.0 #P org.eclipse.pde.ui Index: src/org/eclipse/pde/internal/ui/wizards/tools/ConvertedProjectsPage.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/tools/ConvertedProjectsPage.java,v retrieving revision 1.43 diff -u -r1.43 ConvertedProjectsPage.java --- src/org/eclipse/pde/internal/ui/wizards/tools/ConvertedProjectsPage.java 2 Oct 2007 22:03:04 -0000 1.43 +++ src/org/eclipse/pde/internal/ui/wizards/tools/ConvertedProjectsPage.java 25 Oct 2007 09:13:11 -0000 @@ -11,18 +11,9 @@ package org.eclipse.pde.internal.ui.wizards.tools; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; import java.util.Vector; -import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jdt.core.IClasspathEntry; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.CheckboxTableViewer; @@ -30,37 +21,17 @@ import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.wizard.WizardPage; -import org.eclipse.pde.core.IBaseModel; -import org.eclipse.pde.core.build.IBuild; -import org.eclipse.pde.core.build.IBuildEntry; -import org.eclipse.pde.core.plugin.IPluginBase; -import org.eclipse.pde.core.plugin.IPluginLibrary; -import org.eclipse.pde.core.plugin.IPluginModelFactory; -import org.eclipse.pde.internal.core.ICoreConstants; -import org.eclipse.pde.internal.core.TargetPlatformHelper; -import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; -import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel; -import org.eclipse.pde.internal.core.ibundle.IBundle; -import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; -import org.eclipse.pde.internal.core.natures.PDE; -import org.eclipse.pde.internal.core.util.CoreUtility; -import org.eclipse.pde.internal.core.util.IdUtil; import org.eclipse.pde.internal.ui.IHelpContextIds; import org.eclipse.pde.internal.ui.PDEPlugin; import org.eclipse.pde.internal.ui.PDEUIMessages; import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart; -import org.eclipse.pde.internal.ui.util.ModelModification; -import org.eclipse.pde.internal.ui.util.PDEModelUtility; -import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.actions.WorkspaceModifyOperation; import org.eclipse.ui.ide.IDE; -import org.osgi.framework.Constants; public class ConvertedProjectsPage extends WizardPage { private CheckboxTableViewer projectViewer; @@ -68,10 +39,6 @@ private IProject[] fSelected; private IProject[] fUnconverted; - private String fLibraryName; - private String[] fSrcEntries; - private String[] fLibEntries; - public class ProjectContentProvider extends DefaultContentProvider implements IStructuredContentProvider { @@ -137,91 +104,16 @@ PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.CONVERTED_PROJECTS); } - - private String createInitialName(String id) { - int loc = id.lastIndexOf('.'); - if (loc == -1) - return id; - StringBuffer buf = new StringBuffer(id.substring(loc + 1)); - buf.setCharAt(0, Character.toUpperCase(buf.charAt(0))); - return buf.toString(); - } - private void createManifestFile(IFile file, IProgressMonitor monitor) - throws CoreException { - WorkspaceBundlePluginModel model = new WorkspaceBundlePluginModel(file, null); - model.load(); - IBundle pluginBundle = model.getBundleModel().getBundle(); - - String pluginId = pluginBundle.getHeader(Constants.BUNDLE_SYMBOLICNAME); - String pluginName = pluginBundle.getHeader(Constants.BUNDLE_NAME); - String pluginVersion = pluginBundle.getHeader(Constants.BUNDLE_VERSION); - - boolean missingInfo = (pluginId == null || pluginName == null || pluginVersion == null); - - //If no ID exists, create one - if(pluginId == null) - { pluginId = IdUtil.getValidId(file.getProject().getName()); - } - //At this point, the plug-in ID is not null - - //If no version number exists, create one - if(pluginVersion == null) - { pluginVersion = "1.0.0"; //$NON-NLS-1$ - } - - //If no name exists, create one using the non-null pluginID - if(pluginName == null) - { pluginName = createInitialName(pluginId); - } - - pluginBundle.setHeader(Constants.BUNDLE_SYMBOLICNAME, pluginId); - pluginBundle.setHeader(Constants.BUNDLE_VERSION, pluginVersion); - pluginBundle.setHeader(Constants.BUNDLE_NAME, pluginName); - - if(missingInfo) - { IPluginModelFactory factory = model.getPluginFactory(); - IPluginBase base = model.getPluginBase(); - if (fLibraryName != null && !fLibraryName.equals(".")) { //$NON-NLS-1$ - IPluginLibrary library = factory.createLibrary(); - library.setName(fLibraryName); - library.setExported(true); - base.add(library); - } - for (int i = 0; i < fLibEntries.length; i++) { - IPluginLibrary library = factory.createLibrary(); - library.setName(fLibEntries[i]); - library.setExported(true); - base.add(library); - } - if (TargetPlatformHelper.getTargetVersion() >= 3.1) - pluginBundle.setHeader(Constants.BUNDLE_MANIFESTVERSION, "2"); //$NON-NLS-1$ - } - - model.save(); - monitor.done(); - organizeExports(file.getProject()); - } - - private boolean isOldTarget() { - return TargetPlatformHelper.getTargetVersion() < 3.1; - } public boolean finish() { final Object [] selected = tablePart.getSelection(); - IRunnableWithProgress operation = new WorkspaceModifyOperation() { - public void execute(IProgressMonitor monitor) { - try { - convertProjects(selected, monitor); - } catch (CoreException e) { - PDEPlugin.logException(e); - } finally { - monitor.done(); - } - } - }; + IProject[] projectsToConvert = castSelectionToProjects(selected); + try { - getContainer().run(false, true, operation); + IRunnableWithProgress convertOperation; + convertOperation = new ConvertProjectToPluginOperation( projectsToConvert ); + getContainer().run(false, true, convertOperation); } catch (InvocationTargetException e) { PDEPlugin.logException(e); return false; @@ -232,120 +124,28 @@ return true; } - public void convertProject(IProject project, IProgressMonitor monitor) - throws CoreException { - - CoreUtility.addNatureToProject(project, PDE.PLUGIN_NATURE, monitor); - - loadClasspathEntries(project, monitor); - loadLibraryName(project); - - createManifestFile(project.getFile(PDEModelUtility.F_MANIFEST_FP), monitor); + /** + * Convert the selected Object[] into an IProject[] + * @param selected Selected projects in an Object [] + * @return the same selected projects in an IProject [] + */ + private IProject[] castSelectionToProjects(Object[] selected) { - IFile buildFile = project.getFile(PDEModelUtility.F_BUILD); - if (!buildFile.exists()) { - WorkspaceBuildModel model = new WorkspaceBuildModel(buildFile); - IBuild build = model.getBuild(true); - IBuildEntry entry = model.getFactory().createEntry("bin.includes"); //$NON-NLS-1$ - if (project.getFile(ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR).exists()) - entry.addToken("plugin.xml"); //$NON-NLS-1$ - if (project.getFile(ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR).exists()) - entry.addToken("META-INF/"); //$NON-NLS-1$ - for (int i = 0; i < fLibEntries.length; i++) { - entry.addToken(fLibEntries[i]); - } - - if (fSrcEntries.length > 0) { - entry.addToken(fLibraryName); - IBuildEntry source = model.getFactory().createEntry("source." + fLibraryName); //$NON-NLS-1$ - for (int i = 0; i < fSrcEntries.length; i++) { - source.addToken(fSrcEntries[i]); - } - build.add(source); - } - if (entry.getTokens().length > 0) - build.add(entry); - - model.save(); + if( selected instanceof IProject[] ) { + return (IProject[])selected; } - } - - private void convertProjects(Object[] selected, IProgressMonitor monitor) - throws CoreException { - monitor.beginTask(PDEUIMessages.ConvertedProjectWizard_converting, selected.length); + + IProject [] projects = new IProject[selected.length]; + for (int i = 0; i < selected.length; i++) { - convertProject((IProject) selected[i], monitor); - monitor.worked(1); - } - monitor.done(); - } - - private void loadClasspathEntries(IProject project, IProgressMonitor monitor) { - IJavaProject javaProject = JavaCore.create(project); - IClasspathEntry[] currentClassPath = new IClasspathEntry[0]; - ArrayList sources = new ArrayList(); - ArrayList libraries = new ArrayList(); - try { - currentClassPath = javaProject.getRawClasspath(); - } catch (JavaModelException e) { - } - for (int i = 0; i < currentClassPath.length; i++) { - int contentType = currentClassPath[i].getEntryKind(); - if (contentType == IClasspathEntry.CPE_SOURCE) { - String relativePath = getRelativePath(currentClassPath[i], project); - if (relativePath.equals("")) { //$NON-NLS-1$ - sources.add("."); //$NON-NLS-1$ - } else { - sources.add(relativePath + "/"); //$NON-NLS-1$ - } - } else if (contentType == IClasspathEntry.CPE_LIBRARY) { - String path = getRelativePath(currentClassPath[i], project); - if (path.length() > 0) - libraries.add(path); - else - libraries.add("."); //$NON-NLS-1$ - } + + // N.B. I've not added any cast checks here because the original + // code didn't either and I'm aiming (initially) for a + // minimal refactoring. + projects[i] = (IProject)selected[i]; } - fSrcEntries = (String[])sources.toArray(new String[sources.size()]); - fLibEntries = (String[])libraries.toArray(new String[libraries.size()]); - IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1]; - System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length); - classPath[classPath.length - 1] = ClasspathComputer.createContainerEntry(); - try { - javaProject.setRawClasspath(classPath, monitor); - } catch (JavaModelException e) { - } - } - - private String getRelativePath(IClasspathEntry cpe, IProject project) { - IPath path = project.getFile(cpe.getPath()).getProjectRelativePath(); - return path.removeFirstSegments(1).toString(); - } - - private void loadLibraryName(IProject project) { - if (isOldTarget() || - (fLibEntries.length > 0 && fSrcEntries.length > 0)) { - String libName = project.getName(); - int i = libName.lastIndexOf("."); //$NON-NLS-1$ - if (i != -1) - libName = libName.substring(i + 1); - fLibraryName = libName + ".jar"; //$NON-NLS-1$ - } else { - fLibraryName = "."; //$NON-NLS-1$ - } - } - - private void organizeExports(final IProject project) { - PDEModelUtility.modifyModel(new ModelModification( - project.getFile(PDEModelUtility.F_MANIFEST_FP)) { - protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException { - if (!(model instanceof IBundlePluginModelBase)) - return; - OrganizeManifest.organizeExportPackages( - ((IBundlePluginModelBase)model).getBundleModel().getBundle(), - project, true, true); - } - }, null); + return projects; } + } Index: src/org/eclipse/pde/internal/ui/wizards/tools/ConvertProjectToPluginOperation.java =================================================================== RCS file: src/org/eclipse/pde/internal/ui/wizards/tools/ConvertProjectToPluginOperation.java diff -N src/org/eclipse/pde/internal/ui/wizards/tools/ConvertProjectToPluginOperation.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/pde/internal/ui/wizards/tools/ConvertProjectToPluginOperation.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,291 @@ +package org.eclipse.pde.internal.ui.wizards.tools; + +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.pde.core.IBaseModel; +import org.eclipse.pde.core.build.IBuild; +import org.eclipse.pde.core.build.IBuildEntry; +import org.eclipse.pde.core.plugin.IPluginBase; +import org.eclipse.pde.core.plugin.IPluginLibrary; +import org.eclipse.pde.core.plugin.IPluginModelFactory; +import org.eclipse.pde.internal.core.ICoreConstants; +import org.eclipse.pde.internal.core.TargetPlatformHelper; +import org.eclipse.pde.internal.core.build.WorkspaceBuildModel; +import org.eclipse.pde.internal.core.bundle.WorkspaceBundlePluginModel; +import org.eclipse.pde.internal.core.ibundle.IBundle; +import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; +import org.eclipse.pde.internal.core.natures.PDE; +import org.eclipse.pde.internal.core.util.CoreUtility; +import org.eclipse.pde.internal.core.util.IdUtil; +import org.eclipse.pde.internal.ui.PDEPlugin; +import org.eclipse.pde.internal.ui.PDEUIMessages; +import org.eclipse.pde.internal.ui.util.ModelModification; +import org.eclipse.pde.internal.ui.util.PDEModelUtility; +import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer; +import org.eclipse.ui.actions.WorkspaceModifyOperation; +import org.osgi.framework.Constants; + +/** + * Operation to convert a normal workspace project into a plug-in project. This + * code has, in the main, been refactored (copied with little or no amendment) + * from org.eclipse.pde.internal.ui.wizards.tool.ConvertedProjectsPage. + * + * @author Les Jones + */ +public class ConvertProjectToPluginOperation extends WorkspaceModifyOperation { + + private IProject[] projectsToConvert; + + private String fLibraryName; + private String[] fSrcEntries; + private String[] fLibEntries; + + /** + * Workspace operation to convert the specified project into a plug-in + * project. + * + * @param theProjectsToConvert + * The project to be converted. + */ + public ConvertProjectToPluginOperation(IProject[] theProjectsToConvert) { + + this.projectsToConvert = theProjectsToConvert; + } + + /** + * Convert a normal java project into a plug-in project. + * + * @param monitor + * Progress monitor + */ + protected void execute(IProgressMonitor monitor) throws CoreException, + InvocationTargetException, InterruptedException { + + try { + monitor.beginTask(PDEUIMessages.ConvertedProjectWizard_converting, + projectsToConvert.length); + + for (int i = 0; i < projectsToConvert.length; i++) { + IProject projectToConvert = projectsToConvert[i]; + + convertProject(projectToConvert, monitor); + monitor.worked(1); + } + + } catch (CoreException e) { + PDEPlugin.logException(e); + } finally { + monitor.done(); + } + } + + private void convertProject(IProject projectToConvert, + IProgressMonitor monitor) throws CoreException { + + // Do early checks to make sure we can get out fast if we're not setup + // properly + if (projectToConvert == null || !projectToConvert.exists()) { + // TODO : Log message to confirm why the early exit? + return; + } + + // Nature check - do we need to do anything at all? + if (projectToConvert.hasNature(PDE.PLUGIN_NATURE)) { + // TODO : Log message to confirm why the early exit? + return; + } + + CoreUtility.addNatureToProject(projectToConvert, PDE.PLUGIN_NATURE, + monitor); + + loadClasspathEntries(projectToConvert, monitor); + loadLibraryName(projectToConvert); + + createManifestFile(projectToConvert + .getFile(PDEModelUtility.F_MANIFEST_FP), monitor); + + IFile buildFile = projectToConvert.getFile(PDEModelUtility.F_BUILD); + if (!buildFile.exists()) { + WorkspaceBuildModel model = new WorkspaceBuildModel(buildFile); + IBuild build = model.getBuild(true); + IBuildEntry entry = model.getFactory().createEntry("bin.includes"); //$NON-NLS-1$ + if (projectToConvert.getFile( + ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR).exists()) + entry.addToken("plugin.xml"); //$NON-NLS-1$ + if (projectToConvert.getFile( + ICoreConstants.BUNDLE_FILENAME_DESCRIPTOR).exists()) + entry.addToken("META-INF/"); //$NON-NLS-1$ + for (int i = 0; i < fLibEntries.length; i++) { + entry.addToken(fLibEntries[i]); + } + + if (fSrcEntries.length > 0) { + entry.addToken(fLibraryName); + IBuildEntry source = model.getFactory().createEntry( + "source." + fLibraryName); //$NON-NLS-1$ + for (int i = 0; i < fSrcEntries.length; i++) { + source.addToken(fSrcEntries[i]); + } + build.add(source); + } + if (entry.getTokens().length > 0) + build.add(entry); + + model.save(); + } + } + + private void loadClasspathEntries(IProject project, IProgressMonitor monitor) { + IJavaProject javaProject = JavaCore.create(project); + IClasspathEntry[] currentClassPath = new IClasspathEntry[0]; + ArrayList sources = new ArrayList(); + ArrayList libraries = new ArrayList(); + try { + currentClassPath = javaProject.getRawClasspath(); + } catch (JavaModelException e) { + } + for (int i = 0; i < currentClassPath.length; i++) { + int contentType = currentClassPath[i].getEntryKind(); + if (contentType == IClasspathEntry.CPE_SOURCE) { + String relativePath = getRelativePath(currentClassPath[i], + project); + if (relativePath.equals("")) { //$NON-NLS-1$ + sources.add("."); //$NON-NLS-1$ + } else { + sources.add(relativePath + "/"); //$NON-NLS-1$ + } + } else if (contentType == IClasspathEntry.CPE_LIBRARY) { + String path = getRelativePath(currentClassPath[i], project); + if (path.length() > 0) + libraries.add(path); + else + libraries.add("."); //$NON-NLS-1$ + } + } + fSrcEntries = (String[]) sources.toArray(new String[sources.size()]); + fLibEntries = (String[]) libraries + .toArray(new String[libraries.size()]); + + IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1]; + System.arraycopy(currentClassPath, 0, classPath, 0, + currentClassPath.length); + classPath[classPath.length - 1] = ClasspathComputer + .createContainerEntry(); + try { + javaProject.setRawClasspath(classPath, monitor); + } catch (JavaModelException e) { + } + } + + private String getRelativePath(IClasspathEntry cpe, IProject project) { + IPath path = project.getFile(cpe.getPath()).getProjectRelativePath(); + return path.removeFirstSegments(1).toString(); + } + + private void loadLibraryName(IProject project) { + if (isOldTarget() || (fLibEntries.length > 0 && fSrcEntries.length > 0)) { + String libName = project.getName(); + int i = libName.lastIndexOf("."); //$NON-NLS-1$ + if (i != -1) + libName = libName.substring(i + 1); + fLibraryName = libName + ".jar"; //$NON-NLS-1$ + } else { + fLibraryName = "."; //$NON-NLS-1$ + } + } + + private void organizeExports(final IProject project) { + PDEModelUtility.modifyModel(new ModelModification(project + .getFile(PDEModelUtility.F_MANIFEST_FP)) { + protected void modifyModel(IBaseModel model, + IProgressMonitor monitor) throws CoreException { + if (!(model instanceof IBundlePluginModelBase)) + return; + OrganizeManifest.organizeExportPackages( + ((IBundlePluginModelBase) model).getBundleModel() + .getBundle(), project, true, true); + } + }, null); + } + + private String createInitialName(String id) { + int loc = id.lastIndexOf('.'); + if (loc == -1) + return id; + StringBuffer buf = new StringBuffer(id.substring(loc + 1)); + buf.setCharAt(0, Character.toUpperCase(buf.charAt(0))); + return buf.toString(); + } + + private void createManifestFile(IFile file, IProgressMonitor monitor) + throws CoreException { + WorkspaceBundlePluginModel model = new WorkspaceBundlePluginModel(file, + null); + model.load(); + IBundle pluginBundle = model.getBundleModel().getBundle(); + + String pluginId = pluginBundle.getHeader(Constants.BUNDLE_SYMBOLICNAME); + String pluginName = pluginBundle.getHeader(Constants.BUNDLE_NAME); + String pluginVersion = pluginBundle.getHeader(Constants.BUNDLE_VERSION); + + boolean missingInfo = (pluginId == null || pluginName == null || pluginVersion == null); + + // If no ID exists, create one + if (pluginId == null) { + pluginId = IdUtil.getValidId(file.getProject().getName()); + } + // At this point, the plug-in ID is not null + + // If no version number exists, create one + if (pluginVersion == null) { + pluginVersion = "1.0.0"; //$NON-NLS-1$ + } + + // If no name exists, create one using the non-null pluginID + if (pluginName == null) { + pluginName = createInitialName(pluginId); + } + + pluginBundle.setHeader(Constants.BUNDLE_SYMBOLICNAME, pluginId); + pluginBundle.setHeader(Constants.BUNDLE_VERSION, pluginVersion); + pluginBundle.setHeader(Constants.BUNDLE_NAME, pluginName); + + if (missingInfo) { + IPluginModelFactory factory = model.getPluginFactory(); + IPluginBase base = model.getPluginBase(); + if (fLibraryName != null && !fLibraryName.equals(".")) { //$NON-NLS-1$ + IPluginLibrary library = factory.createLibrary(); + library.setName(fLibraryName); + library.setExported(true); + base.add(library); + } + for (int i = 0; i < fLibEntries.length; i++) { + IPluginLibrary library = factory.createLibrary(); + library.setName(fLibEntries[i]); + library.setExported(true); + base.add(library); + } + if (TargetPlatformHelper.getTargetVersion() >= 3.1) + pluginBundle.setHeader(Constants.BUNDLE_MANIFESTVERSION, "2"); //$NON-NLS-1$ + } + + model.save(); + monitor.done(); + organizeExports(file.getProject()); + } + + private boolean isOldTarget() { + return TargetPlatformHelper.getTargetVersion() < 3.1; + } + +}