Eclipse中Action创办的两种方式

Eclipse中Action创建的两种方式

在Eclipse里,是由actions来描述菜单及工具栏的可以通过两种方法向应用程序里添加菜单和工具栏
1.编写代码
如果是第一种方法,利用ApplicationActionBarAdvisor 类的makeActions()声名actions。你可以利用方法fillMenuBar()或者fillCoolBar()向你的程序添加菜单或者工具栏(coolbar)。
示例代码:

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

    // Actions - important to allocate these only in makeActions, and then use them
    // in the fill methods.  This ensures that the actions aren't recreated
    // when fillActionBars is called with FILL_PROXY.
    private IWorkbenchAction exitAction;
    private IWorkbenchAction aboutAction;
    private IWorkbenchAction newWindowAction;
    private OpenViewAction openViewAction;
    private Action messagePopupAction;

    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }
    
    protected void makeActions(final IWorkbenchWindow window) {
        // Creates the actions and registers them.
        // Registering is needed to ensure that key bindings work.
        // The corresponding commands keybindings are defined in the plugin.xml file.
        // Registering also provides automatic disposal of the actions when
        // the window is closed.

        exitAction = ActionFactory.QUIT.create(window);
        register(exitAction);
        
        aboutAction = ActionFactory.ABOUT.create(window);
        register(aboutAction);
        
        newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
        register(newWindowAction);
        
        openViewAction = new OpenViewAction(window, "Open Another Message View", View.ID);
        register(openViewAction);
        
        messagePopupAction = new MessagePopupAction("Open Message", window);
        register(messagePopupAction);
    }
    
    protected void fillMenuBar(IMenuManager menuBar) {
        MenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE);
        MenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP);
        
        menuBar.add(fileMenu);
        // Add a group marker indicating where action set menus will appear.
        menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        menuBar.add(helpMenu);
        
        // File
        fileMenu.add(newWindowAction);
        fileMenu.add(new Separator());
        fileMenu.add(messagePopupAction);
        fileMenu.add(openViewAction);
        fileMenu.add(new Separator());
        fileMenu.add(exitAction);
        
        // Help
        helpMenu.add(aboutAction);
    }
    
    protected void fillCoolBar(ICoolBarManager coolBar) {
        IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
        coolBar.add(new ToolBarContributionItem(toolbar, "main"));   
        toolbar.add(openViewAction);
        toolbar.add(messagePopupAction);
    }
}


2.扩展(Extensions)
如果你用第二种方法,将使用Eclipse向导以扩展点形式创建Actions。
  plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   .............
   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            id="com.javake.rcp.action.first.actionSet3"
            label="label"
            visible="true">
         <menu
               id="com.javake.rcp.action.first.menu1"
               label="sample menu">
            <separator
                  name="separator">
            </separator>
         </menu>
         <action
               class="com.javake.rcp.action.first.Hello"
               id="com.javake.rcp.action.first.Hello"
               label="HelloAction"
               menubarPath="com.javake.rcp.action.first.menu1/separator"
               toolbarPath="separator">
         </action>
      </actionSet>
   </extension>
</plugin>