package be.kuleuven.cs.distrinet.chameleon.eclipse.nature;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import be.kuleuven.cs.distrinet.chameleon.eclipse.project.ChameleonProjectNature;
import be.kuleuven.cs.distrinet.chameleon.eclipse.util.Files;

public class AddBackgroundNature extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
        .getActivePage().getSelection();
		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structureSelection = (IStructuredSelection) selection;
			Iterator iterator = structureSelection.iterator();
			while(iterator.hasNext()) {
				try {
				Object current = iterator.next();
				if(current instanceof IProject) {
					IProject project = (IProject) current;
					if(project.exists() && project.isOpen()) {
						IProjectDescription description = project.getDescription();
						IFile projectFile = project.getFile(".project");
						IFile backup = project.getFile(".project.chameleonbackup");
						Files.copy(projectFile, backup);
						String[] natures = description.getNatureIds();
						String[] newNatures = new String[natures.length + 1];
						System.arraycopy(natures, 0, newNatures, 0, natures.length);
						newNatures[natures.length] = ChameleonProjectNature.BACKGROUND_NATURE;
						description.setNatureIds(newNatures);
						// We must create the Chameleon project file before we change the project
						// description because the latter will trigger the creation of the
						// chameleon project nature.
						try {
							project.setDescription(description, new NullProgressMonitor());
						} catch(CoreException exc) {
							Files.copy(backup, projectFile);
						}
					}
				}
				} catch(CoreException exc) {
					// Because of the check for project.exists() and project.isOpen()
					// the call to project.getDescription() is not allowed to throw
					// a CoreException. Print if it happens.
					exc.printStackTrace();
				}
			}
		}
		return null;
	}
	

}

@@ -57,101 +57,8 @@
 e;%0A%0A
-import java.io.BufferedReader;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0A
 impo
@@ -80,16 +80,16 @@
 erator;%0A
+
 %0Aimport 
@@ -618,16 +618,229 @@
 rUtil;%0A%0A
+import be.kuleuven.cs.distrinet.chameleon.core.language.Language;%0Aimport be.kuleuven.cs.distrinet.chameleon.eclipse.LanguageMgt;%0Aimport be.kuleuven.cs.distrinet.chameleon.eclipse.connector.EclipseEditorExtension;%0A
 import b
@@ -974,16 +974,151 @@
 l.Files;
+%0Aimport be.kuleuven.cs.distrinet.chameleon.workspace.LanguageRepository;%0Aimport be.kuleuven.cs.distrinet.chameleon.workspace.Workspace;
 %0A%0Apublic
@@ -2288,23 +2288,31 @@
 s);%0A
-%09%09%09%09%09%09
+            
 // We 
-mus
+do no
 t cr
@@ -2316,19 +2316,17 @@
  create 
-the
+a
  Chamele
@@ -2345,144 +2345,45 @@
 ile 
-before we change the project%0A%09%09%09%09%09%09// description because the latter will trigger the creation of the%0A%09%09%09%09%09%09// chameleon project nature.
+to avoid overwriting a file.%0A%09%09%09%09%09%09//
 %0A%09%09%09
@@ -2840,11 +2840,405 @@
 l;%0A%09%7D%0A%09%0A
+  private void ploks(IProject project) %7B%0A  %09Workspace workspace = LanguageMgt.getInstance().workspace();%0A%09%09LanguageRepository repository = workspace.languageRepository();%0A%09%09for(Language language: repository.languages()) %7B%0A%09%09%09EclipseEditorExtension loader = language.plugin(EclipseEditorExtension.class);%0A%09%09%09if(loader != null && loader.canLoad(project)) %7B%0A%09%09%09%09loader.load(project);%0A%09%09%09%7D%0A%09%09%7D%0A%0A  %7D
 %0A%7D%0A

Enable the action to add the Chameleon background nature to a project.