如何在视图的工具栏中添加下拉按钮?

时间:2010-05-05 12:36:58

标签: eclipse-plugin eclipse-rcp eclipse-pde

我需要在Eclipse插件的视图工具栏中添加pulldown button

实际上工具栏中的按钮添加如下:

<extension point="org.eclipse.ui.viewActions">
  <viewContribution id="..." targetId="$MyViewId$">
    <action id="..."
            toolbarPath="action1"
            class="Class extending Action and implementing IViewActionDelegate">
    </action>
  </viewContribution>
</extension>

1 个答案:

答案 0 :(得分:9)

我已经弄清楚了。两种方式:一种使用org.eclipse.ui.viewActions扩展名,另一种使用org.eclipse.ui.menus

使用org.eclipse.ui.viewActions扩展名(eclipse&gt; = 3.5)

  • 动作的样式必须设置为pulldown
    <extension point="org.eclipse.ui.viewActions">
      <viewContribution id="..." targetId="$MyViewId$">
        <action id="..."
                toolbarPath="action1"
                class="xxx.MyAction"
                style="pulldown">
        </action>
      </viewContribution>
    </extension>
  • 操作类必须实现IViewActionDelegate(对视图工具栏有贡献的操作所必需)和IMenuCreator(定义菜单行为)。
    public class RetrieveViolationsViewActionDelegate implements IViewActionDelegate, IMenuCreator
    {
      private IAction action;
      private Menu menu;

      // IViewActionDelegate methods
      ...

      // IMenuCreator methods
      public void selectionChanged(IAction action, ISelection selection)
      {
        if (action != this.action)
        {
          action.setMenuCreator(this);
          this.action = action;
        }
      }

      public void dispose()
      {
        if (menu != null)
        {
          menu.dispose();
        }
      }

      public Menu getMenu(Control parent)
      {
        Menu menu = new Menu(parent);
        addActionToMenu(menu, new ClassImplemententingIAction());
        return menu;
      }

      public Menu getMenu(Menu parent)
      {
        // Not use
        return null;
      }



      private void addActionToMenu(Menu menu, IAction action)
      {
        ActionContributionItem item= new ActionContributionItem(action);
        item.fill(menu, -1);
      }
    }

使用org.eclipse.ui.menus(eclipse&gt; = 3.3)

  • org.eclipse.ui.menus扩展点添加新的菜单贡献。
  • 将位置URI设置为toolbar:IdOfYourView
  • 向此扩展添加工具栏,并为此新工具栏添加新命令。
  • 将命令样式更改为pulldown
  • 创建新的menucontribution并将locationURI设置为menu:IdOfThePullDownCommand
  • 向此菜单添加命令。

More info