How to create/add resources
From BioclipseWiki
| Responsible author: | Ola |
| Bioclipse version: | N/Awarning.png"N/A" is not a number. |
| Last updated: | 2009-10-10 |
| Tags: |
Contents |
Introduction
The Bioclispe Explorer (or Bioclipse Navigator, we should decide on a name) has Projects, Folders, and Files which all implement the IResource interface. Project implements IProject, File implements IFile, and Folder implements IFolder. All interfaces hence extend IResource.
Creating resources
To create a resource, you first have to create a resource handle and then tell it to create the resource. The following snippet uses resource handles to create a project, a folder, and a file.
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject project = root.getProject("MyProject");
IFolder folder = project.getFolder("Folder1");
IFile file = folder.getFile("hello.txt");
//at this point, no resources have been created
if (!project.exists()) project.create(null);
if (!project.isOpen()) project.open(null);
if (!folder.exists())
folder.create(IResource.NONE, true, null);
if (!file.exists()) {
byte[] bytes = "File contents".getBytes();
InputStream source = new ByteArrayInputStream(bytes);
file.create(source, IResource.NONE, null);
}
To get the selected project/folder:
ISelection sel=PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
Add the following method to extract the selected resource:
IResource extractSelection(ISelection sel) {
if (!(sel instanceof IStructuredSelection))
return null;
IStructuredSelection ss = (IStructuredSelection) sel;
Object element = ss.getFirstElement();
if (element instanceof IResource)
return (IResource) element;
if (!(element instanceof IAdaptable))
return null;
IAdaptable adaptable = (IAdaptable)element;
Object adapter = adaptable.getAdapter(IResource.class);
return (IResource) adapter;
}
See the following link for the whole story: http://wiki.eclipse.org/FAQ_How_do_I_access_the_active_project%3F
Links
- http://wiki.eclipse.org/FAQ_How_are_resources_created%3F
- http://wiki.eclipse.org/The_Official_Eclipse_FAQs#Workspace_and_Resources_API
- http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html
Facts about How to create/add resourcesRDF feed
| Applies to bioclipse version | warning.png"N/A" is not a number. |
| Has author | Ola + |
| Was last updated | 10 October 2009 + |
