《Eclipse.Rich.Client.Platform》chapter 6 Adding Actions

6.1 Adding to the Menus and Toolbar

In most applications the menu and toolbar play a supporting role for the main content area.

The general pattern for making the toobar, status line, or menu bar available is to configure the window before it is opened.

This is done in ApplicationWorkbenchWindowAdvisor.preWindowOpen() by adding a call to methods such as setShowMenuBar(boolean), as shown here:


public void preWindowOpen(){

     IWorkbenchWindowConfigurer configurer = getWindowConfigurer();

    configurer.setInitialSize(new Point(250,350));

    configurer.setShowMenuBar(true);

    ....

}

The preWindowOpen() method is called before the window's controls have been created,and it's the primary place for you to control which workbenchwindow parts are visible.


6.1.1 Create a Top-Level Menu

Notice that ActionBarAdvisor.makeActions() is called before the workbenchWindow's controls are created in createWindowContent().

The steps for creating actions,makeActions(),and placing actions,fil"(),are separated.

This allows you to create the actions just once and then place the same action objects in several locations,for example,in both the toolbar and a menu,

org.eclipsercp.hyperbola/ApplicationActionBarAdvisor

public class ApplicationActionBarAdvisor ectends ActionBarAdvisor{

  public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }

    protected void makeActions(IWorkbenchWindow window) {
    //chapter 6
    exitAction = ActionFactory.QUIT.create(window);
    register(exitAction);
    aboutAction = ActionFactory.ABOUT.create(window);
    register(aboutAction);

    }

    protected void fillMenuBar(IMenuManager menuBar) {
    //chapter 6
    MenuManager hyperbolaMenu = new MenuManager("&Hyperbola","hyperbola");
    hyperbolaMenu.add(exitAction);
         
    MenuManager helpMenu = new MenuManager("&Help","help");
    helpMenu.add(aboutAction);
    menuBar.add(hyperbolaMenu);
    menuBar.add(helpMenu);
   
    }

}

makeAction() creates each action and saves it in a field. Each action is also registered. Registering actions ensures that they are deleted when the related Workbench window is closed - a very important characteristic . Registering actions also enables key bindings,as discussed in Chapter 12,"Adding Key Bindings."

6.1.2 Menu Managers

此处代码修改完之后报错,稍后再弄一下


6.1.3 The Add Contact Action

The Exit and About actions are generic, so we used predefined actions. Of course,Hyperbola also needs some actions specific to instant messaging.

1.Implement an action that adds a contact to the contacts list

      org.eclipsercp.hyperbola/AddContactAction

    public class AddContactAction extends Action implements ISelectionListener,ActionFactory.IWorkbenchAction{

          private final IWorkbenchWindow window;

         public final static String ID= "org.eclipsercp.hyperbola.addContact";

         private IStructuredSelection selection;


       public AddContactAction(IWorikbencheWindow window){

        this.window = window;

setId(ID);

setText("&Add Contact...");

               setToolTipText("Add a contact to your contacts list.");

              setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin("org.eclipsercp.hyperbola", IImageKeys.ADD_CONTACT));

             window.getSelectionService().addSelectionListener(this);

     }

    public void dispose(){

window.getSelectionService().removeSelectionListener(this);

     }

}

2.Ensure that the action is enabled only if a contact group is selected in the Contacts view


3.Add the action to the top-level Hperbola toolbar



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值