package fr.obeo.baseliner.pde;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.pde.internal.ui.wizards.tools.OrganizeManifestsProcessor;

import fr.obeo.baseliner.ManifestCleanup;

public class PDEManifestCleanup implements ManifestCleanup {

	private void clean(Collection<IProject> projects, IProgressMonitor pm) {
		OrganizeManifestsProcessor organizer = new OrganizeManifestsProcessor(
				new ArrayList(projects));
		organizer.setCalculateUses(true);
		organizer.setMarkInternal(true);
		organizer.setPackageFilter("*.internal*");
		organizer.setAddMissing(true);
		
		try {
			Change change = organizer.createChange(pm);
			change.initializeValidationData(pm);
			if (change.isEnabled() && change.isValid(pm).isOK()) {
				change.perform(pm);
			}
		} catch (OperationCanceledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void cleanup(File manifestFile) {
//		IFile[] files = ResourcesPlugin.getWorkspace().getRoot()
//				.findFilesForLocationURI(manifestFile.toURI());
//		Collection<IProject> projects = new LinkedHashSet<IProject>();
//		for (IFile iFile : files) {
//			if (iFile.exists() && iFile.getProject() != null) {
//				projects.add(iFile.getProject());
//			}
//		}
//		// TODO project monitor;
//		clean(projects, new NullProgressMonitor());

	}

}

@@ -610,16 +610,18 @@
 r pm) %7B%0A
+//
 %09%09Organi
@@ -685,16 +685,18 @@
 cessor(%0A
+//
 %09%09%09%09new 
@@ -717,16 +717,18 @@
 ects));%0A
+//
 %09%09organi
@@ -747,32 +747,34 @@
 lateUses(true);%0A
+//
 %09%09organizer.setM
@@ -792,16 +792,18 @@
 (true);%0A
+//
 %09%09organi
@@ -839,16 +839,18 @@
 nal*%22);%0A
+//
 %09%09organi
@@ -878,19 +878,23 @@
 e);%0A
+//
 %09%09%0A
+//
 %09%09try %7B%0A
 %09%09%09C
@@ -889,16 +889,18 @@
 %09%09try %7B%0A
+//
 %09%09%09Chang
@@ -938,16 +938,18 @@
 ge(pm);%0A
+//
 %09%09%09chang
@@ -980,16 +980,18 @@
 ta(pm);%0A
+//
 %09%09%09if (c
@@ -1040,16 +1040,18 @@
 OK()) %7B%0A
+//
 %09%09%09%09chan
@@ -1066,21 +1066,25 @@
 rm(pm);%0A
+//
 %09%09%09%7D%0A
+//
 %09%09%7D catc
@@ -1110,32 +1110,34 @@
 dException e) %7B%0A
+//
 %09%09%09// TODO Auto-
@@ -1150,32 +1150,34 @@
 ted catch block%0A
+//
 %09%09%09e.printStackT
@@ -1184,16 +1184,18 @@
 race();%0A
+//
 %09%09%7D catc
@@ -1216,16 +1216,18 @@
 on e) %7B%0A
+//
 %09%09%09// TO
@@ -1256,16 +1256,18 @@
 h block%0A
+//
 %09%09%09e.pri
@@ -1282,16 +1282,18 @@
 race();%0A
+//
 %09%09%7D%0A%0A%09%7D%0A

disable manifest cleanup, too slow.