Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Getting a handle to project root?
Getting a handle to project root? [message #283595] Wed, 06 April 2005 16:24 Go to next message
Eclipse UserFriend
How do I get a handle to a project's root folder? IProject.getFolder()
throws an exception no matter what I pass in. I've tried null, "", "/",
".", "/.", and probably some others too...
Re: Getting a handle to project root? [message #283603 is a reply to message #283595] Wed, 06 April 2005 22:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cburleso.us.ibm.com

To access the workspace in plugin code, you need to get a reference to the
currently available workspace...

IWorkspace myWorkspace =
org.eclipse.core.resources.ResourcesPlugin.getWorkspace();

Once you have a reference to the workspace, you can navigate from there to
the IWorkspaceRoot...

myWorkspace.getRoot();

Once you have the workspace root, you can find other resources. Each
resource type provides methods that can be used to navigate the resource
tree, get handles to resources that may or may not exist, or find specific
resource instances.

The IContainer interface is a general programming interface for resource
constructs that have children. The containers are workspace root, project,
and folder. Following are the resource model interfaces and their methods
that can be used to navigate between resources, all the way down to the
contents of a file:

IWorkspaceRoot
getProjects()
getProject("project_name")
IProject
getReferencedProjects()
getReferencingProjects()
getFolder("folder_name")
getFile("file_name")
IFolder
get_file("file_name")
IResource
getParent()
getProject()
getWorkspace()
findMarkers()
IContainer
members()
getFolder("folder_name")
getFile("file_name")
findMember("member_name")
findMember(IPath)


So... here is an example of this stuff in use. I realize it does not
directly answer your question. However, I think that it is this general
set of interfaces and methods that you need to focus on.

private void createProject(){
IWorkspace workspace = ResourcePlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject newProjectHandle = root.getProject("new_project");
// Get a project descriptor
IPath targetPath = new Path( "d:/workspacehome/" +
newProjectHandle.getName() );
description.setLocation(targetPath);
try{
newProjectHandle.create(description, null);
newProjectHandle.open(null);
} catch (CoreException e) {
//Deal with it
}
}


Finally, I must state that the information I have proivided is not my own
work. It is taken directly from Chapter 23, "Workspace Resource
Programming", of the book: The Java Developer's Guide to Eclipse, Second
Edition, by Jim D'Anjou, Scott Fairbrother, Dan Kehn, John Kellerman, and
Pat McCarthy. ISBN: 0-321-30502-7

I hope this helps you.

Cody Burleson
IBM
Re: Getting a handle to project root? [message #283693 is a reply to message #283595] Fri, 08 April 2005 15:52 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: john.eclipsefaq.org

IProject itself acts as a folder. For example, if you call
IProject.getFile("abc.txt"), it will return the file "abc.txt" directly
in the project "root" folder.
--

Matthew Beermann wrote:
> How do I get a handle to a project's root folder? IProject.getFolder()
> throws an exception no matter what I pass in. I've tried null, "", "/",
> ".", "/.", and probably some others too...
>
Re: Getting a handle to project root? [message #283885 is a reply to message #283603] Wed, 13 April 2005 06:26 Go to previous message
Eclipse UserFriend
Originally posted by: anunayashish.hotmail.com

Hi All,
I tried the mentioned steps but the statement root.getProjects().length is
always returning 0.

IWorkspace myWorkspace =
org.eclipse.core.resources.ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = myWorkspace.getRoot();
System.out.println("===> length = " + root.getProjects().length);

If it is required, I can send a screen shot of the Eclipse IDE(showing 11
projects, 3 open and 8 closed).

Where am I going wrong?
Thanks in advance.

Regards,
Ashish A.
Previous Topic:how to replace PendingUpdateAdapter in DeferredTreeContentManager ?
Next Topic:scheduling rules and multiple projects
Goto Forum:
  


Current Time: Sun Nov 09 21:57:06 EST 2025

Powered by FUDForum. Page generated in 0.04865 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top