我正在开发一个Eclipse插件,它以只读状态加载所有项目文件(IFile)。
ResourceAttributes resAttr = new ResourceAttributes();
resAttr.setReadOnly(true);
classFile.setResourceAttributes(resAttr);
现在,当用户开始在JavaEditor中写入内容时,会显示一个消息框:
File ABC.java is read-only. Do you wish to make it writable?
我想捕获用户的答案,因为有些文件不应该是只读的。
为了做到这一点,我实现了一个IElementChangedListener,但我不知道如果IFile只读属性改变了怎么办。
class SinfoniaCloudElementChangedListener implements IElementChangedListener {
@Override
public void elementChanged(ElementChangedEvent event) {
Activator.logInfo("SinfoniaCloudElementChangedListener - elementChanged");
try {
handleDelta(event.getDelta());
} catch (JavaModelException e) {
Activator.logError("Error handling delta for ElementChangeListener. ", e);
}
}
private void handleDelta(IJavaElementDelta delta) throws JavaModelException {
IJavaElement element = delta.getElement();
if (element instanceof ICompilationUnit) {
IJavaProject project = element.getJavaProject();
if (project.getElementName().equals(SinfoniaProject.getProjectName(true)) == false) {
return;
}
ICompilationUnit cu = (ICompilationUnit) element;
for (IType type : cu.getAllTypes()) {
if (type != IType.CLASS_FILE)
continue;
switch (delta.getKind()) {
case IJavaElementDelta.CHANGED:
Activator.logInfo("CHANGED - Element:" + cu.getElementName());
// ?????????????????????????????
break;
default:
// do nothing
break;
}
}
}
for (IJavaElementDelta child : delta.getAffectedChildren()) {
handleDelta(child);
}
}
}
谢谢。
==
根据Andrew的建议,IElementChangedListener被IResourceChangeListener取代,但是我仍然不知道如何识别IResource只读属性是否发生了变化。
class ABCResourceChangeListener implements IResourceChangeListener {
@Override
public void resourceChanged(IResourceChangeEvent event) {
IResource res = event.getResource();
try {
switch (event.getType()) {
case IResourceChangeEvent.PRE_CLOSE:
System.out.print("Project ");
System.out.print(res.getFullPath());
System.out.println(" is about to close.");
break;
case IResourceChangeEvent.PRE_DELETE:
System.out.print("Project ");
System.out.print(res.getFullPath());
System.out.println(" is about to be deleted.");
break;
case IResourceChangeEvent.POST_CHANGE:
System.out.println("Resources have changed.");
event.getDelta().accept(new DeltaPrinter());
break;
case IResourceChangeEvent.PRE_BUILD:
System.out.println("Build about to run.");
event.getDelta().accept(new DeltaPrinter());
break;
case IResourceChangeEvent.POST_BUILD:
System.out.println("Build complete.");
event.getDelta().accept(new DeltaPrinter());
break;
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class DeltaPrinter implements IResourceDeltaVisitor {
public boolean visit(IResourceDelta delta) {
IResource res = delta.getResource();
switch (delta.getKind()) {
case IResourceDelta.ADDED:
System.out.print("Resource ");
System.out.print(res.getFullPath());
System.out.println(" was added.");
break;
case IResourceDelta.REMOVED:
System.out.print("Resource ");
System.out.print(res.getFullPath());
System.out.println(" was removed.");
break;
case IResourceDelta.CHANGED:
System.out.print("Resource ");
System.out.print(res.getFullPath());
System.out.println(" has changed.");
int flags = delta.getFlags();
if ((flags & IResourceDelta.CONTENT) != 0) {
System.out.println("--> Content Change");
// ?????????????????????????
}
if ((flags & IResourceDelta.REPLACED) != 0) {
System.out.println("--> Content Replaced");
}
if ((flags & IResourceDelta.MARKERS) != 0) {
System.out.println("--> Marker Change");
IMarkerDelta[] markers = delta.getMarkerDeltas();
// if interested in markers, check these deltas
for (int j=0; j<markers.length;j++) {
IMarkerDelta markerDelta = markers[j];
if (markerDelta.isSubtypeOf(IMarker.SOURCE_ID)) {
switch (markerDelta.getKind()) {
case IResourceDelta.ADDED :
System.out.println("--> Marker Delta Added " );
break;
case IResourceDelta.REMOVED :
System.out.println("--> Marker Delta Removed " );
break;
case IResourceDelta.CHANGED:
System.out.println("--> Marker Delta Changed ");
break;
}
}
}
}
break;
}
return true; // visit the children
}
}
答案 0 :(得分:0)
IElementChangedListener
不是你想要的。你应该使用IResourceChangeListener
。监听器监听IResourceDelta
。 IResourceDelta
有一个名为getFlags()
的方法应该返回您想要的内容。
有关详细信息,请参阅IWorkspace.addResourceChangeListener(IResourceChangeListener, int)
的javadoc。