Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Problems with adding commands to the toolbar.(I add commands to both a menu entry and a toolbar, but the toolbar doesn't work properly.)
Problems with adding commands to the toolbar. [message #856192] Wed, 25 April 2012 13:02 Go to next message
Lorand Szakacs is currently offline Lorand SzakacsFriend
Messages: 1
Registered: April 2012
Junior Member
Hello,

I have 4 commands, for brevity let's give them the ids: c1, c2, c3, c4.

In my plugin I hook up to the following extensions:
 <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:es_commands">
         <dynamic
                class="MenuContribution"
               id="dynamicMenu">
         </dynamic>
      </menuContribution>
      <menuContribution
            allPopups="false"
            class="ToolBarContributionFactory"
            locationURI="toolbar:toolbar.commands">
      </menuContribution>


So basically:

  • ToolBarContributionFactory extends ExtensionContributionFactory;
  • MenuContribution extends CompoundContributionItem
  • (I tried adding </dynamic><dynamic> to the toolbar; it wouldn't even display, hence the difference in approach)

Both classes contribute 4 newly constructed CommandContributionItem objects, associated with the commands mentioned before.

The problem:
The entries in the menu function properly, i.e. they are enabled/disabled as I'd expect according to the isEnabled() method of their respective handlers.

The entries in the toolbar are not updated properly. They start out enabled, even though they shouldn't be (the ones in the menu aren't).

I added a SelectionListener to the ISelectionService in which I force a refresh of the buttons:


@Override
    public void selectionChanged(IWorkbenchPart part, ISelection selection)
    {
      CommandService commandService = (ICommandService) PlatformUI.getWorkbench()
                .getService(ICommandService.class);
      Map<String, IWorkbenchWindow> filter = new HashMap<String, IWorkbenchWindow>();
        filter.put(IServiceScopes.WINDOW_SCOPE, PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow());
        commandService.refreshElements("c1", filter);
      //and so on for all commands
    }


The plugin doesn't seem to load before I click the CommandsMenu. After that, if I click on an item in the treeviewer the buttons are displayed properly.

Another weird thing is that once I add bindings to my commands (Ctrl+1, ...; with "org.eclipse.ui.contexts.window" as a context), the toolbar items are always enabled, the ones in the menu still work properly.

I would greatly appreciate an elegant solution to the problem, not only a quick hack to my approach.(I'm also fairly new to RCP and I'd like to learn new stuff). Preferably without actions.

And yes, I am sure that I associated the same commands to the items.

Failed approaches:

  • Tried adding actionSets to the Menu and toolbar. But had trouble with:
    public class ToolbarActionDelegate implements IWorkbenchWindowActionDelegate
    {
        @Override
        public void run(IAction action)
        {
            //values always are "c1", "c2", etc...
            String actionDefinitionId = action.getActionDefinitionId();
            CommandUtil.executeCommand(actionDefinitionId);
        }
    
        @Override
        public void selectionChanged(IAction action, ISelection selection)
        {
            String commandID = action.getActionDefinitionId();
            ICommandService cs = (ICommandService) PlatformUI.getWorkbench()
                    .getService(ICommandService.class);
            Command command = cs.getCommand(commandID);
            boolean result = command.isEnabled();
            action.setEnabled(result);
        }
    
    }
    
    Once the action set is added the relation between "cx" commands and their handlers got obscured and the isEnabled() or execute() methods would never run.(I put breakpoints and logging to check it);
  • adding the items to the toolbar individually instead of using an ExtensionContributionFactory exhibited the same problem as described above.




Thank you for your help!
Re: Problems with adding commands to the toolbar. [message #857341 is a reply to message #856192] Thu, 26 April 2012 13:19 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

your handler controls the enabled state of the commands. When that state changes, the handler needs to fire an event. Using org.eclipse.core.commands.AbstractHandler.setBaseEnabled(boolean) will do it correctly for you.

The reason the menu seems to work is because the SWT.Show event kicks of enabled state reconciliation.

PW


Previous Topic:Modal dialogs randomly not working on OS X 10.7.3
Next Topic:Save File on Crash
Goto Forum:
  


Current Time: Thu Apr 25 14:14:00 GMT 2024

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

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

Back to the top