すべてのプロジェクト ファイル (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
}
}