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