我正在使用资源更改侦听器来跟踪对我的项目所做的更改。如果我在项目中删除、创建或保存对文件的任何更改,则会调用此侦听器。我有 ResourceDelta 对象。有了这个,我怎样才能找到文件是正在创建还是将被删除。下面是我的代码:
在我的插件的激活器类中,我有:
IResourceChangeListener listener = new MyResourceChangeListener();
this.workspace.addResourceChangeListener(this.listener);
在 MyResourceChangeListener 类中,我有:
System.out.println(event.getBuildKind());
System.out.println(event.getSource());
System.out.println(event.getType());
if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
System.out.println("this is post change event");
final IResourceDelta delta = event.getDelta();
System.out.println(delta.getFlags());
System.out.println(delta.getKind());
System.out.println(delta.getFlags());
if (delta.getKind() == IResourceDelta.ADDED) {
System.out.println("this is ADD event");
}
if (delta.getKind() == IResourceDelta.CHANGED) {
System.out.println("this is CHANGED event");
}
if ((delta.getFlags() & IResourceDelta.CONTENT) == 0) {
System.out.println("this is CONTENT event");
}
}
输出总是如下,或者我创建一个类,删除一个类,或者进行更改并保存一个类:
0
org.eclipse.core.internal.resources.Workspace@5f9f1f42
1
this is post change event
0
4
0
this is CHANGED event
this is CONTENT event
如何区分保存、删除或创建事件。