Eclipse 插件,我如何知道 IResourceDeltaVisitor 何时结束处理树节点?

发布于 2024-12-10 15:44:08 字数 3043 浏览 0 评论 0原文

我编写了一个 Eclipse 插件,它基本上允许程序员从 Project Explorer 选择 Java 源,并通过选择相应的 DropDown 菜单选项,它将根据所选文件创建一个接口 .java 文件。 一切工作正常,但现在我需要对工作的更新部分进行编程。 更新要求很简单,我需要监听更改并确定生成接口的源已被修改并重新创建接口文件。 为此,我编写了一个实现 IResourceChangeListener 接口的类。 该类如下所示:

public class DTOChangeListener implements IResourceChangeListener {
    private List<UpdatedUnit> updatedUnits;



    public DTOChangeListener() {
        super();
        this.updatedUnits=new ArrayList<UpdatedUnit>();
    }

    @Override
    public void resourceChanged(IResourceChangeEvent event) {
        try{
            if(event.getType() == IResourceChangeEvent.POST_CHANGE){
                  event.getDelta().accept(this.buildVisitor());
            }
        }catch(CoreException ex){
               ex.printStackTrace();
        }

    }

    protected IResourceDeltaVisitor buildVisitor(){
        IResourceDeltaVisitor result=new IResourceDeltaVisitor() {

        @Override
        public boolean visit(IResourceDelta resDelta) throws CoreException    {
               String resName=resDelta.getResource().getName();
               if(resName==null || resName.equals("")){
                     return true;
               }
               String[] splits=resName.split("\\.");
               String name = splits[0];
               if(name.contains("PropertyAccess")){
                    return false;
               }
               String interfaceName=name + "PropertyAccess";
               String interfaceFile=interfaceName + ".java";
               IResource res=resDelta.getResource();
               if((res instanceof IFolder) || (res instanceof IProject)){
               // Avoid Folder & Project Nodes
                   return true;
               }
               IProject project=res.getProject();
               if(project!=null){
                  if(project.isNatureEnabled("org.eclipse.jdt.core.javanature")){
                     IJavaElement element=JavaCore.create(res);
                     if(element instanceof ICompilationUnit){
                        ICompilationUnit unit=(ICompilationUnit)element;
                        IPath path=res.getProjectRelativePath().removeLastSegments(1);
                        IResource propertyAccess=project.findMember(path.append(interfaceFile));
                        if(propertyAccess!=null){
                           UpdatedUnit updatedUnit=new UpdatedUnit(project, path, unit);
                           updatedUnits.add(updatedUnit);
                           return false;
                        }
                     }
                  }
               }
               return true;
        }
      };
      return result;
    }

    public List<UpdatedUnit> getUpdatedUnits() {
           return updatedUnits;
    }
}

我将侦听器添加到工作区,现在我的问题是:

我如何知道 UpdatedUnits 列表何时完成,以便使用我自己的代码处理列表? 这个问题的一个可能的答案是,不用担心,:

event.getData().accept(this.buildVisitor());

将阻塞,直到访问者的处理完成。 但至少没有像以前那样记录下来。

任何想法将不胜感激。 提前致谢。

丹尼尔

I wrote an Eclipse Plugin that basically allow a programmer to select a Java source from the Project Explorer and by selecting the corresponding DropDown menu option it will creates an interface .java file based on the one selected.
Everything works fine, but now I need to program the update part of the job.
The update requierement is simple, I need to listen for changes and identify that the sources that have the interface generated have been modified and recreate the interface file.
To do this I wrote a class that implements IResourceChangeListener interface.
That class looks like:

public class DTOChangeListener implements IResourceChangeListener {
    private List<UpdatedUnit> updatedUnits;



    public DTOChangeListener() {
        super();
        this.updatedUnits=new ArrayList<UpdatedUnit>();
    }

    @Override
    public void resourceChanged(IResourceChangeEvent event) {
        try{
            if(event.getType() == IResourceChangeEvent.POST_CHANGE){
                  event.getDelta().accept(this.buildVisitor());
            }
        }catch(CoreException ex){
               ex.printStackTrace();
        }

    }

    protected IResourceDeltaVisitor buildVisitor(){
        IResourceDeltaVisitor result=new IResourceDeltaVisitor() {

        @Override
        public boolean visit(IResourceDelta resDelta) throws CoreException    {
               String resName=resDelta.getResource().getName();
               if(resName==null || resName.equals("")){
                     return true;
               }
               String[] splits=resName.split("\\.");
               String name = splits[0];
               if(name.contains("PropertyAccess")){
                    return false;
               }
               String interfaceName=name + "PropertyAccess";
               String interfaceFile=interfaceName + ".java";
               IResource res=resDelta.getResource();
               if((res instanceof IFolder) || (res instanceof IProject)){
               // Avoid Folder & Project Nodes
                   return true;
               }
               IProject project=res.getProject();
               if(project!=null){
                  if(project.isNatureEnabled("org.eclipse.jdt.core.javanature")){
                     IJavaElement element=JavaCore.create(res);
                     if(element instanceof ICompilationUnit){
                        ICompilationUnit unit=(ICompilationUnit)element;
                        IPath path=res.getProjectRelativePath().removeLastSegments(1);
                        IResource propertyAccess=project.findMember(path.append(interfaceFile));
                        if(propertyAccess!=null){
                           UpdatedUnit updatedUnit=new UpdatedUnit(project, path, unit);
                           updatedUnits.add(updatedUnit);
                           return false;
                        }
                     }
                  }
               }
               return true;
        }
      };
      return result;
    }

    public List<UpdatedUnit> getUpdatedUnits() {
           return updatedUnits;
    }
}

I add the Listener to the Workspace, now the question I have is:

How can I know when the updatedUnits List is completed in order to proccess the list with my own code?
One posible answer to this question would be, don't worry, the:

event.getData().accept(this.buildVisitor());

will block until proccessing of the visitor finish.
but at least is not documented like it would.

Any ideas would be appreciated.
Thanks in Advance.

Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-12-17 15:44:08

除非记录为阻止,否则它会阻止。

Unless it's documented to not block, it blocks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文