首页 > 解决方案 > Eclipse 中自定义编辑器的源代码菜单

问题描述

我正在编写一个 Eclipse 插件来在我自己的自定义编辑器中显示自己的文件类型的内容。

编辑器应该启用源菜单并使其可见(例如 java 编辑器),但我不知道如何启用该源菜单。

有了这个 如何在 Eclipse 中扩展源菜单?(或:它的 locationURI 是什么?) 我可以向源菜单添加动作,现在可见(总是..),但动作被禁用......

这就是我在 plugin.xml 中定义操作的方式:

<extension
      point="org.eclipse.ui.actionSets">
   <actionSet
         id="com.marchesini.mas.rcp.ui.actionset.mps.source"
         label="MPS Source Format">
      <action
               definitionId="com.marchesini.mas.rcp.ui.edit.text.mps.format"
               label="%FormatAction.label"
               retarget="true"
               id="com.marchesini.mas.rcp.ui.actions.Format">
         </action>   
      <action
               definitionId="com.marchesini.mas.rcp.ui.edit.text.mps.indent"
               label="%IndentAction.label"
               retarget="true"
               id="com.marchesini.mas.rcp.ui.actions.Indent">
         </action>   
        <action
               definitionId="com.marchesini.mas.rcp.ui.edit.text.mps.sort_op_def"
               label="%SortOpDefinitionAction.label"
               retarget="true"               id="com.marchesini.mas.rcp.ui.actions.SortOpDefinition">
         </action>     
   </actionSet>
</extension>

这些是命令:

<extension
      point="org.eclipse.ui.commands">
   <category
         id="com.marchesini.mas.rcp.ui.category.source"
         name="MPS Source">
   </category>
   <command
         categoryId="com.marchesini.mas.rcp.ui.category.source"
         id="com.marchesini.mas.rcp.ui.edit.text.mps.format"
         name="Format MPS">
   </command>
    <command
         categoryId="com.marchesini.mas.rcp.ui.category.source"
         id="com.marchesini.mas.rcp.ui.edit.text.mps.indent"
         name="Indent MPS">
   </command>
    <command
         categoryId="com.marchesini.mas.rcp.ui.category.source"
         id="com.marchesini.mas.rcp.ui.edit.text.mps.sort_op_def"
         name="Sort Op Definition MPS">
   </command>
</extension>

这是我编辑的createAction()方法:

    @Override
    protected void createActions() {
        super.createActions();
        
        final ResourceBundle bundle = EditorPlugin.getResourceBundle();
        
        IAction action = new TextOperationAction(bundle, "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
        action.setActionDefinitionId(IMPSTextEditorActionDefinitionIds.FORMAT);
        setAction("Format", action); //$NON-NLS-1$
        markAsStateDependentAction("Format", true); //$NON-NLS-1$
        
        action = new MPSIndentAction(bundle, "Indent.", this, false); //$NON-NLS-1$
        action.setActionDefinitionId(IMPSTextEditorActionDefinitionIds.INDENT);
        setAction("Indent", action); //$NON-NLS-1$
        markAsStateDependentAction("Indent", true); //$NON-NLS-1$
        markAsSelectionDependentAction("Indent", true); //$NON-NLS-1$
        
        action = new MPSSortOpDefinitionAction(bundle, "SortOpDef.", this); //$NON-NLS-1$
        action.setActionDefinitionId(IMPSTextEditorActionDefinitionIds.SORT_OP_DEF);
        setAction("SortOpDef", action); //$NON-NLS-1$
        markAsStateDependentAction("SortOpDef", true); //$NON-NLS-1$
        
        m_generateActionGroup = new GenerateActionGroup(this, ITextEditorActionConstants.GROUP_EDIT);
    }

我应该类似于 CDT 中的 Java 编辑器和 C++ 编辑器。

我还在编辑器上下文菜单中注册了一个 ActionGroup:动作被添加到动作组中,它们在那里工作。

我还没有定义源菜单..我应该定义它还是在其他地方定义它?

谢谢

标签: eclipseeclipse-plugineclipse-rcp

解决方案


让您的编辑器拥有自己的源菜单的最简单方法可能是覆盖contributeToMenu编辑器操作栏贡献者中的方法:

@Override
public void contributeToMenu(final IMenuManager manager)
{
  final IMenuManager menu = new MenuManager("Menu Title");
   
  manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu);

  // Append your actions here  
  menu.add(sampleAction);
}

推荐阅读