[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: Synchronizing toggle state of Toolbar- and MenuItems

vlr wrote:
Hi,

in my plugin.xml I have a certain command extension (org.eclipse.ui.commands) that is linked into two menuContributions: toolbar and menu (see below). Both menuContributions are of type 'toggle'.

How can I manage to keep the toggle state of these two items in sync (e.g. if the user toggles the state via the menu entry, the toolbar button must also change its state)?

This is done by having your handler implement IElementUpdater and adding keeping the checked state in sync. See some of the examples in http://wiki.eclipse.org/Menu_Contributions#Example_Matrix



Usually I would have expected such things should be done by the toolkit and not by the programmer...

There is some deficiencies in the check box/radio button part of commands (basically, we're having a hard time defining a model that all implementors would need to use, so for now you must implement your own model). Depending on the current state of the SWT widget itself is the opposite of MVC, and so would couldn't go that route.



Excerpt from plugin.xml:
...
   <extension
         point="org.eclipse.core.expressions.definitions">
      <definition
            id="EditorIsOpen">
         <with
               variable="activeEditor">
            <instanceof
                  value="AbstractMyObjectEditor">
            </instanceof>
         </with>
      </definition>
   </extension>
   <extension
         point="org.eclipse.ui.commands">
      <command
            id="EnableMaskCommand"
            name="enable/disable Mask">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="toolbar:mainToolbar">
         <command
               commandId="EnableMaskCommand"
               icon="icons/edit.png"
               style="toggle">
            <visibleWhen
                  checkEnabled="true">
               <reference
                     definitionId="EditorIsOpen">
               </reference>
            </visibleWhen>
         </command>
      </menuContribution>
      <menuContribution
            locationURI="menu:edit">
         <command
               commandId="EnableMaskCommand"
               label="enable/disable Mask"
               style="toggle">
            <visibleWhen
                  checkEnabled="true">
               <reference
                     definitionId="EditorIsOpen">
               </reference>
            </visibleWhen>
         </command>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="EnableMaskHandler"
            commandId="EnableMaskCommand">
      </handler>
   </extension>


using visibleWhen with checkeEnabled means you 1) cannot then specify any other visibleWhen clause (since it won't matter) and 2) it depends on your handler's enabledWhen (which you don't provide) or your handler's enabled state (be sure to fire HandlerEvents on state change).

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/ganymede/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench.htm