RCP去掉多余菜单

时间:2022-03-15 02:41:07
 在Eclipse RCP开发中,如果你的dependencies中包含了org.eclipse.ui.ide,在创建菜单栏是你会发现出现了多余的菜单项,例如"Convert Line Delimiters to"和"Last Edit Location"这两个菜单项。如何去掉这些多余的菜单项呢?

可以在你的ApplicationActionBarAdvisor类中添加如下代码(可以在makeActions方法中调用以下代码):

Java代码
/* 
* 去掉因为导入依赖包(org.eclipse.ui.ide)而产生的多余的菜单项,一般 
* 情况下包括三个 
*/ 
@SuppressWarnings("restriction")  
public void removeDuplicateAction()  
{  
    ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();  
    IActionSetDescriptor[] actionSets = reg.getActionSets();  
    // removing annoying gotoLastPosition Message.  
    String actionSetId = "org.eclipse.ui.edit.text.actionSet.navigation"; //$NON-NLS-1$  
    for (int i = 0; i <actionSets.length; i++)  
    {  
        if (!actionSets[i].getId().equals(actionSetId))  
            continue;  
            IExtension ext = actionSets[i].getConfigurationElement()  
                .getDeclaringExtension();  
            reg.removeExtension(ext, new Object[] { actionSets[i] });  
    }  
    // Removing convert line delimiters menu.  
    actionSetId = "org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo"; //$NON-NLS-1$  
    for (int i = 0; i <actionSets.length; i++)  
    {  
        if (!actionSets[i].getId().equals(actionSetId))  
            continue;  
        IExtension ext = actionSets[i].getConfigurationElement()  
                .getDeclaringExtension();  
       reg.removeExtension(ext, new Object[] { actionSets[i] });  
    }  
    actionSetId = "org.eclipse.ui.actionSet.openFiles";  
    for (int i = 0; i <actionSets.length; i++)  
    {  
        if (!actionSets[i].getId().equals(actionSetId))  
            continue;  
        IExtension ext = actionSets[i].getConfigurationElement()  
                .getDeclaringExtension();  
       reg.removeExtension(ext, new Object[] { actionSets[i] });  
    }  


    /*
     * 去掉因为导入依赖包(org.eclipse.ui.ide)而产生的多余的菜单项,一般
     * 情况下包括三个
     */
    @SuppressWarnings("restriction")
    public void removeDuplicateAction()
    {
    ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
    IActionSetDescriptor[] actionSets = reg.getActionSets();
    // removing annoying gotoLastPosition Message.
    String actionSetId = "org.eclipse.ui.edit.text.actionSet.navigation"; //$NON-NLS-1$
    for (int i = 0; i <actionSets.length; i++)
    {
        if (!actionSets[i].getId().equals(actionSetId))
            continue;
            IExtension ext = actionSets[i].getConfigurationElement()
                .getDeclaringExtension();
            reg.removeExtension(ext, new Object[] { actionSets[i] });
    }
    // Removing convert line delimiters menu.
    actionSetId = "org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo"; //$NON-NLS-1$
    for (int i = 0; i <actionSets.length; i++)
    {
        if (!actionSets[i].getId().equals(actionSetId))
            continue;
        IExtension ext = actionSets[i].getConfigurationElement()
                .getDeclaringExtension();
       reg.removeExtension(ext, new Object[] { actionSets[i] });
    }
    actionSetId = "org.eclipse.ui.actionSet.openFiles";
    for (int i = 0; i <actionSets.length; i++)
    {
        if (!actionSets[i].getId().equals(actionSetId))
            continue;
        IExtension ext = actionSets[i].getConfigurationElement()
                .getDeclaringExtension();
       reg.removeExtension(ext, new Object[] { actionSets[i] });
    }
    }