Eclipse RCP项目默认菜单缺失,如何找回全部默认菜单?
解决Eclipse RCP应用缺失默认菜单(含「从文件系统打开项目」)的方案
一、补全必要插件依赖
项目相关菜单依赖IDE核心插件,需在RCP项目的plugin.xml中添加以下依赖:
org.eclipse.ui.ide:提供IDE核心功能,包含项目导入/打开类菜单与动作org.eclipse.core.resources:负责资源管理,是项目操作的基础框架
操作方式:打开plugin.xml的Dependencies标签页,点击Add按钮,搜索并添加这两个插件。
二、配置Workbench窗口菜单贡献
默认RCP项目不会自动加载完整IDE菜单,需在ApplicationWorkbenchWindowAdvisor.java中配置菜单管理器,引入标准菜单组:
- 重写
createMenuBar方法,补充IDE菜单贡献:
@Override public void createMenuBar(IMenuManager menuManager) { super.createMenuBar(menuManager); IMenuManager fileMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_FILE); if (fileMenu != null) { fileMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); PlatformUI.getWorkbench().getActionBuilder().fillActionBars(getWindowConfigurer().getActionBarConfigurer()); } }
- 在
preWindowOpen方法中启用IDE动作集合:
@Override public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(800, 600)); configurer.setShowCoolBar(true); configurer.setShowStatusLine(true); configurer.setShowProgressIndicator(true); configurer.getActionBarConfigurer().setActionBars(new ActionBars(configurer.getActionBarConfigurer().getWindow())); PlatformUI.getWorkbench().getActionBuilder().configureActionBar(configurer.getActionBarConfigurer()); }
三、手动添加「从文件系统打开项目」动作
若上述配置未生效,可直接将该动作添加到菜单:
- 导入必要类:
import org.eclipse.ui.actions.ImportResourcesAction; import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
- 在
createMenuBar方法中添加动作到文件菜单:
@Override public void createMenuBar(IMenuManager menuManager) { super.createMenuBar(menuManager); IMenuManager fileMenu = menuManager.findMenuUsingPath(IWorkbenchActionConstants.M_FILE); if (fileMenu != null) { ImportResourcesAction importAction = new ImportResourcesAction(getWindowConfigurer().getWindow()); importAction.setText(IDEWorkbenchMessages.ImportResourcesAction_importProjectFromFileSystem); fileMenu.add(new Separator()); fileMenu.add(importAction); } }
四、检查自定义透视图配置
如果使用自定义透视图,需确认透视图未隐藏标准菜单:
- 打开透视图定义类(如
XXXPerspective.java) - 检查
createInitialLayout方法,确保没有调用layout.setEditorAreaVisible(false)等隐藏菜单的配置 - 确认透视图包含
org.eclipse.ui.navigator.ProjectExplorer等标准视图,这类视图会触发相关菜单的加载
内容的提问来源于stack exchange,提问作者Kelly Wang
相关产品推荐
相关产品推荐

