Eclipse RCP: how to get Show View menu instead of a dialog

那年仲夏 提交于 2019-12-24 10:14:24

问题


I've added to my perspective's org.eclipse.ui.menus

<command
      commandId="org.eclipse.ui.views.showView"
      style="pulldown">
</command>

This adds Show View item to main menu, but this item is not a menu (as in the Eclipse Window menu). Instead pressing it shows a dialog where I can select a view. How do I get a menu instead?


回答1:


You have to create ContributionItem class like below:

public class MyShowViewContributionItem extends org.eclipse.ui.internal.ShowViewMenu {
    public MyShowViewContributionItem() {
        this("om.myplugin.myShowViewId");
    }
    public MyShowViewContributionItem(String id) {
        super(org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow(), id);
    }
}

then in your plugin.xml org.eclipse.ui.menus extension:

    <menu
          label="My Show View">
       <dynamic
             class="com.myplugin.MyShowViewContributionItem"
             id="com.myplugin.myShowViewId">
       </dynamic>
    </menu>

Cheers, Max




回答2:


Just to share on my recent experiment in trying to do the same thing, what Max suggested in his answer will work but leaves you using internal code (resulting in a 'Discouraged Access' warning).

Another approach is to build the menu through your applications action bar advisor. Although, this approach will leave you to having to write code (oppose to use providing menu contributions in the plugin XML definition). Consider the following example:

public class ApplicationActionBarAdvisor extends ActionBarAdvisor
{
    private IContributionItem contributionOpenPerspective;
    private IContributionItem contributionShowView; 

    ...

    protected void makeActions(IWorkbenchWindow window)
    {
        ...
        contributionOpenPerspective = ContributionItemFactory.
           PERSPECTIVES_SHORTLIST.create(window);
        contributionShowView = ContributionItemFactory.
           VIEWS_SHORTLIST.create(window);
        ...
    }

    protected void fillMenuBar(IMenuManager menuBar)
    {
        ...
        MenuManager windowMenu = new MenuManager("&Window", 
           IWorkbenchActionConstants.M_WINDOW);
        menuBar.add(windowMenu);

        MenuManager openPerspectiveMenu = new MenuManager("&Open Perspective");
        openPerspectiveMenu.add(perspectivesContribution);
        windowMenu.add(openPerspectiveMenu);

        MenuManager showViewMenu = new MenuManager("Show &View");
        showViewMenu.add(viewsContribution);
        windowMenu.add(showViewMenu);
        ...
    } 
}

A possible downside to this approach is with the interaction between menus created in the advisor and menus created by menu contributions. Since advisor menu items are created before menu contributions, you are left to deal with adding more sorting logic in your menu contributions. This might be fine for most people, however, you lose the 'feel' of a centralized menu structure from org.eclipse.ui.menus (even if the feeling is an illusion when other plugins come into play with their own menu contributions).

I've also included the building of a perspective menu as well; completely option, but I added it if anyone was attempting to perform the same menu building with perspectives.



来源:https://stackoverflow.com/questions/6881581/eclipse-rcp-how-to-get-show-view-menu-instead-of-a-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!