Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » e4ActivePart and visible when
e4ActivePart and visible when [message #865418] Mon, 30 April 2012 11:45 Go to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
I am trying to make a menu item active when a certain part becomes active. In e3 I used the activeEditorId. I have been trying to use the e4ActivePart variable but from looking at the code using it in an injection it always returns an MPart which in turn contains an object of the type I want.

So a core expression such as with="e4ActivePart" and an instanceOf="MyClass" does not work as the class is always org.eclipse.e4.ui.model.application.ui.basic.MPart.

so
<definition id="activepart">
<with variable="e4ActivePart">
<instanceof value="swb3.quotes.QuoteEditor"></instanceof>
</with>
</definition>

is never true, I tried iterating over the active part in case it contained a collection of user classes.

but
<definition id="activepart">
<with variable="e4ActivePart">
<instanceof value="org.eclipse.e4.ui.model.application.ui.basic.MPart"></instanceof>
</with>
</definition>

is always true.

Is there another way to display a menu item only for certain parts.
Re: e4ActivePart and visible when [message #865709 is a reply to message #865418] Mon, 30 April 2012 14:33 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
have you tried something like this in your handler?
@CanExecute
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MApplicationElement part) {
	return part != null && part.getElementId().equals(MY_PART_ID);
}
Re: e4ActivePart and visible when [message #865726 is a reply to message #865709] Mon, 30 April 2012 14:40 Go to previous messageGo to next message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
That works fine for disabling but I don't want the handler menu item to be displayed at all so need to hide it with a visible when rather than have it showing but disabled. The can execute won't hide it only disable it. Given the number of different editors and commands I would have something like 25 disabled commands under a menu as opposed to 4-5 visible ones relevant to the editor in question. So I really need a core expression to control the visibility based on the active part.
Re: e4ActivePart and visible when [message #870320 is a reply to message #865418] Mon, 07 May 2012 16:05 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

so far in Eclipse4 we publish the MPart that's active. If you want to check if your editor part is active, for now you could try writing a property tester that could check

"your.part.id".equals(mpart.getElementId())

PW


Re: e4ActivePart and visible when [message #871030 is a reply to message #870320] Thu, 10 May 2012 14:55 Go to previous message
Phill Perryman is currently offline Phill PerrymanFriend
Messages: 214
Registered: July 2009
Senior Member
Thanks that put me on the right track. I noticed that the receiver in the test method is the current input part.

For anyone else looking I created a property tester

public PartPropertytester() {}

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (receiver instanceof InputPartImpl) {
InputPartImpl imp = (InputPartImpl) receiver;
return imp.getObject() instanceof QuoteEditor;
}
return false;
}

Where QuoteEditor is the class of my editor part.

I added it as a property tester in the manifest
<extension point="org.eclipse.core.expressions.propertyTesters">
<propertyTester class="swb3.PartPropertytester" id="swb3.PartPropertytester" namespace="swb3" properties="part" type="java.lang.Object">
</propertyTester>
</extension>

I then created a definition for use by the menu
<definition id="activepart">
<with variable="e4ActivePart">
<test forcePluginActivation="true" property="swb3.part">
</test>
</with>
</definition>

I then added the menu in the xmi file

<children xsi:type="menu:HandledMenuItem" xmi:id="_lSY_kH5UEeGwE_NvT3bHXA" elementId="handled.InvoiceAddress" label="Invoice Address" command="_uvx2IH5LEeG8BsWZBE4Y6g">
<visibleWhen xsi:type="ui:CoreExpression" xmi:id="_z8SYEIL9EeGHd819fxj36w" coreExpressionId="activepart"/>
</children>

The menu now appears whenever a QuoteEditor is the active part.

I tried injecting an MPart into the property tester but it was always null. Now to make the tester more generic by passing in a class name rather than using an instanceof test.
Previous Topic:Using Factories in Class URI for Parts
Next Topic:Compatibility Layer
Goto Forum:
  


Current Time: Thu Apr 18 20:11:37 GMT 2024

Powered by FUDForum. Page generated in 0.02691 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top