Eclipse RCP menus & actions: Configure or code?

前端 未结 4 1583
栀梦
栀梦 2020-12-28 20:33

This is a general question but my current problem revolves around menu handling.

In a normal plugin with contributes menu actions you would configure ActionSets etc

相关标签:
4条回答
  • 2020-12-28 20:53

    For adding actions in RCP you can also use ApplicationActinBarAcvisor.It is more easy than the above mentioned solution In this u just have to first declare the action as an object of IWorkbenchAction, then in method "protected void makeActions(IWorkbenchWindow window)" u can register it. And the last step is to add it into menu. Following code will help u out.

    1.First declare the action :-

    private IWorkbenchAction newAction
    

    2.Registering action :-

    protected void makeActions(IWorkbenchWindow window) {
    newAction = ActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
        register(newAction);
        newAction.setText("New");
    

    3.Last step is to add the action in menu:-

    MenuManager filemenu = new MenuManager("&File", "file");
        filemenu.add(newAction);
    

    You can also add the action in toolbar as follows:-

    protected void fillCoolBar(ICoolBarManager coolBar) {
        IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
        coolBar.add(toolbar);
        toolbar.add(newAction);
    
    0 讨论(0)
  • 2020-12-28 20:59

    If you are writing the RCP application, the good practice is to create the place holders in your ActionBarAdvisor. While that would define the basic structure of your menus and toolbars, you can extend menuContributions and using commands to contribute the actual menu/tool items.

    0 讨论(0)
  • 2020-12-28 21:07

    My opinion is that the plugin.xml implementation is the way to go.

    My main two reasons for using this method:

    • It's really easy to reconfigure and reorganize the menus and buttons without writing java code.
    • Very clear hierarchical visualization of the menu trees.

    Here is a code snippet that implements menus and submenus. In this example, they are added to the main menu.

    You can paste this into your plugin.xml:

    <extension
             name="Main Menu Contributions"
             point="org.eclipse.ui.menus">
     <menuContribution
            allPopups="false"
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               id="fileMenu"
               label="File">
            <command
                  commandId="org.eclipse.ui.file.exit"
                  label="Exit"
                  style="push">
            </command>
         </menu>
         <menu
               label="Edit">
            <command
                  commandId="org.eclipse.ui.edit.selectAll"
                  label="Select All"
                  style="push">
            </command>
            <menu
                  label="Submenu">
               <command
                     commandId="org.eclipse.ui.edit.selectAll"
                     label="Select All Submenu"
                     style="push">
               </command>
               <command
                     commandId="org.eclipse.ui.edit.delete"
                     label="Delete submenu"
                     style="push">
               </command>
            </menu>
         </menu>
      </menuContribution>
    </extension>
    

    For activating/deactivating a menu, you have to use Core Expressions to enable/disable command handlers. If a command doesn't have any active handlers attached, it will be disabled. So, the menu item that calls that command will also be disabled.

    The following code snippets show how to create a button on the toolbar of a view and have it be enabled/disabled depending of a variable's value. Bare in mind that you will have to change some things in this code to make it work. Most of the changes are for reference names and class implementation.

    Create the button in the toolbar (plugin.xml):

       <extension
             name="View Toolbar Contributions"
             point="org.eclipse.ui.menus">
          <menuContribution
                allPopups="false"
                locationURI="toolbar:myapp.views.MyView">
           <command
                 commandId="myapp.commands.PauseSound"
                 icon=""
                 label="Pause Playback Sound"
                 style="push"
                 tooltip="Pause">
           </command>
         </menuContribution>
    </extension>
    

    Create the command (plugin.xml):

    <extension
             id="myapp.commands.PauseSound"
             name="Pause sound command"
             point="org.eclipse.ui.commands">
          <command
                id="myapp.commands.PauseSound"
                name="Pause Sound">
          </command>
    </extension> 
    

    Create the command handler (plugin.xml):

    <extension
             point="org.eclipse.ui.handlers">
          <handler
                commandId="myapp.commands.PauseSound">
             <activeWhen>
                <with
                      variable="myapp.commands.sourceprovider.active">
                   <or>
                      <equals
                            value="PLAYING">
                      </equals>
                      <equals
                            value="PAUSED">
                      </equals>
                   </or>
                </with>
             </activeWhen>
             <class
                   class="myapp.rcp.commands.toolbar.PausePlayback">
             </class>
          </handler>
    </extension> 
    

    Create the state variable for the command (plugin.xml):

       <extension
             point="org.eclipse.ui.services">
          <sourceProvider
                provider="myapp.commands.sourceprovider.CommandState">
             <variable
                   name="myapp.commands.sourceprovider.active"
                   priorityLevel="workbench">
             </variable>
          </sourceProvider>
       </extension>
    

    Implement the class that changes the variable's state:

    public class CommandState extends AbstractSourceProvider {
        public final static String STATE = "myapp.commands.sourceprovider.active";
        public final static String STOPPED = "STOPPED";
        public final static String PLAYING = "PLAYING";
        public final static String PAUSED = "PAUSED";
        public final static String NOT_LOADED = "NOT_LOADED";
    
    enum State {
            NOT_LOADED, PLAYING, PAUSED, STOPPED
        };
        private State curState = State.NOT_LOADED;
    
        @Override
        public void dispose() {
        }
    
        @Override
        public String[] getProvidedSourceNames() {
            return new String[] { STATE };
        }
    
        // You cannot return NULL
        @SuppressWarnings("unchecked")
        @Override
        public Map getCurrentState() {
            Map map = new HashMap(1);
            if (curState == State.PLAYING)
                map.put(STATE, PLAYING);
            else if (curState == State.STOPPED)
                map.put(STATE, STOPPED);
            else if (curState == State.PAUSED)
                map.put(STATE, PAUSED);
    
            return map;
        }
    
        public void setPlaying() {
            fireSourceChanged(ISources.WORKBENCH, STATE, PLAYING);
        }
    
        public void setPaused() {
            fireSourceChanged(ISources.WORKBENCH, STATE, PAUSED);
        }
    
        public void setStopped() {
            fireSourceChanged(ISources.WORKBENCH, STATE, STOPPED);
        }
    
        public void setNotLoaded() {
            fireSourceChanged(ISources.WORKBENCH, STATE, NOT_LOADED);
        }
    
    }
    

    More details on how to implement these features can be found at these locations:

    • Eclipse Commands Tutorial
    • Limiting Visibility of Commands
    0 讨论(0)
  • 2020-12-28 21:07

    For Eclipse there are two different ways to contributing to the Workbench: Actions and Commands.

    I definitely recommend the Commands as the newer and more advanced than Actions. The drawbacks of Actions as specified here (1):

    • The UI and handling are always tied. There is no way you can separate each other

    • While Actions can be contributed to different parts of the workbench (popup menu/tool bar), all of them were different extension points and so you end up duplicating the XML in multiple places. The worst of it is that not all the extension points expect the same configuration.

    • Specifying Actions in multiple places is a maintenance nightmare. If you have to change the icon of an action, you need to change in all the places.

    • Another issue with duplicating Actions in plugin.xml is that multiple instance of the same Actions will be created in the memory

      (1) Actions vs Commands

    0 讨论(0)
提交回复
热议问题