EclipseRCP开发(三)-如何去除顽固原生菜单项
去除new-project及other菜单
获取所有菜单项,筛选出new菜单,将new菜单清空,再重新从已注册的向导中添加所需菜单。记录之。
if ("new".equals(ii.getId())) {
MenuManager newMenu = (MenuManager) ii;
// 清除现有的所有子项
newMenu.removeAll();
// 手动填充 New Wizard 项目
fillNewWizardMenu(newMenu, window);
}
private void fillNewWizardMenu(MenuManager newMenu, IWorkbenchWindow window) {
// 获取已注册的所有 New Wizard
IWizardRegistry wizardRegistry = PlatformUI.getWorkbench().getNewWizardRegistry();
IWizardCategory[] categories = wizardRegistry.getRootCategory().getCategories();
// 遍历每个分类中的向导New4diacProjectWizard
for (IWizardCategory category : categories) {
//筛选自定义的导航类
if(!category.getId().contains("xxxxx")) {
continue;
}
for (IWizardDescriptor wizard : wizards) {
if (wizard.getId().contains("xxxx")) {
Action action = new Action(wizard.getLabel()) {
@Override
public void run() {
try {
// 打开向导
IWizard newWizard = wizard.createWizard();
WizardDialog wizardDialog = new WizardDialog(window.getShell(), newWizard);
wizardDialog.open();
} catch (Exception e) {
e.printStackTrace();
}
}
};
// 设置向导的图标
ImageDescriptor imageDescriptor = wizard.getImageDescriptor();
if (imageDescriptor != null) {
action.setImageDescriptor(imageDescriptor);
}
// 添加向导项到 New 菜单
newMenu.add(action);
}
}
}
}