This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
/
ProjectDeletionListenerManager.java
60 lines (52 loc) · 1.96 KB
/
ProjectDeletionListenerManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*******************************************************************************
* 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;
}
}
}