Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004, 2006 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.ant.internal.ui.datatransfer;
 
 import java.io.File;
 
 import  org.apache.tools.ant.BuildException;
 import  org.apache.tools.ant.taskdefs.Javac;
 
 public class ProjectCreator {
 		
 	public IJavaProject createJavaProjectFromJavacNode(String projectName, Javac javacTaskIProgressMonitor monitorthrows CoreException {
 		try {
 			IJavaProject javaProject = createJavaProject(projectNamemonitor);
 			
 			File destDirjavacTask.getDestdir();
 			String destDirNamedestDir == null ? null : destDir.getName();
 			org.apache.tools.ant.types.Path sourceDirsjavacTask.getSrcdir();
 			createSourceDirectories(destDirdestDirNamesourceDirsjavaProjectmonitor);
 			
 			// add rt.jar
 			
 			setClasspath(javacTaskjavaProjectmonitor);
 			
 			javaProject.getProject().refreshLocal(.monitor);
 			return javaProject;
 		} catch (BuildException be) {
 			IStatus statusnew Status(...be.getLocalizedMessage(), be);
 			throw new CoreException(status);
 		}
 	}
 	
 	private void setClasspath(Javac javacTaskIJavaProject javaProjectIProgressMonitor monitorthrows CoreException {
 		try {
 			org.apache.tools.ant.types.Path classpathjavacTask.getClasspath();
 			if (classpath == null) {
 				return;
 			}
 			String[] classpathsclasspath.list();
 			for (int i = 0; i < classpaths.lengthi++) {
 				String cp = classpaths[i];
 				File classpathEntrynew File(cp);
 				if (classpathEntry.isFile()) {
 					addLibrary(javaProjectnew Path(classpathEntry.getAbsolutePath()), monitor);
 				} else {
 					addContainer(javaProjectnew Path(classpathEntry.getAbsolutePath()), monitor);
 				}
 			}
 		} catch (BuildException be) {
 			IStatus statusnew Status(..., MessageFormat.format(.new String[]{be.getLocalizedMessage()}), null);
 			throw new CoreException(status);
 		}
 	}
 
 	private void createSourceDirectories(File destDirString destDirName, org.apache.tools.ant.types.Path sourceDirsIJavaProject javaProjectIProgressMonitor monitorthrows CoreException {
 		String[] sourceDirectoriessourceDirs.list();
 		for (int i = 0; i < sourceDirectories.lengthi++) {
 			String srcDir = sourceDirectories[i];
 			File srcDirectorynew File(srcDir);
 			String srcDirectoryNamesrcDirectory.getName();
 			String destDirPathdestDir == null ? srcDir : destDir.getAbsolutePath();
 			if (destDirName == null) {
 			    destDirNamesrcDirectoryName;
 			}
 			addSourceContainer(javaProjectsrcDirectoryNamesrcDirdestDirNamedestDirPathmonitor);
 		}
 	}
 
 	private IJavaProject createJavaProject(String projectNameIProgressMonitor monitorthrows CoreException {
		IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
		IProject projectroot.getProject(projectName);
		if (!project.exists()) {
			project.create(monitor);
else {
		}
		if (!project.isOpen()) {
			project.open(monitor);
		}
		if (!project.hasNature(.)) {
		}
		IJavaProject jproject= JavaCore.create(project);
		jproject.setRawClasspath(new IClasspathEntry[0], monitor);
		return jproject;	
	}
	private void addNatureToProject(IProject projString natureIdIProgressMonitor monitorthrows CoreException {
		IProjectDescription description = proj.getDescription();
		String[] prevNaturesdescription.getNatureIds();
		String[] newNaturesnew String[prevNatures.length + 1];
		System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
		newNatures[prevNatures.length]= natureId;
		description.setNatureIds(newNatures);
		proj.setDescription(descriptionmonitor);
	}

Adds a source container to a IJavaProject.
	private void addSourceContainer(IJavaProject jprojectString srcNameString srcPathString outputNameString outputPathIProgressMonitor monitorthrows CoreException {
		IProject projectjproject.getProject();
		IContainer containernull;
		if (srcName == null || srcName.length() == 0) {
			containerproject;
else {
			IFolder folderproject.getFolder(srcName);
			if (!folder.exists()) {
				folder.createLink(new Path(srcPath), .monitor);
			}
			containerfolder;
		}
		IPackageFragmentRoot rootjproject.getPackageFragmentRoot(container);
		IPath outputnull;
		if (outputName!= null) {
			IFolder outputFolder = project.getFolder(outputName);
			if (!outputFolder.exists()) {
			    outputFolder.createLink(new Path(outputPath), .monitor);
			}
			outputoutputFolder.getFullPath();
		}
		IClasspathEntry cpe= JavaCore.newSourceEntry(root.getPath(), new IPath[0], output);
		addToClasspath(jprojectcpemonitor);		
	}	
	private void addToClasspath(IJavaProject jprojectIClasspathEntry cpeIProgressMonitor monitorthrows JavaModelException {
		IClasspathEntry[] oldEntriesjproject.getRawClasspath();
		for (int i= 0; i < oldEntries.lengthi++) {
			if (oldEntries[i].equals(cpe)) {
				return;
			}
		}
		int nEntriesoldEntries.length;
		IClasspathEntry[] newEntriesnew IClasspathEntry[nEntries + 1];
		System.arraycopy(oldEntries, 0, newEntries, 0, nEntries);
		newEntries[nEntries]= cpe;
		jproject.setRawClasspath(newEntriesmonitor);
	}

Adds a variable entry with source attachment to a IJavaProject if the path can be resolved.
	private void addVariableEntry(IJavaProject jprojectIPath pathIPath sourceAttachPathIPath sourceAttachRootIProgressMonitor monitorthrows JavaModelException {
		IClasspathEntry cpe= JavaCore.newVariableEntry(pathsourceAttachPathsourceAttachRoot);
		addToClasspath(jprojectcpemonitor);
	}

Adds a library entry to an IJavaProject.
	private void addLibrary(IJavaProject jprojectIPath pathIProgressMonitor monitorthrows JavaModelException {
		IClasspathEntry cpe= JavaCore.newLibraryEntry(pathnullnull);
		addToClasspath(jprojectcpemonitor);
	}

Adds a container entry to an IJavaProject.
	private void addContainer(IJavaProject jprojectIPath pathIProgressMonitor monitorthrows CoreException {
		IClasspathEntry cpe= JavaCore.newContainerEntry(path);
		addToClasspath(jprojectcpemonitor);
		IProject projectjproject.getProject();
		IFolder folderproject.getFolder(path.lastSegment());
		if (!folder.exists()) {
			folder.createLink(path.monitor);
		}
	}
New to GrepCode? Check out our FAQ X