使用 IEditorreference 关闭编辑器? (日食 RCP)

发布于 2024-11-29 00:19:59 字数 821 浏览 2 评论 0原文

我有一个用户界面,当我选择一个项目(在树中)然后按“添加”按钮时,我会得到一个新的编辑器。每个项目我都可以得到一个编辑。 (但都有相同的ID) 我的目的是仅关闭 item1 的编辑器,例如,当我按“保存”时。 我可以通过以下方式关闭所有编辑器: getSite().getWorkbenchWindow().getActivePage().closeAllEditors(true);

但不仅仅是我需要关闭的那个。

我认为,这个问题可以使用 IEditorreferences 来解决,但不知道到底如何做! :( 请帮忙:)

List<IEditorReference> editors = new ArrayList<IEditorReference>();               
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
    for (IWorkbenchPage page : window.getPages()) {
        for (IEditorReference editor : page.getEditorReferences()) {
           editors.add(editor);
         }
    }
}

getSite().getWorkbenchWindow().getActivePage().closeEditor(editors.get(index)????,true);

I have a UI in which when I select an item (in a tree) and then press a button "add", I get a new editor. With each item I can get an editor. (but all have the same ID)
My purpose is to close only the editor of item1, for example, when I press "save".
I'm able to close all the editors with:
getSite().getWorkbenchWindow().getActivePage().closeAllEditors(true);

But not only the one that I need to close.

I think, this problem might be solved using the IEditorreferences but don't know exactly how to do it! :(
please help :)

List<IEditorReference> editors = new ArrayList<IEditorReference>();               
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
    for (IWorkbenchPage page : window.getPages()) {
        for (IEditorReference editor : page.getEditorReferences()) {
           editors.add(editor);
         }
    }
}

getSite().getWorkbenchWindow().getActivePage().closeEditor(editors.get(index)????,true);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2024-12-06 00:19:59

可以通过编辑器输入来跟踪编辑器。表示 item1 的对象必须是编辑器输入的一部分...

类似于:

// Creating and opening
MyObject item1 = ... //create item1
// open editor
myInput = new MyEditorInput(item1)
IDE.openEditor(workbenchPage, myInput, MY_EDITOR_ID);

// Closing
tmpInput = new MyEditorInput(item1)
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getActivePage()
                .getEditorReferences();
        List<IEditorReference> relevantEditors = new ArrayList<IEditorReference>();
        for (IEditorReference iEditorReference : editorReferences) {
            if (iEditorReference.getEditorInput().equals(tmpInput)) {
                relevantEditors.add(iEditorReference);
            }
        }
        PlatformUI
                .getWorkbench()
                .getActiveWorkbenchWindow()
                .getActivePage()
                .closeEditors(
                        (IEditorReference[]) relevantEditors.toArray(new IEditorReference[relevantEditors
                                .size()]), true);

确保您已覆盖 EditorInput 的 equals 和 hashCode 以检查包装的 MyObject 实例的相等性

Editor can be tracked with the editor-input. The object representing item1 must be part of your editor-input...

Something like:

// Creating and opening
MyObject item1 = ... //create item1
// open editor
myInput = new MyEditorInput(item1)
IDE.openEditor(workbenchPage, myInput, MY_EDITOR_ID);

// Closing
tmpInput = new MyEditorInput(item1)
IEditorReference[] editorReferences = PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getActivePage()
                .getEditorReferences();
        List<IEditorReference> relevantEditors = new ArrayList<IEditorReference>();
        for (IEditorReference iEditorReference : editorReferences) {
            if (iEditorReference.getEditorInput().equals(tmpInput)) {
                relevantEditors.add(iEditorReference);
            }
        }
        PlatformUI
                .getWorkbench()
                .getActiveWorkbenchWindow()
                .getActivePage()
                .closeEditors(
                        (IEditorReference[]) relevantEditors.toArray(new IEditorReference[relevantEditors
                                .size()]), true);

Make sure that you have overriden the equals and hashCode of your EditorInput to check the equality of the wrapped MyObject-instance

他是夢罘是命 2024-12-06 00:19:59

打开编辑器时,您必须跟踪项目与关联的打开的 IEditorReference 之间的映射,这可以使用简单的 HashMap 对象来完成。

When opening your editor you have to track the mapping between your items and the associated opened IEditorReference This can be done for example using a simple HashMap object.

毅然前行 2024-12-06 00:19:59

感谢汤姆,你的回答很有帮助。

由于每个 IEditorInput 都可以有其可设置的名称,因此我们还可以使用如下所示:

// String str =.....
// str, could be an editor's property

if (iEditorReference.getEditorInput().getName().equals(str))

此外,它还应抛出 PartInitException,如下所示:

//....................

    try {
        for (IEditorReference iEditorReference : editorReferences) {
            if (iEditorReference.getEditorInput().getName().equals(str)) {
                relevantEditors.add(iEditorReference);
            }
        }
    } catch (PartInitException e) {
        e.printStackTrace();
    }
//...................

thanks to Tom, your answer helps a lot.

As each IEditorInput could have its name that can be set, we also can use like followings:

// String str =.....
// str, could be an editor's property

if (iEditorReference.getEditorInput().getName().equals(str))

Besides, it shall throw PartInitException like this:

//....................

    try {
        for (IEditorReference iEditorReference : editorReferences) {
            if (iEditorReference.getEditorInput().getName().equals(str)) {
                relevantEditors.add(iEditorReference);
            }
        }
    } catch (PartInitException e) {
        e.printStackTrace();
    }
//...................
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文