GMF context 菜单工具栏

转自http://blog.csdn.net/wu_07/archive/2009/06/21/4286300.aspx GMF 经验点滴积累(二)-菜单工具栏篇 收藏 一、增加主菜单和工具栏项(printPreviewAction) 1、在plugin.xml中配置contributionItemProvider扩展点。 view plaincopy to clipboardprint? <extension point="org.eclipse.gmf.runtime.common.ui.services.action.contributionItemProviders"><?gmfgen generated="true"?><contributionitemprovider class="providers.×ContributionItemProvider"><priority name="Low"></priority><partcontribution id="diagram.part.×DiagramEditorID"><partaction menubarpath="/file/print" id="printPreviewAction"></partaction></partcontribution></contributionitemprovider></extension> 2、在定义的×ContributionItemProvider.java中修改createAction方法,根据actionId.equals??的结果返回相应的Action。 view plaincopy to clipboardprint? protected IAction createAction(String actionId, IWorkbenchPartDescriptor partDescriptor) { if (actionId.equals(PrintPreviewAction.ID)) { return new RenderedPrintPreviewAction( new EnhancedPrintActionHelper()); } return super.createAction(actionId, partDescriptor); } 二、删除ContextMenu子项(addGroup、autoSizeAction) 1、利用扩展点的方法 view plaincopy to clipboardprint? <extension point="org.eclipse.gmf.runtime.common.ui.services.action.contributionItemProviders"><?gmfgen generated="true"?><contributionitemprovider class="providers.×ContributionItemProvider"><priority name="Low"></priority><popupcontribution class="diagram.part.DiagramEditorContextMenuProvider"><popupstructuredcontributioncriteria objectclass="..diagram.edit.parts.*DiagramEditPart"></popupstructuredcontributioncriteria><popuppredefineditem id="addGroup" remove="true"></popuppredefineditem><popuppredefineditem id="autoSizeAction" path="/formatMenu/" remove="true"></popuppredefineditem></popupcontribution></contributionitemprovider></extension> 2、直接编辑代码的方法 view plaincopy to clipboardprint? public class CustomContextMenuProvider extends DiagramContextMenuProvider { public CustomContextMenuProvider (IWorkbenchPart part, EditPartViewer viewer) { super(part, viewer); // TODO Auto-generated constructor stub } public void buildContextMenu(IMenuManager menu) { // SampleAction action = new SampleAction("Sample"); // menu.add(action); super.buildContextMenu(menu); } // 过滤不需要的上下文菜单 public IContributionItem[] getItems() { IContributionItem[] ic = super.getItems(); filterSystemPopMenu(ic); return ic; } } view plaincopy to clipboardprint? //将DiagramEditor的configureGraphicalViewer方法中替换为自定义的ContextMenuProvider protected void configureGraphicalViewer() { super.configureGraphicalViewer(); IDiagramGraphicalViewer viewer = getDiagramGraphicalViewer(); /* customize popup menu */ ContextMenuProvider provider = new CustomContextMenuProvider(this, viewer); viewer.setContextMenu(provider); getSite().registerContextMenu(ActionIds.DIAGRAM_EDITOR_CONTEXT_MENU, provider, viewer); } 三、修改Diagram菜单和工具栏 view plaincopy to clipboardprint? public class CustomDiagramActionBarContributor extends DiagramActionBarContributor { /** * 重载init方法 * @generated NOT */ // public void init(final IActionBars bars) { super.init(bars); IActionBars bar=getActionBars(); IContributionItem[] items=bar.getMenuManager().getItems(); //IContributionItem[] items=bar.getToolBarManager().getItems(); for (IContributionItem i:items) { if (i.getId().equals("diagramMenu")) { SubContributionItem diagramMenu=(SubContributionItem)i; IContributionManager diagramMenuMagager=(IContributionManager) diagramMenu.getInnerItem(); IContributionItem[] subDiagramsMenu=diagramMenuMagager.getItems(); //在diagramMenu下增加一个子菜单 diagramMenuMagager.add(new AboutAction()); for (IContributionItem j:subDiagramsMenu) { //删除arrangeMenu if (j.getId().equals("arrangeMenu")) { diagramMenuMagager.remove(j); } } } } } /** * @generated */ protected Class getEditorClass() { return DfdDiagramEditor.class; } /** * @generated */ protected String getEditorId() { return DfdDiagramEditor.ID; } /** * @generated NOT */ public static class AboutAction extends Action { public AboutAction() { setText("About"); setId("About"); } public void run() { MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "About", "This is a example."); } } }

你可能感兴趣的:(eclipse,UI,xml,.net,J#)