2
votes

I am developing an Eclipse plug-in wherein I am trying to get input from the user through the Wizard.
After pressing the finish button in the wizard, I'm creating a project in the workspace using the IWorkspace, IProject etc. interfaces.

I want to copy an existing folder (which may further contain sub-folders and files) into the newly created project and show that in the workspace. The folder is not an Eclipse project.

Is there any way to do this?

1

1 Answers

3
votes

A quick-and-dirty solution would copy the existing folder (and all its contents) to the desired location underneath the project location and refresh the project afterwards.

For example:

IProject project = ...
IPath location = project.getLocation();
Files.copy( pathToExistingFolder, location.toFile().toPath() );
project.refreshLocal( IResource.DEPTH_INFINITE, null );

A more appropriate solution would traverse the existing folder and create a copy of each file and folder found therein.

To create a new file,use

IProject project = ...
IFile file = project.getFile( "path/relative/to/prooject" );
file.create( inputStream, IResource.NONE, null );
// or, to override and existing file (file.eists() == true):
file.setContents( inputStream, IResource.NONE, null );

The inputStream is assumed to be backed by the currently traversed file from the existing files folder.

To create a new folder, use

IProject project = ...
IFolder folder = project.getFolder( "path/relative/to/prooject" );
folder.create( IResource.NONE, true, null );

I further assume that your existing files will ultimately end up being stored in a (jared) plug-in. In this case, the latter approach is the only viable as it lets you supply the folder structure and file contents from the plug-in storage.

The Plugin Development Environment (PDE) provides extension points and API to manage and execute templates during project-creation. While you certainly wouldn't want to depend on plug-ins from the PDE, you may want to have a look at the sources: https://github.com/eclipse/eclipse.pde.ui/tree/master/ui/org.eclipse.pde.ui/src/org/eclipse/pde/ui/templates