Eclipse RCP:我可以在编辑器部件中添加工具栏吗?

时间:2013-04-11 12:53:57

标签: eclipse-rcp toolbar

我成功地通过将以下块添加到org.eclipse.ui.menus扩展点来为我的RCP应用程序添加特定于视图的工具栏:

  <menuContribution
        allPopups="false"
        locationURI="toolbar:my.package.path.views.ClassOfMyView">
     <dynamic
           class="my.package.path.toolbars.ViewToolBar"
           id="MyViewToolbar">
     </dynamic>
  </menuContribution>

然而,尝试对我的编辑部分做同样的事情似乎不起作用:

  <menuContribution
        allPopups="false"
        locationURI="toolbar:my.package.path.views.ClassOfMyEditorPart">
     <dynamic
           class="my.package.path.toolbars.EditorPartToolBar"
           id="MyEditorpartToolbar">
     </dynamic>
  </menuContribution>

是否有一些我遗漏的明显事实,或者这在RCP中根本不受支持?

2 个答案:

答案 0 :(得分:3)

这些是编辑和观点之间的一些重要视觉差异:

  • 编辑器为顶级菜单和工具栏提供其他操作;观点不*
  • Views有一个独立的菜单栏和工具栏;编辑不
  • 透视图中最多只有一个活动编辑器;可以有任意数量的活动视图
  • 每个“类型”最多只有一个视图;可以有任意数量的编辑*
  • 编辑都被放置在“编辑区”;视图放置在许多“视图堆栈”

使用RCP平台的各种功能可以解决其中一些差异(标有*以上):

  • 界面ISaveblePart可以为视图提供与编辑器相同的生命周期。
  • 当视图处于活动状态时,menues扩展点可以将项目添加到主菜单和工具栏。
  • views扩展点可用于允许多个相同类型的视图。

使用Eclipse 4,您还可以在同一堆栈或文件夹中组合编辑器和视图。

但添加菜单或工具栏与视图一样需要更多工作!但它可以使用Presentation API完成,其中大部分视觉差异都可以解决。

另外一条评论:不是 toolbar:my.package.path.views.ClassOfMyView,而是toolbar:id-of-the-view

答案 1 :(得分:3)

关于Tonny Madsen给出的关于Presentation API的提示,我已经找到了解决问题的方法。我的解决方案基于以下文章:

为了使我的解决方案有效,我最终改变了两个文件和一个新类:

在plugin.xml文件中,我添加了以下扩展名:

<extension point="org.eclipse.ui.presentationFactories">
    <factory
        name="Extended Presentation Factory"
        class="org.eclipse.minicrm.ui.swt.custom.ExtendedPresentationFactory"
        id="org.eclipse.minicrm.ui.swt.custom.ExtendedPresentationFactory"
    />
</extension>

这似乎被Eclipse 4.x忽略了,所以为了满足这些情况,我将以下行添加到plugin_customisation.ini文件中:

org.eclipse.ui/presentationFactoryId = org.eclipse.minicrm.ui.swt.custom.ExtendedPresentationFactory

然后我创建了相应的类:

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.internal.presentations.util.TabbedStackPresentation;
import org.eclipse.ui.presentations.IStackPresentationSite;
import org.eclipse.ui.presentations.StackPresentation;
import org.eclipse.ui.presentations.WorkbenchPresentationFactory;

@SuppressWarnings("restriction")
public class ExtendedPresentationFactory extends WorkbenchPresentationFactory {
  private ToolBarManager m_toolBarManager = null;
  private ToolBar m_toolbar = null;

  @Override
  public StackPresentation createEditorPresentation(final Composite parent, final IStackPresentationSite site) {
    final TabbedStackPresentation presentation = (TabbedStackPresentation) super.createViewPresentation(parent, site);

    m_toolbar = new ToolBar(presentation.getTabFolder().getToolbarParent(), 0);
    m_toolBarManager = new ToolBarManager(m_toolbar);
    m_toolBarManager.add(new MyAction1());
    m_toolBarManager.add(new MyAction2());
    m_toolBarManager.add(new MyAction3());
    m_toolBarManager.update(true);
    presentation.getTabFolder().setToolbar(m_toolbar);

    return presentation;
  }
}

这会在我的编辑区域添加一个带有三个按钮的工具栏。

我仍然需要摆弄一些更精细的点(位置等),但是一开始它满足了我对编辑区域中工具栏的需求。

相关问题