package phasereditor.canvas.ui.refactoring;

import java.util.List;

import org.eclipse.core.resources.IFile;
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.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
import org.eclipse.ltk.core.refactoring.resource.DeleteResourceChange;

import phasereditor.canvas.core.CanvasCore;
import phasereditor.canvas.core.CanvasCore.PrefabReference;
import phasereditor.canvas.core.CanvasType;
import phasereditor.canvas.core.Prefab;
import phasereditor.canvas.ui.CanvasUI;
import phasereditor.canvas.ui.CanvasUI.FindPrefabReferencesResult;

public class CanvasFileDeleteParticipant extends DeleteParticipant {

	private IFile _file;
	private CanvasType _canvasType;

	public CanvasFileDeleteParticipant() {
	}

	@Override
	protected boolean initialize(Object element) {
		if (!(element instanceof IFile)) {
			return false;
		}

		IFile file = (IFile) element;

		if (!CanvasCore.isCanvasFile(file)) {
			return false;
		}

		_file = file;

		_canvasType = CanvasCore.getCanvasType(file);

		return true;
	}

	@Override
	public String getName() {
		return "Delete Canvas";
	}

	@Override
	public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
			throws OperationCanceledException {

		RefactoringStatus status = new RefactoringStatus();

		if (!_canvasType.isPrefab()) {
			return status;
		}

		FindPrefabReferencesResult result = CanvasUI.findAllPrefabReferences(new Prefab(_file, _canvasType), pm);

		for (IFile file : result.getFiles()) {
			List<PrefabReference> refs = result.getReferencesOf(file);

			String filepath = file.getProjectRelativePath().toString();

			status.addWarning("The canvas file '" + filepath + "' has " + refs.size() + " prefab '" + _file.getName()
					+ "' instances.", new CanvasFileRefactoringStatusContext(file));
		}

		return status;
	}

	@Override
	public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
		List<IFile> files = CanvasCore.getCanvasDereivedFiles(_file);

		CompositeChange change = new CompositeChange("Delete " + _file.getName());

		for (IFile file : files) {
			change.add(new DeleteResourceChange(file.getFullPath(), true));
		}

		return change;
	}

}

@@ -49,21 +49,131 @@
 ort 
-java.util.Lis
+static java.lang.System.out;%0A%0Aimport java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0A
@@ -2437,16 +2437,102 @@
 ption %7B%0A
+%09%09Set%3CObject%3E elements = new HashSet%3C%3E(Arrays.asList(getProcessor().getElements()));%0A%0A
 %09%09List%3CI
@@ -2587,16 +2587,16 @@
 _file);%0A
-
 %0A%09%09Compo
@@ -2695,16 +2695,52 @@
 iles) %7B%0A
+%09%09%09if (!elements.contains(file)) %7B%0A%09
 %09%09%09chang
@@ -2794,24 +2794,29 @@
 (), true));%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09retur

delete refactoring exclude js file when it is selected.