Enable and disable org.eclipse.ui.actionSets - eclipse

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 :)

Related

Visibility in popup menus

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>

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.

Contributed control to the status bar not visible

I would like to place a control in the status bar of the workbench window. The whole process should be straight-forward, but whatever I try, the status bar contribution does not become visible.
Because I do not own the application but just contrinbute a plug-in to the IDE, WorkbenchWindowAdvisor and friends are not an option.
The extension point is this:
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="toolbar:org.eclipse.ui.trim.status" allPopups="false">
<control class="MyContributionItem" id="myContributionItem" />
</menuContribution>
</extension>
and the MyContributionItem class is like this:
public class MyContributionItem extends WorkbenchWindowControlContribution {
protected Control createControl( Composite parent ) {
Label label = new Label( parent, SWT.NONE );
label.setText( "STATUS BAR!" );
return label;
}
}
What I tried so far, all without sucess (i.e. the status bar contribution does not show up):
added ?after=org.eclipse.jface.action.StatusLineManager to the locationURI
put a breakpoint in MyContributionItem#createControl(), it is never reached
target platform: Eclipse Platform SDK 3.8 or 4.4: makes no difference
changing the allPopups attribute to true
I am quite sure that I am mising something very obvious, ...
Try this:
Plugin.xml
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.trim.status">
<toolbar
id="org.ancit.search.web.searchbar"
label="Search Bar">
<control
class="org.ancit.search.web.controlContributions.GoogleSearchControlContribution"
id="org.ancit.search.web.controlContributions.GoogleSearchControlContribution">
</control>
</toolbar>
</menuContribution>
</extension>
Check GoogleSearchControlContribution class on github

Checking for null selection variable value in the command framework of Eclipse RCP

How can I check whether the selection variable value is null when I open the editor?
I want to activate a context menu command when the certain editor is opened, it is not dirty and nothing was selected by the user. First two coditions are working fine:
<and>
<with
variable="activePartId">
<equals
value="com.eclipse.someeditor">
</equals>
</with>
<with
variable="activePart">
<not>
<test
property="com.eclipse.isEditorDirty">
</test>
</not>
</with>
<and>
My current problem is that the straightforward solution would be to include another condition:
<with variable="selection">
<count value="0" />
</with>
Unfortunately when the editor is first opened, the count is not 0. It probably is null. If the user selects something and then deselects, it becomes 0. Any ideas how to check whether the value of selection is null or similar?
UPDATE
I also tried creating a property tester that checks whether the value of the selection is null, but the code is not executed. I think that it happens, because it does not even go inside the selection if the user did not select/deselect anything.
<with variable="selection">
<test
property="com.eclipse.isSelectionNullOrEmpty">
</test>
</with>
Try using 'instanceof' and 'and'. Like:
<and>
<instanceof value="fully_qualified_class_name_of_your_selection_class"/>
<equals value="0" />
</and>
Hope this helps.
The following solution worked:
I made the Editor a SelectionProvider that is capable of delegating to another SelectionProvider to be able to switch between them (e.g. when I have an inner table that provides selection as well and take procedence.
Then I set the getSelection() to return an empty StructuredSelection when there is no delegate (opposite to returning null). Thanks to that there is always a StructuredSelection being returned, but sometimes it is empty.
public ISelection getSelection() {
return delegate == null ? new StructuredSelection() : delegate.getSelection();
}
After that I could add the simple condition that I mentioned earlier:
<with variable="selection">
<count value="0" />
</with>
Some people may find this useful.

"firePropertyChange(IEditorPart.PROP_DIRTY)" could not make the “Save” menu item active

In my editor, I have below function to set the editor dirty whenever I make some changes in the text widgets within the editor,
private void setDirty(boolean b){
isDirty = b;
firePropertyChange(IEditorPart.PROP_DIRTY);
}
The issue is that I could see the editor title shown a symbol “*” into dirty state when setDirty(…) is called, but I see the “Save” menu item is still gray(see below snapshot).
The "Save" menu item is defined by plugin.xml as follows,
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="myProject.file"
label="File">
<command
commandId="org.eclipse.ui.file.save"
label="Save"
style="push">
</command>
</menu>
</menuContribution>
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="myProject.toolbar1">
<command
commandId="org.eclipse.ui.file.save"
label="Save"
style="push">
</command>
</toolbar>
</menuContribution>
</extension>
Could anyone help to see the issue? What's wrong in my implementation?
Did you override isDirty() in your editor class? Something like:
#Override
public boolean isDirty() {
return isDirty;
}
I just tried to add following codes into ApplicationActionBarAdvisor class, then the Save menuitem could be enabled and my issue is solved,
protected void makeActions(IWorkbenchWindow window) {
register(ActionFactory.SAVE.create(window));
}
does the "register(...)" must be called?
It is strange that I run my RCP at Eclipse 3.7 without calling register(...), the Save menu item could be enabled by firePropertyChange(). But it doesn't work at eclipse 4.0. Therefore, I created this question here.
Try to call editorDirtyStateChanged() after firing property change.