Eclipse 4.x popup menu visibility based on tree viewer selection - eclipse

I'm creating an eclipse 4 plugin project. I have a tree viewer that shows model elements.
Problem:
I need to show a popup menu based on the selection that I'm doing on the tree viewer.
I tired to use core expression as explained in the link:
http://www.vogella.com/tutorials/EclipseRCP/article.html#menuadvanced_popup
But whenever I attach the core expression with my popup menu it disappears.
The popup menu appears for all the elements if i don't attach the core expression.
Is there something else that i'm supposed to do to get things right ?
or Should i use a different approach ?
Please find the below snap of my Application.e4xmi file
My plugin.xml file definition for the core expression
<definition
id="xxx.abc.project.addchilddefinition">
<with variable="org.eclipse.ui.selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="xxx.abc.project.model.ObjectName">
</instanceof>
</iterate>
</with>
</definition>
I've registered my popup menu using the below code snippet:
menuService.registerContextMenu(treeviewer.getControl(),
"xxx.abc.project.popupmenu.addchild");
menuService is the EMenuService object and "xxx.abc.project.popupmenu.addchild" is my popupmenu id

You need to use a with element to specify that the selection should be used:
<definition
id="xxx.abc.project.addchilddefinition">
<with
variable="org.eclipse.ui.selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="xxx.abc.project.model.ObjectName">
</instanceof>
</iterate>
</with>
You must also call the ESelectionService setSelection(Object) method when the tree selection changes.
The selection variable org.eclipse.ui.selection is defined in IServiceConstants.ACTIVE_SELECTION

Related

How to show the Eclipse Move context menu in project explorer

I have noticed that by default Eclipse "Move" context menu is not shown when a folder and a file is multi selected at once.
My scenario is i want to show the my context menu -Move for certain type of files and folders when multi selected.
<and>
<iterate
operator="or">
<with
variable="org.eclipse.ui.selection">
<adapt
type="org.eclipse.core.resources.IResource">
<or>
<test property="com.sap.ndb.studio.dwb.team.ui.expressions.isContainedInSharedProject" value = "true"/>
<test property="com.sap.ndb.studio.dwb.team.ui.expressions.isFile" value="true"/>
<test property="com.sap.ndb.studio.dwb.team.ui.expressions.isFolder" value="true"/>
</or>
</adapt>
</with>
</iterate>
</and>
How can i enable my context menu for the type folder and a file in multi select?
Regards,
Pavitra
in Project explorer, select those files that you want to move,
right click on one of them, select Refactor, by default Move... option is available.
or simply you can use shortcut to move: Alt+Shift+V.
if it wasn't enabled or doesn't exist, in eclipse go to Window menu,
select Customize perspective...
a windows with for tabs will appear.
select Menu visibility tab then expand Refactor, then tic the Move... checkbox.

Hide unhide toolbars based on the editors open

In eclipse there are certain toolbars which becomes visible when I open java editor. It goes invisible when I close the editor (only one editor was open.)
Here opening the java editor is not changing the perspective.
How to achieve this functionality in eclipse rcp application?
Pre-3.3 this can be accomplished through a org.eclipse.ui.IEditorActionBarContributor, defined in your editor extension with the contributorClass attribute.
Since 3.3 a core expression definition can be used in a visiblewhen expression.
For example, a re-usable core expression for an editor can be defined as follows
<extension point = "org.eclipse.core.expressions.definitions">
<definition id="org.eclipse.ui.examples.contributions.activeEditor">
<with variable="activeEditorId">
<equals value="org.eclipse.ui.examples.contributions.editor"/>
</with>
</definition>
</extension>
Then the following expression can be used to control whether a menu or toolbar is visible
<visibleWhen>
<reference definitionId=""org.eclipse.ui.examples.contributions.activeEditor"/>
</visibleWhen>
You need to look at Activities and Contexts to hide/unhide contributions
http://www.vogella.com/blog/2009/07/13/eclipse-activities/

Add popup menu to Eclipse Problems view

I wish to add a popup menu to the Eclipse Problem View. I can add the menu but I want it to only appear under certain condition. Like for example when a certain type of problem is selected in the problems view.
Assuming you are using the "org.eclipse.ui.menus" extension point and the command framework, you can set the visibleWhen part of the menu definition to something like;
<visibleWhen>
<with variable="selection">
<iterate>
<and>
<instanceof value="com.example.MyClass">
</instanceof>
</and>
</iterate>
</with>
</visibleWhen>
This should only make the option visible when the selected items are of the correct instance.
It is also possible to set enabled and active states on command handlers in a similar way.

Enable/disable menu item in Eclipse plugin

I've made a pop-up menu with one menu item, I want to enable it only when I do a right click on a tree item of a certain class type otherwise disable it.
How can I achieve this?
You can add a handler that uses activeWhen and associate it with that menu's command id.
Here is a handler that makes a command active only when the current selection is not empty, and the selection is an item that can be adapted to an object of type Widget:
<extension point="org.eclipse.ui.handlers">
<handler class="com.myproject.handlers.ExportWidgetHandler"
commandId="com.myproject.commands.exportWidget">
<activeWhen>
<with variable="selection">
<iterate ifEmpty="false" operator="and">
<adapt type="com.myproject.objects.Widget"/>
</iterate>
</with>
</activeWhen>
</handler>
</extension>
We can enable or disable a menu item by overriding the isEnabled method of Handler class. Below is the sample code :
#Override
public boolean isEnabled() {
return false;
}

Override Eclipse File > Save Action

I am trying to override the Eclipse File > Save menu action to add some functionality.
I have tried the following things
a) Create a new action and add it to the global action handler
actionBars.setGlobalActionHandler(ActionFactory.SAVE.getId(), mySaveAction);
actionRegistry.registerAction(action);
b) Create a new handler and override the save command
<extension point="org.eclipse.ui.handlers">
<handler commandId="org.eclipse.ui.file.save"
class="com.diagrams.ui.SaveFileHandler">
<enabledWhen>
<with variable="activePartId">
<equals
value="com.diagrams.editors.MultiPageEditor" />
</with>
</enabledWhen>
<activeWhen>
<with variable="activePartId">
<equals
value="com.diagrams.editors.MultiPageEditor" />
</with>
</activeWhen>
</handler>
</extension>
With both these approaches I have been able to override the Keyboard Ctrl+S functionality but the "File > Save" menu seem to work differently.
Would really appreciate any help, Thanks
In an RCP application, you can contribute the Save action in your ActionBarAdvisor. This also registers the action so it is available from the save command.
But as a plugin in the Eclipse IDE, the IDE provides the ActionBarAdvisor and hooks up the Save action in the File menu. Because that's not technically a command (Actions are a step above an SWT.Selection listener) that's why you can't override the File>Save action.
However, each part provides its own save implementation, so you can do whatever you want in your MultiPageEditor.
The other option is to use org.eclipse.ui.commands.ICommandService.addExecutionListener(IExecutionListener) and add an IExecutionListener (or IEL2). That can listen for the save command, the ID is declared in org.eclipse.ui.IWorkbenchCommandConstants.
It might have something to do with the activePartId being different when the main menu is selected versus a keystroke or using the right-click menu. Have you looked at other extension points?