Hide unhide toolbars based on the editors open - eclipse

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/

Related

"Run" menu is placed before other menus in an Eclipse RCP IDE project

I have developed a IDE for a custom language in Eclipse 3 for RCP and RAP developers. Therefore I used IDE plug-ins wherever possible and applicable. This week I was busy migrating the application to Eclipse e4 (with compatibility layer) with Eclipse for RCP and RAP developers 2021-6. Everything works nearly fine so far.
The one thing i cannot figure out is how I can place the "run" menu where I want. It is placed as first menu in the menu bar.
Another strange thing by the way is that if I use Eclipse 2020-6 instead of 2021-6 as development environment, also the search menu is at the wrong position.
The problem is that the run and search menus come from an IDE plug-in and they are implemented as actions and therefore I cannot specify any order in terms of menus. For all other menus I defined appropriate menuContibutions, commands and handlers. For the latter, I can decide where each menu should be placed with the help of plugin.xml by specifying ?before= or ?after=.
I did a search over stackoverflow issues that have to do with adding menus, reorderung menus, mixing actions with commands and handlers and so on but I could not find a solution how I could place the run menu where I want.
I hoped that there would be something like an ID that I can use to specify as ?before= or ?after= in plugin.xml but I think this will not work with actions.
Can anyone give me a hint how I could place all menus in the desired order? Or is this simply impossible when mixing actions and commands+handlers? Is there any actions-wrapping functionality in order for me to specify ?before= or ?after= in my menuContibutions in plugin.xml?
If you mean the Run menu added by the org.eclipse.debug.ui plug-in this is created using and action set:
<extension point="org.eclipse.ui.actionSets">
<actionSet
label="%BreakpointActionSet.label"
visible="false"
id="org.eclipse.debug.ui.breakpointActionSet">
<menu
label="%RunMenu.label"
path="additions"
id="org.eclipse.ui.run">
which is adding the Run menu at the position additions in the main menu.
The standard main menu created by org.eclipse.ui.internal.ide.WorkbenchActionBuilder creates the main menu like this:
#Override
protected void fillMenuBar(IMenuManager menuBar) {
menuBar.add(createFileMenu());
menuBar.add(createEditMenu());
menuBar.add(createNavigateMenu());
menuBar.add(createProjectMenu());
// This line creates the 'additions' position
menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
menuBar.add(createWindowMenu());
menuBar.add(createHelpMenu());
}
Thanks to greg-449, I finally figured it out. The following definition finally solved my problem (--> ?before=org.eclipse.ui.run):
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu?before=org.eclipse.ui.run">
<menu
id="at.boi.tabex.dvl.mainmenu.file"
label="&File">
</menu>
<menu
id="at.boi.tabex.dvl.mainmenu.edit"
label="&Edit">
</menu>
<menu
id="at.boi.tabex.dvl.mainmenu.project"
label="&Project">
</menu>
<menu
id="at.boi.tabex.dvl.mainmenu.tools"
label="&Tools">
</menu>
<menu
id="at.boi.tabex.dvl.mainmenu.table"
label="&Ta&ble">
</menu>
</menuContribution>
</extension>

Open another editor from the eclipse search view

I have a file which can be edited using two different editors.
In eclipses' project explorer you can open a file in a different editor from the context menu.
How can I open a search result (shown in the Search view) in a different editor?
As I am developing an own plug-in, a way to extend the Search view's context menu would be a valid solution as well.
I don' think there is a way to get the search view to use a different editor.
You can contribute to the search view using the org.eclipse.ui.menus extension point. The view id is org.eclipse.search.ui.views.SearchView so something like:
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.search.ui.views.SearchView">
<command
commandId="my.command.id"
label="Test Search View Contrib"
style="push">
</command>
</menuContribution>

In Eclipse RCP, how do we add a combo control to the main toolbar and update it when a new editor is activated

Ok, so I have successfully added the combo control to the main toolbar of my eclipse RCP application using the following extension within one of my application's plugins.
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.company.module.toolbar"
label="Sample">
<control
class="com.company.module.ui.ComboToolbarContribution"
id="zoomControl">
</control>
</toolbar>
I have defined
public class ComboToolbarContribution extends
WorkbenchWindowControlContribution
and the above class returns a combo control that is pre-populated with values 100%, 200%, 300%, 400%
This toolbar is enabled for certain types of editors in my application using the visibleWhen clause on the toolbar extension.
Each editor in the application can support a different zoom level, so the content of the combo needs to be updated everytime an editor is activated to reflect the zoom level currently supported by it.
How do I update this combocontrol to reflect the value of the editor that is currently in focus or activated? Is there any hook available to do so?
Use IPartListener to listen for changes to the active part.
IWokbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IPartService partService = window.getPartService();
partService.addPartListener(listener);
You can use IPartListener#partActivated(IWorkbenchPart part) . See help here
Also check this

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

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

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.