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
CodePudding user response:
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);
}
CodePudding user response:
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!