Contributed control to the status bar not visible - eclipse

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

Related

How can I calculate the enablement of a handler based on the parameter value of a parameterized command in Eclipse RCP/IDE?

In an Eclipse IDE extension, I have declared a parameterized command, two menuContributions, and a handler as usual (very basic example):
<extension point="org.eclipse.ui.commands">
<command id="my.command.id" name="FooBar">
<commandParameter
id="my.command.id.param"
name="Param"
optional="false">
</commandParameter>
</command>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution allPopups="false" locationURI="...">
<command
commandId="my.command.id"
label="Foobar">
<parameter
name="my.command.id.param"
value="BAZ">
</parameter>
</command>
<command
commandId="my.command.id"
label="Foobar (disabled)">
<parameter
name="my.command.id.param"
value="DISABLED">
</parameter>
</command>
</menuContribution>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler class="net.winklerweb.MyHandler" commandId="my.command.id" />
</extension>
The handler is also pretty basic in the minimal example:
public class MyHandler extends AbstractHandler {
#Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
var param = event.getParameter("my.command.id.param");
System.out.println("Hello " + param);
return null;
}
#Override
public void setEnabled(Object evaluationContext) {
var param = // how ???
super.setBaseEnabled(!"DISABLED".equals(param));
}
}
The question is: How can I set the handler's enablement based on the parameter value.
In the above minimal example, the decision would be made directly based on the parameter value. In my concrete use case, the parameter value points to a project and I want to disable/enable the menu contribution based on whether the project is accessible or not. But once I know how to get access to a parameter value in the setEnabled() method, the rest is trivial.
I have tried to guess a key under which the parameter could be contained in the evaluationContext, but all my guesses were wrong and inspecting the context with a debugger also did not yield any example. The parameterized command feature of Eclipse does not seem to be very well documented, so I am at a dead end right now.
(My example right now is programmatic, but if there was a solution to use the parameter of the handled command declaratively in an enabledWhen expression, I'd happily take that as well...)

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.

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

"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.