Eclipse RCP menus & actions: Configure or code?

前端 未结 4 1590
栀梦
栀梦 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 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:

    
     
         
            
            
         
         
            
            
            
               
               
               
               
            
         
      
    
    

    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):

       
          
           
           
         
    
    

    Create the command (plugin.xml):

    
          
          
     
    

    Create the command handler (plugin.xml):

    
          
             
                
                   
                      
                      
                      
                      
                   
                
             
             
             
          
     
    

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

       
          
             
             
          
       
    

    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

提交回复
热议问题