Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Help needed writing a custom expression for visibleWhen of a command menuContribution
Help needed writing a custom expression for visibleWhen of a command menuContribution [message #325887] Fri, 29 February 2008 22:35 Go to next message
Ryan Norris is currently offline Ryan NorrisFriend
Messages: 18
Registered: July 2009
Junior Member
I work on an RCP-based application and I'm trying to use the
org.eclipse.ui.menus extension point to conditionally include a toolbar
item in a view's local toolbar where the toolbar item is contributed
from a different plug-in. I'd like the toolbar item to conditionally
show up, however, I'm having trouble with building an expression to make
this happen. The problem is that I need the condition to evaluate for
the hosting view's input and not a selection in the view. I was
wondering if someone could help me with this problem.

I defined a command and a handler for the action. I then created an
extension to org.eclipse.ui.menus which uses <menuContribution> and
<command>. The location URI for the menuContribution uses the "toolbar"
scheme and my view's id. I'd like to build an expression using
<visibleWhen> to show or hide the toolbar item.

My view takes as input a specific object which contains a reference to a
parent object. The parent object has a type property whose values are
either type1 or type2. I'd like for the toolbar item to only appear
whenever the type of the view input's parent object is type1. I'd like
the expression to get the view's input, then from it get the input's
parent and finally check the type on the parent object.

Can I write an expression where the context is set to either the view id
specified in the menuContribution's location URI, or a specific view id
(which would be set to the same value as the one in the location URI)?
If I could get this, I could maybe use a propertyTester to translate
from the view to its input to its parent object's type. Given a parent
object I already have a propertyTester which can test it's type. I just
can't figure out how to get to the parent object.

I see how to set the context to the activeViewId, however, I want the
expression context set to a specific view since this visibility
expression will be defined for a command contributed to a specific view.
If I used activeViewId, the command would appear or disappear when
unrelated views are activated which wouldn't be good because it's only
hosted in the specific view. Currently the context is based on the
current selection which isn't that useful for this case.

Is there perhaps another way to go about this?

Any help is appreciated.

-Ryan
Re: Help needed writing a custom expression for visibleWhen of a command menuContribution [message #325927 is a reply to message #325887] Mon, 03 March 2008 14:10 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

In a visibleWhen expression, all you have at your disposal are the
source variables. All of the workbench provided variables are listed in
ISources (and you can check out the command core expressions link in my
sig for more description).

That means you need to start from a known variable, in your case
probably your view part.


<visibleWhen>
<with variable="activePart">
<instanceof value="org.example.MyViewPart"/>
<test property="org.example.myViewPart.inputType" value="type1"/>
</with>
</visibleWhen>
[/xml]

The receiver will be your view part, and you'll need to extract the
input and test it there.

There's no way currently to generate a more generic "property like"
access, i.e. with variable="activePart.input"

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: Help needed writing a custom expression for visibleWhen of a command menuContribution [message #325937 is a reply to message #325927] Mon, 03 March 2008 17:56 Go to previous messageGo to next message
Ryan Norris is currently offline Ryan NorrisFriend
Messages: 18
Registered: July 2009
Junior Member
Thanks for the response Paul.

I would like for the visibleWhen to apply to a specific view whenver
it's open or visible, and not only when it's active. Even if my view is
not the active view the toolbar item should still appear in it if the
view's input meets the condition. I tried your suggestion. It worked
except for when the only change is the view's input (activation stays
the same). If I could somehow fake an activation change which results
in a source change notification, I could direct the call from my view's
input change method.

I then tried to programmatically create a source provider and register
it in my view like:

ISourceProvider provider = new MyViewInputSourceProvider(this);
IEvaluationService service =
(IEvaluationService)PlatformUI.getWorkbench().getService(IEv aluationService.class);
service.addSourceProvider(provider);

My source provider would fire a source changed whenever the view's input
changed. I then created a visibleWhen expression using the source name
from my source provider and an expression reference:


<menuContribution locationURI="toolbar:MyViewID">
<command [command properties]>
<visibleWhen checkEnabled="false">
<with variable="myview.input">
<reference definitionId="isFM">
</reference>
</with>
</visibleWhen>
</command>
</menuContribution>
[/xml]

The expression reference is defined in another plug-in and uses adapt
prior to calling test:

[xml]
<definition id="isFM">
<adapt type="fmType">
<test property="cycle.type" value="fm">
</test>
</adapt>
</definition>
[/xml

I discovered that this would only work when my view was the active view
in the workbench. The expressions would not run when my view is not
active because EvaluationAuthority.refsWithSameExpression() would return
quickly because the EvaluationReference for my expression returns false
for isPostingChanges(). It seems to only return true when my view is
active (set from RestrictionListener).

Is programmatically creating and registering my own source permitted?
Is there a way to make sure my expressions evaluate to bypass the
isPostingChanges() check?

Thanks,
-Ryan



Paul Webster wrote:
> In a visibleWhen expression, all you have at your disposal are the
> source variables. All of the workbench provided variables are listed in
> ISources (and you can check out the command core expressions link in my
> sig for more description).
>
> That means you need to start from a known variable, in your case
> probably your view part.
>
>
> <visibleWhen>
> <with variable="activePart">
> <instanceof value="org.example.MyViewPart"/>
> <test property="org.example.myViewPart.inputType" value="type1"/>
> </with>
> </visibleWhen>
> [/xml]
>
> The receiver will be your view part, and you'll need to extract the
> input and test it there.
>
> There's no way currently to generate a more generic "property like"
> access, i.e. with variable="activePart.input"
>
> Later,
> PW
>
>
Re: Help needed writing a custom expression for visibleWhen of a command menuContribution [message #326078 is a reply to message #325937] Fri, 07 March 2008 00:27 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Ryan Norris wrote:
>
> Is programmatically creating and registering my own source permitted? Is
> there a way to make sure my expressions evaluate to bypass the
> isPostingChanges() check?

Actually, registering your own source is not fully supported until 3.4M6
(and even then it's declarative) but that's not what is causing your
isPostingChanges() problem. What you have will cause your visibleWhen
to re-evaluate on your variable change (although not property tester
change). I'll respond on your other post.

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Previous Topic:Associating a command to a global handler
Next Topic:Why does HandlerUtil.getActiveWorkbenchWindow sometimes return null?
Goto Forum:
  


Current Time: Sun Jun 30 13:53:54 GMT 2024

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

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

Back to the top