Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Problems with command expressions and enableWhen, activeWhen
Problems with command expressions and enableWhen, activeWhen [message #495812] Fri, 06 November 2009 09:07 Go to next message
Bjoern Berg is currently offline Bjoern BergFriend
Messages: 47
Registered: November 2009
Location: Essen
Member
Hi @ll,

first of all I think I have a problem using enableWhen and activeWhen combined with commands. Maybe someone can give me a reference where I find more information when to use which of the expressions.

The concrete problem is: I have created a view which works perfectly. Now I have added a command and a key binding. The command should be always executed when ENTER is pressed.
Now I have the problem that always ENTER is pressed and the view is not active the command is executed. So I tried it with enableWhen and activeWhen that the command is only activated if the viewPart is in focus or active:

The code in XML looks as follows:

<handler
commandId="de.genesys_e.trace.ui.command.analyze">
<enabledWhen>
<with
variable="activePart">
<iterate>
<instanceof
value="de.genesys_e.trace.ui.views.AnalyzerView">
</instanceof>
</iterate>
</with>
</enabledWhen>
<activeWhen>
<with
variable="activePart">
<iterate>
<instanceof
value="de.genesys_e.trace.ui.views.AnalyzerView">
</instanceof>
</iterate>
</with>
</activeWhen>
</handler>

But it does not work. So, maybe someone can tell me what's wrong?!

Bjoern
Re: Problems with command expressions and enableWhen, activeWhen [message #495815 is a reply to message #495812] Fri, 06 November 2009 09:17 Go to previous messageGo to next message
T. Wilhelm is currently offline T. WilhelmFriend
Messages: 129
Registered: July 2009
Senior Member
Hi,

Dont use activePart and instanceof. Try to use activePartId and compare it (equals) my.view.id

i.e.:
<handler
commandId="de.genesys_e.trace.ui.command.analyze">
    <enabledWhen>
    <with
        variable="activePartId">
    <equals>
    value="de.genesys_e.trace.ui.views.AnalyzerView"> <- insert your view id here
    </equals>
    </with>
    </enabledWhen>
</handler>

[Updated on: Fri, 06 November 2009 09:19]

Report message to a moderator

Re: Problems with command expressions and enableWhen, activeWhen [message #495827 is a reply to message #495815] Fri, 06 November 2009 10:17 Go to previous messageGo to next message
Bjoern Berg is currently offline Bjoern BergFriend
Messages: 47
Registered: November 2009
Location: Essen
Member
Thank you for the fast response. I have tested it, but it does not work. The command ist still active when the view is not in the foreground and it is also active when the view is closed.

That's confusing...
Re: Problems with command expressions and enableWhen, activeWhen [message #495828 is a reply to message #495827] Fri, 06 November 2009 10:25 Go to previous messageGo to next message
T. Wilhelm is currently offline T. WilhelmFriend
Messages: 129
Registered: July 2009
Senior Member
Be careful, you have to do that for all F5 Handlers. Did you check which handler will be executed when your view is closed? Is it still the one where you added the activeWhen and EnabledWhen expression?

The problem is often that a menu entry won´t be grayed out because there is a handler which is always active (because an enabledWhen and activeWhen expression was not declared).

Like mentioned before make a System.out.println() in your execute Method and check which handler will be executed if the view has no Focus or was already closed.

Greetz
Thomas
Re: Problems with command expressions and enableWhen, activeWhen [message #495829 is a reply to message #495828] Fri, 06 November 2009 10:42 Go to previous messageGo to next message
Bjoern Berg is currently offline Bjoern BergFriend
Messages: 47
Registered: November 2009
Location: Essen
Member
It is the same Handler, I have defined under org.eclipse.ui.handlers:

<handler          class="de.genesys_e.trace.ui.commands.AnalyzeHandler"
   commandId="de.genesys_e.trace.ui.command.analyze">
         <enabledWhen>
            <with
                  variable="activePartId">
               <equals
                     value="de.genesys_e.trace.views.analyzer">
               </equals>
            </with>
         </enabledWhen>
         <activeWhen>
            <with
                  variable="activePartId">
               <equals
                     value="de.genesys_e.trace.views.analyzer">
               </equals>
            </with>
         </activeWhen>
      </handler>


And if the view is closed the Handler is still executed. Is this a problem of extending AbstractHandler and not overriding isEnabled...?
Re: Problems with command expressions and enableWhen, activeWhen [message #495851 is a reply to message #495829] Fri, 06 November 2009 12:01 Go to previous messageGo to next message
T. Wilhelm is currently offline T. WilhelmFriend
Messages: 129
Registered: July 2009
Senior Member
hey,

no its definitely not a problem of not overwriting isEnabled(). I just the same mechanism and it works perfectely.

Maybe there is something wrong with your keybinding? How do you realize that the handler will be executed when ENTER is pressed?

Re: Problems with command expressions and enableWhen, activeWhen [message #495865 is a reply to message #495851] Fri, 06 November 2009 12:55 Go to previous messageGo to next message
Bjoern Berg is currently offline Bjoern BergFriend
Messages: 47
Registered: November 2009
Location: Essen
Member
Therefore I used org.eclipse.ui.bindings:
<extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="de.genesys_e.trace.ui.command.analyze"
            contextId="org.eclipse.ui.contexts.window"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="CR">
      </key>
</extension>


Is it a problem with contextId?
Re: Problems with command expressions and enableWhen, activeWhen [message #495868 is a reply to message #495865] Fri, 06 November 2009 13:15 Go to previous messageGo to next message
T. Wilhelm is currently offline T. WilhelmFriend
Messages: 129
Registered: July 2009
Senior Member
Hmm no i have same contextId and for me it works... but maybe its because of the CR. Did you try it with something else, i.e. F5 ?

I´m not sure but CR is very special and i wouldn´t be surprised if something going wrong there...

If this also doesnt work, the scheme id could also be the problem. We defined our own scheme id, so maybe try that.
Greetz
Thomas

Re: Problems with command expressions and enableWhen, activeWhen [message #495937 is a reply to message #495812] Fri, 06 November 2009 17:44 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Where is your command definition, from org.eclipse.ui.commands?

If you had a defaultHandler, for example, that would always be active.

PW


Re: Problems with command expressions and enableWhen, activeWhen [message #496171 is a reply to message #495937] Mon, 09 November 2009 09:25 Go to previous messageGo to next message
Bjoern Berg is currently offline Bjoern BergFriend
Messages: 47
Registered: November 2009
Location: Essen
Member
Sorry for the late reply but it was weekend and I had no access to the code. In my command definition I do not use a defaultHandler now. I had defined a defaultHandler for each command wich was my own Handler class. Very silly...

So, to sum it up:

1. I have to define my Handler below org.eclipse.ui.handlers with a class and an ID.

2. Then I define my command. If I need a defaultHandler which is always active, I define it in the command using the field defaultHandler.

3. After all I define my key bindings.

If I don't use a defaultHandler I can use activeWhen and enabledWhen to manipulate the state of a command (e.g. grayed out)?!

Am I right so far?

Björn
Re: Problems with command expressions and enableWhen, activeWhen [message #496843 is a reply to message #496171] Wed, 11 November 2009 14:30 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Bjoern Berg wrote on Mon, 09 November 2009 04:25


If I don't use a defaultHandler I can use activeWhen and enabledWhen to manipulate the state of a command (e.g. grayed out)?!

Björn


That's mostly it. Just a quick note on activeWhen and enabledWhen.

A command can only have one active handler at a time. That's where activeWhen comes in. Those expressions are evaluated on all handlers for a command (like copy) and the output is a 2 or 3 handlers that can be active. Then they are prioritized (based on a system like the numbers in org.eclipse.ui.ISources). Then one is picked.

That handler can then be enabled or disabled, and that determines the state of the command.

If no handler is picked (because none were candidates or because 2 or more had the same, highest priority) then the command is disabled.

So to recap: Use activeWhen to determine when your handler should be the behaviour for a command. Then use enabledWhen to determine if it is enabled/disabled.

PW


Re: Problems with command expressions and enableWhen, activeWhen [message #779829 is a reply to message #496843] Mon, 16 January 2012 16:28 Go to previous message
Mark Walters is currently offline Mark WaltersFriend
Messages: 25
Registered: June 2011
Junior Member
Hi,
thanks for this post - helped me define a tricky context menu enablement.

Ta,
Mark.
Previous Topic:How to access configuration directory
Next Topic:Click a menu button to display the drop down list
Goto Forum:
  


Current Time: Fri Mar 29 01:46:09 GMT 2024

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

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

Back to the top