Eclipse e4 Context Menus - eclipse

I'm trying to build an RCP application with Eclipse 4.2.
First problem; I have a TreeViewer. When an item is selected in the tree viewer, I need one of two stacked MParts to be displayed. How do I activate a part in a part stack in e4?
Second; I have a context menu associated with one of my parts. I need the menu to appear only when certain composites are right clicked. I have a mouse down handler that is adding an object with all the data I need into the current context (IEclipseContext), but I don't know how to access that from a CoreExpression (which seems to be the only real way to control the visibility of the menu). The object is removed from the context on mouse up. Say my class is RightClickData. How do I write a core expression to test whether an instance of RightClickData exists in the current context?
Edit, part of my Application.e4xmi:
<children xsi:type="basic:InputPart" xmi:id="_6nSEEJuhEeGpoZf9DvK6pQ" elementId="com.example.MyEditor" contributionURI="bundleclass://MyPlugin/com.example.MyEditor" label="The Editor" tooltip="My Editor">
<menus xsi:type="menu:PopupMenu" xmi:id="_UdHPEJ4SEeGi0uxvOaa4gw" elementId="MyPlugin.rsmenu" label="Asdf" tooltip="asdf">
<children xsi:type="menu:HandledMenuItem" xmi:id="_jwOz0J4SEeGi0uxvOaa4gw" label="Add Column" command="_fhTxwJ4REeGi0uxvOaa4gw"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_trlpsJ4SEeGi0uxvOaa4gw" label="Delete Column" command="_iAIkoJ4REeGi0uxvOaa4gw"/>
<children xsi:type="menu:MenuSeparator" xmi:id="_v8f8YJ4SEeGi0uxvOaa4gw"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_wR7Z0J4SEeGi0uxvOaa4gw" label="Add Row" command="_bXrp8J4REeGi0uxvOaa4gw"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_yf5GMJ4SEeGi0uxvOaa4gw" label="Delete Row" command="_l58HwJ4REeGi0uxvOaa4gw"/>
</menus>
</children>

For your first question, you can use org.eclipse.e4.ui.workbench.modeling.EPartService.activate(MPart) to activate the part you want.

I make visible my parts like this:
MPart part = partService.findPart("my.part.id");
if( part != null ){
if( part.isVisible() == false )
part.setVisible(true);
partService.showPart( part, PartState.VISIBLE);
}

Related

Eclipse RCP dynamic MenuContribution hide and unhide with CoreExpression

