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.
Related
I'm writing a plugin. To start it, the menu entry is displayed when doing a rightclick on a project in SVN-Plugin-View. By using "org.tigris.subversion.subclipse.ui.repository.RepositoriesView" as locationURI inside the plugin.xml my menu entry is visible, as I wanted.
But now I want to make it visible under special conditions. I don't find out, what property I can use to test the name of the Project.
I already did something similar inside Eclipse with
<command...
<visible when>
<with variable="activeMenuSelection">
<iterate ifEmpty="false">
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.name" value="Test_*"/>
</adapt>
</iterate>
</with>
</visibleWhen>
</command>
Now I use
but I don't find out what to use as test property
Like in my working (without SVN) example I tried
<test property="org.tigris.subversion.subclipse.core.resources.name" value="Test_*/>"
But it did not work. The menu entry was not visible for SVN-Projects with names starting with "Test_"
I have written a custom launcher in Eclipse which I can access via the "Run As" and "Debug As" on class level. I have a similar configuration as described in How do I use "org.eclipse.debug.ui.launchShortcuts"?. Unfortunately I can't see the submenu in the package explorer at the method level (for example submenu to launch the customer launcher in order to execute a single JUnit test). How can I do this?
JUnit uses a contextualLaunch based on the selected element being a org.eclipse.jdt.core.IJavaElement rather than just a resource. You will need to do the same.
This is what JUnit uses:
<contextualLaunch>
<enablement>
<with variable="selection">
<count value="1"/>
<iterate>
<adapt type="org.eclipse.jdt.core.IJavaElement">
<and>
<test property="org.eclipse.jdt.core.isInJavaProject"/>
<or>
<test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="junit.framework.Test"/>
<test property="org.eclipse.jdt.core.hasTypeOnClasspath" value="org.junit.platform.commons.annotation.Testable"/>
</or>
<test property="org.eclipse.jdt.junit.canLaunchAsJUnit" forcePluginActivation="true"/>
</and>
</adapt>
</iterate>
</with>
</enablement>
</contextualLaunch>
Some of the <test elements are specific to JUnit so you will need to use something different.
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 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.
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>