I have a popup-menu contribution for .txt file in a project:
<menuContribution
allPopups="false"
locationURI="popup:com.XXXX.ui.view.navigator?endof=group1">
<command
commandId="com.XXXX.ui.commandid2"
style="push">
<visibleWhen
checkEnabled="false">
<iterate
ifEmpty="false"
operator="or">
<or>
<adapt
type="org.eclipse.core.resources.IFile">
<test
property="org.eclipse.core.resources.extension"
value="txt">
</test>
</adapt>
<instanceof
value="org.eclipse.core.resources.IFolder">
</instanceof>
</or>
</iterate>
</visibleWhen>
</command>
As you can see, currently I have enable for folder also. But wanted to make invisible for a blank folder and empty project. No clue how to apply property tester for an empty folder or Project. Any pointer would be much helpful.
There is no standard property tester available which tests if the folder or project is empty.
You could write your own property tester for this using the org.eclipse.core.expressions.propertyTesters extension point.
Related
I am trying to follow Lars Vogel's tutorial on natures and am stuck at the point where the visibleWhen for a menu is defined. When I add the following to my command definition in my menuContribution:
<visibleWhen
checkEnabled="false">
<adapt
type="org.eclipse.core.resources.IProject">
</adapt>
</visibleWhen>
my menu item never appears when right-clicking a project in Project Explorer.
I double-checked that I have all necessary things like org.eclipse.core.runtime, org.eclipse.core.resources and org.eclipse.ui as dependencies.
What am I missing?
You need to use <iterate> since what you are testing is a selection which may have multiple items:
For example this is one of the PDE API analysis tool command definitions:
<command
commandId="org.eclipse.pde.api.tools.ui.convert.javadocs"
style="push">
<visibleWhen
checkEnabled="false">
<iterate>
<adapt
type="org.eclipse.core.resources.IProject">
<test
property="org.eclipse.core.resources.projectNature"
value="org.eclipse.pde.api.tools.apiAnalysisNature">
</test>
</adapt>
</iterate>
</visibleWhen>
</command>
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;
}
I want to contribute a context menu only when the following 2 conditions are met:
1.- If selected file has .txt extension
2.- If parent project of the selected file has an specific facet
I can do this by separate using the following conditions using the org.eclipse.ui.menus extension point, i.e: for the project facet:
<with variable="activeMenuSelection">
<iterate operator="and" ifEmpty="false">
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.wst.common.project.facet.core.projectFacet" value="jst.code.quality" forcePluginActivation="true" />
</adapt>
</iterate>
</with>
And for the file extension:
<with variable="activeMenuSelection">
<iterate operator="and" ifEmpty="false">
<adapt type="org.eclipse.core.resources.IFile">
<test property="org.eclipse.core.resources.extension" value="txt" forcePluginActivation="true" />
</adapt>
</iterate>
</with>
But I have problems to combine this two conditions since the activeMenuSelection variable will only contain the file and I can't use it to test the project facet, is there a variable I can use to access the active project in the same condition?
The org.eclipse.wst.common.project.facet.core.projectFacet test will work on any IResource so it will work on an IFile as well as an IProject. So you can combine the tests.
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.
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>