Eclipse RCP入门(二)

本文介绍如何在 Eclipse RCP 应用中通过编程方式和配置方式添加自定义菜单项。包括修改 ApplicationActionBarAdvisor 类直接加入菜单项及使用 plugin.xml 配置新增 Action 的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eclipse RCP入门(二)

生成的代码
Application.java 程序入口点
ApplicationActionBarAdvisor.java 生成菜单
ApplicationWorkbenchAdvisor.java 选择调用哪个视图
ApplicationWorkbenchWindowAdvisor.java 设置窗口属性,显示页面
Perspective.java 视图

1、演示用修改程序的方法加入菜单一
ApplicationActionBarAdvisor.java修改为:

package com.sillycat.rpc;

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;

import com.sillycat.actions.TestAction;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

//增加的ACTION
private IWorkbenchAction testAction;

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

protected void makeActions(IWorkbenchWindow window) {
testAction = new TestAction(window);
testAction.setText("TestAction");
testAction.setId("com.sillycat.actions.TestAction");
register(testAction);
}

protected void fillMenuBar(IMenuManager menuBar) {
MenuManager newMenu = new MenuManager("FirstMenu",
"com.sillycat.menus.FirstMenu");
menuBar.add(newMenu);
newMenu.add(testAction);
}

}

增加的ACTION类TestAction.java:
package com.sillycat.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import com.sillycat.views.dialogs.FirstDialog;

public class TestAction extends Action implements IWorkbenchAction {

private IWorkbenchWindow workbenchWindow;

public TestAction(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;
}

}

该ACTION点击后打开对话框,FirstDialog.java:
package com.sillycat.views.dialogs;

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);
}

}

2、演示二,用ECLIPSE的配置方法新增一个ACTION到菜单
步骤1:打开plugin.xml文件的Extensions
步骤2:新增org.eclipse.ui.actionSets,新增com.sillycat.actions.SampleAction
保存后,会生成对应的类
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值