gpt4 book ai didi

java - Eclipse Plugin IFile 只读属性更改

转载 作者:行者123 更新时间:2023-12-03 20:24:55 26 4
gpt4 key购买 nike

我正在开发一个以只读状态加载所有项目文件 (IFile) 的 Eclipse 插件。

        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
}
}

最佳答案

IElementChangedListener 不是您想要的。您应该使用 IResourceChangeListener。监听器监听 IResourceDeltaIResourceDelta 有一个名为 getFlags() 的方法,它应该返回您想要的内容。

有关详细信息,请参阅 IWorkspace.addResourceChangeListener(IResourceChangeListener, int) 的 javadoc。

关于java - Eclipse Plugin IFile 只读属性更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12169497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com