简体   繁体   中英

how to get different workspace corresponding to project selection

I am working on eclipse plugin. In project explorer there are several projects and i have store those projects in different Workspace (i have add new project but different workspace). For getting selected project's workspace, i use following code

ResourcesPlugin.getWorkspace().getRoot().getLocation().toString();

but it always returns the workspace that i have selected when i open project first time. Is there any way to get different workspace corresponding to different project selection from project explorer.

In your project selection listener, you can do something like this. I hope you're referring to standard eclipse project explorer.

ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
    IStructuredSelection structured = (IStructuredSelection) service.getSelection(IPageLayout.ID_PROJECT_EXPLORER);
    if (structured instanceof IStructuredSelection)
    {
        Object selectedObject = ((IStructuredSelection) structured).getFirstElement();
        if (selectedObject instanceof IAdaptable)
        {
            IResource resource = (IResource) ((IAdaptable) selectedObject).getAdapter(IResource.class);
            if (resource != null)
            {
                IProject project = resource.getProject();
                 IPath location = project.getLocation();
            }
        }
    }   

All Projects are created in the current workspace. Eclipse never has more that one workspace active at a time.

If you create a project using a different location you can see that in the project location.

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

IProject project = root.getProject("Project");

IPath pathInWorkspace = project.getFullPath();

IPath actualLocation = project.getLocation();

Replace "Project" with the name of your project, or find the project from the selection.

pathInWorkspace is the workspace relative path and would just be /Project in this case.

actualLocation is the actual location of the project in the file system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM