Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Menu items added by <dynamic> provider cannot be disabled(When I try to disable menu items added by a dynamic provider, instead of being "greyed" out, they are never visible. )
Menu items added by <dynamic> provider cannot be disabled [message #654265] Mon, 14 February 2011 19:52 Go to next message
Max Mising name is currently offline Max Mising nameFriend
Messages: 54
Registered: September 2010
Member
I am adding menu items using the <dynamic> tag under <menu>. My dynamic class extends CompoundContributionItem and creates a dozen CommandContributionItems in the getContributionItems() function. Depending on state, some of these items created should be greyed out. I have tried multiple ways of doing this, and each way instead of becoming greyed out, they just don't show up in my menu.

Here are the ways I have tried:

1. Overriding methods on CommandContributionItem:
CommandContributionItem item = new CommandContributionItem(...) {
 @Override
                public boolean isEnabled() {
                       return customEnabled;
                }
}


2. When that caused the item to not show up in the menu, I tried this as well:
CommandContributionItem item = new CommandContributionItem(...) {
               @Override
                public boolean isEnabled() {
                       return customEnabled;
                }

               @Override
                public boolean isVisible() {
                    return true;
                }
}


3. That still didn't work, so I tried out using a different command for the items that shouldn't be enabled. My different command's handler was simple:
public class DisabledHandler extends AbstractHandler {

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
     * ExecutionEvent)
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        return null;
    }

    @Override
    public void setEnabled(Object evaluationContext) {
        setBaseEnabled(false);
    }

    @Override
    public boolean isEnabled() {
        return false;
    }
    
}


That still just caused the items to not be visible. I'm all out of things to try. Any ideas?

Thanks,

Max
Re: Menu items added by <dynamic> provider cannot be disabled [message #654371 is a reply to message #654265] Tue, 15 February 2011 12:51 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 02/14/2011 02:52 PM, Max wrote:
>
> 3. That still didn't work, so I tried out using a different command for
> the items that shouldn't be enabled. My different command's handler was
> simple:
>
> public class DisabledHandler extends AbstractHandler {
>

provide a "disabled" handler how? Commands have handlers, and the
handlers are responsible for the enabled state. The active handler for
that command (and there can only be one handler active for a command at
a given time) determines the state by calling setBaseEnabled(*) so that
state change events are generated. The state applies to everywhere that
Command is used.


I created 2 commands, one with an enabled handler and one with a
disabled handler. Then I used "dynamic" to add CCIs to a submenu:

protected IContributionItem[] getContributionItems() {
CommandContributionItemParameter p1 = new CommandContributionItemParameter(
locator, "one", "z.ex.cmpd.en",
CommandContributionItem.STYLE_PUSH);
CommandContributionItemParameter p2 = new CommandContributionItemParameter(
locator, "two", "z.ex.cmpd.dis",
CommandContributionItem.STYLE_PUSH);
CommandContributionItemParameter p3 = new CommandContributionItemParameter(
locator, "three", "z.ex.cmpd.en",
CommandContributionItem.STYLE_PUSH);
CommandContributionItemParameter p4 = new CommandContributionItemParameter(
locator, "four", "z.ex.cmpd.dis",
CommandContributionItem.STYLE_PUSH);
CommandContributionItemParameter p5 = new CommandContributionItemParameter(
locator, "five", "z.ex.cmpd.en",
CommandContributionItem.STYLE_PUSH);
IContributionItem[] items = new IContributionItem[] {
new CommandContributionItem(p1),
new CommandContributionItem(p2),
new CommandContributionItem(p3),
new CommandContributionItem(p4),
new CommandContributionItem(p5), };
return items;
}

This worked fine, and the z.ex.cmpd.dis menu entries were disabled. Is
there anything in your error log? Or barring that, step through an
update(String) of one of your disabled CommandContributionItem.

PW


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


Re: Menu items added by <dynamic> provider cannot be disabled [message #654430 is a reply to message #654371] Tue, 15 February 2011 18:04 Go to previous messageGo to next message
Max Mising name is currently offline Max Mising nameFriend
Messages: 54
Registered: September 2010
Member
Yes, I had two commands that I was using. Each one with the appropriate handler. If I wanted my menu item to be disabled, it would use my disabled commandId. That was still not working for me, instead of being disabled they would not show up. I ended up giving up on CommandContributionItem since I don't really like to hardcode commandIds in my java so I instead added ActionContributionItems and set those to disabled and they showed up grey.

Thanks for the response

Max
Re: Menu items added by <dynamic> provider cannot be disabled [message #654445 is a reply to message #654430] Tue, 15 February 2011 19:52 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 02/15/2011 01:04 PM, Max wrote:
> Yes, I had two commands that I was using. Each one with the appropriate
> handler. If I wanted my menu item to be disabled, it would use my
> disabled commandId. That was still not working for me, instead of being
> disabled they would not show up. I ended up giving up on
> CommandContributionItem since I don't really like to hardcode commandIds
> in my java so I instead added ActionContributionItems and set those to
> disabled and they showed up grey.

There's nothing wrong withe using ActionContributionItems ... but
they're considered part of the legacy system for a reason. They're one
step up from doing new MenuItem(menu).setText("MyItem"), and are
excellent if that's what is needed.

For anything beyond that commands are used.

PW

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


Previous Topic:glue code for linking two classes?
Next Topic:weird junit testing
Goto Forum:
  


Current Time: Fri Mar 29 14:59:16 GMT 2024

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

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

Back to the top