/**
 * 
 */
package de.tuberlin.uebb.emodelica.operations;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

import de.tuberlin.uebb.emodelica.EModelicaPlugin;

/**
 * @author choeger
 * 
 */
public class NewPackageCreationOperation extends WorkspaceModifyOperation {

	private String sourceFolder;
	private String packageName;
	
	public NewPackageCreationOperation(String sourceFolder, String packageName) {
		super();
		this.sourceFolder = sourceFolder;
		this.packageName = packageName;
	}

	@Override
	protected void execute(IProgressMonitor arg0) throws CoreException,
			InvocationTargetException, InterruptedException {
		System.err.println("creating a new package! (" + packageName + " in " + sourceFolder + ")");

		IFolder srcFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(
				new Path(sourceFolder));
		IFolder last = srcFolder;

		for (String fName : packageName.split("\\.")) {
			last = last.getFolder(fName);
			System.err.println("handling: " + fName);
			if (!last.exists()) {
				System.err.println("creating...");
				last.create(true, true, null);
			}
		}

		IFile packagemo = last.getFile("package.mo");
		if (packagemo.exists())
			return;
		
		try {
			PipedInputStream in = new PipedInputStream();
			PipedOutputStream out = new PipedOutputStream(in);
			String newLine = System.getProperty("line.separator");

			StringBuffer buffer = new StringBuffer();
			buffer.append("within ");
			
			if (packageName.contains("."))
				buffer.append(packageName
					.substring(0, packageName.lastIndexOf(".")));
			else buffer.append(" ");
			
			buffer.append(";");
			buffer.append(newLine);

			buffer.append("package ");
			buffer.append(packageName
					.substring(packageName.lastIndexOf(".") + 1));
			buffer.append(newLine);

			buffer.append(newLine);
			buffer.append("end ");
			buffer.append(packageName
					.substring(packageName.lastIndexOf(".") + 1));
			buffer.append(";");
			buffer.append(newLine);

			out.write(buffer.toString().getBytes());
			out.close();

			packagemo.create(in, true, null);

		} catch (IOException e) {
			// TODO change -1 to error constant
			throw new CoreException(new Status(IStatus.ERROR,
					EModelicaPlugin.PLUGIN_ID, -1, e.getMessage(), e));
		}

	}

}

@@ -285,16 +285,112 @@
 Folder;%0A
+import org.eclipse.core.resources.IWorkspace;%0Aimport org.eclipse.core.resources.IWorkspaceRoot;%0A
 import o
@@ -1326,24 +1326,27 @@
 %0A%09%09I
-Folder srcFolder
+Workspace workspace
  = R
@@ -1378,56 +1378,134 @@
 ce()
-.getRoot().getFolder(%0A%09%09%09%09new Path(sourceFolder)
+;%0A%09%09IWorkspaceRoot root = workspace.getRoot();%0A%09%09Path path = new Path(sourceFolder);%0A%09%09IFolder srcFolder = root.getFolder(path
 );%0A%09
@@ -2907,15 +2907,13 @@
 ));%0A%09%09%7D%0A
-%0A
 %09%7D%0A
-%0A
 %7D%0A

debug the crash in NewPackage Op