I'm trying currently to add a visibleWhen condition for a MenuContribution in my Eclipse RCP application. We've different versions of our app and we've differentiate them with a IClientConfiguration interface/service which works more or less as feature toggles.
Depending on witch app-$version.product is called different implementations of the configuration will part of the app.
However.. get to the current situation: We've a menu which is defined in a common fragment.e4xmi and I would like to hide one menu depending on the configuration. For that I've added the toggle isAdminMenuActive(). I've hoped that I can create a CoreExpression and using (somehow) the return value of this service. But unfortunately I can't get this to work by now.
What I tried was the following:
fragment.e4xmi:
<elements xsi:type="menu:MenuContribution" xmi:id="_ZuL6oA4IEeqbk5lJVdc6WQ" elementId="com.example.client.menucontribution.administration" parentId="com.example.product.main.menu">
<children xsi:type="menu:Menu" xmi:id="_ngOY4LOMEemRpJf6BiOdqQ" elementId="com.example.client.menu.administration" label="%UI.general.administration">
<visibleWhen xsi:type="ui:CoreExpression" xmi:id="_rrmcQA4lEeqbk5lJVdc6WQ" coreExpressionId="com.example.client.configuration.adminMenuActiveExpression"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_B4RqsLONEemRpJf6BiOdqQ" elementId="com.example.client.menuitem.action.administration" label="%UI.action" tooltip="%UI.action" command="_XnSp8LOPEemRpJf6BiOdqQ"/>
</children>
</elements>
plugin.xml
<extension
point="org.eclipse.core.expressions.definitions">
<definition
id="com.example.client.configuration.adminMenuActiveExpression">
<with variable="com.example.client.configuration.adminMenuActive">
<equals
value="true">
</equals>
</with>
</definition>
</extension>
Startapp.java
#PostConstruct
public void init(IEclipseContext context) {
context.set(com.example.client.configuration.adminMenuActive,
configuration.isAdminMenuActive().toString());
}
The current behaviour is that the menu is never shown no matter which configuration is loaded. I've tried if the variable is added to the IEclipseContext and this happens very early in the start up phase of the application. Currently I've no idea what's wrong.
Rather than using a core expression in the fragment.e4xmi you can use an 'Imperative Expression'. You do this by changing the 'Visible-When Expression' in the fragment editor.
An imperative expression specifies a Java class which is called directly making it easier to access your code. The method tagged with #Evaluate is called to evaluate the expression. Maybe something like:
#Evaluate
public boolean evaluate(#Named("com.example.client.configuration.adminMenuActive") String active)
{
return Boolean.valueOf(active);
}
Using this makes it much easier to see what is happening.

How to make Eclipse enabledWhen expression work for selection in non-focused view?

I have a handler which is connected to a menu contribution and a command. The menu contribution adds a button to a view and I want to have the button enabled depending on a selection in the Debug view.
So here's the expression:
<handler
class="com.example.myhandler"
commandId=" com.example.mycommand">
<enabledWhen>
<with
variable="selection">
<iterate
ifEmpty="false">
<instanceof
value="org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext">
</instanceof>
</iterate>
</with>
</enabledWhen>
</handler>
This works absolutely fine to the point where the Debug view has focus, meaning that if I select the element in the Debug view, the added button in a separate view is also enabled (as desired). As soon as I click on the view where I added the button via the menu contribution, then it's suddenly disabled (I guess because the selection is empty even though it's still selected; but the Debug view has no focus). How can I make this work so that the selection is still considered independently of the Debug view's focus state?
(You appear to be asking a DSF specific question, which has a different answer to the "general" case your title refers to. As such this answer probably solves your problem, but may not solve the general case.)
A complete example of extending DSF-GDB is provided in the CDT source repo in the org.eclipse.cdt.examples.dsf.gdb bundle.
That example defines a new command org.eclipse.cdt.examples.dsf.gdb.command.showVersion:
<!-- Example showing how to add a custom command with toolbar/menu contributions with DSF.
The example command id is org.eclipse.cdt.examples.dsf.gdb.command.showVersion.
In this example, when run it will display the version of GDB that is connected. -->
<extension point="org.eclipse.ui.commands">
<command
categoryId="org.eclipse.cdt.debug.ui.category.debugViewLayout"
description="Show the GDB Version in a pop-up"
id="org.eclipse.cdt.examples.dsf.gdb.command.showVersion"
name="Show GDB Version">
</command>
</extension>
It goes on to show how to contribute the command to the menus with the org.eclipse.ui.menus extension point. Then binds the command to a command handler with the org.eclipse.ui.handlers extension point.
Up until this point, DSF behaves the same as "normal" commands. But in DSF (using the retargettable command infrastructure provided by platform debug), the handler is not directly the command you are trying to run, but is a subclass of DebugCommandHandler.
DSF then can bind that command, using adapters to the concrete command implementation, depending on what the selected debug session in the Debug view is. In the show version case, this is GdbShowVersionHandler (implementation of IDebugCommandHandler). The handler has a canExecute which can connect to the back end if needed (gdb) to see if the current selection is applicable. The canExecute receives something that can be converted into a DSF context object like this:
private Optional<ICommandControlDMContext> getContext(final IDebugCommandRequest request) {
if (request.getElements().length != 1 || !(request.getElements()[0] instanceof IDMVMContext)) {
return Optional.empty();
}
final IDMVMContext context = (IDMVMContext) request.getElements()[0];
ICommandControlDMContext controlDmc = DMContexts.getAncestorOfType(context.getDMContext(),
ICommandControlDMContext.class);
if (controlDmc != null)
return Optional.of(controlDmc);
return Optional.empty();
}
PS I added this example to CDT to help another extender a while back. The conversation on cdt-dev may be useful too? This was all added initially for this bug, with its associated gerrit which pulls all the changes for adding a new command into one place.

