Eclipse RCP: Disable Menu Entry for Command via Plugin XML - eclipse

I know I can disable menu entries for commands in the plugin XML like that:
<visibleWhen checkEnabled="false">
<with variable="activeWorkbenchWindow.activePerspective">
<equals value="myperspective"/>
</with>
</visibleWhen>
My question is: is there a way to just disable a menu entry, instead of hiding it?

That is controlled by the handler for the command. The handler can define when it is active with <activeWhen> element and when it is enabled with <enabledWhen>
<extension
point="org.eclipse.ui.handlers">
<handler
class="..."
commandId="...">
<activeWhen>
....
</activeWhen>
<enabledWhen>
....
</enabledWhen>
</handler>
The menu item will be disabled if there is no active handler or the active handler is not enabled.

Related

How to check whether editor is active while popup in eclipse?

I am working on a project in which I need to show a popup menu for files having particular extensions only and from popup of editor window and project explorer.
Following is the plugin.xml file that I have made:
<menuContribution
locationURI="popup:org.eclipse.ui.popup.any">
<command
commandId="abc.contextMenu"
label="Open"
mnemonic="P"
style="push">
<visibleWhen
checkEnabled="false">
<or>
<with
variable="selection">
<iterate
ifEmpty="false">
<reference
definitionId="com.varun.ease.ui.sign.definition.testType">
</reference>
</iterate>
</with>
<with
variable="activeEditorInput">
<and>
<test
property="org.eclipse.ui.IWorkbenchPart"
value="true">
</test>
<reference
definitionId="com.varun.ease.ui.sign.definition.testType">
</reference>
</and>
</with>
</or>
</visibleWhen>
</command>
</menuContribution>
<extension
point="org.eclipse.core.expressions.definitions">
<definition
id="com.varun.ease.ui.sign.definition.testType">
<adapt
type="org.eclipse.core.resources.IFile">
<or>
<test
property="org.eclipse.core.resources.name"
value="*.py">
</test>
<test
property="org.eclipse.core.resources.name"
value="*.js">
</test>
</or>
</adapt>
</definition>
I want to show command either only when editor is active i.e. it is having focus in window or during selection of project from project explorer.
What should I do to check whether editor is active part i.e. whether it is having focus in window?
Currently I am using <test/> with org.eclipse.ui.IWorkbenchPart property but command is not at all visible in popup of editor window.
Running without that property, command is even shown when appropriate file is open in editor though not active and popup is opened from project explorer not from appropriate file.
Thanks for the help
EDIT
Updated code after successfully running command for editor as directed by #greg-449. But now, command is not visible when trying to restrict to particular file types.
<visibleWhen
checkEnabled="false">
<or>
<with
variable="selection">
<iterate
ifEmpty="false">
<reference
definitionId="com.varun.ease.ui.sign.definition.testType">
</reference>
</iterate>
</with>
<with
variable="activePart">
<and>
<reference
definitionId="com.varun.ease.ui.sign.definition.testType">
</reference>
<instanceof
value="org.eclipse.ui.IEditorPart">
</instanceof>
</and>
</with>
</or>
</visibleWhen>
Use activePart and test for an instance of the editor class.
So:
<with
variable="activePart">
<instanceof
value="org.eclipse.ui.IEditorPart">
</instanceof>
</with>
Tests for any editor.
You can also use activePartId to test against an editor id.
If you want to test the editor input as well you will need to use an <and>:
<and>
<with
variable="activePart">
<instanceof
value="org.eclipse.ui.IEditorPart">
</instanceof>
</with>
<with
variable="activeEditorInput">
<reference
definitionId="com.varun.ease.ui.sign.definition.testType">
</reference>
</with>
</and>
Check whether editor is active or not using rcp eclipse
public class Stud_editor_open extends AbstractHandler{
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
IEditorReference[] editors = page.getEditorReferences();
// How many editor open..
// System.out.println("Length : "+editors.length);
if(editors.length==0){
System.out.println("Editor is not an active");
}else{
System.out.println("Editor is an active");
System.out.println("Editor Name is :: "+page.getActiveEditor().getTitle());
}
}
List out open editor
Multiple Editor open at time not dublicate or reopen new editor
for (int i=0; i<editors.length; i++) {
//page.activate(editors[i].getEditor(true));
System.out.println("Editor Name :"+editors[i].getTitle().toString());
return null;
}

