Top-level Menu(1)在 ApplicationWorkbenchWindowAdvisor#preWindowOpen() 方法中启用菜单:
configurer.setShowMenuBar(true);
此方法调用并不一定显示菜单,只有当菜单中包含菜单项时,菜单才会显示。
(2)创建并添加菜单项
org.eclipsercp.hyperbola/ApplicationActionBarAdvisor
private IWorkbenchAction exitAction;
private IWorkbenchAction aboutAction;
private AddContactAction addContactAction;
protected
void makeActions(IWorkbenchWindow window) {
exitAction = ActionFactory.QUIT.create(window);
register(exitAction);
aboutAction = ActionFactory.ABOUT.create(window);
register(aboutAction);
addContactAction =
new AddContactAction(window);
register(addContactAction);
}
protected
void fillMenuBar(IMenuManager menuBar) {
MenuManager hyperbolaMenu =
new MenuManager(
"&Hyperbola",
"hyperbola");
hyperbolaMenu.add(addContactAction);
hyperbolaMenu.add(
new Separator());
hyperbolaMenu.add(exitAction);
MenuManager helpMenu =
new MenuManager(
"&Help",
"help");
helpMenu.add(aboutAction);
menuBar.add(hyperbolaMenu);
menuBar.add(helpMenu);
}
退出和关于菜单使用 org.eclipse.iu.actions.ActionFactory 中预设的 action。
通过 register 方法注册一个action,这样在 workbench window 退出后可以销毁这些资源。
MenuManager.add() 方法可将 action 添加到菜单项,或者将其他 MenuManager 添加到子菜单。
(3)创建自定义菜单项:Action
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(IWorkbenchWindow 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);
}
public
void selectionChanged(IWorkbenchPart part, ISelection incoming) {
// Selection containing elements
if (incoming
instanceof IStructuredSelection) {
selection = (IStructuredSelection) incoming;
setEnabled(selection.size() == 1
&& selection.getFirstElement()
instanceof ContactsGroup);
}
else {
// Other selections, for example containing text or of other kinds.
setEnabled(
false);
}
}
public
void run() {
AddContactDialog d =
new AddContactDialog(window.getShell());
int code = d.open();
if (code == Window.OK) {
Object item = selection.getFirstElement();
ContactsGroup group = (ContactsGroup) item;
ContactsEntry entry =
new ContactsEntry(group, d.getUserId(), d
.getNickname(), d.getServer());
group.addEntry(entry);
}
}
}
构造函数中注册了一个 ISelectionListener 到 window.getSelectionService() 中,这样当 window 发生 selection change 事件时,此 Action 就会收到相应的事件通知,并调用此类的 selectionChanged 方法。这个方法首先判断发生事件的是不是 IStructuredSelection 对象(用于区分 editor 中选择文字所触发此事件的情况),IStructuredSelection 接口的继承关系如下图:
window 本身并不产生 selection 事件,而 editors 和 views 可以作为 selection provider 注册到 window 上,将其产生的 selection 事件通知给 window.getSelectionService() 上的 ISelectionListener (本例是 实现了 ISelectionListener 接口的 AddContactAction)。ContactsView 在 createPartControl 方法中调用 getSite().setSelectionProvider(treeViewer) 方法将自己注册为selection provider 。
注意:此 Action 还实现了 ActionFactory.IWorkbenchAction 接口,并在 dispose 接口方法中从window.getSelectionService() 删除了 ISelectionListener 来释放资源,避免内存泄露