【Eclipse Plug-Ins 】刷新资源管理器功能

最近在做向导开发,中间遇到了很多有意思的事情。今天把工作中遇到的知识点呢梳理一下,以备后用哈~

项目大致背景:

在Eclipse Runtime工作空间创建一个简单的Java项目。

 

 然后呢现在有一个向导Wizard,在向导页面中,我选择到了这个项目中的src目录。

然后在向导中点击Finish按钮后,会执行Wizard类的performFinish类方法。很多处理的事情都放在这里做。

那么假如向导页名字叫做AWizardPage.java,向导名字叫做BWizard.java

即可通过一些操作获取到我们需要的内容。

获取src目录:TheSimplestProject/src 

String str = AWizardPage.getPackageFragmentRootText();     

获取项目名字
String temp =
str.split("/");
projectName = temp[0];

获取Runtime项目的位置:D:/eclipse/runtime-workspace

String projectLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString(); 

当向导执行完毕后,需要刷新资源管理器的内容

/**
 * 
 * @param refreshType    project:refresh the whole project; file:refresh the file; etc
 * @param resourcePath    the relative path of the resource which need to refresh, if want to refresh the project, the resourcePaht can be null.
 * @param projectName    the target peoject name
 */
public static void refresh(String refreshType, String resourcePath, String projectName){
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);
    if (project.exists()) {
        try {
            // Refresh Project
            if ("project".equals(refreshType)) {
                project.refreshLocal(IResource.DEPTH_INFINITE, null);
            }else if ("package".equals(refreshType) || "folder".equals(refreshType)) {
                // Refresh the package or folder
                IFolder folder = project.getFolder(resourcePath);
                if (folder.exists()) {
                    folder.refreshLocal(IResource.DEPTH_INFINITE, null);
                }
            }else if ("packagefile".equals(refreshType) || "folderfile".equals(refreshType) || "file".equals(refreshType)) {
                IFile file = project.getFile(resourcePath);
                if (file.exists()) {
                    file.refreshLocal(IResource.DEPTH_INFINITE, null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

---------------------------------------------------------------------------------
参数实例:

  refreshType:“project”

  resourcePath:""

  projectName: "TheSimplestProject"

 
原文地址:https://www.cnblogs.com/Night-Watch/p/12674618.html