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
保存后,会生成对应的类
生成的代码
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
保存后,会生成对应的类