RCP 开发中去掉系统默认的菜单项和工具项

 

//隐藏掉系统中的默认的菜单和工具项

public static void hideSystemMenu() {
  IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
  page.hideActionSet("org.eclipse.ui.WorkingSetActionSet");
  page.hideActionSet("org.eclipse.ui.edit.text.actionSet.annotationNavigation");
  page.hideActionSet("org.eclipse.ui.edit.text.actionSet.navigation");
  page.hideActionSet("org.eclipse.search.searchActionSet");
 }

----------------------------------

//把该方法在postWindowOpen()中调用 一下就ok了,如果是多个透视图的情况,在透视图转换后调用一下该方法就ok了

透视图转换的操作:

public static void switchPerspective(String perspectiveID) {;//perspectiveID 是对应的透视图的ID,是自定义的
  IPerspectiveRegistry reg = PlatformUI.getWorkbench().getPerspectiveRegistry();
  IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
  page.setPerspective(reg.findPerspectiveWithId(perspectiveID));//从新设置透视图,根据透视图ID
 //隐藏到系统的默认菜单项

  hideSystemMenu();
 }

-----------------------------------

其实,隐藏菜单项和工具项也可以通过配置实现,而不是直接像上面硬编码的方式:

对org.eclipse.ui.perspectiveExtensions 透视图扩展点进行扩展:

在perspectiveExtension项下可以选择hiddenMenuItem、hiddenToolBarItem进行相应的隐藏操作。

 

 

 

 

 

 

  1. public void postWindowCreate() {  
  2.         IActionBarConfigurer actionBarConfigurer = getWindowConfigurer()  
  3.                 .getActionBarConfigurer();  
  4.         IContributionItem[] coolItems = actionBarConfigurer.getCoolBarManager()  
  5.                 .getItems();  
  6.         for (int i = 0; i < coolItems.length; i++) {  
  7.             if (coolItems[i] instanceof ToolBarContributionItem) {  
  8.                 ToolBarContributionItem toolbarItem = (ToolBarContributionItem) coolItems[i];  
  9.                 if (toolbarItem.getId().equals(  
  10.                         "org.eclipse.ui.WorkingSetActionSet") || //$NON-NLS-1$  
  11.                         toolbarItem  
  12.                                 .getId()  
  13.                                 .equals(  
  14.                                         "org.eclipse.ui.edit.text.actionSet.annotationNavigation") || //$NON-NLS-1$  
  15.                         toolbarItem  
  16.                                 .getId()  
  17.                                 .equals(  
  18.                                         "org.eclipse.ui.edit.text.actionSet.navigation")) { //$NON-NLS-1$  
  19.                     toolbarItem.getToolBarManager().removeAll();  
  20.                 }  
  21.             }  
  22.         }  
  23.         actionBarConfigurer.getCoolBarManager().update(true);  
  24.     }  

 

 

private static final String[] ACTIONSETID = new String[] { "org.eclipse.ant.ui.actionSet.presentation", //$NON-NLS-1$
         "org.eclipse.debug.ui.breakpointActionSet",//$NON-NLS-1$  
         "org.eclipse.debug.ui.debugActionSet",//$NON-NLS-1$  
         "org.eclipse.debug.ui.launchActionSet",//$NON-NLS-1$  
         "org.eclipse.debug.ui.profileActionSet",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.GoToPackage",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.GoToType", //$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.OpenExternalJavaDoc",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.OpenSuperImplementation", //$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.CopyQualifiedName",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.Open", //$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.OpenTypeHierarchy", //$NON-NLS-1$  
         "org.eclipse.jdt.ui.actions.OpenCallHierarchy",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.JavaElementCreationActionSet",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.JavaActionSet",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.A_OpenActionSet",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.CodingActionSet",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.SearchActionSet",//$NON-NLS-1$  
         "org.eclipse.jdt.ui.text.java.actionSet.presentation",//$NON-NLS-1$  
         "org.eclipse.search.searchActionSet",//$NON-NLS-1$  
         "org.eclipse.team.ui.actionSet",//$NON-NLS-1$  
         "org.eclipse.ui.NavigateActionSet", //$NON-NLS-1$  
         "org.eclipse.ui.edit.text.actionSet.openExternalFile", //$NON-NLS-1$  
         "org.eclipse.ui.edit.text.actionSet.presentation",//$NON-NLS-1$  
         "org.eclipse.ui.edit.text.actionSet.annotationNavigation",//$NON-NLS-1$  
         "org.eclipse.ui.edit.text.actionSet.navigation",//$NON-NLS-1$  
         "org.eclipse.ui.externaltools.ExternalToolsSet",//$NON-NLS-1$  
         "org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo", //$NON-NLS-1$
         "org.eclipse.ui.actionSet.keyBindings",//$NON-NLS-1$  
         "org.eclipse.ui.actionSet.openFiles",//$NON-NLS-1$  
         "org.eclipse.ui.WorkingSetActionSet",//$NON-NLS-1$  
         "org.eclipse.ui.WorkingSetModificationActionSet",//$NON-NLS-1$  
         "org.eclipse.ui.WorkingSetActionSet.toolbar",//$NON-NLS-1$  
         "org.eclipse.update.ui.softwareUpdates",//$NON-NLS-1$  
 };

 /*
  * (non-Javadoc)
  * 
  * @see org.eclipse.ui.application.ActionBarAdvisor#fillCoolBar(org.eclipse.jface.action.ICoolBarManager)
  */
 protected void fillCoolBar(ICoolBarManager coolBar) {
     IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);

     coolBar.add(new ToolBarContributionItem(toolbar, "save")); //$NON-NLS-1$
    

     // toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
     // coolBar.add(new ToolBarContributionItem(toolbar, "ads")); //$NON-NLS-1$
     // AnnouncementContributionItem announcementItem = new AnnouncementContributionItem(Messages
     // .getString("ApplicationActionBarAdvisor.message"));
     // toolbar.add(announcementItem); //$NON-NLS-1$

     ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
     IActionSetDescriptor[] actionSets = reg.getActionSets();
     List list = Arrays.asList(ACTIONSETID);
     for (int i = 0; i < actionSets.length; i++) {
         if (list.contains(actionSets[i].getId())) {
             removeAction(reg, actionSets[i]);
         }
     }
 }

 private void removeAction(final ActionSetRegistry reg, final IActionSetDescriptor actionSet) {
     IExtension ext = actionSet.getConfigurationElement().getDeclaringExtension();
     reg.removeExtension(ext, new Object[] { actionSet });
 }

 

     具体使用方法如下:

