简体   繁体   English

Eclipse插件:扩展INewWizard的新文件向导,如何在其中创建所选项目

[英]Eclipse plugin: New file wizard extending INewWizard, how to get selected project to create in it

I'm currently writing an eclipse plugin, and in it, I have a new project creation and a new file creation wizard. 我当前正在编写一个eclipse插件,在其中,我有一个新的项目创建和一个新的文件创建向导。

In the new project wizard, I create it, so I have no problem getting it and creating new files in it. 在新的项目向导中,我创建了它,因此我可以轻松地获取它并在其中创建新文件。 (like creating the Main class for your project) (例如为您的项目创建Main类)

But when I'm in my New file wizard, I have literally no idea how to select the right project, and I would like some help please. 但是,当我处于“新建文件”向导中时,实际上我不知道如何选择正确的项目,请给我一些帮助。 Since it is a wizard, I would like to avoid needing an opened editor, and since it's a new wizard, it doesn't have a handler so I can't get it from there... 由于它是一个向导,因此我想避免需要一个打开的编辑器,并且由于它是一个新的向导,因此它没有处理程序,因此无法从那里获取它。

Thank you in advance, Cordially, 预先感谢您,诚挚地,

Okay, I looked at it from another approach and it feels quite stupid right now. 好的,我从另一种方法看了它,现在感觉很愚蠢。

When you initialize your wizard, you get an init method that contains the workbench and the selection, so you can keep it. 初始化向导时,您将获得一个包含工作台和所选内容的init方法,因此可以保留它。

private IWorkbench wb;
private IStructuredSelection sel;
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
    wb = workbench;
    sel = selection;
}

Then, I had found a code snippet earlier on Eclipse website, linked under here, that I had to change a bit, and it does what I want it to do. 然后,我早些时候在Eclipse网站上找到了一个链接在这里的代码片段,我必须对其进行一些更改,并且它可以实现我想要的功能。

// Get selected resource (can get project from it)
// https://wiki.eclipse.org/FAQ_How_do_I_access_the_active_project%3F
private IResource extractSelection() {
    Object element = sel.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;
}

With this, I can get my project in my wizard by doing 这样,我可以通过执行以下操作将项目添加到向导中

IProject project = extractSelection().getProject();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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