How to create a toggle/radio item in the menu/toolbar of an Eclipse e4 application? - eclipse

What is the canonical way of creating a menu item that implements a toggle or radio state in an eclipse e4 rcp application?
It seems such a basic thing, but all the documentation I found relies on e3 API and creates dependencies to org.eclipse.ui, which is not allowed in e4.

One possible way that I use for a radio button menu which saves the radio button state in the part class.
I use multiple Direct Menu Item entries with the type set to Radio.
I set a Tag value (on the supplementary page of the menu item) to the value I want to associate with the menu item.
I use the same handler for all of the menu items.
In the handler I inject the MPart and the MItem:
#Execute
public void execute(final MPart part, final MItem mitem)
{
// Only take action on the selected radio item
if (!mitem.isSelected())
return;
// The tag value specifying the radio state
String tag = mitem.getTags().get(0);
// Get the part class
MyPart myPart = (MyPart)part.getObject();
// tell the part about the state change
myPart.setState(tag);
}
Instead of the MPart you could also use any class that is in the Eclipse Context - such as one declared as #Creatable and #Singleton.

Related

Is there a possibility to send ToolbarItems bounds as paramter to an command handler?

In my E4 Application I have a Toolbar with ToolbarItems, on click on one of them I want to display a small dialog directly under the Toolbaritem. To achieve that I need the coordinates of the button.
Is there a way to pass it via paramters to the #Execute annotated method in my handler?
I solved it via injection of MPart into my Handler and a call of getToolbar. But it looks very dirty.
You can inject the MToolItem to get the item rather than injecting the MPart.
#Execute
public void execute(MToolItem mitem)
{
ToolItem item = (ToolItem)mitem.getWidget();
...
}
But you can associate a menu with a tool item by checking the "Menu" check box in the 'Handled Tool Item' entry in the e4xmi file. You will then be able to define menu items as children of the tool item.

Change the Part selection in RCP Application when Button click in different Part

In Eclipse RCP Application UI design of my project will be as below:
PartSashContainer->PartStack->Part1, Part2,Part3.,Part4,Part5
|
->PartStack->Part6
Part6 contains the button. If button click in Part6 should set the selection to Part1.
Can you please provide how to achieve the Part selection from different Part.
Use the EPartService showPart method:
#Inject
EPartService partService;
...
partService.showPart("part id", PartState.ACTIVATE);
Use an injected EPartService where your button is, then pass the Part1's ID to the service to find the part:
final MPart part1 = partService.findPart("part1.id");
part1.setToBeRendered(true);
part1.setVisible(true);
This snippet creates it if it wasn't there. TBH I don't really know if this grants focus or not.

How to add Perspective Bar Switcher to pure eclipse 4 rcp application

I have created a pure Eclipse e4 rich client platform application application model. I created multiple perspectives using perspective stack, but I am unable to switch other perspective because there is no default perspective bar or switcher icon present in Eclipse e4. How to implement a perspective switcher in pure Eclipse e4?
EPartService.switchPerspective will do the actual switch, but you will have to design and implement the UI.
You could use a ToolBar in the window Trim Bar with buttons for each perspective. Alternatively a Combo as a Tool Control with a list of the perspectives, it is up to you.
To put a control at the right of a Trim Bar you need to add two Tool Control objects to the trim. Something like:
The first Tool Control is just a spacer to fill the center of the bar.
On the tags tab for the control add the word stretch to tell e4 to stretch this control over as much space as possible:
You will also have to specify a class for the control. This just needs to create an empty Composite to occupy the space. For example:
public class SpacerControl
{
#PostConstruct
public void postConstruct(final Composite parent)
{
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new FillLayout());
}
}
The second Tool Control will contain your Combo control for the perspective switch. Something like:
public class ComboControl
{
#PostConstruct
public void createGui(final Composite parent)
{
Combo combo = new Combo(parent, SWT.READ_ONLY);
... initialize Combo, add listeners, ....
}
}
This should end up looking something like this:

Eclipse RCP 4.x show view

I'm working for a short time on the libraries of eclipse 4.x someone could tell me how can I open a view through from the context menu? Thank you in advance.
To show a part anywhere you should define a command in the application model and a handler for the command. To show a part in the handler use:
#Execute
public void execute(EPartService partService)
{
MPart mpart = partService.showPart(part id, PartState.ACTIVATE);
}
In the application Part definition for your part add a Popup Menu to the Menus section. In the popup menu define a HandledMenuItem for your command.
To register the popup menu as the context menu for a control (tree, table etc) use:
#Inject
private EMenuService;
...
menuService.registerContextMenu(control, menu id);

Use default Common Navigator Action Set for IResource wrapper class

I am developing an Eclipse Plugin.
My View extends CommonNavigator.
the View's contents is an hierarchy of MyWrapper classes
class MyWrapper implement IAdaptable{
IResource resource;
MyWrapper parent;
List<MyWrapper> children;
}
I want to display PopUp menu the same as the Default Common Navigator displays.
I have registered an AdapterFactory that adapt MyWrapper to IResource.
List of actions that are displayed:
New
Remove from context (disabled)
Import
Export
Refresh
Validate
Team
Compare with
Restore from Local History
Properties
List of actions that I need for menu:
Copy
Paste
Rename
does anybody know how to do this?
This tutorial explains how to add those actions: Building a Common Navigator based viewer, Part I: Defining the Viewer Search for the part which adds the "org.eclipse.ui.navigator.resources.*"