eclipse rcp eclipse project菜单的扩展的问题

rcp编程中,可以使用eclipse原有的project菜单项来完成项目的新建,但是这样这能先添加一个project文件,里面的文件得自己添加,自定义的项目中往往包含一些自定义的文件需要在项目新建时一并创建,所以我们考虑修改eclipse自定义的project菜单

通常使用eclipse的project菜单项是通过在ApplicationActionBarAdvisor类中,通过创建

private IWorkbenchAction projectAction ;

protected void makeActions(IWorkbenchWindow window) {
    projectAction = ActionFactory.NEW.create(window);
    projectAction .setText("新建xxx项目");
    register(projectAction );
}

protected void fillMenuBar(IMenuManager menuBar) {
    IMenuManager fileMenu = new MenuManager(TitleServer.TTEMenu_File, IWorkbenchActionConstants.M_FILE);
    IMenuManager newFileMenu = new MenuManager(TitleServer.TTEMenu_File_new, "new");
    newFileMenu .add(projectAction );
    fileMenu.add(newFileMenu);
}

这样点击菜单就可以创建一个空的项目,如下图所示

这并不能达到我们的要求,因此我查看了ActionFactory.NEW的代码,明白了new action的调用是通过id,但是能力有限修改.class源码我不知道咋弄,也没时间深入的研究,不过我发现了new action对应的commandId是org.eclipse.ui.newWizard

因此我就尝试通过直接通过commandId调用new action,结果成功了

    IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
        .getActivePage().getActivePart().getSite().getService(IHandlerService.class);
    try {
        handlerService.executeCommand("org.eclipse.ui.newWizard", null);
    } catch (Exception ex) {
	    throw new RuntimeException("executing command error!");
    }

因此我在File菜单上换掉了系统的action,自己重新定义了一个IAction类

private IWorkbenchAction projectAction ;

protected void fillMenuBar(IMenuManager menuBar) {
    IMenuManager fileMenu = new MenuManager(TitleServer.TTEMenu_File, IWorkbenchActionConstants.M_FILE);
    IMenuManager newFileMenu = new MenuManager(TitleServer.TTEMenu_File_new, "new");
    newFileMenu.add(new TTEProjectNew(window));
    fileMenu.add(newFileMenu);
}

在IAction类的runWithEvent(Event event)下通过commandId:org.eclipse.ui.newWizard调用并创建项目,在创建完项目之后再在此项目文件中新建了自定义的文件,并打开了自定义文件对应的自定义编辑器

@Override
	public void runWithEvent(Event event) {
		// TODO Auto-generated method stub
		IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().getActivePart().getSite().getService(IHandlerService.class);
		try {
			handlerService.executeCommand("org.eclipse.ui.newWizard", null);
		} catch (Exception ex) {
			throw new RuntimeException("executing command error!");
		}
		// 获取当前工作空间所有的工程
		IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		if (allProjects.length > 0) {
			IProject newProject = allProjects[allProjects.length - 1];
			final String newProjectPath = newProject.getLocation().toFile().getAbsolutePath();
			System.out.println(newProject.getName());
			// 创建工程
			// 创建默认进度条实现
			NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
			final IFile file = newProject.getFile(new Path(Variable.defaultNetworkTopologyName));
			try {
				InputStream stream = openContentStream();
				if (file.exists()) {
					file.setContents(stream, true, true, nullProgressMonitor);
				} else {
					file.create(stream, true, nullProgressMonitor);
				}
				stream.close();
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//新建文件后打开编辑器
			shell.getDisplay().asyncExec(new Runnable() {
				public void run() {
					IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					IEditorInput editorInput = new TopologyEditorInput(file.getName()
							,newProjectPath + "\\" + file.getProjectRelativePath().toString());
					// 查找要打开的编辑器对象
					IEditorPart editor = page.findEditor(editorInput);
					if (editor != null) {
						page.bringToTop(editor);
					} else {
						try {
							editor = page.openEditor(editorInput, Variable.editorID);
						} catch (PartInitException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}
			});
		}
		
	}

执行结果如下:

猜你喜欢

转载自blog.csdn.net/qq_26991187/article/details/84648869