I am trying to override the Eclipse File > Save menu action to add some functionality.
I have tried the following things
a) Create a new action and add it to the global action handler
actionBars.setGlobalActionHandler(ActionFactory.SAVE.getId(), mySaveAction);
actionRegistry.registerAction(action);
b) Create a new handler and override the save command
<extension point="org.eclipse.ui.handlers">
<handler commandId="org.eclipse.ui.file.save"
class="com.diagrams.ui.SaveFileHandler">
<enabledWhen>
<with variable="activePartId">
<equals
value="com.diagrams.editors.MultiPageEditor" />
</with>
</enabledWhen>
<activeWhen>
<with variable="activePartId">
<equals
value="com.diagrams.editors.MultiPageEditor" />
</with>
</activeWhen>
</handler>
</extension>
With both these approaches I have been able to override the Keyboard Ctrl+S functionality but the "File > Save" menu seem to work differently.
Would really appreciate any help, Thanks
In an RCP application, you can contribute the Save action in your ActionBarAdvisor. This also registers the action so it is available from the save command.
But as a plugin in the Eclipse IDE, the IDE provides the ActionBarAdvisor and hooks up the Save action in the File menu. Because that's not technically a command (Actions are a step above an SWT.Selection listener) that's why you can't override the File>Save action.
However, each part provides its own save implementation, so you can do whatever you want in your MultiPageEditor.
The other option is to use org.eclipse.ui.commands.ICommandService.addExecutionListener(IExecutionListener) and add an IExecutionListener (or IEL2). That can listen for the save command, the ID is declared in org.eclipse.ui.IWorkbenchCommandConstants.
It might have something to do with the activePartId being different when the main menu is selected versus a keystroke or using the right-click menu. Have you looked at other extension points?
Related
I'm creating Eclipse plugin for my DSL, using Xtext and need to add context menu item for my editor (what I did already), but need to find out whether the code in editor is valid, if yes -> this menu item should be enabled, otherwise disabled. Is it somethow possible to check whether the whole code is valid and update the state of this context menu item accordingly ?
I added this context menu item by adding this fragment to the plugin.xml file in .ui project in Xtext.
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:#TextEditorContext?after=additions">
<command commandId="org.first.langu.ui.handler.InterpreterCommand" style="push">
<enabledWhen checkEnabled="true">
<reference definitionId="org.first.langu.Program.XtextEditor.opened"/>
</enabledWhen>
</command>
</menuContribution>
</extension>
<extension point="org.eclipse.ui.commands">
<command id="org.first.langu.ui.handler.InterpreterCommand" name="Interpret Code"/>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler
class="org.first.langu.ui.ProgramExecutableExtensionFactory:org.first.langu.ui.handler.InterpretCodeHandler"
commandId="org.first.langu.ui.handler.InterpreterCommand"/>
</extension>
Basically I need to check whether code in editor is valid, instead of if editor is just opened.
you can use the editors annotation model to find out if there are any errors.
see org.eclipse.xtext.ui.editor.XtextEditorErrorTickUpdater.getSeverity(XtextEditor)
for an example on how to retrieve and filter the annotations. that class is used to display the red x in the editor icon if there are errors
i am building my custom Editor. But when i right click on my desired file and try to open it with custom editor via "Open With" option, my command handler does not work. Do i have to use locationURI under menuContribution tag with commandId in plugin.xml for this? If it is then how? Please have a look at my current plugin.xml for better understanding.
Plugin.xml
<extension point="org.eclipse.ui.editors">
<editor
class="launcher.ChartEditor"
default="false"
id="launcher.ChartEditor"
name="ChartEditor">
</editor>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
id="launcher.openChartEditor"
name="OpenChartEditor">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="launcher.ChartEditorHandler"
commandId="launcher.openChartEditor">
</handler>
</extension>
The 'Open With...' menu just opens the editor directly. It does not use any command or handler. There is no other way to contribute to the 'Open With' menu. The EditorSelectionDialog is the same.
To use your command and handler you do need to using a menuContribution (or a tool bar contribution) but this will have to somewhere other than Open With.
The standard 'Open With' action opens the selected editor passing it the IEditorInput for the current selection. This will usually be an IFileEditorInput from which you can get the file.
I'm creating an eclipse 4 plugin project. I have a tree viewer that shows model elements.
Problem:
I need to show a popup menu based on the selection that I'm doing on the tree viewer.
I tired to use core expression as explained in the link:
http://www.vogella.com/tutorials/EclipseRCP/article.html#menuadvanced_popup
But whenever I attach the core expression with my popup menu it disappears.
The popup menu appears for all the elements if i don't attach the core expression.
Is there something else that i'm supposed to do to get things right ?
or Should i use a different approach ?
Please find the below snap of my Application.e4xmi file
My plugin.xml file definition for the core expression
<definition
id="xxx.abc.project.addchilddefinition">
<with variable="org.eclipse.ui.selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="xxx.abc.project.model.ObjectName">
</instanceof>
</iterate>
</with>
</definition>
I've registered my popup menu using the below code snippet:
menuService.registerContextMenu(treeviewer.getControl(),
"xxx.abc.project.popupmenu.addchild");
menuService is the EMenuService object and "xxx.abc.project.popupmenu.addchild" is my popupmenu id
You need to use a with element to specify that the selection should be used:
<definition
id="xxx.abc.project.addchilddefinition">
<with
variable="org.eclipse.ui.selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="xxx.abc.project.model.ObjectName">
</instanceof>
</iterate>
</with>
You must also call the ESelectionService setSelection(Object) method when the tree selection changes.
The selection variable org.eclipse.ui.selection is defined in IServiceConstants.ACTIVE_SELECTION
In eclipse there are certain toolbars which becomes visible when I open java editor. It goes invisible when I close the editor (only one editor was open.)
Here opening the java editor is not changing the perspective.
How to achieve this functionality in eclipse rcp application?
Pre-3.3 this can be accomplished through a org.eclipse.ui.IEditorActionBarContributor, defined in your editor extension with the contributorClass attribute.
Since 3.3 a core expression definition can be used in a visiblewhen expression.
For example, a re-usable core expression for an editor can be defined as follows
<extension point = "org.eclipse.core.expressions.definitions">
<definition id="org.eclipse.ui.examples.contributions.activeEditor">
<with variable="activeEditorId">
<equals value="org.eclipse.ui.examples.contributions.editor"/>
</with>
</definition>
</extension>
Then the following expression can be used to control whether a menu or toolbar is visible
<visibleWhen>
<reference definitionId=""org.eclipse.ui.examples.contributions.activeEditor"/>
</visibleWhen>
You need to look at Activities and Contexts to hide/unhide contributions
http://www.vogella.com/blog/2009/07/13/eclipse-activities/
I wish to add a popup menu to the Eclipse Problem View. I can add the menu but I want it to only appear under certain condition. Like for example when a certain type of problem is selected in the problems view.
Assuming you are using the "org.eclipse.ui.menus" extension point and the command framework, you can set the visibleWhen part of the menu definition to something like;
<visibleWhen>
<with variable="selection">
<iterate>
<and>
<instanceof value="com.example.MyClass">
</instanceof>
</and>
</iterate>
</with>
</visibleWhen>
This should only make the option visible when the selected items are of the correct instance.
It is also possible to set enabled and active states on command handlers in a similar way.