Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

eclipse plugin - How to add a pulldown button in a view's toolbar?

I need to add a pulldown button to a view's toolbar in an Eclipse plugin.

Actually buttons in the toolbar are added like that :

<extension point="org.eclipse.ui.viewActions">
  <viewContribution id="..." targetId="$MyViewId$">
    <action id="..."
            toolbarPath="action1"
            class="Class extending Action and implementing IViewActionDelegate">
    </action>
  </viewContribution>
</extension>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've figured it out. Two ways: one using org.eclipse.ui.viewActions extension, the other with org.eclipse.ui.menus

Using org.eclipse.ui.viewActions extension (eclipse >= 3.5)

  • action's style must set to pulldown
    <extension point="org.eclipse.ui.viewActions">
      <viewContribution id="..." targetId="$MyViewId$">
        <action id="..."
                toolbarPath="action1"
                class="xxx.MyAction"
                style="pulldown">
        </action>
      </viewContribution>
    </extension>
  • action class must implement IViewActionDelegate (required for an action contributing to a view toolbar) and IMenuCreator (defines the menu behavior).
    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);
      }
    }

Using org.eclipse.ui.menus (eclipse >= 3.3)

  • Add a new menucontribution to the org.eclipse.ui.menus extension point.
  • Set the location URI to toolbar:IdOfYourView
  • Add a toolbar to this extension and a new command to this new toolbar.
  • Change the command style to pulldown
  • Create a new menucontribution and set the locationURI to menu:IdOfThePullDownCommand
  • Add commands to this menu.

More info


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...