1 11 package org.eclipse.ant.internal.ui.datatransfer; 12 13 import java.io.File ; 14 import com.ibm.icu.text.MessageFormat; 15 16 import org.apache.tools.ant.BuildException; 17 import org.apache.tools.ant.taskdefs.Javac; 18 import org.eclipse.ant.internal.ui.AntUIPlugin; 19 import org.eclipse.core.resources.IContainer; 20 import org.eclipse.core.resources.IFolder; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IProjectDescription; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IWorkspaceRoot; 25 import org.eclipse.core.resources.ResourcesPlugin; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.IProgressMonitor; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.core.runtime.Status; 32 import org.eclipse.jdt.core.IClasspathEntry; 33 import org.eclipse.jdt.core.IJavaProject; 34 import org.eclipse.jdt.core.IPackageFragmentRoot; 35 import org.eclipse.jdt.core.JavaCore; 36 import org.eclipse.jdt.core.JavaModelException; 37 import org.eclipse.jdt.launching.JavaRuntime; 38 39 public class ProjectCreator { 40 41 public IJavaProject createJavaProjectFromJavacNode(String projectName, Javac javacTask, IProgressMonitor monitor) throws CoreException { 42 try { 43 IJavaProject javaProject = createJavaProject(projectName, monitor); 44 45 File destDir= javacTask.getDestdir(); 46 String destDirName= destDir == null ? null : destDir.getName(); 47 org.apache.tools.ant.types.Path sourceDirs= javacTask.getSrcdir(); 48 createSourceDirectories(destDir, destDirName, sourceDirs, javaProject, monitor); 49 50 addVariableEntry(javaProject, new Path(JavaRuntime.JRELIB_VARIABLE), new Path(JavaRuntime.JRESRC_VARIABLE), new Path(JavaRuntime.JRESRCROOT_VARIABLE), monitor); 52 53 setClasspath(javacTask, javaProject, monitor); 54 55 javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor); 56 return javaProject; 57 } catch (BuildException be) { 58 IStatus status= new Status(IStatus.ERROR, AntUIPlugin.PI_ANTUI, IStatus.OK, be.getLocalizedMessage(), be); 59 throw new CoreException(status); 60 } 61 } 62 63 private void setClasspath(Javac javacTask, IJavaProject javaProject, IProgressMonitor monitor) throws CoreException { 64 try { 65 org.apache.tools.ant.types.Path classpath= javacTask.getClasspath(); 66 if (classpath == null) { 67 return; 68 } 69 String [] classpaths= classpath.list(); 70 for (int i = 0; i < classpaths.length; i++) { 71 String cp = classpaths[i]; 72 File classpathEntry= new File (cp); 73 if (classpathEntry.isFile()) { 74 addLibrary(javaProject, new Path(classpathEntry.getAbsolutePath()), monitor); 75 } else { 76 addContainer(javaProject, new Path(classpathEntry.getAbsolutePath()), monitor); 77 } 78 } 79 } catch (BuildException be) { 80 IStatus status= new Status(IStatus.ERROR, AntUIPlugin.PI_ANTUI, IStatus.OK, MessageFormat.format(DataTransferMessages.ProjectCreator_0, new String []{be.getLocalizedMessage()}), null); 81 throw new CoreException(status); 82 } 83 } 84 85 private void createSourceDirectories(File destDir, String destDirName, org.apache.tools.ant.types.Path sourceDirs, IJavaProject javaProject, IProgressMonitor monitor) throws CoreException { 86 String [] sourceDirectories= sourceDirs.list(); 87 for (int i = 0; i < sourceDirectories.length; i++) { 88 String srcDir = sourceDirectories[i]; 89 File srcDirectory= new File (srcDir); 90 String srcDirectoryName= srcDirectory.getName(); 91 String destDirPath= destDir == null ? srcDir : destDir.getAbsolutePath(); 92 if (destDirName == null) { 93 destDirName= srcDirectoryName; 94 } 95 addSourceContainer(javaProject, srcDirectoryName, srcDir, destDirName, destDirPath, monitor); 96 } 97 } 98 99 private IJavaProject createJavaProject(String projectName, IProgressMonitor monitor) throws CoreException { 100 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); 101 IProject project= root.getProject(projectName); 102 if (!project.exists()) { 103 project.create(monitor); 104 } else { 105 project.refreshLocal(IResource.DEPTH_INFINITE, monitor); 106 } 107 108 if (!project.isOpen()) { 109 project.open(monitor); 110 } 111 112 if (!project.hasNature(JavaCore.NATURE_ID)) { 113 addNatureToProject(project, JavaCore.NATURE_ID, monitor); 114 } 115 116 IJavaProject jproject= JavaCore.create(project); 117 118 jproject.setRawClasspath(new IClasspathEntry[0], monitor); 119 120 return jproject; 121 } 122 123 private void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException { 124 IProjectDescription description = proj.getDescription(); 125 String [] prevNatures= description.getNatureIds(); 126 String [] newNatures= new String [prevNatures.length + 1]; 127 System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); 128 newNatures[prevNatures.length]= natureId; 129 description.setNatureIds(newNatures); 130 proj.setDescription(description, monitor); 131 } 132 133 136 private void addSourceContainer(IJavaProject jproject, String srcName, String srcPath, String outputName, String outputPath, IProgressMonitor monitor) throws CoreException { 137 IProject project= jproject.getProject(); 138 IContainer container= null; 139 if (srcName == null || srcName.length() == 0) { 140 container= project; 141 } else { 142 IFolder folder= project.getFolder(srcName); 143 if (!folder.exists()) { 144 folder.createLink(new Path(srcPath), IResource.ALLOW_MISSING_LOCAL, monitor); 145 } 146 container= folder; 147 } 148 IPackageFragmentRoot root= jproject.getPackageFragmentRoot(container); 149 150 IPath output= null; 151 if (outputName!= null) { 152 IFolder outputFolder = project.getFolder(outputName); 153 if (!outputFolder.exists()) { 154 outputFolder.createLink(new Path(outputPath), IResource.ALLOW_MISSING_LOCAL, monitor); 155 } 156 output= outputFolder.getFullPath(); 157 } 158 159 IClasspathEntry cpe= JavaCore.newSourceEntry(root.getPath(), new IPath[0], output); 160 161 addToClasspath(jproject, cpe, monitor); 162 } 163 164 private void addToClasspath(IJavaProject jproject, IClasspathEntry cpe, IProgressMonitor monitor) throws JavaModelException { 165 IClasspathEntry[] oldEntries= jproject.getRawClasspath(); 166 for (int i= 0; i < oldEntries.length; i++) { 167 if (oldEntries[i].equals(cpe)) { 168 return; 169 } 170 } 171 int nEntries= oldEntries.length; 172 IClasspathEntry[] newEntries= new IClasspathEntry[nEntries + 1]; 173 System.arraycopy(oldEntries, 0, newEntries, 0, nEntries); 174 newEntries[nEntries]= cpe; 175 jproject.setRawClasspath(newEntries, monitor); 176 } 177 178 181 private void addVariableEntry(IJavaProject jproject, IPath path, IPath sourceAttachPath, IPath sourceAttachRoot, IProgressMonitor monitor) throws JavaModelException { 182 IClasspathEntry cpe= JavaCore.newVariableEntry(path, sourceAttachPath, sourceAttachRoot); 183 addToClasspath(jproject, cpe, monitor); 184 } 185 186 189 private void addLibrary(IJavaProject jproject, IPath path, IProgressMonitor monitor) throws JavaModelException { 190 IClasspathEntry cpe= JavaCore.newLibraryEntry(path, null, null); 191 addToClasspath(jproject, cpe, monitor); 192 } 193 194 197 private void addContainer(IJavaProject jproject, IPath path, IProgressMonitor monitor) throws CoreException { 198 IClasspathEntry cpe= JavaCore.newContainerEntry(path); 199 addToClasspath(jproject, cpe, monitor); 200 IProject project= jproject.getProject(); 201 IFolder folder= project.getFolder(path.lastSegment()); 202 if (!folder.exists()) { 203 folder.createLink(path, IResource.ALLOW_MISSING_LOCAL, monitor); 204 } 205 } 206 } 207 | Popular Tags |