0

I'm writing an Eclipse plugin to show the contents of an own file type in my own custom editor.

The editor should enable the source menu and make it visibile (like the java editor for example), but I can't figure out how to enable that source menu.

With this How to extend the source menu in Eclipse? (or: What is its locationURI?) I can add actions to the source menu, that now is visible (always..), but the actions are disabled...

This is how I defined the actions in 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>

These are the commands:

<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>

And this is my editor's createAction() method:

    @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);
    }

I should be similar to the Java editor and the C++ editor in CDT.

I also have an ActionGroup registered at the editor context menu: the actions are added to the action group and they work in there.

I haven't defined the source menu.. should I define it or is it defined somewhere else?

Thankyou

Gionata
  • 133
  • 11
  • Show us how you have defined the menu and your actions – greg-449 Nov 16 '21 at 10:06
  • I have actions definitions to the question – Gionata Nov 16 '21 at 11:24
  • You have defined those with `retarget="true"`, what have you done to handle the retargetable action? – greg-449 Nov 16 '21 at 12:04
  • It was a miskate.. I done need a retargetable acion.. I will remove it from the question. – Gionata Nov 16 '21 at 13:07
  • You are specifying a `definitionId` - have you set up the org.eclipse.ui.commands for those ids? Do the commands have handlers? Are the handlers active? – greg-449 Nov 16 '21 at 13:11
  • I have added command and code in the question. The actions work for my editor, they are available in the context menu. The problem is that the source menu (in the menu bar) should be shown with those actions inside, as for the Java/C++ editor. So I am missing something. – Gionata Nov 16 '21 at 13:21
  • Looking at other editors, they just define a completely separate Source menu of their own. – greg-449 Nov 16 '21 at 14:21
  • OK but where in the code? Can you show me? I can't seem to find the point of the definiton. The problem is just to show the source menu when the editor is opened (with the actions enabled..) – Gionata Nov 16 '21 at 15:00

2 Answers2

1

Probably the simplest way for your editor to have its own source menu is to just override the contributeToMenu method in your editor action bar contributor:

@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);
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
0

OK, I have ended up with this solution: the Actions need to be retargettable (I have edited the question... again..), and the action bar contributor is:

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);
    }
}

It's working good! I can see the source menu when the editor opens, and the actions are present in the menu. Thank you @greg-449!

Gionata
  • 133
  • 11