Visibility in popup menus - plugins

I have created an eclipse plugin project. I want this plugin to be available as a popup. Therefore I have created an extension point with "org.eclipse.ui.popupMenus" (I know it is deprecated now, ours is an old project.)
I want this popup option to appear only at the file level with certain extension (say xml). Currently, it is appearing anywhere on right click.
I have looked around the internet and got to know that I can add a "visibility" tag that can set rules where this popup should be visible. However, I do not know the syntax for that.
Can someone please help me out? How to set the visibility of the popup menu so that it is visible only when I right click ON the filename with extension xml?
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension point="org.eclipse.ui.popupMenus">
<objectContribution
adaptable="true"
objectClass="org.eclipse.core.resources.IFile"
nameFilter="*.*"
id="org.eclipse.lyo.tools.codegenerator.ui.popupMenus.contribution.IFile">
<menu id="org.eclipse.acceleo.module.menu" label="Acceleo Model Code Generator" path="additionsAcceleo">
<groupMarker name="acceleo"/>
</menu>
<action
class="org.eclipse.lyo.tools.codegenerator.ui.popupMenus.AcceleoGenerateCodegeneratorAction"
enablesFor="+"
id="org.eclipse.lyo.tools.codegenerator.ui.popupMenus.AcceleoGenerateCodegeneratorAction"
icon="icons/default.gif"
label="Generate Java Code from Model"
menubarPath="org.eclipse.acceleo.module.menu/acceleo"/>
<visibility>
//what should come here?
</visibility>
</objectContribution>
</extension>
</plugin>
(http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fextension-points%2Forg_eclipse_ui_popupMenus.html)
Regards,
Yash

visibility can only be a child of objectContribution not action.
In any case you can use the namefilter attribute to restrict the file name matching. You would only use visiblity to do more complex checks.
For example this is one of the JDT items:
<objectContribution
adaptable="true"
objectClass="org.eclipse.core.resources.IFile"
nameFilter="*.xml"
id="org.eclipse.jdt.internal.ui.javadocexport.JavadocWizard">
<visibility>
<objectState name="contentTypeId" value="org.eclipse.ant.core.antBuildFile"/>
</visibility>
In this
adaptable="true"
objectClass="org.eclipse.core.resources.IFile"
restricts the actions to a workspace file
nameFilter="*.xml"
restricts the actions to files ending in .xml
<visibility>
<objectState name="contentTypeId" value="org.eclipse.ant.core.antBuildFile"/>
</visibility>
further restricts the actions to files with a 'content type' of 'Ant build file'
To match multiple name patterns remove the nameFilter and use a visibility like:
<visibility>
<or>
<objectState name="name" value="*.xml"/>
<objectState name="name" value="*.java"/>
</or>
</visibility>

Related

Contributing a custom delete handler to the project explorer context menu

In an Eclipse plugin I have a custom org.eclipse.ui.navigator.navigatorContent extension. I am trying to provide a custom Delete handler. Previously I was using the org.eclipse.ui.popupMenus extension point and with a objectContribution/action, but that does not support key bindings due to Eclipse bug #329979: [Key Bindings] Support keybinding of objectContributions.
I've tried a couple of approaches:
Defining a handler for the delete command
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="org.eclipse.ui.edit.delete"
class="org.apache.sling.ide.eclipse.ui.actions.JcrNodeDeleteHandler">
<activeWhen>
<adapt type="org.apache.sling.ide.eclipse.ui.nav.model.JcrNode"/>
</activeWhen>
</handler>
</extension>
Registering a custom delete action in my actionProvider
I've registered an actionProvider for my custom navigatorContent.
<actionProvider
class="org.apache.sling.ide.eclipse.ui.nav.PackageExplorerOpenActionProvider"
id="org.apache.sling.ide.eclipse.ui.nav.actions.OpenActions"
overrides="org.eclipse.jdt.ui.navigator.actions.OpenActions">
<enablement>
<instanceof value="org.apache.sling.ide.eclipse.ui.nav.model.JcrNode"/>
</enablement>
</actionProvider>
Then in that ActionProvider I've registered the action
#Override
public void fillActionBars(IActionBars actionBars) {
actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId() ,deleteAction);
}
None of them produced the desired effect, so now I'm stuck.
How can I provide a custom implementation of the delete command for my custom navigator that also reacts to keybindings?
Update
I've been able to register the delete command using the following:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<command
commandId="org.eclipse.ui.edit.delete"
icon="icons/ovr16/delete_obj.gif"
mnemonic="D"
label="Delete">
<visibleWhen
checkEnabled="false">
<iterate ifEmpty="false">
<adapt
type="org.apache.sling.ide.eclipse.ui.nav.model.JcrNode">
</adapt>
</iterate>
</visibleWhen>
</command>
</menuContribution>
</extension>
It's important to note that the visibleWhen condition must match the one from the handler declaration.
However, it's not located where I would expect the 'delete' action to be, but in the 'general' area with the Run As contributions, etc. This is probably due to the menuLocation value of popup:org.eclipse.ui.popup.any?after=additions, but I'm not sure what the correct value would be.
The right approach to register the command programatically is to override the fillContextMenu method:
#Override
public void fillContextMenu(IMenuManager menu) {
super.fillContextMenu(menu);
if ( deleteAction != null ) {
menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, renameAction);
}
}
To register the command declaratively, in addition to a handler for the delete command a menuContribution/command must be registered as well.
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.ui.popup.any?after=group.edit">
<command
commandId="org.eclipse.ui.edit.delete"
icon="icons/ovr16/delete_obj.gif"
mnemonic="D"
label="Delete">
<visibleWhen
checkEnabled="false">
<iterate ifEmpty="false">
<adapt
type="org.apache.sling.ide.eclipse.ui.nav.model.JcrNode">
</adapt>
</iterate>
</visibleWhen>
</command>
</menuContribution>
</extension>
Some points to note:
The locationURI specifies ?after=group.edit. That part is important as the group.edit is the id of the menu which typically holds the delete/copy/paste actions
The visibleWhen must match exactly what is declared in the command. I mistakenly used only the adapt tag, but it must be wraped inside an iterate tag. I seem to recall that this is due to the IStructuredSelection being validated against the criteria
Thanks to RĂ¼diger Herrmann for guiding me towards the right answer. Also see How to add items in popup menu?, which has valuable information.

