zoukankan      html  css  js  c++  java
  • Eclipse RCP 资源管理(自定义Project)四 (转)

    eclipse资源监听

    1.资源监听必须实现IResourceChangeListener接口

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
       IResourceChangeListener listener = new IResourceChangeListener() {
          public void resourceChanged(IResourceChangeEvent event) {
             System.out.println("Something changed!");
          }
       };
       workspace.addResourceChangeListener(listener);
    
       //... some time later one ...
       workspace.removeResourceChangeListener(listener);

    添加资源监听:IWorkspace.addResourceChangeListener(Listener)

    删除资源监听:IWorkspace.removeResourceChangeListener(Listener)

    2.创建资源的样本

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
       final IProject project = workspace.getRoot().getProject("My Project");
       IWorkspaceRunnable operation = new IWorkspaceRunnable() {
          public void run(IProgressMonitor monitor) throws CoreException {
             int fileCount = 10;
             project.create(null);
             project.open(null);
             for (int i = 0; i < fileCount; i++) {
                IFile file = project.getFile("File" + i);
                file.create(null, IResource.NONE, null);
             }
          }
       };
       workspace.run(operation, null);

    3.一个简单的完整的资源监听实现类

    public class DocIndexUpdater implements IResourceChangeListener {
          private TableViewer table; //assume this gets initialized somewhere
          private static final IPath DOC_PATH = new Path("MyProject/doc");
          public void resourceChanged(IResourceChangeEvent event) {
             //we are only interested in POST_CHANGE events
             if (event.getType() != IResourceChangeEvent.POST_CHANGE)
                return;
             IResourceDelta rootDelta = event.getDelta();
             //get the delta, if any, for the documentation directory
             IResourceDelta docDelta = rootDelta.findMember(DOC_PATH);
             if (docDelta == null)
                return;
             final ArrayList changed = new ArrayList();
             IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
                public boolean visit(IResourceDelta delta) {
                   //only interested in changed resources (not added or removed)
                   if (delta.getKind() != IResourceDelta.CHANGED)
                      return true;
                   //only interested in content changes
                   if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)
                      return true;
                   IResource resource = delta.getResource();
                   //only interested in files with the "txt" extension
                   if (resource.getType() == IResource.FILE &&
        "txt".equalsIgnoreCase(resource.getFileExtension())) {
                      changed.add(resource);
                   }
                   return true;
                }
             };
             try {
                docDelta.accept(visitor);
             } catch (CoreException e) {
                //open error dialog with syncExec or print to plugin log file
             }
             //nothing more to do if there were no changed text files
             if (changed.size() == 0)
                return;
             //post this update to the table
             Display display = table.getControl().getDisplay();
             if (!display.isDisposed()) {
                display.asyncExec(new Runnable() {
                   public void run() {
                      //make sure the table still exists
                      if (table.getControl().isDisposed())
                         return;
                      table.update(changed.toArray(), null);
                   }
                });
             }
          }
       }

  • 相关阅读:
    html5中利用FileReader来读取文件。
    在响应式布局中,碰到图片不会自动缩放,因此需要一段js脚本
    简单的游戏副本刷新倒计时制作
    在webpack4 中利用Babel 7取消严格模式方法
    vue中按需引入mint-UI报Error: .plugins[3][1] must be an object, false, or undefined
    如何在webpack开发中利用vue框架使用ES6中提供的新语法
    Module not found: Error: Can't resolve '@babel/runtime/helpers/classCallCheck' and Module not found: Error: Can't resolve '@babel/runtime/helpers/defineProperty'
    DOTNET CORE源码分析之IOC容器结果获取内容补充
    DOTNET CORE源码分析之IServiceProvider、ServiceProvider、IServiceProviderEngine、ServiceProviderEngine和ServiceProviderEngineScope
    DOTNET CORE源码分析之IServiceCollection、ServiceCollection和ServiceCollectionDescriptorExtensions
  • 原文地址:https://www.cnblogs.com/yunxiaguo/p/7372341.html
Copyright © 2011-2022 走看看