一. 菜单栏,工具栏,状态栏中的操作可以共享一个Action。
二. actionSets创建有两种方式 1.扩展(plugin,xml)2.编程(ActionBarAdvisor.java)
1. actionSets可以包括:菜单栏,工具栏,状态栏
2. plugin.xml中通过:action中属性menubarPath和toolbarPath区分
程序中:覆盖父类方法:fillMenuBar,fillCollBar(这里主要讲诉这种方法)
3.Action编程的步骤:1)创建具体的action,需继承Action类,实现run()方法
2)注册action
三. 菜单栏所涉及到的问题:
1. 菜单栏中的菜单
2. 菜单中的菜单项
3. 菜单项的样式
4. 二级菜单
5. 图标
6. 动态设置不可用
四. 工具栏所涉及到的问题:
1. 工具栏分组
2. 工具栏样式
3. 工具栏中的下拉菜单
4. 图标
5. 动态设置不可用
五. 代码实现:
package rcptest;
import java.net.URL;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.Util;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
import org.eclipse.ui.actions.ActionFactory;
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 NewAction new1;
private IWorkbenchAction exitAction;
private IWorkbenchAction helpopen;
private IWorkbenchAction refOpen;
private PullDownAction pd;
public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
super(configurer);
}
//创建并注册action
protected void makeActions(IWorkbenchWindow window) {
new1= new NewAction();
register(new1);
exitAction = ActionFactory.QUIT.create(window);
register(exitAction);
//帮助菜单,需要增加帮助扩展点
helpopen = ActionFactory.HELP_CONTENTS.create(window);
register(helpopen);
//首选项菜单,需要增加首选项扩展点
refOpen = ActionFactory.PREFERENCES.create(window);
register(refOpen);
//设置不可用
exitAction.setEnabled(false);
}
//创建菜单,菜单项
protected void fillMenuBar(IMenuManager menuBar) {
MenuManager code = new MenuManager("code(&c)");
code.add(new1);
code.add(exitAction);
code.add(helpopen);
code.add(refOpen);
//增加到菜单栏
menuBar.add(code);
MenuManager help = new MenuManager("help(&h)");
//添加二级菜单
MenuManager second = new MenuManager("second");
second.add(new1);
pd = new PullDownAction();
help.add(second);
help.add(new1);
//添加分割线
help.add(new Separator());
help.add(pd);
help.add(helpopen);
menuBar.add(help);
}
//创建工具栏,需在ApplicationWorkbenchWindowAdvisor中设置setShowCoolBar(true);
protected void fillCoolBar(ICoolBarManager coolBar){
IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
toolbar.add(new1);
toolbar.add(new Separator());
toolbar.add(exitAction);
coolBar.add(toolbar);
IToolBarManager toolbar2 = new ToolBarManager(coolBar.getStyle());
pd = new PullDownAction();
//设置工具栏下拉菜单
pd.setMenuCreator(new TestMenuCreator());
toolbar2.add(new1);
toolbar2.add(pd);
toolbar2.add(new PullDownBar());
toolbar2.add(exitAction);
coolBar.add(toolbar2);
}
//状态栏, 需在ApplicationWorkbenchWindowAdvisor中设置setShowStatusLine(true);
@Override
protected void fillStatusLine(IStatusLineManager statusLine) {
// TODO Auto-generated method stub
super.fillStatusLine(statusLine);
statusLine.add(new1);
}
class NewAction extends Action{
NewAction(){
//设置Action的样式
super("",Action.AS_PUSH_BUTTON);
//设置label
this.setText("new(&n)");
//设置ID,便于管理
this.setId("NewAction");
//设置提示
this.setToolTipText("testnew");
//添加图标,菜单栏、工具栏、状态栏共用
//使用绝对路径
//ImageData data = new ImageData("D:\\workspace\\RCPTest\\icons\\alt_window_16.gif");
//ImageDescriptor imageData = ImageDescriptor.createFromImageData( data );
//this.setImageDescriptor(imageData);
//使用相对路径
// try{
// URL u = new URL(Activator.getDefault().getBundle().getEntry("/"),"icons/sample.gif");
// this.setImageDescriptor(ImageDescriptor.createFromURL(u));
// }catch(Exception e){
//
// }
}
public void run(){
//动态设置不可用
//得到MenuBar上的菜单,菜单索引从0开始
MenuItem menu = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell().getMenuBar().getItem(1);
//得到菜单中的菜单项
MenuItem item = menu.getMenu().getItem(3);
//菜单项的具体设置
item.setEnabled(false);
System.out.println( menu.getText());
System.out.println( menu.getMenu().getItem(3).getText());
}
}
class PullDownAction extends Action{
PullDownAction(){
super("",Action.AS_DROP_DOWN_MENU);
this.setText("PullDown(&P)");
this.setId("PullDownAction");
this.setToolTipText("PullDownAction");
}
public void run(){
System.out.println("testtest");
}
}
//为工具栏中的选项增加下拉菜单
class TestMenuCreator implements IMenuCreator{
private MenuManager dropDownMenuMgr;
public Menu getMenu(Menu parent) {//在菜单栏被调用
return null;
}
public Menu getMenu(Control parent) {//在工具栏被 调用
createDropDownMenuMgr();
return dropDownMenuMgr.createContextMenu(parent);
}
public void dispose() {
if(null != dropDownMenuMgr){
dropDownMenuMgr.dispose();
dropDownMenuMgr = null;
}
}
private void createDropDownMenuMgr() {
if (dropDownMenuMgr == null) {
dropDownMenuMgr = new MenuManager();
dropDownMenuMgr.add(new InnerAction("One"));
dropDownMenuMgr.add(new InnerAction("Two"));
}
}
}
class InnerAction extends Action{
private String text;
InnerAction(String text){
super(text);
this.text = text;
}
@Override
public void run() {
super.run();
if("One".equals(text)){
System.out.println("click one");
}else{
//do two something
}
}
}
//工具栏下拉菜单实现
class PullDownBar extends Action implements IMenuCreator{
private MenuManager dropDownMenuMgr;
PullDownBar(){
super("",Action.AS_DROP_DOWN_MENU);
this.setId("PullDownBar");
this.setText("PullDownBar");
this.setToolTipText("pulldownbar");
setMenuCreator(this);
}
public void run(){
System.out.println("pulldownbar");
}
public void dispose() {
if(null != dropDownMenuMgr){
dropDownMenuMgr.dispose();
dropDownMenuMgr = null;
}
}
//parent为工具栏时调用
public Menu getMenu(Control parent) {
createDropDownMenuMgr();
return dropDownMenuMgr.createContextMenu(parent);
}
///parent为菜单栏时调用
public Menu getMenu(Menu parent) {
createDropDownMenuMgr();
Menu menu = new Menu(parent);
IContributionItem[] items = dropDownMenuMgr.getItems();
for (int i = 0; i < items.length; i++) {
IContributionItem item = items[i];
IContributionItem newItem = item;
if (item instanceof ActionContributionItem) {
newItem = new ActionContributionItem(
((ActionContributionItem) item).getAction());
}
newItem.fill(menu, -1);
}
return menu;
}
private void createDropDownMenuMgr() {
if (dropDownMenuMgr == null) {
dropDownMenuMgr = new MenuManager();
dropDownMenuMgr.add(new InnerAction("One"));
dropDownMenuMgr.add(new InnerAction("two"));
}
}
}
}