自定义 RCP 编辑器和视图标题菜单

编辑器和视图标题中有许多默认的菜单,这些菜单不是由 IEditorPart 或者 IViewPart 控制的,而是通过 AbstracPresentationFactory 来初始化和进行处理的。 Eclipse 提供一个默认的实现—— WorkbenchPresentationFactory , 核心代码如下:

public StackPresentation createEditorPresentation(Composite parent,
			IStackPresentationSite site) {
		DefaultTabFolder folder = new DefaultTabFolder(parent,
				editorTabPosition | SWT.BORDER, site
						.supportsState(IStackPresentationSite.STATE_MINIMIZED),
				site.supportsState(IStackPresentationSite.STATE_MAXIMIZED));

		/*
		 * 
		 * Set the minimum characters to display, if the preference is something
		 * 
		 * other than the default. This is mainly intended for RCP applications
		 * 
		 * or for expert users (i.e., via the plug-in customization file).
		 */
		final IPreferenceStore store = PlatformUI.getPreferenceStore();
		if (store
				.contains(IWorkbenchPreferenceConstants.EDITOR_MINIMUM_CHARACTERS)) {
			final int minimumCharacters = store
					.getInt(IWorkbenchPreferenceConstants.EDITOR_MINIMUM_CHARACTERS);
			if (minimumCharacters >= 0) {
				folder.setMinimumCharacters(minimumCharacters);
			}
		}
		PresentablePartFolder partFolder = new PresentablePartFolder(folder);
		TabbedStackPresentation result = new TabbedStackPresentation(site,
				partFolder, new StandardEditorSystemMenu(site));
		DefaultThemeListener themeListener = new DefaultThemeListener(folder,
				result.getTheme());
		result.getTheme().addListener(themeListener);
		new DefaultMultiTabListener(result.getApiPreferences(),
				IWorkbenchPreferenceConstants.SHOW_MULTIPLE_EDITOR_TABS, folder);
		new DefaultSimpleTabListener(result.getApiPreferences(),
				IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS,
				folder);
		return result;
	}

 这个方法是用来给编辑器添加标题菜单的,默认的实现为 StandardEditorSystemMenu , 跟踪代码可以发现这个类继承自 StandardViewSystemMenu , 不同的地方是多添加了三个菜单项 CloseOthers CloseAll OpenAgain 。 知道这些,如何去实现自己的编辑器标题菜单就很容易了。 StandardEditorSystemMenu 的 关键代码:

public StandardEditorSystemMenu(IStackPresentationSite site) {
		super(site);
		closeOthers = new SystemMenuCloseOthers(site);
		closeAll = new SystemMenuCloseAll(site);
		openAgain = ActionFactory.NEW_EDITOR.create
		(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
		menuManager.add(closeOthers);
     	        menuManager.add(closeAll);
		menuManager.add(new Separator());
		menuManager.add(openAgain);
}

 实现了自己的 PresentationFactory 以 后,让它生效还必须添加一个 Extension —— org.eclipse.ui.presentationFactories ,语法很简单:

<extension

         point="org.eclipse.ui.presentationFactories">

         <factory 

           class="test.PresentationFactory" 

           id="test.PresentationFactory" 

           name="My Presentation"/> 

</extension>

然后在 plugin_customization.ini 文 件中指定要使用的 presentationFactory org.eclipse.ui/presentationFactoryId=test.PresentationFactory

这样 RCP 就 会使用自定义的 PresentationFactory 中的标题菜单 了。

你可能感兴趣的:(eclipse,UI)