3

我正在开发一个以只读状态加载所有项目文件(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
}
}
4

1 回答 1

0

IElementChangedListener不是你想要的。您应该使用IResourceChangeListener. 监听器监听IResourceDeltas。 IResourceDelta有一个调用的方法getFlags()应该返回你想要的。

IWorkspace.addResourceChangeListener(IResourceChangeListener, int)有关更多信息,请参阅 javadoc 。

于 2012-08-29T05:15:52.100 回答