我正在使用资源更改侦听器来跟踪对项目所做的更改。如果我在项目中删除或创建或保存对文件的任何更改,则会调用此侦听器。我有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
如何区分保存,删除或创建事件。
答案 0 :(得分:2)
测试IResourceChangeEvent.getType()
为PRE_DELETE位返回的位图。使用IWorkspace.addResourceChangeListener(IResourceChangeListener, int)
此article也可能有用。