----------------------------------------------------------------------------------------------
学习:http://www.blogjava.net/youxia/archive/2006/11/20/82388.html
----------------------------------------------------------------------------------------------
1. 以编辑 ApplicationActionBarAdvisor.java 这个类的方式进行:
1) 编写菜单action类:
package hellorcp.action;
import hellorcp.dialog.FirstDialog;
import org.eclipse.jface.action.Action;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
public class FirstAction extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
public FirstAction(IWorkbenchWindow window) {
if (window == null) {
throw new IllegalArgumentException();
}
this.workbenchWindow = window;
}
public void run() {
// make sure action is not disposed
if (workbenchWindow != null) {
// 在这里添加功能
FirstDialog dg = new FirstDialog(workbenchWindow.getShell());
dg.open();
}
}
public void dispose() {
workbenchWindow = null;
}
}
2) 该菜单的动作为打开对话框,故还需编写对话框类:
package hellorcp.dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FirstDialog extends Dialog {
protected Shell shell;
private int result;
public FirstDialog(Shell parent, int style) {
super(parent, style);
}
public FirstDialog(Shell parent) {
this(parent, SWT.NONE);
}
public int open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(150, 70);
shell.setText(" 第一个对话框 ");
final Button okButton = new Button(shell, SWT.NONE);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
result = 1;
shell.dispose();
}
});
okButton.setText(" OK ");
okButton.setBounds(10, 10, 48, 22);
final Button cancelButton = new Button(shell, SWT.NONE);
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
result = 2;
shell.dispose();
}
});
cancelButton.setText(" Cancel ");
cancelButton.setBounds(89, 10, 48, 22);
}
}
3) 编辑方法makeActions, 注册菜单action; 然后编辑方法 fillMenuBar 画出菜单:
package hellorcp;
import hellorcp.action.FirstAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
private IWorkbenchAction action1;
private IWorkbenchAction action2;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
protected void makeActions(IWorkbenchWindow window) {
action1 = new FirstAction(window);
action1.setText("FirstMenu1");
action1.setId(action1.getClass().getName());
register(action1);
action2 = new FirstAction(window);
action2.setText("FirstMenu2");
action2.setId(action2.getClass().getName());
register(action2);
}
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager newMenu = new MenuManager("第一个菜单", "hellorcp.firstmenu1");
menuBar.add(newMenu);
newMenu.add(action1);
MenuManager newMenu2 = new MenuManager("第一个菜单", "hellorcp.firstmenu2");
menuBar.add(newMenu2);
newMenu.add(action2);
}
}
4) 运行效果如下图:
2. 以编辑 plugin.xml 的方式添加菜单:
1) 编写菜单action类:
package hellorcp.action;
import hellorcp.dialog.FirstDialog;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
public class Action2 implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
public void dispose() {
// TODO
}
public void init(IWorkbenchWindow window) {
// TODO
this.window = window;
}
public void run(IAction action) {
// TODO
FirstDialog dg = new FirstDialog(window.getShell());
dg.open();
}
public void selectionChanged(IAction action, ISelection selection) {
// TODO
}
}
2) 双击 plugin.xml 打开 Extensions 编辑窗口,点击 "All Extensions" 下的 "Add..." 打开 Extension 选择窗口,并选择 org.eclipse.ui.actionSets, 如下图:
3) 回到 Extensions 编辑界面,选中"org.eclipse.ui.actionSets",并在右侧 "Extension Details" 的 "ID"、"Name" 中输入内容,保存,如下图:
4) 点击 "plugin.xml" 选项卡,可以看到该文件中出现了如下代码:
<extension id="hellorcp.actionset" name="菜单" point="org.eclipse.ui.actionSets"> </extension>
直接将该片段编辑为如下内容:
<extension id="hellorcp.actionset" name="菜单" point="org.eclipse.ui.actionSets"> <actionSet description ="第一个扩展" id ="RCP_Start.actionSet1" label ="RCP_Start.actionSet1" visible ="true"> <action class ="hellorcp.action.Action2" icon ="icons/alt_window_16.gif" id ="RCP_Start.action2" label ="第二个菜单项" menubarPath ="hellorcp.firstmenu/additions" style ="push" toolbarPath ="additions" tooltip ="第二个菜单项的按钮" /> </actionSet> </extension>
5) 再次切换到 "Extensions" 选项卡,可以看到相应内容也出现了,如下图:
6) 运行程序,效果如下图:
注:需要将类 ApplicationWorkbenchWindowAdvisor 方法 preWindowOpen() 中的 configurer.setShowCoolBar(false); 改为 configurer.setShowCoolBar(true); 才会出现该图标菜单。