How to customize the context menu in the solution explorer for a specific project type?

Description
I have developed a Visual Studio extension (VSPackage) which adds a new Project Type to Visual Studio (using CPS Project System). I also have added some Commands to the VSPackage.
When right clicking on my Project Node in the Solution Explorer, I want to have a customized context menu to appear.
Example
For example: in the screen shot below, I want to get rid of the Build command and add a custom command (e.x. mycommand).
I tried..
Setting the Parent of my custom command to IDM_VS_CTXT_PROJNODE.
Question
When I create a new Custom Project Type, How to create a new Context Menu for my Project Nodes in the Solution Explorer?
How to remove/add Commands to the Context Menu only for the Custom Projects:
If I have a C# project, the context menu should be the default one, if I add a MyProjectType project, I want to see a different context menu when right clicking on the Project Node in the Solution Explorer.
You were close with the IDM_VS_CTXT_PROJNODE parent.
Here is how I achieved it in my FluentMigratorRunner extension, which only shows its context menu item for a project if it has a reference to the FluentMigrator NuGet package.
Step 1: Add a submenu to the context menu
<Menus>
<Menu guid="guidCmdSet" id="packageMenu" priority="0x0300" type="Menu">
<Parent guid="guidSHLMainMenu" id="IDG_VS_CTXT_PROJECT_BUILD" />
<CommandFlag>DynamicVisibility</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<Strings>
<ButtonText>CPSProject</ButtonText>
<CommandName>CPSProject</CommandName>
</Strings>
</Menu>
Note the added special CommandFlag elements.
Step 2: Add a group to the menu
<Groups>
<Group guid="guidCmdSet" id="packageMenuGroup" priority="0x0600">
<Parent guid="guidCmdSet" id="packageMenu" />
</Group>
</Groups>
Step 3: Add a button
<Button guid="guidCmdSet" id="specialBuildActionId" priority="0x0100" type="Button">
<Parent guid="guidCmdSet" id="packageMenuGroup" />
<CommandFlag>DynamicVisibility</CommandFlag>
<Strings>
<ButtonText>Special build</ButtonText>
</Strings>
Step 4: Add the menu in your *Package.cs
protected override async System.Threading.Tasks.Task InitializeAsync(System.Threading.CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
// Initialize the Fluent Migrator Menu, should only be visible for projects with FluentMigrator reference
var mcs = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
var menuCommandId = new CommandID(packageCmdSetGuidString, 0x1010);
var menuItem = new OleMenuCommand(null, menuCommandId);
menuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;
mcs.AddCommand(menuItem);
}
private void MenuItem_BeforeQueryStatus(object sender, EventArgs e) =>
((OleMenuCommand)sender).Visible = ???;
Note the added BeforeQueryStatus eventhandler.
In that eventhandler you can check the type of the project and return a boolean controlling if the extra context menu should be shown yes or no
I'm not sure about removing the existing Build menu item... but as for having context menu items appear only when right-clicking on your custom project type, <VisibilityConstraints> might help:
(Excerpt from an example usage):
Often a package is being loaded to run a BeforeQueryStatus method for a button to determine its visibility. With <VisibilityConstraints> we can determine the visibility of a button without running BeforeQueryStatus and therefore don't need to load the package before the user clicks the button.
I've only ever used this strategy to show menu items for specific file types, but there are also some term types related to projects that might be more useful for this scenario.

White/UI Automation doesn't recognize the container (pane control) in tab

