package blade.migrate.core;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import aQute.lib.io.IO;

public class WorkspaceHelper {

	private void addNaturesToProject( IProject proj, String[] natureIds, IProgressMonitor monitor )
	        throws CoreException {
        IProjectDescription description = proj.getDescription();

        String[] prevNatures = description.getNatureIds();
        String[] newNatures = new String[prevNatures.length + natureIds.length];

        System.arraycopy( prevNatures, 0, newNatures, 0, prevNatures.length );

        for( int i = prevNatures.length; i < newNatures.length; i++ ) {
            newNatures[i] = natureIds[i - prevNatures.length];
        }

        description.setNatureIds( newNatures );
        proj.setDescription( description, monitor );
    }

	public IFile createIFile(String projectName, File file) throws CoreException, IOException {
		IJavaProject project = getJavaProject(projectName);

		IFile projectFile = project.getProject().getFile(file.getName());

		final IProgressMonitor npm = new NullProgressMonitor();

		if (projectFile.exists()) {
			projectFile.delete(IFile.FORCE, npm);
		}

		projectFile.create(new ByteArrayInputStream(IO.read(file)), IFile.FORCE, npm);

		return projectFile;
	}

	private IJavaProject getJavaProject(String projectName) throws CoreException {
		IProject javaProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

		if (javaProject.exists()) {
			javaProject.open(new NullProgressMonitor());
			return JavaCore.create(javaProject);
		}

		IProgressMonitor monitor = new NullProgressMonitor();

		javaProject.create(monitor);
		javaProject.open(monitor);
		addNaturesToProject(javaProject, new String[] { JavaCore.NATURE_ID }, monitor);

		return JavaCore.create(javaProject);
	}

}

@@ -1890,22 +1890,80 @@
 Name);%0A%0A
+%09%09IProgressMonitor monitor = new NullProgressMonitor();%0A%0A
 %09%09if (
+!
 javaProj
@@ -1985,157 +1985,174 @@
 %0A%09%09%09
-java
+I
 Project
-.open(new NullProgressMonitor());%0A%09%09%09return JavaCore.create(javaProject);%0A%09%09%7D%0A%0A%09%09IProgressMonitor monitor = new NullProgressMonitor(
+Description description = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);%0A%09%09%09javaProject.create(monitor);%0A%09%09%09javaProject.open(monitor
 );%0A
-%0A
 %09%09
+%09
 java
@@ -2151,39 +2151,60 @@
 %0A%09%09%09javaProject.
-create(
+setDescription(description, 
 monitor);%0A%09%09java
@@ -2197,16 +2197,21 @@
 nitor);%0A
+%09%09%7D%0A%0A
 %09%09javaPr

make sure helper project creation works for clean workspaces