【问题标题】:Source menu for custom editor in EclipseEclipse 中自定义编辑器的源代码菜单
【发布时间】:2021-12-27 09:23:31
【问题描述】:

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

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

有了这个 How to extend the source menu in Eclipse? (or: What is its 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:动作被添加到动作组并在那里工作。

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

谢谢

【问题讨论】:

  • 向我们展示您是如何定义菜单和您的操作的
  • 我有问题的动作定义
  • 你用retarget="true"定义了那些,你做了什么来处理retargetable action?
  • 这是一个错误..我确实需要一个可重新定位的行动..我会从问题中删除它。
  • 您正在指定 definitionId - 您是否为这些 id 设置了 org.eclipse.ui.commands?命令有处理程序吗?处理程序是否处于活动状态?

标签: eclipse eclipse-plugin eclipse-rcp


【解决方案1】:

让您的编辑器拥有自己的源代码菜单的最简单方法可能是在您的编辑器操作栏贡献者中覆盖 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);
}

【讨论】:

    【解决方案2】:

    好的,我最终得到了这个解决方案:动作需要重新定位(我已经编辑了问题......再次......),动作栏贡献者是:

    public class MPSEditorActionContributor extends TextEditorActionContributor {
    
        private ITextEditor m_textEditor;
    
        /**
         * The format action.
         */
        private RetargetTextEditorAction m_format;
    
        /**
         * The indent action.
         */
        private RetargetTextEditorAction m_indent;
    
        /**
         * The sort operand definition action.
         */
        private RetargetTextEditorAction m_sortOpDef;
    
        public MPSEditorActionContributor() {
            final ResourceBundle bundle = EditorPlugin.getResourceBundle();
    
            m_format = new RetargetTextEditorAction(bundle, "Format."); //$NON-NLS-1$
            m_format.setActionDefinitionId(IMPSTextEditorActionDefinitionIds.FORMAT);
    
            m_indent = new RetargetTextEditorAction(bundle, "Indent."); //$NON-NLS-1$
            m_indent.setActionDefinitionId(IMPSTextEditorActionDefinitionIds.INDENT);
    
            m_sortOpDef = new RetargetTextEditorAction(bundle, "SortOpDef."); //$NON-NLS-1$
            m_sortOpDef.setActionDefinitionId(IMPSTextEditorActionDefinitionIds.SORT_OP_DEF);
        }
    
        @Override
        public void setActiveEditor(IEditorPart part) {
            super.setActiveEditor(part);
    
            m_textEditor = null;
            if (part instanceof ITextEditor) {
                m_textEditor = (ITextEditor) part;
    
                IAction editorFormatAction = getAction(m_textEditor, "Format"); //$NON-NLS-1$
                IAction editorIndentAction = getAction(m_textEditor, "Indent"); //$NON-NLS-1$
                IAction editorSortOpDefAction = getAction(m_textEditor, "SortOpDef"); //$NON-NLS-1$
    
                m_format.setAction(editorFormatAction);
                m_indent.setAction(editorIndentAction);
                m_sortOpDef.setAction(editorSortOpDefAction);
    
                // Source menu.
                IActionBars bars = getActionBars();
                bars.setGlobalActionHandler(IMPSTextEditorActionDefinitionIds.FORMAT, editorFormatAction);
                bars.setGlobalActionHandler(IMPSTextEditorActionDefinitionIds.INDENT, editorIndentAction);
                bars.setGlobalActionHandler(IMPSTextEditorActionDefinitionIds.SORT_OP_DEF, editorSortOpDefAction);
            }
        }
    
        @Override
        public void contributeToMenu(IMenuManager manager) {
            super.contributeToMenu(manager);
    
            final IMenuManager menu = new MenuManager("Source"); //$NON-NLS-1$ TODO
            manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, menu);
    
            menu.add(m_format);
            menu.add(m_indent);
            menu.add(m_sortOpDef);
        }
    }
    

    效果很好!当编辑器打开时,我可以看到源菜单,并且菜单中存在操作。 谢谢@greg-449!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2010-12-19
      • 1970-01-01
      • 2015-02-21
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多