Eclipse plugin: Disable context menu contributon if no xml is selected

We're developing an eclipse-plugin and have some contributions for the context-menu in the package-explorer. Using the old org.eclipse.ui.popupMenus, we could specify a name-filter in the contribution, so the menu-entry is only shown if the selected file has a specific name:
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="false"
id="..."
nameFilter="file.xml"
objectClass="org.eclipse.core.resources.IFile">
...
</objectContribution>
</extension>
Now, we want to use the new way to create that context-menu and can also disable the entry, if it's no file:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.jdt.ui.PackageExplorer">
<command
commandId="..."
label="..."
style="push">
</command>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
id="..."
name="...">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="..."
commandId="...">
<activeWhen>
<with
variable="selection">
<iterate
ifEmpty="false"
operator="and">
<adapt
type="org.eclipse.core.resources.IFile">
</adapt>
</iterate>
</with>
</activeWhen>
</handler>
</extension>
(Also compare: Enable/disable menu item in Eclipse plugin)
However, we not only want to disable the entry, when there's no file selected, but also, when the file isn't an xml-file. We tried to hack something in the <activeWhen>-tag, but we couldn't find a solution to let it watch the filename, too.
Is there a possibility to do this? And if: How?
There is an Eclipse wiki page about Menu Contributions/IFile objectContribution which might help. I haven't tested it myself, but it seems the nameFilter attribute of the action-based extension can be replaced with a call to a predefined property tester with id org.eclipse.core.resources.name when using the command framework.

Trouble deciding how to add popup menu to eclipse plugin using either action ObjectContributions or command handlers

The issue is this: Most things I read on the web says that we should avoid actions when creating popup menus because the command framework allows for more decoupling.
Example: http://wiki.eclipse.org/FAQ_What_is_the_difference_between_a_command_and_an_action%3F
Fair enough.
But I am having a heck of a time getting the command framework to add my menu when I right click on a .java file inside the editor, and only when I click inside the editor. I can get the menu to show (using 'with' and the activeEditor variable), but it also shows when I right click on the java file inside the package explorer, which I don't want. I suspect it's because the file is already open inside the editor.
I also have a menu that gets added when I right click on the java file inside the package explorer, using IComplilationUnit. That works fine.
So my problem is solved using a popup menu action for when I click inside the file. I also get access to to all the ISelection stuff there. But the coupling is to high and I lose the flexability of using handlers.
I'm looking for either:
Tell me I'm doing it the only way possible; or
Tell me how to have my popup appear only when I right click on the java file editor.
Regards
In the end it was really straight forward. The example below uses the command framework. It doesn't have the handlers, so just click the class hyperlink whenever you need a class generated.
Create a new eclipse plugin project called com.test.plugin.project
In the dependency tab of the plugin.xml file add the following dependencies
org.eclipse.jdt
org.eclipse.jdt.core
org.eclipse.jdt.ui
org.eclipse.jface.text
Put this in the plugin.xml tab section:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="true"
locationURI="popup:org.eclipse.ui.popup.any">
<menu
label="Test Project Sub Menu">
<command
commandId="com.test.plugin.project.command.packageexplorer"
id="packageexplorerId"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="org.eclipse.jdt.internal.core.CompilationUnit">
</instanceof>
</iterate>
</visibleWhen>
</command>
<command
commandId="com.test.plugin.project.command.classfile"
id="classfileId"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<and>
<with
variable="selection">
<instanceof
value="org.eclipse.jface.text.TextSelection">
</instanceof>
</with>
<with
variable="activeEditorId">
<equals
value="org.eclipse.jdt.ui.CompilationUnitEditor">
</equals>
</with>
</and>
</iterate>
</visibleWhen>
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
id="com.test.plugin.project.command.packageexplorer"
name="Only Show In Package Explorer">
</command>
<command
id="com.test.plugin.project.command.classfile"
name="Only Show In Class File">
</command>
</extension>
</plugin>
What this does
It creates one popup menu in a sub menu when you right click on a java file inside the package explorer (and only when you click on a java file).
It create a different popup menu in a sub menu when you right click on a java file.

