将FormPage添加到FormEditor时的StackOverflowError

时间:2016-06-15 10:11:02

标签: java eclipse-plugin

我有一个非常基本的FormEditorFormPage

public class CustomFieldSetVersionEditor extends FormEditor {

    @Override
    protected void addPages() {
        try {
            addPage(new MyFormPage(this), getEditorInput());
        } catch (final PartInitException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void doSave(final IProgressMonitor monitor) {}

    @Override
    public void doSaveAs() {}

    @Override
    public boolean isSaveAsAllowed() { return false; }
}

class MyFormPage extends FormPage {

    public MyFormPage(final FormEditor editor) {
        super(editor, "id", "Title");
    }

    @Override
    protected void createFormContent(final IManagedForm managedForm) { }

}

这个公认的复杂和异国情调的用例带来以下例外:

  

java.lang.StackOverflowError的     在org.eclipse.swt.widgets.Widget.getData(Widget.java:525)     at org.eclipse.ui.part.MultiPageEditorPart.getEditor(MultiPageEditorPart.java:599)     at org.eclipse.ui.part.MultiPageEditorPart.getActiveEditor(MultiPageEditorPart.java:523)     at org.eclipse.ui.forms.editor.FormEditor.getActiveEditor(FormEditor.java:430)     at org.eclipse.ui.forms.editor.FormEditor $ FormEditorSelectionProvider.getSelection(FormEditor.java:84)     at org.eclipse.ui.forms.editor.FormEditor $ FormEditorSelectionProvider.getSelection(FormEditor.java:89)     at org.eclipse.ui.forms.editor.FormEditor $ FormEditorSelectionProvider.getSelection(FormEditor.java:89)     at org.eclipse.ui.forms.editor.FormEditor $ FormEditorSelectionProvider.getSelection(FormEditor.java:89)     ...

但是,如果我使用addPage(IFormPage)代替addPage(IEditorPart, IEditorInput)则可行。甚至还有针对此问题的“错误”报告:Bug 283039(这不是错误,因为...没有理由给出。)

我们不需要讨论这是否是一个错误,但我想知道的是:如何在仍然定义FormPage的同时向FormEditor添加IEditorInput

1 个答案:

答案 0 :(得分:1)

FormPage正在初始化两次,第二次与主IEditorSite具有相同的FormEditor。这导致相同的选择提供程序被用作FormPage作为主编辑器,此选择提供程序不期望这样并进入循环。

您可以通过覆盖isEditor的{​​{1}}方法停止第二次初始化并返回true:

FormPage

另请注意,如果您使用@Override public boolean isEditor() { return true; } 调用,页面仍会使用编辑器输入进行初始化(只要addPage(IFormPage)返回isEditor)。