Create conditions in Eclipse plugin.xml

Is there a way to create conditions (similar to if/else blocks) in the Eclipse plugin.xml's files.
I'm creating a Eclipse plugins in which I need to set a different value depending on the project facet, I'm getting the project facet using:
<with variable="projectFacet">
<test property="org.eclipse.wst.common.project.facet.core.projectFacet" value="projectFacet" forcePluginActivation="true"/>
</with>
And then I have this code:
<item
label="test"
visible="false"
id="test.test1"
category="category1"
description="test description">
</item>
At this momment I'm hardcoding "category1", what I need is to select the correct category depending on the variable I got before, for example:
facet1 should be on the category1
facet2 should be on the category2
Someone knows how to do that? Thanks.

Enable and disable org.eclipse.ui.actionSets

I have a menu item defined this way:
<extension point="org.eclipse.ui.actionSets">
<actionSet
description="My Sample"
id="MySample.actionSet"
label="My Sample"
visible="true">
<menu
id="mysample.actionset.menu1"
label="My Sample">
<groupMarker
name="mysample.groupCreatesimilar">
</groupMarker>
</menu>
<action
class="org.mysample.actions.create.MyCreateCodeAction"
definitionId="MySample.myCreateCode.command"
id="MySample.myCreateCode.command"
label="Create Sample Code"
menubarPath="mysample.actionset.menu1/mysample.groupMarker2">
</action>
</actionSet>
</extension>
The above actionSet has many other actions. I want the action (menu) "Create Sample Code" to be enabled or disable based on the value of a preference variable. How can I do this?
In the enablement attribute you can write a property tester where you can test your preference variable.
For more on property testers
http://wiki.eclipse.org/Platform_Expression_Framework
http://www.robertwloch.net/2011/01/eclipse-tips-tricks-property-testers-with-command-core-expressions/
Also just as a suggestion , you should not use action sets as they are deprecated. Commands should be used. Unless you are in a legacy code :)

netbeans, xml autocomplete for attributes fails for default namespace

I've defined two namespaces: beans, aop.
If you cut & paste into a Netbeans the tag that i've written below, and you try to edit a tag inside beans:beans you should observe this behavior:
<beans:import />
typing this the autocomplete assists you, and after the element name, it proposes the list of attributes (in this case just one).
<aspectj-autoproxy />
typing this the autocomplete dosn't propose the list of attributes (there are two attributes: proxy-target-class, expose-proxy)
It happens for the attributes of the default namespace's elements.
Is it a bug? Am I doing something wrong?
I've tested on NB 6.9 and NB 7.0
thanks in advance
<beans:beans
xmlns:beans ="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
try here
</beans:beans>

How do I update an Eclipse template variable on the fly?

I've added the following new Eclipse template via extension point. It simply adds a template for a sample testTag tag.
<!-- Add code template -->
<extension point="org.eclipse.ui.editors.templates">
<template autoinsert="true"
contextTypeId="html_tag"
description="[Description] Template populated by Snippet values ***"
id="org.eclipse.jst.jsf.ui.newHtmltag"
name="testTag">
<pattern>
<![CDATA[
<testTag style="background: ${color}"></testTag>
]]>
</pattern>
</template>
<resolver
contextTypeId="html_tag"
type="src"
class="TestTagTemplateVariableResolver">
</resolver>
</extension>
What I'd cannot figure out is how to change the value of the $(color) variable at runtime. More specifically, when the user presses Ctrl + Space (or the equivalent for content-assist) and types in "testTag" and presses Enter -- instead of the "color" placeholder text, I'd like it replaced by some other text value I have in another class. How do I do this?
This email chain from 2004 says it might not be possible:
the Java editor chooses not to respect resolvers contributed to its two context types ('java' and 'javadoc'), but only recognizes the built-in resolvers.
The html editor you are working with may have a similar restriction.