visibleWhen menuContribution showing up wrongly

I have created an eclipse menucontribution in my plugin which should show up ONLY when rightclicking a project that has a specific nature:
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<menu
icon="ico/full/obj16/icon-logo-composer.png"
id="com.dubture.composer.lib.ui.menu"
label="Composer">
<visibleWhen
checkEnabled="false">
<iterate>
<adapt
type="org.eclipse.core.resources.IProject">
<and>
<test
property="org.eclipse.core.resources.projectNature"
value="com.dubture.composer.core.composerNature">
</test>
</and>
</adapt>
</iterate>
</visibleWhen>
</menu>
</menuContribution>
The visibleWhen condition seems to work only when right-clicking a project. The menu is only shown when the project has the specified nature.
However, when i right-click somewhere else (for example in some empty area in the project explorer or inside the problems view), the menu entry will show up despite the adapt condition.
Anyone knows how to restrict it to right clicking on projects only?
the test property is used by PropertyTester
if you add your menucontribution to popup:org.eclipse.ui.popup.any?after=additions, I believe it could show everywhere, you should restrict to popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu unless required otherwise
this snippet uses commands but I think you can try to mix with your requirements to see if it works
<menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu">
<command
commandId="COMMANDID"
icon="icons/icon.png"
label="LABEL"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="and">
<instanceof
value="org.eclipse.core.resources.IProject">
</instanceof>
</iterate>
</visibleWhen>
</command>
</menuContribution>

How do you create an retargetable action for a specific object type in Eclipse using the commands framework?

The Eclipse command framework allows you to create a generic command and handler. However, when you create an extension point that targets 'copy' the runtime complains:
<extension point="org.eclipse.ui.handlers">
<handler class="example.Handler" commandId="org.eclipse.ui.edit.copy"/>
</extension>
!MESSAGE Conflicting handlers for org.eclipse.ui.edit.copy: {org.eclipse.ui.internal.handlers.WidgetMethodHandler:copy} vs {example.Handler}
If you add an activeWhen clause, so that the special handler is only invoked when an object of a particular type is selected in a viewer, then you get an exception when you try and copy:
<extension point="org.eclipse.ui.handlers">
<handler class="example.Handler" commandId="org.eclipse.ui.edit.copy">
<activeWhen>
<with
variable="selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="sample.Class">
</instanceof>
</iterate>
</with>
</activeWhen>
</handler>
</extension>
Caused by: org.eclipse.core.commands.NotHandledException: There is no handler to execute for command org.eclipse.ui.edit.copy
What's the right way of using the commands framework to provide a specific copy operation for objects of a particular type?
Your activeWhen clause looks acceptable. What will matter is what selection you are going for. ex:
<handler class="z.ex.cmd.handlers.LongCopyHandler"
commandId="org.eclipse.ui.edit.copy">
<activeWhen>
<with variable="selection">
<iterate ifEmpty="false"
operator="and">
<adapt type="org.eclipse.core.resources.IMarker"/>
</iterate>
</with>
</activeWhen>
</handler>
I originally tried instanceof in my activeWhen expression to get access to the IMarkers in the markers view, but that didn't work (the objects don't implement IMarker). I had to adapt them to get my handler to work correctly. This would apply to .java files in the Package Explorer as well, they adapt to an IResource but they're actually IJavaElements or something.