java - 如何使用 StructuredTextEditor 关闭 MultiPageEditor 以显示所有页面的大纲 View ?

标签 java eclipse-plugin

我正在为 Eclipse 开发多页 XML 编辑器,并在第一页使用 StructuredTextEditor。我想知道是否只有在选择文本编辑器时才可以让大纲 View 显示内容。虽然选择的是其他页面之一,但大纲 View 不应显示任何内容。

我在多页编辑器中添加文本编辑器如下:

private StructuredTextEditor textEditor;

@Override
protected void addPages()
{
    textEditor = new StructuredTextEditor();
    addPage(textEditor, getEditorInput());

    // add other pages
}

我还在 plugin.xml 中声明了一个内容类型:

<extension point="org.eclipse.core.contenttype.contentTypes">
     <content-type
         id="artFile"
         name="%content-type.name"
         base-type="org.eclipse.core.runtime.xml"
         file-names="app_registration_template.vm"
         file-extensions="vm">
         <describer class="org.eclipse.core.runtime.content.XMLContentDescriber"/>
     </content-type>
</extension>

我现在如何控制大纲 View ?

最佳答案

我遇到了类似的问题。

@greg-449 在问题评论中提到“内容大纲 View 代码对多页编辑器一无所知,每个编辑器只支持一个内容大纲。”

这是真的,实际上让我知道了如何解决这个问题。我想出的解决方案与在选项卡之间导航时刷新大纲 View (以编程方式关闭并再次打开它)有关。

因此,首先,我在结合了 EMF/GMF 选项卡和用于显示 XML 的 StructuredTextEditor 的多页编辑器中应用了该解决方案。

你应该已经拥有的

您的多页编辑器应该覆盖/添加以下方法。请注意,这不是即插即用的情况,请搜索如何创建自定义大纲页面或如何基于 editorInput 调用默认大纲页面。我添加这个的原因是为了理解。


(注意 showOutlineView 方法)它返回 false,因为我们要禁用其他 View 。但是,如果您愿意,可以为每位编辑提供不同的大纲。为此,您需要始终在 showOutlineView 中返回 true 并在 getContentOutlinePage() 方法中以不同方式初始化大纲页面。可能根据编辑器的要求对页面进行不同的实现。


/**
 * This is how the framework determines which interfaces we implement.
*/
@SuppressWarnings("rawtypes")
@Override
public Object getAdapter(Class key) {
    if (key.equals(IContentOutlinePage.class)) {
        return showOutlineView() ? getContentOutlinePage() : null;
    } else {
        return super.getAdapter(key);
    }
}

/**
 * This accesses a cached version of the content outliner.
 */
public IContentOutlinePage getContentOutlinePage() {
    if (contentOutlinePage == null) {
        //your outlinepage from your editor
        //or create a custom page(s)
    }
    return contentOutlinePage;
}

/**
 * Returns whether the outline view should be presented to the user.
 */
protected boolean showOutlineView() {
    //e.g if StructuredTextEditor xmlEditor;
    //like this we force the editor getAdapter contentOutline part
    //to return false, and therefore the contentoutline will get a value of
    //null that results in "an outline in not available" in the outline view
    if(getActiveEditor() != xmlEditor)) {
        return false;
    }
    return true;
}

如何应用解决方案

为了应用该解决方案,您需要修改 pageChange 方法并引入一种以编程方式刷新大纲 View 并适当调用它的方法。

public void refreshOutlineView() {
    //get the activePage
    IWorkbenchPage wp =  PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    //Find desired view by its visual ID
    IViewPart myView=wp.findView("org.eclipse.ui.views.ContentOutline");

    //Hide the view :
    wp.hideView(myView);
    try {
      //show the view again     
      PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("org.eclipse.ui.views.ContentOutline");
    } catch (PartInitException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}

@Override
protected void pageChange(int pageIndex) {
    refreshOutlineView();
    ....
    ....
    ....
}

解决方案是做什么的/它是如何做的

编辑器的大纲 View 在编辑器初始化时被初始化(例如,当您使用编辑器打开文件时)。在那之后它被检索并且它不会因缓存而改变。现在,此解决方法所做的是以编程方式关闭大纲 View (以强制进行初始化调用)。进行此调用后,我们可以初始化并显示我们偏好的大纲 View 。


右键单击gif - 在新选项卡中打开以查看它


Gif demonstrating the solution

关于java - 如何使用 StructuredTextEditor 关闭 MultiPageEditor 以显示所有页面的大纲 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694269/

相关文章:

Eclipse StarTeam 插件?

java - 为什么保存在通过自定义类路径容器添加的 jar 中的类不可用于代码完成?

java - try-catch、循环、失败信息显示

java - 尝试将 InputEvent 插入 ArrayDeque 时出现 NullPointerException

eclipse - 无法在 IValidationContext Eclipse EMF 中设置多个 IStatus 消息

eclipse - 如何向 eclipse 添加插件,以便我可以从 Eclipse 运行 QML

eclipse - 选择一个工具来创建/维护自定义 Eclipse 发行版

java - java中如何将对象转换为 double 型数组?

java - 如果两个线程使用不同的监视器,它们是否可以在同一个对象上执行相同的同步代码块?

java - 在 Java 中使用 JSoup 解析 HTML 中的标签数据