【问题标题】:Eclipse Plugin - How to open a wizard page in a command handler?Eclipse 插件 - 如何在命令处理程序中打开向导页面?
【发布时间】:2015-03-02 04:59:30
【问题描述】:

我为 Eclipse 编写了一个插件。我在工具栏菜单中有一个按钮。我希望按下它 - 将打开一个向导页面对话框。我已经编写了一个扩展向导并实现 IWizardPage 的类,并且我还编写了所有 5 个相关页面,我只是找不到在命令处理程序中打开它的任何方法。

这是我的代码片段:

命令处理程序:

public class AddProjectHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        return null;
    }
}

向导页面管理器:

public class NewProjectWizardManager extends Wizard implements INewWizard {


    private NewProjectWizardPage1 _page1;
    private NewProjectWizardPage2 _page2;
    private NewProjectWizardPage3 _page3;
    private NewProjectWizardPage4 _page4;
    private NewProjectWizardPage5 _page5;

    // constructor
    public NewProjectWizardManager() {
        super();
        setWindowTitle("New Project");
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
    }

    @Override
    public boolean performCancel() {

        return true;
    }

    @Override
    public void addPages() {
        super.addPages();
        _page1 = new NewProjectWizardPage1();
        addPage(_page1);
        _page2 = new NewProjectWizardPage2(_page1);
        addPage(_page2);
        _page3 = new NewProjectWizardPage3(_page1);
        addPage(_page3);
        _page4 = new NewProjectWizardPage4();
        addPage(_page4);
        _page5 = new NewProjectWizardPage5(_page1);
        addPage(_page5);
    }

    @Override
    public boolean canFinish() {
        IWizardContainer container = getContainer();
        if (_page5.equals(container.getCurrentPage())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public IWizardPage getNextPage(IWizardPage page) {
        IWizardPage nextPage = super.getNextPage(page);
        IWizardContainer container = getContainer();
        if (nextPage != null) {
            if (_page2.equals(container.getCurrentPage()) && _page2.isCheckFinishChecked())
                nextPage = super.getNextPage(super.getNextPage(nextPage));
        }
        return nextPage;
    }

    @Override
    public boolean performFinish() {
}
}

plugin.xml 片段:

<command
                categoryId="com.commands.category"
                description="Add new Project"
                id="com.commands.AddProject"
                name="Add new Project">
</command>


<handler
                class="com.handlers.AddProjectHandler"
                commandId="com.commands.AddProject">
</handler>

你有什么想法吗?

【问题讨论】:

    标签: plugins eclipse-plugin handler wizard new-project


    【解决方案1】:

    使用WizardDialog 显示向导。比如:

    public Object execute(ExecutionEvent event) throws ExecutionException
    {
      Shell activeShell = HandlerUtil.getActiveShell(event);
    
      IWizard wizard = new NewProjectWizardManager();
    
      WizardDialog dialog = new WizardDialog(activeShell, wizard);
    
      dialog.open();
    
      return null;
    }
    

    【讨论】:

    • 完美运行,节省了很多时间。
    【解决方案2】:

    我从 org.eclipse.jdt.ui.actions.AbstractOpenWizardAction 中找到了以下代码。 在 Eclipse3.4 之前,你可以扩展这个类来创建一个 Action。但是现在不推荐使用 action,我想知道 Eclipse.org 是否提供了类似于 AbstractOpenWizardAction 的东西来在命令处理程序模式下做同样的工作。我还没找到呢。

    public void run() {
        Shell shell = getShell();
        if (!(doCreateProjectFirstOnEmptyWorkspace(shell)))
            return;
        try {
            INewWizard wizard = createWizard();
            wizard.init(PlatformUI.getWorkbench(), getSelection());
    
            WizardDialog dialog = new WizardDialog(shell, wizard);
            PixelConverter converter = new PixelConverter(JFaceResources.getDialogFont());
            dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70),
                    converter.convertHeightInCharsToPixels(20));
            dialog.create();
            int res = dialog.open();
            if ((res == 0) && (wizard instanceof NewElementWizard)) {
                this.fCreatedElement = ((NewElementWizard) wizard).getCreatedElement();
            }
    
            notifyResult(res == 0);
        } catch (CoreException e) {
            String title = NewWizardMessages.AbstractOpenWizardAction_createerror_title;
            String message = NewWizardMessages.AbstractOpenWizardAction_createerror_message;
            ExceptionHandler.handle(e, shell, title, message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2018-12-28
      • 1970-01-01
      相关资源
      最近更新 更多