【问题标题】:How to add a pulldown button in a view's toolbar?如何在视图的工具栏中添加下拉按钮?
【发布时间】:2011-02-15 22:07:26
【问题描述】:

我需要在 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>

【问题讨论】:

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


    【解决方案1】:

    我想通了。两种方式:一种使用org.eclipse.ui.viewActions扩展,另一种使用org.eclipse.ui.menus

    使用org.eclipse.ui.viewActions 扩展(eclipse >= 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 >= 3.3)

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

    More info

    【讨论】:

    • 一个很好的资源:@​​987654322@ 通过指定 MyViewId 应该是您希望添加按钮的 Eclipse 视图 ID,特别改进了这个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多