/*******************************************************************************
 * Copyright (c) 2015 Pivotal, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Pivotal, Inc. - initial API and implementation
 *******************************************************************************/
package org.springsource.ide.eclipse.commons.frameworks.core.workspace;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IWorkspace;

/**
 * An adapter/manager for a single listener interested in project deletions
 * from the Eclipse workspace.
 * <p>
 * Create an instance of ProjectDeletionListenerManager and pass it a
 * ProjectDeletionListener to attach it to the workspace.
 * <p>
 * To deregister the listener, dispose the manager.
 *
 * @author Kris De Volder
 */
public class ProjectDeletionListenerManager implements IResourceChangeListener {

	public interface ProjectDeletionListener {
		void projectAboutToBeDeleted(IProject project);
	}

	private IWorkspace workspace;
	private ProjectDeletionListener listener;

	public ProjectDeletionListenerManager(IWorkspace workspace, ProjectDeletionListener listener) {
		this.workspace = workspace;
		this.listener = listener;
		this.workspace.addResourceChangeListener(this, IResourceChangeEvent.PRE_DELETE);
	}

	@Override
	public void resourceChanged(IResourceChangeEvent event) {
		IResource rsrc = event.getResource();
		if (rsrc instanceof IProject) {
			listener.projectAboutToBeDeleted((IProject) rsrc);
		}
	}

	public void dispose() {
		if (listener!=null) {
			workspace.removeResourceChangeListener(this);
			listener = null;
		}
	}

}

@@ -789,16 +789,66 @@
 stener;%0A
+import org.eclipse.core.resources.IResourceDelta;%0A
 import o
@@ -1360,33 +1360,27 @@
 void project
-AboutToBe
+Was
 Deleted(IPro
@@ -1703,16 +1703,17 @@
 nt.P
-RE_DELET
+OST_CHANG
 E);%0A
@@ -1801,21 +1801,245 @@
 urce
- rsrc = event
+Delta _delta = event.getDelta();%0A%09%09if (_delta!=null) %7B%0A%09%09%09IResourceDelta%5B%5D children = _delta.getAffectedChildren();%0A%09%09%09if (children!=null) %7B%0A%09%09%09%09for (IResourceDelta delta : children) %7B%0A%09%09%09%09%09if (delta!=null) %7B%0A%09%09%09%09%09%09IResource rsrc = delta
 .get
@@ -2046,24 +2046,28 @@
 Resource();%0A
+%09%09%09%09
 %09%09if (rsrc i
@@ -2088,16 +2088,132 @@
 ject) %7B%0A
+%09%09%09%09%09%09%09int kind = delta.getKind();%0A%09%09%09%09%09%09%09if (kind==IResourceDelta.REMOVED) %7B%0A%09%09%09%09%09%09%09%09if (!isRename(delta)) %7B%0A%09%09%09%09%09%09
 %09%09%09liste
@@ -2227,17 +2227,11 @@
 ject
-AboutToBe
+Was
 Dele
@@ -2250,25 +2250,180 @@
 t) rsrc);%0A%09%09
-%7D
+%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09private boolean isRename(IResourceDelta delta) %7B%0A%09%09return 0!=(delta.getFlags()&IResourceDelta.MOVED_TO);
 %0A%09%7D%0A%0A%09public

Fix bug in ProjectDeletionListener. Treated renames as deletions