【问题标题】:Setting a eclipse plug-in to singleton when creating a plug-in project via IPluginContentWizard interface通过IPluginContentWizard接口创建插件项目时设置eclipse插件为单例
【发布时间】:2019-03-31 01:44:46
【问题描述】:

我正在开发一个实现 org.eclipse.pde.ui.IPluginContentWizard 接口的向导。因此,它在插件项目向导的末尾被添加为插件项目模板。所有文件都将被很好地创建,但项目中有一个错误。插件未声明为单例,扩展扩展点时必须声明为单例。

如何在向导中执行此操作?我认为它需要在 performFinish(IProject 项目, IPluginModelBase 模型, IProgressMonitor 监视器) 中完成,但项目和模型都没有给我这样做的可能性。

编辑:对于未来的读者:我的错误是,我没有通过 API 添加扩展,而是通过“手动”生成 plugin.xml。这导致后台没有机制来完成他们的工作,因此没有设置单例指令。

【问题讨论】:

  • 执行单例的代码似乎在org.eclipse.pde.internal.core.bundle.BundlePluginBase 中。我不知道这是怎么称呼的。

标签: eclipse plugins wizard eclipse-pde


【解决方案1】:

这种方式太长了,我们用更多的PDE API吧:

首先,定义模板部分

import org.eclipse.pde.ui.templates.OptionTemplateSection;

public class YourTemplateSection extends OptionTemplateSection {

    //implement abstract methods according your needs

    @Override
    protected void updateModel(IProgressMonitor monitor) throws CoreException {
        IPluginBase plugin = model.getPluginBase();

        //do what is needed

        plugin.add(extension);//here the "singleton" directive will be set

    }
}

然后使用向导的部分

import org.eclipse.pde.ui.templates.ITemplateSection;
import org.eclipse.pde.ui.templates.NewPluginTemplateWizard;

public class YourContentWizard extends NewPluginTemplateWizard {

    @Override
    public ITemplateSection[] createTemplateSections() {
        return new ITemplateSection[] { new YourTemplateSection() };
    }

}

【讨论】:

  • 我的错误是我没有通过 API 添加扩展。不幸的是,当时我的知识还不够,并且手动创建了 plugin.xml,这显然忽略了单例指令的启用。你的方法也可能更适合我分配的任务。
【解决方案2】:

如果有人犯了和我一样的菜鸟错误,我想发布我后来重新访问该项目后提出的解决方案:

不要手动创建plugin.xml,使用插件模型的PDE API来添加扩展。

org.eclipse.pde.ui.IPluginContentWizard 实现的performFinish(...) 方法中这样做:

try {
    IPluginExtension extension = model.getExtensions().getModel().getFactory().createExtension();
    extension.setPoint("org.eclipse.elk.core.layoutProviders");
    IPluginElement provider = model.getPluginFactory().createElement(extension);
    provider.setName("provider");
    provider.setAttribute("class", id + "." + algorithmName + "MetadataProvider");
            extension.add(provider);
    model.getExtensions().add(extension);
} catch (CoreException e) {
    e.printStackTrace();
}

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 2012-01-26
    • 2011-12-21
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多