1、(移除wizard中的General// 移除文件菜单下导入功能中自带的General

              AbstractExtensionWizardRegistry importWizardRegistry = (AbstractExtensionWizardRegistry) WorkbenchPlugin

                 .getDefault().getImportWizardRegistry();

              IWizardCategory[] categories = wizardRegistry.getRootCategory().getCategories();

              for (int i = 0; i < categories.length; i++){

                     if(categories[i].getId().equals("org.eclipse.ui.Basic")){

                            IWizardDescriptor[] wizard = categories[i].getWizards();

                            IExtensionRegistry registry = Platform.getExtensionRegistry();

                            IExtensionPoint point = registry.getExtensionPoint("org.eclipse.ui.importWizards");

                            IExtension[] extensions = point.getExtensions();

                            for (int j = 0; j < wizard.length; j++){

                                   wizardRegistry.removeExtension(extensions[i],

                                      new Object[] {wizard[j] });

                            }

                     }

              }

 

2、(移除wizard中的General// 移除文件菜单下导出功能中自带的General

              AbstractExtensionWizardRegistry exportWizardRegistry = (AbstractExtensionWizardRegistry) WorkbenchPlugin

                 .getDefault().getExportWizardRegistry();

              IWizardCategory[] categories = wizardRegistry.getRootCategory().getCategories();

              for (int i = 0; i < categories.length; i++){

                     if(categories[i].getId().equals("org.eclipse.ui.Basic")){

                            IWizardDescriptor[] wizard = categories[i].getWizards();

                            IExtensionRegistry registry = Platform.getExtensionRegistry();

                            IExtensionPoint point = registry.getExtensionPoint("org.eclipse.ui.exportWizards");

                            IExtension[] extensions = point.getExtensions();

                            for (int j = 0; j < wizard.length; j++){

                                   wizardRegistry.removeExtension(extensions[i],

                                      new Object[] {wizard[j] });

                            }

                     }

              }

 

3、(移除view中的General// 移除窗口菜单下显示视图中自带的General

              ViewRegistry viewRegistry = (ViewRegistry) WorkbenchPlugin.getDefault().getViewRegistry();

              IViewCategory[] viewCat = viewRegistry.getCategories();

              for (int i = 0; i < viewCat.length; i++){

                     if(viewCat[i].getId().equals("org.eclipse.ui")){

                            IViewDescriptor[] viewDes = viewCat[i].getViews();

                            IExtensionRegistry registry = Platform.getExtensionRegistry();

                            IExtensionPoint point = registry.getExtensionPoint("org.eclipse.ui.views");

                            IExtension[] extensions = point.getExtensions();

                            for (int j = 0; j < viewDes.length; j++){

                                   if(!viewDes[j].getId().equals("org.eclipse.ui.console.ConsoleView")){

                                          viewRegistry.removeExtension(extensions[i],

                                             new Object[] {viewDes[j] });

                                   }

 

                            }

                     }

              }

4、(移除ActionSet中的General

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] });
}

 

你可能感兴趣的:(RCP)