Morpheus
Morpheus

Reputation: 113

Eclipse - how to handle custom projects creation

I want to develop capability in my own Eclipse feature to create projects of my own DSL languages - in other words, I want have wizard so I can click New > My Custom Project, then fill in all necessary fields in wizard pages and then, after perform Finish I want basic folders hierarchy and template files to be created and my DSL nature to be added. I know how to create plugins/features, as good as Wizard. I can create everything in "hard-code", I mean create folders and files, but maybe there is some build-in solution to facilitate it? I found IProject interface with its stuff, is it a good clue?

Upvotes: 0

Views: 78

Answers (2)

Morpheus
Morpheus

Reputation: 113

I have found interesting implementation of org.eclipse.core.resources.* library on Eclipse FAQ site, and it seems to work.

    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);
    }

It is very simple and do its job well, makes possible to easily initialize project's content

Upvotes: 0

howlger
howlger

Reputation: 34255

Assuming your plug-in contains a subfolder template with all template files, you can copy the file structure recursively into the newly created project:

Bundle bundle = MyPlugin.getDefault().getBundle();
String templateFolderName = "template";
for (Enumeration entries = bundle.findEntries(templateFolderName, "*", true);
        entries.hasMoreElements();) {
    URL url = (URL) entries.nextElement();
    String path = url.getPath();
    if (!path.startsWith("/" + templateFolderName + "/"))
        throw new InvocationTargetException(
                new Throwable("Unknown template file: " + path));

    // create folder or file (overwrite if file already exists)
    String targetPath = path.substring(("/" + templateFolderName).length());
    if (path.endsWith("/")) {
        IFolder folder = project.getFolder(targetPath);
        if (! folder.exists()) {
            folder.create(false, true, null);
        }
    } else {
        InputStream in = url.openStream();
        IFile file = project.getFile(targetPath);
        if (file.exists()) {
            file.delete(true, null);
        }
        file.create(in, true, null);
    }
}

(see the source of the code snippet above)

See for example this implementation of a customized New Project wizard dialog.

Upvotes: 1

Related Questions