eclipse资源监听
1.资源监听必须实现IResourceChangeListener接口
- IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IResourceChangeListener listener = new IResourceChangeListener() {
- publicvoid resourceChanged(IResourceChangeEvent event) {
- System.out.println("Something changed!");
- }
- };
- workspace.addResourceChangeListener(listener);
- //... some time later one ...
- workspace.removeResourceChangeListener(listener);
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResourceChangeListener listener = new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
System.out.println("Something changed!");
}
};
workspace.addResourceChangeListener(listener);
//... some time later one ...
workspace.removeResourceChangeListener(listener);
添加资源监听:IWorkspace.addResourceChangeListener(
Listener)
删除资源监听:IWorkspace.removeResourceChangeListener(
Listener)
2.创建资源的样本
- IWorkspace workspace = ResourcesPlugin.getWorkspace();
- final IProject project = workspace.getRoot().getProject("My Project");
- IWorkspaceRunnable operation = new IWorkspaceRunnable() {
- publicvoid run(IProgressMonitor monitor)throws CoreException {
- int fileCount =10;
- project.create(null);
- project.open(null);
- for (int i =0; i < fileCount; i++) {
- IFile file = project.getFile("File" + i);
- file.create(null, IResource.NONE,null);
- }
- }
- };
- workspace.run(operation, null);
IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProject project = workspace.getRoot().getProject("My Project");
IWorkspaceRunnable operation = new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
int fileCount = 10;
project.create(null);
project.open(null);
for (int i = 0; i < fileCount; i++) {
IFile file = project.getFile("File" + i);
file.create(null, IResource.NONE, null);
}
}
};
workspace.run(operation, null);
3.一个简单的完整的资源监听实现类
- publicclass DocIndexUpdaterimplements IResourceChangeListener {
- private TableViewer table;//assume this gets initialized somewhere
- privatestaticfinal IPath DOC_PATH =new Path("MyProject/doc");
- publicvoid resourceChanged(IResourceChangeEvent event) {
- //we are only interested in POST_CHANGE events
- if (event.getType() != IResourceChangeEvent.POST_CHANGE)
- return;
- IResourceDelta rootDelta = event.getDelta();
- //get the delta, if any, for the documentation directory
- IResourceDelta docDelta = rootDelta.findMember(DOC_PATH);
- if (docDelta ==null)
- return;
- final ArrayList changed =new ArrayList();
- IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
- publicboolean visit(IResourceDelta delta) {
- //only interested in changed resources (not added or removed)
- if (delta.getKind() != IResourceDelta.CHANGED)
- returntrue;
- //only interested in content changes
- if ((delta.getFlags() & IResourceDelta.CONTENT) ==0)
- returntrue;
- IResource resource = delta.getResource();
- //only interested in files with the "txt" extension
- if (resource.getType() == IResource.FILE &&
- "txt".equalsIgnoreCase(resource.getFileExtension())) {
- changed.add(resource);
- }
- returntrue;
- }
- };
- try {
- docDelta.accept(visitor);
- } catch (CoreException e) {
- //open error dialog with syncExec or print to plugin log file
- }
- //nothing more to do if there were no changed text files
- if (changed.size() ==0)
- return;
- //post this update to the table
- Display display = table.getControl().getDisplay();
- if (!display.isDisposed()) {
- display.asyncExec(new Runnable() {
- publicvoid run() {
- //make sure the table still exists
- if (table.getControl().isDisposed())
- return;
- table.update(changed.toArray(), null);
- }
- });
- }
- }
- }