如何在 RCP-Perspective 中保存编辑器?

How to save the editor in a RCP-Perspective?

在我的 RCP 插件项目中,我有一个视图和一个编辑器。我还设置了一个透视图,其中创建了视图和编辑器。

问题是编辑器的内容(在createPartControl-方法中描述)没有显示。如果我更改 run configurations 并在 Workspace Data 中设置 clear,我将获得默认视角,当我单击 Window -> Perspective -> Open Perspective -> Other... 时,我可以选择我的 "BDD Perspective" 和编辑器及其内容和视图可见。 Perspective with editor and view 如果 Workspace Data 中的 clear 未设置,我的 "BDD Perspective" 会出现,但编辑器为空。 Perspective with empty editor and view

有谁知道如何使编辑器的内容在用户定义的视角中可见?

这是相关代码。在透视图中有一个名为 "CallBDDEditor" 的处理程序,它打开编辑器:

public class Perspective implements IPerspectiveFactory {

String ID = "bddperspective";

@Override
public void createInitialLayout(IPageLayout layout) {

    String editorArea = layout.getEditorArea();

    IFolderLayout right = layout.createFolder("right", IPageLayout.RIGHT, .15f, editorArea);
    right.addView(DiagramView.ID);

    IViewLayout viewLayout = layout.getViewLayout(DiagramView.ID);
    if (viewLayout != null) {
        viewLayout.setCloseable(false);
        viewLayout.setMoveable(false);
    }
    layout.setEditorAreaVisible(true);
    layout.setFixed(true);
    try {
        new CallBDDEditor().execute(null);
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = window.getActivePage();
    IPerspectiveRegistry perspectiveRegistry = window.getWorkbench().getPerspectiveRegistry();
    IPerspectiveDescriptor personalPerspectiveDescriptor = perspectiveRegistry.findPerspectiveWithId(ID);

    if (page != null && personalPerspectiveDescriptor != null) {
        page.savePerspectiveAs(personalPerspectiveDescriptor);
    }
}}

public class CallBDDEditor extends AbstractHandler {

public static final String ID = "bdd.klighd.openBDDEditor";

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = window.getActivePage();
    try {
        page.openEditor(new BDDEditorInput("test of editor input"), BDDEditor.ID);
    } catch (PartInitException e) {
        e.printStackTrace();
    }
    return null;
}

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

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

@Override
public void removeHandlerListener(IHandlerListener handlerListener) {
}}

public class BDDEditor extends EditorPart {

public static final String ID = "bdd.klighd.BDDEditor";
boolean dirty = false;
private String formula_ = "f(x,y,z) = x!y + z!x";

ToolItem inputItem_;
ToolItem showItem_;
ToolItem simplifyItem_;
ToolItem removeItem_;
ToolItem cancelItem_;
Composite parent_;
ToolBar bar_;
private Label formulaLbl_;

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.setSite(site);
    super.setInput(input);
}

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

@Override
public void createPartControl(Composite parent) {
    parent_ = parent;
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    parent_.setLayout(layout);
    formulaLbl_ = new Label(parent_, SWT.NONE);
    formulaLbl_.setText("");
    bar_ = new ToolBar(parent_, SWT.BORDER | SWT.VERTICAL);

    // create items
    inputItem_ = new ToolItem(bar_, 0);
    showItem_ = new ToolItem(bar_, SWT.PUSH);
    simplifyItem_ = new ToolItem(bar_, SWT.PUSH);
    removeItem_ = new ToolItem(bar_, SWT.PUSH);
    cancelItem_ = new ToolItem(bar_, 0);

    // set text on items
    inputItem_.setText("INPUT");
    showItem_.setText("SHOW");
    simplifyItem_.setText("SIMPLIFY");
    removeItem_.setText("REMOVE");
    cancelItem_.setText("CANCEL");

    // disable show-, simplify-, remove- and cancelItems
    showItem_.setEnabled(false);
    simplifyItem_.setEnabled(false);
    removeItem_.setEnabled(false);
    cancelItem_.setEnabled(false);

    DropdownSelectionListener listenerInput = new DropdownSelectionListener(inputItem_);
    listenerInput.add("As String", true);
    listenerInput.add("From File", false);
    inputItem_.addSelectionListener(listenerInput);

    bar_.pack();
    parent_.pack();
}

public IEditorInput getEditorInput() {
    return super.getEditorInput();
}

@Override
public void doSave(IProgressMonitor monitor) {
}

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

@Override
public void setFocus() {
}

@Override
public void dispose() {
    super.dispose();
}

@Override
public void doSaveAs() {
}}

我已经在 Extensions ElementFactory 中添加了,就像您在此处看到的那样 ElementFactory。这是我的 ElementFactory 代码:

public class ElementFactory implements IElementFactory, IAdaptable {

String ID = "bdd.klighd.factory";

@Override
public IAdaptable createElement(IMemento memento) {
    return new BDDEditorInput("test of editor input");
}

@Override
public <T> T getAdapter(Class<T> adapter) {
    // TODO Auto-generated method stub
    return null;
}

}

这是我的 EditorInput 代码:

public class BDDEditorInput implements IStorageEditorInput, IPersistableElement {

private final String inputString;

public BDDEditorInput(String inputString) {
    this.inputString = inputString;
}

public boolean exists() {
    return true;
}

public ImageDescriptor getImageDescriptor() {
    return null;
}


@Override
public void saveState(IMemento memento) {
    // TODO Auto-generated method stub

}

@Override
public String getFactoryId() {
    return "bdd.klighd.factory";
}

public IPersistableElement getPersistable() {
    return this;
}

public IStorage getStorage() throws CoreException {
    return new IStorage() {
        public InputStream getContents() throws CoreException {
            return new StringBufferInputStream(inputString);
        }

        public IPath getFullPath() {
            return null;
        }

        public String getName() {
            return BDDEditorInput.this.getName();
        }

        public boolean isReadOnly() {
            return false;
        }

        public Object getAdapter(Class adapter) {
            return null;
        }
    };
}

public Object getAdapter(Class adapter) {
    return null;
}

public String getName() {
    return "input name";
}

public String getToolTipText() {
    return "BDD Editor";
}

}

是的,它有效,我的 items/buttons 现在可见了。但是不知道为什么,因为我没有在EditorInput-class中实现它们。但是,它有效!