I'm automating the windows application. I tried to access the pane element(which has text box, combo box controls) under tab control, but it's not accessible. White returns null.
I tried other techniques like UI automation TreeWalker (Rawview, Control view, content view), but nothing helps.
refer images in below links:
https://dl.dropboxusercontent.com/u/68446125/Tab.png
https://dl.dropboxusercontent.com/u/68446125/General%20Pane.png
As in picture 1, tab control is retrieved properly by White/UI Automation, but the child element General* Pane is not returned and it's controls are not accessible (Refer pic 2 highlighted), the first accessible child element is "General* tab Item".
Strange thing is, these controls are accessible in Inspect.exe (in windows SDK). I tried following methods to retrieve controls, but General* Pane is never accessible through White/UI Automation.
var tab = Window.Get<Tab>(SearchCriteria.ByControlType(ControlType.Tab).AndByClassName("TwoPageControl")); // Tab control is retrieved properly
var pane = tab.GetElement(SearchCriteria.ByControlType(ControlType.Pane).AndByText("General*")); // this line returns NULL
var pane1 = revWindow.GetElement(SearchCriteria.ByControlType(ControlType.Pane).AndByText("General*")); // this line returns NULL
var pane2 = revWindow.Get<Panel>(SearchCriteria.ByControlType(ControlType.Pane).AndByText("General*"));// throws exception "Failed to get ControlType=pane,Name=General*,ControlType=pane"
Tried windows UI automation code as well, but no luck.
System.Windows.Automation.Condition cd1 = new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab),
new PropertyCondition(AutomationElement.ClassNameProperty, "TwoPageControl"));
AutomationElement a = window.FindFirst(TreeScope.Descendants, cd1); // Tab control is returned properly here
TreeWalker rawViewWalker = TreeWalker.RawViewWalker;
AutomationElement cc = rawViewWalker.GetFirstChild(a); // General * Pane is not returned, instead General* Tab item is returned, though it's not the first child.
var cd = rawViewWalker.GetNextSibling(cc); // this returns next pane element available, not General * Pane.
Please help me how to access General * Pane and it's children under tab control. Any help is much appreciated.
I had exactly the same problem with my application. I use Inspect and open source UIAVerify where Pane element was visible as children of tab element. But when i compile Verify as a .Net 4.5 project, Pane element was not seen as part of tab. It only appears when I pointed it directly. I also search for my Pane element in Descendants of main window but there was nothing. I think it has something to do with dynamically creation of that pane content (i mean there is different content when you choose different tabItem).
I think you can't get access to that element from tree point of view.
My solution was to use AutomationElement.FromPoint Method.
http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.frompoint(v=vs.110).aspx
I also think this shoud help if you have conntact with people developing program.
Some controls on a page are not visible for MS UI Automation
You can break up the TabControl to just the TabPanel, but without its ContentPresenter. You create your own ContentPresenter which used the content of the selected TabItem.
This way, White will be able to discover the controls in the ContentPresenter.
This is a workaround and it is a shame you have to change your WPF code just because "something goes wrong with UIA". But this is the best I could do.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TabControl x:Name="uiTab" Grid.Row="1">
<TabControl.Template>
<ControlTemplate TargetType="TabControl">
<TabPanel IsItemsHost="True" VerticalAlignment="Stretch"/>
</ControlTemplate>
</TabControl.Template>
<TabItem Header="first">
<Button>FIRST</Button>
</TabItem>
<TabItem Header="second">
<Button>SECOND</Button>
</TabItem>
</TabControl>
<ContentPresenter Content="{Binding SelectedItem.Content, ElementName=uiTab}"/>
</Grid>

Eclipse Plugin: How to show a menu contribution only when a persective is active?

I created a menu contribution for an Eclipse plugin.
I would like the menu only to be visible when the plugin perspective is active.
Here is what I found, in an open source project, and it works for me:
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu
id="menu1"
label="Menu 1">
<visibleWhen checkEnabled="false">
<with variable="activeWorkbenchWindow.activePerspective">
<equals value="myperspective"/>
</with>
</visibleWhen>
<dynamic class="MenuPopulationClass"
id="MenuPopulation"/>
</menu>
</menuContribution>
</extension>
I suggest to use the Command Framework.
this tutorial can help you
To restrict the visibility of your commands you should check par. 5
This is a bare outline of what you need to do.
To limit the visibility of the top level menu, create an empty ActionSet through the Extension Point Selection dialog.
Next, add a visibleWhen expression to the top level menu item defined in the plugin.xml. Right click on the menu item and select New -> visibleWhen. Right click on the visibleWhen element and select New -> with
Set variable - "activeContexts"
Right click on the with element and select New -> iterate. Select the new iterate element and modify its properties so that any matching element will cause the expression to evaluate true.
Right click on the iterate element and select New -> equals, then modify the value property.
value = your new ActionSet
Finally, add the top level menu item to your ActionSet.